Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/bn/bn_blind.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1998-2023 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
#include <openssl/opensslconf.h>
11
#include "internal/cryptlib.h"
12
#include "bn_local.h"
13
14
0
#define BN_BLINDING_COUNTER     32
15
16
struct bn_blinding_st {
17
    BIGNUM *A;
18
    BIGNUM *Ai;
19
    BIGNUM *e;
20
    BIGNUM *mod;                /* just a reference */
21
    CRYPTO_THREAD_ID tid;
22
    int counter;
23
    unsigned long flags;
24
    BN_MONT_CTX *m_ctx;
25
    int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
26
                       const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
27
    CRYPTO_RWLOCK *lock;
28
};
29
30
BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
31
8.87k
{
32
8.87k
    BN_BLINDING *ret = NULL;
33
34
8.87k
    bn_check_top(mod);
35
36
8.87k
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
37
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
38
0
        return NULL;
39
0
    }
40
41
8.87k
    ret->lock = CRYPTO_THREAD_lock_new();
42
8.87k
    if (ret->lock == NULL) {
43
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
44
0
        OPENSSL_free(ret);
45
0
        return NULL;
46
0
    }
47
48
8.87k
    BN_BLINDING_set_current_thread(ret);
49
50
8.87k
    if (A != NULL) {
51
0
        if ((ret->A = BN_dup(A)) == NULL)
52
0
            goto err;
53
0
    }
54
55
8.87k
    if (Ai != NULL) {
56
0
        if ((ret->Ai = BN_dup(Ai)) == NULL)
57
0
            goto err;
58
0
    }
59
60
    /* save a copy of mod in the BN_BLINDING structure */
61
8.87k
    if ((ret->mod = BN_dup(mod)) == NULL)
62
0
        goto err;
63
64
8.87k
    if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
65
8.87k
        BN_set_flags(ret->mod, BN_FLG_CONSTTIME);
66
67
    /*
68
     * Set the counter to the special value -1 to indicate that this is
69
     * never-used fresh blinding that does not need updating before first
70
     * use.
71
     */
72
8.87k
    ret->counter = -1;
73
74
8.87k
    return ret;
75
76
0
 err:
77
0
    BN_BLINDING_free(ret);
78
0
    return NULL;
