diff options
author | tb <> | 2023-07-26 17:15:25 +0000 |
---|---|---|
committer | tb <> | 2023-07-26 17:15:25 +0000 |
commit | 90222448640edb75c0fb60c2e5cf2a537991eba9 (patch) | |
tree | 8148023a7d5051074ff3d84edddf71de06f2a7fe | |
parent | 9d7ceead35e184d56cd85a5b4741828341b03d05 (diff) | |
download | openbsd-90222448640edb75c0fb60c2e5cf2a537991eba9.tar.gz openbsd-90222448640edb75c0fb60c2e5cf2a537991eba9.tar.bz2 openbsd-90222448640edb75c0fb60c2e5cf2a537991eba9.zip |
Tweak EC_GROUP_check_discriminant()
Make the logic and control flow a bit more explicit and use a single
extra variable for computing the discriminant. Call it discriminant,
not tmp, tmp_1 or tmp_2.
ok jsing
-rw-r--r-- | src/lib/libcrypto/ec/ecp_smpl.c | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/src/lib/libcrypto/ec/ecp_smpl.c b/src/lib/libcrypto/ec/ecp_smpl.c index f591fa0267..de1f9a3472 100644 --- a/src/lib/libcrypto/ec/ecp_smpl.c +++ b/src/lib/libcrypto/ec/ecp_smpl.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: ecp_smpl.c,v 1.54 2023/07/26 12:26:48 tb Exp $ */ | 1 | /* $OpenBSD: ecp_smpl.c,v 1.55 2023/07/26 17:15:25 tb Exp $ */ |
2 | /* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> | 2 | /* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> |
3 | * for the OpenSSL project. | 3 | * for the OpenSSL project. |
4 | * Includes code written by Bodo Moeller for the OpenSSL project. | 4 | * Includes code written by Bodo Moeller for the OpenSSL project. |
@@ -222,7 +222,7 @@ ec_GFp_simple_group_get_degree(const EC_GROUP *group) | |||
222 | int | 222 | int |
223 | ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) | 223 | ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) |
224 | { | 224 | { |
225 | BIGNUM *p, *a, *b, *tmp_1, *tmp_2; | 225 | BIGNUM *p, *a, *b, *discriminant; |
226 | int ret = 0; | 226 | int ret = 0; |
227 | 227 | ||
228 | BN_CTX_start(ctx); | 228 | BN_CTX_start(ctx); |
@@ -233,41 +233,41 @@ ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) | |||
233 | goto err; | 233 | goto err; |
234 | if ((b = BN_CTX_get(ctx)) == NULL) | 234 | if ((b = BN_CTX_get(ctx)) == NULL) |
235 | goto err; | 235 | goto err; |
236 | if ((tmp_1 = BN_CTX_get(ctx)) == NULL) | 236 | if ((discriminant = BN_CTX_get(ctx)) == NULL) |
237 | goto err; | ||
238 | if ((tmp_2 = BN_CTX_get(ctx)) == NULL) | ||
239 | goto err; | 237 | goto err; |
240 | 238 | ||
241 | if (!EC_GROUP_get_curve(group, p, a, b, ctx)) | 239 | if (!EC_GROUP_get_curve(group, p, a, b, ctx)) |
242 | goto err; | 240 | goto err; |
243 | 241 | ||
244 | /* | 242 | /* |
245 | * check the discriminant: y^2 = x^3 + a*x + b is an elliptic curve | 243 | * Check that the discriminant 4a^3 + 27b^2 is non-zero modulo p. |
246 | * <=> 4*a^3 + 27*b^2 != 0 (mod p) 0 =< a, b < p | ||
247 | */ | 244 | */ |
248 | if (BN_is_zero(a)) { | ||
249 | if (BN_is_zero(b)) | ||
250 | goto err; | ||
251 | } else if (!BN_is_zero(b)) { | ||
252 | if (!BN_mod_sqr(tmp_1, a, p, ctx)) | ||
253 | goto err; | ||
254 | if (!BN_mod_mul(tmp_2, tmp_1, a, p, ctx)) | ||
255 | goto err; | ||
256 | if (!BN_lshift(tmp_1, tmp_2, 2)) | ||
257 | goto err; | ||
258 | /* tmp_1 = 4*a^3 */ | ||
259 | 245 | ||
260 | if (!BN_mod_sqr(tmp_2, b, p, ctx)) | 246 | if (BN_is_zero(a) && BN_is_zero(b)) |
261 | goto err; | 247 | goto err; |
262 | if (!BN_mul_word(tmp_2, 27)) | 248 | if (BN_is_zero(a) || BN_is_zero(b)) |
263 | goto err; | 249 | goto done; |
264 | /* tmp_2 = 27*b^2 */ | ||
265 | 250 | ||
266 | if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx)) | 251 | /* Compute the discriminant: first 4a^3, then 27b^2, then their sum. */ |
267 | goto err; | 252 | if (!BN_mod_sqr(discriminant, a, p, ctx)) |
268 | if (BN_is_zero(a)) | 253 | goto err; |
269 | goto err; | 254 | if (!BN_mod_mul(discriminant, discriminant, a, p, ctx)) |
270 | } | 255 | goto err; |
256 | if (!BN_lshift(discriminant, discriminant, 2)) | ||
257 | goto err; | ||
258 | |||
259 | if (!BN_mod_sqr(b, b, p, ctx)) | ||
260 | goto err; | ||
261 | if (!BN_mul_word(b, 27)) | ||
262 | goto err; | ||
263 | |||
264 | if (!BN_mod_add(discriminant, discriminant, b, p, ctx)) | ||
265 | goto err; | ||
266 | |||
267 | if (BN_is_zero(discriminant)) | ||
268 | goto err; | ||
269 | |||
270 | done: | ||
271 | ret = 1; | 271 | ret = 1; |
272 | 272 | ||
273 | err: | 273 | err: |