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