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