des.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * DES & Triple DES EDE key verification helpers
  4. */
  5. #ifndef __CRYPTO_INTERNAL_DES_H
  6. #define __CRYPTO_INTERNAL_DES_H
  7. #include <linux/crypto.h>
  8. #include <linux/fips.h>
  9. #include <crypto/des.h>
  10. #include <crypto/aead.h>
  11. #include <crypto/skcipher.h>
  12. /**
  13. * crypto_des_verify_key - Check whether a DES key is weak
  14. * @tfm: the crypto algo
  15. * @key: the key buffer
  16. *
  17. * Returns -EINVAL if the key is weak and the crypto TFM does not permit weak
  18. * keys. Otherwise, 0 is returned.
  19. *
  20. * It is the job of the caller to ensure that the size of the key equals
  21. * DES_KEY_SIZE.
  22. */
  23. static inline int crypto_des_verify_key(struct crypto_tfm *tfm, const u8 *key)
  24. {
  25. struct des_ctx tmp;
  26. int err;
  27. err = des_expand_key(&tmp, key, DES_KEY_SIZE);
  28. if (err == -ENOKEY) {
  29. if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
  30. err = -EINVAL;
  31. else
  32. err = 0;
  33. }
  34. memzero_explicit(&tmp, sizeof(tmp));
  35. return err;
  36. }
  37. /*
  38. * RFC2451:
  39. *
  40. * For DES-EDE3, there is no known need to reject weak or
  41. * complementation keys. Any weakness is obviated by the use of
  42. * multiple keys.
  43. *
  44. * However, if the first two or last two independent 64-bit keys are
  45. * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
  46. * same as DES. Implementers MUST reject keys that exhibit this
  47. * property.
  48. *
  49. */
  50. static inline int des3_ede_verify_key(const u8 *key, unsigned int key_len,
  51. bool check_weak)
  52. {
  53. int ret = fips_enabled ? -EINVAL : -ENOKEY;
  54. u32 K[6];
  55. memcpy(K, key, DES3_EDE_KEY_SIZE);
  56. if ((!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
  57. !((K[2] ^ K[4]) | (K[3] ^ K[5]))) &&
  58. (fips_enabled || check_weak))
  59. goto bad;
  60. if ((!((K[0] ^ K[4]) | (K[1] ^ K[5]))) && fips_enabled)
  61. goto bad;
  62. ret = 0;
  63. bad:
  64. memzero_explicit(K, DES3_EDE_KEY_SIZE);
  65. return ret;
  66. }
  67. /**
  68. * crypto_des3_ede_verify_key - Check whether a DES3-EDE key is weak
  69. * @tfm: the crypto algo
  70. * @key: the key buffer
  71. *
  72. * Returns -EINVAL if the key is weak and the crypto TFM does not permit weak
  73. * keys or when running in FIPS mode. Otherwise, 0 is returned. Note that some
  74. * keys are rejected in FIPS mode even if weak keys are permitted by the TFM
  75. * flags.
  76. *
  77. * It is the job of the caller to ensure that the size of the key equals
  78. * DES3_EDE_KEY_SIZE.
  79. */
  80. static inline int crypto_des3_ede_verify_key(struct crypto_tfm *tfm,
  81. const u8 *key)
  82. {
  83. return des3_ede_verify_key(key, DES3_EDE_KEY_SIZE,
  84. crypto_tfm_get_flags(tfm) &
  85. CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
  86. }
  87. static inline int verify_skcipher_des_key(struct crypto_skcipher *tfm,
  88. const u8 *key)
  89. {
  90. return crypto_des_verify_key(crypto_skcipher_tfm(tfm), key);
  91. }
  92. static inline int verify_skcipher_des3_key(struct crypto_skcipher *tfm,
  93. const u8 *key)
  94. {
  95. return crypto_des3_ede_verify_key(crypto_skcipher_tfm(tfm), key);
  96. }
  97. static inline int verify_aead_des_key(struct crypto_aead *tfm, const u8 *key,
  98. int keylen)
  99. {
  100. if (keylen != DES_KEY_SIZE)
  101. return -EINVAL;
  102. return crypto_des_verify_key(crypto_aead_tfm(tfm), key);
  103. }
  104. static inline int verify_aead_des3_key(struct crypto_aead *tfm, const u8 *key,
  105. int keylen)
  106. {
  107. if (keylen != DES3_EDE_KEY_SIZE)
  108. return -EINVAL;
  109. return crypto_des3_ede_verify_key(crypto_aead_tfm(tfm), key);
  110. }
  111. #endif /* __CRYPTO_INTERNAL_DES_H */