diff options
author | tb <> | 2025-01-11 14:53:46 +0000 |
---|---|---|
committer | tb <> | 2025-01-11 14:53:46 +0000 |
commit | 793c33000da18d1042676e579534a57987870576 (patch) | |
tree | 1140e408ee01fbbd333d39f2e033dd8aa7f904ce | |
parent | a1c7b99a47688922533665b476a7ba6b6ca0ed74 (diff) | |
download | openbsd-793c33000da18d1042676e579534a57987870576.tar.gz openbsd-793c33000da18d1042676e579534a57987870576.tar.bz2 openbsd-793c33000da18d1042676e579534a57987870576.zip |
Rework ec_point_set_compressed_coordinates()
While this is nicely done, it is a bit too clever. We can do the
calculation in the normal domain rather than the Montgomery domain
and this way the method becomes method agnostic. This will be a bit
slower but since a couple of field operations are nothing compared
to the cost of BN_mod_sqrt() this isn't a concern.
ok jsing
-rw-r--r-- | src/lib/libcrypto/ec/ecp_methods.c | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/src/lib/libcrypto/ec/ecp_methods.c b/src/lib/libcrypto/ec/ecp_methods.c index 9ee5da43e1..57efce0366 100644 --- a/src/lib/libcrypto/ec/ecp_methods.c +++ b/src/lib/libcrypto/ec/ecp_methods.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: ecp_methods.c,v 1.29 2025/01/11 14:48:20 tb Exp $ */ | 1 | /* $OpenBSD: ecp_methods.c,v 1.30 2025/01/11 14:53:46 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. |
@@ -279,14 +279,19 @@ static int | |||
279 | ec_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point, | 279 | ec_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point, |
280 | const BIGNUM *in_x, int y_bit, BN_CTX *ctx) | 280 | const BIGNUM *in_x, int y_bit, BN_CTX *ctx) |
281 | { | 281 | { |
282 | const BIGNUM *p = group->p, *a = group->a, *b = group->b; | 282 | BIGNUM *p, *a, *b, *w, *x, *y; |
283 | BIGNUM *w, *x, *y; | ||
284 | int ret = 0; | 283 | int ret = 0; |
285 | 284 | ||
286 | y_bit = (y_bit != 0); | 285 | y_bit = (y_bit != 0); |
287 | 286 | ||
288 | BN_CTX_start(ctx); | 287 | BN_CTX_start(ctx); |
289 | 288 | ||
289 | if ((p = BN_CTX_get(ctx)) == NULL) | ||
290 | goto err; | ||
291 | if ((a = BN_CTX_get(ctx)) == NULL) | ||
292 | goto err; | ||
293 | if ((b = BN_CTX_get(ctx)) == NULL) | ||
294 | goto err; | ||
290 | if ((w = BN_CTX_get(ctx)) == NULL) | 295 | if ((w = BN_CTX_get(ctx)) == NULL) |
291 | goto err; | 296 | goto err; |
292 | if ((x = BN_CTX_get(ctx)) == NULL) | 297 | if ((x = BN_CTX_get(ctx)) == NULL) |
@@ -299,19 +304,17 @@ ec_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point, | |||
299 | * square roots of x^3 + ax + b. The y-bit indicates which one. | 304 | * square roots of x^3 + ax + b. The y-bit indicates which one. |
300 | */ | 305 | */ |
301 | 306 | ||
307 | if (!EC_GROUP_get_curve(group, p, a, b, ctx)) | ||
308 | goto err; | ||
309 | |||
302 | /* XXX - should we not insist on 0 <= x < p instead? */ | 310 | /* XXX - should we not insist on 0 <= x < p instead? */ |
303 | if (!BN_nnmod(x, in_x, p, ctx)) | 311 | if (!BN_nnmod(x, in_x, p, ctx)) |
304 | goto err; | 312 | goto err; |
305 | 313 | ||
306 | if (group->meth->field_encode != NULL) { | ||
307 | if (!group->meth->field_encode(group, x, x, ctx)) | ||
308 | goto err; | ||
309 | } | ||
310 | |||
311 | /* y = x^3 */ | 314 | /* y = x^3 */ |
312 | if (!group->meth->field_sqr(group, y, x, ctx)) | 315 | if (!BN_mod_sqr(y, x, p, ctx)) |
313 | goto err; | 316 | goto err; |
314 | if (!group->meth->field_mul(group, y, y, x, ctx)) | 317 | if (!BN_mod_mul(y, y, x, p, ctx)) |
315 | goto err; | 318 | goto err; |
316 | 319 | ||
317 | /* y += ax */ | 320 | /* y += ax */ |
@@ -323,7 +326,7 @@ ec_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point, | |||
323 | if (!BN_mod_sub_quick(y, y, w, p)) | 326 | if (!BN_mod_sub_quick(y, y, w, p)) |
324 | goto err; | 327 | goto err; |
325 | } else { | 328 | } else { |
326 | if (!group->meth->field_mul(group, w, a, x, ctx)) | 329 | if (!BN_mod_mul(w, a, x, p, ctx)) |
327 | goto err; | 330 | goto err; |
328 | if (!BN_mod_add_quick(y, y, w, p)) | 331 | if (!BN_mod_add_quick(y, y, w, p)) |
329 | goto err; | 332 | goto err; |
@@ -333,13 +336,6 @@ ec_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point, | |||
333 | if (!BN_mod_add_quick(y, y, b, p)) | 336 | if (!BN_mod_add_quick(y, y, b, p)) |
334 | goto err; | 337 | goto err; |
335 | 338 | ||
336 | if (group->meth->field_decode != NULL) { | ||
337 | if (!group->meth->field_decode(group, x, x, ctx)) | ||
338 | goto err; | ||
339 | if (!group->meth->field_decode(group, y, y, ctx)) | ||
340 | goto err; | ||
341 | } | ||
342 | |||
343 | if (!BN_mod_sqrt(y, y, p, ctx)) { | 339 | if (!BN_mod_sqrt(y, y, p, ctx)) { |
344 | ECerror(EC_R_INVALID_COMPRESSED_POINT); | 340 | ECerror(EC_R_INVALID_COMPRESSED_POINT); |
345 | goto err; | 341 | goto err; |