summaryrefslogtreecommitdiff
path: root/regress
diff options
context:
space:
mode:
authorJoel Sing <jsing@cvs.openbsd.org>2023-05-27 15:50:57 +0000
committerJoel Sing <jsing@cvs.openbsd.org>2023-05-27 15:50:57 +0000
commit82b28af923d82d9b73c507777257e13460c32a02 (patch)
tree3ebfe10496937dc9ded9e29745b09320d1a4a8b5 /regress
parent42018c73df75ecaad9354a29882f0412e37abb22 (diff)
Add coverage for calling BN_{dec,hex}2bn() with NULL inputs.
Diffstat (limited to 'regress')
-rw-r--r--regress/lib/libcrypto/bn/bn_convert.c46
1 files changed, 37 insertions, 9 deletions
diff --git a/regress/lib/libcrypto/bn/bn_convert.c b/regress/lib/libcrypto/bn/bn_convert.c
index ea4cbda79db..147bb6839a5 100644
--- a/regress/lib/libcrypto/bn/bn_convert.c
+++ b/regress/lib/libcrypto/bn/bn_convert.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bn_convert.c,v 1.1 2023/04/22 14:03:03 jsing Exp $ */
+/* $OpenBSD: bn_convert.c,v 1.2 2023/05/27 15:50:56 jsing Exp $ */
/*
* Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
*
@@ -55,20 +55,20 @@ check_bin_output(size_t test_no, const char *label, const uint8_t *bin,
out_len = BN_num_bytes(bn);
if (out_len != (int)bin_len) {
- fprintf(stderr, "FAIL: Test %zu - BN_num_bytes() = %d, "
- "want %zu\n", test_no, out_len, bin_len);
+ fprintf(stderr, "FAIL: Test %zu %s - BN_num_bytes() = %d, "
+ "want %zu\n", test_no, label, out_len, bin_len);
goto failure;
}
if ((out = malloc(out_len)) == NULL)
err(1, "malloc");
if ((ret = BN_bn2bin(bn, out)) != out_len) {
- fprintf(stderr, "FAIL: BN_bn2bin() returned %d, "
- "want %d\n", ret, out_len);
+ fprintf(stderr, "FAIL: Test %zu %s - BN_bn2bin() returned %d, "
+ "want %d\n", test_no, label, ret, out_len);
goto failure;
}
if (memcmp(out, bin, bin_len) != 0) {
- fprintf(stderr, "FAIL: Test %zu - output from "
- "BN_bn2bin() differs\n", test_no);
+ fprintf(stderr, "FAIL: Test %zu %s - output from "
+ "BN_bn2bin() differs\n", test_no, label);
fprintf(stderr, "Got:\n");
hexdump(out, out_len);
fprintf(stderr, "Want:\n");
@@ -437,7 +437,7 @@ test_bn_dec2bn(void)
int ret;
int failed = 1;
- /* An empty string fails to parse. */
+ /* An empty string fails to parse, as does NULL. */
if (BN_dec2bn(&bn, "") != 0) {
fprintf(stderr, "FAIL: BN_dec2bn(_, \"\") succeeded\n");
goto failure;
@@ -446,6 +446,14 @@ test_bn_dec2bn(void)
fprintf(stderr, "FAIL: BN_dec2bn(_, \"\") succeeded\n");
goto failure;
}
+ if (BN_dec2bn(&bn, NULL) != 0) {
+ fprintf(stderr, "FAIL: BN_dec2bn(_, NULL) succeeded\n");
+ goto failure;
+ }
+ if (bn != NULL) {
+ fprintf(stderr, "FAIL: BN_dec2bn(_, NULL) succeeded\n");
+ goto failure;
+ }
/* A minus sign parses as 0. */
if (BN_dec2bn(&bn, "-") != 1) {
@@ -492,6 +500,12 @@ test_bn_dec2bn(void)
goto failure;
}
+ /* And we can call BN_dec2bn() without actually converting to a BIGNUM. */
+ if ((ret = BN_dec2bn(NULL, "0123456789abcdef")) != 10) {
+ fprintf(stderr, "FAIL: BN_dec2bn() returned %d, want 10\n", ret);
+ goto failure;
+ }
+
failed = 0;
failure:
@@ -508,7 +522,7 @@ test_bn_hex2bn(void)
int ret;
int failed = 1;
- /* An empty string fails to parse. */
+ /* An empty string fails to parse, as does NULL. */
if (BN_hex2bn(&bn, "") != 0) {
fprintf(stderr, "FAIL: BN_hex2bn(_, \"\") succeeded\n");
goto failure;
@@ -517,6 +531,14 @@ test_bn_hex2bn(void)
fprintf(stderr, "FAIL: BN_hex2bn(_, \"\") succeeded\n");
goto failure;
}
+ if (BN_hex2bn(&bn, NULL) != 0) {
+ fprintf(stderr, "FAIL: BN_hex2bn(_, NULL) succeeded\n");
+ goto failure;
+ }
+ if (bn != NULL) {
+ fprintf(stderr, "FAIL: BN_hex2bn(_, NULL) succeeded\n");
+ goto failure;
+ }
/* A minus sign parses as 0. */
if (BN_hex2bn(&bn, "-") != 1) {
@@ -568,6 +590,12 @@ test_bn_hex2bn(void)
goto failure;
}
+ /* And we can call BN_hex2bn() without actually converting to a BIGNUM. */
+ if ((ret = BN_hex2bn(NULL, "9abcdefz")) != 7) {
+ fprintf(stderr, "FAIL: BN_hex2bn() returned %d, want 7\n", ret);
+ goto failure;
+ }
+
failed = 0;
failure: