summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Sing <jsing@cvs.openbsd.org>2023-06-20 06:36:10 +0000
committerJoel Sing <jsing@cvs.openbsd.org>2023-06-20 06:36:10 +0000
commit8b2c18135bd59c7fd70fb586135568db1b169d72 (patch)
tree9ab5abdc8d00b648a54e2815490a86485eb0b505
parentdbda49da6a11c9df34465c4a9abfc2a9610cb640 (diff)
Add regress coverage for BN_num_bits()
-rw-r--r--regress/lib/libcrypto/bn/bn_unit.c36
1 files changed, 35 insertions, 1 deletions
diff --git a/regress/lib/libcrypto/bn/bn_unit.c b/regress/lib/libcrypto/bn/bn_unit.c
index 95764dfce16..24c7569ff97 100644
--- a/regress/lib/libcrypto/bn/bn_unit.c
+++ b/regress/lib/libcrypto/bn/bn_unit.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bn_unit.c,v 1.4 2023/03/31 19:40:08 tb Exp $ */
+/* $OpenBSD: bn_unit.c,v 1.5 2023/06/20 06:36:09 jsing Exp $ */
/*
* Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
@@ -70,6 +70,39 @@ test_bn_print_null_derefs(void)
}
static int
+test_bn_num_bits(void)
+{
+ BIGNUM *bn;
+ int i, num_bits;
+ int failed = 0;
+
+ if ((bn = BN_new()) == NULL)
+ errx(1, "BN_new");
+
+ if ((num_bits = BN_num_bits(bn)) != 0) {
+ warnx("BN_num_bits_word(0): want 0, got %d", num_bits);
+ failed |= 1;
+ }
+
+ if (!BN_set_word(bn, 1))
+ errx(1, "BN_set_word");
+
+ for (i = 0; i <= 5 * BN_BITS2; i++) {
+ if ((num_bits = BN_num_bits(bn)) != i + 1) {
+ warnx("BN_num_bits(1 << %d): want %d, got %d",
+ i, i + 1, num_bits);
+ failed |= 1;
+ }
+ if (!BN_lshift1(bn, bn))
+ errx(1, "BN_lshift1");
+ }
+
+ BN_free(bn);
+
+ return failed;
+}
+
+static int
test_bn_num_bits_word(void)
{
BN_ULONG w = 1;
@@ -255,6 +288,7 @@ main(void)
int failed = 0;
failed |= test_bn_print_null_derefs();
+ failed |= test_bn_num_bits();
failed |= test_bn_num_bits_word();
failed |= test_bn_copy_copies_flags();
failed |= test_bn_copy_consttime_is_sticky();