diff options
| author | jsing <> | 2023-05-27 15:50:56 +0000 | 
|---|---|---|
| committer | jsing <> | 2023-05-27 15:50:56 +0000 | 
| commit | 9c3b105719935c142e3485188462567d37a1d3b3 (patch) | |
| tree | c8274d162d1c732a6be9c049475c56e5696f9923 /src/regress/lib/libcrypto | |
| parent | 2f2250317843f203cc5e5a81d870b029d8c8aea9 (diff) | |
| download | openbsd-9c3b105719935c142e3485188462567d37a1d3b3.tar.gz openbsd-9c3b105719935c142e3485188462567d37a1d3b3.tar.bz2 openbsd-9c3b105719935c142e3485188462567d37a1d3b3.zip  | |
Add coverage for calling BN_{dec,hex}2bn() with NULL inputs.
Diffstat (limited to 'src/regress/lib/libcrypto')
| -rw-r--r-- | src/regress/lib/libcrypto/bn/bn_convert.c | 46 | 
1 files changed, 37 insertions, 9 deletions
diff --git a/src/regress/lib/libcrypto/bn/bn_convert.c b/src/regress/lib/libcrypto/bn/bn_convert.c index ea4cbda79d..147bb6839a 100644 --- a/src/regress/lib/libcrypto/bn/bn_convert.c +++ b/src/regress/lib/libcrypto/bn/bn_convert.c  | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: bn_convert.c,v 1.1 2023/04/22 14:03:03 jsing Exp $ */ | 1 | /* $OpenBSD: bn_convert.c,v 1.2 2023/05/27 15:50:56 jsing Exp $ */ | 
| 2 | /* | 2 | /* | 
| 3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> | 
| 4 | * | 4 | * | 
| @@ -55,20 +55,20 @@ check_bin_output(size_t test_no, const char *label, const uint8_t *bin, | |||
| 55 | 55 | ||
| 56 | out_len = BN_num_bytes(bn); | 56 | out_len = BN_num_bytes(bn); | 
| 57 | if (out_len != (int)bin_len) { | 57 | if (out_len != (int)bin_len) { | 
| 58 | fprintf(stderr, "FAIL: Test %zu - BN_num_bytes() = %d, " | 58 | fprintf(stderr, "FAIL: Test %zu %s - BN_num_bytes() = %d, " | 
| 59 | "want %zu\n", test_no, out_len, bin_len); | 59 | "want %zu\n", test_no, label, out_len, bin_len); | 
| 60 | goto failure; | 60 | goto failure; | 
| 61 | } | 61 | } | 
| 62 | if ((out = malloc(out_len)) == NULL) | 62 | if ((out = malloc(out_len)) == NULL) | 
| 63 | err(1, "malloc"); | 63 | err(1, "malloc"); | 
| 64 | if ((ret = BN_bn2bin(bn, out)) != out_len) { | 64 | if ((ret = BN_bn2bin(bn, out)) != out_len) { | 
| 65 | fprintf(stderr, "FAIL: BN_bn2bin() returned %d, " | 65 | fprintf(stderr, "FAIL: Test %zu %s - BN_bn2bin() returned %d, " | 
| 66 | "want %d\n", ret, out_len); | 66 | "want %d\n", test_no, label, ret, out_len); | 
| 67 | goto failure; | 67 | goto failure; | 
| 68 | } | 68 | } | 
| 69 | if (memcmp(out, bin, bin_len) != 0) { | 69 | if (memcmp(out, bin, bin_len) != 0) { | 
| 70 | fprintf(stderr, "FAIL: Test %zu - output from " | 70 | fprintf(stderr, "FAIL: Test %zu %s - output from " | 
| 71 | "BN_bn2bin() differs\n", test_no); | 71 | "BN_bn2bin() differs\n", test_no, label); | 
| 72 | fprintf(stderr, "Got:\n"); | 72 | fprintf(stderr, "Got:\n"); | 
| 73 | hexdump(out, out_len); | 73 | hexdump(out, out_len); | 
| 74 | fprintf(stderr, "Want:\n"); | 74 | fprintf(stderr, "Want:\n"); | 
| @@ -437,7 +437,7 @@ test_bn_dec2bn(void) | |||
| 437 | int ret; | 437 | int ret; | 
| 438 | int failed = 1; | 438 | int failed = 1; | 
| 439 | 439 | ||
| 440 | /* An empty string fails to parse. */ | 440 | /* An empty string fails to parse, as does NULL. */ | 
| 441 | if (BN_dec2bn(&bn, "") != 0) { | 441 | if (BN_dec2bn(&bn, "") != 0) { | 
| 442 | fprintf(stderr, "FAIL: BN_dec2bn(_, \"\") succeeded\n"); | 442 | fprintf(stderr, "FAIL: BN_dec2bn(_, \"\") succeeded\n"); | 
| 443 | goto failure; | 443 | goto failure; | 
| @@ -446,6 +446,14 @@ test_bn_dec2bn(void) | |||
| 446 | fprintf(stderr, "FAIL: BN_dec2bn(_, \"\") succeeded\n"); | 446 | fprintf(stderr, "FAIL: BN_dec2bn(_, \"\") succeeded\n"); | 
| 447 | goto failure; | 447 | goto failure; | 
| 448 | } | 448 | } | 
| 449 | if (BN_dec2bn(&bn, NULL) != 0) { | ||
| 450 | fprintf(stderr, "FAIL: BN_dec2bn(_, NULL) succeeded\n"); | ||
| 451 | goto failure; | ||
| 452 | } | ||
| 453 | if (bn != NULL) { | ||
| 454 | fprintf(stderr, "FAIL: BN_dec2bn(_, NULL) succeeded\n"); | ||
| 455 | goto failure; | ||
| 456 | } | ||
| 449 | 457 | ||
| 450 | /* A minus sign parses as 0. */ | 458 | /* A minus sign parses as 0. */ | 
| 451 | if (BN_dec2bn(&bn, "-") != 1) { | 459 | if (BN_dec2bn(&bn, "-") != 1) { | 
| @@ -492,6 +500,12 @@ test_bn_dec2bn(void) | |||
| 492 | goto failure; | 500 | goto failure; | 
| 493 | } | 501 | } | 
| 494 | 502 | ||
| 503 | /* And we can call BN_dec2bn() without actually converting to a BIGNUM. */ | ||
| 504 | if ((ret = BN_dec2bn(NULL, "0123456789abcdef")) != 10) { | ||
| 505 | fprintf(stderr, "FAIL: BN_dec2bn() returned %d, want 10\n", ret); | ||
| 506 | goto failure; | ||
| 507 | } | ||
| 508 | |||
| 495 | failed = 0; | 509 | failed = 0; | 
| 496 | 510 | ||
| 497 | failure: | 511 | failure: | 
| @@ -508,7 +522,7 @@ test_bn_hex2bn(void) | |||
| 508 | int ret; | 522 | int ret; | 
| 509 | int failed = 1; | 523 | int failed = 1; | 
| 510 | 524 | ||
| 511 | /* An empty string fails to parse. */ | 525 | /* An empty string fails to parse, as does NULL. */ | 
| 512 | if (BN_hex2bn(&bn, "") != 0) { | 526 | if (BN_hex2bn(&bn, "") != 0) { | 
| 513 | fprintf(stderr, "FAIL: BN_hex2bn(_, \"\") succeeded\n"); | 527 | fprintf(stderr, "FAIL: BN_hex2bn(_, \"\") succeeded\n"); | 
| 514 | goto failure; | 528 | goto failure; | 
| @@ -517,6 +531,14 @@ test_bn_hex2bn(void) | |||
| 517 | fprintf(stderr, "FAIL: BN_hex2bn(_, \"\") succeeded\n"); | 531 | fprintf(stderr, "FAIL: BN_hex2bn(_, \"\") succeeded\n"); | 
| 518 | goto failure; | 532 | goto failure; | 
| 519 | } | 533 | } | 
| 534 | if (BN_hex2bn(&bn, NULL) != 0) { | ||
| 535 | fprintf(stderr, "FAIL: BN_hex2bn(_, NULL) succeeded\n"); | ||
| 536 | goto failure; | ||
| 537 | } | ||
| 538 | if (bn != NULL) { | ||
| 539 | fprintf(stderr, "FAIL: BN_hex2bn(_, NULL) succeeded\n"); | ||
| 540 | goto failure; | ||
| 541 | } | ||
| 520 | 542 | ||
| 521 | /* A minus sign parses as 0. */ | 543 | /* A minus sign parses as 0. */ | 
| 522 | if (BN_hex2bn(&bn, "-") != 1) { | 544 | if (BN_hex2bn(&bn, "-") != 1) { | 
| @@ -568,6 +590,12 @@ test_bn_hex2bn(void) | |||
| 568 | goto failure; | 590 | goto failure; | 
| 569 | } | 591 | } | 
| 570 | 592 | ||
| 593 | /* And we can call BN_hex2bn() without actually converting to a BIGNUM. */ | ||
| 594 | if ((ret = BN_hex2bn(NULL, "9abcdefz")) != 7) { | ||
| 595 | fprintf(stderr, "FAIL: BN_hex2bn() returned %d, want 7\n", ret); | ||
| 596 | goto failure; | ||
| 597 | } | ||
| 598 | |||
| 571 | failed = 0; | 599 | failed = 0; | 
| 572 | 600 | ||
| 573 | failure: | 601 | failure: | 
