Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/bn/bn_blind.c
Line
Count
Source
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
17.3k
{
32
17.3k
    BN_BLINDING *ret = NULL;
33
34
17.3k
    bn_check_top(mod);
35
36
17.3k
    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
17.3k
    ret->lock = CRYPTO_THREAD_lock_new();
42
17.3k
    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
17.3k
    BN_BLINDING_set_current_thread(ret);
49
50
17.3k
    if (A != NULL) {
51
0
        if ((ret->A = BN_dup(A)) == NULL)
52
0
            goto err;
53
0
    }
54
55
17.3k
    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
17.3k
    if ((ret->mod = BN_dup(mod)) == NULL)
62
0
        goto err;
63
64
17.3k
    if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
65
17.3k
        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
17.3k
    ret->counter = -1;
73
74
17.3k
    return ret;
75
76
0
err:
77
0
    BN_BLINDING_free(ret);
78
0
    return NULL;
79
17.3k
}
80
81
void BN_BLINDING_free(BN_BLINDING *r)
82
821k
{
83
821k
    if (r == NULL)
84
804k
        return;
85
17.3k
    BN_free(r->A);
86
17.3k
    BN_free(r->Ai);
87
17.3k
    BN_free(r->e);
88
17.3k
    BN_free(r->mod);
89
17.3k
    CRYPTO_THREAD_lock_free(r->lock);
90
17.3k
    OPENSSL_free(r);
91
17.3k
}
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 && !(b->flags & BN_BLINDING_NO_RECREATE)) {
106
        /* re-create blinding parameters */
107
0
        if (!BN_BLINDING_create_param(b, NULL, NULL, ctx, NULL, NULL))
108
0
            goto err;
109
0
    } else if (!(b->flags & BN_BLINDING_NO_UPDATE)) {
110
0
        if (b->m_ctx != NULL) {
111
0
            if (!bn_mul_mont_fixed_top(b->Ai, b->Ai, b->Ai, b->m_ctx, ctx)
112
0
                || !bn_mul_mont_fixed_top(b->A, b->A, b->A, b->m_ctx, ctx))
113
0
                goto err;
114
0
        } else {
115
0
            if (!BN_mod_mul(b->Ai, b->Ai, b->Ai, b->mod, ctx)
116
0
                || !BN_mod_mul(b->A, b->A, b->A, b->mod, ctx))
117
0
                goto err;
118
0
        }
119
0
    }
120
121
0
    ret = 1;
122
0
err:
123
0
    if (b->counter == BN_BLINDING_COUNTER)
124
0
        b->counter = 0;
125
0
    return ret;
126
0
}
127
128
int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)
129
0
{
130
0
    return BN_BLINDING_convert_ex(n, NULL, b, ctx);
131
0
}
132
133
int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
134
17.3k
{
135
17.3k
    int ret = 1;
136
137
17.3k
    bn_check_top(n);
138
139
17.3k
    if ((b->A == NULL) || (b->Ai == NULL)) {
140
0
        ERR_raise(ERR_LIB_BN, BN_R_NOT_INITIALIZED);
141
0
        return 0;
142
0
    }
143
144
17.3k
    if (b->counter == -1)
145
        /* Fresh blinding, doesn't need updating. */
146
17.3k
        b->counter = 0;
147
0
    else if (!BN_BLINDING_update(b, ctx))
148
0
        return 0;
149
150
17.3k
    if (r != NULL && (BN_copy(r, b->Ai) == NULL))
151
0
        return 0;
152
153
17.3k
    if (b->m_ctx != NULL)
154
17.3k
        ret = BN_mod_mul_montgomery(n, n, b->A, b->m_ctx, ctx);
155
0
    else
156
0
        ret = BN_mod_mul(n, n, b->A, b->mod, ctx);
157
158
17.3k
    return ret;
159
17.3k
}
160
161
int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)
162
0
{
163
0
    return BN_BLINDING_invert_ex(n, NULL, b, ctx);
164
0
}
165
166
int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b,
167
    BN_CTX *ctx)
