From 0d31acbab99bff6bc512b3f4883f5c0e0cbf0861 Mon Sep 17 00:00:00 2001 From: jsing Date: Tue, 20 Jun 2023 06:36:09 +0000 Subject: [PATCH] Add regress coverage for BN_num_bits() --- regress/lib/libcrypto/bn/bn_unit.c | 36 +++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) 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 @@ -69,6 +69,39 @@ test_bn_print_null_derefs(void) return failed; } +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) { @@ -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(); -- 2.20.1