diff options
author | tb <> | 2023-05-09 05:39:24 +0000 |
---|---|---|
committer | tb <> | 2023-05-09 05:39:24 +0000 |
commit | 977a2002270d79625c8895d7172b67a0c8544bbf (patch) | |
tree | 9cfc28931845a9b1947e12d527139be0906244c0 /src | |
parent | a12cb743f19d02d79b27833e923b1eda920e4381 (diff) | |
download | openbsd-977a2002270d79625c8895d7172b67a0c8544bbf.tar.gz openbsd-977a2002270d79625c8895d7172b67a0c8544bbf.tar.bz2 openbsd-977a2002270d79625c8895d7172b67a0c8544bbf.zip |
Add regress coverage for -1 modulus as well.
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libcrypto/bn/bn_mod_exp.c | 63 |
1 files changed, 38 insertions, 25 deletions
diff --git a/src/regress/lib/libcrypto/bn/bn_mod_exp.c b/src/regress/lib/libcrypto/bn/bn_mod_exp.c index c20ad72442..14e1883979 100644 --- a/src/regress/lib/libcrypto/bn/bn_mod_exp.c +++ b/src/regress/lib/libcrypto/bn/bn_mod_exp.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_mod_exp.c,v 1.37 2023/04/25 17:17:21 tb Exp $ */ | 1 | /* $OpenBSD: bn_mod_exp.c,v 1.38 2023/05/09 05:39:24 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> |
@@ -86,19 +86,21 @@ bn_print(const char *name, const BIGNUM *bn) | |||
86 | } | 86 | } |
87 | 87 | ||
88 | static void | 88 | static void |
89 | print_zero_test_failure(const BIGNUM *got, const BIGNUM *a, const char *name) | 89 | print_zero_test_failure(const BIGNUM *got, const BIGNUM *a, const BIGNUM *m, |
90 | const char *name) | ||
90 | { | 91 | { |
91 | fprintf(stderr, "%s() zero test failed:\n", name); | 92 | fprintf(stderr, "%s() zero test failed:\n", name); |
92 | 93 | ||
93 | bn_print("a", a); | 94 | bn_print("a", a); |
95 | bn_print("m", m); | ||
94 | bn_print("got", got); | 96 | bn_print("got", got); |
95 | } | 97 | } |
96 | 98 | ||
97 | static int | 99 | static int |
98 | bn_mod_exp_zero_test(const struct mod_exp_test *test, BN_CTX *ctx, int use_random) | 100 | bn_mod_exp_zero_test(const struct mod_exp_test *test, BN_CTX *ctx, |
101 | int neg_modulus, int random_base) | ||
99 | { | 102 | { |
100 | const BIGNUM *one; | 103 | BIGNUM *a, *m, *p, *got; |
101 | BIGNUM *a, *p, *got; | ||
102 | int mod_exp_ret; | 104 | int mod_exp_ret; |
103 | int failed = 1; | 105 | int failed = 1; |
104 | 106 | ||
@@ -106,24 +108,29 @@ bn_mod_exp_zero_test(const struct mod_exp_test *test, BN_CTX *ctx, int use_rando | |||
106 | 108 | ||
107 | if ((a = BN_CTX_get(ctx)) == NULL) | 109 | if ((a = BN_CTX_get(ctx)) == NULL) |
108 | errx(1, "BN_CTX_get"); | 110 | errx(1, "BN_CTX_get"); |
111 | if ((m = BN_CTX_get(ctx)) == NULL) | ||
112 | errx(1, "BN_CTX_get"); | ||
109 | if ((p = BN_CTX_get(ctx)) == NULL) | 113 | if ((p = BN_CTX_get(ctx)) == NULL) |
110 | errx(1, "BN_CTX_get"); | 114 | errx(1, "BN_CTX_get"); |
111 | if ((got = BN_CTX_get(ctx)) == NULL) | 115 | if ((got = BN_CTX_get(ctx)) == NULL) |
112 | errx(1, "BN_CTX_get"); | 116 | errx(1, "BN_CTX_get"); |
113 | 117 | ||
114 | one = BN_value_one(); | 118 | if (!BN_one(m)) |
119 | errx(1, "BN_one"); | ||
120 | if (neg_modulus) | ||
121 | BN_set_negative(m, 1); | ||
115 | BN_zero(a); | 122 | BN_zero(a); |
116 | BN_zero(p); | 123 | BN_zero(p); |
117 | 124 | ||
118 | if (use_random) { | 125 | if (random_base) { |
119 | if (!BN_rand(a, 1024, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)) | 126 | if (!BN_rand(a, 1024, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)) |
120 | errx(1, "BN_rand"); | 127 | errx(1, "BN_rand"); |
121 | } | 128 | } |
122 | 129 | ||
123 | if (test->mod_exp_fn != NULL) { | 130 | if (test->mod_exp_fn != NULL) { |
124 | mod_exp_ret = test->mod_exp_fn(got, a, p, one, ctx); | 131 | mod_exp_ret = test->mod_exp_fn(got, a, p, m, ctx); |
125 | } else { | 132 | } else { |
126 | mod_exp_ret = test->mod_exp_mont_fn(got, a, p, one, ctx, NULL); | 133 | mod_exp_ret = test->mod_exp_mont_fn(got, a, p, m, ctx, NULL); |
127 | } | 134 | } |
128 | 135 | ||
129 | if (!mod_exp_ret) { | 136 | if (!mod_exp_ret) { |
@@ -133,7 +140,7 @@ bn_mod_exp_zero_test(const struct mod_exp_test *test, BN_CTX *ctx, int use_rando | |||
133 | } | 140 | } |
134 | 141 | ||
135 | if (!BN_is_zero(got)) { | 142 | if (!BN_is_zero(got)) { |
136 | print_zero_test_failure(got, a, test->name); | 143 | print_zero_test_failure(got, a, m, test->name); |
137 | goto err; | 144 | goto err; |
138 | } | 145 | } |
139 | 146 | ||
@@ -146,31 +153,35 @@ bn_mod_exp_zero_test(const struct mod_exp_test *test, BN_CTX *ctx, int use_rando | |||
146 | } | 153 | } |
147 | 154 | ||
148 | static int | 155 | static int |
149 | bn_mod_exp_zero_word_test(BN_CTX *ctx) | 156 | bn_mod_exp_zero_word_test(BN_CTX *ctx, int neg_modulus) |
150 | { | 157 | { |
151 | const char *name = "BN_mod_exp_mont_word"; | 158 | const char *name = "BN_mod_exp_mont_word"; |
152 | const BIGNUM *one; | 159 | BIGNUM *m, *p, *got; |
153 | BIGNUM *p, *got; | ||
154 | int failed = 1; | 160 | int failed = 1; |
155 | 161 | ||
156 | BN_CTX_start(ctx); | 162 | BN_CTX_start(ctx); |
157 | 163 | ||
164 | if ((m = BN_CTX_get(ctx)) == NULL) | ||
165 | errx(1, "BN_CTX_get"); | ||
158 | if ((p = BN_CTX_get(ctx)) == NULL) | 166 | if ((p = BN_CTX_get(ctx)) == NULL) |
159 | errx(1, "BN_CTX_get"); | 167 | errx(1, "BN_CTX_get"); |
160 | if ((got = BN_CTX_get(ctx)) == NULL) | 168 | if ((got = BN_CTX_get(ctx)) == NULL) |
161 | errx(1, "BN_CTX_get"); | 169 | errx(1, "BN_CTX_get"); |
162 | 170 | ||
163 | one = BN_value_one(); | 171 | if (!BN_one(m)) |
172 | errx(1, "BN_one"); | ||
173 | if (neg_modulus) | ||
174 | BN_set_negative(m, neg_modulus); | ||
164 | BN_zero(p); | 175 | BN_zero(p); |
165 | 176 | ||
166 | if (!BN_mod_exp_mont_word(got, 1, p, one, ctx, NULL)) { | 177 | if (!BN_mod_exp_mont_word(got, 1, p, m, ctx, NULL)) { |
167 | fprintf(stderr, "%s failed\n", name); | 178 | fprintf(stderr, "%s failed\n", name); |
168 | ERR_print_errors_fp(stderr); | 179 | ERR_print_errors_fp(stderr); |
169 | goto err; | 180 | goto err; |
170 | } | 181 | } |
171 | 182 | ||
172 | if (!BN_is_zero(got)) { | 183 | if (!BN_is_zero(got)) { |
173 | print_zero_test_failure(got, one, name); | 184 | print_zero_test_failure(got, p, m, name); |
174 | goto err; | 185 | goto err; |
175 | } | 186 | } |
176 | 187 | ||
@@ -186,22 +197,24 @@ static int | |||
186 | test_bn_mod_exp_zero(void) | 197 | test_bn_mod_exp_zero(void) |
187 | { | 198 | { |
188 | BN_CTX *ctx; | 199 | BN_CTX *ctx; |
189 | size_t i; | 200 | size_t i, j; |
190 | int use_random; | ||
191 | int failed = 0; | 201 | int failed = 0; |
192 | 202 | ||
193 | if ((ctx = BN_CTX_new()) == NULL) | 203 | if ((ctx = BN_CTX_new()) == NULL) |
194 | errx(1, "BN_CTX_new"); | 204 | errx(1, "BN_CTX_new"); |
195 | 205 | ||
196 | use_random = 1; | 206 | for (i = 0; i < N_MOD_EXP_FN; i++) { |
197 | for (i = 0; i < N_MOD_EXP_FN; i++) | 207 | for (j = 0; j < 4; j++) { |
198 | failed |= bn_mod_exp_zero_test(&mod_exp_fn[i], ctx, use_random); | 208 | int neg_modulus = (j >> 0) & 1; |
209 | int random_base = (j >> 1) & 1; | ||
199 | 210 | ||
200 | use_random = 0; | 211 | failed |= bn_mod_exp_zero_test(&mod_exp_fn[i], ctx, |
201 | for (i = 0; i < N_MOD_EXP_FN; i++) | 212 | neg_modulus, random_base); |
202 | failed |= bn_mod_exp_zero_test(&mod_exp_fn[i], ctx, use_random); | 213 | } |
214 | } | ||
203 | 215 | ||
204 | failed |= bn_mod_exp_zero_word_test(ctx); | 216 | failed |= bn_mod_exp_zero_word_test(ctx, 0); |
217 | failed |= bn_mod_exp_zero_word_test(ctx, 1); | ||
205 | 218 | ||
206 | BN_CTX_free(ctx); | 219 | BN_CTX_free(ctx); |
207 | 220 | ||