Path: blob/main/crypto/openssl/providers/common/securitycheck.c
106275 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)45== RSA_FLAG_TYPE_RSASSAPSS) {46ERR_raise_data(ERR_LIB_PROV,47PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE,48"operation: %d", operation);49return 0;50}51break;52default:53ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,54"invalid operation: %d", operation);55return 0;56}57*outprotect = protect;58return 1;59}6061/*62* FIPS requires a minimum security strength of 112 bits (for encryption or63* signing), and for legacy purposes 80 bits (for decryption or verifying).64* Set protect = 1 for encryption or signing operations, or 0 otherwise. See65* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf.66*/67int ossl_rsa_check_key_size(const RSA *rsa, int protect)68{69int sz = RSA_bits(rsa);7071if (protect ? (sz < 2048) : (sz < 1024))72return 0;73return 1;74}7576/*77* FIPS requires a minimum security strength of 112 bits for key-derivation key.78* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf.79*/80int ossl_kdf_check_key_size(size_t keylen)81{82return (keylen * 8) >= OSSL_FIPS_MIN_SECURITY_STRENGTH_BITS;83}8485int ossl_mac_check_key_size(size_t keylen)86{87return ossl_kdf_check_key_size(keylen);88}8990#ifndef OPENSSL_NO_EC9192int ossl_ec_check_curve_allowed(const EC_GROUP *group)93{94const char *curve_name;95int nid = EC_GROUP_get_curve_name(group);9697/* Explicit curves are not FIPS approved */98if (nid == NID_undef)99return 0;100/* Only NIST curves are FIPS approved */101curve_name = EC_curve_nid2nist(nid);102if (curve_name == NULL)103return 0;104return 1;105}106107/*108* In FIPS mode:109* protect should be 1 for any operations that need 112 bits of security110* strength (such as signing, and key exchange), or 0 for operations that allow111* a lower security strength (such as verify).112*113* For ECDH key agreement refer to SP800-56A114* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf115* "Appendix D"116*117* For ECDSA signatures refer to118* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf119* "Table 2"120*/121int ossl_ec_check_security_strength(const EC_GROUP *group, int protect)122{123/*124* For EC the security strength is the (order_bits / 2)125* e.g. P-224 is 112 bits.126*/127int strength = EC_GROUP_order_bits(group) / 2;128/* The min security strength allowed for legacy verification is 80 bits */129if (strength < 80)130return 0;131/*132* For signing or key agreement only allow curves with at least 112 bits of133* security strength134*/135if (protect && strength < OSSL_FIPS_MIN_SECURITY_STRENGTH_BITS)136return 0;137return 1;138}139140#endif /* OPENSSL_NO_EC */141142#ifndef OPENSSL_NO_DSA143/*144* Check for valid key sizes if fips mode. Refer to145* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf146* "Table 2"147*/148int ossl_dsa_check_key(const DSA *dsa, int sign)149{150size_t L, N;151const BIGNUM *p, *q;152153if (dsa == NULL)154return 0;155156p = DSA_get0_p(dsa);157q = DSA_get0_q(dsa);158if (p == NULL || q == NULL)159return 0;160161L = BN_num_bits(p);162N = BN_num_bits(q);163164/*165* For Digital signature verification DSA keys with < 112 bits of166* security strength, are still allowed for legacy167* use. The bounds given in SP 800-131Ar2 - Table 2 are168* (512 <= L < 2048 or 160 <= N < 224).169*170* We are a little stricter and insist that both minimums are met.171* For example a L = 256, N = 160 key *would* be allowed by SP 800-131Ar2172* but we don't.173*/174if (!sign) {175if (L < 512 || N < 160)176return 0;177if (L < 2048 || N < 224)178return 1;179}180181/* Valid sizes for both sign and verify */182if (L == 2048 && (N == 224 || N == 256)) /* 112 bits */183return 1;184return (L == 3072 && N == 256); /* 128 bits */185}186#endif /* OPENSSL_NO_DSA */187188#ifndef OPENSSL_NO_DH189/*190* For DH key agreement refer to SP800-56A191* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf192* "Section 5.5.1.1FFC Domain Parameter Selection/Generation" and193* "Appendix D" FFC Safe-prime Groups194*/195int ossl_dh_check_key(const DH *dh)196{197size_t L, N;198const BIGNUM *p, *q;199200if (dh == NULL)201return 0;202203p = DH_get0_p(dh);204q = DH_get0_q(dh);205if (p == NULL || q == NULL)206return 0;207208L = BN_num_bits(p);209if (L < 2048)210return 0;211212/* If it is a safe prime group then it is ok */213if (DH_get_nid(dh))214return 1;215216/* If not then it must be FFC, which only allows certain sizes. */217N = BN_num_bits(q);218219return (L == 2048 && (N == 224 || N == 256));220}221#endif /* OPENSSL_NO_DH */222223224