securitycheck.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 2020 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/err.h>
  15. #include <openssl/core_names.h>
  16. #include <openssl/obj_mac.h>
  17. #include "prov/securitycheck.h"
  18. #include "prov/providercommonerr.h"
  19. /*
  20. * FIPS requires a minimum security strength of 112 bits (for encryption or
  21. * signing), and for legacy purposes 80 bits (for decryption or verifying).
  22. * Set protect = 1 for encryption or signing operations, or 0 otherwise. See
  23. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf.
  24. */
  25. int ossl_rsa_check_key(const RSA *rsa, int protect)
  26. {
  27. #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
  28. if (securitycheck_enabled()) {
  29. int sz = RSA_bits(rsa);
  30. return protect ? (sz >= 2048) : (sz >= 1024);
  31. }
  32. #endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
  33. return 1;
  34. }
  35. #ifndef OPENSSL_NO_EC
  36. /*
  37. * In FIPS mode:
  38. * protect should be 1 for any operations that need 112 bits of security
  39. * strength (such as signing, and key exchange), or 0 for operations that allow
  40. * a lower security strength (such as verify).
  41. *
  42. * For ECDH key agreement refer to SP800-56A
  43. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf
  44. * "Appendix D"
  45. *
  46. * For ECDSA signatures refer to
  47. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf
  48. * "Table 2"
  49. */
  50. int ec_check_key(const EC_KEY *ec, int protect)
  51. {
  52. # if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
  53. if (securitycheck_enabled()) {
  54. int nid, strength;
  55. const char *curve_name;
  56. const EC_GROUP *group = EC_KEY_get0_group(ec);
  57. if (group == NULL) {
  58. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_CURVE, "No group");
  59. return 0;
  60. }
  61. nid = EC_GROUP_get_curve_name(group);
  62. if (nid == NID_undef) {
  63. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_CURVE,
  64. "Explicit curves are not allowed in fips mode");
  65. return 0;
  66. }
  67. curve_name = EC_curve_nid2nist(nid);
  68. if (curve_name == NULL) {
  69. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_CURVE,
  70. "Curve %s is not approved in FIPS mode", curve_name);
  71. return 0;
  72. }
  73. /*
  74. * For EC the security strength is the (order_bits / 2)
  75. * e.g. P-224 is 112 bits.
  76. */
  77. strength = EC_GROUP_order_bits(group) / 2;
  78. /* The min security strength allowed for legacy verification is 80 bits */
  79. if (strength < 80) {
  80. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
  81. return 0;
  82. }
  83. /*
  84. * For signing or key agreement only allow curves with at least 112 bits of
  85. * security strength
  86. */
  87. if (protect && strength < 112) {
  88. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_CURVE,
  89. "Curve %s cannot be used for signing", curve_name);
  90. return 0;
  91. }
  92. }
  93. # endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
  94. return 1;
  95. }
  96. #endif /* OPENSSL_NO_EC */
  97. #ifndef OPENSSL_NO_DSA
  98. /*
  99. * Check for valid key sizes if fips mode. Refer to
  100. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf
  101. * "Table 2"
  102. */
  103. int dsa_check_key(const DSA *dsa, int sign)
  104. {
  105. # if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
  106. if (securitycheck_enabled()) {
  107. size_t L, N;
  108. const BIGNUM *p, *q;
  109. if (dsa == NULL)
  110. return 0;
  111. p = DSA_get0_p(dsa);
  112. q = DSA_get0_q(dsa);
  113. if (p == NULL || q == NULL)
  114. return 0;
  115. L = BN_num_bits(p);
  116. N = BN_num_bits(q);
  117. /*
  118. * For Digital signature verification DSA keys with < 112 bits of
  119. * security strength (i.e L < 2048 bits), are still allowed for legacy
  120. * use. The bounds given in SP800 131Ar2 - Table 2 are
  121. * (512 <= L < 2048 and 160 <= N < 224)
  122. */
  123. if (!sign && L < 2048)
  124. return (L >= 512 && N >= 160 && N < 224);
  125. /* Valid sizes for both sign and verify */
  126. if (L == 2048 && (N == 224 || N == 256))
  127. return 1;
  128. return (L == 3072 && N == 256);
  129. }
  130. # endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
  131. return 1;
  132. }
  133. #endif /* OPENSSL_NO_DSA */
  134. #ifndef OPENSSL_NO_DH
  135. /*
  136. * For DH key agreement refer to SP800-56A
  137. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf
  138. * "Section 5.5.1.1FFC Domain Parameter Selection/Generation" and
  139. * "Appendix D" FFC Safe-prime Groups
  140. */
  141. int dh_check_key(const DH *dh)
  142. {
  143. # if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
  144. if (securitycheck_enabled()) {
  145. size_t L, N;
  146. const BIGNUM *p, *q;
  147. if (dh == NULL)
  148. return 0;
  149. p = DH_get0_p(dh);
  150. q = DH_get0_q(dh);
  151. if (p == NULL || q == NULL)
  152. return 0;
  153. L = BN_num_bits(p);
  154. if (L < 2048)
  155. return 0;
  156. /* If it is a safe prime group then it is ok */
  157. if (DH_get_nid(dh))
  158. return 1;
  159. /* If not then it must be FFC, which only allows certain sizes. */
  160. N = BN_num_bits(q);
  161. return (L == 2048 && (N == 224 || N == 256));
  162. }
  163. # endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
  164. return 1;
  165. }
  166. #endif /* OPENSSL_NO_DH */
  167. int digest_get_approved_nid_with_sha1(const EVP_MD *md, int sha1_allowed)
  168. {
  169. int mdnid = digest_get_approved_nid(md);
  170. # if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
  171. if (securitycheck_enabled()) {
  172. if (mdnid == NID_sha1 && !sha1_allowed)
  173. mdnid = NID_undef;
  174. }
  175. # endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
  176. return mdnid;
  177. }
  178. int digest_is_allowed(const EVP_MD *md)
  179. {
  180. # if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
  181. if (securitycheck_enabled())
  182. return digest_get_approved_nid(md) != NID_undef;
  183. # endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
  184. return 1;
  185. }