diff options
| author | tb <> | 2022-03-15 16:28:42 +0000 |
|---|---|---|
| committer | tb <> | 2022-03-15 16:28:42 +0000 |
| commit | a30a99d65aa72b3d8775fa8cfe722b67ac5d4263 (patch) | |
| tree | 2830b610d392bde01298e64305c1edd88a954d46 | |
| parent | 4464a9c037233b957ca783d0001309572663dd52 (diff) | |
| download | openbsd-a30a99d65aa72b3d8775fa8cfe722b67ac5d4263.tar.gz openbsd-a30a99d65aa72b3d8775fa8cfe722b67ac5d4263.tar.bz2 openbsd-a30a99d65aa72b3d8775fa8cfe722b67ac5d4263.zip | |
Add a simple regress to verify that the infinite loop in BN_mod_sqrt()
is fixed.
| -rw-r--r-- | src/regress/lib/libcrypto/bn/general/Makefile | 7 | ||||
| -rw-r--r-- | src/regress/lib/libcrypto/bn/general/bn_mod_sqrt.c | 132 |
2 files changed, 138 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/bn/general/Makefile b/src/regress/lib/libcrypto/bn/general/Makefile index fec9575d0e..e322d319cf 100644 --- a/src/regress/lib/libcrypto/bn/general/Makefile +++ b/src/regress/lib/libcrypto/bn/general/Makefile | |||
| @@ -1,9 +1,10 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.8 2022/03/10 04:39:49 tb Exp $ | 1 | # $OpenBSD: Makefile,v 1.9 2022/03/15 16:28:42 tb Exp $ |
| 2 | 2 | ||
| 3 | .include "../../Makefile.inc" | 3 | .include "../../Makefile.inc" |
| 4 | 4 | ||
| 5 | PROGS += bntest | 5 | PROGS += bntest |
| 6 | PROGS += bn_mod_exp2_mont | 6 | PROGS += bn_mod_exp2_mont |
| 7 | PROGS += bn_mod_sqrt | ||
| 7 | PROGS += bn_to_string | 8 | PROGS += bn_to_string |
| 8 | 9 | ||
| 9 | LDADD = ${CRYPTO_INT} | 10 | LDADD = ${CRYPTO_INT} |
| @@ -25,6 +26,10 @@ REGRESS_TARGETS += run-bn_mod_exp2_mont | |||
| 25 | run-bn_mod_exp2_mont: bn_mod_exp2_mont | 26 | run-bn_mod_exp2_mont: bn_mod_exp2_mont |
| 26 | ./bn_mod_exp2_mont | 27 | ./bn_mod_exp2_mont |
| 27 | 28 | ||
| 29 | REGRESS_TARGETS += run-bn_mod_sqrt | ||
| 30 | run-bn_mod_sqrt: bn_mod_exp2_mont | ||
| 31 | ./bn_mod_sqrt | ||
| 32 | |||
| 28 | REGRESS_TARGETS += run-bn_to_string | 33 | REGRESS_TARGETS += run-bn_to_string |
| 29 | run-bn_to_string: bn_to_string | 34 | run-bn_to_string: bn_to_string |
| 30 | ./bn_to_string | 35 | ./bn_to_string |
diff --git a/src/regress/lib/libcrypto/bn/general/bn_mod_sqrt.c b/src/regress/lib/libcrypto/bn/general/bn_mod_sqrt.c new file mode 100644 index 0000000000..2017492e2b --- /dev/null +++ b/src/regress/lib/libcrypto/bn/general/bn_mod_sqrt.c | |||
| @@ -0,0 +1,132 @@ | |||
| 1 | /* $OpenBSD: bn_mod_sqrt.c,v 1.1 2022/03/15 16:28:42 tb Exp $ */ | ||
| 2 | /* | ||
| 3 | * Copyright (c) 2022 Theo Buehler <tb@openbsd.org> | ||
| 4 | * | ||
| 5 | * Permission to use, copy, modify, and distribute this software for any | ||
| 6 | * purpose with or without fee is hereby granted, provided that the above | ||
| 7 | * copyright notice and this permission notice appear in all copies. | ||
| 8 | * | ||
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <openssl/bn.h> | ||
| 19 | |||
| 20 | /* Test that sqrt * sqrt = A (mod p) where p is a prime */ | ||
| 21 | struct mod_sqrt_test { | ||
| 22 | const char *sqrt; | ||
| 23 | const char *a; | ||
| 24 | const char *p; | ||
| 25 | int bn_mod_sqrt_fails; | ||
| 26 | } mod_sqrt_test_data[] = { | ||
| 27 | { | ||
| 28 | .sqrt = "1", | ||
| 29 | .a = "1", | ||
| 30 | .p = "2", | ||
| 31 | .bn_mod_sqrt_fails = 0, | ||
| 32 | }, | ||
| 33 | { | ||
| 34 | .sqrt = "-1", | ||
| 35 | .a = "20a7ee", | ||
| 36 | .p = "460201", /* 460201 == 4D5 * E7D */ | ||
| 37 | .bn_mod_sqrt_fails = 1, | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | .sqrt = "-1", | ||
| 41 | .a = "65bebdb00a96fc814ec44b81f98b59fba3c30203928fa521" | ||
| 42 | "4c51e0a97091645280c947b005847f239758482b9bfc45b0" | ||
| 43 | "66fde340d1fe32fc9c1bf02e1b2d0ed", | ||
| 44 | .p = "9df9d6cc20b8540411af4e5357ef2b0353cb1f2ab5ffc3e2" | ||
| 45 | "46b41c32f71e951f", | ||
| 46 | .bn_mod_sqrt_fails = 1, | ||
| 47 | }, | ||
| 48 | }; | ||
| 49 | |||
| 50 | const size_t N_TESTS = sizeof(mod_sqrt_test_data) / sizeof(*mod_sqrt_test_data); | ||
| 51 | |||
| 52 | int mod_sqrt_test(struct mod_sqrt_test *test); | ||
| 53 | |||
| 54 | int | ||
| 55 | mod_sqrt_test(struct mod_sqrt_test *test) | ||
| 56 | { | ||
| 57 | BN_CTX *ctx = NULL; | ||
| 58 | BIGNUM *a = NULL, *p = NULL, *want = NULL, *got = NULL, *diff = NULL; | ||
| 59 | int failed = 1; | ||
| 60 | |||
| 61 | if ((ctx = BN_CTX_new()) == NULL) { | ||
| 62 | fprintf(stderr, "BN_CTX_new failed\n"); | ||
| 63 | goto out; | ||
| 64 | } | ||
| 65 | |||
| 66 | if (!BN_hex2bn(&a, test->a)) { | ||
| 67 | fprintf(stderr, "BN_hex2bn(a) failed\n"); | ||
| 68 | goto out; | ||
| 69 | } | ||
| 70 | if (!BN_hex2bn(&p, test->p)) { | ||
| 71 | fprintf(stderr, "BN_hex2bn(p) failed\n"); | ||
| 72 | goto out; | ||
| 73 | } | ||
| 74 | if (!BN_hex2bn(&want, test->sqrt)) { | ||
| 75 | fprintf(stderr, "BN_hex2bn(want) failed\n"); | ||
| 76 | goto out; | ||
| 77 | } | ||
| 78 | |||
| 79 | if (((got = BN_mod_sqrt(NULL, a, p, ctx)) == NULL) != | ||
| 80 | test->bn_mod_sqrt_fails) { | ||
| 81 | fprintf(stderr, "BN_mod_sqrt %s unexpectedly\n", | ||
| 82 | test->bn_mod_sqrt_fails ? "succeeded" : "failed"); | ||
| 83 | goto out; | ||
| 84 | } | ||
| 85 | |||
| 86 | if (test->bn_mod_sqrt_fails) { | ||
| 87 | failed = 0; | ||
| 88 | goto out; | ||
| 89 | } | ||
| 90 | |||
| 91 | if ((diff = BN_new()) == NULL) { | ||
| 92 | fprintf(stderr, "diff = BN_new() failed\n"); | ||
| 93 | goto out; | ||
| 94 | } | ||
| 95 | |||
| 96 | if (!BN_mod_sub(diff, want, got, p, ctx)) { | ||
| 97 | fprintf(stderr, "BN_mod_sub failed\n"); | ||
| 98 | goto out; | ||
| 99 | } | ||
| 100 | |||
| 101 | if (!BN_is_zero(diff)) { | ||
| 102 | fprintf(stderr, "want != got\n"); | ||
| 103 | goto out; | ||
| 104 | } | ||
| 105 | |||
| 106 | failed = 0; | ||
| 107 | |||
| 108 | out: | ||
| 109 | BN_CTX_free(ctx); | ||
| 110 | BN_free(a); | ||
| 111 | BN_free(p); | ||
| 112 | BN_free(want); | ||
| 113 | BN_free(got); | ||
| 114 | BN_free(diff); | ||
| 115 | |||
| 116 | return failed; | ||
| 117 | } | ||
| 118 | |||
| 119 | int | ||
| 120 | main(void) | ||
| 121 | { | ||
| 122 | size_t i; | ||
| 123 | int failed = 0; | ||
| 124 | |||
| 125 | for (i = 0; i < N_TESTS; i++) | ||
| 126 | failed |= mod_sqrt_test(&mod_sqrt_test_data[i]); | ||
| 127 | |||
| 128 | if (!failed) | ||
| 129 | printf("SUCCESS\n"); | ||
| 130 | |||
| 131 | return failed; | ||
| 132 | } | ||
