Coverage Report

Created: 2025-06-13 06:58

/src/openssl31/crypto/rsa/rsa_crpt.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * RSA low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include <openssl/crypto.h>
18
#include "internal/cryptlib.h"
19
#include "crypto/bn.h"
20
#include <openssl/rand.h>
21
#include "rsa_local.h"
22
23
int RSA_bits(const RSA *r)
24
105k
{
25
105k
    return BN_num_bits(r->n);
26
105k
}
27
28
int RSA_size(const RSA *r)
29
161k
{
30
161k
    return BN_num_bytes(r->n);
31
161k
}
32
33
int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
34
                       RSA *rsa, int padding)
35
1.35k
{
36
1.35k
    return rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding);
37
1.35k
}
38
39
int RSA_private_encrypt(int flen, const unsigned char *from,
40
                        unsigned char *to, RSA *rsa, int padding)
41
3.29k
{
42
3.29k
    return rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding);
43
3.29k
}
44
45
int RSA_private_decrypt(int flen, const unsigned char *from,
46
                        unsigned char *to, RSA *rsa, int padding)
47
5.60k
{
48
5.60k
    return rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding);
49
5.60k
}
50
51
int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
52
                       RSA *rsa, int padding)
53
14.0k
{
54
14.0k
    return rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding);
55
14.0k
}
56
57
int RSA_flags(const RSA *r)
58
0
{
59
0
    return r == NULL ? 0 : r->meth->flags;
60
0
}
61
62
void RSA_blinding_off(RSA *rsa)
63
0
{
64
0
    BN_BLINDING_free(rsa->blinding);
65
0
    rsa->blinding = NULL;
66
0
    rsa->flags &= ~RSA_FLAG_BLINDING;
67
0
    rsa->flags |= RSA_FLAG_NO_BLINDING;
68
0
}
69
70
int RSA_blinding_on(RSA *rsa, BN_CTX *ctx)
71
0
{
72
0
    int ret = 0;
73
74
0
    if (rsa->blinding != NULL)
75
0
        RSA_blinding_off(rsa);
76
77
0
    rsa->blinding = RSA_setup_blinding(rsa, ctx);
78
0
    if (rsa->blinding == NULL)
79
0
        goto err;
80
81
0
    rsa->flags |= RSA_FLAG_BLINDING;
82
0
    rsa->flags &= ~RSA_FLAG_NO_BLINDING;
83
0
    ret = 1;
84
0
 err:
85
0
    return ret;
86
0
}
87
88
static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p,
89
                                  const BIGNUM *q, BN_CTX *ctx)
90
0
{
91
0
    BIGNUM *ret = NULL, *r0, *r1, *r2;
92
93
0
    if (d == NULL || p == NULL || q == NULL)
94
0
        return NULL;
95
96
0
    BN_CTX_start(ctx);
97
0
    r0 = BN_CTX_get(ctx);
98
0
    r1 = BN_CTX_get(ctx);
99
0
    r2 = BN_CTX_get(ctx);
100
0
    if (r2 == NULL)
101
0
        goto err;
102
103
0
    if (!BN_sub(r1, p, BN_value_one()))
104
0
        goto err;
105
0
    if (!BN_sub(r2, q, BN_value_one()))
106
0
        goto err;
107
0
    if (!BN_mul(r0, r1, r2, ctx))
108
0
        goto err;
109
110
0
    ret = BN_mod_inverse(NULL, d, r0, ctx);
111
0
 err:
112
0
    BN_CTX_end(ctx);
113
0
    return ret;
114
0
}
115
116
BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx)
117
8.87k
{
118
8.87k
    BIGNUM *e;
119
8.87k
    BN_CTX *ctx;
120
8.87k
    BN_BLINDING *ret = NULL;
121
122
8.87k
    if (in_ctx == NULL) {
123
0
        if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
124
0
            return 0;
125
8.87k
    } else {
126
8.87k
        ctx = in_ctx;
127
8.87k
    }
128
129
8.87k
    BN_CTX_start(ctx);
130
8.87k
    e = BN_CTX_get(ctx);
131
8.87k
    if (e == NULL) {
132
0
        ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
133
0
        goto err;
134
0
    }
135
136
8.87k
    if (rsa->e == NULL) {
137
0
        e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);
138
0
        if (e == NULL) {
139
0
            ERR_raise(ERR_LIB_RSA, RSA_R_NO_PUBLIC_EXPONENT);
140
0
            goto err;
141
0
        }
142
8.87k
    } else {
143
8.87k
        e = rsa->e;
144
8.87k
    }
145
146
8.87k
    {
147
8.87k
        BIGNUM *n = BN_new();
148
149
8.87k
        if (n == NULL) {
150
0
            ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
151
0
            goto err;
152
0
        }
153
8.87k
        BN_with_flags(n, rsa->n, BN_FLG_CONSTTIME);
154
155
8.87k
        ret = BN_BLINDING_create_param(NULL, e, n, ctx, rsa->meth->bn_mod_exp,
156
8.87k
                                       rsa->_method_mod_n);
157
        /* We MUST free n before any further use of rsa->n */
158
8.87k
        BN_free(n);
159
8.87k
    }
160
8.87k
    if (ret == NULL) {
161
0
        ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
162
0
        goto err;
163
0
    }
164
165
8.87k
    BN_BLINDING_set_current_thread(ret);
166
167
8.87k
 err:
168
8.87k
    BN_CTX_end(ctx);
169
8.87k
    if (ctx != in_ctx)
170
0
        BN_CTX_free(ctx);
171
8.87k
    if (e != rsa->e)
172
0
        BN_free(e);
173
174
8.87k
    return ret;
175
8.87k
}