]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rsa/rsa_lib.c
[crypto/asn1] Fix multiple SCA vulnerabilities during RSA key validation.
[thirdparty/openssl.git] / crypto / rsa / rsa_lib.c
CommitLineData
2039c421 1/*
83cf7abf 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
2a7b6f39 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
ec577822 11#include <openssl/crypto.h>
b39fc560 12#include "internal/cryptlib.h"
cd420b0b 13#include "internal/refcount.h"
18125f7f 14#include "internal/bn_int.h"
3c27208f 15#include <openssl/engine.h>
e5e04ee3
DSH
16#include <openssl/evp.h>
17#include "internal/evp_int.h"
9862e9aa 18#include "rsa_locl.h"
d02b48c6 19
6b691a5c 20RSA *RSA_new(void)
0f113f3e 21{
076fc555 22 return RSA_new_method(NULL);
0f113f3e 23}
ce8b2574 24
29c1f061 25const RSA_METHOD *RSA_get_method(const RSA *rsa)
0f113f3e
MC
26{
27 return rsa->meth;
28}
cb78486d
GT
29
30int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
0f113f3e
MC
31{
32 /*
33 * NB: The caller is specifically setting a method, so it's not up to us
34 * to deal with which ENGINE it comes from.
35 */
36 const RSA_METHOD *mtmp;
37 mtmp = rsa->meth;
38 if (mtmp->finish)
39 mtmp->finish(rsa);
0b13e9f0 40#ifndef OPENSSL_NO_ENGINE
7c96dbcd
RS
41 ENGINE_finish(rsa->engine);
42 rsa->engine = NULL;
0b13e9f0 43#endif
0f113f3e
MC
44 rsa->meth = meth;
45 if (meth->init)
46 meth->init(rsa);
47 return 1;
48}
ce8b2574 49
5270e702 50RSA *RSA_new_method(ENGINE *engine)
0f113f3e 51{
11ed851d 52 RSA *ret = OPENSSL_zalloc(sizeof(*ret));
d02b48c6 53
0f113f3e
MC
54 if (ret == NULL) {
55 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
56 return NULL;
57 }
d02b48c6 58
11ed851d
F
59 ret->references = 1;
60 ret->lock = CRYPTO_THREAD_lock_new();
61 if (ret->lock == NULL) {
62 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
63 OPENSSL_free(ret);
64 return NULL;
65 }
66
0f113f3e 67 ret->meth = RSA_get_default_method();
0b13e9f0 68#ifndef OPENSSL_NO_ENGINE
11ed851d 69 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
0f113f3e
MC
70 if (engine) {
71 if (!ENGINE_init(engine)) {
72 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
11ed851d 73 goto err;
0f113f3e
MC
74 }
75 ret->engine = engine;
90862ab4 76 } else {
0f113f3e 77 ret->engine = ENGINE_get_default_RSA();
90862ab4 78 }
0f113f3e
MC
79 if (ret->engine) {
80 ret->meth = ENGINE_get_RSA(ret->engine);
7c96dbcd 81 if (ret->meth == NULL) {
0f113f3e 82 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
11ed851d 83 goto err;
0f113f3e
MC
84 }
85 }
0b13e9f0 86#endif
0c9de428 87
0f113f3e
MC
88 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
89 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
11ed851d 90 goto err;
d188a536
AG
91 }
92
93 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
11ed851d
F
94 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
95 goto err;
0f113f3e 96 }
d188a536
AG
97
98 return ret;
11ed851d 99
544648a8 100 err:
11ed851d
F
101 RSA_free(ret);
102 return NULL;
0f113f3e 103}
d02b48c6 104
6b691a5c 105void RSA_free(RSA *r)
0f113f3e
MC
106{
107 int i;
d02b48c6 108
0f113f3e
MC
109 if (r == NULL)
110 return;
d02b48c6 111
2f545ae4 112 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
f3f1cf84 113 REF_PRINT_COUNT("RSA", r);
0f113f3e
MC
114 if (i > 0)
115 return;
f3f1cf84 116 REF_ASSERT_ISNT(i < 0);
d02b48c6 117
0c5d725e 118 if (r->meth != NULL && r->meth->finish != NULL)
0f113f3e 119 r->meth->finish(r);
0b13e9f0 120#ifndef OPENSSL_NO_ENGINE
412bafdc 121 ENGINE_finish(r->engine);
0b13e9f0 122#endif
d02b48c6 123
0f113f3e 124 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
7abe8305 125
d188a536
AG
126 CRYPTO_THREAD_lock_free(r->lock);
127
c033101d
MB
128 BN_free(r->n);
129 BN_free(r->e);
23a1d5e9
RS
130 BN_clear_free(r->d);
131 BN_clear_free(r->p);
132 BN_clear_free(r->q);
133 BN_clear_free(r->dmp1);
134 BN_clear_free(r->dmq1);
135 BN_clear_free(r->iqmp);
d771441d 136 RSA_PSS_PARAMS_free(r->pss);
665d899f 137 sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
23a1d5e9
RS
138 BN_BLINDING_free(r->blinding);
139 BN_BLINDING_free(r->mt_blinding);
4c42ebd2 140 OPENSSL_free(r->bignum_data);
0f113f3e
MC
141 OPENSSL_free(r);
142}
d02b48c6 143
6ac4e8bd 144int RSA_up_ref(RSA *r)
0f113f3e 145{
d188a536
AG
146 int i;
147
2f545ae4 148 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
d188a536 149 return 0;
f3f1cf84
RS
150
151 REF_PRINT_COUNT("RSA", r);
152 REF_ASSERT_ISNT(i < 2);
8686c474 153 return i > 1 ? 1 : 0;
0f113f3e 154}
5cbc2e8b 155
dd9d233e 156int RSA_set_ex_data(RSA *r, int idx, void *arg)
0f113f3e 157{
8686c474 158 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
0f113f3e 159}
58964a49 160
29c1f061 161void *RSA_get_ex_data(const RSA *r, int idx)
0f113f3e 162{
8686c474 163 return CRYPTO_get_ex_data(&r->ex_data, idx);
0f113f3e 164}
58964a49 165
97b0b713
P
166/*
167 * Define a scaling constant for our fixed point arithmetic.
168 * This value must be a power of two because the base two logarithm code
169 * makes this assumption. The exponent must also be a multiple of three so
170 * that the scale factor has an exact cube root. Finally, the scale factor
171 * should not be so large that a multiplication of two scaled numbers
172 * overflows a 64 bit unsigned integer.
173 */
174static const unsigned int scale = 1 << 18;
175static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
176
177/* Define some constants, none exceed 32 bits */
178static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */
179static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */
180static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */
181static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */
182
183/*
2beb004b 184 * Multiply two scaled integers together and rescale the result.
97b0b713
P
185 */
186static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
187{
188 return a * b / scale;
189}
190
191/*
192 * Calculate the cube root of a 64 bit scaled integer.
193 * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
194 * integer, this is not guaranteed after scaling, so this function has a
195 * 64 bit return. This uses the shifting nth root algorithm with some
196 * algebraic simplifications.
197 */
198static uint64_t icbrt64(uint64_t x)
199{
200 uint64_t r = 0;
201 uint64_t b;
202 int s;
203
204 for (s = 63; s >= 0; s -= 3) {
205 r <<= 1;
206 b = 3 * r * (r + 1) + 1;
207 if ((x >> s) >= b) {
208 x -= b << s;
209 r++;
210 }
211 }
212 return r * cbrt_scale;
213}
214
215/*
216 * Calculate the natural logarithm of a 64 bit scaled integer.
217 * This is done by calculating a base two logarithm and scaling.
218 * The maximum logarithm (base 2) is 64 and this reduces base e, so
219 * a 32 bit result should not overflow. The argument passed must be
220 * greater than unity so we don't need to handle negative results.
221 */
222static uint32_t ilog_e(uint64_t v)
223{
224 uint32_t i, r = 0;
225
226 /*
227 * Scale down the value into the range 1 .. 2.
228 *
229 * If fractional numbers need to be processed, another loop needs
230 * to go here that checks v < scale and if so multiplies it by 2 and
231 * reduces r by scale. This also means making r signed.
232 */
233 while (v >= 2 * scale) {
234 v >>= 1;
235 r += scale;
236 }
237 for (i = scale / 2; i != 0; i /= 2) {
238 v = mul2(v, v);
239 if (v >= 2 * scale) {
240 v >>= 1;
241 r += i;
242 }
243 }
244 r = (r * (uint64_t)scale) / log_e;
245 return r;
246}
247
248/*
249 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
250 * Modulus Lengths.
251 *
252 * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
253 * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
254 * The two cube roots are merged together here.
255 */
8240d5fa 256uint16_t rsa_compute_security_bits(int n)
97b0b713
P
257{
258 uint64_t x;
259 uint32_t lx;
260 uint16_t y;
261
262 /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
263 switch (n) {
264 case 2048:
265 return 112;
266 case 3072:
267 return 128;
268 case 4096:
269 return 152;
270 case 6144:
271 return 176;
272 case 8192:
273 return 200;
274 }
275 /*
276 * The first incorrect result (i.e. not accurate or off by one low) occurs
277 * for n = 699668. The true value here is 1200. Instead of using this n
278 * as the check threshold, the smallest n such that the correct result is
279 * 1200 is used instead.
280 */
281 if (n >= 687737)
282 return 1200;
283 if (n < 8)
284 return 0;
285
286 x = n * (uint64_t)log_2;
287 lx = ilog_e(x);
288 y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
289 / log_2);
290 return (y + 4) & ~7;
291}
292
2514fa79 293int RSA_security_bits(const RSA *rsa)
0f113f3e 294{
0122add6
AP
295 int bits = BN_num_bits(rsa->n);
296
297 if (rsa->version == RSA_ASN1_VERSION_MULTI) {
298 /* This ought to mean that we have private key at hand. */
299 int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
300
301 if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
302 return 0;
303 }
97b0b713 304 return rsa_compute_security_bits(bits);
0f113f3e 305}
9862e9aa
RL
306
307int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
308{
fd809cfd 309 /* If the fields n and e in r are NULL, the corresponding input
1da12e34
RL
310 * parameters MUST be non-NULL for n and e. d may be
311 * left NULL (in case only the public key is used).
1da12e34 312 */
b84e1226
MC
313 if ((r->n == NULL && n == NULL)
314 || (r->e == NULL && e == NULL))
9862e9aa
RL
315 return 0;
316
1da12e34
RL
317 if (n != NULL) {
318 BN_free(r->n);
319 r->n = n;
320 }
321 if (e != NULL) {
322 BN_free(r->e);
323 r->e = e;
324 }
325 if (d != NULL) {
c033101d 326 BN_clear_free(r->d);
1da12e34 327 r->d = d;
311e903d 328 BN_set_flags(r->d, BN_FLG_CONSTTIME);
1da12e34 329 }
9862e9aa
RL
330
331 return 1;
332}
333
334int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
335{
fd809cfd 336 /* If the fields p and q in r are NULL, the corresponding input
1da12e34 337 * parameters MUST be non-NULL.
1da12e34 338 */
b84e1226
MC
339 if ((r->p == NULL && p == NULL)
340 || (r->q == NULL && q == NULL))
9862e9aa
RL
341 return 0;
342
1da12e34 343 if (p != NULL) {
c033101d 344 BN_clear_free(r->p);
1da12e34 345 r->p = p;
311e903d 346 BN_set_flags(r->p, BN_FLG_CONSTTIME);
1da12e34
RL
347 }
348 if (q != NULL) {
c033101d 349 BN_clear_free(r->q);
1da12e34 350 r->q = q;
311e903d 351 BN_set_flags(r->q, BN_FLG_CONSTTIME);
1da12e34 352 }
9862e9aa
RL
353
354 return 1;
355}
356
357int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
358{
fd809cfd 359 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
1da12e34 360 * parameters MUST be non-NULL.
1da12e34 361 */
b84e1226
MC
362 if ((r->dmp1 == NULL && dmp1 == NULL)
363 || (r->dmq1 == NULL && dmq1 == NULL)
364 || (r->iqmp == NULL && iqmp == NULL))
9862e9aa
RL
365 return 0;
366
1da12e34 367 if (dmp1 != NULL) {
c033101d 368 BN_clear_free(r->dmp1);
1da12e34 369 r->dmp1 = dmp1;
311e903d 370 BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
1da12e34
RL
371 }
372 if (dmq1 != NULL) {
c033101d 373 BN_clear_free(r->dmq1);
1da12e34 374 r->dmq1 = dmq1;
311e903d 375 BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
1da12e34
RL
376 }
377 if (iqmp != NULL) {
c033101d 378 BN_clear_free(r->iqmp);
1da12e34 379 r->iqmp = iqmp;
311e903d 380 BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
1da12e34 381 }
9862e9aa
RL
382
383 return 1;
384}
385
665d899f
PY
386/*
387 * Is it better to export RSA_PRIME_INFO structure
388 * and related functions to let user pass a triplet?
389 */
390int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
391 BIGNUM *coeffs[], int pnum)
392{
393 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
394 RSA_PRIME_INFO *pinfo;
395 int i;
396
397 if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
398 return 0;
399
400 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
401 if (prime_infos == NULL)
402 return 0;
403
404 if (r->prime_infos != NULL)
405 old = r->prime_infos;
406
407 for (i = 0; i < pnum; i++) {
408 pinfo = rsa_multip_info_new();
409 if (pinfo == NULL)
410 goto err;
411 if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
412 BN_free(pinfo->r);
413 BN_free(pinfo->d);
414 BN_free(pinfo->t);
415 pinfo->r = primes[i];
416 pinfo->d = exps[i];
417 pinfo->t = coeffs[i];
418 } else {
419 rsa_multip_info_free(pinfo);
420 goto err;
421 }
422 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
423 }
424
425 r->prime_infos = prime_infos;
426
427 if (!rsa_multip_calc_product(r)) {
428 r->prime_infos = old;
429 goto err;
430 }
431
432 if (old != NULL) {
433 /*
434 * This is hard to deal with, since the old infos could
435 * also be set by this function and r, d, t should not
436 * be freed in that case. So currently, stay consistent
437 * with other *set0* functions: just free it...
438 */
439 sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
440 }
441
442 r->version = RSA_ASN1_VERSION_MULTI;
443
444 return 1;
445 err:
446 /* r, d, t should not be freed */
447 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
448 return 0;
449}
450
fd809cfd
RL
451void RSA_get0_key(const RSA *r,
452 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
9862e9aa
RL
453{
454 if (n != NULL)
455 *n = r->n;
456 if (e != NULL)
457 *e = r->e;
458 if (d != NULL)
459 *d = r->d;
460}
461
fd809cfd 462void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
9862e9aa
RL
463{
464 if (p != NULL)
465 *p = r->p;
466 if (q != NULL)
467 *q = r->q;
468}
469
665d899f
PY
470int RSA_get_multi_prime_extra_count(const RSA *r)
471{
472 int pnum;
473
474 pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
475 if (pnum <= 0)
476 pnum = 0;
477 return pnum;
478}
479
480int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
481{
482 int pnum, i;
483 RSA_PRIME_INFO *pinfo;
484
485 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
486 return 0;
487
488 /*
489 * return other primes
490 * it's caller's responsibility to allocate oth_primes[pnum]
491 */
492 for (i = 0; i < pnum; i++) {
493 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
494 primes[i] = pinfo->r;
495 }
496
497 return 1;
498}
499
9862e9aa 500void RSA_get0_crt_params(const RSA *r,
fd809cfd
RL
501 const BIGNUM **dmp1, const BIGNUM **dmq1,
502 const BIGNUM **iqmp)
9862e9aa
RL
503{
504 if (dmp1 != NULL)
505 *dmp1 = r->dmp1;
506 if (dmq1 != NULL)
507 *dmq1 = r->dmq1;
508 if (iqmp != NULL)
509 *iqmp = r->iqmp;
510}
511
665d899f
PY
512int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
513 const BIGNUM *coeffs[])
514{
515 int pnum;
516
517 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
518 return 0;
519
520 /* return other primes */
521 if (exps != NULL || coeffs != NULL) {
522 RSA_PRIME_INFO *pinfo;
523 int i;
524
525 /* it's the user's job to guarantee the buffer length */
526 for (i = 0; i < pnum; i++) {
527 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
528 if (exps != NULL)
529 exps[i] = pinfo->d;
530 if (coeffs != NULL)
531 coeffs[i] = pinfo->t;
532 }
533 }
534
535 return 1;
536}
537
6692ff77
DMSP
538const BIGNUM *RSA_get0_n(const RSA *r)
539{
540 return r->n;
541}
542
543const BIGNUM *RSA_get0_e(const RSA *r)
544{
545 return r->e;
546}
547
548const BIGNUM *RSA_get0_d(const RSA *r)
549{
550 return r->d;
551}
552
553const BIGNUM *RSA_get0_p(const RSA *r)
554{
555 return r->p;
556}
557
558const BIGNUM *RSA_get0_q(const RSA *r)
559{
560 return r->q;
561}
562
563const BIGNUM *RSA_get0_dmp1(const RSA *r)
564{
565 return r->dmp1;
566}
567
568const BIGNUM *RSA_get0_dmq1(const RSA *r)
569{
570 return r->dmq1;
571}
572
573const BIGNUM *RSA_get0_iqmp(const RSA *r)
574{
575 return r->iqmp;
576}
577
9862e9aa
RL
578void RSA_clear_flags(RSA *r, int flags)
579{
580 r->flags &= ~flags;
581}
582
583int RSA_test_flags(const RSA *r, int flags)
584{
585 return r->flags & flags;
586}
587
588void RSA_set_flags(RSA *r, int flags)
589{
590 r->flags |= flags;
591}
592
665d899f
PY
593int RSA_get_version(RSA *r)
594{
595 /* { two-prime(0), multi(1) } */
596 return r->version;
597}
598
e0685d24 599ENGINE *RSA_get0_engine(const RSA *r)
9862e9aa
RL
600{
601 return r->engine;
602}
e5e04ee3
DSH
603
604int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
605{
606 /* If key type not RSA or RSA-PSS return error */
607 if (ctx != NULL && ctx->pmeth != NULL
608 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
609 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
610 return -1;
611 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
612}