/src/openssl/crypto/rsa/rsa_sp800_56b_check.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2018-2024 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  * Copyright (c) 2018-2019, Oracle and/or its affiliates.  All rights reserved.  | 
4  |  |  *  | 
5  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use  | 
6  |  |  * this file except in compliance with the License.  You can obtain a copy  | 
7  |  |  * in the file LICENSE in the source distribution or at  | 
8  |  |  * https://www.openssl.org/source/license.html  | 
9  |  |  */  | 
10  |  |  | 
11  |  | #include <openssl/err.h>  | 
12  |  | #include <openssl/bn.h>  | 
13  |  | #include "crypto/bn.h"  | 
14  |  | #include "rsa_local.h"  | 
15  |  |  | 
16  |  | /*  | 
17  |  |  * Part of the RSA keypair test.  | 
18  |  |  * Check the Chinese Remainder Theorem components are valid.  | 
19  |  |  *  | 
20  |  |  * See SP800-5bBr1  | 
21  |  |  *   6.4.1.2.3: rsakpv1-crt Step 7  | 
22  |  |  *   6.4.1.3.3: rsakpv2-crt Step 7  | 
23  |  |  */  | 
24  |  | int ossl_rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx)  | 
25  | 0  | { | 
26  | 0  |     int ret = 0;  | 
27  | 0  |     BIGNUM *r = NULL, *p1 = NULL, *q1 = NULL;  | 
28  |  |  | 
29  |  |     /* check if only some of the crt components are set */  | 
30  | 0  |     if (rsa->dmp1 == NULL || rsa->dmq1 == NULL || rsa->iqmp == NULL) { | 
31  | 0  |         if (rsa->dmp1 != NULL || rsa->dmq1 != NULL || rsa->iqmp != NULL)  | 
32  | 0  |             return 0;  | 
33  | 0  |         return 1; /* return ok if all components are NULL */  | 
34  | 0  |     }  | 
35  |  |  | 
36  | 0  |     BN_CTX_start(ctx);  | 
37  | 0  |     r = BN_CTX_get(ctx);  | 
38  | 0  |     p1 = BN_CTX_get(ctx);  | 
39  | 0  |     q1 = BN_CTX_get(ctx);  | 
40  | 0  |     if (q1 != NULL) { | 
41  | 0  |         BN_set_flags(r, BN_FLG_CONSTTIME);  | 
42  | 0  |         BN_set_flags(p1, BN_FLG_CONSTTIME);  | 
43  | 0  |         BN_set_flags(q1, BN_FLG_CONSTTIME);  | 
44  | 0  |         ret = 1;  | 
45  | 0  |     } else { | 
46  | 0  |         ret = 0;  | 
47  | 0  |     }  | 
48  | 0  |     ret = ret  | 
49  |  |           /* p1 = p -1 */  | 
50  | 0  |           && (BN_copy(p1, rsa->p) != NULL)  | 
51  | 0  |           && BN_sub_word(p1, 1)  | 
52  |  |           /* q1 = q - 1 */  | 
53  | 0  |           && (BN_copy(q1, rsa->q) != NULL)  | 
54  | 0  |           && BN_sub_word(q1, 1)  | 
55  |  |           /* (a) 1 < dP < (p – 1). */  | 
56  | 0  |           && (BN_cmp(rsa->dmp1, BN_value_one()) > 0)  | 
57  | 0  |           && (BN_cmp(rsa->dmp1, p1) < 0)  | 
58  |  |           /* (b) 1 < dQ < (q - 1). */  | 
59  | 0  |           && (BN_cmp(rsa->dmq1, BN_value_one()) > 0)  | 
60  | 0  |           && (BN_cmp(rsa->dmq1, q1) < 0)  | 
61  |  |           /* (c) 1 < qInv < p */  | 
62  | 0  |           && (BN_cmp(rsa->iqmp, BN_value_one()) > 0)  | 
63  | 0  |           && (BN_cmp(rsa->iqmp, rsa->p) < 0)  | 
64  |  |           /* (d) 1 = (dP . e) mod (p - 1)*/  | 
65  | 0  |           && BN_mod_mul(r, rsa->dmp1, rsa->e, p1, ctx)  | 
66  | 0  |           && BN_is_one(r)  | 
67  |  |           /* (e) 1 = (dQ . e) mod (q - 1) */  | 
68  | 0  |           && BN_mod_mul(r, rsa->dmq1, rsa->e, q1, ctx)  | 
69  | 0  |           && BN_is_one(r)  | 
70  |  |           /* (f) 1 = (qInv . q) mod p */  | 
71  | 0  |           && BN_mod_mul(r, rsa->iqmp, rsa->q, rsa->p, ctx)  | 
72  | 0  |           && BN_is_one(r);  | 
73  | 0  |     BN_clear(r);  | 
74  | 0  |     BN_clear(p1);  | 
75  | 0  |     BN_clear(q1);  | 
76  | 0  |     BN_CTX_end(ctx);  | 
77  | 0  |     return ret;  | 
78  | 0  | }  | 
79  |  |  | 
80  |  | /*  | 
81  |  |  * Part of the RSA keypair test.  | 
82  |  |  * Check that (√2)(2^(nbits/2 - 1) <= p <= 2^(nbits/2) - 1  | 
83  |  |  *  | 
84  |  |  * See SP800-5bBr1 6.4.1.2.1 Part 5 (c) & (g) - used for both p and q.  | 
85  |  |  *  | 
86  |  |  * (√2)(2^(nbits/2 - 1) = (√2/2)(2^(nbits/2))  | 
87  |  |  */  | 
88  |  | int ossl_rsa_check_prime_factor_range(const BIGNUM *p, int nbits, BN_CTX *ctx)  | 
89  | 0  | { | 
90  | 0  |     int ret = 0;  | 
91  | 0  |     BIGNUM *low;  | 
92  | 0  |     int shift;  | 
93  |  | 
  | 
94  | 0  |     nbits >>= 1;  | 
95  | 0  |     shift = nbits - BN_num_bits(&ossl_bn_inv_sqrt_2);  | 
96  |  |  | 
97  |  |     /* Upper bound check */  | 
98  | 0  |     if (BN_num_bits(p) != nbits)  | 
99  | 0  |         return 0;  | 
100  |  |  | 
101  | 0  |     BN_CTX_start(ctx);  | 
102  | 0  |     low = BN_CTX_get(ctx);  | 
103  | 0  |     if (low == NULL)  | 
104  | 0  |         goto err;  | 
105  |  |  | 
106  |  |     /* set low = (√2)(2^(nbits/2 - 1) */  | 
107  | 0  |     if (!BN_copy(low, &ossl_bn_inv_sqrt_2))  | 
108  | 0  |         goto err;  | 
109  |  |  | 
110  | 0  |     if (shift >= 0) { | 
111  |  |         /*  | 
112  |  |          * We don't have all the bits. ossl_bn_inv_sqrt_2 contains a rounded up  | 
113  |  |          * value, so there is a very low probability that we'll reject a valid  | 
114  |  |          * value.  | 
115  |  |          */  | 
116  | 0  |         if (!BN_lshift(low, low, shift))  | 
117  | 0  |             goto err;  | 
118  | 0  |     } else if (!BN_rshift(low, low, -shift)) { | 
119  | 0  |         goto err;  | 
120  | 0  |     }  | 
121  | 0  |     if (BN_cmp(p, low) <= 0)  | 
122  | 0  |         goto err;  | 
123  | 0  |     ret = 1;  | 
124  | 0  | err:  | 
125  | 0  |     BN_CTX_end(ctx);  | 
126  | 0  |     return ret;  | 
127  | 0  | }  | 
128  |  |  | 
129  |  | /*  | 
130  |  |  * Part of the RSA keypair test.  | 
131  |  |  * Check the prime factor (for either p or q)  | 
132  |  |  * i.e: p is prime AND GCD(p - 1, e) = 1  | 
133  |  |  *  | 
134  |  |  * See SP800-56Br1 6.4.1.2.3 Step 5 (a to d) & (e to h).  | 
135  |  |  */  | 
136  |  | int ossl_rsa_check_prime_factor(BIGNUM *p, BIGNUM *e, int nbits, BN_CTX *ctx)  | 
137  | 0  | { | 
138  | 0  |     int ret = 0;  | 
139  | 0  |     BIGNUM *p1 = NULL, *gcd = NULL;  | 
140  |  |  | 
141  |  |     /* (Steps 5 a-b) prime test */  | 
142  | 0  |     if (BN_check_prime(p, ctx, NULL) != 1  | 
143  |  |             /* (Step 5c) (√2)(2^(nbits/2 - 1) <= p <= 2^(nbits/2 - 1) */  | 
144  | 0  |             || ossl_rsa_check_prime_factor_range(p, nbits, ctx) != 1)  | 
145  | 0  |         return 0;  | 
146  |  |  | 
147  | 0  |     BN_CTX_start(ctx);  | 
148  | 0  |     p1 = BN_CTX_get(ctx);  | 
149  | 0  |     gcd = BN_CTX_get(ctx);  | 
150  | 0  |     if (gcd != NULL) { | 
151  | 0  |         BN_set_flags(p1, BN_FLG_CONSTTIME);  | 
152  | 0  |         BN_set_flags(gcd, BN_FLG_CONSTTIME);  | 
153  | 0  |         ret = 1;  | 
154  | 0  |     } else { | 
155  | 0  |         ret = 0;  | 
156  | 0  |     }  | 
157  | 0  |     ret = ret  | 
158  |  |           /* (Step 5d) GCD(p-1, e) = 1 */  | 
159  | 0  |           && (BN_copy(p1, p) != NULL)  | 
160  | 0  |           && BN_sub_word(p1, 1)  | 
161  | 0  |           && BN_gcd(gcd, p1, e, ctx)  | 
162  | 0  |           && BN_is_one(gcd);  | 
163  |  | 
  | 
164  | 0  |     BN_clear(p1);  | 
165  | 0  |     BN_CTX_end(ctx);  | 
166  | 0  |     return ret;  | 
167  | 0  | }  | 
168  |  |  | 
169  |  | /*  | 
170  |  |  * See SP800-56Br1 6.4.1.2.3 Part 6(a-b) Check the private exponent d  | 
171  |  |  * satisfies:  | 
172  |  |  *     (Step 6a) 2^(nBit/2) < d < LCM(p–1, q–1).  | 
173  |  |  *     (Step 6b) 1 = (d*e) mod LCM(p–1, q–1)  | 
174  |  |  */  | 
175  |  | int ossl_rsa_check_private_exponent(const RSA *rsa, int nbits, BN_CTX *ctx)  | 
176  | 0  | { | 
177  | 0  |     int ret;  | 
178  | 0  |     BIGNUM *r, *p1, *q1, *lcm, *p1q1, *gcd;  | 
179  |  |  | 
180  |  |     /* (Step 6a) 2^(nbits/2) < d */  | 
181  | 0  |     if (BN_num_bits(rsa->d) <= (nbits >> 1))  | 
182  | 0  |         return 0;  | 
183  |  |  | 
184  | 0  |     BN_CTX_start(ctx);  | 
185  | 0  |     r = BN_CTX_get(ctx);  | 
186  | 0  |     p1 = BN_CTX_get(ctx);  | 
187  | 0  |     q1 = BN_CTX_get(ctx);  | 
188  | 0  |     lcm = BN_CTX_get(ctx);  | 
189  | 0  |     p1q1 = BN_CTX_get(ctx);  | 
190  | 0  |     gcd = BN_CTX_get(ctx);  | 
191  | 0  |     if (gcd != NULL) { | 
192  | 0  |         BN_set_flags(r, BN_FLG_CONSTTIME);  | 
193  | 0  |         BN_set_flags(p1, BN_FLG_CONSTTIME);  | 
194  | 0  |         BN_set_flags(q1, BN_FLG_CONSTTIME);  | 
195  | 0  |         BN_set_flags(lcm, BN_FLG_CONSTTIME);  | 
196  | 0  |         BN_set_flags(p1q1, BN_FLG_CONSTTIME);  | 
197  | 0  |         BN_set_flags(gcd, BN_FLG_CONSTTIME);  | 
198  | 0  |         ret = 1;  | 
199  | 0  |     } else { | 
200  | 0  |         ret = 0;  | 
201  | 0  |     }  | 
202  | 0  |     ret = (ret  | 
203  |  |           /* LCM(p - 1, q - 1) */  | 
204  | 0  |           && (ossl_rsa_get_lcm(ctx, rsa->p, rsa->q, lcm, gcd, p1, q1,  | 
205  | 0  |                                p1q1) == 1)  | 
206  |  |           /* (Step 6a) d < LCM(p - 1, q - 1) */  | 
207  | 0  |           && (BN_cmp(rsa->d, lcm) < 0)  | 
208  |  |           /* (Step 6b) 1 = (e . d) mod LCM(p - 1, q - 1) */  | 
209  | 0  |           && BN_mod_mul(r, rsa->e, rsa->d, lcm, ctx)  | 
210  | 0  |           && BN_is_one(r));  | 
211  |  | 
  | 
212  | 0  |     BN_clear(r);  | 
213  | 0  |     BN_clear(p1);  | 
214  | 0  |     BN_clear(q1);  | 
215  | 0  |     BN_clear(lcm);  | 
216  | 0  |     BN_clear(gcd);  | 
217  | 0  |     BN_CTX_end(ctx);  | 
218  | 0  |     return ret;  | 
219  | 0  | }  | 
220  |  |  | 
221  |  | /*  | 
222  |  |  * Check exponent is odd.  | 
223  |  |  * For FIPS also check the bit length is in the range [17..256]  | 
224  |  |  */  | 
225  |  | int ossl_rsa_check_public_exponent(const BIGNUM *e)  | 
226  | 0  | { | 
227  |  | #ifdef FIPS_MODULE  | 
228  |  |     int bitlen;  | 
229  |  |  | 
230  |  |     bitlen = BN_num_bits(e);  | 
231  |  |     return (BN_is_odd(e) && bitlen > 16 && bitlen < 257);  | 
232  |  | #else  | 
233  |  |     /* Allow small exponents larger than 1 for legacy purposes */  | 
234  | 0  |     return BN_is_odd(e) && BN_cmp(e, BN_value_one()) > 0;  | 
235  | 0  | #endif /* FIPS_MODULE */  | 
236  | 0  | }  | 
237  |  |  | 
238  |  | /*  | 
239  |  |  * SP800-56Br1 6.4.1.2.1 (Step 5i): |p - q| > 2^(nbits/2 - 100)  | 
240  |  |  * i.e- numbits(p-q-1) > (nbits/2 -100)  | 
241  |  |  */  | 
242  |  | int ossl_rsa_check_pminusq_diff(BIGNUM *diff, const BIGNUM *p, const BIGNUM *q,  | 
243  |  |                            int nbits)  | 
244  | 0  | { | 
245  | 0  |     int bitlen = (nbits >> 1) - 100;  | 
246  |  | 
  | 
247  | 0  |     if (!BN_sub(diff, p, q))  | 
248  | 0  |         return -1;  | 
249  | 0  |     BN_set_negative(diff, 0);  | 
250  |  | 
  | 
251  | 0  |     if (BN_is_zero(diff))  | 
252  | 0  |         return 0;  | 
253  |  |  | 
254  | 0  |     if (!BN_sub_word(diff, 1))  | 
255  | 0  |         return -1;  | 
256  | 0  |     return (BN_num_bits(diff) > bitlen);  | 
257  | 0  | }  | 
258  |  |  | 
259  |  | /*  | 
260  |  |  * return LCM(p-1, q-1)  | 
261  |  |  *  | 
262  |  |  * Caller should ensure that lcm, gcd, p1, q1, p1q1 are flagged with  | 
263  |  |  * BN_FLG_CONSTTIME.  | 
264  |  |  */  | 
265  |  | int ossl_rsa_get_lcm(BN_CTX *ctx, const BIGNUM *p, const BIGNUM *q,  | 
266  |  |                      BIGNUM *lcm, BIGNUM *gcd, BIGNUM *p1, BIGNUM *q1,  | 
267  |  |                      BIGNUM *p1q1)  | 
268  | 0  | { | 
269  | 0  |     return BN_sub(p1, p, BN_value_one())    /* p-1 */  | 
270  | 0  |            && BN_sub(q1, q, BN_value_one()) /* q-1 */  | 
271  | 0  |            && BN_mul(p1q1, p1, q1, ctx)     /* (p-1)(q-1) */  | 
272  | 0  |            && BN_gcd(gcd, p1, q1, ctx)  | 
273  | 0  |            && BN_div(lcm, NULL, p1q1, gcd, ctx); /* LCM((p-1, q-1)) */  | 
274  | 0  | }  | 
275  |  |  | 
276  |  | /*  | 
277  |  |  * SP800-56Br1 6.4.2.2 Partial Public Key Validation for RSA refers to  | 
278  |  |  * SP800-89 5.3.3 (Explicit) Partial Public Key Validation for RSA  | 
279  |  |  * caveat is that the modulus must be as specified in SP800-56Br1  | 
280  |  |  */  | 
281  |  | int ossl_rsa_sp800_56b_check_public(const RSA *rsa)  | 
282  | 0  | { | 
283  | 0  |     int ret = 0, status;  | 
284  | 0  |     int nbits;  | 
285  | 0  |     BN_CTX *ctx = NULL;  | 
286  | 0  |     BIGNUM *gcd = NULL;  | 
287  |  | 
  | 
288  | 0  |     if (rsa->n == NULL || rsa->e == NULL)  | 
289  | 0  |         return 0;  | 
290  |  |  | 
291  | 0  |     nbits = BN_num_bits(rsa->n);  | 
292  | 0  |     if (nbits > OPENSSL_RSA_MAX_MODULUS_BITS) { | 
293  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);  | 
294  | 0  |         return 0;  | 
295  | 0  |     }  | 
296  |  |  | 
297  |  | #ifdef FIPS_MODULE  | 
298  |  |     /*  | 
299  |  |      * (Step a): modulus must be 2048 or 3072 (caveat from SP800-56Br1)  | 
300  |  |      * NOTE: changed to allow keys >= 2048  | 
301  |  |      */  | 
302  |  |     if (!ossl_rsa_sp800_56b_validate_strength(nbits, -1)) { | 
303  |  |         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_KEY_LENGTH);  | 
304  |  |         return 0;  | 
305  |  |     }  | 
306  |  | #endif  | 
307  | 0  |     if (!BN_is_odd(rsa->n)) { | 
308  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MODULUS);  | 
309  | 0  |         return 0;  | 
310  | 0  |     }  | 
311  |  |     /* (Steps b-c): 2^16 < e < 2^256, n and e must be odd */  | 
312  | 0  |     if (!ossl_rsa_check_public_exponent(rsa->e)) { | 
313  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_PUB_EXPONENT_OUT_OF_RANGE);  | 
314  | 0  |         return 0;  | 
315  | 0  |     }  | 
316  |  |  | 
317  | 0  |     ctx = BN_CTX_new_ex(rsa->libctx);  | 
318  | 0  |     gcd = BN_new();  | 
319  | 0  |     if (ctx == NULL || gcd == NULL)  | 
320  | 0  |         goto err;  | 
321  |  |  | 
322  |  |     /* (Steps d-f):  | 
323  |  |      * The modulus is composite, but not a power of a prime.  | 
324  |  |      * The modulus has no factors smaller than 752.  | 
325  |  |      */  | 
326  | 0  |     if (!BN_gcd(gcd, rsa->n, ossl_bn_get0_small_factors(), ctx)  | 
327  | 0  |         || !BN_is_one(gcd)) { | 
328  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MODULUS);  | 
329  | 0  |         goto err;  | 
330  | 0  |     }  | 
331  |  |  | 
332  |  |     /* Highest number of MR rounds from FIPS 186-5 Section B.3 Table B.1 */  | 
333  | 0  |     ret = ossl_bn_miller_rabin_is_prime(rsa->n, 5, ctx, NULL, 1, &status);  | 
334  |  | #ifdef FIPS_MODULE  | 
335  |  |     if (ret != 1 || status != BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME) { | 
336  |  | #else  | 
337  | 0  |     if (ret != 1 || (status != BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME  | 
338  | 0  |                      && (nbits >= RSA_MIN_MODULUS_BITS  | 
339  | 0  |                          || status != BN_PRIMETEST_COMPOSITE_WITH_FACTOR))) { | 
340  | 0  | #endif  | 
341  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MODULUS);  | 
342  | 0  |         ret = 0;  | 
343  | 0  |         goto err;  | 
344  | 0  |     }  | 
345  |  |  | 
346  | 0  |     ret = 1;  | 
347  | 0  | err:  | 
348  | 0  |     BN_free(gcd);  | 
349  | 0  |     BN_CTX_free(ctx);  | 
350  | 0  |     return ret;  | 
351  | 0  | }  | 
352  |  |  | 
353  |  | /*  | 
354  |  |  * Perform validation of the RSA private key to check that 0 < D < N.  | 
355  |  |  */  | 
356  |  | int ossl_rsa_sp800_56b_check_private(const RSA *rsa)  | 
357  | 0  | { | 
358  | 0  |     if (rsa->d == NULL || rsa->n == NULL)  | 
359  | 0  |         return 0;  | 
360  | 0  |     return BN_cmp(rsa->d, BN_value_one()) >= 0 && BN_cmp(rsa->d, rsa->n) < 0;  | 
361  | 0  | }  | 
362  |  |  | 
363  |  | /*  | 
364  |  |  * RSA key pair validation.  | 
365  |  |  *  | 
366  |  |  * SP800-56Br1.  | 
367  |  |  *    6.4.1.2 "RSAKPV1 Family: RSA Key - Pair Validation with a Fixed Exponent"  | 
368  |  |  *    6.4.1.3 "RSAKPV2 Family: RSA Key - Pair Validation with a Random Exponent"  | 
369  |  |  *  | 
370  |  |  * It uses:  | 
371  |  |  *     6.4.1.2.3 "rsakpv1 - crt"  | 
372  |  |  *     6.4.1.3.3 "rsakpv2 - crt"  | 
373  |  |  */  | 
374  |  | int ossl_rsa_sp800_56b_check_keypair(const RSA *rsa, const BIGNUM *efixed,  | 
375  |  |                                      int strength, int nbits)  | 
376  | 0  | { | 
377  | 0  |     int ret = 0;  | 
378  | 0  |     BN_CTX *ctx = NULL;  | 
379  | 0  |     BIGNUM *r = NULL;  | 
380  |  | 
  | 
381  | 0  |     if (rsa->p == NULL  | 
382  | 0  |             || rsa->q == NULL  | 
383  | 0  |             || rsa->e == NULL  | 
384  | 0  |             || rsa->d == NULL  | 
385  | 0  |             || rsa->n == NULL) { | 
386  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_REQUEST);  | 
387  | 0  |         return 0;  | 
388  | 0  |     }  | 
389  |  |     /* (Step 1): Check Ranges */  | 
390  | 0  |     if (!ossl_rsa_sp800_56b_validate_strength(nbits, strength))  | 
391  | 0  |         return 0;  | 
392  |  |  | 
393  |  |     /* If the exponent is known */  | 
394  | 0  |     if (efixed != NULL) { | 
395  |  |         /* (2): Check fixed exponent matches public exponent. */  | 
396  | 0  |         if (BN_cmp(efixed, rsa->e) != 0) { | 
397  | 0  |             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_REQUEST);  | 
398  | 0  |             return 0;  | 
399  | 0  |         }  | 
400  | 0  |     }  | 
401  |  |     /* (Step 1.c): e is odd integer 65537 <= e < 2^256 */  | 
402  | 0  |     if (!ossl_rsa_check_public_exponent(rsa->e)) { | 
403  |  |         /* exponent out of range */  | 
404  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_PUB_EXPONENT_OUT_OF_RANGE);  | 
405  | 0  |         return 0;  | 
406  | 0  |     }  | 
407  |  |     /* (Step 3.b): check the modulus */  | 
408  | 0  |     if (nbits != BN_num_bits(rsa->n)) { | 
409  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_KEYPAIR);  | 
410  | 0  |         return 0;  | 
411  | 0  |     }  | 
412  |  |     /* (Step 3.c): check that the modulus length is a positive even integer */  | 
413  | 0  |     if (nbits <= 0 || (nbits & 0x1)) { | 
414  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_KEYPAIR);  | 
415  | 0  |         return 0;  | 
416  | 0  |     }  | 
417  |  |  | 
418  | 0  |     ctx = BN_CTX_new_ex(rsa->libctx);  | 
419  | 0  |     if (ctx == NULL)  | 
420  | 0  |         return 0;  | 
421  |  |  | 
422  | 0  |     BN_CTX_start(ctx);  | 
423  | 0  |     r = BN_CTX_get(ctx);  | 
424  | 0  |     if (r == NULL || !BN_mul(r, rsa->p, rsa->q, ctx))  | 
425  | 0  |         goto err;  | 
426  |  |     /* (Step 4.c): Check n = pq */  | 
427  | 0  |     if (BN_cmp(rsa->n, r) != 0) { | 
428  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_REQUEST);  | 
429  | 0  |         goto err;  | 
430  | 0  |     }  | 
431  |  |  | 
432  |  |     /* (Step 5): check prime factors p & q */  | 
433  | 0  |     ret = ossl_rsa_check_prime_factor(rsa->p, rsa->e, nbits, ctx)  | 
434  | 0  |           && ossl_rsa_check_prime_factor(rsa->q, rsa->e, nbits, ctx)  | 
435  | 0  |           && (ossl_rsa_check_pminusq_diff(r, rsa->p, rsa->q, nbits) > 0)  | 
436  |  |           /* (Step 6): Check the private exponent d */  | 
437  | 0  |           && ossl_rsa_check_private_exponent(rsa, nbits, ctx)  | 
438  |  |           /* 6.4.1.2.3 (Step 7): Check the CRT components */  | 
439  | 0  |           && ossl_rsa_check_crt_components(rsa, ctx);  | 
440  | 0  |     if (ret != 1)  | 
441  | 0  |         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_KEYPAIR);  | 
442  |  | 
  | 
443  | 0  | err:  | 
444  | 0  |     BN_clear(r);  | 
445  | 0  |     BN_CTX_end(ctx);  | 
446  | 0  |     BN_CTX_free(ctx);  | 
447  | 0  |     return ret;  | 
448  | 0  | }  |