securitycheck.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/deprecated.h"
  10. #include <openssl/rsa.h>
  11. #include <openssl/dsa.h>
  12. #include <openssl/dh.h>
  13. #include <openssl/ec.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/err.h>
  16. #include <openssl/proverr.h>
  17. #include <openssl/core_names.h>
  18. #include <openssl/obj_mac.h>
  19. #include "prov/securitycheck.h"
  20. #define OSSL_FIPS_MIN_SECURITY_STRENGTH_BITS 112
  21. int ossl_rsa_key_op_get_protect(const RSA *rsa, int operation, int *outprotect)
  22. {
  23. int protect = 0;
  24. switch (operation) {
  25. case EVP_PKEY_OP_SIGN:
  26. case EVP_PKEY_OP_SIGNMSG:
  27. protect = 1;
  28. /* fallthrough */
  29. case EVP_PKEY_OP_VERIFY:
  30. case EVP_PKEY_OP_VERIFYMSG:
  31. break;
  32. case EVP_PKEY_OP_ENCAPSULATE:
  33. case EVP_PKEY_OP_ENCRYPT:
  34. protect = 1;
  35. /* fallthrough */
  36. case EVP_PKEY_OP_VERIFYRECOVER:
  37. case EVP_PKEY_OP_DECAPSULATE:
  38. case EVP_PKEY_OP_DECRYPT:
  39. if (RSA_test_flags(rsa,
  40. RSA_FLAG_TYPE_MASK) == RSA_FLAG_TYPE_RSASSAPSS) {
  41. ERR_raise_data(ERR_LIB_PROV,
  42. PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE,
  43. "operation: %d", operation);
  44. return 0;
  45. }
  46. break;
  47. default:
  48. ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
  49. "invalid operation: %d", operation);
  50. return 0;
  51. }
  52. *outprotect = protect;
  53. return 1;
  54. }
  55. /*
  56. * FIPS requires a minimum security strength of 112 bits (for encryption or
  57. * signing), and for legacy purposes 80 bits (for decryption or verifying).
  58. * Set protect = 1 for encryption or signing operations, or 0 otherwise. See
  59. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf.
  60. */
  61. int ossl_rsa_check_key_size(const RSA *rsa, int protect)
  62. {
  63. int sz = RSA_bits(rsa);
  64. if (protect ? (sz < 2048) : (sz < 1024))
  65. return 0;
  66. return 1;
  67. }
  68. /*
  69. * FIPS requires a minimum security strength of 112 bits for key-derivation key.
  70. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf.
  71. */
  72. int ossl_kdf_check_key_size(size_t keylen)
  73. {
  74. return (keylen * 8) >= OSSL_FIPS_MIN_SECURITY_STRENGTH_BITS;
  75. }
  76. int ossl_mac_check_key_size(size_t keylen)
  77. {
  78. return ossl_kdf_check_key_size(keylen);
  79. }
  80. #ifndef OPENSSL_NO_EC
  81. int ossl_ec_check_curve_allowed(const EC_GROUP *group)
  82. {
  83. const char *curve_name;
  84. int nid = EC_GROUP_get_curve_name(group);
  85. /* Explicit curves are not FIPS approved */
  86. if (nid == NID_undef)
  87. return 0;
  88. /* Only NIST curves are FIPS approved */
  89. curve_name = EC_curve_nid2nist(nid);
  90. if (curve_name == NULL)
  91. return 0;
  92. return 1;
  93. }
  94. /*
  95. * In FIPS mode:
  96. * protect should be 1 for any operations that need 112 bits of security
  97. * strength (such as signing, and key exchange), or 0 for operations that allow
  98. * a lower security strength (such as verify).
  99. *
  100. * For ECDH key agreement refer to SP800-56A
  101. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf
  102. * "Appendix D"
  103. *
  104. * For ECDSA signatures refer to
  105. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf
  106. * "Table 2"
  107. */
  108. int ossl_ec_check_security_strength(const EC_GROUP *group, int protect)
  109. {
  110. /*
  111. * For EC the security strength is the (order_bits / 2)
  112. * e.g. P-224 is 112 bits.
  113. */
  114. int strength = EC_GROUP_order_bits(group) / 2;
  115. /* The min security strength allowed for legacy verification is 80 bits */
  116. if (strength < 80)
  117. return 0;
  118. /*
  119. * For signing or key agreement only allow curves with at least 112 bits of
  120. * security strength
  121. */
  122. if (protect && strength < OSSL_FIPS_MIN_SECURITY_STRENGTH_BITS)
  123. return 0;
  124. return 1;
  125. }
  126. #endif /* OPENSSL_NO_EC */
  127. #ifndef OPENSSL_NO_DSA
  128. /*
  129. * Check for valid key sizes if fips mode. Refer to
  130. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf
  131. * "Table 2"
  132. */
  133. int ossl_dsa_check_key(const DSA *dsa, int sign)
  134. {
  135. size_t L, N;
  136. const BIGNUM *p, *q;
  137. if (dsa == NULL)
  138. return 0;
  139. p = DSA_get0_p(dsa);
  140. q = DSA_get0_q(dsa);
  141. if (p == NULL || q == NULL)
  142. return 0;
  143. L = BN_num_bits(p);
  144. N = BN_num_bits(q);
  145. /*
  146. * For Digital signature verification DSA keys with < 112 bits of
  147. * security strength, are still allowed for legacy
  148. * use. The bounds given in SP 800-131Ar2 - Table 2 are
  149. * (512 <= L < 2048 or 160 <= N < 224).
  150. *
  151. * We are a little stricter and insist that both minimums are met.
  152. * For example a L = 256, N = 160 key *would* be allowed by SP 800-131Ar2
  153. * but we don't.
  154. */
  155. if (!sign) {
  156. if (L < 512 || N < 160)
  157. return 0;
  158. if (L < 2048 || N < 224)
  159. return 1;
  160. }
  161. /* Valid sizes for both sign and verify */
  162. if (L == 2048 && (N == 224 || N == 256)) /* 112 bits */
  163. return 1;
  164. return (L == 3072 && N == 256); /* 128 bits */
  165. }
  166. #endif /* OPENSSL_NO_DSA */
  167. #ifndef OPENSSL_NO_DH
  168. /*
  169. * For DH key agreement refer to SP800-56A
  170. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf
  171. * "Section 5.5.1.1FFC Domain Parameter Selection/Generation" and
  172. * "Appendix D" FFC Safe-prime Groups
  173. */
  174. int ossl_dh_check_key(const DH *dh)
  175. {
  176. size_t L, N;
  177. const BIGNUM *p, *q;
  178. if (dh == NULL)
  179. return 0;
  180. p = DH_get0_p(dh);
  181. q = DH_get0_q(dh);
  182. if (p == NULL || q == NULL)
  183. return 0;
  184. L = BN_num_bits(p);
  185. if (L < 2048)
  186. return 0;
  187. /* If it is a safe prime group then it is ok */
  188. if (DH_get_nid(dh))
  189. return 1;
  190. /* If not then it must be FFC, which only allows certain sizes. */
  191. N = BN_num_bits(q);
  192. return (L == 2048 && (N == 224 || N == 256));
  193. }
  194. #endif /* OPENSSL_NO_DH */