Coverage Report

Created: 2018-08-29 13:53

/src/openssl/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_locl.h"
14
15
void rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo)
16
144k
{
17
144k
    /* free pp and pinfo only */
18
144k
    BN_clear_free(pinfo->pp);
19
144k
    OPENSSL_free(pinfo);
20
144k
}
21
22
void rsa_multip_info_free(RSA_PRIME_INFO *pinfo)
23
144k
{
24
144k
    /* free a RSA_PRIME_INFO structure */
25
144k
    BN_clear_free(pinfo->r);
26
144k
    BN_clear_free(pinfo->d);
27
144k
    BN_clear_free(pinfo->t);
28
144k
    rsa_multip_info_free_ex(pinfo);
29
144k
}
30
31
RSA_PRIME_INFO *rsa_multip_info_new(void)
32
0
{
33
0
    RSA_PRIME_INFO *pinfo;
34
0
35
0
    /* 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
0
49
0
    return pinfo;
50
0
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
91.9k
{
63
91.9k
    RSA_PRIME_INFO *pinfo;
64
91.9k
    BIGNUM *p1 = NULL, *p2 = NULL;
65
91.9k
    BN_CTX *ctx = NULL;
66
91.9k
    int i, rv = 0, ex_primes;
67
91.9k
68
91.9k
    if ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0) {
69
572
        /* invalid */
70
572
        goto err;
71
572
    }
72
91.3k
73
91.3k
    if ((ctx = BN_CTX_new()) == NULL)
74
91.3k
        goto err;
75
91.3k
76
91.3k
    /* calculate pinfo->pp = p * q for first 'extra' prime */
77
91.3k
    p1 = rsa->p;
78
91.3k
    p2 = rsa->q;
79
91.3k
80
226k
    for (i = 0; i < ex_primes; i++) {
81
135k
        pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
82
135k
        if (pinfo->pp == NULL) {
83
135k
            pinfo->pp = BN_secure_new();
84
135k
            if (pinfo->pp == NULL)
85
135k
                goto err;
86
135k
        }
87
135k
        if (!BN_mul(pinfo->pp, p1, p2, ctx))
88
0
            goto err;
89
135k
        /* save previous one */
90
135k
        p1 = pinfo->pp;
91
135k
        p2 = pinfo->r;
92
135k
    }
93
91.3k
94
91.3k
    rv = 1;
95
91.9k
 err:
96
91.9k
    BN_CTX_free(ctx);
97
91.9k
    return rv;
98
91.3k
}
99
100
int rsa_multip_cap(int bits)
101
0
{
102
0
    int cap = 5;
103
0
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
0
111
0
    if (cap > RSA_MAX_PRIME_NUM)
112
0
        cap = RSA_MAX_PRIME_NUM;
113
0
114
0
    return cap;
115
0
}