summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ec2_smpl.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/ec/ec2_smpl.c')
-rw-r--r--src/lib/libcrypto/ec/ec2_smpl.c78
1 files changed, 25 insertions, 53 deletions
diff --git a/src/lib/libcrypto/ec/ec2_smpl.c b/src/lib/libcrypto/ec/ec2_smpl.c
index 1ad339cbd7..850159cb25 100644
--- a/src/lib/libcrypto/ec/ec2_smpl.c
+++ b/src/lib/libcrypto/ec/ec2_smpl.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec2_smpl.c,v 1.34 2023/03/27 10:25:02 tb Exp $ */ 1/* $OpenBSD: ec2_smpl.c,v 1.35 2023/04/11 18:58:20 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4 * 4 *
@@ -216,18 +216,11 @@ ec_GF2m_simple_group_get_degree(const EC_GROUP *group)
216static int 216static int
217ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) 217ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
218{ 218{
219 int ret = 0;
220 BIGNUM *b; 219 BIGNUM *b;
221 BN_CTX *new_ctx = NULL; 220 int ret = 0;
222 221
223 if (ctx == NULL) {
224 ctx = new_ctx = BN_CTX_new();
225 if (ctx == NULL) {
226 ECerror(ERR_R_MALLOC_FAILURE);
227 goto err;
228 }
229 }
230 BN_CTX_start(ctx); 222 BN_CTX_start(ctx);
223
231 if ((b = BN_CTX_get(ctx)) == NULL) 224 if ((b = BN_CTX_get(ctx)) == NULL)
232 goto err; 225 goto err;
233 226
@@ -244,9 +237,8 @@ ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
244 ret = 1; 237 ret = 1;
245 238
246 err: 239 err:
247 if (ctx != NULL) 240 BN_CTX_end(ctx);
248 BN_CTX_end(ctx); 241
249 BN_CTX_free(new_ctx);
250 return ret; 242 return ret;
251} 243}
252 244
@@ -368,7 +360,6 @@ static int
368ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, 360ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
369 const EC_POINT *b, BN_CTX *ctx) 361 const EC_POINT *b, BN_CTX *ctx)
370{ 362{
371 BN_CTX *new_ctx = NULL;
372 BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t; 363 BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t;
373 int ret = 0; 364 int ret = 0;
374 365
@@ -382,12 +373,9 @@ ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
382 return 0; 373 return 0;
383 return 1; 374 return 1;
384 } 375 }
385 if (ctx == NULL) { 376
386 ctx = new_ctx = BN_CTX_new();
387 if (ctx == NULL)
388 return 0;
389 }
390 BN_CTX_start(ctx); 377 BN_CTX_start(ctx);
378
391 if ((x0 = BN_CTX_get(ctx)) == NULL) 379 if ((x0 = BN_CTX_get(ctx)) == NULL)
392 goto err; 380 goto err;
393 if ((y0 = BN_CTX_get(ctx)) == NULL) 381 if ((y0 = BN_CTX_get(ctx)) == NULL)
@@ -475,7 +463,7 @@ ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
475 463
476 err: 464 err:
477 BN_CTX_end(ctx); 465 BN_CTX_end(ctx);
478 BN_CTX_free(new_ctx); 466
479 return ret; 467 return ret;
480} 468}
481 469
@@ -517,11 +505,10 @@ ec_GF2m_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
517static int 505static int
518ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx) 506ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
519{ 507{
520 int ret = -1;
521 BN_CTX *new_ctx = NULL;
522 BIGNUM *lh, *y2;
523 int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *); 508 int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
524 int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); 509 int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
510 BIGNUM *lh, *y2;
511 int ret = -1;
525 512
526 if (EC_POINT_is_at_infinity(group, point) > 0) 513 if (EC_POINT_is_at_infinity(group, point) > 0)
527 return 1; 514 return 1;
@@ -533,12 +520,8 @@ ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX
533 if (!point->Z_is_one) 520 if (!point->Z_is_one)
534 return -1; 521 return -1;
535 522
536 if (ctx == NULL) {
537 ctx = new_ctx = BN_CTX_new();
538 if (ctx == NULL)
539 return -1;
540 }
541 BN_CTX_start(ctx); 523 BN_CTX_start(ctx);
524
542 if ((y2 = BN_CTX_get(ctx)) == NULL) 525 if ((y2 = BN_CTX_get(ctx)) == NULL)
543 goto err; 526 goto err;
544 if ((lh = BN_CTX_get(ctx)) == NULL) 527 if ((lh = BN_CTX_get(ctx)) == NULL)
@@ -563,11 +546,12 @@ ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX
563 goto err; 546 goto err;
564 if (!BN_GF2m_add(lh, lh, y2)) 547 if (!BN_GF2m_add(lh, lh, y2))
565 goto err; 548 goto err;
549
566 ret = BN_is_zero(lh); 550 ret = BN_is_zero(lh);
551
567 err: 552 err:
568 if (ctx) 553 BN_CTX_end(ctx);
569 BN_CTX_end(ctx); 554
570 BN_CTX_free(new_ctx);
571 return ret; 555 return ret;
572} 556}
573 557
@@ -583,24 +567,19 @@ ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
583 const EC_POINT *b, BN_CTX *ctx) 567 const EC_POINT *b, BN_CTX *ctx)
584{ 568{
585 BIGNUM *aX, *aY, *bX, *bY; 569 BIGNUM *aX, *aY, *bX, *bY;
586 BN_CTX *new_ctx = NULL;
587 int ret = -1; 570 int ret = -1;
588 571
589 if (EC_POINT_is_at_infinity(group, a) > 0) { 572 if (EC_POINT_is_at_infinity(group, a) > 0)
590 return EC_POINT_is_at_infinity(group, b) > 0 ? 0 : 1; 573 return EC_POINT_is_at_infinity(group, b) > 0 ? 0 : 1;
591 } 574
592 if (EC_POINT_is_at_infinity(group, b) > 0) 575 if (EC_POINT_is_at_infinity(group, b) > 0)
593 return 1; 576 return 1;
594 577
595 if (a->Z_is_one && b->Z_is_one) { 578 if (a->Z_is_one && b->Z_is_one)
596 return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1; 579 return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
597 } 580
598 if (ctx == NULL) {
599 ctx = new_ctx = BN_CTX_new();
600 if (ctx == NULL)
601 return -1;
602 }
603 BN_CTX_start(ctx); 581 BN_CTX_start(ctx);
582
604 if ((aX = BN_CTX_get(ctx)) == NULL) 583 if ((aX = BN_CTX_get(ctx)) == NULL)
605 goto err; 584 goto err;
606 if ((aY = BN_CTX_get(ctx)) == NULL) 585 if ((aY = BN_CTX_get(ctx)) == NULL)
@@ -617,9 +596,8 @@ ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
617 ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1; 596 ret = ((BN_cmp(aX, bX) == 0) && BN_cmp(aY, bY) == 0) ? 0 : 1;
618 597
619 err: 598 err:
620 if (ctx) 599 BN_CTX_end(ctx);
621 BN_CTX_end(ctx); 600
622 BN_CTX_free(new_ctx);
623 return ret; 601 return ret;
624} 602}
625 603
@@ -627,19 +605,14 @@ ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
627static int 605static int
628ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) 606ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
629{ 607{
630 BN_CTX *new_ctx = NULL;
631 BIGNUM *x, *y; 608 BIGNUM *x, *y;
632 int ret = 0; 609 int ret = 0;
633 610
634 if (point->Z_is_one || EC_POINT_is_at_infinity(group, point) > 0) 611 if (point->Z_is_one || EC_POINT_is_at_infinity(group, point) > 0)
635 return 1; 612 return 1;
636 613
637 if (ctx == NULL) {
638 ctx = new_ctx = BN_CTX_new();
639 if (ctx == NULL)
640 return 0;
641 }
642 BN_CTX_start(ctx); 614 BN_CTX_start(ctx);
615
643 if ((x = BN_CTX_get(ctx)) == NULL) 616 if ((x = BN_CTX_get(ctx)) == NULL)
644 goto err; 617 goto err;
645 if ((y = BN_CTX_get(ctx)) == NULL) 618 if ((y = BN_CTX_get(ctx)) == NULL)
@@ -657,9 +630,8 @@ ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
657 ret = 1; 630 ret = 1;
658 631
659 err: 632 err:
660 if (ctx) 633 BN_CTX_end(ctx);
661 BN_CTX_end(ctx); 634
662 BN_CTX_free(new_ctx);
663 return ret; 635 return ret;
664} 636}
665 637