/src/openssl30/crypto/bn/bn_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-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 <assert.h> |
11 | | #include <limits.h> |
12 | | #include "internal/cryptlib.h" |
13 | | #include "internal/endian.h" |
14 | | #include "bn_local.h" |
15 | | #include <openssl/opensslconf.h> |
16 | | #include "internal/constant_time.h" |
17 | | |
18 | | /* This stuff appears to be completely unused, so is deprecated */ |
19 | | #ifndef OPENSSL_NO_DEPRECATED_0_9_8 |
20 | | /*- |
21 | | * For a 32 bit machine |
22 | | * 2 - 4 == 128 |
23 | | * 3 - 8 == 256 |
24 | | * 4 - 16 == 512 |
25 | | * 5 - 32 == 1024 |
26 | | * 6 - 64 == 2048 |
27 | | * 7 - 128 == 4096 |
28 | | * 8 - 256 == 8192 |
29 | | */ |
30 | | static int bn_limit_bits = 0; |
31 | | static int bn_limit_num = 8; /* (1<<bn_limit_bits) */ |
32 | | static int bn_limit_bits_low = 0; |
33 | | static int bn_limit_num_low = 8; /* (1<<bn_limit_bits_low) */ |
34 | | static int bn_limit_bits_high = 0; |
35 | | static int bn_limit_num_high = 8; /* (1<<bn_limit_bits_high) */ |
36 | | static int bn_limit_bits_mont = 0; |
37 | | static int bn_limit_num_mont = 8; /* (1<<bn_limit_bits_mont) */ |
38 | | |
39 | | void BN_set_params(int mult, int high, int low, int mont) |
40 | 0 | { |
41 | 0 | if (mult >= 0) { |
42 | 0 | if (mult > (int)(sizeof(int) * 8) - 1) |
43 | 0 | mult = sizeof(int) * 8 - 1; |
44 | 0 | bn_limit_bits = mult; |
45 | 0 | bn_limit_num = 1 << mult; |
46 | 0 | } |
47 | 0 | if (high >= 0) { |
48 | 0 | if (high > (int)(sizeof(int) * 8) - 1) |
49 | 0 | high = sizeof(int) * 8 - 1; |
50 | 0 | bn_limit_bits_high = high; |
51 | 0 | bn_limit_num_high = 1 << high; |
52 | 0 | } |
53 | 0 | if (low >= 0) { |
54 | 0 | if (low > (int)(sizeof(int) * 8) - 1) |
55 | 0 | low = sizeof(int) * 8 - 1; |
56 | 0 | bn_limit_bits_low = low; |
57 | 0 | bn_limit_num_low = 1 << low; |
58 | 0 | } |
59 | 0 | if (mont >= 0) { |
60 | 0 | if (mont > (int)(sizeof(int) * 8) - 1) |
61 | 0 | mont = sizeof(int) * 8 - 1; |
62 | 0 | bn_limit_bits_mont = mont; |
63 | 0 | bn_limit_num_mont = 1 << mont; |
64 | 0 | } |
65 | 0 | } |
66 | | |
67 | | int BN_get_params(int which) |
68 | 0 | { |
69 | 0 | if (which == 0) |
70 | 0 | return bn_limit_bits; |
71 | 0 | else if (which == 1) |
72 | 0 | return bn_limit_bits_high; |
73 | 0 | else if (which == 2) |
74 | 0 | return bn_limit_bits_low; |
75 | 0 | else if (which == 3) |
76 | 0 | return bn_limit_bits_mont; |
77 | 0 | else |
78 | 0 | return 0; |
79 | 0 | } |
80 | | #endif |
81 | | |
82 | | const BIGNUM *BN_value_one(void) |
83 | 761 | { |
84 | 761 | static const BN_ULONG data_one = 1L; |
85 | 761 | static const BIGNUM const_one = |
86 | 761 | { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA }; |
87 | | |
88 | 761 | return &const_one; |
89 | 761 | } |
90 | | |
91 | | /* |
92 | | * Old Visual Studio ARM compiler miscompiles BN_num_bits_word() |
93 | | * https://mta.openssl.org/pipermail/openssl-users/2018-August/008465.html |
94 | | */ |
95 | | #if defined(_MSC_VER) && defined(_ARM_) && defined(_WIN32_WCE) \ |
96 | | && _MSC_VER>=1400 && _MSC_VER<1501 |
97 | | # define MS_BROKEN_BN_num_bits_word |
98 | | # pragma optimize("", off) |
99 | | #endif |
100 | | int BN_num_bits_word(BN_ULONG l) |
101 | 362k | { |
102 | 362k | BN_ULONG x, mask; |
103 | 362k | int bits = (l != 0); |
104 | | |
105 | 362k | #if BN_BITS2 > 32 |
106 | 362k | x = l >> 32; |
107 | 362k | mask = (0 - x) & BN_MASK2; |
108 | 362k | mask = (0 - (mask >> (BN_BITS2 - 1))); |
109 | 362k | bits += 32 & mask; |
110 | 362k | l ^= (x ^ l) & mask; |
111 | 362k | #endif |
112 | | |
113 | 362k | x = l >> 16; |
114 | 362k | mask = (0 - x) & BN_MASK2; |
115 | 362k | mask = (0 - (mask >> (BN_BITS2 - 1))); |
116 | 362k | bits += 16 & mask; |
117 | 362k | l ^= (x ^ l) & mask; |
118 | | |
119 | 362k | x = l >> 8; |
120 | 362k | mask = (0 - x) & BN_MASK2; |
121 | 362k | mask = (0 - (mask >> (BN_BITS2 - 1))); |
122 | 362k | bits += 8 & mask; |
123 | 362k | l ^= (x ^ l) & mask; |
124 | | |
125 | 362k | x = l >> 4; |
126 | 362k | mask = (0 - x) & BN_MASK2; |
127 | 362k | mask = (0 - (mask >> (BN_BITS2 - 1))); |
128 | 362k | bits += 4 & mask; |
129 | 362k | l ^= (x ^ l) & mask; |
130 | | |
131 | 362k | x = l >> 2; |
132 | 362k | mask = (0 - x) & BN_MASK2; |
133 | 362k | mask = (0 - (mask >> (BN_BITS2 - 1))); |
134 | 362k | bits += 2 & mask; |
135 | 362k | l ^= (x ^ l) & mask; |
136 | | |
137 | 362k | x = l >> 1; |
138 | 362k | mask = (0 - x) & BN_MASK2; |
139 | 362k | mask = (0 - (mask >> (BN_BITS2 - 1))); |
140 | 362k | bits += 1 & mask; |
141 | | |
142 | 362k | return bits; |
143 | 362k | } |
144 | | #ifdef MS_BROKEN_BN_num_bits_word |
145 | | # pragma optimize("", on) |
146 | | #endif |
147 | | |
148 | | /* |
149 | | * This function still leaks `a->dmax`: it's caller's responsibility to |
150 | | * expand the input `a` in advance to a public length. |
151 | | */ |
152 | | static ossl_inline |
153 | | int bn_num_bits_consttime(const BIGNUM *a) |
154 | 0 | { |
155 | 0 | int j, ret; |
156 | 0 | unsigned int mask, past_i; |
157 | 0 | int i = a->top - 1; |
158 | 0 | bn_check_top(a); |
159 | |
|
160 | 0 | for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) { |
161 | 0 | mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */ |
162 | |
|
163 | 0 | ret += BN_BITS2 & (~mask & ~past_i); |
164 | 0 | ret += BN_num_bits_word(a->d[j]) & mask; |
165 | |
|
166 | 0 | past_i |= mask; /* past_i will become 0xff..ff after i==j */ |
167 | 0 | } |
168 | | |
169 | | /* |
170 | | * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the |
171 | | * final result. |
172 | | */ |
173 | 0 | mask = ~(constant_time_eq_int(i, ((int)-1))); |
174 | |
|
175 | 0 | return ret & mask; |
176 | 0 | } |
177 | | |
178 | | int BN_num_bits(const BIGNUM *a) |
179 | 93.1k | { |
180 | 93.1k | int i = a->top - 1; |
181 | 93.1k | bn_check_top(a); |
182 | | |
183 | 93.1k | if (a->flags & BN_FLG_CONSTTIME) { |
184 | | /* |
185 | | * We assume that BIGNUMs flagged as CONSTTIME have also been expanded |
186 | | * so that a->dmax is not leaking secret information. |
187 | | * |
188 | | * In other words, it's the caller's responsibility to ensure `a` has |
189 | | * been preallocated in advance to a public length if we hit this |
190 | | * branch. |
191 | | * |
192 | | */ |
193 | 0 | return bn_num_bits_consttime(a); |
194 | 0 | } |
195 | | |
196 | 93.1k | if (BN_is_zero(a)) |
197 | 358 | return 0; |
198 | | |
199 | 92.8k | return ((i * BN_BITS2) + BN_num_bits_word(a->d[i])); |
200 | 93.1k | } |
201 | | |
202 | | static void bn_free_d(BIGNUM *a, int clear) |
203 | 82.3k | { |
204 | 82.3k | if (BN_get_flags(a, BN_FLG_SECURE)) |
205 | 0 | OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0])); |
206 | 82.3k | else if (clear != 0) |
207 | 63.2k | OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0])); |
208 | 19.0k | else |
209 | 19.0k | OPENSSL_free(a->d); |
210 | 82.3k | } |
211 | | |
212 | | |
213 | | void BN_clear_free(BIGNUM *a) |
214 | 37.0k | { |
215 | 37.0k | if (a == NULL) |
216 | 0 | return; |
217 | 37.0k | if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA)) |
218 | 35.7k | bn_free_d(a, 1); |
219 | 37.0k | if (BN_get_flags(a, BN_FLG_MALLOCED)) { |
220 | 0 | OPENSSL_cleanse(a, sizeof(*a)); |
221 | 0 | OPENSSL_free(a); |
222 | 0 | } |
223 | 37.0k | } |
224 | | |
225 | | void BN_free(BIGNUM *a) |
226 | 19.0k | { |
227 | 19.0k | if (a == NULL) |
228 | 0 | return; |
229 | 19.0k | if (!BN_get_flags(a, BN_FLG_STATIC_DATA)) |
230 | 19.0k | bn_free_d(a, 0); |
231 | 19.0k | if (a->flags & BN_FLG_MALLOCED) |
232 | 15.8k | OPENSSL_free(a); |
233 | 19.0k | } |
234 | | |
235 | | void bn_init(BIGNUM *a) |
236 | 57.7k | { |
237 | 57.7k | static BIGNUM nilbn; |
238 | | |
239 | 57.7k | *a = nilbn; |
240 | 57.7k | bn_check_top(a); |
241 | 57.7k | } |
242 | | |
243 | | BIGNUM *BN_new(void) |
244 | 15.8k | { |
245 | 15.8k | BIGNUM *ret; |
246 | | |
247 | 15.8k | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) { |
248 | 0 | ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE); |
249 | 0 | return NULL; |
250 | 0 | } |
251 | 15.8k | ret->flags = BN_FLG_MALLOCED; |
252 | 15.8k | bn_check_top(ret); |
253 | 15.8k | return ret; |
254 | 15.8k | } |
255 | | |
256 | | BIGNUM *BN_secure_new(void) |
257 | 0 | { |
258 | 0 | BIGNUM *ret = BN_new(); |
259 | 0 | if (ret != NULL) |
260 | 0 | ret->flags |= BN_FLG_SECURE; |
261 | 0 | return ret; |
262 | 0 | } |
263 | | |
264 | | /* This is used by bn_expand2() */ |
265 | | /* The caller MUST check that words > b->dmax before calling this */ |
266 | | static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) |
267 | 80.3k | { |
268 | 80.3k | BN_ULONG *a = NULL; |
269 | | |
270 | 80.3k | if (words > (INT_MAX / (4 * BN_BITS2))) { |
271 | 0 | ERR_raise(ERR_LIB_BN, BN_R_BIGNUM_TOO_LONG); |
272 | 0 | return NULL; |
273 | 0 | } |
274 | 80.3k | if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { |
275 | 0 | ERR_raise(ERR_LIB_BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); |
276 | 0 | return NULL; |
277 | 0 | } |
278 | 80.3k | if (BN_get_flags(b, BN_FLG_SECURE)) |
279 | 0 | a = OPENSSL_secure_zalloc(words * sizeof(*a)); |
280 | 80.3k | else |
281 | 80.3k | a = OPENSSL_zalloc(words * sizeof(*a)); |
282 | 80.3k | if (a == NULL) { |
283 | 0 | ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE); |
284 | 0 | return NULL; |
285 | 0 | } |
286 | | |
287 | 80.3k | assert(b->top <= words); |
288 | 80.3k | if (b->top > 0) |
289 | 10.4k | memcpy(a, b->d, sizeof(*a) * b->top); |
290 | | |
291 | 80.3k | return a; |
292 | 80.3k | } |
293 | | |
294 | | /* |
295 | | * This is an internal function that should not be used in applications. It |
296 | | * ensures that 'b' has enough room for a 'words' word number and initialises |
297 | | * any unused part of b->d with leading zeros. It is mostly used by the |
298 | | * various BIGNUM routines. If there is an error, NULL is returned. If not, |
299 | | * 'b' is returned. |
300 | | */ |
301 | | |
302 | | BIGNUM *bn_expand2(BIGNUM *b, int words) |
303 | 80.3k | { |
304 | 80.3k | if (words > b->dmax) { |
305 | 80.3k | BN_ULONG *a = bn_expand_internal(b, words); |
306 | 80.3k | if (!a) |
307 | 0 | return NULL; |
308 | 80.3k | if (b->d != NULL) |
309 | 27.5k | bn_free_d(b, 1); |
310 | 80.3k | b->d = a; |
311 | 80.3k | b->dmax = words; |
312 | 80.3k | } |
313 | | |
314 | 80.3k | return b; |
315 | 80.3k | } |
316 | | |
317 | | BIGNUM *BN_dup(const BIGNUM *a) |
318 | 0 | { |
319 | 0 | BIGNUM *t; |
320 | |
|
321 | 0 | if (a == NULL) |
322 | 0 | return NULL; |
323 | 0 | bn_check_top(a); |
324 | |
|
325 | 0 | t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new(); |
326 | 0 | if (t == NULL) |
327 | 0 | return NULL; |
328 | 0 | if (!BN_copy(t, a)) { |
329 | 0 | BN_free(t); |
330 | 0 | return NULL; |
331 | 0 | } |
332 | 0 | bn_check_top(t); |
333 | 0 | return t; |
334 | 0 | } |
335 | | |
336 | | BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) |
337 | 232k | { |
338 | 232k | int bn_words; |
339 | | |
340 | 232k | bn_check_top(b); |
341 | | |
342 | 232k | bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top; |
343 | | |
344 | 232k | if (a == b) |
345 | 0 | return a; |
346 | 232k | if (bn_wexpand(a, bn_words) == NULL) |
347 | 0 | return NULL; |
348 | | |
349 | 232k | if (b->top > 0) |
350 | 216k | memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words); |
351 | | |
352 | 232k | a->neg = b->neg; |
353 | 232k | a->top = b->top; |
354 | 232k | a->flags |= b->flags & BN_FLG_FIXED_TOP; |
355 | 232k | bn_check_top(a); |
356 | 232k | return a; |
357 | 232k | } |
358 | | |
359 | 0 | #define FLAGS_DATA(flags) ((flags) & (BN_FLG_STATIC_DATA \ |
360 | 0 | | BN_FLG_CONSTTIME \ |
361 | 0 | | BN_FLG_SECURE \ |
362 | 0 | | BN_FLG_FIXED_TOP)) |
363 | 0 | #define FLAGS_STRUCT(flags) ((flags) & (BN_FLG_MALLOCED)) |
364 | | |
365 | | void BN_swap(BIGNUM *a, BIGNUM *b) |
366 | 0 | { |
367 | 0 | int flags_old_a, flags_old_b; |
368 | 0 | BN_ULONG *tmp_d; |
369 | 0 | int tmp_top, tmp_dmax, tmp_neg; |
370 | |
|
371 | 0 | bn_check_top(a); |
372 | 0 | bn_check_top(b); |
373 | |
|
374 | 0 | flags_old_a = a->flags; |
375 | 0 | flags_old_b = b->flags; |
376 | |
|
377 | 0 | tmp_d = a->d; |
378 | 0 | tmp_top = a->top; |
379 | 0 | tmp_dmax = a->dmax; |
380 | 0 | tmp_neg = a->neg; |
381 | |
|
382 | 0 | a->d = b->d; |
383 | 0 | a->top = b->top; |
384 | 0 | a->dmax = b->dmax; |
385 | 0 | a->neg = b->neg; |
386 | |
|
387 | 0 | b->d = tmp_d; |
388 | 0 | b->top = tmp_top; |
389 | 0 | b->dmax = tmp_dmax; |
390 | 0 | b->neg = tmp_neg; |
391 | |
|
392 | 0 | a->flags = FLAGS_STRUCT(flags_old_a) | FLAGS_DATA(flags_old_b); |
393 | 0 | b->flags = FLAGS_STRUCT(flags_old_b) | FLAGS_DATA(flags_old_a); |
394 | 0 | bn_check_top(a); |
395 | 0 | bn_check_top(b); |
396 | 0 | } |
397 | | |
398 | | void BN_clear(BIGNUM *a) |
399 | 0 | { |
400 | 0 | if (a == NULL) |
401 | 0 | return; |
402 | 0 | bn_check_top(a); |
403 | 0 | if (a->d != NULL) |
404 | 0 | OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax); |
405 | 0 | a->neg = 0; |
406 | 0 | a->top = 0; |
407 | 0 | a->flags &= ~BN_FLG_FIXED_TOP; |
408 | 0 | } |
409 | | |
410 | | BN_ULONG BN_get_word(const BIGNUM *a) |
411 | 0 | { |
412 | 0 | if (a->top > 1) |
413 | 0 | return BN_MASK2; |
414 | 0 | else if (a->top == 1) |
415 | 0 | return a->d[0]; |
416 | | /* a->top == 0 */ |
417 | 0 | return 0; |
418 | 0 | } |
419 | | |
420 | | int BN_set_word(BIGNUM *a, BN_ULONG w) |
421 | 7.47k | { |
422 | 7.47k | bn_check_top(a); |
423 | 7.47k | if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL) |
424 | 0 | return 0; |
425 | 7.47k | a->neg = 0; |
426 | 7.47k | a->d[0] = w; |
427 | 7.47k | a->top = (w ? 1 : 0); |
428 | 7.47k | a->flags &= ~BN_FLG_FIXED_TOP; |
429 | 7.47k | bn_check_top(a); |
430 | 7.47k | return 1; |
431 | 7.47k | } |
432 | | |
433 | | BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret) |
434 | 9.51k | { |
435 | 9.51k | unsigned int i, m; |
436 | 9.51k | unsigned int n; |
437 | 9.51k | BN_ULONG l; |
438 | 9.51k | BIGNUM *bn = NULL; |
439 | | |
440 | 9.51k | if (ret == NULL) |
441 | 0 | ret = bn = BN_new(); |
442 | 9.51k | if (ret == NULL) |
443 | 0 | return NULL; |
444 | 9.51k | bn_check_top(ret); |
445 | | /* Skip leading zero's. */ |
446 | 9.86k | for ( ; len > 0 && *s == 0; s++, len--) |
447 | 355 | continue; |
448 | 9.51k | n = len; |
449 | 9.51k | if (n == 0) { |
450 | 1.15k | ret->top = 0; |
451 | 1.15k | return ret; |
452 | 1.15k | } |
453 | 8.35k | i = ((n - 1) / BN_BYTES) + 1; |
454 | 8.35k | m = ((n - 1) % (BN_BYTES)); |
455 | 8.35k | if (bn_wexpand(ret, (int)i) == NULL) { |
456 | 0 | BN_free(bn); |
457 | 0 | return NULL; |
458 | 0 | } |
459 | 8.35k | ret->top = i; |
460 | 8.35k | ret->neg = 0; |
461 | 8.35k | l = 0; |
462 | 373k | while (n--) { |
463 | 364k | l = (l << 8L) | *(s++); |
464 | 364k | if (m-- == 0) { |
465 | 51.3k | ret->d[--i] = l; |
466 | 51.3k | l = 0; |
467 | 51.3k | m = BN_BYTES - 1; |
468 | 51.3k | } |
469 | 364k | } |
470 | | /* |
471 | | * need to call this due to clear byte at top if avoiding having the top |
472 | | * bit set (-ve number) |
473 | | */ |
474 | 8.35k | bn_correct_top(ret); |
475 | 8.35k | return ret; |
476 | 8.35k | } |
477 | | |
478 | | typedef enum {big, little} endianess_t; |
479 | | |
480 | | /* ignore negative */ |
481 | | static |
482 | | int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen, endianess_t endianess) |
483 | 0 | { |
484 | 0 | int n; |
485 | 0 | size_t i, lasti, j, atop, mask; |
486 | 0 | BN_ULONG l; |
487 | | |
488 | | /* |
489 | | * In case |a| is fixed-top, BN_num_bytes can return bogus length, |
490 | | * but it's assumed that fixed-top inputs ought to be "nominated" |
491 | | * even for padded output, so it works out... |
492 | | */ |
493 | 0 | n = BN_num_bytes(a); |
494 | 0 | if (tolen == -1) { |
495 | 0 | tolen = n; |
496 | 0 | } else if (tolen < n) { /* uncommon/unlike case */ |
497 | 0 | BIGNUM temp = *a; |
498 | |
|
499 | 0 | bn_correct_top(&temp); |
500 | 0 | n = BN_num_bytes(&temp); |
501 | 0 | if (tolen < n) |
502 | 0 | return -1; |
503 | 0 | } |
504 | | |
505 | | /* Swipe through whole available data and don't give away padded zero. */ |
506 | 0 | atop = a->dmax * BN_BYTES; |
507 | 0 | if (atop == 0) { |
508 | 0 | if (tolen != 0) |
509 | 0 | memset(to, '\0', tolen); |
510 | 0 | return tolen; |
511 | 0 | } |
512 | | |
513 | 0 | lasti = atop - 1; |
514 | 0 | atop = a->top * BN_BYTES; |
515 | 0 | if (endianess == big) |
516 | 0 | to += tolen; /* start from the end of the buffer */ |
517 | 0 | for (i = 0, j = 0; j < (size_t)tolen; j++) { |
518 | 0 | unsigned char val; |
519 | 0 | l = a->d[i / BN_BYTES]; |
520 | 0 | mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1)); |
521 | 0 | val = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask); |
522 | 0 | if (endianess == big) |
523 | 0 | *--to = val; |
524 | 0 | else |
525 | 0 | *to++ = val; |
526 | 0 | i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */ |
527 | 0 | } |
528 | |
|
529 | 0 | return tolen; |
530 | 0 | } |
531 | | |
532 | | int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen) |
533 | 0 | { |
534 | 0 | if (tolen < 0) |
535 | 0 | return -1; |
536 | 0 | return bn2binpad(a, to, tolen, big); |
537 | 0 | } |
538 | | |
539 | | int BN_bn2bin(const BIGNUM *a, unsigned char *to) |
540 | 0 | { |
541 | 0 | return bn2binpad(a, to, -1, big); |
542 | 0 | } |
543 | | |
544 | | BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret) |
545 | 0 | { |
546 | 0 | unsigned int i, m; |
547 | 0 | unsigned int n; |
548 | 0 | BN_ULONG l; |
549 | 0 | BIGNUM *bn = NULL; |
550 | |
|
551 | 0 | if (ret == NULL) |
552 | 0 | ret = bn = BN_new(); |
553 | 0 | if (ret == NULL) |
554 | 0 | return NULL; |
555 | 0 | bn_check_top(ret); |
556 | 0 | s += len; |
557 | | /* Skip trailing zeroes. */ |
558 | 0 | for ( ; len > 0 && s[-1] == 0; s--, len--) |
559 | 0 | continue; |
560 | 0 | n = len; |
561 | 0 | if (n == 0) { |
562 | 0 | ret->top = 0; |
563 | 0 | return ret; |
564 | 0 | } |
565 | 0 | i = ((n - 1) / BN_BYTES) + 1; |
566 | 0 | m = ((n - 1) % (BN_BYTES)); |
567 | 0 | if (bn_wexpand(ret, (int)i) == NULL) { |
568 | 0 | BN_free(bn); |
569 | 0 | return NULL; |
570 | 0 | } |
571 | 0 | ret->top = i; |
572 | 0 | ret->neg = 0; |
573 | 0 | l = 0; |
574 | 0 | while (n--) { |
575 | 0 | s--; |
576 | 0 | l = (l << 8L) | *s; |
577 | 0 | if (m-- == 0) { |
578 | 0 | ret->d[--i] = l; |
579 | 0 | l = 0; |
580 | 0 | m = BN_BYTES - 1; |
581 | 0 | } |
582 | 0 | } |
583 | | /* |
584 | | * need to call this due to clear byte at top if avoiding having the top |
585 | | * bit set (-ve number) |
586 | | */ |
587 | 0 | bn_correct_top(ret); |
588 | 0 | return ret; |
589 | 0 | } |
590 | | |
591 | | int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen) |
592 | 0 | { |
593 | 0 | if (tolen < 0) |
594 | 0 | return -1; |
595 | 0 | return bn2binpad(a, to, tolen, little); |
596 | 0 | } |
597 | | |
598 | | BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret) |
599 | 0 | { |
600 | 0 | DECLARE_IS_ENDIAN; |
601 | |
|
602 | 0 | if (IS_LITTLE_ENDIAN) |
603 | 0 | return BN_lebin2bn(s, len, ret); |
604 | 0 | return BN_bin2bn(s, len, ret); |
605 | 0 | } |
606 | | |
607 | | int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen) |
608 | 0 | { |
609 | 0 | DECLARE_IS_ENDIAN; |
610 | |
|
611 | 0 | if (IS_LITTLE_ENDIAN) |
612 | 0 | return BN_bn2lebinpad(a, to, tolen); |
613 | 0 | return BN_bn2binpad(a, to, tolen); |
614 | 0 | } |
615 | | |
616 | | int BN_ucmp(const BIGNUM *a, const BIGNUM *b) |
617 | 312k | { |
618 | 312k | int i; |
619 | 312k | BN_ULONG t1, t2, *ap, *bp; |
620 | | |
621 | 312k | bn_check_top(a); |
622 | 312k | bn_check_top(b); |
623 | | |
624 | 312k | i = a->top - b->top; |
625 | 312k | if (i != 0) |
626 | 94.8k | return i; |
627 | 217k | ap = a->d; |
628 | 217k | bp = b->d; |
629 | 465k | for (i = a->top - 1; i >= 0; i--) { |
630 | 464k | t1 = ap[i]; |
631 | 464k | t2 = bp[i]; |
632 | 464k | if (t1 != t2) |
633 | 216k | return ((t1 > t2) ? 1 : -1); |
634 | 464k | } |
635 | 1.36k | return 0; |
636 | 217k | } |
637 | | |
638 | | int BN_cmp(const BIGNUM *a, const BIGNUM *b) |
639 | 3.12k | { |
640 | 3.12k | int i; |
641 | 3.12k | int gt, lt; |
642 | 3.12k | BN_ULONG t1, t2; |
643 | | |
644 | 3.12k | if ((a == NULL) || (b == NULL)) { |
645 | 0 | if (a != NULL) |
646 | 0 | return -1; |
647 | 0 | else if (b != NULL) |
648 | 0 | return 1; |
649 | 0 | else |
650 | 0 | return 0; |
651 | 0 | } |
652 | | |
653 | 3.12k | bn_check_top(a); |
654 | 3.12k | bn_check_top(b); |
655 | | |
656 | 3.12k | if (a->neg != b->neg) { |
657 | 0 | if (a->neg) |
658 | 0 | return -1; |
659 | 0 | else |
660 | 0 | return 1; |
661 | 0 | } |
662 | 3.12k | if (a->neg == 0) { |
663 | 3.12k | gt = 1; |
664 | 3.12k | lt = -1; |
665 | 3.12k | } else { |
666 | 0 | gt = -1; |
667 | 0 | lt = 1; |
668 | 0 | } |
669 | | |
670 | 3.12k | if (a->top > b->top) |
671 | 0 | return gt; |
672 | 3.12k | if (a->top < b->top) |
673 | 0 | return lt; |
674 | 35.2k | for (i = a->top - 1; i >= 0; i--) { |
675 | 32.1k | t1 = a->d[i]; |
676 | 32.1k | t2 = b->d[i]; |
677 | 32.1k | if (t1 > t2) |
678 | 0 | return gt; |
679 | 32.1k | if (t1 < t2) |
680 | 0 | return lt; |
681 | 32.1k | } |
682 | 3.12k | return 0; |
683 | 3.12k | } |
684 | | |
685 | | int BN_set_bit(BIGNUM *a, int n) |
686 | 4.21k | { |
687 | 4.21k | int i, j, k; |
688 | | |
689 | 4.21k | if (n < 0) |
690 | 0 | return 0; |
691 | | |
692 | 4.21k | i = n / BN_BITS2; |
693 | 4.21k | j = n % BN_BITS2; |
694 | 4.21k | if (a->top <= i) { |
695 | 4.21k | if (bn_wexpand(a, i + 1) == NULL) |
696 | 0 | return 0; |
697 | 88.7k | for (k = a->top; k < i + 1; k++) |
698 | 84.5k | a->d[k] = 0; |
699 | 4.21k | a->top = i + 1; |
700 | 4.21k | a->flags &= ~BN_FLG_FIXED_TOP; |
701 | 4.21k | } |
702 | | |
703 | 4.21k | a->d[i] |= (((BN_ULONG)1) << j); |
704 | 4.21k | bn_check_top(a); |
705 | 4.21k | return 1; |
706 | 4.21k | } |
707 | | |
708 | | int BN_clear_bit(BIGNUM *a, int n) |
709 | 0 | { |
710 | 0 | int i, j; |
711 | |
|
712 | 0 | bn_check_top(a); |
713 | 0 | if (n < 0) |
714 | 0 | return 0; |
715 | | |
716 | 0 | i = n / BN_BITS2; |
717 | 0 | j = n % BN_BITS2; |
718 | 0 | if (a->top <= i) |
719 | 0 | return 0; |
720 | | |
721 | 0 | a->d[i] &= (~(((BN_ULONG)1) << j)); |
722 | 0 | bn_correct_top(a); |
723 | 0 | return 1; |
724 | 0 | } |
725 | | |
726 | | int BN_is_bit_set(const BIGNUM *a, int n) |
727 | 507k | { |
728 | 507k | int i, j; |
729 | | |
730 | 507k | bn_check_top(a); |
731 | 507k | if (n < 0) |
732 | 0 | return 0; |
733 | 507k | i = n / BN_BITS2; |
734 | 507k | j = n % BN_BITS2; |
735 | 507k | if (a->top <= i) |
736 | 0 | return 0; |
737 | 507k | return (int)(((a->d[i]) >> j) & ((BN_ULONG)1)); |
738 | 507k | } |
739 | | |
740 | | int BN_mask_bits(BIGNUM *a, int n) |
741 | 0 | { |
742 | 0 | int b, w; |
743 | |
|
744 | 0 | bn_check_top(a); |
745 | 0 | if (n < 0) |
746 | 0 | return 0; |
747 | | |
748 | 0 | w = n / BN_BITS2; |
749 | 0 | b = n % BN_BITS2; |
750 | 0 | if (w >= a->top) |
751 | 0 | return 0; |
752 | 0 | if (b == 0) |
753 | 0 | a->top = w; |
754 | 0 | else { |
755 | 0 | a->top = w + 1; |
756 | 0 | a->d[w] &= ~(BN_MASK2 << b); |
757 | 0 | } |
758 | 0 | bn_correct_top(a); |
759 | 0 | return 1; |
760 | 0 | } |
761 | | |
762 | | void BN_set_negative(BIGNUM *a, int b) |
763 | 6.34k | { |
764 | 6.34k | if (b && !BN_is_zero(a)) |
765 | 2.90k | a->neg = 1; |
766 | 3.43k | else |
767 | 3.43k | a->neg = 0; |
768 | 6.34k | } |
769 | | |
770 | | int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) |
771 | 1.11M | { |
772 | 1.11M | int i; |
773 | 1.11M | BN_ULONG aa, bb; |
774 | | |
775 | 1.11M | if (n == 0) |
776 | 0 | return 0; |
777 | | |
778 | 1.11M | aa = a[n - 1]; |
779 | 1.11M | bb = b[n - 1]; |
780 | 1.11M | if (aa != bb) |
781 | 1.03M | return ((aa > bb) ? 1 : -1); |
782 | 504k | for (i = n - 2; i >= 0; i--) { |
783 | 481k | aa = a[i]; |
784 | 481k | bb = b[i]; |
785 | 481k | if (aa != bb) |
786 | 60.4k | return ((aa > bb) ? 1 : -1); |
787 | 481k | } |
788 | 23.1k | return 0; |
789 | 83.5k | } |
790 | | |
791 | | /* |
792 | | * Here follows a specialised variants of bn_cmp_words(). It has the |
793 | | * capability of performing the operation on arrays of different sizes. The |
794 | | * sizes of those arrays is expressed through cl, which is the common length |
795 | | * ( basically, min(len(a),len(b)) ), and dl, which is the delta between the |
796 | | * two lengths, calculated as len(a)-len(b). All lengths are the number of |
797 | | * BN_ULONGs... |
798 | | */ |
799 | | |
800 | | int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) |
801 | 1.15M | { |
802 | 1.15M | int n, i; |
803 | 1.15M | n = cl - 1; |
804 | | |
805 | 1.15M | if (dl < 0) { |
806 | 104k | for (i = dl; i < 0; i++) { |
807 | 100k | if (b[n - i] != 0) |
808 | 39.4k | return -1; /* a < b */ |
809 | 100k | } |
810 | 43.6k | } |
811 | 1.11M | if (dl > 0) { |
812 | 138k | for (i = dl; i > 0; i--) { |
813 | 130k | if (a[n + i] != 0) |
814 | 36.8k | return 1; /* a > b */ |
815 | 130k | } |
816 | 45.1k | } |
817 | 1.08M | return bn_cmp_words(a, b, cl); |
818 | 1.11M | } |
819 | | |
820 | | /*- |
821 | | * Constant-time conditional swap of a and b. |
822 | | * a and b are swapped if condition is not 0. |
823 | | * nwords is the number of words to swap. |
824 | | * Assumes that at least nwords are allocated in both a and b. |
825 | | * Assumes that no more than nwords are used by either a or b. |
826 | | */ |
827 | | void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords) |
828 | 0 | { |
829 | 0 | BN_ULONG t; |
830 | 0 | int i; |
831 | |
|
832 | 0 | if (a == b) |
833 | 0 | return; |
834 | | |
835 | 0 | bn_wcheck_size(a, nwords); |
836 | 0 | bn_wcheck_size(b, nwords); |
837 | |
|
838 | 0 | condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1; |
839 | |
|
840 | 0 | t = (a->top ^ b->top) & condition; |
841 | 0 | a->top ^= t; |
842 | 0 | b->top ^= t; |
843 | |
|
844 | 0 | t = (a->neg ^ b->neg) & condition; |
845 | 0 | a->neg ^= t; |
846 | 0 | b->neg ^= t; |
847 | | |
848 | | /*- |
849 | | * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention |
850 | | * is actually to treat it as it's read-only data, and some (if not most) |
851 | | * of it does reside in read-only segment. In other words observation of |
852 | | * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal |
853 | | * condition. It would either cause SEGV or effectively cause data |
854 | | * corruption. |
855 | | * |
856 | | * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be |
857 | | * preserved. |
858 | | * |
859 | | * BN_FLG_SECURE: must be preserved, because it determines how x->d was |
860 | | * allocated and hence how to free it. |
861 | | * |
862 | | * BN_FLG_CONSTTIME: sufficient to mask and swap |
863 | | * |
864 | | * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on |
865 | | * the data, so the d array may be padded with additional 0 values (i.e. |
866 | | * top could be greater than the minimal value that it could be). We should |
867 | | * be swapping it |
868 | | */ |
869 | |
|
870 | 0 | #define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP) |
871 | |
|
872 | 0 | t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition; |
873 | 0 | a->flags ^= t; |
874 | 0 | b->flags ^= t; |
875 | | |
876 | | /* conditionally swap the data */ |
877 | 0 | for (i = 0; i < nwords; i++) { |
878 | 0 | t = (a->d[i] ^ b->d[i]) & condition; |
879 | 0 | a->d[i] ^= t; |
880 | 0 | b->d[i] ^= t; |
881 | 0 | } |
882 | 0 | } |
883 | | |
884 | | #undef BN_CONSTTIME_SWAP_FLAGS |
885 | | |
886 | | /* Bits of security, see SP800-57 */ |
887 | | |
888 | | int BN_security_bits(int L, int N) |
889 | 0 | { |
890 | 0 | int secbits, bits; |
891 | 0 | if (L >= 15360) |
892 | 0 | secbits = 256; |
893 | 0 | else if (L >= 7680) |
894 | 0 | secbits = 192; |
895 | 0 | else if (L >= 3072) |
896 | 0 | secbits = 128; |
897 | 0 | else if (L >= 2048) |
898 | 0 | secbits = 112; |
899 | 0 | else if (L >= 1024) |
900 | 0 | secbits = 80; |
901 | 0 | else |
902 | 0 | return 0; |
903 | 0 | if (N == -1) |
904 | 0 | return secbits; |
905 | 0 | bits = N / 2; |
906 | 0 | if (bits < 80) |
907 | 0 | return 0; |
908 | 0 | return bits >= secbits ? secbits : bits; |
909 | 0 | } |
910 | | |
911 | | void BN_zero_ex(BIGNUM *a) |
912 | 1.90M | { |
913 | 1.90M | a->neg = 0; |
914 | 1.90M | a->top = 0; |
915 | 1.90M | a->flags &= ~BN_FLG_FIXED_TOP; |
916 | 1.90M | } |
917 | | |
918 | | int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w) |
919 | 4.26k | { |
920 | 4.26k | return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0)); |
921 | 4.26k | } |
922 | | |
923 | | int BN_is_zero(const BIGNUM *a) |
924 | 550k | { |
925 | 550k | return a->top == 0; |
926 | 550k | } |
927 | | |
928 | | int BN_is_one(const BIGNUM *a) |
929 | 2.61k | { |
930 | 2.61k | return BN_abs_is_word(a, 1) && !a->neg; |
931 | 2.61k | } |
932 | | |
933 | | int BN_is_word(const BIGNUM *a, const BN_ULONG w) |
934 | 0 | { |
935 | 0 | return BN_abs_is_word(a, w) && (!w || !a->neg); |
936 | 0 | } |
937 | | |
938 | | int BN_is_odd(const BIGNUM *a) |
939 | 63.3k | { |
940 | 63.3k | return (a->top > 0) && (a->d[0] & 1); |
941 | 63.3k | } |
942 | | |
943 | | int BN_is_negative(const BIGNUM *a) |
944 | 0 | { |
945 | 0 | return (a->neg != 0); |
946 | 0 | } |
947 | | |
948 | | int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont, |
949 | | BN_CTX *ctx) |
950 | 406 | { |
951 | 406 | return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx); |
952 | 406 | } |
953 | | |
954 | | void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags) |
955 | 0 | { |
956 | 0 | dest->d = b->d; |
957 | 0 | dest->top = b->top; |
958 | 0 | dest->dmax = b->dmax; |
959 | 0 | dest->neg = b->neg; |
960 | 0 | dest->flags = ((dest->flags & BN_FLG_MALLOCED) |
961 | 0 | | (b->flags & ~BN_FLG_MALLOCED) |
962 | 0 | | BN_FLG_STATIC_DATA | flags); |
963 | 0 | } |
964 | | |
965 | | BN_GENCB *BN_GENCB_new(void) |
966 | 0 | { |
967 | 0 | BN_GENCB *ret; |
968 | |
|
969 | 0 | if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) { |
970 | 0 | ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE); |
971 | 0 | return NULL; |
972 | 0 | } |
973 | | |
974 | 0 | return ret; |
975 | 0 | } |
976 | | |
977 | | void BN_GENCB_free(BN_GENCB *cb) |
978 | 0 | { |
979 | 0 | if (cb == NULL) |
980 | 0 | return; |
981 | 0 | OPENSSL_free(cb); |
982 | 0 | } |
983 | | |
984 | | void BN_set_flags(BIGNUM *b, int n) |
985 | 0 | { |
986 | 0 | b->flags |= n; |
987 | 0 | } |
988 | | |
989 | | int BN_get_flags(const BIGNUM *b, int n) |
990 | 592k | { |
991 | 592k | return b->flags & n; |
992 | 592k | } |
993 | | |
994 | | /* Populate a BN_GENCB structure with an "old"-style callback */ |
995 | | void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback) (int, int, void *), |
996 | | void *cb_arg) |
997 | 0 | { |
998 | 0 | BN_GENCB *tmp_gencb = gencb; |
999 | 0 | tmp_gencb->ver = 1; |
1000 | 0 | tmp_gencb->arg = cb_arg; |
1001 | 0 | tmp_gencb->cb.cb_1 = callback; |
1002 | 0 | } |
1003 | | |
1004 | | /* Populate a BN_GENCB structure with a "new"-style callback */ |
1005 | | void BN_GENCB_set(BN_GENCB *gencb, int (*callback) (int, int, BN_GENCB *), |
1006 | | void *cb_arg) |
1007 | 0 | { |
1008 | 0 | BN_GENCB *tmp_gencb = gencb; |
1009 | 0 | tmp_gencb->ver = 2; |
1010 | 0 | tmp_gencb->arg = cb_arg; |
1011 | 0 | tmp_gencb->cb.cb_2 = callback; |
1012 | 0 | } |
1013 | | |
1014 | | void *BN_GENCB_get_arg(BN_GENCB *cb) |
1015 | 0 | { |
1016 | 0 | return cb->arg; |
1017 | 0 | } |
1018 | | |
1019 | | BIGNUM *bn_wexpand(BIGNUM *a, int words) |
1020 | 2.28M | { |
1021 | 2.28M | return (words <= a->dmax) ? a : bn_expand2(a, words); |
1022 | 2.28M | } |
1023 | | |
1024 | | void bn_correct_top_consttime(BIGNUM *a) |
1025 | 0 | { |
1026 | 0 | int j, atop; |
1027 | 0 | BN_ULONG limb; |
1028 | 0 | unsigned int mask; |
1029 | |
|
1030 | 0 | for (j = 0, atop = 0; j < a->dmax; j++) { |
1031 | 0 | limb = a->d[j]; |
1032 | 0 | limb |= 0 - limb; |
1033 | 0 | limb >>= BN_BITS2 - 1; |
1034 | 0 | limb = 0 - limb; |
1035 | 0 | mask = (unsigned int)limb; |
1036 | 0 | mask &= constant_time_msb(j - a->top); |
1037 | 0 | atop = constant_time_select_int(mask, j + 1, atop); |
1038 | 0 | } |
1039 | |
|
1040 | 0 | mask = constant_time_eq_int(atop, 0); |
1041 | 0 | a->top = atop; |
1042 | 0 | a->neg = constant_time_select_int(mask, 0, a->neg); |
1043 | 0 | a->flags &= ~BN_FLG_FIXED_TOP; |
1044 | 0 | } |
1045 | | |
1046 | | void bn_correct_top(BIGNUM *a) |
1047 | 902k | { |
1048 | 902k | BN_ULONG *ftl; |
1049 | 902k | int tmp_top = a->top; |
1050 | | |
1051 | 902k | if (tmp_top > 0) { |
1052 | 1.86M | for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) { |
1053 | 1.83M | ftl--; |
1054 | 1.83M | if (*ftl != 0) |
1055 | 843k | break; |
1056 | 1.83M | } |
1057 | 865k | a->top = tmp_top; |
1058 | 865k | } |
1059 | 902k | if (a->top == 0) |
1060 | 59.6k | a->neg = 0; |
1061 | 902k | a->flags &= ~BN_FLG_FIXED_TOP; |
1062 | 902k | bn_pollute(a); |
1063 | 902k | } |