Update RSA keygen to use sp800-56b by default
[openssl.git] / crypto / rsa / rsa_sp800_56b_check.c
1 /*
2  * Copyright 2018-2020 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 rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx)
25 {
26     int ret = 0;
27     BIGNUM *r = NULL, *p1 = NULL, *q1 = NULL;
28
29     /* check if only some of the crt components are set */
30     if (rsa->dmp1 == NULL || rsa->dmq1 == NULL || rsa->iqmp == NULL) {
31         if (rsa->dmp1 != NULL || rsa->dmq1 != NULL || rsa->iqmp != NULL)
32             return 0;
33         return 1; /* return ok if all components are NULL */
34     }
35
36     BN_CTX_start(ctx);
37     r = BN_CTX_get(ctx);
38     p1 = BN_CTX_get(ctx);
39     q1 = BN_CTX_get(ctx);
40     ret = (q1 != NULL)
41           /* p1 = p -1 */
42           && (BN_copy(p1, rsa->p) != NULL)
43           && BN_sub_word(p1, 1)
44           /* q1 = q - 1 */
45           && (BN_copy(q1, rsa->q) != NULL)
46           && BN_sub_word(q1, 1)
47           /* (a) 1 < dP < (p – 1). */
48           && (BN_cmp(rsa->dmp1, BN_value_one()) > 0)
49           && (BN_cmp(rsa->dmp1, p1) < 0)
50           /* (b) 1 < dQ < (q - 1). */
51           && (BN_cmp(rsa->dmq1, BN_value_one()) > 0)
52           && (BN_cmp(rsa->dmq1, q1) < 0)
53           /* (c) 1 < qInv < p */
54           && (BN_cmp(rsa->iqmp, BN_value_one()) > 0)
55           && (BN_cmp(rsa->iqmp, rsa->p) < 0)
56           /* (d) 1 = (dP . e) mod (p - 1)*/
57           && BN_mod_mul(r, rsa->dmp1, rsa->e, p1, ctx)
58           && BN_is_one(r)
59           /* (e) 1 = (dQ . e) mod (q - 1) */
60           && BN_mod_mul(r, rsa->dmq1, rsa->e, q1, ctx)
61           && BN_is_one(r)
62           /* (f) 1 = (qInv . q) mod p */
63           && BN_mod_mul(r, rsa->iqmp, rsa->q, rsa->p, ctx)
64           && BN_is_one(r);
65     BN_clear(p1);
66     BN_clear(q1);
67     BN_CTX_end(ctx);
68     return ret;
69 }
70
71 /*
72  * Part of the RSA keypair test.
73  * Check that (√2)(2^(nbits/2 - 1) <= p <= 2^(nbits/2) - 1
74  *
75  * See SP800-5bBr1 6.4.1.2.1 Part 5 (c) & (g) - used for both p and q.
76  *
77  * (√2)(2^(nbits/2 - 1) = (√2/2)(2^(nbits/2))
78  */
79 int rsa_check_prime_factor_range(const BIGNUM *p, int nbits, BN_CTX *ctx)
80 {
81     int ret = 0;
82     BIGNUM *low;
83     int shift;
84
85     nbits >>= 1;
86     shift = nbits - BN_num_bits(&bn_inv_sqrt_2);
87
88     /* Upper bound check */
89     if (BN_num_bits(p) != nbits)
90         return 0;
91
92     BN_CTX_start(ctx);
93     low = BN_CTX_get(ctx);
94     if (low == NULL)
95         goto err;
96
97     /* set low = (√2)(2^(nbits/2 - 1) */
98     if (!BN_copy(low, &bn_inv_sqrt_2))
99         goto err;
100
101     if (shift >= 0) {
102         /*
103          * We don't have all the bits. bn_inv_sqrt_2 contains a rounded up
104          * value, so there is a very low probability that we'll reject a valid
105          * value.
106          */
107         if (!BN_lshift(low, low, shift))
108             goto err;
109     } else if (!BN_rshift(low, low, -shift)) {
110         goto err;
111     }
112     if (BN_cmp(p, low) <= 0)
113         goto err;
114     ret = 1;
115 err:
116     BN_CTX_end(ctx);
117     return ret;
118 }
119
120 /*
121  * Part of the RSA keypair test.
122  * Check the prime factor (for either p or q)
123  * i.e: p is prime AND GCD(p - 1, e) = 1
124  *
125  * See SP800-56Br1 6.4.1.2.3 Step 5 (a to d) & (e to h).
126  */
127 int rsa_check_prime_factor(BIGNUM *p, BIGNUM *e, int nbits, BN_CTX *ctx)
128 {
129     int ret = 0;
130     BIGNUM *p1 = NULL, *gcd = NULL;
131
132     /* (Steps 5 a-b) prime test */
133     if (BN_check_prime(p, ctx, NULL) != 1
134             /* (Step 5c) (√2)(2^(nbits/2 - 1) <= p <= 2^(nbits/2 - 1) */
135             || rsa_check_prime_factor_range(p, nbits, ctx) != 1)
136         return 0;
137
138     BN_CTX_start(ctx);
139     p1 = BN_CTX_get(ctx);
140     gcd = BN_CTX_get(ctx);
141     ret = (gcd != NULL)
142           /* (Step 5d) GCD(p-1, e) = 1 */
143           && (BN_copy(p1, p) != NULL)
144           && BN_sub_word(p1, 1)
145           && BN_gcd(gcd, p1, e, ctx)
146           && BN_is_one(gcd);
147
148     BN_clear(p1);
149     BN_CTX_end(ctx);
150     return ret;
151 }
152
153 /*
154  * See SP800-56Br1 6.4.1.2.3 Part 6(a-b) Check the private exponent d
155  * satisfies:
156  *     (Step 6a) 2^(nBit/2) < d < LCM(p–1, q–1).
157  *     (Step 6b) 1 = (d*e) mod LCM(p–1, q–1)
158  */
159 int rsa_check_private_exponent(const RSA *rsa, int nbits, BN_CTX *ctx)
160 {
161     int ret;
162     BIGNUM *r, *p1, *q1, *lcm, *p1q1, *gcd;
163
164     /* (Step 6a) 2^(nbits/2) < d */
165     if (BN_num_bits(rsa->d) <= (nbits >> 1))
166         return 0;
167
168     BN_CTX_start(ctx);
169     r = BN_CTX_get(ctx);
170     p1 = BN_CTX_get(ctx);
171     q1 = BN_CTX_get(ctx);
172     lcm = BN_CTX_get(ctx);
173     p1q1 = BN_CTX_get(ctx);
174     gcd = BN_CTX_get(ctx);
175     ret = (gcd != NULL
176           /* LCM(p - 1, q - 1) */
177           && (rsa_get_lcm(ctx, rsa->p, rsa->q, lcm, gcd, p1, q1, p1q1) == 1)
178           /* (Step 6a) d < LCM(p - 1, q - 1) */
179           && (BN_cmp(rsa->d, lcm) < 0)
180           /* (Step 6b) 1 = (e . d) mod LCM(p - 1, q - 1) */
181           && BN_mod_mul(r, rsa->e, rsa->d, lcm, ctx)
182           && BN_is_one(r));
183
184     BN_clear(p1);
185     BN_clear(q1);
186     BN_clear(lcm);
187     BN_clear(gcd);
188     BN_CTX_end(ctx);
189     return ret;
190 }
191
192 #ifndef FIPS_MODULE
193 static int bn_is_three(const BIGNUM *bn)
194 {
195     BIGNUM *num = BN_dup(bn);
196     int ret = (num != NULL && BN_sub_word(num, 3) && BN_is_zero(num));
197
198     BN_free(num);
199     return ret;
200 }
201 #endif /* FIPS_MODULE */
202
203 /* Check exponent is odd, and has a bitlen ranging from [17..256] */
204 int rsa_check_public_exponent(const BIGNUM *e)
205 {
206     int bitlen;
207
208     /* For legacy purposes RSA_3 is allowed in non fips mode */
209 #ifndef FIPS_MODULE
210     if (bn_is_three(e))
211         return 1;
212 #endif /* FIPS_MODULE */
213
214     bitlen = BN_num_bits(e);
215     return (BN_is_odd(e) && bitlen > 16 && bitlen < 257);
216 }
217
218 /*
219  * SP800-56Br1 6.4.1.2.1 (Step 5i): |p - q| > 2^(nbits/2 - 100)
220  * i.e- numbits(p-q-1) > (nbits/2 -100)
221  */
222 int rsa_check_pminusq_diff(BIGNUM *diff, const BIGNUM *p, const BIGNUM *q,
223                            int nbits)
224 {
225     int bitlen = (nbits >> 1) - 100;
226
227     if (!BN_sub(diff, p, q))
228         return -1;
229     BN_set_negative(diff, 0);
230
231     if (BN_is_zero(diff))
232         return 0;
233
234     if (!BN_sub_word(diff, 1))
235         return -1;
236     return (BN_num_bits(diff) > bitlen);
237 }
238
239 /* return LCM(p-1, q-1) */
240 int rsa_get_lcm(BN_CTX *ctx, const BIGNUM *p, const BIGNUM *q,
241                 BIGNUM *lcm, BIGNUM *gcd, BIGNUM *p1, BIGNUM *q1,
242                 BIGNUM *p1q1)
243 {
244     return BN_sub(p1, p, BN_value_one())    /* p-1 */
245            && BN_sub(q1, q, BN_value_one()) /* q-1 */
246            && BN_mul(p1q1, p1, q1, ctx)     /* (p-1)(q-1) */
247            && BN_gcd(gcd, p1, q1, ctx)
248            && BN_div(lcm, NULL, p1q1, gcd, ctx); /* LCM((p-1, q-1)) */
249 }
250
251 /*
252  * SP800-56Br1 6.4.2.2 Partial Public Key Validation for RSA refers to
253  * SP800-89 5.3.3 (Explicit) Partial Public Key Validation for RSA
254  * caveat is that the modulus must be as specified in SP800-56Br1
255  */
256 int rsa_sp800_56b_check_public(const RSA *rsa)
257 {
258     int ret = 0, status;
259 #ifdef FIPS_MODULE
260     int nbits;
261 #endif
262     BN_CTX *ctx = NULL;
263     BIGNUM *gcd = NULL;
264
265     if (rsa->n == NULL || rsa->e == NULL)
266         return 0;
267
268 #ifdef FIPS_MODULE
269     /*
270      * (Step a): modulus must be 2048 or 3072 (caveat from SP800-56Br1)
271      * NOTE: changed to allow keys >= 2048
272      */
273     nbits = BN_num_bits(rsa->n);
274     if (!rsa_sp800_56b_validate_strength(nbits, -1)) {
275         RSAerr(RSA_F_RSA_SP800_56B_CHECK_PUBLIC, RSA_R_INVALID_KEY_LENGTH);
276         return 0;
277     }
278 #endif
279     if (!BN_is_odd(rsa->n)) {
280         RSAerr(RSA_F_RSA_SP800_56B_CHECK_PUBLIC, RSA_R_INVALID_MODULUS);
281         return 0;
282     }
283     /* (Steps b-c): 2^16 < e < 2^256, n and e must be odd */
284     if (!rsa_check_public_exponent(rsa->e)) {
285         RSAerr(RSA_F_RSA_SP800_56B_CHECK_PUBLIC,
286                RSA_R_PUB_EXPONENT_OUT_OF_RANGE);
287         return 0;
288     }
289
290     ctx = BN_CTX_new_ex(rsa->libctx);
291     gcd = BN_new();
292     if (ctx == NULL || gcd == NULL)
293         goto err;
294
295     /* (Steps d-f):
296      * The modulus is composite, but not a power of a prime.
297      * The modulus has no factors smaller than 752.
298      */
299     if (!BN_gcd(gcd, rsa->n, bn_get0_small_factors(), ctx) || !BN_is_one(gcd)) {
300         RSAerr(RSA_F_RSA_SP800_56B_CHECK_PUBLIC, RSA_R_INVALID_MODULUS);
301         goto err;
302     }
303
304     ret = bn_miller_rabin_is_prime(rsa->n, 0, ctx, NULL, 1, &status);
305     if (ret != 1 || status != BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME) {
306         RSAerr(RSA_F_RSA_SP800_56B_CHECK_PUBLIC, RSA_R_INVALID_MODULUS);
307         ret = 0;
308         goto err;
309     }
310
311     ret = 1;
312 err:
313     BN_free(gcd);
314     BN_CTX_free(ctx);
315     return ret;
316 }
317
318 /*
319  * Perform validation of the RSA private key to check that 0 < D < N.
320  */
321 int rsa_sp800_56b_check_private(const RSA *rsa)
322 {
323     if (rsa->d == NULL || rsa->n == NULL)
324         return 0;
325     return BN_cmp(rsa->d, BN_value_one()) >= 0 && BN_cmp(rsa->d, rsa->n) < 0;
326 }
327
328 /*
329  * RSA key pair validation.
330  *
331  * SP800-56Br1.
332  *    6.4.1.2 "RSAKPV1 Family: RSA Key - Pair Validation with a Fixed Exponent"
333  *    6.4.1.3 "RSAKPV2 Family: RSA Key - Pair Validation with a Random Exponent"
334  *
335  * It uses:
336  *     6.4.1.2.3 "rsakpv1 - crt"
337  *     6.4.1.3.3 "rsakpv2 - crt"
338  */
339 int rsa_sp800_56b_check_keypair(const RSA *rsa, const BIGNUM *efixed,
340                                 int strength, int nbits)
341 {
342     int ret = 0;
343     BN_CTX *ctx = NULL;
344     BIGNUM *r = NULL;
345
346     if (rsa->p == NULL
347             || rsa->q == NULL
348             || rsa->e == NULL
349             || rsa->d == NULL
350             || rsa->n == NULL) {
351         RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_REQUEST);
352         return 0;
353     }
354     /* (Step 1): Check Ranges */
355     if (!rsa_sp800_56b_validate_strength(nbits, strength))
356         return 0;
357
358     /* If the exponent is known */
359     if (efixed != NULL) {
360         /* (2): Check fixed exponent matches public exponent. */
361         if (BN_cmp(efixed, rsa->e) != 0) {
362             RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_REQUEST);
363             return 0;
364         }
365     }
366     /* (Step 1.c): e is odd integer 65537 <= e < 2^256 */
367     if (!rsa_check_public_exponent(rsa->e)) {
368         /* exponent out of range */
369         RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR,
370                RSA_R_PUB_EXPONENT_OUT_OF_RANGE);
371         return 0;
372     }
373     /* (Step 3.b): check the modulus */
374     if (nbits != BN_num_bits(rsa->n)) {
375         RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_KEYPAIR);
376         return 0;
377     }
378
379     ctx = BN_CTX_new_ex(rsa->libctx);
380     if (ctx == NULL)
381         return 0;
382
383     BN_CTX_start(ctx);
384     r = BN_CTX_get(ctx);
385     if (r == NULL || !BN_mul(r, rsa->p, rsa->q, ctx))
386         goto err;
387     /* (Step 4.c): Check n = pq */
388     if (BN_cmp(rsa->n, r) != 0) {
389         RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_REQUEST);
390         goto err;
391     }
392
393     /* (Step 5): check prime factors p & q */
394     ret = rsa_check_prime_factor(rsa->p, rsa->e, nbits, ctx)
395           && rsa_check_prime_factor(rsa->q, rsa->e, nbits, ctx)
396           && (rsa_check_pminusq_diff(r, rsa->p, rsa->q, nbits) > 0)
397           /* (Step 6): Check the private exponent d */
398           && rsa_check_private_exponent(rsa, nbits, ctx)
399           /* 6.4.1.2.3 (Step 7): Check the CRT components */
400           && rsa_check_crt_components(rsa, ctx);
401     if (ret != 1)
402         RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_KEYPAIR);
403
404 err:
405     BN_clear(r);
406     BN_CTX_end(ctx);
407     BN_CTX_free(ctx);
408     return ret;
409 }