168
17.3k
{
169
17.3k
    int ret;
170
171
17.3k
    bn_check_top(n);
172
173
17.3k
    if (r == NULL && (r = b->Ai) == NULL) {
174
0
        ERR_raise(ERR_LIB_BN, BN_R_NOT_INITIALIZED);
175
0
        return 0;
176
0
    }
177
178
17.3k
    if (b->m_ctx != NULL) {
179
        /* ensure that BN_mod_mul_montgomery takes pre-defined path */
180
17.3k
        if (n->dmax >= r->top) {
181
17.3k
            size_t i, rtop = r->top, ntop = n->top;
182
17.3k
            BN_ULONG mask;
183
184
572k
            for (i = 0; i < rtop; i++) {
185
554k
                mask = (BN_ULONG)0 - ((i - ntop) >> (8 * sizeof(i) - 1));
186
554k
                n->d[i] &= mask;
187
554k
            }
188
17.3k
            mask = (BN_ULONG)0 - ((rtop - ntop) >> (8 * sizeof(ntop) - 1));
189
            /* always true, if (rtop >= ntop) n->top = r->top; */
190
17.3k
            n->top = (int)(rtop & ~mask) | (ntop & mask);
191
17.3k
            n->flags |= (BN_FLG_FIXED_TOP & ~mask);
192
17.3k
        }
193
17.3k
        ret = bn_mul_mont_fixed_top(n, n, r, b->m_ctx, ctx);
194
17.3k
        bn_correct_top_consttime(n);
195
17.3k
    } else {
196
0
        ret = BN_mod_mul(n, n, r, b->mod, ctx);
197
0
    }
198
199
17.3k
    bn_check_top(n);
200
17.3k
    return ret;
201
17.3k
}
202
203
int BN_BLINDING_is_current_thread(BN_BLINDING *b)
204
10.7k
{
205
10.7k
    return CRYPTO_THREAD_compare_id(CRYPTO_THREAD_get_current_id(), b->tid);
206
10.7k
}
207
208
void BN_BLINDING_set_current_thread(BN_BLINDING *b)
209
28.1k
{
210
28.1k
    b->tid = CRYPTO_THREAD_get_current_id();
211
28.1k
}
212
213
int BN_BLINDING_lock(BN_BLINDING *b)
214
0
{
215
0
    return CRYPTO_THREAD_write_lock(b->lock);
216
0
}
217
218
int BN_BLINDING_unlock(BN_BLINDING *b)
219
0
{
220
0
    return CRYPTO_THREAD_unlock(b->lock);
221
0
}
222
223
unsigned long BN_BLINDING_get_flags(const BN_BLINDING *b)
224
0
{
225
0
    return b->flags;
226
0
}
227
228
void BN_BLINDING_set_flags(BN_BLINDING *b, unsigned long flags)
229
0
{
230
0
    b->flags = flags;
231
0
}
232
233
BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
234
    const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
235
    int (*bn_mod_exp)(BIGNUM *r,
236
        const BIGNUM *a,
237
        const BIGNUM *p,
238
        const BIGNUM *m,
239
        BN_CTX *ctx,
240
        BN_MONT_CTX *m_ctx),
241
    BN_MONT_CTX *m_ctx)
242
17.3k
{
243
17.3k
    int retry_counter = 32;
244
17.3k
    BN_BLINDING *ret = NULL;
245
246
17.3k
    if (b == NULL)
247
17.3k
        ret = BN_BLINDING_new(NULL, NULL, m);
248
0
    else
249
0
        ret = b;
250
251
17.3k
    if (ret == NULL)
252
0
        goto err;
253
254
17.3k
    if (ret->A == NULL && (ret->A = BN_new()) == NULL)
255
0
        goto err;
256
17.3k
    if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL)
257
0
        goto err;
258
259
17.3k
    if (e != NULL) {
260
17.3k
        BN_free(ret->e);
261
17.3k
        ret->e = BN_dup(e);
262
17.3k
    }
263
17.3k
    if (ret->e == NULL)
264
0
        goto err;
265
266
17.3k
    if (bn_mod_exp != NULL)
267
17.3k
        ret->bn_mod_exp = bn_mod_exp;
268
17.3k
    if (m_ctx != NULL)
269
17.3k
        ret->m_ctx = m_ctx;
270
271
17.3k
    do {
272
17.3k
        int rv;
273
17.3k
        if (!BN_priv_rand_range_ex(ret->A, ret->mod, 0, ctx))
274
0
            goto err;
275
17.3k
        if (int_bn_mod_inverse(ret->Ai, ret->A, ret->mod, ctx, &rv))
276
17.3k
            break;
277
278
        /*
279
         * this should almost never happen for good RSA keys
280
         */
281
0
        if (!rv)
282
0
            goto err;
283
284
0
        if (retry_counter-- == 0) {
285
0
            ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
286
0
            goto err;
287
0
        }
288
0
    } while (1);
289
290
17.3k
    if (ret->bn_mod_exp != NULL && ret->m_ctx != NULL) {
291
17.3k
        if (!ret->bn_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx))
292
0
            goto err;
293
17.3k
    } else {
294
0
        if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx))
295
0
            goto err;
296
0
    }
297
298
17.3k
    if (ret->m_ctx != NULL) {
299
17.3k
        if (!bn_to_mont_fixed_top(ret->Ai, ret->Ai, ret->m_ctx, ctx)
300
17.3k
            || !bn_to_mont_fixed_top(ret->A, ret->A, ret->m_ctx, ctx))
301
0
            goto err;
302
17.3k
    }
303
304
17.3k
    return ret;
305
0
err:
306
0
    if (b == NULL) {
307
0
        BN_BLINDING_free(ret);
308
0
        ret = NULL;
309
0
    }
310
311
0
    return ret;
312
17.3k
}