Coverage Report

Created: 2025-03-09 06:52

/src/libressl/crypto/bn/bn_lib.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: bn_lib.c,v 1.93 2024/04/16 13:07:14 jsing Exp $ */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
 * All rights reserved.
4
 *
5
 * This package is an SSL implementation written
6
 * by Eric Young (eay@cryptsoft.com).
7
 * The implementation was written so as to conform with Netscapes SSL.
8
 *
9
 * This library is free for commercial and non-commercial use as long as
10
 * the following conditions are aheared to.  The following conditions
11
 * apply to all code found in this distribution, be it the RC4, RSA,
12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13
 * included with this distribution is covered by the same copyright terms
14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15
 *
16
 * Copyright remains Eric Young's, and as such any Copyright notices in
17
 * the code are not to be removed.
18
 * If this package is used in a product, Eric Young should be given attribution
19
 * as the author of the parts of the library used.
20
 * This can be in the form of a textual message at program startup or
21
 * in documentation (online or textual) provided with the package.
22
 *
23
 * Redistribution and use in source and binary forms, with or without
24
 * modification, are permitted provided that the following conditions
25
 * are met:
26
 * 1. Redistributions of source code must retain the copyright
27
 *    notice, this list of conditions and the following disclaimer.
28
 * 2. Redistributions in binary form must reproduce the above copyright
29
 *    notice, this list of conditions and the following disclaimer in the
30
 *    documentation and/or other materials provided with the distribution.
31
 * 3. All advertising materials mentioning features or use of this software
32
 *    must display the following acknowledgement:
33
 *    "This product includes cryptographic software written by
34
 *     Eric Young (eay@cryptsoft.com)"
35
 *    The word 'cryptographic' can be left out if the rouines from the library
36
 *    being used are not cryptographic related :-).
37
 * 4. If you include any Windows specific code (or a derivative thereof) from
38
 *    the apps directory (application code) you must include an acknowledgement:
39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
 * SUCH DAMAGE.
52
 *
53
 * The licence and distribution terms for any publically available version or
54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55
 * copied and put under another distribution licence
56
 * [including the GNU Public Licence.]
57
 */
