Coverage Report

Created: 2025-06-13 06:57

/src/openssl/crypto/rsa/rsa_mp.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2017-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright 2017 BaishanCloud. All rights reserved.
4
 *
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
9
 */
10
11
#include <openssl/bn.h>
12
#include <openssl/err.h>
13
#include "rsa_local.h"
14
15
void ossl_rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo)
16
64.6k
{
17
    /* free pp and pinfo only */
18
64.6k
    BN_clear_free(pinfo->pp);
19
64.6k
    OPENSSL_free(pinfo);
20
64.6k
}
21
22
void ossl_rsa_multip_info_free(RSA_PRIME_INFO *pinfo)
23
64.6k
{
24
    /* free an RSA_PRIME_INFO structure */
25
64.6k
    BN_clear_free(pinfo->r);
26
64.6k
    BN_clear_free(pinfo->d);
27
64.6k
    BN_clear_free(pinfo->t);
28
64.6k
    ossl_rsa_multip_info_free_ex(pinfo);
29
64.6k
}
30
31
RSA_PRIME_INFO *ossl_rsa_multip_info_new(void)
32
0
{
33
0
    RSA_PRIME_INFO *pinfo;
34
35
    /* create an RSA_PRIME_INFO structure */
36
0
    if ((pinfo = OPENSSL_zalloc(sizeof(RSA_PRIME_INFO))) == NULL)
37
0
        return NULL;
38
0
    if ((pinfo->r = BN_secure_new()) == NULL)
39
0
        goto err;
40
0
    if ((pinfo->d = BN_secure_new()) == NULL)
41
0
        goto err;
42
0
    if ((pinfo->t = BN_secure_new()) == NULL)
43
0
        goto err;
44
0
    if ((pinfo->pp = BN_secure_new()) == NULL)
45
0
        goto err;
46
47
0
    return pinfo;
48
49
0
 err:
50
0
    BN_free(pinfo->r);
51
0
    BN_free(pinfo->d);
52
0
    BN_free(pinfo->t);
53
0
    BN_free(pinfo->pp);
54
0
    OPENSSL_free(pinfo);
55
0
    return NULL;
56
0
}
57
58
/* Refill products of primes */
59
int ossl_rsa_multip_calc_product(RSA *rsa)
60
1.16k
{
61
1.16k
    RSA_PRIME_INFO *pinfo;
62
1.16k
    BIGNUM *p1 = NULL, *p2 = NULL;
63
1.16k
    BN_CTX *ctx = NULL;
64
1.16k
    int i, rv = 0, ex_primes;
65
66
1.16k
    if ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0) {
67
        /* invalid */
68
7
        goto err;
69
7
    }
70
71
1.16k
    if ((ctx = BN_CTX_new()) == NULL)
72
0
        goto err;
73
74
    /* calculate pinfo->pp = p * q for first 'extra' prime */
75
1.16k
    p1 = rsa->p;
76
1.16k
    p2 = rsa->q;
77
78
48.8k
    for (i = 0; i < ex_primes; i++) {
79
47.6k
        pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
80
47.6k
        if (pinfo->pp == NULL) {
81
47.6k
            pinfo->pp = BN_secure_new();
82
47.6k
            if (pinfo->pp == NULL)
83
0
                goto err;
84
47.6k
        }
85
47.6k
        if (!BN_mul(pinfo->pp, p1, p2, ctx))
86
0
            goto err;
87
        /* save previous one */
88
47.6k
        p1 = pinfo->pp;
89
47.6k
        p2 = pinfo->r;
90
47.6k
    }
91
92
1.16k
    rv = 1;
93
1.16k
 err:
94
1.16k
    BN_CTX_free(ctx);
95
1.16k
    return rv;
96
1.16k
}
97
98
int ossl_rsa_multip_cap(int bits)
99
29
{
100
29
    int cap = RSA_MAX_PRIME_NUM;
101
102
29
    if (bits < 1024)
103
20
        cap = 2;
104
9
    else if (bits < 4096)
105
4
        cap = 3;
106
5
    else if (bits < 8192)
107
4
        cap = 4;
108
109
29
    if (cap > RSA_MAX_PRIME_NUM)
110
0
        cap = RSA_MAX_PRIME_NUM;
111
112
29
    return cap;
113
29
}