/src/openssl111/crypto/rsa/rsa_mp.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * Copyright 2017 BaishanCloud. All rights reserved. | 
| 4 |  |  * | 
| 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 | 
| 9 |  |  */ | 
| 10 |  |  | 
| 11 |  | #include <openssl/bn.h> | 
| 12 |  | #include <openssl/err.h> | 
| 13 |  | #include "rsa_local.h" | 
| 14 |  |  | 
| 15 |  | void rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo) | 
| 16 | 0 | { | 
| 17 |  |     /* free pp and pinfo only */ | 
| 18 | 0 |     BN_clear_free(pinfo->pp); | 
| 19 | 0 |     OPENSSL_free(pinfo); | 
| 20 | 0 | } | 
| 21 |  |  | 
| 22 |  | void rsa_multip_info_free(RSA_PRIME_INFO *pinfo) | 
| 23 | 0 | { | 
| 24 |  |     /* free a RSA_PRIME_INFO structure */ | 
| 25 | 0 |     BN_clear_free(pinfo->r); | 
| 26 | 0 |     BN_clear_free(pinfo->d); | 
| 27 | 0 |     BN_clear_free(pinfo->t); | 
| 28 | 0 |     rsa_multip_info_free_ex(pinfo); | 
| 29 | 0 | } | 
| 30 |  |  | 
| 31 |  | RSA_PRIME_INFO *rsa_multip_info_new(void) | 
| 32 | 0 | { | 
| 33 | 0 |     RSA_PRIME_INFO *pinfo; | 
| 34 |  |  | 
| 35 |  |     /* create a RSA_PRIME_INFO structure */ | 
| 36 | 0 |     if ((pinfo = OPENSSL_zalloc(sizeof(RSA_PRIME_INFO))) == NULL) { | 
| 37 | 0 |         RSAerr(RSA_F_RSA_MULTIP_INFO_NEW, 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 rsa_multip_calc_product(RSA *rsa) | 
| 62 | 0 | { | 
| 63 | 0 |     RSA_PRIME_INFO *pinfo; | 
| 64 | 0 |     BIGNUM *p1 = NULL, *p2 = NULL; | 
| 65 | 0 |     BN_CTX *ctx = NULL; | 
| 66 | 0 |     int i, rv = 0, ex_primes; | 
| 67 |  | 
 | 
| 68 | 0 |     if ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0) { | 
| 69 |  |         /* invalid */ | 
| 70 | 0 |         goto err; | 
| 71 | 0 |     } | 
| 72 |  |  | 
| 73 | 0 |     if ((ctx = BN_CTX_new()) == NULL) | 
| 74 | 0 |         goto err; | 
| 75 |  |  | 
| 76 |  |     /* calculate pinfo->pp = p * q for first 'extra' prime */ | 
| 77 | 0 |     p1 = rsa->p; | 
| 78 | 0 |     p2 = rsa->q; | 
| 79 |  | 
 | 
| 80 | 0 |     for (i = 0; i < ex_primes; i++) { | 
| 81 | 0 |         pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i); | 
| 82 | 0 |         if (pinfo->pp == NULL) { | 
| 83 | 0 |             pinfo->pp = BN_secure_new(); | 
| 84 | 0 |             if (pinfo->pp == NULL) | 
| 85 | 0 |                 goto err; | 
| 86 | 0 |         } | 
| 87 | 0 |         if (!BN_mul(pinfo->pp, p1, p2, ctx)) | 
| 88 | 0 |             goto err; | 
| 89 |  |         /* save previous one */ | 
| 90 | 0 |         p1 = pinfo->pp; | 
| 91 | 0 |         p2 = pinfo->r; | 
| 92 | 0 |     } | 
| 93 |  |  | 
| 94 | 0 |     rv = 1; | 
| 95 | 0 |  err: | 
| 96 | 0 |     BN_CTX_free(ctx); | 
| 97 | 0 |     return rv; | 
| 98 | 0 | } | 
| 99 |  |  | 
| 100 |  | int rsa_multip_cap(int bits) | 
| 101 | 0 | { | 
| 102 | 0 |     int cap = 5; | 
| 103 |  | 
 | 
| 104 | 0 |     if (bits < 1024) | 
| 105 | 0 |         cap = 2; | 
| 106 | 0 |     else if (bits < 4096) | 
| 107 | 0 |         cap = 3; | 
| 108 | 0 |     else if (bits < 8192) | 
| 109 | 0 |         cap = 4; | 
| 110 |  | 
 | 
| 111 | 0 |     if (cap > RSA_MAX_PRIME_NUM) | 
| 112 | 0 |         cap = RSA_MAX_PRIME_NUM; | 
| 113 |  | 
 | 
| 114 | 0 |     return cap; | 
| 115 | 0 | } |