]>
Commit | Line | Data |
---|---|---|
665d899f | 1 | /* |
605856d7 | 2 | * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved. |
665d899f PY |
3 | * Copyright 2017 BaishanCloud. All rights reserved. |
4 | * | |
2a7b6f39 | 5 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
665d899f PY |
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> | |
cdb10bae | 12 | #include <openssl/err.h> |
706457b7 | 13 | #include "rsa_local.h" |
665d899f | 14 | |
4158b0dc | 15 | void ossl_rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo) |
665d899f PY |
16 | { |
17 | /* free pp and pinfo only */ | |
18 | BN_clear_free(pinfo->pp); | |
19 | OPENSSL_free(pinfo); | |
20 | } | |
21 | ||
4158b0dc | 22 | void ossl_rsa_multip_info_free(RSA_PRIME_INFO *pinfo) |
665d899f PY |
23 | { |
24 | /* free a RSA_PRIME_INFO structure */ | |
25 | BN_clear_free(pinfo->r); | |
26 | BN_clear_free(pinfo->d); | |
27 | BN_clear_free(pinfo->t); | |
4158b0dc | 28 | ossl_rsa_multip_info_free_ex(pinfo); |
665d899f PY |
29 | } |
30 | ||
4158b0dc | 31 | RSA_PRIME_INFO *ossl_rsa_multip_info_new(void) |
665d899f PY |
32 | { |
33 | RSA_PRIME_INFO *pinfo; | |
34 | ||
35 | /* create a RSA_PRIME_INFO structure */ | |
cdb10bae | 36 | if ((pinfo = OPENSSL_zalloc(sizeof(RSA_PRIME_INFO))) == NULL) { |
9311d0c4 | 37 | ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE); |
665d899f | 38 | return NULL; |
cdb10bae | 39 | } |
665d899f PY |
40 | if ((pinfo->r = BN_secure_new()) == NULL) |
41 | goto err; | |
42 | if ((pinfo->d = BN_secure_new()) == NULL) | |
43 | goto err; | |
44 | if ((pinfo->t = BN_secure_new()) == NULL) | |
45 | goto err; | |
46 | if ((pinfo->pp = BN_secure_new()) == NULL) | |
47 | goto err; | |
48 | ||
49 | return pinfo; | |
50 | ||
51 | err: | |
52 | BN_free(pinfo->r); | |
53 | BN_free(pinfo->d); | |
54 | BN_free(pinfo->t); | |
55 | BN_free(pinfo->pp); | |
62542d04 | 56 | OPENSSL_free(pinfo); |
665d899f PY |
57 | return NULL; |
58 | } | |
59 | ||
60 | /* Refill products of primes */ | |
4158b0dc | 61 | int ossl_rsa_multip_calc_product(RSA *rsa) |
665d899f PY |
62 | { |
63 | RSA_PRIME_INFO *pinfo; | |
64 | BIGNUM *p1 = NULL, *p2 = NULL; | |
65 | BN_CTX *ctx = NULL; | |
66 | int i, rv = 0, ex_primes; | |
67 | ||
68 | if ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0) { | |
69 | /* invalid */ | |
70 | goto err; | |
71 | } | |
72 | ||
73 | if ((ctx = BN_CTX_new()) == NULL) | |
74 | goto err; | |
75 | ||
76 | /* calculate pinfo->pp = p * q for first 'extra' prime */ | |
77 | p1 = rsa->p; | |
78 | p2 = rsa->q; | |
79 | ||
80 | for (i = 0; i < ex_primes; i++) { | |
81 | pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i); | |
82 | if (pinfo->pp == NULL) { | |
83 | pinfo->pp = BN_secure_new(); | |
84 | if (pinfo->pp == NULL) | |
85 | goto err; | |
86 | } | |
87 | if (!BN_mul(pinfo->pp, p1, p2, ctx)) | |
88 | goto err; | |
89 | /* save previous one */ | |
90 | p1 = pinfo->pp; | |
91 | p2 = pinfo->r; | |
92 | } | |
93 | ||
94 | rv = 1; | |
95 | err: | |
96 | BN_CTX_free(ctx); | |
97 | return rv; | |
98 | } | |
0122add6 | 99 | |
4158b0dc | 100 | int ossl_rsa_multip_cap(int bits) |
0122add6 AP |
101 | { |
102 | int cap = 5; | |
103 | ||
104 | if (bits < 1024) | |
105 | cap = 2; | |
106 | else if (bits < 4096) | |
107 | cap = 3; | |
108 | else if (bits < 8192) | |
109 | cap = 4; | |
110 | ||
f9085209 BE |
111 | if (cap > RSA_MAX_PRIME_NUM) |
112 | cap = RSA_MAX_PRIME_NUM; | |
113 | ||
0122add6 AP |
114 | return cap; |
115 | } |