summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ecp_smpl.c
diff options
context:
space:
mode:
authortb <>2024-11-02 15:50:50 +0000
committertb <>2024-11-02 15:50:50 +0000
commit04277a8a70494b7b35dc16881dea60c36382073c (patch)
treeafddf8953e07c4b922c56d9d930051ace171ebfe /src/lib/libcrypto/ec/ecp_smpl.c
parent7ac9d79f59c8680854d47ab54d8cb8d38183a391 (diff)
downloadopenbsd-04277a8a70494b7b35dc16881dea60c36382073c.tar.gz
openbsd-04277a8a70494b7b35dc16881dea60c36382073c.tar.bz2
openbsd-04277a8a70494b7b35dc16881dea60c36382073c.zip
Merge compressed coordinate setting back into ecp_smpl and ec_lib
The reason these were in separate files was FIPS. Not our problem.
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/ec/ecp_smpl.c100
1 files changed, 99 insertions, 1 deletions
diff --git a/src/lib/libcrypto/ec/ecp_smpl.c b/src/lib/libcrypto/ec/ecp_smpl.c
index ab79680742..5890ca994a 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.57 2024/10/31 15:37:53 tb Exp $ */ 1/* $OpenBSD: ecp_smpl.c,v 1.58 2024/11/02 15:50:50 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.
@@ -469,6 +469,104 @@ ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group,
469} 469}
470 470
471int 471int
472ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
473 EC_POINT *point, const BIGNUM *in_x, int y_bit, BN_CTX *ctx)
474{
475 const BIGNUM *p = &group->field, *a = &group->a, *b = &group->b;
476 BIGNUM *w, *x, *y;
477 int ret = 0;
478
479 y_bit = (y_bit != 0);
480
481 BN_CTX_start(ctx);
482
483 if ((w = BN_CTX_get(ctx)) == NULL)
484 goto err;
485 if ((x = BN_CTX_get(ctx)) == NULL)
486 goto err;
487 if ((y = BN_CTX_get(ctx)) == NULL)
488 goto err;
489
490 /*
491 * Weierstrass equation: y^2 = x^3 + ax + b, so y is one of the
492 * square roots of x^3 + ax + b. The y-bit indicates which one.
493 */
494
495 /* XXX - should we not insist on 0 <= x < p instead? */
496 if (!BN_nnmod(x, in_x, p, ctx))
497 goto err;
498
499 if (group->meth->field_encode != NULL) {
500 if (!group->meth->field_encode(group, x, x, ctx))
501 goto err;
502 }
503
504 /* y = x^3 */
505 if (!group->meth->field_sqr(group, y, x, ctx))
506 goto err;
507 if (!group->meth->field_mul(group, y, y, x, ctx))
508 goto err;
509
510 /* y += ax */
511 if (group->a_is_minus3) {
512 if (!BN_mod_lshift1_quick(w, x, p))
513 goto err;
514 if (!BN_mod_add_quick(w, w, x, p))
515 goto err;
516 if (!BN_mod_sub_quick(y, y, w, p))
517 goto err;
518 } else {
519 if (!group->meth->field_mul(group, w, a, x, ctx))
520 goto err;
521 if (!BN_mod_add_quick(y, y, w, p))
522 goto err;
523 }
524
525 /* y += b */
526 if (!BN_mod_add_quick(y, y, b, p))
527 goto err;
528
529 if (group->meth->field_decode != NULL) {
530 if (!group->meth->field_decode(group, x, x, ctx))
531 goto err;
532 if (!group->meth->field_decode(group, y, y, ctx))
533 goto err;
534 }
535
536 if (!BN_mod_sqrt(y, y, p, ctx)) {
537 ECerror(EC_R_INVALID_COMPRESSED_POINT);
538 goto err;
539 }
540
541 if (y_bit == BN_is_odd(y))
542 goto done;
543
544 if (BN_is_zero(y)) {
545 ECerror(EC_R_INVALID_COMPRESSION_BIT);
546 goto err;
547 }
548 if (!BN_usub(y, &group->field, y))
549 goto err;
550
551 if (y_bit != BN_is_odd(y)) {
552 /* Can only happen if p is even and should not be reachable. */
553 ECerror(ERR_R_INTERNAL_ERROR);
554 goto err;
555 }
556
557 done:
558 if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
559 goto err;
560
561 ret = 1;
562
563 err:
564 BN_CTX_end(ctx);
565
566 return ret;
567}
568
569int
472ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx) 570ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
473{ 571{
474 int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *); 572 int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);