summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2023-07-26 12:12:13 +0000
committertb <>2023-07-26 12:12:13 +0000
commite1df32281280bf2a9fc7e0f31572601bd086f7d4 (patch)
tree5e0ce7b3e02f2f4db916460c18753ac7de6e0d9d
parent90340319d5c3aaa4aafab37d65055a6f43025434 (diff)
downloadopenbsd-e1df32281280bf2a9fc7e0f31572601bd086f7d4.tar.gz
openbsd-e1df32281280bf2a9fc7e0f31572601bd086f7d4.tar.bz2
openbsd-e1df32281280bf2a9fc7e0f31572601bd086f7d4.zip
Introduce ec_decode_scalar()
This is a helper that decodes a scalar from field-internal representation to a representation as a BIGNUM in the interval [0, p). This simplifies EC_GROUP_get_curve() and EC_POINT_get_Jprojective_coordinates() to a few obvious lines and prepares cleanup in EC_POINT_get_affine_coordinates(). ok jsing
-rw-r--r--src/lib/libcrypto/ec/ecp_smpl.c120
1 files changed, 39 insertions, 81 deletions
diff --git a/src/lib/libcrypto/ec/ecp_smpl.c b/src/lib/libcrypto/ec/ecp_smpl.c
index d270d495b8..21d0d32ef9 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.49 2023/07/26 11:58:34 tb Exp $ */ 1/* $OpenBSD: ecp_smpl.c,v 1.50 2023/07/26 12:12:13 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.
@@ -114,6 +114,18 @@ ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
114 return 1; 114 return 1;
115} 115}
116 116
117static int
118ec_decode_scalar(const EC_GROUP *group, BIGNUM *bn, const BIGNUM *x, BN_CTX *ctx)
119{
120 if (bn == NULL)
121 return 1;
122
123 if (group->meth->field_decode != NULL)
124 return group->meth->field_decode(group, bn, x, ctx);
125
126 return bn_copy(bn, x);
127}
128
117int 129int
118ec_GFp_simple_group_set_curve(EC_GROUP *group, 130ec_GFp_simple_group_set_curve(EC_GROUP *group,
119 const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) 131 const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
@@ -167,31 +179,17 @@ ec_GFp_simple_group_set_curve(EC_GROUP *group,
167} 179}
168 180
169int 181int
170ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx) 182ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
183 BIGNUM *b, BN_CTX *ctx)
171{ 184{
172 if (p != NULL) { 185 if (p != NULL) {
173 if (!bn_copy(p, &group->field)) 186 if (!bn_copy(p, &group->field))
174 return 0; 187 return 0;
175 } 188 }
176 if (group->meth->field_decode != NULL) { 189 if (!ec_decode_scalar(group, a, &group->a, ctx))
177 if (a != NULL) { 190 return 0;
178 if (!group->meth->field_decode(group, a, &group->a, ctx)) 191 if (!ec_decode_scalar(group, b, &group->b, ctx))
179 return 0; 192 return 0;
180 }
181 if (b != NULL) {
182 if (!group->meth->field_decode(group, b, &group->b, ctx))
183 return 0;
184 }
185 } else {
186 if (a != NULL) {
187 if (!bn_copy(a, &group->a))
188 return 0;
189 }
190 if (b != NULL) {
191 if (!bn_copy(b, &group->b))
192 return 0;
193 }
194 }
195 193
196 return 1; 194 return 1;
197} 195}
@@ -363,33 +361,12 @@ ec_GFp_simple_get_Jprojective_coordinates(const EC_GROUP *group,
363{ 361{
364 int ret = 0; 362 int ret = 0;
365 363
366 if (group->meth->field_decode != NULL) { 364 if (!ec_decode_scalar(group, x, &point->X, ctx))
367 if (x != NULL) { 365 goto err;
368 if (!group->meth->field_decode(group, x, &point->X, ctx)) 366 if (!ec_decode_scalar(group, y, &point->Y, ctx))
369 goto err; 367 goto err;
370 } 368 if (!ec_decode_scalar(group, z, &point->Z, ctx))
371 if (y != NULL) { 369 goto err;
372 if (!group->meth->field_decode(group, y, &point->Y, ctx))
373 goto err;
374 }
375 if (z != NULL) {
376 if (!group->meth->field_decode(group, z, &point->Z, ctx))
377 goto err;
378 }
379 } else {
380 if (x != NULL) {
381 if (!bn_copy(x, &point->X))
382 goto err;
383 }
384 if (y != NULL) {
385 if (!bn_copy(y, &point->Y))
386 goto err;
387 }
388 if (z != NULL) {
389 if (!bn_copy(z, &point->Z))
390 goto err;
391 }
392 }
393 370
394 ret = 1; 371 ret = 1;
395 372
@@ -411,11 +388,10 @@ ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group, EC_POINT *poin
411} 388}
412 389
413int 390int
414ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *point, 391ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group,
415 BIGNUM *x, BIGNUM *y, BN_CTX *ctx) 392 const EC_POINT *point, BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
416{ 393{
417 BIGNUM *Z, *Z_1, *Z_2, *Z_3; 394 BIGNUM *z, *Z, *Z_1, *Z_2, *Z_3;
418 const BIGNUM *Z_;
419 int ret = 0; 395 int ret = 0;
420 396
421 if (EC_POINT_is_at_infinity(group, point) > 0) { 397 if (EC_POINT_is_at_infinity(group, point) > 0) {
@@ -425,6 +401,8 @@ ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, const EC_POINT
425 401
426 BN_CTX_start(ctx); 402 BN_CTX_start(ctx);
427 403
404 if ((z = BN_CTX_get(ctx)) == NULL)
405 goto err;
428 if ((Z = BN_CTX_get(ctx)) == NULL) 406 if ((Z = BN_CTX_get(ctx)) == NULL)
429 goto err; 407 goto err;
430 if ((Z_1 = BN_CTX_get(ctx)) == NULL) 408 if ((Z_1 = BN_CTX_get(ctx)) == NULL)
@@ -434,38 +412,18 @@ ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, const EC_POINT
434 if ((Z_3 = BN_CTX_get(ctx)) == NULL) 412 if ((Z_3 = BN_CTX_get(ctx)) == NULL)
435 goto err; 413 goto err;
436 414
437 /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */ 415 /* Convert from projective coordinates (X, Y, Z) into (X/Z^2, Y/Z^3). */
438 416
439 if (group->meth->field_decode) { 417 if (!ec_decode_scalar(group, z, &point->Z, ctx))
440 if (!group->meth->field_decode(group, Z, &point->Z, ctx)) 418 goto err;
441 goto err;
442 Z_ = Z;
443 } else {
444 Z_ = &point->Z;
445 }
446 419
447 if (BN_is_one(Z_)) { 420 if (BN_is_one(z)) {
448 if (group->meth->field_decode) { 421 if (!ec_decode_scalar(group, x, &point->X, ctx))
449 if (x != NULL) { 422 goto err;
450 if (!group->meth->field_decode(group, x, &point->X, ctx)) 423 if (!ec_decode_scalar(group, y, &point->Y, ctx))
451 goto err; 424 goto err;
452 }
453 if (y != NULL) {
454 if (!group->meth->field_decode(group, y, &point->Y, ctx))
455 goto err;
456 }
457 } else {
458 if (x != NULL) {
459 if (!bn_copy(x, &point->X))
460 goto err;
461 }
462 if (y != NULL) {
463 if (!bn_copy(y, &point->Y))
464 goto err;
465 }
466 }
467 } else { 425 } else {
468 if (BN_mod_inverse_ct(Z_1, Z_, &group->field, ctx) == NULL) { 426 if (BN_mod_inverse_ct(Z_1, z, &group->field, ctx) == NULL) {
469 ECerror(ERR_R_BN_LIB); 427 ECerror(ERR_R_BN_LIB);
470 goto err; 428 goto err;
471 } 429 }