xref: /freebsd/crypto/openssl/crypto/rsa/rsa_ossl.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 1995-2024 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 "internal/cryptlib.h"
17 #include "crypto/bn.h"
18 #include "rsa_local.h"
19 #include "internal/constant_time.h"
20 #include <openssl/evp.h>
21 #include <openssl/sha.h>
22 #include <openssl/hmac.h>
23 
24 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
25     unsigned char *to, RSA *rsa, int padding);
26 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
27     unsigned char *to, RSA *rsa, int padding);
28 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
29     unsigned char *to, RSA *rsa, int padding);
30 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
31     unsigned char *to, RSA *rsa, int padding);
32 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
33     BN_CTX *ctx);
34 static int rsa_ossl_init(RSA *rsa);
35 static int rsa_ossl_finish(RSA *rsa);
36 #ifdef S390X_MOD_EXP
37 static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
38     BN_CTX *ctx);
39 static RSA_METHOD rsa_pkcs1_ossl_meth = {
40     "OpenSSL PKCS#1 RSA",
41     rsa_ossl_public_encrypt,
42     rsa_ossl_public_decrypt, /* signature verification */
43     rsa_ossl_private_encrypt, /* signing */
44     rsa_ossl_private_decrypt,
45     rsa_ossl_s390x_mod_exp,
46     s390x_mod_exp,
47     rsa_ossl_init,
48     rsa_ossl_finish,
49     RSA_FLAG_FIPS_METHOD, /* flags */
50     NULL,
51     0, /* rsa_sign */
52     0, /* rsa_verify */
53     NULL, /* rsa_keygen */
54     NULL /* rsa_multi_prime_keygen */
55 };
56 #else
57 static RSA_METHOD rsa_pkcs1_ossl_meth = {
58     "OpenSSL PKCS#1 RSA",
59     rsa_ossl_public_encrypt,
60     rsa_ossl_public_decrypt, /* signature verification */
61     rsa_ossl_private_encrypt, /* signing */
62     rsa_ossl_private_decrypt,
63     rsa_ossl_mod_exp,
64     BN_mod_exp_mont, /* XXX probably we should not use Montgomery
65                       * if e == 3 */
66     rsa_ossl_init,
67     rsa_ossl_finish,
68     RSA_FLAG_FIPS_METHOD, /* flags */
69     NULL,
70     0, /* rsa_sign */
71     0, /* rsa_verify */
72     NULL, /* rsa_keygen */
73     NULL /* rsa_multi_prime_keygen */
74 };
75 #endif
76 
77 static const RSA_METHOD *default_RSA_meth = &rsa_pkcs1_ossl_meth;
78 
RSA_set_default_method(const RSA_METHOD * meth)79 void RSA_set_default_method(const RSA_METHOD *meth)
80 {
81     default_RSA_meth = meth;
82 }
83 
RSA_get_default_method(void)84 const RSA_METHOD *RSA_get_default_method(void)
85 {
86     return default_RSA_meth;
87 }
88 
RSA_PKCS1_OpenSSL(void)89 const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
90 {
91     return &rsa_pkcs1_ossl_meth;
92 }
93 
RSA_null_method(void)94 const RSA_METHOD *RSA_null_method(void)
95 {
96     return NULL;
97 }
98 
rsa_ossl_public_encrypt(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)99 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
100     unsigned char *to, RSA *rsa, int padding)
101 {
102     BIGNUM *f, *ret;
103     int i, num = 0, r = -1;
104     unsigned char *buf = NULL;
105     BN_CTX *ctx = NULL;
106 
107     if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
108         ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
109         return -1;
110     }
111 
112     if (BN_ucmp(rsa->n, rsa->e) <= 0) {
113         ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
114         return -1;
115     }
116 
117     /* for large moduli, enforce exponent limit */
118     if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
119         if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
120             ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
121             return -1;
122         }
123     }
124 
125     if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
126         goto err;
127     BN_CTX_start(ctx);
128     f = BN_CTX_get(ctx);
129     ret = BN_CTX_get(ctx);
130     num = BN_num_bytes(rsa->n);
131     buf = OPENSSL_malloc(num);
132     if (ret == NULL || buf == NULL)
133         goto err;
134 
135     switch (padding) {
136     case RSA_PKCS1_PADDING:
137         i = ossl_rsa_padding_add_PKCS1_type_2_ex(rsa->libctx, buf, num,
138             from, flen);
139         break;
140     case RSA_PKCS1_OAEP_PADDING:
141         i = ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(rsa->libctx, buf, num,
142             from, flen, NULL, 0,
143             NULL, NULL);
144         break;
145     case RSA_NO_PADDING:
146         i = RSA_padding_add_none(buf, num, from, flen);
147         break;
148     default:
149         ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
150         goto err;
151     }
152     if (i <= 0)
153         goto err;
154 
155     if (BN_bin2bn(buf, num, f) == NULL)
156         goto err;
157 
158 #ifdef FIPS_MODULE
159     /*
160      * See SP800-56Br2, section 7.1.1.1
161      * RSAEP: 1 < f < (n – 1).
162      * (where f is the plaintext).
163      */
164     if (padding == RSA_NO_PADDING) {
165         BIGNUM *nminus1 = BN_CTX_get(ctx);
166 
167         if (BN_ucmp(f, BN_value_one()) <= 0) {
168             ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
169             goto err;
170         }
171         if (nminus1 == NULL
172             || BN_copy(nminus1, rsa->n) == NULL
173             || !BN_sub_word(nminus1, 1))
174             goto err;
175         if (BN_ucmp(f, nminus1) >= 0) {
176             ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
177             goto err;
178         }
179     } else
180 #endif
181     {
182         if (BN_ucmp(f, rsa->n) >= 0) {
183             /* usually the padding functions would catch this */
184             ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
185             goto err;
186         }
187     }
188 
189     if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
190         if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
191                 rsa->n, ctx))
192             goto err;
193 
194     if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
195             rsa->_method_mod_n))
196         goto err;
197 
198     /*
199      * BN_bn2binpad puts in leading 0 bytes if the number is less than
200      * the length of the modulus.
201      */
202     r = BN_bn2binpad(ret, to, num);
203 err:
204     BN_CTX_end(ctx);
205     BN_CTX_free(ctx);
206     OPENSSL_clear_free(buf, num);
207     return r;
208 }
209 
rsa_get_blinding(RSA * rsa,int * local,BN_CTX * ctx)210 static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
211 {
212     BN_BLINDING *ret;
213 
214     if (!CRYPTO_THREAD_read_lock(rsa->lock))
215         return NULL;
216 
217     if (rsa->blinding == NULL) {
218         /*
219          * This dance with upgrading the lock from read to write will be
220          * slower in cases of a single use RSA object, but should be
221          * significantly better in multi-thread cases (e.g. servers). It's
222          * probably worth it.
223          */
224         CRYPTO_THREAD_unlock(rsa->lock);
225         if (!CRYPTO_THREAD_write_lock(rsa->lock))
226             return NULL;
227         if (rsa->blinding == NULL)
228             rsa->blinding = RSA_setup_blinding(rsa, ctx);
229     }
230 
231     ret = rsa->blinding;
232     if (ret == NULL)
233         goto err;
234 
235     if (BN_BLINDING_is_current_thread(ret)) {
236         /* rsa->blinding is ours! */
237 
238         *local = 1;
239     } else {
240         /* resort to rsa->mt_blinding instead */
241 
242         /*
243          * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
244          * BN_BLINDING is shared, meaning that accesses require locks, and
245          * that the blinding factor must be stored outside the BN_BLINDING
246          */
247         *local = 0;
248 
249         if (rsa->mt_blinding == NULL) {
250             CRYPTO_THREAD_unlock(rsa->lock);
251             if (!CRYPTO_THREAD_write_lock(rsa->lock))
252                 return NULL;
253             if (rsa->mt_blinding == NULL)
254                 rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
255         }
256         ret = rsa->mt_blinding;
257     }
258 
259 err:
260     CRYPTO_THREAD_unlock(rsa->lock);
261     return ret;
262 }
263 
rsa_blinding_convert(BN_BLINDING * b,BIGNUM * f,BIGNUM * unblind,BN_CTX * ctx)264 static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
265     BN_CTX *ctx)
266 {
267     if (unblind == NULL) {
268         /*
269          * Local blinding: store the unblinding factor in BN_BLINDING.
270          */
271         return BN_BLINDING_convert_ex(f, NULL, b, ctx);
272     } else {
273         /*
274          * Shared blinding: store the unblinding factor outside BN_BLINDING.
275          */
276         int ret;
277 
278         if (!BN_BLINDING_lock(b))
279             return 0;
280 
281         ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
282         BN_BLINDING_unlock(b);
283 
284         return ret;
285     }
286 }
287 
rsa_blinding_invert(BN_BLINDING * b,BIGNUM * f,BIGNUM * unblind,BN_CTX * ctx)288 static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
289     BN_CTX *ctx)
290 {
291     /*
292      * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
293      * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
294      * is shared between threads, unblind must be non-null:
295      * BN_BLINDING_invert_ex will then use the local unblinding factor, and
296      * will only read the modulus from BN_BLINDING. In both cases it's safe
297      * to access the blinding without a lock.
298      */
299     BN_set_flags(f, BN_FLG_CONSTTIME);
300     return BN_BLINDING_invert_ex(f, unblind, b, ctx);
301 }
302 
303 /* signing */
rsa_ossl_private_encrypt(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)304 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
305     unsigned char *to, RSA *rsa, int padding)
306 {
307     BIGNUM *f, *ret, *res;
308     int i, num = 0, r = -1;
309     unsigned char *buf = NULL;
310     BN_CTX *ctx = NULL;
311     int local_blinding = 0;
312     /*
313      * Used only if the blinding structure is shared. A non-NULL unblind
314      * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
315      * the unblinding factor outside the blinding structure.
316      */
317     BIGNUM *unblind = NULL;
318     BN_BLINDING *blinding = NULL;
319 
320     if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
321         goto err;
322     BN_CTX_start(ctx);
323     f = BN_CTX_get(ctx);
324     ret = BN_CTX_get(ctx);
325     num = BN_num_bytes(rsa->n);
326     buf = OPENSSL_malloc(num);
327     if (ret == NULL || buf == NULL)
328         goto err;
329 
330     switch (padding) {
331     case RSA_PKCS1_PADDING:
332         i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
333         break;
334     case RSA_X931_PADDING:
335         i = RSA_padding_add_X931(buf, num, from, flen);
336         break;
337     case RSA_NO_PADDING:
338         i = RSA_padding_add_none(buf, num, from, flen);
339         break;
340     default:
341         ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
342         goto err;
343     }
344     if (i <= 0)
345         goto err;
346 
347     if (BN_bin2bn(buf, num, f) == NULL)
348         goto err;
349 
350     if (BN_ucmp(f, rsa->n) >= 0) {
351         /* usually the padding functions would catch this */
352         ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
353         goto err;
354     }
355 
356     if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
357         if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
358                 rsa->n, ctx))
359             goto err;
360 
361     if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
362         blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
363         if (blinding == NULL) {
364             ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
365             goto err;
366         }
367     }
368 
369     if (blinding != NULL) {
370         if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
371             ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
372             goto err;
373         }
374         if (!rsa_blinding_convert(blinding, f, unblind, ctx))
375             goto err;
376     }
377 
378     if ((rsa->flags & RSA_FLAG_EXT_PKEY) || (rsa->version == RSA_ASN1_VERSION_MULTI) || ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
379         if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
380             goto err;
381     } else {
382         BIGNUM *d = BN_new();
383         if (d == NULL) {
384             ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
385             goto err;
386         }
387         if (rsa->d == NULL) {
388             ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
389             BN_free(d);
390             goto err;
391         }
392         BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
393 
394         if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
395                 rsa->_method_mod_n)) {
396             BN_free(d);
397             goto err;
398         }
399         /* We MUST free d before any further use of rsa->d */
400         BN_free(d);
401     }
402 
403     if (blinding)
404         if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
405             goto err;
406 
407     if (padding == RSA_X931_PADDING) {
408         if (!BN_sub(f, rsa->n, ret))
409             goto err;
410         if (BN_cmp(ret, f) > 0)
411             res = f;
412         else
413             res = ret;
414     } else {
415         res = ret;
416     }
417 
418     /*
419      * BN_bn2binpad puts in leading 0 bytes if the number is less than
420      * the length of the modulus.
421      */
422     r = BN_bn2binpad(res, to, num);
423 err:
424     BN_CTX_end(ctx);
425     BN_CTX_free(ctx);
426     OPENSSL_clear_free(buf, num);
427     return r;
428 }
429 
derive_kdk(int flen,const unsigned char * from,RSA * rsa,unsigned char * buf,int num,unsigned char * kdk)430 static int derive_kdk(int flen, const unsigned char *from, RSA *rsa,
431     unsigned char *buf, int num, unsigned char *kdk)
432 {
433     int ret = 0;
434     HMAC_CTX *hmac = NULL;
435     EVP_MD *md = NULL;
436     unsigned int md_len = SHA256_DIGEST_LENGTH;
437     unsigned char d_hash[SHA256_DIGEST_LENGTH] = { 0 };
438     /*
439      * because we use d as a handle to rsa->d we need to keep it local and
440      * free before any further use of rsa->d
441      */
442     BIGNUM *d = BN_new();
443 
444     if (d == NULL) {
445         ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB);
446         goto err;
447     }
448     if (rsa->d == NULL) {
449         ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
450         BN_free(d);
451         goto err;
452     }
453     BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
454     if (BN_bn2binpad(d, buf, num) < 0) {
455         ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
456         BN_free(d);
457         goto err;
458     }
459     BN_free(d);
460 
461     /*
462      * we use hardcoded hash so that migrating between versions that use
463      * different hash doesn't provide a Bleichenbacher oracle:
464      * if the attacker can see that different versions return different
465      * messages for the same ciphertext, they'll know that the message is
466      * synthetically generated, which means that the padding check failed
467      */
468     md = EVP_MD_fetch(rsa->libctx, "sha256", NULL);
469     if (md == NULL) {
470         ERR_raise(ERR_LIB_RSA, ERR_R_FETCH_FAILED);
471         goto err;
472     }
473 
474     if (EVP_Digest(buf, num, d_hash, NULL, md, NULL) <= 0) {
475         ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
476         goto err;
477     }
478 
479     hmac = HMAC_CTX_new();
480     if (hmac == NULL) {
481         ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB);
482         goto err;
483     }
484 
485     if (HMAC_Init_ex(hmac, d_hash, sizeof(d_hash), md, NULL) <= 0) {
486         ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
487         goto err;
488     }
489 
490     if (flen < num) {
491         memset(buf, 0, num - flen);
492         if (HMAC_Update(hmac, buf, num - flen) <= 0) {
493             ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
494             goto err;
495         }
496     }
497     if (HMAC_Update(hmac, from, flen) <= 0) {
498         ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
499         goto err;
500     }
501 
502     md_len = SHA256_DIGEST_LENGTH;
503     if (HMAC_Final(hmac, kdk, &md_len) <= 0) {
504         ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
505         goto err;
506     }
507     ret = 1;
508 
509 err:
510     HMAC_CTX_free(hmac);
511     EVP_MD_free(md);
512     return ret;
513 }
514 
rsa_ossl_private_decrypt(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)515 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
516     unsigned char *to, RSA *rsa, int padding)
517 {
518     BIGNUM *f, *ret;
519     int j, num = 0, r = -1;
520     unsigned char *buf = NULL;
521     unsigned char kdk[SHA256_DIGEST_LENGTH] = { 0 };
522     BN_CTX *ctx = NULL;
523     int local_blinding = 0;
524     /*
525      * Used only if the blinding structure is shared. A non-NULL unblind
526      * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
527      * the unblinding factor outside the blinding structure.
528      */
529     BIGNUM *unblind = NULL;
530     BN_BLINDING *blinding = NULL;
531 
532     /*
533      * we need the value of the private exponent to perform implicit rejection
534      */
535     if ((rsa->flags & RSA_FLAG_EXT_PKEY) && (padding == RSA_PKCS1_PADDING))
536         padding = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;
537 
538     if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
539         goto err;
540     BN_CTX_start(ctx);
541     f = BN_CTX_get(ctx);
542     ret = BN_CTX_get(ctx);
543     if (ret == NULL) {
544         ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
545         goto err;
546     }
547     num = BN_num_bytes(rsa->n);
548     buf = OPENSSL_malloc(num);
549     if (buf == NULL)
550         goto err;
551 
552     /*
553      * This check was for equality but PGP does evil things and chops off the
554      * top '0' bytes
555      */
556     if (flen > num) {
557         ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
558         goto err;
559     }
560 
561     if (flen < 1) {
562         ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
563         goto err;
564     }
565 
566     /* make data into a big number */
567     if (BN_bin2bn(from, (int)flen, f) == NULL)
568         goto err;
569 
570 #ifdef FIPS_MODULE
571     /*
572      * See SP800-56Br2, section 7.1.2.1
573      * RSADP: 1 < f < (n – 1)
574      * (where f is the ciphertext).
575      */
576     if (padding == RSA_NO_PADDING) {
577         BIGNUM *nminus1 = BN_CTX_get(ctx);
578 
579         if (BN_ucmp(f, BN_value_one()) <= 0) {
580             ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
581             goto err;
582         }
583         if (nminus1 == NULL
584             || BN_copy(nminus1, rsa->n) == NULL
585             || !BN_sub_word(nminus1, 1))
586             goto err;
587         if (BN_ucmp(f, nminus1) >= 0) {
588             ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
589             goto err;
590         }
591     } else
592 #endif
593     {
594         if (BN_ucmp(f, rsa->n) >= 0) {
595             ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
596             goto err;
597         }
598     }
599     if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
600         if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
601                 rsa->n, ctx))
602             goto err;
603 
604     if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
605         blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
606         if (blinding == NULL) {
607             ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
608             goto err;
609         }
610     }
611 
612     if (blinding != NULL) {
613         if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
614             ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
615             goto err;
616         }
617         if (!rsa_blinding_convert(blinding, f, unblind, ctx))
618             goto err;
619     }
620 
621     /* do the decrypt */
622     if ((rsa->flags & RSA_FLAG_EXT_PKEY) || (rsa->version == RSA_ASN1_VERSION_MULTI) || ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
623         if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
624             goto err;
625     } else {
626         BIGNUM *d = BN_new();
627         if (d == NULL) {
628             ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
629             goto err;
630         }
631         if (rsa->d == NULL) {
632             ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
633             BN_free(d);
634             goto err;
635         }
636         BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
637         if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
638                 rsa->_method_mod_n)) {
639             BN_free(d);
640             goto err;
641         }
642         /* We MUST free d before any further use of rsa->d */
643         BN_free(d);
644     }
645 
646     if (blinding)
647         if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
648             goto err;
649 
650     /*
651      * derive the Key Derivation Key from private exponent and public
652      * ciphertext
653      */
654     if (padding == RSA_PKCS1_PADDING) {
655         if (derive_kdk(flen, from, rsa, buf, num, kdk) == 0)
656             goto err;
657     }
658 
659     j = BN_bn2binpad(ret, buf, num);
660     if (j < 0)
661         goto err;
662 
663     switch (padding) {
664     case RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING:
665         r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
666         break;
667     case RSA_PKCS1_PADDING:
668         r = ossl_rsa_padding_check_PKCS1_type_2(rsa->libctx, to, num, buf, j, num, kdk);
669         break;
670     case RSA_PKCS1_OAEP_PADDING:
671         r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
672         break;
673     case RSA_NO_PADDING:
674         memcpy(to, buf, (r = j));
675         break;
676     default:
677         ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
678         goto err;
679     }
680 #ifndef FIPS_MODULE
681     /*
682      * This trick doesn't work in the FIPS provider because libcrypto manages
683      * the error stack. Instead we opt not to put an error on the stack at all
684      * in case of padding failure in the FIPS provider.
685      */
686     ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
687     err_clear_last_constant_time(1 & ~constant_time_msb(r));
688 #endif
689 
690 err:
691     BN_CTX_end(ctx);
692     BN_CTX_free(ctx);
693     OPENSSL_clear_free(buf, num);
694     return r;
695 }
696 
697 /* signature verification */
rsa_ossl_public_decrypt(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)698 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
699     unsigned char *to, RSA *rsa, int padding)
700 {
701     BIGNUM *f, *ret;
702     int i, num = 0, r = -1;
703     unsigned char *buf = NULL;
704     BN_CTX *ctx = NULL;
705 
706     if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
707         ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
708         return -1;
709     }
710 
711     if (BN_ucmp(rsa->n, rsa->e) <= 0) {
712         ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
713         return -1;
714     }
715 
716     /* for large moduli, enforce exponent limit */
717     if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
718         if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
719             ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
720             return -1;
721         }
722     }
723 
724     if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
725         goto err;
726     BN_CTX_start(ctx);
727     f = BN_CTX_get(ctx);
728     ret = BN_CTX_get(ctx);
729     if (ret == NULL) {
730         ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
731         goto err;
732     }
733     num = BN_num_bytes(rsa->n);
734     buf = OPENSSL_malloc(num);
735     if (buf == NULL)
736         goto err;
737 
738     /*
739      * This check was for equality but PGP does evil things and chops off the
740      * top '0' bytes
741      */
742     if (flen > num) {
743         ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
744         goto err;
745     }
746 
747     if (BN_bin2bn(from, flen, f) == NULL)
748         goto err;
749 
750     if (BN_ucmp(f, rsa->n) >= 0) {
751         ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
752         goto err;
753     }
754 
755     if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
756         if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
757                 rsa->n, ctx))
758             goto err;
759 
760     if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
761             rsa->_method_mod_n))
762         goto err;
763 
764     /* For X9.31: Assuming e is odd it does a 12 mod 16 test */
765     if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
766         if (!BN_sub(ret, rsa->n, ret))
767             goto err;
768 
769     i = BN_bn2binpad(ret, buf, num);
770     if (i < 0)
771         goto err;
772 
773     switch (padding) {
774     case RSA_PKCS1_PADDING:
775         r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
776         break;
777     case RSA_X931_PADDING:
778         r = RSA_padding_check_X931(to, num, buf, i, num);
779         break;
780     case RSA_NO_PADDING:
781         memcpy(to, buf, (r = i));
782         break;
783     default:
784         ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
785         goto err;
786     }
787     if (r < 0)
788         ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
789 
790 err:
791     BN_CTX_end(ctx);
792     BN_CTX_free(ctx);
793     OPENSSL_clear_free(buf, num);
794     return r;
795 }
796 
rsa_ossl_mod_exp(BIGNUM * r0,const BIGNUM * I,RSA * rsa,BN_CTX * ctx)797 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
798 {
799     BIGNUM *r1, *m1, *vrfy;
800     int ret = 0, smooth = 0;
801 #ifndef FIPS_MODULE
802     BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2];
803     int i, ex_primes = 0;
804     RSA_PRIME_INFO *pinfo;
805 #endif
806 
807     BN_CTX_start(ctx);
808 
809     r1 = BN_CTX_get(ctx);
810 #ifndef FIPS_MODULE
811     r2 = BN_CTX_get(ctx);
812 #endif
813     m1 = BN_CTX_get(ctx);
814     vrfy = BN_CTX_get(ctx);
815     if (vrfy == NULL)
816         goto err;
817 
818 #ifndef FIPS_MODULE
819     if (rsa->version == RSA_ASN1_VERSION_MULTI
820         && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0
821             || ex_primes > RSA_MAX_PRIME_NUM - 2))
822         goto err;
823 #endif
824 
825     if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
826         BIGNUM *factor = BN_new();
827 
828         if (factor == NULL)
829             goto err;
830 
831         /*
832          * Make sure BN_mod_inverse in Montgomery initialization uses the
833          * BN_FLG_CONSTTIME flag
834          */
835         if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),
836                 BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,
837                     factor, ctx))
838             || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),
839                 BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,
840                     factor, ctx))) {
841             BN_free(factor);
842             goto err;
843         }
844 #ifndef FIPS_MODULE
845         for (i = 0; i < ex_primes; i++) {
846             pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
847             BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);
848             if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {
849                 BN_free(factor);
850                 goto err;
851             }
852         }
853 #endif
854         /*
855          * We MUST free |factor| before any further use of the prime factors
856          */
857         BN_free(factor);
858 
859         smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont)
860 #ifndef FIPS_MODULE
861             && (ex_primes == 0)
862 #endif
863             && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));
864     }
865 
866     if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
867         if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
868                 rsa->n, ctx))
869             goto err;
870 
871     if (smooth) {
872         /*
873          * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
874          * accepts values in [0-m*2^w) range. w is m's bit width rounded up
875          * to limb width. So that at the very least if |I| is fully reduced,
876          * i.e. less than p*q, we can count on from-to round to perform
877          * below modulo operations on |I|. Unlike BN_mod it's constant time.
878          */
879         if (/* m1 = I moq q */
880             !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)
881             || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)
882             /* r1 = I mod p */
883             || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)
884             || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
885             /*
886              * Use parallel exponentiations optimization if possible,
887              * otherwise fallback to two sequential exponentiations:
888              *    m1 = m1^dmq1 mod q
889              *    r1 = r1^dmp1 mod p
890              */
891             || !BN_mod_exp_mont_consttime_x2(m1, m1, rsa->dmq1, rsa->q,
892                 rsa->_method_mod_q,
893                 r1, r1, rsa->dmp1, rsa->p,
894                 rsa->_method_mod_p,
895                 ctx)
896             /* r1 = (r1 - m1) mod p */
897             /*
898              * bn_mod_sub_fixed_top is not regular modular subtraction,
899              * it can tolerate subtrahend to be larger than modulus, but
900              * not bit-wise wider. This makes up for uncommon q>p case,
901              * when |m1| can be larger than |rsa->p|.
902              */
903             || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)
904 
905             /* r1 = r1 * iqmp mod p */
906             || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
907             || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,
908                 ctx)
909             /* r0 = r1 * q + m1 */
910             || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
911             || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
912             goto err;
913 
914         goto tail;
915     }
916 
917     /* compute I mod q */
918     {
919         BIGNUM *c = BN_new();
920         if (c == NULL)
921             goto err;
922         BN_with_flags(c, I, BN_FLG_CONSTTIME);
923 
924         if (!BN_mod(r1, c, rsa->q, ctx)) {
925             BN_free(c);
926             goto err;
927         }
928 
929         {
930             BIGNUM *dmq1 = BN_new();
931             if (dmq1 == NULL) {
932                 BN_free(c);
933                 goto err;
934             }
935             BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
936 
937             /* compute r1^dmq1 mod q */
938             if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
939                     rsa->_method_mod_q)) {
940                 BN_free(c);
941                 BN_free(dmq1);
942                 goto err;
943             }
944             /* We MUST free dmq1 before any further use of rsa->dmq1 */
945             BN_free(dmq1);
946         }
947 
948         /* compute I mod p */
949         if (!BN_mod(r1, c, rsa->p, ctx)) {
950             BN_free(c);
951             goto err;
952         }
953         /* We MUST free c before any further use of I */
954         BN_free(c);
955     }
956 
957     {
958         BIGNUM *dmp1 = BN_new();
959         if (dmp1 == NULL)
960             goto err;
961         BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
962 
963         /* compute r1^dmp1 mod p */
964         if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
965                 rsa->_method_mod_p)) {
966             BN_free(dmp1);
967             goto err;
968         }
969         /* We MUST free dmp1 before any further use of rsa->dmp1 */
970         BN_free(dmp1);
971     }
972 
973 #ifndef FIPS_MODULE
974     if (ex_primes > 0) {
975         BIGNUM *di = BN_new(), *cc = BN_new();
976 
977         if (cc == NULL || di == NULL) {
978             BN_free(cc);
979             BN_free(di);
980             goto err;
981         }
982 
983         for (i = 0; i < ex_primes; i++) {
984             /* prepare m_i */
985             if ((m[i] = BN_CTX_get(ctx)) == NULL) {
986                 BN_free(cc);
987                 BN_free(di);
988                 goto err;
989             }
990 
991             pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
992 
993             /* prepare c and d_i */
994             BN_with_flags(cc, I, BN_FLG_CONSTTIME);
995             BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
996 
997             if (!BN_mod(r1, cc, pinfo->r, ctx)) {
998                 BN_free(cc);
999                 BN_free(di);
1000                 goto err;
1001             }
1002             /* compute r1 ^ d_i mod r_i */
1003             if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
1004                 BN_free(cc);
1005                 BN_free(di);
1006                 goto err;
1007             }
1008         }
1009 
1010         BN_free(cc);
1011         BN_free(di);
1012     }
1013 #endif
1014 
1015     if (!BN_sub(r0, r0, m1))
1016         goto err;
1017     /*
1018      * This will help stop the size of r0 increasing, which does affect the
1019      * multiply if it optimised for a power of 2 size
1020      */
1021     if (BN_is_negative(r0))
1022         if (!BN_add(r0, r0, rsa->p))
1023             goto err;
1024 
1025     if (!BN_mul(r1, r0, rsa->iqmp, ctx))
1026         goto err;
1027 
1028     {
1029         BIGNUM *pr1 = BN_new();
1030         if (pr1 == NULL)
1031             goto err;
1032         BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
1033 
1034         if (!BN_mod(r0, pr1, rsa->p, ctx)) {
1035             BN_free(pr1);
1036             goto err;
1037         }
1038         /* We MUST free pr1 before any further use of r1 */
1039         BN_free(pr1);
1040     }
1041 
1042     /*
1043      * If p < q it is occasionally possible for the correction of adding 'p'
1044      * if r0 is negative above to leave the result still negative. This can
1045      * break the private key operations: the following second correction
1046      * should *always* correct this rare occurrence. This will *never* happen
1047      * with OpenSSL generated keys because they ensure p > q [steve]
1048      */
1049     if (BN_is_negative(r0))
1050         if (!BN_add(r0, r0, rsa->p))
1051             goto err;
1052     if (!BN_mul(r1, r0, rsa->q, ctx))
1053         goto err;
1054     if (!BN_add(r0, r1, m1))
1055         goto err;
1056 
1057 #ifndef FIPS_MODULE
1058     /* add m_i to m in multi-prime case */
1059     if (ex_primes > 0) {
1060         BIGNUM *pr2 = BN_new();
1061 
1062         if (pr2 == NULL)
1063             goto err;
1064 
1065         for (i = 0; i < ex_primes; i++) {
1066             pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1067             if (!BN_sub(r1, m[i], r0)) {
1068                 BN_free(pr2);
1069                 goto err;
1070             }
1071 
1072             if (!BN_mul(r2, r1, pinfo->t, ctx)) {
1073                 BN_free(pr2);
1074                 goto err;
1075             }
1076 
1077             BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
1078 
1079             if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
1080                 BN_free(pr2);
1081                 goto err;
1082             }
1083 
1084             if (BN_is_negative(r1))
1085                 if (!BN_add(r1, r1, pinfo->r)) {
1086                     BN_free(pr2);
1087                     goto err;
1088                 }
1089             if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
1090                 BN_free(pr2);
1091                 goto err;
1092             }
1093             if (!BN_add(r0, r0, r1)) {
1094                 BN_free(pr2);
1095                 goto err;
1096             }
1097         }
1098         BN_free(pr2);
1099     }
1100 #endif
1101 
1102 tail:
1103     if (rsa->e && rsa->n) {
1104         if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
1105             if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
1106                     rsa->_method_mod_n))
1107                 goto err;
1108         } else {
1109             bn_correct_top(r0);
1110             if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
1111                     rsa->_method_mod_n))
1112                 goto err;
1113         }
1114         /*
1115          * If 'I' was greater than (or equal to) rsa->n, the operation will
1116          * be equivalent to using 'I mod n'. However, the result of the
1117          * verify will *always* be less than 'n' so we don't check for
1118          * absolute equality, just congruency.
1119          */
1120         if (!BN_sub(vrfy, vrfy, I))
1121             goto err;
1122         if (BN_is_zero(vrfy)) {
1123             bn_correct_top(r0);
1124             ret = 1;
1125             goto err; /* not actually error */
1126         }
1127         if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
1128             goto err;
1129         if (BN_is_negative(vrfy))
1130             if (!BN_add(vrfy, vrfy, rsa->n))
1131                 goto err;
1132         if (!BN_is_zero(vrfy)) {
1133             /*
1134              * 'I' and 'vrfy' aren't congruent mod n. Don't leak
1135              * miscalculated CRT output, just do a raw (slower) mod_exp and
1136              * return that instead.
1137              */
1138 
1139             BIGNUM *d = BN_new();
1140             if (d == NULL)
1141                 goto err;
1142             BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
1143 
1144             if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
1145                     rsa->_method_mod_n)) {
1146                 BN_free(d);
1147                 goto err;
1148             }
1149             /* We MUST free d before any further use of rsa->d */
1150             BN_free(d);
1151         }
1152     }
1153     /*
1154      * It's unfortunate that we have to bn_correct_top(r0). What hopefully
1155      * saves the day is that correction is highly unlike, and private key
1156      * operations are customarily performed on blinded message. Which means
1157      * that attacker won't observe correlation with chosen plaintext.
1158      * Secondly, remaining code would still handle it in same computational
1159      * time and even conceal memory access pattern around corrected top.
1160      */
1161     bn_correct_top(r0);
1162     ret = 1;
1163 err:
1164     BN_CTX_end(ctx);
1165     return ret;
1166 }
1167 
rsa_ossl_init(RSA * rsa)1168 static int rsa_ossl_init(RSA *rsa)
1169 {
1170     rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
1171     return 1;
1172 }
1173 
rsa_ossl_finish(RSA * rsa)1174 static int rsa_ossl_finish(RSA *rsa)
1175 {
1176 #ifndef FIPS_MODULE
1177     int i;
1178     RSA_PRIME_INFO *pinfo;
1179 
1180     for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
1181         pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1182         BN_MONT_CTX_free(pinfo->m);
1183     }
1184 #endif
1185 
1186     BN_MONT_CTX_free(rsa->_method_mod_n);
1187     BN_MONT_CTX_free(rsa->_method_mod_p);
1188     BN_MONT_CTX_free(rsa->_method_mod_q);
1189     return 1;
1190 }
1191 
1192 #ifdef S390X_MOD_EXP
rsa_ossl_s390x_mod_exp(BIGNUM * r0,const BIGNUM * i,RSA * rsa,BN_CTX * ctx)1193 static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
1194     BN_CTX *ctx)
1195 {
1196     if (rsa->version != RSA_ASN1_VERSION_MULTI) {
1197         if (s390x_crt(r0, i, rsa->p, rsa->q, rsa->dmp1, rsa->dmq1, rsa->iqmp) == 1)
1198             return 1;
1199     }
1200     return rsa_ossl_mod_exp(r0, i, rsa, ctx);
1201 }
1202 
1203 #endif
1204