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