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