2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2017 BaishanCloud. All rights reserved.
5 * Licensed under the OpenSSL license (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
11 #include <openssl/bn.h>
14 void rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo)
16 /* free pp and pinfo only */
17 BN_clear_free(pinfo->pp);
21 void rsa_multip_info_free(RSA_PRIME_INFO *pinfo)
23 /* free a RSA_PRIME_INFO structure */
24 BN_clear_free(pinfo->r);
25 BN_clear_free(pinfo->d);
26 BN_clear_free(pinfo->t);
27 rsa_multip_info_free_ex(pinfo);
30 RSA_PRIME_INFO *rsa_multip_info_new(void)
32 RSA_PRIME_INFO *pinfo;
34 /* create a RSA_PRIME_INFO structure */
35 pinfo = OPENSSL_zalloc(sizeof(RSA_PRIME_INFO));
38 if ((pinfo->r = BN_secure_new()) == NULL)
40 if ((pinfo->d = BN_secure_new()) == NULL)
42 if ((pinfo->t = BN_secure_new()) == NULL)
44 if ((pinfo->pp = BN_secure_new()) == NULL)
58 /* Refill products of primes */
59 int rsa_multip_calc_product(RSA *rsa)
61 RSA_PRIME_INFO *pinfo;
62 BIGNUM *p1 = NULL, *p2 = NULL;
64 int i, rv = 0, ex_primes;
66 if ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0) {
71 if ((ctx = BN_CTX_new()) == NULL)
74 /* calculate pinfo->pp = p * q for first 'extra' prime */
78 for (i = 0; i < ex_primes; i++) {
79 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
80 if (pinfo->pp == NULL) {
81 pinfo->pp = BN_secure_new();
82 if (pinfo->pp == NULL)
85 if (!BN_mul(pinfo->pp, p1, p2, ctx))
87 /* save previous one */
98 int rsa_multip_cap(int bits)
104 else if (bits < 4096)
106 else if (bits < 8192)
109 if (cap > RSA_MAX_PRIME_NUM)
110 cap = RSA_MAX_PRIME_NUM;