des_check_key.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Function for checking keys for the DES and Tripple DES Encryption
  5. * algorithms.
  6. *
  7. * Originally released as descore by Dana L. How <how@isl.stanford.edu>.
  8. * Modified by Raimar Falke <rf13@inf.tu-dresden.de> for the Linux-Kernel.
  9. * Derived from Cryptoapi and Nettle implementations, adapted for in-place
  10. * scatterlist interface. Changed LGPL to GPL per section 3 of the LGPL.
  11. *
  12. * s390 Version:
  13. * Copyright IBM Corp. 2003
  14. * Author(s): Thomas Spatzier
  15. * Jan Glauber (jan.glauber@de.ibm.com)
  16. *
  17. * Derived from "crypto/des.c"
  18. * Copyright (c) 1992 Dana L. How.
  19. * Copyright (c) Raimar Falke <rf13@inf.tu-dresden.de>
  20. * Copyright (c) Gisle Sflensminde <gisle@ii.uib.no>
  21. * Copyright (C) 2001 Niels Mvller.
  22. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License as published by
  26. * the Free Software Foundation; either version 2 of the License, or
  27. * (at your option) any later version.
  28. *
  29. */
  30. #include <linux/init.h>
  31. #include <linux/module.h>
  32. #include <linux/errno.h>
  33. #include <linux/crypto.h>
  34. #include "crypto_des.h"
  35. #define ROR(d,c,o) ((d) = (d) >> (c) | (d) << (o))
  36. static const u8 parity[] = {
  37. 8,1,0,8,0,8,8,0,0,8,8,0,8,0,2,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,3,
  38. 0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,
  39. 0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,
  40. 8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,
  41. 0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,
  42. 8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,
  43. 8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,
  44. 4,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,5,0,8,0,8,8,0,0,8,8,0,8,0,6,8,
  45. };
  46. /*
  47. * RFC2451: Weak key checks SHOULD be performed.
  48. */
  49. int
  50. crypto_des_check_key(const u8 *key, unsigned int keylen, u32 *flags)
  51. {
  52. u32 n, w;
  53. n = parity[key[0]]; n <<= 4;
  54. n |= parity[key[1]]; n <<= 4;
  55. n |= parity[key[2]]; n <<= 4;
  56. n |= parity[key[3]]; n <<= 4;
  57. n |= parity[key[4]]; n <<= 4;
  58. n |= parity[key[5]]; n <<= 4;
  59. n |= parity[key[6]]; n <<= 4;
  60. n |= parity[key[7]];
  61. w = 0x88888888L;
  62. if ((*flags & CRYPTO_TFM_REQ_WEAK_KEY)
  63. && !((n - (w >> 3)) & w)) { /* 1 in 10^10 keys passes this test */
  64. if (n < 0x41415151) {
  65. if (n < 0x31312121) {
  66. if (n < 0x14141515) {
  67. /* 01 01 01 01 01 01 01 01 */
  68. if (n == 0x11111111) goto weak;
  69. /* 01 1F 01 1F 01 0E 01 0E */
  70. if (n == 0x13131212) goto weak;
  71. } else {
  72. /* 01 E0 01 E0 01 F1 01 F1 */
  73. if (n == 0x14141515) goto weak;
  74. /* 01 FE 01 FE 01 FE 01 FE */
  75. if (n == 0x16161616) goto weak;
  76. }
  77. } else {
  78. if (n < 0x34342525) {
  79. /* 1F 01 1F 01 0E 01 0E 01 */
  80. if (n == 0x31312121) goto weak;
  81. /* 1F 1F 1F 1F 0E 0E 0E 0E (?) */
  82. if (n == 0x33332222) goto weak;
  83. } else {
  84. /* 1F E0 1F E0 0E F1 0E F1 */
  85. if (n == 0x34342525) goto weak;
  86. /* 1F FE 1F FE 0E FE 0E FE */
  87. if (n == 0x36362626) goto weak;
  88. }
  89. }
  90. } else {
  91. if (n < 0x61616161) {
  92. if (n < 0x44445555) {
  93. /* E0 01 E0 01 F1 01 F1 01 */
  94. if (n == 0x41415151) goto weak;
  95. /* E0 1F E0 1F F1 0E F1 0E */
  96. if (n == 0x43435252) goto weak;
  97. } else {
  98. /* E0 E0 E0 E0 F1 F1 F1 F1 (?) */
  99. if (n == 0x44445555) goto weak;
  100. /* E0 FE E0 FE F1 FE F1 FE */
  101. if (n == 0x46465656) goto weak;
  102. }
  103. } else {
  104. if (n < 0x64646565) {
  105. /* FE 01 FE 01 FE 01 FE 01 */
  106. if (n == 0x61616161) goto weak;
  107. /* FE 1F FE 1F FE 0E FE 0E */
  108. if (n == 0x63636262) goto weak;
  109. } else {
  110. /* FE E0 FE E0 FE F1 FE F1 */
  111. if (n == 0x64646565) goto weak;
  112. /* FE FE FE FE FE FE FE FE */
  113. if (n == 0x66666666) goto weak;
  114. }
  115. }
  116. }
  117. }
  118. return 0;
  119. weak:
  120. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  121. return -EINVAL;
  122. }
  123. EXPORT_SYMBOL(crypto_des_check_key);
  124. MODULE_LICENSE("GPL");
  125. MODULE_DESCRIPTION("Key Check function for DES & DES3 Cipher Algorithms");