/src/openssl31/crypto/rsa/rsa_mp.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2017-2022 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  * Copyright 2017 BaishanCloud. All rights reserved.  | 
4  |  |  *  | 
5  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use  | 
6  |  |  * this file except in compliance with the License.  You can obtain a copy  | 
7  |  |  * in the file LICENSE in the source distribution or at  | 
8  |  |  * https://www.openssl.org/source/license.html  | 
9  |  |  */  | 
10  |  |  | 
11  |  | #include <openssl/bn.h>  | 
12  |  | #include <openssl/err.h>  | 
13  |  | #include "rsa_local.h"  | 
14  |  |  | 
15  |  | void ossl_rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo)  | 
16  | 288k  | { | 
17  |  |     /* free pp and pinfo only */  | 
18  | 288k  |     BN_clear_free(pinfo->pp);  | 
19  | 288k  |     OPENSSL_free(pinfo);  | 
20  | 288k  | }  | 
21  |  |  | 
22  |  | void ossl_rsa_multip_info_free(RSA_PRIME_INFO *pinfo)  | 
23  | 288k  | { | 
24  |  |     /* free an RSA_PRIME_INFO structure */  | 
25  | 288k  |     BN_clear_free(pinfo->r);  | 
26  | 288k  |     BN_clear_free(pinfo->d);  | 
27  | 288k  |     BN_clear_free(pinfo->t);  | 
28  | 288k  |     ossl_rsa_multip_info_free_ex(pinfo);  | 
29  | 288k  | }  | 
30  |  |  | 
31  |  | RSA_PRIME_INFO *ossl_rsa_multip_info_new(void)  | 
32  | 0  | { | 
33  | 0  |     RSA_PRIME_INFO *pinfo;  | 
34  |  |  | 
35  |  |     /* create an RSA_PRIME_INFO structure */  | 
36  | 0  |     if ((pinfo = OPENSSL_zalloc(sizeof(RSA_PRIME_INFO))) == NULL) { | 
37  | 0  |         ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);  | 
38  | 0  |         return NULL;  | 
39  | 0  |     }  | 
40  | 0  |     if ((pinfo->r = BN_secure_new()) == NULL)  | 
41  | 0  |         goto err;  | 
42  | 0  |     if ((pinfo->d = BN_secure_new()) == NULL)  | 
43  | 0  |         goto err;  | 
44  | 0  |     if ((pinfo->t = BN_secure_new()) == NULL)  | 
45  | 0  |         goto err;  | 
46  | 0  |     if ((pinfo->pp = BN_secure_new()) == NULL)  | 
47  | 0  |         goto err;  | 
48  |  |  | 
49  | 0  |     return pinfo;  | 
50  |  |  | 
51  | 0  |  err:  | 
52  | 0  |     BN_free(pinfo->r);  | 
53  | 0  |     BN_free(pinfo->d);  | 
54  | 0  |     BN_free(pinfo->t);  | 
55  | 0  |     BN_free(pinfo->pp);  | 
56  | 0  |     OPENSSL_free(pinfo);  | 
57  | 0  |     return NULL;  | 
58  | 0  | }  | 
59  |  |  | 
60  |  | /* Refill products of primes */  | 
61  |  | int ossl_rsa_multip_calc_product(RSA *rsa)  | 
62  | 3.22k  | { | 
63  | 3.22k  |     RSA_PRIME_INFO *pinfo;  | 
64  | 3.22k  |     BIGNUM *p1 = NULL, *p2 = NULL;  | 
65  | 3.22k  |     BN_CTX *ctx = NULL;  | 
66  | 3.22k  |     int i, rv = 0, ex_primes;  | 
67  |  |  | 
68  | 3.22k  |     if ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0) { | 
69  |  |         /* invalid */  | 
70  | 35  |         goto err;  | 
71  | 35  |     }  | 
72  |  |  | 
73  | 3.19k  |     if ((ctx = BN_CTX_new()) == NULL)  | 
74  | 0  |         goto err;  | 
75  |  |  | 
76  |  |     /* calculate pinfo->pp = p * q for first 'extra' prime */  | 
77  | 3.19k  |     p1 = rsa->p;  | 
78  | 3.19k  |     p2 = rsa->q;  | 
79  |  |  | 
80  | 197k  |     for (i = 0; i < ex_primes; i++) { | 
81  | 194k  |         pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);  | 
82  | 194k  |         if (pinfo->pp == NULL) { | 
83  | 194k  |             pinfo->pp = BN_secure_new();  | 
84  | 194k  |             if (pinfo->pp == NULL)  | 
85  | 0  |                 goto err;  | 
86  | 194k  |         }  | 
87  | 194k  |         if (!BN_mul(pinfo->pp, p1, p2, ctx))  | 
88  | 0  |             goto err;  | 
89  |  |         /* save previous one */  | 
90  | 194k  |         p1 = pinfo->pp;  | 
91  | 194k  |         p2 = pinfo->r;  | 
92  | 194k  |     }  | 
93  |  |  | 
94  | 3.19k  |     rv = 1;  | 
95  | 3.22k  |  err:  | 
96  | 3.22k  |     BN_CTX_free(ctx);  | 
97  | 3.22k  |     return rv;  | 
98  | 3.19k  | }  | 
99  |  |  | 
100  |  | int ossl_rsa_multip_cap(int bits)  | 
101  | 1.06k  | { | 
102  | 1.06k  |     int cap = 5;  | 
103  |  |  | 
104  | 1.06k  |     if (bits < 1024)  | 
105  | 876  |         cap = 2;  | 
106  | 187  |     else if (bits < 4096)  | 
107  | 133  |         cap = 3;  | 
108  | 54  |     else if (bits < 8192)  | 
109  | 51  |         cap = 4;  | 
110  |  |  | 
111  | 1.06k  |     if (cap > RSA_MAX_PRIME_NUM)  | 
112  | 0  |         cap = RSA_MAX_PRIME_NUM;  | 
113  |  |  | 
114  | 1.06k  |     return cap;  | 
115  | 1.06k  | }  |