rsa: Do not allow less than 512 bit RSA keys
[openssl.git] / crypto / rsa / rsa_gen.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 /*
11  * NB: these functions have been "upgraded", the deprecated versions (which
12  * are compatibility wrappers using these functions) are in rsa_depr.c. -
13  * Geoff
14  */
15
16 #include <stdio.h>
17 #include <time.h>
18 #include "internal/cryptlib.h"
19 #include <openssl/bn.h>
20 #include "rsa_locl.h"
21
22 static int rsa_builtin_keygen(RSA *rsa, int bits, int primes, BIGNUM *e_value,
23                               BN_GENCB *cb);
24
25 /*
26  * NB: this wrapper would normally be placed in rsa_lib.c and the static
27  * implementation would probably be in rsa_eay.c. Nonetheless, is kept here
28  * so that we don't introduce a new linker dependency. Eg. any application
29  * that wasn't previously linking object code related to key-generation won't
30  * have to now just because key-generation is part of RSA_METHOD.
31  */
32 int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
33 {
34     if (rsa->meth->rsa_keygen != NULL)
35         return rsa->meth->rsa_keygen(rsa, bits, e_value, cb);
36
37     return RSA_generate_multi_prime_key(rsa, bits, RSA_DEFAULT_PRIME_NUM,
38                                         e_value, cb);
39 }
40
41 int RSA_generate_multi_prime_key(RSA *rsa, int bits, int primes,
42                                  BIGNUM *e_value, BN_GENCB *cb)
43 {
44     /* multi-prime is only supported with the builtin key generation */
45     if (rsa->meth->rsa_multi_prime_keygen != NULL) {
46         return rsa->meth->rsa_multi_prime_keygen(rsa, bits, primes,
47                                                  e_value, cb);
48     } else if (rsa->meth->rsa_keygen != NULL) {
49         /*
50          * However, if rsa->meth implements only rsa_keygen, then we
51          * have to honour it in 2-prime case and assume that it wouldn't
52          * know what to do with multi-prime key generated by builtin
53          * subroutine...
54          */
55         if (primes == 2)
56             return rsa->meth->rsa_keygen(rsa, bits, e_value, cb);
57         else
58             return 0;
59     }
60
61     return rsa_builtin_keygen(rsa, bits, primes, e_value, cb);
62 }
63
64 static int rsa_builtin_keygen(RSA *rsa, int bits, int primes, BIGNUM *e_value,
65                               BN_GENCB *cb)
66 {
67     BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *tmp, *prime;
68     int ok = -1, n = 0, bitsr[RSA_MAX_PRIME_NUM], bitse = 0;
69     int i = 0, quo = 0, rmd = 0, adj = 0, retries = 0;
70     RSA_PRIME_INFO *pinfo = NULL;
71     STACK_OF(RSA_PRIME_INFO) *prime_infos = NULL;
72     BN_CTX *ctx = NULL;
73     BN_ULONG bitst = 0;
74
75     if (bits < RSA_MIN_MODULUS_BITS) {
76         ok = 0;             /* we set our own err */
77         RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_SIZE_TOO_SMALL);
78         goto err;
79     }
80
81     if (primes < RSA_DEFAULT_PRIME_NUM || primes > rsa_multip_cap(bits)) {
82         ok = 0;             /* we set our own err */
83         RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_PRIME_NUM_INVALID);
84         goto err;
85     }
86
87     ctx = BN_CTX_new();
88     if (ctx == NULL)
89         goto err;
90     BN_CTX_start(ctx);
91     r0 = BN_CTX_get(ctx);
92     r1 = BN_CTX_get(ctx);
93     r2 = BN_CTX_get(ctx);
94     if (r2 == NULL)
95         goto err;
96
97     /* divide bits into 'primes' pieces evenly */
98     quo = bits / primes;
99     rmd = bits % primes;
100
101     for (i = 0; i < primes; i++)
102         bitsr[i] = (i < rmd) ? quo + 1 : quo;
103
104     /* We need the RSA components non-NULL */
105     if (!rsa->n && ((rsa->n = BN_new()) == NULL))
106         goto err;
107     if (!rsa->d && ((rsa->d = BN_secure_new()) == NULL))
108         goto err;
109     if (!rsa->e && ((rsa->e = BN_new()) == NULL))
110         goto err;
111     if (!rsa->p && ((rsa->p = BN_secure_new()) == NULL))
112         goto err;
113     if (!rsa->q && ((rsa->q = BN_secure_new()) == NULL))
114         goto err;
115     if (!rsa->dmp1 && ((rsa->dmp1 = BN_secure_new()) == NULL))
116         goto err;
117     if (!rsa->dmq1 && ((rsa->dmq1 = BN_secure_new()) == NULL))
118         goto err;
119     if (!rsa->iqmp && ((rsa->iqmp = BN_secure_new()) == NULL))
120         goto err;
121
122     /* initialize multi-prime components */
123     if (primes > RSA_DEFAULT_PRIME_NUM) {
124         rsa->version = RSA_ASN1_VERSION_MULTI;
125         prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, primes - 2);
126         if (prime_infos == NULL)
127             goto err;
128         if (rsa->prime_infos != NULL) {
129             /* could this happen? */
130             sk_RSA_PRIME_INFO_pop_free(rsa->prime_infos, rsa_multip_info_free);
131         }
132         rsa->prime_infos = prime_infos;
133
134         /* prime_info from 2 to |primes| -1 */
135         for (i = 2; i < primes; i++) {
136             pinfo = rsa_multip_info_new();
137             if (pinfo == NULL)
138                 goto err;
139             (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
140         }
141     }
142
143     if (BN_copy(rsa->e, e_value) == NULL)
144         goto err;
145
146     /* generate p, q and other primes (if any) */
147     for (i = 0; i < primes; i++) {
148         adj = 0;
149         retries = 0;
150
151         if (i == 0) {
152             prime = rsa->p;
153         } else if (i == 1) {
154             prime = rsa->q;
155         } else {
156             pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
157             prime = pinfo->r;
158         }
159
160         for (;;) {
161  redo:
162             if (!BN_generate_prime_ex(prime, bitsr[i] + adj, 0, NULL, NULL, cb))
163                 goto err;
164             /*
165              * prime should not be equal to p, q, r_3...
166              * (those primes prior to this one)
167              */
168             {
169                 int j;
170
171                 for (j = 0; j < i; j++) {
172                     BIGNUM *prev_prime;
173
174                     if (j == 0)
175                         prev_prime = rsa->p;
176                     else if (j == 1)
177                         prev_prime = rsa->q;
178                     else
179                         prev_prime = sk_RSA_PRIME_INFO_value(prime_infos,
180                                                              j - 2)->r;
181
182                     if (!BN_cmp(prime, prev_prime)) {
183                         goto redo;
184                     }
185                 }
186             }
187             if (!BN_sub(r2, prime, BN_value_one()))
188                 goto err;
189             if (!BN_gcd(r1, r2, rsa->e, ctx))
190                 goto err;
191             if (BN_is_one(r1))
192                 break;
193             if (!BN_GENCB_call(cb, 2, n++))
194                 goto err;
195         }
196
197         bitse += bitsr[i];
198
199         /* calculate n immediately to see if it's sufficient */
200         if (i == 1) {
201             /* we get at least 2 primes */
202             if (!BN_mul(r1, rsa->p, rsa->q, ctx))
203                 goto err;
204         } else if (i != 0) {
205             /* modulus n = p * q * r_3 * r_4 ... */
206             if (!BN_mul(r1, rsa->n, prime, ctx))
207                 goto err;
208         } else {
209             /* i == 0, do nothing */
210             if (!BN_GENCB_call(cb, 3, i))
211                 goto err;
212             continue;
213         }
214         /*
215          * if |r1|, product of factors so far, is not as long as expected
216          * (by checking the first 4 bits are less than 0x9 or greater than
217          * 0xF). If so, re-generate the last prime.
218          *
219          * NOTE: This actually can't happen in two-prime case, because of
220          * the way factors are generated.
221          *
222          * Besides, another consideration is, for multi-prime case, even the
223          * length modulus is as long as expected, the modulus could start at
224          * 0x8, which could be utilized to distinguish a multi-prime private
225          * key by using the modulus in a certificate. This is also covered
226          * by checking the length should not be less than 0x9.
227          */
228         if (!BN_rshift(r2, r1, bitse - 4))
229             goto err;
230         bitst = BN_get_word(r2);
231
232         if (bitst < 0x9 || bitst > 0xF) {
233             /*
234              * For keys with more than 4 primes, we attempt longer factor to
235              * meet length requirement.
236              *
237              * Otherwise, we just re-generate the prime with the same length.
238              *
239              * This strategy has the following goals:
240              *
241              * 1. 1024-bit factors are effcient when using 3072 and 4096-bit key
242              * 2. stay the same logic with normal 2-prime key
243              */
244             bitse -= bitsr[i];
245             if (!BN_GENCB_call(cb, 2, n++))
246                 goto err;
247             if (primes > 4) {
248                 if (bitst < 0x9)
249                     adj++;
250                 else
251                     adj--;
252             } else if (retries == 4) {
253                 /*
254                  * re-generate all primes from scratch, mainly used
255                  * in 4 prime case to avoid long loop. Max retry times
256                  * is set to 4.
257                  */
258                 i = -1;
259                 bitse = 0;
260                 continue;
261             }
262             retries++;
263             goto redo;
264         }
265         /* save product of primes for further use, for multi-prime only */
266         if (i > 1 && BN_copy(pinfo->pp, rsa->n) == NULL)
267             goto err;
268         if (BN_copy(rsa->n, r1) == NULL)
269             goto err;
270         if (!BN_GENCB_call(cb, 3, i))
271             goto err;
272     }
273
274     if (BN_cmp(rsa->p, rsa->q) < 0) {
275         tmp = rsa->p;
276         rsa->p = rsa->q;
277         rsa->q = tmp;
278     }
279
280     /* calculate d */
281
282     /* p - 1 */
283     if (!BN_sub(r1, rsa->p, BN_value_one()))
284         goto err;
285     /* q - 1 */
286     if (!BN_sub(r2, rsa->q, BN_value_one()))
287         goto err;
288     /* (p - 1)(q - 1) */
289     if (!BN_mul(r0, r1, r2, ctx))
290         goto err;
291     /* multi-prime */
292     for (i = 2; i < primes; i++) {
293         pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
294         /* save r_i - 1 to pinfo->d temporarily */
295         if (!BN_sub(pinfo->d, pinfo->r, BN_value_one()))
296             goto err;
297         if (!BN_mul(r0, r0, pinfo->d, ctx))
298             goto err;
299     }
300
301     {
302         BIGNUM *pr0 = BN_new();
303
304         if (pr0 == NULL)
305             goto err;
306
307         BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
308         if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {
309             BN_free(pr0);
310             goto err;               /* d */
311         }
312         /* We MUST free pr0 before any further use of r0 */
313         BN_free(pr0);
314     }
315
316     {
317         BIGNUM *d = BN_new();
318
319         if (d == NULL)
320             goto err;
321
322         BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
323
324         /* calculate d mod (p-1) and d mod (q - 1) */
325         if (!BN_mod(rsa->dmp1, d, r1, ctx)
326             || !BN_mod(rsa->dmq1, d, r2, ctx)) {
327             BN_free(d);
328             goto err;
329         }
330
331         /* calculate CRT exponents */
332         for (i = 2; i < primes; i++) {
333             pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
334             /* pinfo->d == r_i - 1 */
335             if (!BN_mod(pinfo->d, d, pinfo->d, ctx)) {
336                 BN_free(d);
337                 goto err;
338             }
339         }
340
341         /* We MUST free d before any further use of rsa->d */
342         BN_free(d);
343     }
344
345     {
346         BIGNUM *p = BN_new();
347
348         if (p == NULL)
349             goto err;
350         BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
351
352         /* calculate inverse of q mod p */
353         if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {
354             BN_free(p);
355             goto err;
356         }
357
358         /* calculate CRT coefficient for other primes */
359         for (i = 2; i < primes; i++) {
360             pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
361             BN_with_flags(p, pinfo->r, BN_FLG_CONSTTIME);
362             if (!BN_mod_inverse(pinfo->t, pinfo->pp, p, ctx)) {
363                 BN_free(p);
364                 goto err;
365             }
366         }
367
368         /* We MUST free p before any further use of rsa->p */
369         BN_free(p);
370     }
371
372     ok = 1;
373  err:
374     if (ok == -1) {
375         RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, ERR_LIB_BN);
376         ok = 0;
377     }
378     if (ctx != NULL)
379         BN_CTX_end(ctx);
380     BN_CTX_free(ctx);
381     return ok;
382 }