79
8.87k
}
80
81
void BN_BLINDING_free(BN_BLINDING *r)
82
810k
{
83
810k
    if (r == NULL)
84
802k
        return;
85
8.87k
    BN_free(r->A);
86
8.87k
    BN_free(r->Ai);
87
8.87k
    BN_free(r->e);
88
8.87k
    BN_free(r->mod);
89
8.87k
    CRYPTO_THREAD_lock_free(r->lock);
90
8.87k
    OPENSSL_free(r);
91
8.87k
}
92
93
int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx)
94
0
{
95
0
    int ret = 0;
96
97
0
    if ((b->A == NULL) || (b->Ai == NULL)) {
98
0
        ERR_raise(ERR_LIB_BN, BN_R_NOT_INITIALIZED);
99
0
        goto err;
100
0
    }
101
102
0
    if (b->counter == -1)
103
0
        b->counter = 0;
104
105
0
    if (++b->counter == BN_BLINDING_COUNTER && b->e != NULL &&
106
0
        !(b->flags & BN_BLINDING_NO_RECREATE)) {
107
        /* re-create blinding parameters */
108
0
        if (!BN_BLINDING_create_param(b, NULL, NULL, ctx, NULL, NULL))
109
0
            goto err;
110
0
    } else if (!(b->flags & BN_BLINDING_NO_UPDATE)) {
111
0
        if (b->m_ctx != NULL) {
112
0
            if (!bn_mul_mont_fixed_top(b->Ai, b->Ai, b->Ai, b->m_ctx, ctx)
113
0
                || !bn_mul_mont_fixed_top(b->A, b->A, b->A, b->m_ctx, ctx))
114
0
                goto err;
115
0
        } else {
116
0
            if (!BN_mod_mul(b->Ai, b->Ai, b->Ai, b->mod, ctx)
117
0
                || !BN_mod_mul(b->A, b->A, b->A, b->mod, ctx))
118
0
                goto err;
119
0
        }
120
0
    }
121
122
0
    ret = 1;
123
0
 err:
124
0
    if (b->counter == BN_BLINDING_COUNTER)
125
0
        b->counter = 0;
126
0
    return ret;
127
0
}
128
129
int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)
130
0
{
131
0
    return BN_BLINDING_convert_ex(n, NULL, b, ctx);
132
0
}
133
134
int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
135
8.87k
{
136
8.87k
    int ret = 1;
137
138
8.87k
    bn_check_top(n);
139
140
8.87k
    if ((b->A == NULL) || (b->Ai == NULL)) {
141
0
        ERR_raise(ERR_LIB_BN, BN_R_NOT_INITIALIZED);
142
0
        return 0;
143
0
    }
144
145
8.87k
    if (b->counter == -1)
146
        /* Fresh blinding, doesn't need updating. */
147
8.87k
        b->counter = 0;
148
0
    else if (!BN_BLINDING_update(b, ctx))
149
0
        return 0;
150
151
8.87k
    if (r != NULL && (BN_copy(r, b->Ai) == NULL))
152
0
        return 0;
153
154
8.87k
    if (b->m_ctx != NULL)
155
8.87k
        ret = BN_mod_mul_montgomery(n, n, b->A, b->m_ctx, ctx);
156
0
    else
157
0
        ret = BN_mod_mul(n, n, b->A, b->mod, ctx);
158
159
8.87k
    return ret;
160
8.87k
}
161
162
int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)
163
0
{
164
0
    return BN_BLINDING_invert_ex(n, NULL, b, ctx);
165
0
}
166
167
int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b,
168
                          BN_CTX *ctx)
169
8.87k
{
170
8.87k
    int ret;
171
172
8.87k
    bn_check_top(n);
173
174
8.87k
    if (r == NULL && (r = b->Ai) == NULL) {
175
0
        ERR_raise(ERR_LIB_BN, BN_R_NOT_INITIALIZED);
176
0
        return 0;
177
0
    }
178
179
8.87k
    if (b->m_ctx != NULL) {
180
        /* ensure that BN_mod_mul_montgomery takes pre-defined path */
181
8.87k
        if (n->dmax >= r->top) {
182
8.87k
            size_t i, rtop = r->top, ntop = n->top;
183
8.87k
            BN_ULONG mask;
184
185
292k
            for (i = 0; i < rtop; i++) {
186
284k
                mask = (BN_ULONG)0 - ((i - ntop) >> (8 * sizeof(i) - 1));
187
284k
                n->d[i] &= mask;
188
284k
            }
189
8.87k
            mask = (BN_ULONG)0 - ((rtop - ntop) >> (8 * sizeof(ntop) - 1));
190
            /* always true, if (rtop >= ntop) n->top = r->top; */
191
8.87k
            n->top = (int)(rtop & ~mask) | (ntop & mask);
192
8.87k
            n->flags |= (BN_FLG_FIXED_TOP & ~mask);
193
8.87k
        }
194
8.87k
        ret = bn_mul_mont_fixed_top(n, n, r, b->m_ctx, ctx);
195
8.87k
        bn_correct_top_consttime(n);
196
8.87k
    } else {
197
0
        ret = BN_mod_mul(n, n, r, b->mod, ctx);
198
0
    }
199
200
8.87k
    bn_check_top(n);
201
8.87k
    return ret;
202
8.87k
}
203
204
int BN_BLINDING_is_current_thread(BN_BLINDING *b)
205
8.87k
{
206
8.87k
    return CRYPTO_THREAD_compare_id(CRYPTO_THREAD_get_current_id(), b->tid);
207
8.87k
}
208
209
void BN_BLINDING_set_current_thread(BN_BLINDING *b)
210
17.7k
{
211
17.7k
    b->tid = CRYPTO_THREAD_get_current_id();
212
17.7k
}
213
214
int BN_BLINDING_lock(BN_BLINDING *b)
215
0
{
216
0
    return CRYPTO_THREAD_write_lock(b->lock);
217
0
}
218
219
int BN_BLINDING_unlock(BN_BLINDING *b)
220
0
{
221
0
    return CRYPTO_THREAD_unlock(b->lock);
222
0
}
223
224
unsigned long BN_BLINDING_get_flags(const BN_BLINDING *b)
225
0
{
226
0
    return b->flags;
227
0
}
228
229
void BN_BLINDING_set_flags(BN_BLINDING *b, unsigned long flags)
230
0
{
231
0
    b->flags = flags;
232
0
}
233
234
BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
235
                                      const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
