Coverage Report

Created: 2025-06-13 06:55

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