Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/bn/bn_blind.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* | - | ||||||||||||
2 | * Copyright 1998-2018 The OpenSSL Project Authors. All Rights Reserved. | - | ||||||||||||
3 | * | - | ||||||||||||
4 | * Licensed under the OpenSSL license (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_lcl.h" | - | ||||||||||||
13 | - | |||||||||||||
14 | #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 | { | - | ||||||||||||
32 | BN_BLINDING *ret = NULL; | - | ||||||||||||
33 | - | |||||||||||||
34 | bn_check_top(mod); | - | ||||||||||||
35 | - | |||||||||||||
36 | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
| 0-1733 | ||||||||||||
37 | BNerr(BN_F_BN_BLINDING_NEW, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
38 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
39 | } | - | ||||||||||||
40 | - | |||||||||||||
41 | ret->lock = CRYPTO_THREAD_lock_new(); | - | ||||||||||||
42 | if (ret->lock == NULL) {
| 0-1733 | ||||||||||||
43 | BNerr(BN_F_BN_BLINDING_NEW, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
44 | OPENSSL_free(ret); | - | ||||||||||||
45 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
46 | } | - | ||||||||||||
47 | - | |||||||||||||
48 | BN_BLINDING_set_current_thread(ret); | - | ||||||||||||
49 | - | |||||||||||||
50 | if (A != NULL) {
| 0-1733 | ||||||||||||
51 | if ((ret->A = BN_dup(A)) == NULL)
| 0 | ||||||||||||
52 | goto err; never executed: goto err; | 0 | ||||||||||||
53 | } never executed: end of block | 0 | ||||||||||||
54 | - | |||||||||||||
55 | if (Ai != NULL) {
| 0-1733 | ||||||||||||
56 | if ((ret->Ai = BN_dup(Ai)) == NULL)
| 0 | ||||||||||||
57 | goto err; never executed: goto err; | 0 | ||||||||||||
58 | } never executed: end of block | 0 | ||||||||||||
59 | - | |||||||||||||
60 | /* save a copy of mod in the BN_BLINDING structure */ | - | ||||||||||||
61 | if ((ret->mod = BN_dup(mod)) == NULL)
| 0-1733 | ||||||||||||
62 | goto err; never executed: goto err; | 0 | ||||||||||||
63 | - | |||||||||||||
64 | if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
| 0-1733 | ||||||||||||
65 | BN_set_flags(ret->mod, BN_FLG_CONSTTIME); executed 1733 times by 1 test: BN_set_flags(ret->mod, 0x04); Executed by:
| 1733 | ||||||||||||
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 | ret->counter = -1; | - | ||||||||||||
73 | - | |||||||||||||
74 | return ret; executed 1733 times by 1 test: return ret; Executed by:
| 1733 | ||||||||||||
75 | - | |||||||||||||
76 | err: | - | ||||||||||||
77 | BN_BLINDING_free(ret); | - | ||||||||||||
78 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
79 | } | - | ||||||||||||
80 | - | |||||||||||||
81 | void BN_BLINDING_free(BN_BLINDING *r) | - | ||||||||||||
82 | { | - | ||||||||||||
83 | if (r == NULL)
| 1733-50207 | ||||||||||||
84 | return; executed 50207 times by 1 test: return; Executed by:
| 50207 | ||||||||||||
85 | BN_free(r->A); | - | ||||||||||||
86 | BN_free(r->Ai); | - | ||||||||||||
87 | BN_free(r->e); | - | ||||||||||||
88 | BN_free(r->mod); | - | ||||||||||||
89 | CRYPTO_THREAD_lock_free(r->lock); | - | ||||||||||||
90 | OPENSSL_free(r); | - | ||||||||||||
91 | } executed 1733 times by 1 test: end of block Executed by:
| 1733 | ||||||||||||
92 | - | |||||||||||||
93 | int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx) | - | ||||||||||||
94 | { | - | ||||||||||||
95 | int ret = 0; | - | ||||||||||||
96 | - | |||||||||||||
97 | if ((b->A == NULL) || (b->Ai == NULL)) {
| 0-648 | ||||||||||||
98 | BNerr(BN_F_BN_BLINDING_UPDATE, BN_R_NOT_INITIALIZED); | - | ||||||||||||
99 | goto err; never executed: goto err; | 0 | ||||||||||||
100 | } | - | ||||||||||||
101 | - | |||||||||||||
102 | if (b->counter == -1)
| 0-648 | ||||||||||||
103 | b->counter = 0; never executed: b->counter = 0; | 0 | ||||||||||||
104 | - | |||||||||||||
105 | if (++b->counter == BN_BLINDING_COUNTER && b->e != NULL &&
| 0-633 | ||||||||||||
106 | !(b->flags & BN_BLINDING_NO_RECREATE)) {
| 0-15 | ||||||||||||
107 | /* re-create blinding parameters */ | - | ||||||||||||
108 | if (!BN_BLINDING_create_param(b, NULL, NULL, ctx, NULL, NULL))
| 0-15 | ||||||||||||
109 | goto err; never executed: goto err; | 0 | ||||||||||||
110 | } else if (!(b->flags & BN_BLINDING_NO_UPDATE)) { executed 15 times by 1 test: end of block Executed by:
| 0-633 | ||||||||||||
111 | if (b->m_ctx != NULL) {
| 161-472 | ||||||||||||
112 | if (!bn_mul_mont_fixed_top(b->Ai, b->Ai, b->Ai, b->m_ctx, ctx)
| 0-472 | ||||||||||||
113 | || !bn_mul_mont_fixed_top(b->A, b->A, b->A, b->m_ctx, ctx))
| 0-472 | ||||||||||||
114 | goto err; never executed: goto err; | 0 | ||||||||||||
115 | } else { executed 472 times by 1 test: end of block Executed by:
| 472 | ||||||||||||
116 | if (!BN_mod_mul(b->Ai, b->Ai, b->Ai, b->mod, ctx)
| 0-161 | ||||||||||||
117 | || !BN_mod_mul(b->A, b->A, b->A, b->mod, ctx))
| 0-161 | ||||||||||||
118 | goto err; never executed: goto err; | 0 | ||||||||||||
119 | } executed 161 times by 1 test: end of block Executed by:
| 161 | ||||||||||||
120 | } | - | ||||||||||||
121 | - | |||||||||||||
122 | ret = 1; | - | ||||||||||||
123 | err: code before this statement executed 648 times by 1 test: err: Executed by:
| 648 | ||||||||||||
124 | if (b->counter == BN_BLINDING_COUNTER)
| 15-633 | ||||||||||||
125 | b->counter = 0; executed 15 times by 1 test: b->counter = 0; Executed by:
| 15 | ||||||||||||
126 | return ret; executed 648 times by 1 test: return ret; Executed by:
| 648 | ||||||||||||
127 | } | - | ||||||||||||
128 | - | |||||||||||||
129 | int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx) | - | ||||||||||||
130 | { | - | ||||||||||||
131 | return BN_BLINDING_convert_ex(n, NULL, b, ctx); never executed: return BN_BLINDING_convert_ex(n, ((void *)0) , b, ctx); | 0 | ||||||||||||
132 | } | - | ||||||||||||
133 | - | |||||||||||||
134 | int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx) | - | ||||||||||||
135 | { | - | ||||||||||||
136 | int ret = 1; | - | ||||||||||||
137 | - | |||||||||||||
138 | bn_check_top(n); | - | ||||||||||||
139 | - | |||||||||||||
140 | if ((b->A == NULL) || (b->Ai == NULL)) {
| 0-2381 | ||||||||||||
141 | BNerr(BN_F_BN_BLINDING_CONVERT_EX, BN_R_NOT_INITIALIZED); | - | ||||||||||||
142 | return 0; never executed: return 0; | 0 | ||||||||||||
143 | } | - | ||||||||||||
144 | - | |||||||||||||
145 | if (b->counter == -1)
| 648-1733 | ||||||||||||
146 | /* Fresh blinding, doesn't need updating. */ | - | ||||||||||||
147 | b->counter = 0; executed 1733 times by 1 test: b->counter = 0; Executed by:
| 1733 | ||||||||||||
148 | else if (!BN_BLINDING_update(b, ctx))
| 0-648 | ||||||||||||
149 | return 0; never executed: return 0; | 0 | ||||||||||||
150 | - | |||||||||||||
151 | if (r != NULL && (BN_copy(r, b->Ai) == NULL))
| 0-2381 | ||||||||||||
152 | return 0; never executed: return 0; | 0 | ||||||||||||
153 | - | |||||||||||||
154 | if (b->m_ctx != NULL)
| 495-1886 | ||||||||||||
155 | ret = BN_mod_mul_montgomery(n, n, b->A, b->m_ctx, ctx); executed 495 times by 1 test: ret = BN_mod_mul_montgomery(n, n, b->A, b->m_ctx, ctx); Executed by:
| 495 | ||||||||||||
156 | else | - | ||||||||||||
157 | ret = BN_mod_mul(n, n, b->A, b->mod, ctx); executed 1886 times by 1 test: ret = BN_mod_mul(n, n, b->A, b->mod, ctx); Executed by:
| 1886 | ||||||||||||
158 | - | |||||||||||||
159 | return ret; executed 2381 times by 1 test: return ret; Executed by:
| 2381 | ||||||||||||
160 | } | - | ||||||||||||
161 | - | |||||||||||||
162 | int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx) | - | ||||||||||||
163 | { | - | ||||||||||||
164 | return BN_BLINDING_invert_ex(n, NULL, b, ctx); never executed: return BN_BLINDING_invert_ex(n, ((void *)0) , b, ctx); | 0 | ||||||||||||
165 | } | - | ||||||||||||
166 | - | |||||||||||||
167 | int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, | - | ||||||||||||
168 | BN_CTX *ctx) | - | ||||||||||||
169 | { | - | ||||||||||||
170 | int ret; | - | ||||||||||||
171 | - | |||||||||||||
172 | bn_check_top(n); | - | ||||||||||||
173 | - | |||||||||||||
174 | if (r == NULL && (r = b->Ai) == NULL) {
| 0-2381 | ||||||||||||
175 | BNerr(BN_F_BN_BLINDING_INVERT_EX, BN_R_NOT_INITIALIZED); | - | ||||||||||||
176 | return 0; never executed: return 0; | 0 | ||||||||||||
177 | } | - | ||||||||||||
178 | - | |||||||||||||
179 | if (b->m_ctx != NULL) {
| 495-1886 | ||||||||||||
180 | /* ensure that BN_mod_mul_montgomery takes pre-defined path */ | - | ||||||||||||
181 | if (n->dmax >= r->top) {
| 0-495 | ||||||||||||
182 | size_t i, rtop = r->top, ntop = n->top; | - | ||||||||||||
183 | BN_ULONG mask; | - | ||||||||||||
184 | - | |||||||||||||
185 | for (i = 0; i < rtop; i++) {
| 495-5961 | ||||||||||||
186 | mask = (BN_ULONG)0 - ((i - ntop) >> (8 * sizeof(i) - 1)); | - | ||||||||||||
187 | n->d[i] &= mask; | - | ||||||||||||
188 | } executed 5961 times by 1 test: end of block Executed by:
| 5961 | ||||||||||||
189 | mask = (BN_ULONG)0 - ((rtop - ntop) >> (8 * sizeof(ntop) - 1)); | - | ||||||||||||
190 | /* always true, if (rtop >= ntop) n->top = r->top; */ | - | ||||||||||||
191 | n->top = (int)(rtop & ~mask) | (ntop & mask); | - | ||||||||||||
192 | n->flags |= (BN_FLG_FIXED_TOP & ~mask); | - | ||||||||||||
193 | } executed 495 times by 1 test: end of block Executed by:
| 495 | ||||||||||||
194 | ret = BN_mod_mul_montgomery(n, n, r, b->m_ctx, ctx); | - | ||||||||||||
195 | } else { executed 495 times by 1 test: end of block Executed by:
| 495 | ||||||||||||
196 | ret = BN_mod_mul(n, n, r, b->mod, ctx); | - | ||||||||||||
197 | } executed 1886 times by 1 test: end of block Executed by:
| 1886 | ||||||||||||
198 | - | |||||||||||||
199 | bn_check_top(n); | - | ||||||||||||
200 | return ret; executed 2381 times by 1 test: return ret; Executed by:
| 2381 | ||||||||||||
201 | } | - | ||||||||||||
202 | - | |||||||||||||
203 | int BN_BLINDING_is_current_thread(BN_BLINDING *b) | - | ||||||||||||
204 | { | - | ||||||||||||
205 | return CRYPTO_THREAD_compare_id(CRYPTO_THREAD_get_current_id(), b->tid); executed 2381 times by 1 test: return CRYPTO_THREAD_compare_id(CRYPTO_THREAD_get_current_id(), b->tid); Executed by:
| 2381 | ||||||||||||
206 | } | - | ||||||||||||
207 | - | |||||||||||||
208 | void BN_BLINDING_set_current_thread(BN_BLINDING *b) | - | ||||||||||||
209 | { | - | ||||||||||||
210 | b->tid = CRYPTO_THREAD_get_current_id(); | - | ||||||||||||
211 | } executed 3466 times by 1 test: end of block Executed by:
| 3466 | ||||||||||||
212 | - | |||||||||||||
213 | int BN_BLINDING_lock(BN_BLINDING *b) | - | ||||||||||||
214 | { | - | ||||||||||||
215 | return CRYPTO_THREAD_write_lock(b->lock); never executed: return CRYPTO_THREAD_write_lock(b->lock); | 0 | ||||||||||||
216 | } | - | ||||||||||||
217 | - | |||||||||||||
218 | int BN_BLINDING_unlock(BN_BLINDING *b) | - | ||||||||||||
219 | { | - | ||||||||||||
220 | return CRYPTO_THREAD_unlock(b->lock); never executed: return CRYPTO_THREAD_unlock(b->lock); | 0 | ||||||||||||
221 | } | - | ||||||||||||
222 | - | |||||||||||||
223 | unsigned long BN_BLINDING_get_flags(const BN_BLINDING *b) | - | ||||||||||||
224 | { | - | ||||||||||||
225 | return b->flags; never executed: return b->flags; | 0 | ||||||||||||
226 | } | - | ||||||||||||
227 | - | |||||||||||||
228 | void BN_BLINDING_set_flags(BN_BLINDING *b, unsigned long flags) | - | ||||||||||||
229 | { | - | ||||||||||||
230 | b->flags = flags; | - | ||||||||||||
231 | } never executed: end of block | 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 | { | - | ||||||||||||
243 | int retry_counter = 32; | - | ||||||||||||
244 | BN_BLINDING *ret = NULL; | - | ||||||||||||
245 | - | |||||||||||||
246 | if (b == NULL)
| 15-1733 | ||||||||||||
247 | ret = BN_BLINDING_new(NULL, NULL, m); executed 1733 times by 1 test: ret = BN_BLINDING_new( ((void *)0) , ((void *)0) , m); Executed by:
| 1733 | ||||||||||||
248 | else | - | ||||||||||||
249 | ret = b; executed 15 times by 1 test: ret = b; Executed by:
| 15 | ||||||||||||
250 | - | |||||||||||||
251 | if (ret == NULL)
| 0-1748 | ||||||||||||
252 | goto err; never executed: goto err; | 0 | ||||||||||||
253 | - | |||||||||||||
254 | if (ret->A == NULL && (ret->A = BN_new()) == NULL)
| 0-1733 | ||||||||||||
255 | goto err; never executed: goto err; | 0 | ||||||||||||
256 | if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL)
| 0-1733 | ||||||||||||
257 | goto err; never executed: goto err; | 0 | ||||||||||||
258 | - | |||||||||||||
259 | if (e != NULL) {
| 15-1733 | ||||||||||||
260 | BN_free(ret->e); | - | ||||||||||||
261 | ret->e = BN_dup(e); | - | ||||||||||||
262 | } executed 1733 times by 1 test: end of block Executed by:
| 1733 | ||||||||||||
263 | if (ret->e == NULL)
| 0-1748 | ||||||||||||
264 | goto err; never executed: goto err; | 0 | ||||||||||||
265 | - | |||||||||||||
266 | if (bn_mod_exp != NULL)
| 15-1733 | ||||||||||||
267 | ret->bn_mod_exp = bn_mod_exp; executed 1733 times by 1 test: ret->bn_mod_exp = bn_mod_exp; Executed by:
| 1733 | ||||||||||||
268 | if (m_ctx != NULL)
| 8-1740 | ||||||||||||
269 | ret->m_ctx = m_ctx; executed 8 times by 1 test: ret->m_ctx = m_ctx; Executed by:
| 8 | ||||||||||||
270 | - | |||||||||||||
271 | do { | - | ||||||||||||
272 | int rv; | - | ||||||||||||
273 | if (!BN_priv_rand_range(ret->A, ret->mod))
| 0-1748 | ||||||||||||
274 | goto err; never executed: goto err; | 0 | ||||||||||||
275 | if (int_bn_mod_inverse(ret->Ai, ret->A, ret->mod, ctx, &rv))
| 0-1748 | ||||||||||||
276 | break; executed 1748 times by 1 test: break; Executed by:
| 1748 | ||||||||||||
277 | - | |||||||||||||
278 | /* | - | ||||||||||||
279 | * this should almost never happen for good RSA keys | - | ||||||||||||
280 | */ | - | ||||||||||||
281 | if (!rv)
| 0 | ||||||||||||
282 | goto err; never executed: goto err; | 0 | ||||||||||||
283 | - | |||||||||||||
284 | if (retry_counter-- == 0) {
| 0 | ||||||||||||
285 | BNerr(BN_F_BN_BLINDING_CREATE_PARAM, BN_R_TOO_MANY_ITERATIONS); | - | ||||||||||||
286 | goto err; never executed: goto err; | 0 | ||||||||||||
287 | } | - | ||||||||||||
288 | } while (1); never executed: end of block | 0 | ||||||||||||
289 | - | |||||||||||||
290 | if (ret->bn_mod_exp != NULL && ret->m_ctx != NULL) {
| 0-1748 | ||||||||||||
291 | if (!ret->bn_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx))
| 0-23 | ||||||||||||
292 | goto err; never executed: goto err; | 0 | ||||||||||||
293 | } else { executed 23 times by 1 test: end of block Executed by:
| 23 | ||||||||||||
294 | if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx))
| 0-1725 | ||||||||||||
295 | goto err; never executed: goto err; | 0 | ||||||||||||
296 | } executed 1725 times by 1 test: end of block Executed by:
| 1725 | ||||||||||||
297 | - | |||||||||||||
298 | if (ret->m_ctx != NULL) {
| 23-1725 | ||||||||||||
299 | if (!bn_to_mont_fixed_top(ret->Ai, ret->Ai, ret->m_ctx, ctx)
| 0-23 | ||||||||||||
300 | || !bn_to_mont_fixed_top(ret->A, ret->A, ret->m_ctx, ctx))
| 0-23 | ||||||||||||
301 | goto err; never executed: goto err; | 0 | ||||||||||||
302 | } executed 23 times by 1 test: end of block Executed by:
| 23 | ||||||||||||
303 | - | |||||||||||||
304 | return ret; executed 1748 times by 1 test: return ret; Executed by:
| 1748 | ||||||||||||
305 | err: | - | ||||||||||||
306 | if (b == NULL) {
| 0 | ||||||||||||
307 | BN_BLINDING_free(ret); | - | ||||||||||||
308 | ret = NULL; | - | ||||||||||||
309 | } never executed: end of block | 0 | ||||||||||||
310 | - | |||||||||||||
311 | return ret; never executed: return ret; | 0 | ||||||||||||
312 | } | - | ||||||||||||
Source code | Switch to Preprocessed file |