des_check_key.c 3.9 KB

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