2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2017 BaishanCloud. All rights reserved.
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
11 #include <openssl/bn.h>
12 #include <openssl/err.h>
13 #include "rsa_local.h"
15 void rsa_multip_info_free_ex(RSA_PRIME_INFO
*pinfo
)
17 /* free pp and pinfo only */
18 BN_clear_free(pinfo
->pp
);
22 void rsa_multip_info_free(RSA_PRIME_INFO
*pinfo
)
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
);
28 rsa_multip_info_free_ex(pinfo
);
31 RSA_PRIME_INFO
*rsa_multip_info_new(void)
33 RSA_PRIME_INFO
*pinfo
;
35 /* create a RSA_PRIME_INFO structure */
36 if ((pinfo
= OPENSSL_zalloc(sizeof(RSA_PRIME_INFO
))) == NULL
) {
37 RSAerr(RSA_F_RSA_MULTIP_INFO_NEW
, ERR_R_MALLOC_FAILURE
);
40 if ((pinfo
->r
= BN_secure_new()) == NULL
)
42 if ((pinfo
->d
= BN_secure_new()) == NULL
)
44 if ((pinfo
->t
= BN_secure_new()) == NULL
)
46 if ((pinfo
->pp
= BN_secure_new()) == NULL
)
60 /* Refill products of primes */
61 int rsa_multip_calc_product(RSA
*rsa
)
63 RSA_PRIME_INFO
*pinfo
;
64 BIGNUM
*p1
= NULL
, *p2
= NULL
;
66 int i
, rv
= 0, ex_primes
;
68 if ((ex_primes
= sk_RSA_PRIME_INFO_num(rsa
->prime_infos
)) <= 0) {
73 if ((ctx
= BN_CTX_new()) == NULL
)
76 /* calculate pinfo->pp = p * q for first 'extra' prime */
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
)
87 if (!BN_mul(pinfo
->pp
, p1
, p2
, ctx
))
89 /* save previous one */
100 int rsa_multip_cap(int bits
)
106 else if (bits
< 4096)
108 else if (bits
< 8192)
111 if (cap
> RSA_MAX_PRIME_NUM
)
112 cap
= RSA_MAX_PRIME_NUM
;