236
                                      int (*bn_mod_exp) (BIGNUM *r,
237
                                                         const BIGNUM *a,
238
                                                         const BIGNUM *p,
239
                                                         const BIGNUM *m,
240
                                                         BN_CTX *ctx,
241
                                                         BN_MONT_CTX *m_ctx),
242
                                      BN_MONT_CTX *m_ctx)
243
8.87k
{
244
8.87k
    int retry_counter = 32;
245
8.87k
    BN_BLINDING *ret = NULL;
246
247
8.87k
    if (b == NULL)
248
8.87k
        ret = BN_BLINDING_new(NULL, NULL, m);
249
0
    else
250
0
        ret = b;
251
252
8.87k
    if (ret == NULL)
253
0
        goto err;
254
255
8.87k
    if (ret->A == NULL && (ret->A = BN_new()) == NULL)
256
0
        goto err;
257
8.87k
    if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL)
258
0
        goto err;
259
260
8.87k
    if (e != NULL) {
261
8.87k
        BN_free(ret->e);
262
8.87k
        ret->e = BN_dup(e);
263
8.87k
    }
264
8.87k
    if (ret->e == NULL)
265
0
        goto err;
266
267
8.87k
    if (bn_mod_exp != NULL)
268
8.87k
        ret->bn_mod_exp = bn_mod_exp;
269
8.87k
    if (m_ctx != NULL)
270
8.87k
        ret->m_ctx = m_ctx;
271
272
8.87k
    do {
273
8.87k
        int rv;
274
8.87k
        if (!BN_priv_rand_range_ex(ret->A, ret->mod, 0, ctx))
275
0
            goto err;
276
8.87k
        if (int_bn_mod_inverse(ret->Ai, ret->A, ret->mod, ctx, &rv))
277
8.87k
            break;
278
279
        /*
280
         * this should almost never happen for good RSA keys
281
         */
282
0
        if (!rv)
283
0
            goto err;
284
285
0
        if (retry_counter-- == 0) {
286
0
            ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
287
0
            goto err;
288
0
        }
289
0
    } while (1);
290
291
8.87k
    if (ret->bn_mod_exp != NULL && ret->m_ctx != NULL) {
292
8.87k
        if (!ret->bn_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx))
293
0
            goto err;
294
8.87k
    } else {
295
0
        if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx))
296
0
            goto err;
297
0
    }
298
299
8.87k
    if (ret->m_ctx != NULL) {
300
8.87k
        if (!bn_to_mont_fixed_top(ret->Ai, ret->Ai, ret->m_ctx, ctx)
301
8.87k
            || !bn_to_mont_fixed_top(ret->A, ret->A, ret->m_ctx, ctx))
302
0
            goto err;
303
8.87k
    }
304
305
8.87k
    return ret;
306
0
 err:
307
0
    if (b == NULL) {
308
0
        BN_BLINDING_free(ret);
309
0
        ret = NULL;
310
0
    }
311
312
0
    return ret;
313
8.87k
}