aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2021-04-26 17:41:43 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2021-04-26 17:41:43 +0200
commit6381f3d4f6d9ac111c2be7cfba041e8b7a28f9f9 (patch)
tree811e52506550874fe82f2fa9e85c87ec9497c76b
parent772e18775e0e1db2392dcbea970d5729018437e8 (diff)
downloadbusybox-w32-6381f3d4f6d9ac111c2be7cfba041e8b7a28f9f9.tar.gz
busybox-w32-6381f3d4f6d9ac111c2be7cfba041e8b7a28f9f9.tar.bz2
busybox-w32-6381f3d4f6d9ac111c2be7cfba041e8b7a28f9f9.zip
tls: stop passing temporary buffer address in P256 code
function old new delta sp_256_proj_point_dbl_10 435 453 +18 sp_256_ecc_mulmod_10 1300 1237 -63 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/1 up/down: 18/-63) Total: -45 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--networking/tls_sp_c32.c61
1 files changed, 26 insertions, 35 deletions
diff --git a/networking/tls_sp_c32.c b/networking/tls_sp_c32.c
index 70e20aa86..c71f716d6 100644
--- a/networking/tls_sp_c32.c
+++ b/networking/tls_sp_c32.c
@@ -170,8 +170,8 @@ static sp_digit sp_256_cmp_10(const sp_digit* a, const sp_digit* b)
170 int i; 170 int i;
171 for (i = 9; i >= 0; i--) { 171 for (i = 9; i >= 0; i--) {
172 r = a[i] - b[i]; 172 r = a[i] - b[i];
173 if (r != 0) 173 if (r != 0)
174 break; 174 break;
175 } 175 }
176 return r; 176 return r;
177} 177}
@@ -553,16 +553,15 @@ static void sp_256_mont_sqr_10(sp_digit* r, const sp_digit* a, const sp_digit* m
553 * 553 *
554 * r Inverse result. 554 * r Inverse result.
555 * a Number to invert. 555 * a Number to invert.
556 * td Temporary data.
557 */ 556 */
558/* Mod-2 for the P256 curve. */ 557/* Mod-2 for the P256 curve. */
559static const uint32_t p256_mod_2[8] = { 558static const uint32_t p256_mod_2[8] = {
560 0xfffffffd,0xffffffff,0xffffffff,0x00000000, 559 0xfffffffd,0xffffffff,0xffffffff,0x00000000,
561 0x00000000,0x00000000,0x00000001,0xffffffff, 560 0x00000000,0x00000000,0x00000001,0xffffffff,
562}; 561};
563static void sp_256_mont_inv_10(sp_digit* r, sp_digit* a, sp_digit* td) 562static void sp_256_mont_inv_10(sp_digit* r, sp_digit* a)
564{ 563{
565 sp_digit* t = td; 564 sp_digit t[2*10]; //can be just [10]?
566 int i; 565 int i;
567 566
568 memcpy(t, a, sizeof(sp_digit) * 10); 567 memcpy(t, a, sizeof(sp_digit) * 10);
@@ -578,15 +577,14 @@ static void sp_256_mont_inv_10(sp_digit* r, sp_digit* a, sp_digit* td)
578 * 577 *
579 * r Resulting affine co-ordinate point. 578 * r Resulting affine co-ordinate point.
580 * p Montgomery form projective co-ordinate point. 579 * p Montgomery form projective co-ordinate point.
581 * t Temporary ordinate data.
582 */ 580 */
583static void sp_256_map_10(sp_point* r, sp_point* p, sp_digit* t) 581static void sp_256_map_10(sp_point* r, sp_point* p)
584{ 582{
585 sp_digit* t1 = t; 583 sp_digit t1[2*10];
586 sp_digit* t2 = t + 2*10; 584 sp_digit t2[2*10];
587 int32_t n; 585 int32_t n;
588 586
589 sp_256_mont_inv_10(t1, p->z, t + 2*10); 587 sp_256_mont_inv_10(t1, p->z);
590 588
591 sp_256_mont_sqr_10(t2, t1, p256_mod, p256_mp_mod); 589 sp_256_mont_sqr_10(t2, t1, p256_mod, p256_mp_mod);
592 sp_256_mont_mul_10(t1, t2, t1, p256_mod, p256_mp_mod); 590 sp_256_mont_mul_10(t1, t2, t1, p256_mod, p256_mp_mod);
@@ -617,21 +615,20 @@ static void sp_256_map_10(sp_point* r, sp_point* p, sp_digit* t)
617 * 615 *
618 * r Result of doubling point. 616 * r Result of doubling point.
619 * p Point to double. 617 * p Point to double.
620 * t Temporary ordinate data.
621 */ 618 */
622static void sp_256_proj_point_dbl_10(sp_point* r, sp_point* p, sp_digit* t) 619static void sp_256_proj_point_dbl_10(sp_point* r, sp_point* p)
623{ 620{
624 sp_point tp; 621 sp_point tp;
625 sp_digit* t1 = t; 622 sp_digit t1[2*10];
626 sp_digit* t2 = t + 2*10; 623 sp_digit t2[2*10];
627 624
628 /* Put point to double into result */ 625 /* Put point to double into result */
629 if (r != p) 626 if (r != p)
630 *r = *p; /* struct copy */ 627 *r = *p; /* struct copy */
631 628
632 if (r->infinity) { 629 if (r->infinity) {
633 /* If infinity, don't double (work on dummy value) */ 630 /* If infinity, don't double (work on dummy value) */
634 r = &tp; 631 r = &tp;
635 } 632 }
636 /* T1 = Z * Z */ 633 /* T1 = Z * Z */
637 sp_256_mont_sqr_10(t1, r->z, p256_mod, p256_mp_mod); 634 sp_256_mont_sqr_10(t1, r->z, p256_mod, p256_mp_mod);
@@ -676,16 +673,14 @@ static void sp_256_proj_point_dbl_10(sp_point* r, sp_point* p, sp_digit* t)
676 * r Result of addition. 673 * r Result of addition.
677 * p Frist point to add. 674 * p Frist point to add.
678 * q Second point to add. 675 * q Second point to add.
679 * t Temporary ordinate data.
680 */ 676 */
681static void sp_256_proj_point_add_10(sp_point* r, sp_point* p, sp_point* q, 677static void sp_256_proj_point_add_10(sp_point* r, sp_point* p, sp_point* q)
682 sp_digit* t)
683{ 678{
684 sp_digit* t1 = t; 679 sp_digit t1[2*10];
685 sp_digit* t2 = t + 2*10; 680 sp_digit t2[2*10];
686 sp_digit* t3 = t + 4*10; 681 sp_digit t3[2*10];
687 sp_digit* t4 = t + 6*10; 682 sp_digit t4[2*10];
688 sp_digit* t5 = t + 8*10; 683 sp_digit t5[2*10];
689 684
690 /* Ensure only the first point is the same as the result. */ 685 /* Ensure only the first point is the same as the result. */
691 if (q == r) { 686 if (q == r) {
@@ -701,7 +696,7 @@ static void sp_256_proj_point_add_10(sp_point* r, sp_point* p, sp_point* q,
701 && sp_256_cmp_equal_10(p->z, q->z) 696 && sp_256_cmp_equal_10(p->z, q->z)
702 && (sp_256_cmp_equal_10(p->y, q->y) || sp_256_cmp_equal_10(p->y, t1)) 697 && (sp_256_cmp_equal_10(p->y, q->y) || sp_256_cmp_equal_10(p->y, t1))
703 ) { 698 ) {
704 sp_256_proj_point_dbl_10(r, p, t); 699 sp_256_proj_point_dbl_10(r, p);
705 } 700 }
706 else { 701 else {
707 sp_point tp; 702 sp_point tp;
@@ -762,7 +757,6 @@ static void sp_256_ecc_mulmod_10(sp_point* r, const sp_point* g, const sp_digit*
762{ 757{
763 enum { map = 1 }; /* we always convert result to affine coordinates */ 758 enum { map = 1 }; /* we always convert result to affine coordinates */
764 sp_point t[3]; 759 sp_point t[3];
765 sp_digit tmp[2 * 10 * 5];
766 sp_digit n; 760 sp_digit n;
767 int i; 761 int i;
768 int c, y; 762 int c, y;
@@ -791,20 +785,17 @@ static void sp_256_ecc_mulmod_10(sp_point* r, const sp_point* g, const sp_digit*
791 y = (n >> 25) & 1; 785 y = (n >> 25) & 1;
792 n <<= 1; 786 n <<= 1;
793 787
794//FIXME: what's "tmp" and why do we pass it down? 788 sp_256_proj_point_add_10(&t[y^1], &t[0], &t[1]);
795//is it scratch space for "sensitive" data, to be memset(0) after we are done?
796 sp_256_proj_point_add_10(&t[y^1], &t[0], &t[1], tmp);
797 memcpy(&t[2], &t[y], sizeof(sp_point)); 789 memcpy(&t[2], &t[y], sizeof(sp_point));
798 sp_256_proj_point_dbl_10(&t[2], &t[2], tmp); 790 sp_256_proj_point_dbl_10(&t[2], &t[2]);
799 memcpy(&t[y], &t[2], sizeof(sp_point)); 791 memcpy(&t[y], &t[2], sizeof(sp_point));
800 } 792 }
801 793
802 if (map) 794 if (map)
803 sp_256_map_10(r, &t[0], tmp); 795 sp_256_map_10(r, &t[0]);
804 else 796 else
805 memcpy(r, &t[0], sizeof(sp_point)); 797 memcpy(r, &t[0], sizeof(sp_point));
806 798
807 memset(tmp, 0, sizeof(tmp)); //paranoia
808 memset(t, 0, sizeof(t)); //paranoia 799 memset(t, 0, sizeof(t)); //paranoia
809} 800}
810 801
@@ -817,7 +808,7 @@ static void sp_256_ecc_mulmod_10(sp_point* r, const sp_point* g, const sp_digit*
817 */ 808 */
818static void sp_256_ecc_mulmod_base_10(sp_point* r, sp_digit* k /*, int map*/) 809static void sp_256_ecc_mulmod_base_10(sp_point* r, sp_digit* k /*, int map*/)
819{ 810{
820 sp_256_ecc_mulmod_10(r, &p256_base, k /*, map*/); 811 sp_256_ecc_mulmod_10(r, &p256_base, k /*, map*/);
821} 812}
822 813
823/* Multiply the point by the scalar and serialize the X ordinate. 814/* Multiply the point by the scalar and serialize the X ordinate.