diff options
-rw-r--r-- | src/regress/lib/libcrypto/bn/bn_mod_sqrt.c | 51 |
1 files changed, 16 insertions, 35 deletions
diff --git a/src/regress/lib/libcrypto/bn/bn_mod_sqrt.c b/src/regress/lib/libcrypto/bn/bn_mod_sqrt.c index 72042341d6..fbf9cd9fc0 100644 --- a/src/regress/lib/libcrypto/bn/bn_mod_sqrt.c +++ b/src/regress/lib/libcrypto/bn/bn_mod_sqrt.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_mod_sqrt.c,v 1.5 2023/04/05 07:52:25 tb Exp $ */ | 1 | /* $OpenBSD: bn_mod_sqrt.c,v 1.6 2023/04/05 08:43:31 tb Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Copyright (c) 2022,2023 Theo Buehler <tb@openbsd.org> | 4 | * Copyright (c) 2022,2023 Theo Buehler <tb@openbsd.org> |
@@ -26,28 +26,22 @@ struct mod_sqrt_test { | |||
26 | const char *sqrt; | 26 | const char *sqrt; |
27 | const char *a; | 27 | const char *a; |
28 | const char *p; | 28 | const char *p; |
29 | int bn_mod_sqrt_fails; | ||
30 | } mod_sqrt_test_data[] = { | 29 | } mod_sqrt_test_data[] = { |
31 | { | 30 | { |
32 | .sqrt = "1", | 31 | .sqrt = "1", |
33 | .a = "1", | 32 | .a = "1", |
34 | .p = "2", | 33 | .p = "2", |
35 | .bn_mod_sqrt_fails = 0, | ||
36 | }, | 34 | }, |
37 | { | 35 | { |
38 | .sqrt = "-1", | ||
39 | .a = "20a7ee", | 36 | .a = "20a7ee", |
40 | .p = "460201", /* 460201 == 4D5 * E7D */ | 37 | .p = "460201", /* 460201 == 4D5 * E7D */ |
41 | .bn_mod_sqrt_fails = 1, | ||
42 | }, | 38 | }, |
43 | { | 39 | { |
44 | .sqrt = "-1", | ||
45 | .a = "65bebdb00a96fc814ec44b81f98b59fba3c30203928fa521" | 40 | .a = "65bebdb00a96fc814ec44b81f98b59fba3c30203928fa521" |
46 | "4c51e0a97091645280c947b005847f239758482b9bfc45b0" | 41 | "4c51e0a97091645280c947b005847f239758482b9bfc45b0" |
47 | "66fde340d1fe32fc9c1bf02e1b2d0ed", | 42 | "66fde340d1fe32fc9c1bf02e1b2d0ed", |
48 | .p = "9df9d6cc20b8540411af4e5357ef2b0353cb1f2ab5ffc3e2" | 43 | .p = "9df9d6cc20b8540411af4e5357ef2b0353cb1f2ab5ffc3e2" |
49 | "46b41c32f71e951f", | 44 | "46b41c32f71e951f", |
50 | .bn_mod_sqrt_fails = 1, | ||
51 | }, | 45 | }, |
52 | }; | 46 | }; |
53 | 47 | ||
@@ -74,38 +68,25 @@ mod_sqrt_test(struct mod_sqrt_test *test, BN_CTX *ctx) | |||
74 | if ((sum = BN_CTX_get(ctx)) == NULL) | 68 | if ((sum = BN_CTX_get(ctx)) == NULL) |
75 | errx(1, "sum = BN_CTX_get()"); | 69 | errx(1, "sum = BN_CTX_get()"); |
76 | 70 | ||
77 | if (!BN_hex2bn(&a, test->a)) { | 71 | if (!BN_hex2bn(&a, test->a)) |
78 | fprintf(stderr, "BN_hex2bn(a) failed\n"); | 72 | errx(1, "BN_hex2bn(%s)", test->a); |
79 | goto out; | 73 | if (!BN_hex2bn(&p, test->p)) |
80 | } | 74 | errx(1, "BN_hex2bn(%s)", test->p); |
81 | if (!BN_hex2bn(&p, test->p)) { | ||
82 | fprintf(stderr, "BN_hex2bn(p) failed\n"); | ||
83 | goto out; | ||
84 | } | ||
85 | if (!BN_hex2bn(&want, test->sqrt)) { | ||
86 | fprintf(stderr, "BN_hex2bn(want) failed\n"); | ||
87 | goto out; | ||
88 | } | ||
89 | |||
90 | if ((BN_mod_sqrt(got, a, p, ctx) == NULL) != test->bn_mod_sqrt_fails) { | ||
91 | fprintf(stderr, "BN_mod_sqrt %s unexpectedly\n", | ||
92 | test->bn_mod_sqrt_fails ? "succeeded" : "failed"); | ||
93 | goto out; | ||
94 | } | ||
95 | 75 | ||
96 | if (test->bn_mod_sqrt_fails) { | 76 | if (BN_mod_sqrt(got, a, p, ctx) == NULL) { |
97 | failed = 0; | 77 | failed = test->sqrt != NULL; |
78 | if (failed) | ||
79 | fprintf(stderr, "BN_mod_sqrt(%s, %s) failed\n", | ||
80 | test->a, test->p); | ||
98 | goto out; | 81 | goto out; |
99 | } | 82 | } |
100 | 83 | ||
101 | if (!BN_mod_sub(diff, want, got, p, ctx)) { | 84 | if (!BN_hex2bn(&want, test->sqrt)) |
102 | fprintf(stderr, "BN_mod_sub() failed\n"); | 85 | errx(1, "BN_hex2bn(%s)", test->sqrt); |
103 | goto out; | 86 | if (!BN_mod_sub(diff, want, got, p, ctx)) |
104 | } | 87 | errx(1, "BN_mod_sub() failed\n"); |
105 | if (!BN_mod_add(sum, want, got, p, ctx)) { | 88 | if (!BN_mod_add(sum, want, got, p, ctx)) |
106 | fprintf(stderr, "BN_mod_add() failed\n"); | 89 | errx(1, "BN_mod_add() failed\n"); |
107 | goto out; | ||
108 | } | ||
109 | 90 | ||
110 | /* XXX - Remove sum once we return the canonical square root. */ | 91 | /* XXX - Remove sum once we return the canonical square root. */ |
111 | if (!BN_is_zero(diff) && !BN_is_zero(sum)) { | 92 | if (!BN_is_zero(diff) && !BN_is_zero(sum)) { |