From: jsing Date: Tue, 9 Apr 2024 14:56:21 +0000 (+0000) Subject: Add regress coverage for BN_bn2mpi()/BN_mpi2bn(). X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=712993742b256825414a59117e28684d97573354;p=openbsd Add regress coverage for BN_bn2mpi()/BN_mpi2bn(). --- diff --git a/regress/lib/libcrypto/bn/bn_convert.c b/regress/lib/libcrypto/bn/bn_convert.c index 69f7da43b1c..df69b78050a 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.3 2023/06/23 10:50:47 tb Exp $ */ +/* $OpenBSD: bn_convert.c,v 1.4 2024/04/09 14:56:21 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -26,7 +26,6 @@ * - BN_bn2binpad() * - BN_bn2lebinpad() * - BN_lebin2bn() - * - BN_bn2mpi()/BN_mpi2bn() * - BN_print()/BN_print_fp() * * - Invalid inputs to {asc,dec,hex,mpi}2bn @@ -238,6 +237,8 @@ struct bn_convert_test { int neg; const char *dec; const char *hex; + const uint8_t mpi[64]; + int mpi_len; }; static const struct bn_convert_test bn_convert_tests[] = { @@ -247,6 +248,8 @@ static const struct bn_convert_test bn_convert_tests[] = { .neg = 0, .dec = "0", .hex = "0", + .mpi = { 0x00, 0x00, 0x00, 0x00, }, + .mpi_len = 4, }, { .bin = { 0x1, }, @@ -254,6 +257,17 @@ static const struct bn_convert_test bn_convert_tests[] = { .neg = 0, .dec = "1", .hex = "01", + .mpi = { 0x00, 0x00, 0x00, 0x01, 0x01, }, + .mpi_len = 5, + }, + { + .bin = { 0x1, }, + .bin_len = 1, + .neg = 1, + .dec = "-1", + .hex = "-01", + .mpi = { 0x00, 0x00, 0x00, 0x01, 0x81, }, + .mpi_len = 5, }, { .bin = { 0x7f, 0xff, 0xff, }, @@ -261,6 +275,8 @@ static const struct bn_convert_test bn_convert_tests[] = { .neg = 0, .dec = "8388607", .hex = "7FFFFF", + .mpi = { 0x00, 0x00, 0x00, 0x03, 0x7f, 0xff, 0xff }, + .mpi_len = 7, }, { .bin = { 0x7f, 0xff, 0xff, }, @@ -268,6 +284,8 @@ static const struct bn_convert_test bn_convert_tests[] = { .neg = 1, .dec = "-8388607", .hex = "-7FFFFF", + .mpi = { 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff }, + .mpi_len = 7, }, { .bin = { 0xff, 0xff, 0xff, 0xff, }, @@ -275,6 +293,11 @@ static const struct bn_convert_test bn_convert_tests[] = { .neg = 0, .dec = "4294967295", .hex = "FFFFFFFF", + .mpi = { + 0x00, 0x00, 0x00, 0x05, 0x00, 0xff, 0xff, 0xff, + 0xff, + }, + .mpi_len = 9, }, { .bin = { 0xff, 0xff, 0xff, 0xff, }, @@ -282,6 +305,11 @@ static const struct bn_convert_test bn_convert_tests[] = { .neg = 1, .dec = "-4294967295", .hex = "-FFFFFFFF", + .mpi = { + 0x00, 0x00, 0x00, 0x05, 0x80, 0xff, 0xff, 0xff, + 0xff, + }, + .mpi_len = 9, }, { .bin = { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, }, @@ -289,6 +317,11 @@ static const struct bn_convert_test bn_convert_tests[] = { .neg = 0, .dec = "18446744069414584320", .hex = "FFFFFFFF00000000", + .mpi = { + 0x00, 0x00, 0x00, 0x09, 0x00, 0xff, 0xff, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, + }, + .mpi_len = 13, }, { .bin = { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, }, @@ -296,6 +329,11 @@ static const struct bn_convert_test bn_convert_tests[] = { .neg = 1, .dec = "-18446744069414584320", .hex = "-FFFFFFFF00000000", + .mpi = { + 0x00, 0x00, 0x00, 0x09, 0x80, 0xff, 0xff, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x00, + }, + .mpi_len = 13, }, { .bin = { 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, }, @@ -303,6 +341,11 @@ static const struct bn_convert_test bn_convert_tests[] = { .neg = 0, .dec = "9223794255762391041", .hex = "8001800180018001", + .mpi = { + 0x00, 0x00, 0x00, 0x09, 0x00, 0x80, 0x01, 0x80, + 0x01, 0x80, 0x01, 0x80, 0x01, + }, + .mpi_len = 13, }, { .bin = { 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, }, @@ -310,20 +353,41 @@ static const struct bn_convert_test bn_convert_tests[] = { .neg = 1, .dec = "-9223794255762391041", .hex = "-8001800180018001", + .mpi = { + 0x00, 0x00, 0x00, 0x09, 0x80, 0x80, 0x01, 0x80, + 0x01, 0x80, 0x01, 0x80, 0x01, + }, + .mpi_len = 13, }, { - .bin = { 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, }, + .bin = { + 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, + 0x01, + }, .bin_len = 9, .neg = 0, .dec = "27670538329471942657", .hex = "018001800180018001", + .mpi = { + 0x00, 0x00, 0x00, 0x09, 0x01, 0x80, 0x01, 0x80, + 0x01, 0x80, 0x01, 0x80, 0x01, + }, + .mpi_len = 13, }, { - .bin = { 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, }, + .bin = { + 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, + 0x01, + }, .bin_len = 9, .neg = 1, .dec = "-27670538329471942657", .hex = "-018001800180018001", + .mpi = { + 0x00, 0x00, 0x00, 0x09, 0x81, 0x80, 0x01, 0x80, + 0x01, 0x80, 0x01, 0x80, 0x01, + }, + .mpi_len = 13, }, { .bin = { @@ -336,6 +400,14 @@ static const struct bn_convert_test bn_convert_tests[] = { .neg = 0, .dec = "57895161181645529494837117048595051142566530671229791132691030063130991362047", .hex = "7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF", + .mpi = { + 0x00, 0x00, 0x00, 0x20, 0x7f, 0xff, 0x7f, 0xff, + 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, + 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, + 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, + 0x7f, 0xff, 0x7f, 0xff, + }, + .mpi_len = 36, }, { .bin = { @@ -348,6 +420,14 @@ static const struct bn_convert_test bn_convert_tests[] = { .neg = 1, .dec = "-57895161181645529494837117048595051142566530671229791132691030063130991362047", .hex = "-7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF", + .mpi = { + 0x00, 0x00, 0x00, 0x20, 0xff, 0xff, 0x7f, 0xff, + 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, + 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, + 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, + 0x7f, 0xff, 0x7f, 0xff, + }, + .mpi_len = 36, }, }; @@ -358,8 +438,10 @@ static int test_bn_convert(void) { const struct bn_convert_test *bct; + uint8_t *mpi_out = NULL; char *out_str = NULL; BIGNUM *bn = NULL; + int mpi_len; size_t i; int failed = 1; @@ -399,6 +481,29 @@ test_bn_convert(void) goto failure; } + free(mpi_out); + + if ((mpi_len = BN_bn2mpi(bn, NULL)) != bct->mpi_len) { + fprintf(stderr, "FAIL: Test %zu - BN_bn2mpi() returned " + "%d, want %d", i, mpi_len, bct->mpi_len); + goto failure; + } + if ((mpi_out = calloc(1, bct->mpi_len)) == NULL) + goto failure; + if ((mpi_len = BN_bn2mpi(bn, mpi_out)) != bct->mpi_len) { + fprintf(stderr, "FAIL: Test %zu - BN_bn2mpi() returned " + "%d, want %d", i, mpi_len, bct->mpi_len); + goto failure; + } + if (memcmp(mpi_out, bct->mpi, bct->mpi_len) != 0) { + fprintf(stderr, "FAIL: Test %zu - BN_bn2mpi() " + "generated:\n", i); + hexdump(mpi_out, bct->mpi_len); + fprintf(stderr, "Want:\n"); + hexdump(bct->mpi, bct->mpi_len); + goto failure; + } + if (BN_dec2bn(&bn, bct->dec) != (int)strlen(bct->dec)) { fprintf(stderr, "FAIL: BN_dec2bn() failed\n"); goto failure; @@ -409,7 +514,6 @@ test_bn_convert(void) bct->neg); goto failure; } - if (check_bin_output(i, "BN_dec2bn()", bct->bin, bct->bin_len, bn) != 0) goto failure; @@ -424,10 +528,23 @@ test_bn_convert(void) bct->neg); goto failure; } - if (check_bin_output(i, "BN_hex2bn()", bct->bin, bct->bin_len, bn) != 0) goto failure; + + if (BN_mpi2bn(bct->mpi, bct->mpi_len, bn) == NULL) { + fprintf(stderr, "FAIL: BN_mpi2bn() failed\n"); + goto failure; + } + if (BN_is_negative(bn) != bct->neg) { + fprintf(stderr, "FAIL: Test %zu - BN_mpi2bn() resulted " + "in negative %d, want %d", i, BN_is_negative(bn), + bct->neg); + goto failure; + } + if (check_bin_output(i, "BN_mpi2bn()", bct->bin, bct->bin_len, + bn) != 0) + goto failure; } failed = 0;