]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rsa/rsa_lib.c
rsa_get0_all_params(): Allow zero CRT params
[thirdparty/openssl.git] / crypto / rsa / rsa_lib.c
1 /*
2 * Copyright 1995-2018 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 /*
11 * RSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include <openssl/crypto.h>
18 #include <openssl/core_names.h>
19 #include <openssl/engine.h>
20 #include <openssl/evp.h>
21 #include "internal/cryptlib.h"
22 #include "internal/refcount.h"
23 #include "crypto/bn.h"
24 #include "crypto/evp.h"
25 #include "crypto/rsa.h"
26 #include "rsa_local.h"
27
28 static RSA *rsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx);
29
30 #ifndef FIPS_MODE
31 RSA *RSA_new(void)
32 {
33 return rsa_new_intern(NULL, NULL);
34 }
35
36 const RSA_METHOD *RSA_get_method(const RSA *rsa)
37 {
38 return rsa->meth;
39 }
40
41 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
42 {
43 /*
44 * NB: The caller is specifically setting a method, so it's not up to us
45 * to deal with which ENGINE it comes from.
46 */
47 const RSA_METHOD *mtmp;
48 mtmp = rsa->meth;
49 if (mtmp->finish)
50 mtmp->finish(rsa);
51 #ifndef OPENSSL_NO_ENGINE
52 ENGINE_finish(rsa->engine);
53 rsa->engine = NULL;
54 #endif
55 rsa->meth = meth;
56 if (meth->init)
57 meth->init(rsa);
58 return 1;
59 }
60
61 RSA *RSA_new_method(ENGINE *engine)
62 {
63 return rsa_new_intern(engine, NULL);
64 }
65 #endif
66
67 RSA *rsa_new_with_ctx(OPENSSL_CTX *libctx)
68 {
69 return rsa_new_intern(NULL, libctx);
70 }
71
72 static RSA *rsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
73 {
74 RSA *ret = OPENSSL_zalloc(sizeof(*ret));
75
76 if (ret == NULL) {
77 RSAerr(0, ERR_R_MALLOC_FAILURE);
78 return NULL;
79 }
80
81 ret->references = 1;
82 ret->lock = CRYPTO_THREAD_lock_new();
83 if (ret->lock == NULL) {
84 RSAerr(0, ERR_R_MALLOC_FAILURE);
85 OPENSSL_free(ret);
86 return NULL;
87 }
88
89 ret->libctx = libctx;
90 ret->meth = RSA_get_default_method();
91 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
92 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
93 if (engine) {
94 if (!ENGINE_init(engine)) {
95 RSAerr(0, ERR_R_ENGINE_LIB);
96 goto err;
97 }
98 ret->engine = engine;
99 } else {
100 ret->engine = ENGINE_get_default_RSA();
101 }
102 if (ret->engine) {
103 ret->meth = ENGINE_get_RSA(ret->engine);
104 if (ret->meth == NULL) {
105 RSAerr(0, ERR_R_ENGINE_LIB);
106 goto err;
107 }
108 }
109 #endif
110
111 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
112 #ifndef FIPS_MODE
113 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
114 goto err;
115 }
116 #endif
117
118 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
119 RSAerr(0, ERR_R_INIT_FAIL);
120 goto err;
121 }
122
123 return ret;
124
125 err:
126 RSA_free(ret);
127 return NULL;
128 }
129
130 void RSA_free(RSA *r)
131 {
132 int i;
133
134 if (r == NULL)
135 return;
136
137 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
138 REF_PRINT_COUNT("RSA", r);
139 if (i > 0)
140 return;
141 REF_ASSERT_ISNT(i < 0);
142
143 if (r->meth != NULL && r->meth->finish != NULL)
144 r->meth->finish(r);
145 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
146 ENGINE_finish(r->engine);
147 #endif
148
149 #ifndef FIPS_MODE
150 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
151 #endif
152
153 CRYPTO_THREAD_lock_free(r->lock);
154
155 BN_free(r->n);
156 BN_free(r->e);
157 BN_clear_free(r->d);
158 BN_clear_free(r->p);
159 BN_clear_free(r->q);
160 BN_clear_free(r->dmp1);
161 BN_clear_free(r->dmq1);
162 BN_clear_free(r->iqmp);
163 /* TODO(3.0): Support PSS in FIPS_MODE */
164 #ifndef FIPS_MODE
165 RSA_PSS_PARAMS_free(r->pss);
166 sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
167 #endif
168 BN_BLINDING_free(r->blinding);
169 BN_BLINDING_free(r->mt_blinding);
170 OPENSSL_free(r->bignum_data);
171 OPENSSL_free(r);
172 }
173
174 int RSA_up_ref(RSA *r)
175 {
176 int i;
177
178 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
179 return 0;
180
181 REF_PRINT_COUNT("RSA", r);
182 REF_ASSERT_ISNT(i < 2);
183 return i > 1 ? 1 : 0;
184 }
185
186 #ifndef FIPS_MODE
187 int RSA_set_ex_data(RSA *r, int idx, void *arg)
188 {
189 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
190 }
191
192 void *RSA_get_ex_data(const RSA *r, int idx)
193 {
194 return CRYPTO_get_ex_data(&r->ex_data, idx);
195 }
196 #endif
197
198 /*
199 * Define a scaling constant for our fixed point arithmetic.
200 * This value must be a power of two because the base two logarithm code
201 * makes this assumption. The exponent must also be a multiple of three so
202 * that the scale factor has an exact cube root. Finally, the scale factor
203 * should not be so large that a multiplication of two scaled numbers
204 * overflows a 64 bit unsigned integer.
205 */
206 static const unsigned int scale = 1 << 18;
207 static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
208
209 /* Define some constants, none exceed 32 bits */
210 static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */
211 static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */
212 static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */
213 static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */
214
215 /*
216 * Multiply two scaled integers together and rescale the result.
217 */
218 static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
219 {
220 return a * b / scale;
221 }
222
223 /*
224 * Calculate the cube root of a 64 bit scaled integer.
225 * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
226 * integer, this is not guaranteed after scaling, so this function has a
227 * 64 bit return. This uses the shifting nth root algorithm with some
228 * algebraic simplifications.
229 */
230 static uint64_t icbrt64(uint64_t x)
231 {
232 uint64_t r = 0;
233 uint64_t b;
234 int s;
235
236 for (s = 63; s >= 0; s -= 3) {
237 r <<= 1;
238 b = 3 * r * (r + 1) + 1;
239 if ((x >> s) >= b) {
240 x -= b << s;
241 r++;
242 }
243 }
244 return r * cbrt_scale;
245 }
246
247 /*
248 * Calculate the natural logarithm of a 64 bit scaled integer.
249 * This is done by calculating a base two logarithm and scaling.
250 * The maximum logarithm (base 2) is 64 and this reduces base e, so
251 * a 32 bit result should not overflow. The argument passed must be
252 * greater than unity so we don't need to handle negative results.
253 */
254 static uint32_t ilog_e(uint64_t v)
255 {
256 uint32_t i, r = 0;
257
258 /*
259 * Scale down the value into the range 1 .. 2.
260 *
261 * If fractional numbers need to be processed, another loop needs
262 * to go here that checks v < scale and if so multiplies it by 2 and
263 * reduces r by scale. This also means making r signed.
264 */
265 while (v >= 2 * scale) {
266 v >>= 1;
267 r += scale;
268 }
269 for (i = scale / 2; i != 0; i /= 2) {
270 v = mul2(v, v);
271 if (v >= 2 * scale) {
272 v >>= 1;
273 r += i;
274 }
275 }
276 r = (r * (uint64_t)scale) / log_e;
277 return r;
278 }
279
280 /*
281 * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
282 * Modulus Lengths.
283 *
284 * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
285 * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
286 * The two cube roots are merged together here.
287 */
288 uint16_t rsa_compute_security_bits(int n)
289 {
290 uint64_t x;
291 uint32_t lx;
292 uint16_t y;
293
294 /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
295 switch (n) {
296 case 2048:
297 return 112;
298 case 3072:
299 return 128;
300 case 4096:
301 return 152;
302 case 6144:
303 return 176;
304 case 8192:
305 return 200;
306 }
307 /*
308 * The first incorrect result (i.e. not accurate or off by one low) occurs
309 * for n = 699668. The true value here is 1200. Instead of using this n
310 * as the check threshold, the smallest n such that the correct result is
311 * 1200 is used instead.
312 */
313 if (n >= 687737)
314 return 1200;
315 if (n < 8)
316 return 0;
317
318 x = n * (uint64_t)log_2;
319 lx = ilog_e(x);
320 y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
321 / log_2);
322 return (y + 4) & ~7;
323 }
324
325 int RSA_security_bits(const RSA *rsa)
326 {
327 int bits = BN_num_bits(rsa->n);
328
329 #ifndef FIPS_MODE
330 if (rsa->version == RSA_ASN1_VERSION_MULTI) {
331 /* This ought to mean that we have private key at hand. */
332 int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
333
334 if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
335 return 0;
336 }
337 #endif
338 return rsa_compute_security_bits(bits);
339 }
340
341 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
342 {
343 /* If the fields n and e in r are NULL, the corresponding input
344 * parameters MUST be non-NULL for n and e. d may be
345 * left NULL (in case only the public key is used).
346 */
347 if ((r->n == NULL && n == NULL)
348 || (r->e == NULL && e == NULL))
349 return 0;
350
351 if (n != NULL) {
352 BN_free(r->n);
353 r->n = n;
354 }
355 if (e != NULL) {
356 BN_free(r->e);
357 r->e = e;
358 }
359 if (d != NULL) {
360 BN_clear_free(r->d);
361 r->d = d;
362 BN_set_flags(r->d, BN_FLG_CONSTTIME);
363 }
364 r->dirty_cnt++;
365
366 return 1;
367 }
368
369 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
370 {
371 /* If the fields p and q in r are NULL, the corresponding input
372 * parameters MUST be non-NULL.
373 */
374 if ((r->p == NULL && p == NULL)
375 || (r->q == NULL && q == NULL))
376 return 0;
377
378 if (p != NULL) {
379 BN_clear_free(r->p);
380 r->p = p;
381 BN_set_flags(r->p, BN_FLG_CONSTTIME);
382 }
383 if (q != NULL) {
384 BN_clear_free(r->q);
385 r->q = q;
386 BN_set_flags(r->q, BN_FLG_CONSTTIME);
387 }
388 r->dirty_cnt++;
389
390 return 1;
391 }
392
393 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
394 {
395 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
396 * parameters MUST be non-NULL.
397 */
398 if ((r->dmp1 == NULL && dmp1 == NULL)
399 || (r->dmq1 == NULL && dmq1 == NULL)
400 || (r->iqmp == NULL && iqmp == NULL))
401 return 0;
402
403 if (dmp1 != NULL) {
404 BN_clear_free(r->dmp1);
405 r->dmp1 = dmp1;
406 BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
407 }
408 if (dmq1 != NULL) {
409 BN_clear_free(r->dmq1);
410 r->dmq1 = dmq1;
411 BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
412 }
413 if (iqmp != NULL) {
414 BN_clear_free(r->iqmp);
415 r->iqmp = iqmp;
416 BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
417 }
418 r->dirty_cnt++;
419
420 return 1;
421 }
422
423 #ifndef FIPS_MODE
424 /*
425 * Is it better to export RSA_PRIME_INFO structure
426 * and related functions to let user pass a triplet?
427 */
428 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
429 BIGNUM *coeffs[], int pnum)
430 {
431 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
432 RSA_PRIME_INFO *pinfo;
433 int i;
434
435 if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
436 return 0;
437
438 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
439 if (prime_infos == NULL)
440 return 0;
441
442 if (r->prime_infos != NULL)
443 old = r->prime_infos;
444
445 for (i = 0; i < pnum; i++) {
446 pinfo = rsa_multip_info_new();
447 if (pinfo == NULL)
448 goto err;
449 if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
450 BN_clear_free(pinfo->r);
451 BN_clear_free(pinfo->d);
452 BN_clear_free(pinfo->t);
453 pinfo->r = primes[i];
454 pinfo->d = exps[i];
455 pinfo->t = coeffs[i];
456 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
457 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
458 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
459 } else {
460 rsa_multip_info_free(pinfo);
461 goto err;
462 }
463 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
464 }
465
466 r->prime_infos = prime_infos;
467
468 if (!rsa_multip_calc_product(r)) {
469 r->prime_infos = old;
470 goto err;
471 }
472
473 if (old != NULL) {
474 /*
475 * This is hard to deal with, since the old infos could
476 * also be set by this function and r, d, t should not
477 * be freed in that case. So currently, stay consistent
478 * with other *set0* functions: just free it...
479 */
480 sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
481 }
482
483 r->version = RSA_ASN1_VERSION_MULTI;
484 r->dirty_cnt++;
485
486 return 1;
487 err:
488 /* r, d, t should not be freed */
489 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
490 return 0;
491 }
492 #endif
493
494 void RSA_get0_key(const RSA *r,
495 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
496 {
497 if (n != NULL)
498 *n = r->n;
499 if (e != NULL)
500 *e = r->e;
501 if (d != NULL)
502 *d = r->d;
503 }
504
505 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
506 {
507 if (p != NULL)
508 *p = r->p;
509 if (q != NULL)
510 *q = r->q;
511 }
512
513 #ifndef FIPS_MODE
514 int RSA_get_multi_prime_extra_count(const RSA *r)
515 {
516 int pnum;
517
518 pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
519 if (pnum <= 0)
520 pnum = 0;
521 return pnum;
522 }
523
524 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
525 {
526 int pnum, i;
527 RSA_PRIME_INFO *pinfo;
528
529 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
530 return 0;
531
532 /*
533 * return other primes
534 * it's caller's responsibility to allocate oth_primes[pnum]
535 */
536 for (i = 0; i < pnum; i++) {
537 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
538 primes[i] = pinfo->r;
539 }
540
541 return 1;
542 }
543 #endif
544
545 void RSA_get0_crt_params(const RSA *r,
546 const BIGNUM **dmp1, const BIGNUM **dmq1,
547 const BIGNUM **iqmp)
548 {
549 if (dmp1 != NULL)
550 *dmp1 = r->dmp1;
551 if (dmq1 != NULL)
552 *dmq1 = r->dmq1;
553 if (iqmp != NULL)
554 *iqmp = r->iqmp;
555 }
556
557 #ifndef FIPS_MODE
558 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
559 const BIGNUM *coeffs[])
560 {
561 int pnum;
562
563 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
564 return 0;
565
566 /* return other primes */
567 if (exps != NULL || coeffs != NULL) {
568 RSA_PRIME_INFO *pinfo;
569 int i;
570
571 /* it's the user's job to guarantee the buffer length */
572 for (i = 0; i < pnum; i++) {
573 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
574 if (exps != NULL)
575 exps[i] = pinfo->d;
576 if (coeffs != NULL)
577 coeffs[i] = pinfo->t;
578 }
579 }
580
581 return 1;
582 }
583 #endif
584
585 const BIGNUM *RSA_get0_n(const RSA *r)
586 {
587 return r->n;
588 }
589
590 const BIGNUM *RSA_get0_e(const RSA *r)
591 {
592 return r->e;
593 }
594
595 const BIGNUM *RSA_get0_d(const RSA *r)
596 {
597 return r->d;
598 }
599
600 const BIGNUM *RSA_get0_p(const RSA *r)
601 {
602 return r->p;
603 }
604
605 const BIGNUM *RSA_get0_q(const RSA *r)
606 {
607 return r->q;
608 }
609
610 const BIGNUM *RSA_get0_dmp1(const RSA *r)
611 {
612 return r->dmp1;
613 }
614
615 const BIGNUM *RSA_get0_dmq1(const RSA *r)
616 {
617 return r->dmq1;
618 }
619
620 const BIGNUM *RSA_get0_iqmp(const RSA *r)
621 {
622 return r->iqmp;
623 }
624
625 /* TODO(3.0): Temporary until we move PSS support into the FIPS module */
626 #ifndef FIPS_MODE
627 const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
628 {
629 return r->pss;
630 }
631 #endif
632
633 void RSA_clear_flags(RSA *r, int flags)
634 {
635 r->flags &= ~flags;
636 }
637
638 int RSA_test_flags(const RSA *r, int flags)
639 {
640 return r->flags & flags;
641 }
642
643 void RSA_set_flags(RSA *r, int flags)
644 {
645 r->flags |= flags;
646 }
647
648 int RSA_get_version(RSA *r)
649 {
650 /* { two-prime(0), multi(1) } */
651 return r->version;
652 }
653
654 #ifndef FIPS_MODE
655 ENGINE *RSA_get0_engine(const RSA *r)
656 {
657 return r->engine;
658 }
659
660 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
661 {
662 /* If key type not RSA or RSA-PSS return error */
663 if (ctx != NULL && ctx->pmeth != NULL
664 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
665 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
666 return -1;
667 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
668 }
669 #endif
670
671 DEFINE_STACK_OF(BIGNUM)
672
673 int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
674 const STACK_OF(BIGNUM) *exps,
675 const STACK_OF(BIGNUM) *coeffs)
676 {
677 #ifndef FIPS_MODE
678 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
679 #endif
680 int pnum;
681
682 if (primes == NULL || exps == NULL || coeffs == NULL)
683 return 0;
684
685 pnum = sk_BIGNUM_num(primes);
686 if (pnum < 2
687 || pnum != sk_BIGNUM_num(exps)
688 || pnum != sk_BIGNUM_num(coeffs) + 1)
689 return 0;
690
691 if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
692 sk_BIGNUM_value(primes, 1))
693 || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
694 sk_BIGNUM_value(exps, 1),
695 sk_BIGNUM_value(coeffs, 0)))
696 return 0;
697
698 #ifndef FIPS_MODE
699 old_infos = r->prime_infos;
700 #endif
701
702 if (pnum > 2) {
703 #ifndef FIPS_MODE
704 int i;
705
706 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
707 if (prime_infos == NULL)
708 return 0;
709
710 for (i = 2; i < pnum; i++) {
711 BIGNUM *prime = sk_BIGNUM_value(primes, i);
712 BIGNUM *exp = sk_BIGNUM_value(exps, i);
713 BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
714 RSA_PRIME_INFO *pinfo = NULL;
715
716 if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
717 goto err;
718
719 /* Using rsa_multip_info_new() is wasteful, so allocate directly */
720 if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
721 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
722 goto err;
723 }
724
725 pinfo->r = prime;
726 pinfo->d = exp;
727 pinfo->t = coeff;
728 BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
729 BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
730 BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
731 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
732 }
733
734 r->prime_infos = prime_infos;
735
736 if (!rsa_multip_calc_product(r)) {
737 r->prime_infos = old_infos;
738 goto err;
739 }
740 #else
741 return 0;
742 #endif
743 }
744
745 #ifndef FIPS_MODE
746 if (old_infos != NULL) {
747 /*
748 * This is hard to deal with, since the old infos could
749 * also be set by this function and r, d, t should not
750 * be freed in that case. So currently, stay consistent
751 * with other *set0* functions: just free it...
752 */
753 sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
754 }
755 #endif
756
757 r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
758 r->dirty_cnt++;
759
760 return 1;
761 #ifndef FIPS_MODE
762 err:
763 /* r, d, t should not be freed */
764 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
765 return 0;
766 #endif
767 }
768
769 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
770
771 int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
772 STACK_OF(BIGNUM_const) *exps,
773 STACK_OF(BIGNUM_const) *coeffs)
774 {
775 #ifndef FIPS_MODE
776 RSA_PRIME_INFO *pinfo;
777 int i, pnum;
778 #endif
779
780 if (r == NULL)
781 return 0;
782
783 /* If |p| is NULL, there are no CRT parameters */
784 if (RSA_get0_p(r) == NULL)
785 return 1;
786
787 sk_BIGNUM_const_push(primes, RSA_get0_p(r));
788 sk_BIGNUM_const_push(primes, RSA_get0_q(r));
789 sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
790 sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
791 sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
792
793 #ifndef FIPS_MODE
794 pnum = RSA_get_multi_prime_extra_count(r);
795 for (i = 0; i < pnum; i++) {
796 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
797 sk_BIGNUM_const_push(primes, pinfo->r);
798 sk_BIGNUM_const_push(exps, pinfo->d);
799 sk_BIGNUM_const_push(coeffs, pinfo->t);
800 }
801 #endif
802
803 return 1;
804 }
805
806 #ifndef FIPS_MODE
807 int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
808 {
809 OSSL_PARAM pad_params[2], *p = pad_params;
810
811 if (ctx == NULL) {
812 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
813 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
814 return -2;
815 }
816
817 /* If key type not RSA or RSA-PSS return error */
818 if (ctx->pmeth != NULL
819 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
820 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
821 return -1;
822
823 /* TODO(3.0): Remove this eventually when no more legacy */
824 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
825 || ctx->op.ciph.ciphprovctx == NULL)
826 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
827 || ctx->op.sig.sigprovctx == NULL))
828 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING,
829 pad_mode, NULL);
830
831 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, &pad_mode);
832 *p++ = OSSL_PARAM_construct_end();
833
834 return EVP_PKEY_CTX_set_params(ctx, pad_params);
835 }
836
837 int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
838 {
839 OSSL_PARAM pad_params[2], *p = pad_params;
840
841 if (ctx == NULL || pad_mode == NULL) {
842 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
843 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
844 return -2;
845 }
846
847 /* If key type not RSA or RSA-PSS return error */
848 if (ctx->pmeth != NULL
849 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
850 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
851 return -1;
852
853 /* TODO(3.0): Remove this eventually when no more legacy */
854 if ((!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
855 || ctx->op.ciph.ciphprovctx == NULL)
856 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
857 || ctx->op.sig.sigprovctx == NULL))
858 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0,
859 pad_mode);
860
861 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE, pad_mode);
862 *p++ = OSSL_PARAM_construct_end();
863
864 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
865 return 0;
866
867 return 1;
868
869 }
870
871 int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
872 {
873 const char *name;
874
875 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
876 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
877 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
878 return -2;
879 }
880
881 /* If key type not RSA return error */
882 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
883 return -1;
884
885 /* TODO(3.0): Remove this eventually when no more legacy */
886 if (ctx->op.ciph.ciphprovctx == NULL)
887 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
888 EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
889
890 name = (md == NULL) ? "" : EVP_MD_name(md);
891
892 return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL);
893 }
894
895 int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
896 const char *mdprops)
897 {
898 OSSL_PARAM rsa_params[3], *p = rsa_params;
899
900 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
901 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
902 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
903 return -2;
904 }
905
906 /* If key type not RSA return error */
907 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
908 return -1;
909
910
911 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
912 /*
913 * Cast away the const. This is read
914 * only so should be safe
915 */
916 (char *)mdname, 0);
917 if (mdprops != NULL) {
918 *p++ = OSSL_PARAM_construct_utf8_string(
919 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
920 /*
921 * Cast away the const. This is read
922 * only so should be safe
923 */
924 (char *)mdprops, 0);
925 }
926 *p++ = OSSL_PARAM_construct_end();
927
928 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
929 }
930
931 int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
932 size_t namelen)
933 {
934 OSSL_PARAM rsa_params[2], *p = rsa_params;
935
936 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
937 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
938 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
939 return -2;
940 }
941
942 /* If key type not RSA return error */
943 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
944 return -1;
945
946 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
947 name, namelen);
948 *p++ = OSSL_PARAM_construct_end();
949
950 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
951 return -1;
952
953 return 1;
954 }
955
956 int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
957 {
958 /* 80 should be big enough */
959 char name[80] = "";
960
961 if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
962 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
963 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
964 return -2;
965 }
966
967 /* If key type not RSA return error */
968 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
969 return -1;
970
971 /* TODO(3.0): Remove this eventually when no more legacy */
972 if (ctx->op.ciph.ciphprovctx == NULL)
973 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
974 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
975
976 if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0)
977 return -1;
978
979 /* May be NULL meaning "unknown" */
980 *md = EVP_get_digestbyname(name);
981
982 return 1;
983 }
984
985 int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
986 {
987 const char *name;
988
989 if (ctx == NULL
990 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
991 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
992 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
993 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
994 return -2;
995 }
996
997 /* If key type not RSA return error */
998 if (ctx->pmeth != NULL
999 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1000 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1001 return -1;
1002
1003 /* TODO(3.0): Remove this eventually when no more legacy */
1004 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1005 && ctx->op.ciph.ciphprovctx == NULL)
1006 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1007 && ctx->op.sig.sigprovctx == NULL))
1008 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
1009 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1010 EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
1011
1012 name = (md == NULL) ? "" : EVP_MD_name(md);
1013
1014 return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, name, NULL);
1015 }
1016
1017 int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
1018 const char *mdprops)
1019 {
1020 OSSL_PARAM rsa_params[3], *p = rsa_params;
1021
1022 if (ctx == NULL
1023 || mdname == NULL
1024 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1025 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1026 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1027 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1028 return -2;
1029 }
1030
1031 /* If key type not RSA return error */
1032 if (ctx->pmeth != NULL
1033 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1034 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1035 return -1;
1036
1037 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1038 /*
1039 * Cast away the const. This is
1040 * read only so should be safe
1041 */
1042 (char *)mdname, 0);
1043 if (mdprops != NULL) {
1044 *p++ =
1045 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_PROPERTIES,
1046 /*
1047 * Cast away the const. This is
1048 * read only so should be safe
1049 */
1050 (char *)mdprops, 0);
1051 }
1052 *p++ = OSSL_PARAM_construct_end();
1053
1054 return EVP_PKEY_CTX_set_params(ctx, rsa_params);
1055 }
1056
1057 int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
1058 size_t namelen)
1059 {
1060 OSSL_PARAM rsa_params[2], *p = rsa_params;
1061
1062 if (ctx == NULL
1063 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1064 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1065 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1066 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1067 return -2;
1068 }
1069
1070 /* If key type not RSA or RSA-PSS return error */
1071 if (ctx->pmeth != NULL
1072 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1073 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1074 return -1;
1075
1076 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MGF1_DIGEST,
1077 name, namelen);
1078 *p++ = OSSL_PARAM_construct_end();
1079
1080 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1081 return -1;
1082
1083 return 1;
1084 }
1085
1086 int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
1087 {
1088 /* 80 should be big enough */
1089 char name[80] = "";
1090
1091 if (ctx == NULL
1092 || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1093 && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
1094 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1095 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1096 return -2;
1097 }
1098
1099 /* If key type not RSA or RSA-PSS return error */
1100 if (ctx->pmeth != NULL
1101 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1102 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1103 return -1;
1104
1105 /* TODO(3.0): Remove this eventually when no more legacy */
1106 if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
1107 && ctx->op.ciph.ciphprovctx == NULL)
1108 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1109 && ctx->op.sig.sigprovctx == NULL))
1110 return EVP_PKEY_CTX_ctrl(ctx, -1,
1111 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1112 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md);
1113
1114 if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0)
1115 return -1;
1116
1117 /* May be NULL meaning "unknown" */
1118 *md = EVP_get_digestbyname(name);
1119
1120 return 1;
1121 }
1122
1123 int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
1124 {
1125 OSSL_PARAM rsa_params[2], *p = rsa_params;
1126
1127 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1128 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1129 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1130 return -2;
1131 }
1132
1133 /* If key type not RSA return error */
1134 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1135 return -1;
1136
1137 /* TODO(3.0): Remove this eventually when no more legacy */
1138 if (ctx->op.ciph.ciphprovctx == NULL)
1139 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1140 EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen,
1141 (void *)label);
1142
1143 *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1144 /*
1145 * Cast away the const. This is
1146 * read only so should be safe
1147 */
1148 (void *)label,
1149 (size_t)llen);
1150 *p++ = OSSL_PARAM_construct_end();
1151
1152 if (!EVP_PKEY_CTX_set_params(ctx, rsa_params))
1153 return 0;
1154
1155 OPENSSL_free(label);
1156 return 1;
1157 }
1158
1159 int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
1160 {
1161 OSSL_PARAM rsa_params[3], *p = rsa_params;
1162 size_t labellen;
1163
1164 if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
1165 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1166 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1167 return -2;
1168 }
1169
1170 /* If key type not RSA return error */
1171 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
1172 return -1;
1173
1174 /* TODO(3.0): Remove this eventually when no more legacy */
1175 if (ctx->op.ciph.ciphprovctx == NULL)
1176 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1177 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0,
1178 (void *)label);
1179
1180 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
1181 (void **)label, 0);
1182 *p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN,
1183 &labellen);
1184 *p++ = OSSL_PARAM_construct_end();
1185
1186 if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
1187 return -1;
1188
1189 if (labellen > INT_MAX)
1190 return -1;
1191
1192 return (int)labellen;
1193 }
1194
1195 int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
1196 {
1197 OSSL_PARAM pad_params[2], *p = pad_params;
1198
1199 if (ctx == NULL) {
1200 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1201 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1202 return -2;
1203 }
1204
1205 /* If key type not RSA or RSA-PSS return error */
1206 if (ctx->pmeth != NULL
1207 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1208 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1209 return -1;
1210
1211 /* TODO(3.0): Remove this eventually when no more legacy */
1212 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1213 || ctx->op.sig.sigprovctx == NULL)
1214 return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PSS_SALTLEN,
1215 saltlen, NULL);
1216
1217 *p++ =
1218 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, &saltlen);
1219 *p++ = OSSL_PARAM_construct_end();
1220
1221 return EVP_PKEY_CTX_set_params(ctx, pad_params);
1222 }
1223
1224 int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
1225 {
1226 OSSL_PARAM pad_params[2], *p = pad_params;
1227
1228 if (ctx == NULL || saltlen == NULL) {
1229 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1230 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1231 return -2;
1232 }
1233
1234 /* If key type not RSA or RSA-PSS return error */
1235 if (ctx->pmeth != NULL
1236 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
1237 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
1238 return -1;
1239
1240 /* TODO(3.0): Remove this eventually when no more legacy */
1241 if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
1242 || ctx->op.sig.sigprovctx == NULL)
1243 return EVP_PKEY_CTX_ctrl(ctx, -1, -1,
1244 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN,
1245 0, saltlen);
1246
1247 *p++ =
1248 OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, saltlen);
1249 *p++ = OSSL_PARAM_construct_end();
1250
1251 if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
1252 return 0;
1253
1254 return 1;
1255
1256 }
1257 #endif