summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib/libcrypto/bn/bn.h18
-rw-r--r--src/lib/libcrypto/bn/bn_lib.c40
2 files changed, 54 insertions, 4 deletions
diff --git a/src/lib/libcrypto/bn/bn.h b/src/lib/libcrypto/bn/bn.h
index 20212bf171..e9837cbbd6 100644
--- a/src/lib/libcrypto/bn/bn.h
+++ b/src/lib/libcrypto/bn/bn.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn.h,v 1.46 2021/12/04 15:53:01 tb Exp $ */ 1/* $OpenBSD: bn.h,v 1.47 2021/12/04 15:59:52 tb Exp $ */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -409,10 +409,21 @@ void *BN_GENCB_get_arg(BN_GENCB *cb);
409 (b) >= 308 ? 8 : \ 409 (b) >= 308 ? 8 : \
410 (b) >= 55 ? 27 : \ 410 (b) >= 55 ? 27 : \
411 /* b >= 6 */ 34) 411 /* b >= 6 */ 34)
412 412
413#define BN_num_bytes(a) ((BN_num_bits(a)+7)/8) 413#define BN_num_bytes(a) ((BN_num_bits(a)+7)/8)
414 414
415/* Note that BN_abs_is_word didn't work reliably for w == 0 until 0.9.8 */ 415#if defined(LIBRESSL_OPAQUE_BN) || defined(LIBRESSL_CRYPTO_INTERNAL)
416int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w);
417int BN_is_zero(const BIGNUM *a);
418int BN_is_one(const BIGNUM *a);
419int BN_is_word(const BIGNUM *a, const BN_ULONG w);
420int BN_is_odd(const BIGNUM *a);
421
422#define BN_one(a) BN_set_word((a), 1)
423
424void BN_zero_ex(BIGNUM *a);
425
426#else
416#define BN_abs_is_word(a,w) ((((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) || \ 427#define BN_abs_is_word(a,w) ((((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) || \
417 (((w) == 0) && ((a)->top == 0))) 428 (((w) == 0) && ((a)->top == 0)))
418#define BN_is_zero(a) ((a)->top == 0) 429#define BN_is_zero(a) ((a)->top == 0)
@@ -427,6 +438,7 @@ void *BN_GENCB_get_arg(BN_GENCB *cb);
427 _tmp_bn->top = 0; \ 438 _tmp_bn->top = 0; \
428 _tmp_bn->neg = 0; \ 439 _tmp_bn->neg = 0; \
429 } while(0) 440 } while(0)
441#endif /* LIBRESSL_OPAQUE_BN */
430 442
431#ifdef OPENSSL_NO_DEPRECATED 443#ifdef OPENSSL_NO_DEPRECATED
432#define BN_zero(a) BN_zero_ex(a) 444#define BN_zero(a) BN_zero_ex(a)
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c
index e4085c9d67..77ee3b1fdc 100644
--- a/src/lib/libcrypto/bn/bn_lib.c
+++ b/src/lib/libcrypto/bn/bn_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_lib.c,v 1.50 2021/12/04 15:53:01 tb Exp $ */ 1/* $OpenBSD: bn_lib.c,v 1.51 2021/12/04 15:59:52 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -1061,6 +1061,44 @@ BN_swap_ct(BN_ULONG condition, BIGNUM *a, BIGNUM *b, size_t nwords)
1061 return 1; 1061 return 1;
1062} 1062}
1063 1063
1064void
1065BN_zero_ex(BIGNUM *a)
1066{
1067 a->neg = 0;
1068 a->top = 0;
1069 /* XXX: a->flags &= ~BN_FIXED_TOP */
1070}
1071
1072int
1073BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
1074{
1075 return (a->top == 1 && a->d[0] == w) || (w == 0 && a->top == 0);
1076}
1077
1078int
1079BN_is_zero(const BIGNUM *a)
1080{
1081 return a->top == 0;
1082}
1083
1084int
1085BN_is_one(const BIGNUM *a)
1086{
1087 return BN_abs_is_word(a, 1) && !a->neg;
1088}
1089
1090int
1091BN_is_word(const BIGNUM *a, const BN_ULONG w)
1092{
1093 return BN_abs_is_word(a, w) && (w == 0 || !a->neg);
1094}
1095
1096int
1097BN_is_odd(const BIGNUM *a)
1098{
1099 return a->top > 0 && (a->d[0] & 1);
1100}
1101
1064BN_GENCB * 1102BN_GENCB *
1065BN_GENCB_new(void) 1103BN_GENCB_new(void)
1066{ 1104{