From 36ba240090a6df24c201bdfd6f435983e6ced3a4 Mon Sep 17 00:00:00 2001 From: jsing Date: Wed, 21 Jun 2023 07:15:38 +0000 Subject: [PATCH] Add a BN_num_bits() with zero padded input. Currently BN_hex2bn() removes the leading zeros, however this will not be the case in the future. --- regress/lib/libcrypto/bn/bn_unit.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/regress/lib/libcrypto/bn/bn_unit.c b/regress/lib/libcrypto/bn/bn_unit.c index bc49192b8c2..3a88bfca6e6 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.6 2023/06/20 06:46:07 tb Exp $ */ +/* $OpenBSD: bn_unit.c,v 1.7 2023/06/21 07:15:38 jsing Exp $ */ /* * Copyright (c) 2022 Theo Buehler @@ -80,7 +80,7 @@ test_bn_num_bits(void) errx(1, "BN_new"); if ((num_bits = BN_num_bits(bn)) != 0) { - warnx("BN_num_bits(0): want 0, got %d", num_bits); + warnx("BN_num_bits(0): got %d, want 0", num_bits); failed |= 1; } @@ -89,14 +89,23 @@ test_bn_num_bits(void) 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); + warnx("BN_num_bits(1 << %d): got %d, want %d", + i, num_bits, i + 1); failed |= 1; } if (!BN_lshift1(bn, bn)) errx(1, "BN_lshift1"); } + if (BN_hex2bn(&bn, "0000000000000000010000000000000000") != 34) + errx(1, "BN_hex2bn"); + + if ((num_bits = BN_num_bits(bn)) != 65) { + warnx("BN_num_bits(1 << 64) padded: got %d, want %d", + num_bits, 65); + failed |= 1; + } + BN_free(bn); return failed; -- 2.20.1