diff options
author | jsing <> | 2024-11-03 12:47:49 +0000 |
---|---|---|
committer | jsing <> | 2024-11-03 12:47:49 +0000 |
commit | 43dc96128277a7f05032e54f8540ea0c163b0480 (patch) | |
tree | c540e830e389e4303366e2d6b10139412544bc54 | |
parent | 4847ae71f9695f306b983673f5bcdcc5310dacaa (diff) | |
download | openbsd-43dc96128277a7f05032e54f8540ea0c163b0480.tar.gz openbsd-43dc96128277a7f05032e54f8540ea0c163b0480.tar.bz2 openbsd-43dc96128277a7f05032e54f8540ea0c163b0480.zip |
Add regress coverage for BN_bn2binpad() and BN_bn2lebinpad().
-rw-r--r-- | src/regress/lib/libcrypto/bn/bn_convert.c | 140 |
1 files changed, 137 insertions, 3 deletions
diff --git a/src/regress/lib/libcrypto/bn/bn_convert.c b/src/regress/lib/libcrypto/bn/bn_convert.c index bd4eba3663..e7d6138f36 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.7 2024/11/03 12:46:55 jsing Exp $ */ | 1 | /* $OpenBSD: bn_convert.c,v 1.8 2024/11/03 12:47:49 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -23,8 +23,6 @@ | |||
23 | /* | 23 | /* |
24 | * Additional test coverage is needed for: | 24 | * Additional test coverage is needed for: |
25 | * | 25 | * |
26 | * - BN_bn2binpad() | ||
27 | * - BN_bn2lebinpad() | ||
28 | * - BN_print()/BN_print_fp() | 26 | * - BN_print()/BN_print_fp() |
29 | * | 27 | * |
30 | * - Invalid inputs to {asc,dec,hex,mpi}2bn | 28 | * - Invalid inputs to {asc,dec,hex,mpi}2bn |
@@ -765,6 +763,141 @@ test_bn_hex2bn(void) | |||
765 | return failed; | 763 | return failed; |
766 | } | 764 | } |
767 | 765 | ||
766 | static int | ||
767 | test_bn_binpad(void) | ||
768 | { | ||
769 | const struct bn_convert_test *bct; | ||
770 | BIGNUM *bn = NULL; | ||
771 | uint8_t lebin[64]; | ||
772 | uint8_t buf[128]; | ||
773 | size_t i, j; | ||
774 | int ret; | ||
775 | int failed = 1; | ||
776 | |||
777 | for (i = 0; i < N_BN_CONVERT_TESTS; i++) { | ||
778 | bct = &bn_convert_tests[i]; | ||
779 | |||
780 | BN_free(bn); | ||
781 | if ((bn = BN_bin2bn(bct->bin, bct->bin_len, NULL)) == NULL) { | ||
782 | fprintf(stderr, "FAIL: BN_bin2bn() failed\n"); | ||
783 | goto failure; | ||
784 | } | ||
785 | BN_set_negative(bn, bct->neg); | ||
786 | |||
787 | for (j = 0; j < bct->bin_len; j++) | ||
788 | lebin[j] = bct->bin[bct->bin_len - j - 1]; | ||
789 | |||
790 | if ((ret = BN_bn2binpad(bn, buf, bct->bin_len)) < 0) { | ||
791 | fprintf(stderr, "FAIL: BN_bn2binpad() failed\n"); | ||
792 | goto failure; | ||
793 | } | ||
794 | if ((size_t)ret != bct->bin_len) { | ||
795 | fprintf(stderr, "FAIL: BN_bn2binpad() = %d, want %zu\n", | ||
796 | ret, bct->bin_len); | ||
797 | goto failure; | ||
798 | } | ||
799 | if (memcmp(buf, bct->bin, bct->bin_len) != 0) { | ||
800 | fprintf(stderr, "FAIL: Test %zu - output from " | ||
801 | "BN_bn2binpad() differs\n", i); | ||
802 | fprintf(stderr, "Got:\n"); | ||
803 | hexdump(buf, bct->bin_len); | ||
804 | fprintf(stderr, "Want:\n"); | ||
805 | hexdump(bct->bin, bct->bin_len); | ||
806 | goto failure; | ||
807 | } | ||
808 | if (bct->bin_len > 0) { | ||
809 | if ((ret = BN_bn2binpad(bn, buf, bct->bin_len - 1)) != -1) { | ||
810 | fprintf(stderr, "FAIL: BN_bn2binpad() succeeded " | ||
811 | "with truncation\n"); | ||
812 | goto failure; | ||
813 | } | ||
814 | } | ||
815 | if ((ret = BN_bn2binpad(bn, buf, 128)) < 0) { | ||
816 | fprintf(stderr, "FAIL: BN_bn2binpad() failed\n"); | ||
817 | goto failure; | ||
818 | } | ||
819 | if (ret != 128) { | ||
820 | fprintf(stderr, "FAIL: BN_bn2binpad() = %d, want 128\n", | ||
821 | ret); | ||
822 | goto failure; | ||
823 | } | ||
824 | if (memcmp(&buf[128 - bct->bin_len], bct->bin, bct->bin_len) != 0) { | ||
825 | fprintf(stderr, "FAIL: Test %zu - output from " | ||
826 | "BN_bn2binpad() differs\n", i); | ||
827 | fprintf(stderr, "Got:\n"); | ||
828 | hexdump(&buf[128 - bct->bin_len], bct->bin_len); | ||
829 | fprintf(stderr, "Want:\n"); | ||
830 | hexdump(bct->bin, bct->bin_len); | ||
831 | goto failure; | ||
832 | } | ||
833 | for (j = 0; j < 128 - bct->bin_len; j++) { | ||
834 | if (buf[j] != 0) { | ||
835 | fprintf(stderr, "FAIL: BN_bn2binpad() is not " | ||
836 | "zero padded\n"); | ||
837 | goto failure; | ||
838 | } | ||
839 | } | ||
840 | |||
841 | if ((ret = BN_bn2lebinpad(bn, buf, bct->bin_len)) < 0) { | ||
842 | fprintf(stderr, "FAIL: BN_bn2lebinpad() failed\n"); | ||
843 | goto failure; | ||
844 | } | ||
845 | if ((size_t)ret != bct->bin_len) { | ||
846 | fprintf(stderr, "FAIL: BN_bn2lebinpad() = %d, want %zu\n", | ||
847 | ret, bct->bin_len); | ||
848 | goto failure; | ||
849 | } | ||
850 | if (memcmp(buf, lebin, bct->bin_len) != 0) { | ||
851 | fprintf(stderr, "FAIL: Test %zu - output from " | ||
852 | "BN_bn2lebinpad() differs\n", i); | ||
853 | fprintf(stderr, "Got:\n"); | ||
854 | hexdump(buf, bct->bin_len); | ||
855 | fprintf(stderr, "Want:\n"); | ||
856 | hexdump(lebin, bct->bin_len); | ||
857 | goto failure; | ||
858 | } | ||
859 | if (bct->bin_len > 0) { | ||
860 | if ((ret = BN_bn2lebinpad(bn, buf, bct->bin_len - 1)) != -1) { | ||
861 | fprintf(stderr, "FAIL: BN_bn2lebinpad() succeeded " | ||
862 | "with truncation\n"); | ||
863 | goto failure; | ||
864 | } | ||
865 | } | ||
866 | if ((ret = BN_bn2lebinpad(bn, buf, 128)) < 0) { | ||
867 | fprintf(stderr, "FAIL: BN_bn2lebinpad() failed\n"); | ||
868 | goto failure; | ||
869 | } | ||
870 | if (ret != 128) { | ||
871 | fprintf(stderr, "FAIL: BN_bn2lebinpad() = %d, want 128\n", | ||
872 | ret); | ||
873 | goto failure; | ||
874 | } | ||
875 | if (memcmp(buf, lebin, bct->bin_len) != 0) { | ||
876 | fprintf(stderr, "FAIL: Test %zu - output from " | ||
877 | "BN_bn2lebinpad() differs\n", i); | ||
878 | fprintf(stderr, "Got:\n"); | ||
879 | hexdump(buf, bct->bin_len); | ||
880 | fprintf(stderr, "Want:\n"); | ||
881 | hexdump(lebin, bct->bin_len); | ||
882 | goto failure; | ||
883 | } | ||
884 | for (j = bct->bin_len; j < 128; j++) { | ||
885 | if (buf[j] != 0) { | ||
886 | fprintf(stderr, "FAIL: BN_bn2lebinpad() is not " | ||
887 | "zero padded\n"); | ||
888 | goto failure; | ||
889 | } | ||
890 | } | ||
891 | } | ||
892 | |||
893 | failed = 0; | ||
894 | |||
895 | failure: | ||
896 | BN_free(bn); | ||
897 | |||
898 | return failed; | ||
899 | } | ||
900 | |||
768 | int | 901 | int |
769 | main(int argc, char **argv) | 902 | main(int argc, char **argv) |
770 | { | 903 | { |
@@ -774,6 +907,7 @@ main(int argc, char **argv) | |||
774 | failed |= test_bn_convert(); | 907 | failed |= test_bn_convert(); |
775 | failed |= test_bn_dec2bn(); | 908 | failed |= test_bn_dec2bn(); |
776 | failed |= test_bn_hex2bn(); | 909 | failed |= test_bn_hex2bn(); |
910 | failed |= test_bn_binpad(); | ||
777 | 911 | ||
778 | return failed; | 912 | return failed; |
779 | } | 913 | } |