Path: blob/main/crypto/openssl/providers/common/securitycheck.c
48261 views
/*1* Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include "internal/deprecated.h"1011#include <openssl/rsa.h>12#include <openssl/dsa.h>13#include <openssl/dh.h>14#include <openssl/ec.h>15#include <openssl/evp.h>16#include <openssl/err.h>17#include <openssl/proverr.h>18#include <openssl/core_names.h>19#include <openssl/obj_mac.h>20#include "prov/securitycheck.h"2122#define OSSL_FIPS_MIN_SECURITY_STRENGTH_BITS 1122324int ossl_rsa_key_op_get_protect(const RSA *rsa, int operation, int *outprotect)25{26int protect = 0;2728switch (operation) {29case EVP_PKEY_OP_SIGN:30case EVP_PKEY_OP_SIGNMSG:31protect = 1;32/* fallthrough */33case EVP_PKEY_OP_VERIFY:34case EVP_PKEY_OP_VERIFYMSG:35break;36case EVP_PKEY_OP_ENCAPSULATE:37case EVP_PKEY_OP_ENCRYPT:38protect = 1;39/* fallthrough */40case EVP_PKEY_OP_VERIFYRECOVER:41case EVP_PKEY_OP_DECAPSULATE:42case EVP_PKEY_OP_DECRYPT:43if (RSA_test_flags(rsa,44RSA_FLAG_TYPE_MASK) == RSA_FLAG_TYPE_RSASSAPSS) {45ERR_raise_data(ERR_LIB_PROV,46PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE,47"operation: %d", operation);48return 0;49}50break;51default:52ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,53"invalid operation: %d", operation);54return 0;55}56*outprotect = protect;57return 1;58}5960/*61* FIPS requires a minimum security strength of 112 bits (for encryption or62* signing), and for legacy purposes 80 bits (for decryption or verifying).63* Set protect = 1 for encryption or signing operations, or 0 otherwise. See64* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf.65*/66int ossl_rsa_check_key_size(const RSA *rsa, int protect)67{68int sz = RSA_bits(rsa);6970if (protect ? (sz < 2048) : (sz < 1024))71return 0;72return 1;73}7475/*76* FIPS requires a minimum security strength of 112 bits for key-derivation key.77* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf.78*/79int ossl_kdf_check_key_size(size_t keylen)80{81return (keylen * 8) >= OSSL_FIPS_MIN_SECURITY_STRENGTH_BITS;82}8384int ossl_mac_check_key_size(size_t keylen)85{86return ossl_kdf_check_key_size(keylen);87}8889#ifndef OPENSSL_NO_EC9091int ossl_ec_check_curve_allowed(const EC_GROUP *group)92{93const char *curve_name;94int nid = EC_GROUP_get_curve_name(group);9596/* Explicit curves are not FIPS approved */97if (nid == NID_undef)98return 0;99/* Only NIST curves are FIPS approved */100curve_name = EC_curve_nid2nist(nid);101if (curve_name == NULL)102return 0;103return 1;104}105106/*107* In FIPS mode:108* protect should be 1 for any operations that need 112 bits of security109* strength (such as signing, and key exchange), or 0 for operations that allow110* a lower security strength (such as verify).111*112* For ECDH key agreement refer to SP800-56A113* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf114* "Appendix D"115*116* For ECDSA signatures refer to117* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf118* "Table 2"119*/120int ossl_ec_check_security_strength(const EC_GROUP *group, int protect)121{122/*123* For EC the security strength is the (order_bits / 2)124* e.g. P-224 is 112 bits.125*/126int strength = EC_GROUP_order_bits(group) / 2;127/* The min security strength allowed for legacy verification is 80 bits */128if (strength < 80)129return 0;130/*131* For signing or key agreement only allow curves with at least 112 bits of132* security strength133*/134if (protect && strength < OSSL_FIPS_MIN_SECURITY_STRENGTH_BITS)135return 0;136return 1;137}138139#endif /* OPENSSL_NO_EC */140141#ifndef OPENSSL_NO_DSA142/*143* Check for valid key sizes if fips mode. Refer to144* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf145* "Table 2"146*/147int ossl_dsa_check_key(const DSA *dsa, int sign)148{149size_t L, N;150const BIGNUM *p, *q;151152if (dsa == NULL)153return 0;154155p = DSA_get0_p(dsa);156q = DSA_get0_q(dsa);157if (p == NULL || q == NULL)158return 0;159160L = BN_num_bits(p);161N = BN_num_bits(q);162163/*164* For Digital signature verification DSA keys with < 112 bits of165* security strength, are still allowed for legacy166* use. The bounds given in SP 800-131Ar2 - Table 2 are167* (512 <= L < 2048 or 160 <= N < 224).168*169* We are a little stricter and insist that both minimums are met.170* For example a L = 256, N = 160 key *would* be allowed by SP 800-131Ar2171* but we don't.172*/173if (!sign) {174if (L < 512 || N < 160)175return 0;176if (L < 2048 || N < 224)177return 1;178}179180/* Valid sizes for both sign and verify */181if (L == 2048 && (N == 224 || N == 256)) /* 112 bits */182return 1;183return (L == 3072 && N == 256); /* 128 bits */184}185#endif /* OPENSSL_NO_DSA */186187#ifndef OPENSSL_NO_DH188/*189* For DH key agreement refer to SP800-56A190* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf191* "Section 5.5.1.1FFC Domain Parameter Selection/Generation" and192* "Appendix D" FFC Safe-prime Groups193*/194int ossl_dh_check_key(const DH *dh)195{196size_t L, N;197const BIGNUM *p, *q;198199if (dh == NULL)200return 0;201202p = DH_get0_p(dh);203q = DH_get0_q(dh);204if (p == NULL || q == NULL)205return 0;206207L = BN_num_bits(p);208if (L < 2048)209return 0;210211/* If it is a safe prime group then it is ok */212if (DH_get_nid(dh))213return 1;214215/* If not then it must be FFC, which only allows certain sizes. */216N = BN_num_bits(q);217218return (L == 2048 && (N == 224 || N == 256));219}220#endif /* OPENSSL_NO_DH */221222223