58
59
#include <assert.h>
60
#include <limits.h>
61
#include <stdio.h>
62
#include <string.h>
63
64
#include <openssl/opensslconf.h>
65
66
#include <openssl/err.h>
67
68
#include "bn_local.h"
69
#include "bn_internal.h"
70
71
BIGNUM *
72
BN_new(void)
73
1.82M
{
74
1.82M
  BIGNUM *bn;
75
76
1.82M
  if ((bn = calloc(1, sizeof(BIGNUM))) == NULL) {
77
0
    BNerror(ERR_R_MALLOC_FAILURE);
78
0
    return NULL;
79
0
  }
80
1.82M
  bn->flags = BN_FLG_MALLOCED;
81
82
1.82M
  return bn;
83
1.82M
}
84
LCRYPTO_ALIAS(BN_new);
85
86
void
87
BN_init(BIGNUM *a)
88
376k
{
89
376k
  memset(a, 0, sizeof(BIGNUM));
90
376k
}
91
92
void
93
BN_clear(BIGNUM *a)
94
0
{
95
0
  if (a->d != NULL)
96
0
    explicit_bzero(a->d, a->dmax * sizeof(a->d[0]));
97
0
  a->top = 0;
98
0
  a->neg = 0;
99
0
}
100
LCRYPTO_ALIAS(BN_clear);
101
102
void
103
BN_free(BIGNUM *bn)
104
3.17M
{
105
3.17M
  if (bn == NULL)
106
1.16M
    return;
107
108
2.01M
  if (!BN_get_flags(bn, BN_FLG_STATIC_DATA))
109
2.01M
    freezero(bn->d, bn->dmax * sizeof(bn->d[0]));
110
111
2.01M
  if (!BN_get_flags(bn, BN_FLG_MALLOCED)) {
112
190k
    explicit_bzero(bn, sizeof(*bn));
113
190k
    return;
114
190k
  }
115
116
1.82M
  freezero(bn, sizeof(*bn));
117
1.82M
}
118
LCRYPTO_ALIAS(BN_free);
119
120
void
121
BN_clear_free(BIGNUM *bn)
122
0
{
123
0
  BN_free(bn);
124
0
}
125
LCRYPTO_ALIAS(BN_clear_free);
126
127
void
128
BN_set_flags(BIGNUM *b, int n)
129
60.0k
{
130
60.0k
  b->flags |= n;
131
60.0k
}
132
LCRYPTO_ALIAS(BN_set_flags);
133
134
int
135
BN_get_flags(const BIGNUM *b, int n)
136
6.66M
{
137
6.66M
  return b->flags & n;
138
6.66M
}
139
LCRYPTO_ALIAS(BN_get_flags);
140
141
void
142
BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
143
4.32M
{
144
4.32M
  int dest_flags;
145
146
4.32M
  dest_flags = (dest->flags & BN_FLG_MALLOCED) |
147
4.32M
      (b->flags & ~BN_FLG_MALLOCED) | BN_FLG_STATIC_DATA | flags;
148
149
4.32M
  *dest = *b;
150
4.32M
  dest->flags = dest_flags;
151
4.32M
}
152
LCRYPTO_ALIAS(BN_with_flags);
153
154
static const BN_ULONG bn_value_one_data = 1;
155
static const BIGNUM bn_value_one = {
156
  .d = (BN_ULONG *)&bn_value_one_data,
157
  .top = 1,
158
  .dmax = 1,
159
  .neg = 0,
160
  .flags = BN_FLG_STATIC_DATA,
161
};
162
163
const BIGNUM *
164
BN_value_one(void)
165
858k
{
166
858k
  return &bn_value_one;
167
858k
}
168
LCRYPTO_ALIAS(BN_value_one);
169
170
int
171
BN_num_bits_word(BN_ULONG w)
172
128k
{
173
128k
  return BN_BITS2 - bn_clzw(w);
174
128k
}
175
LCRYPTO_ALIAS(BN_num_bits_word);
176
177
int
178
BN_num_bits(const BIGNUM *bn)
179
31.3M
{
180
31.3M
  return bn_bitsize(bn);
181
31.3M
}
182
LCRYPTO_ALIAS(BN_num_bits);
183
184
void
185
bn_correct_top(BIGNUM *a)
186
205M
{
187
348M
  while (a->top > 0 && a->d[a->top - 1] == 0)
188
142M
    a->top--;
189
205M
}
190
191
static int
192
bn_expand_internal(BIGNUM *bn, int words)
193
2.62M
{
194
2.62M
  BN_ULONG *d;
195
196
2.62M
  if (words < 0) {
197
0
    BNerror(BN_R_BIGNUM_TOO_LONG); // XXX
198
0
    return 0;
199
0
  }
200
201
2.62M
  if (words > INT_MAX / (4 * BN_BITS2)) {
202
0
    BNerror(BN_R_BIGNUM_TOO_LONG);
203
0
    return 0;
204
0
  }
205
2.62M
  if (BN_get_flags(bn, BN_FLG_STATIC_DATA)) {
206
0
    BNerror(BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
207
0
    return 0;
208
0
  }
209
210
2.62M
  d = recallocarray(bn->d, bn->dmax, words, sizeof(BN_ULONG));
211
2.62M
  if (d == NULL) {
212
0
    BNerror(ERR_R_MALLOC_FAILURE);
213
0
    return 0;
214
0
  }
215
2.62M
  bn->d = d;
216
2.62M
  bn->dmax = words;
217
218
2.62M
  return 1;
219
2.62M
}
220
221
int
222
bn_expand_bits(BIGNUM *bn, size_t bits)
223
49.4k
{
224
49.4k
  int words;
225
226
49.4k
  if (bits > (INT_MAX - BN_BITS2 + 1))
227
0
    return 0;
228
229
49.4k
  words = (bits + BN_BITS2 - 1) / BN_BITS2;
230
231
49.4k
  return bn_wexpand(bn, words);
232
49.4k
}
233
234
int
235
bn_expand_bytes(BIGNUM *bn, size_t bytes)
236
2.00M
{
237
2.00M
  int words;
238
239
2.00M
  if (bytes > (INT_MAX - BN_BYTES + 1))
240
0
    return 0;
241
242
2.00M
  words = (bytes + BN_BYTES - 1) / BN_BYTES;
243
244
2.00M
  return bn_wexpand(bn, words);
245
2.00M
}
246
247
int
248
bn_wexpand(BIGNUM *bn, int words)
249
255M
{
250
255M
  if (words < 0)
251
0
    return 0;
252
253
255M
  if (words <= bn->dmax)
254
252M
    return 1;
255
256
2.62M
  return bn_expand_internal(bn, words);
257
255M
}
258
259
BIGNUM *
260
BN_dup(const BIGNUM *a)
261
78.0k
{
262
78.0k
  BIGNUM *t;
263
264
78.0k
  if (a == NULL)
265
0
    return NULL;
266
267
78.0k
  t = BN_new();
268
78.0k
  if (t == NULL)
269
0
    return NULL;
270
78.0k
  if (!bn_copy(t, a)) {
271
0
    BN_free(t);
272
0
    return NULL;
273
0
  }
274
78.0k
  return t;
275
78.0k
}
276
LCRYPTO_ALIAS(BN_dup);
277
278
static inline void
279
bn_copy_words(BN_ULONG *ap, const BN_ULONG *bp, int n)
280
5.58M
{
281
98.1M
  while (n > 0) {
282
92.5M
    ap[0] = bp[0];
283
92.5M
    ap++;
284
92.5M
    bp++;
285
92.5M
    n--;
286
92.5M
  }
287
5.58M
}
288
289
BIGNUM *
290
BN_copy(BIGNUM *a, const BIGNUM *b)
291
16.7M
{
292
16.7M
  if (a == b)
293
11.1M
    return (a);
294
295
5.58M
  if (!bn_wexpand(a, b->top))
296
0
    return (NULL);
297
298
5.58M
  bn_copy_words(a->d, b->d, b->top);
299
300
  /* Copy constant time flag from b, but make it sticky on a. */
301
5.58M
  a->flags |= b->flags & BN_FLG_CONSTTIME;
302
303
5.58M
  a->top = b->top;
304
5.58M
  a->neg = b->neg;
305
306
5.58M
  return (a);
307
5.58M
}
308
LCRYPTO_ALIAS(BN_copy);
309
310
int
311
bn_copy(BIGNUM *dst, const BIGNUM *src)
312
16.7M
{
313
16.7M
  return BN_copy(dst, src) != NULL;
314
16.7M
}
315
316
void
317
BN_swap(BIGNUM *a, BIGNUM *b)
318
0
{
319
0
  int flags_old_a, flags_old_b;
320
0
  BN_ULONG *tmp_d;
321
0
  int tmp_top, tmp_dmax, tmp_neg;
322
323
324
0
  flags_old_a = a->flags;
325
0
  flags_old_b = b->flags;
326
327
0
  tmp_d = a->d;
328
0
  tmp_top = a->top;
329
0
  tmp_dmax = a->dmax;
330
0
  tmp_neg = a->neg;
331
332
0
  a->d = b->d;
333
0
  a->top = b->top;
334
0
  a->dmax = b->dmax;
335
0
  a->neg = b->neg;
336
337
0
  b->d = tmp_d;
338
0
  b->top = tmp_top;
339
0
  b->dmax = tmp_dmax;
340
0
  b->neg = tmp_neg;
341
342
0
  a->flags = (flags_old_a & BN_FLG_MALLOCED) |
343
0
      (flags_old_b & BN_FLG_STATIC_DATA);
344
0
  b->flags = (flags_old_b & BN_FLG_MALLOCED) |
345
0
      (flags_old_a & BN_FLG_STATIC_DATA);
346
0
}
347
LCRYPTO_ALIAS(BN_swap);
348
349
BN_ULONG
350
BN_get_word(const BIGNUM *a)
351
2.10k
{
352
2.10k
  if (a->top > 1)
353
8
    return BN_MASK2;
354
2.09k
  else if (a->top == 1)
355
2.04k
    return a->d[0];
356
  /* a->top == 0 */
357
49
  return 0;
358
2.10k
}
359
LCRYPTO_ALIAS(BN_get_word);
360
361
int
362
BN_set_word(BIGNUM *a, BN_ULONG w)
363
569k
{
364
569k
  if (!bn_wexpand(a, 1))
365
0
    return (0);
366
569k
  a->neg = 0;
367
569k
  a->d[0] = w;
368
569k
  a->top = (w ? 1 : 0);
369
569k
  return (1);
370
569k
}
371
LCRYPTO_ALIAS(BN_set_word);
372
373
int
374
BN_ucmp(const BIGNUM *a, const BIGNUM *b)
375
51.3M
{
376
51.3M
  int i;
377
378
51.3M
  if (a->top < b->top)
379
226k
    return -1;
380
51.0M
  if (a->top > b->top)
381
13.4M
    return 1;
382
383
39.8M
  for (i = a->top - 1; i >= 0; i--) {
384
39.6M
    if (a->d[i] != b->d[i])
385
37.5M
      return (a->d[i] > b->d[i] ? 1 : -1);
386
39.6M
  }
387
388
139k
  return 0;
389
37.6M
}
390
LCRYPTO_ALIAS(BN_ucmp);
391
392
int
393
BN_cmp(const BIGNUM *a, const BIGNUM *b)
394
1.67M
{
395
1.67M
  if (a == NULL || b == NULL) {
396
0
    if (a != NULL)
397
0
      return -1;
398
0
    if (b != NULL)
399
0
      return 1;
400
0
    return 0;
401
0
  }
402
403
1.67M
  if (a->neg != b->neg)
404
606
    return b->neg - a->neg;
405
406
1.67M
  if (a->neg)
407
1.08k
    return BN_ucmp(b, a);
408
409
1.67M
  return BN_ucmp(a, b);
410
1.67M
}
411
LCRYPTO_ALIAS(BN_cmp);
412
413
int
414
BN_set_bit(BIGNUM *a, int n)
415
208k
{
416
208k
  int i, j, k;
417
418
208k
  if (n < 0)
419
0
    return 0;
420
421
208k
  i = n / BN_BITS2;
422
208k
  j = n % BN_BITS2;
423
208k
  if (a->top <= i) {
424
207k
    if (!bn_wexpand(a, i + 1))
425
0
      return (0);
426
2.04M
    for (k = a->top; k < i + 1; k++)
427
1.83M
      a->d[k] = 0;
428
207k
    a->top = i + 1;
429
207k
  }
430
431
208k
  a->d[i] |= (((BN_ULONG)1) << j);
432
208k
  return (1);
433
208k
}
434
LCRYPTO_ALIAS(BN_set_bit);
435
436
int
437
BN_clear_bit(BIGNUM *a, int n)
438
673
{
439
673
  int i, j;
440
441
673
  if (n < 0)
442
0
    return 0;
443
444
673
  i = n / BN_BITS2;
445
673
  j = n % BN_BITS2;
446
673
  if (a->top <= i)
447
6
    return (0);
448
449
667
  a->d[i] &= (~(((BN_ULONG)1) << j));
450
667
  bn_correct_top(a);
451
452
667
  BN_set_negative(a, a->neg);
453
454
667
  return (1);
455
673
}
456
LCRYPTO_ALIAS(BN_clear_bit);
457
458
int
459
BN_is_bit_set(const BIGNUM *a, int n)
460
88.2M
{
461
88.2M
  int i, j;
462
463
88.2M
  if (n < 0)
464
120
    return 0;
465
88.2M
  i = n / BN_BITS2;
466
88.2M
  j = n % BN_BITS2;
467
88.2M
  if (a->top <= i)
468
156k
    return 0;
469
88.0M
  return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
470
88.2M
}
471
LCRYPTO_ALIAS(BN_is_bit_set);
472
473
int
474
BN_mask_bits(BIGNUM *a, int n)
475
220k
{
476
220k
  int b, w;
477
478
220k
  if (n < 0)
479
0
    return 0;
480
481
220k
  w = n / BN_BITS2;
482
220k
  b = n % BN_BITS2;
483
220k
  if (w >= a->top)
484
3
    return 0;
485
220k
  if (b == 0)
486
4
    a->top = w;
487
220k
  else {
488
220k
    a->top = w + 1;
489
220k
    a->d[w] &= ~(BN_MASK2 << b);
490
220k
  }
491
220k
  bn_correct_top(a);
492
493
220k
  BN_set_negative(a, a->neg);
494
495
220k
  return (1);
496
220k
}
497
LCRYPTO_ALIAS(BN_mask_bits);
498
499
void
500
BN_set_negative(BIGNUM *bn, int neg)
501
163M
{
502
163M
  bn->neg = ~BN_is_zero(bn) & bn_ct_ne_zero(neg);
503
163M
}
504
LCRYPTO_ALIAS(BN_set_negative);
505
506
/*
507
 * Constant-time conditional swap of a and b.
508
 * a and b are swapped if condition is not 0.
509
 * The code assumes that at most one bit of condition is set.
510
 * nwords is the number of words to swap.
511
 * The code assumes that at least nwords are allocated in both a and b,
512
 * and that no more than nwords are used by either a or b.
513
 * a and b cannot be the same number
514
 */
515
void
516
BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
517
0
{
518
0
  BN_ULONG t;
519
0
  int i;
520
521
0
  assert(a != b);
522
0
  assert((condition & (condition - 1)) == 0);
523
0
  assert(sizeof(BN_ULONG) >= sizeof(int));
524
525
0
  condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1;
526
527
0
  t = (a->top^b->top) & condition;
528
0
  a->top ^= t;
529
0
  b->top ^= t;
530
531
0
#define BN_CONSTTIME_SWAP(ind) \
532
0
  do { \
533
0
    t = (a->d[ind] ^ b->d[ind]) & condition; \
534
0
    a->d[ind] ^= t; \
535
0
    b->d[ind] ^= t; \
536
0
  } while (0)
537
538
539
0
  switch (nwords) {
540
0
  default:
541
0
    for (i = 10; i < nwords; i++)
542
0
      BN_CONSTTIME_SWAP(i);
543
    /* Fallthrough */
544
0
  case 10: BN_CONSTTIME_SWAP(9); /* Fallthrough */
545
0
  case 9: BN_CONSTTIME_SWAP(8); /* Fallthrough */
546
0
  case 8: BN_CONSTTIME_SWAP(7); /* Fallthrough */
547
0
  case 7: BN_CONSTTIME_SWAP(6); /* Fallthrough */
548
0
  case 6: BN_CONSTTIME_SWAP(5); /* Fallthrough */
549
0
  case 5: BN_CONSTTIME_SWAP(4); /* Fallthrough */
550
0
  case 4: BN_CONSTTIME_SWAP(3); /* Fallthrough */
551
0
  case 3: BN_CONSTTIME_SWAP(2); /* Fallthrough */
552
0
  case 2: BN_CONSTTIME_SWAP(1); /* Fallthrough */
553
0
  case 1:
554
0
    BN_CONSTTIME_SWAP(0);
555
0
  }
556
0
#undef BN_CONSTTIME_SWAP
557
0
}
558
LCRYPTO_ALIAS(BN_consttime_swap);
559
560
/*
561
 * Constant-time conditional swap of a and b.
562
 * a and b are swapped if condition is not 0.
563
 * nwords is the number of words to swap.
564
 */
565
int
566
BN_swap_ct(BN_ULONG condition, BIGNUM *a, BIGNUM *b, size_t nwords)
567
6.53M
{
568
6.53M
  BN_ULONG t;
569
6.53M
  int i, words;
570
571
6.53M
  if (a == b)
572
0
    return 1;
573
6.53M
  if (nwords > INT_MAX)
574
0
    return 0;
575
6.53M
  words = (int)nwords;
576
6.53M
  if (!bn_wexpand(a, words) || !bn_wexpand(b, words))
577
0
    return 0;
578
6.53M
  if (a->top > words || b->top > words) {
579
0
    BNerror(BN_R_INVALID_LENGTH);
580
0
    return 0;
581
0
  }
582
583
  /* Set condition to 0 (if it was zero) or all 1s otherwise. */
584
6.53M
  condition = ((~condition & (condition - 1)) >> (BN_BITS2 - 1)) - 1;
585
586
  /* swap top field */
587
6.53M
  t = (a->top ^ b->top) & condition;
588
6.53M
  a->top ^= t;
589
6.53M
  b->top ^= t;
590
591
  /* swap neg field */
592
6.53M
  t = (a->neg ^ b->neg) & condition;
593
6.53M
  a->neg ^= t;
594
6.53M
  b->neg ^= t;
595
596
  /* swap BN_FLG_CONSTTIME from flag field */
597
6.53M
  t = ((a->flags ^ b->flags) & BN_FLG_CONSTTIME) & condition;
598
6.53M
  a->flags ^= t;
599
6.53M
  b->flags ^= t;
600
601
  /* swap the data */
602
42.6M
  for (i = 0; i < words; i++) {
603
36.1M
    t = (a->d[i] ^ b->d[i]) & condition;
604
36.1M
    a->d[i] ^= t;
605
36.1M
    b->d[i] ^= t;
606
36.1M
  }
607
608
6.53M
  return 1;
609
6.53M
}
610
611
void
612
BN_zero(BIGNUM *a)
613
138M
{
614
138M
  a->neg = 0;
615
138M
  a->top = 0;
616
138M
}
617
LCRYPTO_ALIAS(BN_zero);
618
619
int
620
BN_one(BIGNUM *a)
621
230k
{
622
230k
  return BN_set_word(a, 1);
623
230k
}
624
LCRYPTO_ALIAS(BN_one);
625
626
int
627
BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
628
2.59M
{
629
2.59M
  return (a->top == 1 && a->d[0] == w) || (w == 0 && a->top == 0);
630
2.59M
}
631
LCRYPTO_ALIAS(BN_abs_is_word);
632
633
int
634
BN_is_zero(const BIGNUM *bn)
635
205M
{
636
205M
  BN_ULONG bits = 0;
637
205M
  int i;
638
639
2.21G
  for (i = 0; i < bn->top; i++)
640
2.00G
    bits |= bn->d[i];
641
642
205M
  return bits == 0;
643
205M
}
644
LCRYPTO_ALIAS(BN_is_zero);
645
646
int
647
BN_is_one(const BIGNUM *a)
648
1.71M
{
649
1.71M
  return BN_abs_is_word(a, 1) && !a->neg;
650
1.71M
}
651
LCRYPTO_ALIAS(BN_is_one);
652
653
int
654
BN_is_word(const BIGNUM *a, const BN_ULONG w)
655
870k
{
656
870k
  return BN_abs_is_word(a, w) && (w == 0 || !a->neg);
657
870k
}
658
LCRYPTO_ALIAS(BN_is_word);
659
660
int
661
BN_is_odd(const BIGNUM *a)
662
7.55M
{
663
7.55M
  return a->top > 0 && (a->d[0] & 1);
664
7.55M
}
665
LCRYPTO_ALIAS(BN_is_odd);
666
667
int
668
BN_is_negative(const BIGNUM *a)
669
5.00M
{
670
5.00M
  return a->neg != 0;
671
5.00M
}
672
LCRYPTO_ALIAS(BN_is_negative);
673
674
/*
675
 * Bits of security, see SP800-57, section 5.6.11, table 2.
676
 */
677
int
678
BN_security_bits(int L, int N)
679
6.90k
{
680
6.90k
  int secbits, bits;
681
682
6.90k
  if (L >= 15360)
683
38
    secbits = 256;
684
6.86k
  else if (L >= 7680)
685
76
    secbits = 192;
686
6.79k
  else if (L >= 3072)
687
31
    secbits = 128;
688
6.76k
  else if (L >= 2048)
689
5.60k
    secbits = 112;
690
1.15k
  else if (L >= 1024)
691
1.13k
    secbits = 80;
692
18
  else
693
18
    return 0;
694
695
6.88k
  if (N == -1)
696
6.88k
    return secbits;
697
698
4
  bits = N / 2;
699
4
  if (bits < 80)
700
0
    return 0;
701
702
4
  return bits >= secbits ? secbits : bits;
703
4
}
704
LCRYPTO_ALIAS(BN_security_bits);
705
706
BN_GENCB *
707
BN_GENCB_new(void)
708
0
{
709
0
  BN_GENCB *cb;
710
711
0
  if ((cb = calloc(1, sizeof(*cb))) == NULL)
712
0
    return NULL;
713
714
0
  return cb;
715
0
}
716
LCRYPTO_ALIAS(BN_GENCB_new);
717
718
void
719
BN_GENCB_free(BN_GENCB *cb)
720
0
{
721
0
  if (cb == NULL)
722
0
    return;
723
0
  free(cb);
724
0
}
725
LCRYPTO_ALIAS(BN_GENCB_free);
726
727
/* Populate a BN_GENCB structure with an "old"-style callback */
728
void
729
BN_GENCB_set_old(BN_GENCB *gencb, void (*cb)(int, int, void *), void *cb_arg)
730
0
{
731
0
  gencb->ver = 1;
732
0
  gencb->cb.cb_1 = cb;
733
0
  gencb->arg = cb_arg;
734
0
}
735
LCRYPTO_ALIAS(BN_GENCB_set_old);
736
737
/* Populate a BN_GENCB structure with a "new"-style callback */
738
void
739
BN_GENCB_set(BN_GENCB *gencb, int (*cb)(int, int, BN_GENCB *), void *cb_arg)
740
0
{
741
0
  gencb->ver = 2;
742
0
  gencb->cb.cb_2 = cb;
743
0
  gencb->arg = cb_arg;
744
0
}
745
LCRYPTO_ALIAS(BN_GENCB_set);
746
747
void *
748
BN_GENCB_get_arg(BN_GENCB *cb)
749
0
{
750
0
  return cb->arg;
751
0
}
752
LCRYPTO_ALIAS(BN_GENCB_get_arg);