@@ -571,6 +571,30 @@ static const struct {
571
571
#endif
572
572
};
573
573
574
+ static int _find_order_for_modulus (void * modulus , void * * order )
575
+ {
576
+ void * bn ;
577
+ int err ;
578
+ const ltc_ecc_curve * curve ;
579
+
580
+ if ((err = mp_init (& bn )) != CRYPT_OK ) return err ;;
581
+ for (curve = ltc_ecc_curves ; curve -> prime != NULL ; curve ++ ) {
582
+ if (mp_read_radix (bn , curve -> prime , 16 ) != CRYPT_OK ) continue ;
583
+ if (mp_cmp (bn , modulus ) != LTC_MP_EQ ) continue ;
584
+ break ; /* found */
585
+ }
586
+ mp_clear (bn );
587
+ if (curve -> order == NULL ) return CRYPT_ERROR ;
588
+ if ((err = mp_init (order )) != CRYPT_OK ) {
589
+ return err ;
590
+ }
591
+ if ((err = mp_read_radix (* order , curve -> order , 16 )) != CRYPT_OK ) {
592
+ mp_clear (order );
593
+ return err ;
594
+ }
595
+ return CRYPT_OK ;
596
+ }
597
+
574
598
/* find a hole and free as required, return -1 if no hole found */
575
599
static int _find_hole (void )
576
600
{
@@ -608,7 +632,7 @@ static int _find_hole(void)
608
632
}
609
633
610
634
/* determine if a base is already in the cache and if so, where */
611
- static int _find_base (ecc_point * g )
635
+ static int _find_base (const ecc_point * g )
612
636
{
613
637
int x ;
614
638
for (x = 0 ; x < FP_ENTRIES ; x ++ ) {
@@ -626,7 +650,7 @@ static int _find_base(ecc_point *g)
626
650
}
627
651
628
652
/* add a new base to the cache */
629
- static int _add_entry (int idx , ecc_point * g )
653
+ static int _add_entry (int idx , const ecc_point * g )
630
654
{
631
655
unsigned x , y ;
632
656
@@ -668,7 +692,7 @@ static int _add_entry(int idx, ecc_point *g)
668
692
* The algorithm builds patterns in increasing bit order by first making all
669
693
* single bit input patterns, then all two bit input patterns and so on
670
694
*/
671
- static int _build_lut (int idx , void * a , void * modulus , void * mp , void * mu )
695
+ static int _build_lut (int idx , void * ma , void * modulus , void * mp , void * mu )
672
696
{
673
697
unsigned x , y , err , bitlen , lut_gap ;
674
698
void * tmp ;
@@ -707,7 +731,7 @@ static int _build_lut(int idx, void *a, void *modulus, void *mp, void *mu)
707
731
708
732
/* now double it bitlen/FP_LUT times */
709
733
for (y = 0 ; y < lut_gap ; y ++ ) {
710
- if ((err = ltc_mp .ecc_ptdbl (fp_cache [idx ].LUT [1 <<x ], fp_cache [idx ].LUT [1 <<x ], a , modulus , mp )) != CRYPT_OK ) {
734
+ if ((err = ltc_mp .ecc_ptdbl (fp_cache [idx ].LUT [1 <<x ], fp_cache [idx ].LUT [1 <<x ], ma , modulus , mp )) != CRYPT_OK ) {
711
735
goto ERR ;
712
736
}
713
737
}
@@ -720,7 +744,7 @@ static int _build_lut(int idx, void *a, void *modulus, void *mp, void *mu)
720
744
721
745
/* perform the add */
722
746
if ((err = ltc_mp .ecc_ptadd (fp_cache [idx ].LUT [lut_orders [y ].terma ], fp_cache [idx ].LUT [lut_orders [y ].termb ],
723
- fp_cache [idx ].LUT [y ], a , modulus , mp )) != CRYPT_OK ) {
747
+ fp_cache [idx ].LUT [y ], ma , modulus , mp )) != CRYPT_OK ) {
724
748
goto ERR ;
725
749
}
726
750
}
@@ -747,9 +771,8 @@ static int _build_lut(int idx, void *a, void *modulus, void *mp, void *mu)
747
771
/* fix y */
748
772
if ((err = mp_mulmod (fp_cache [idx ].LUT [x ]-> y , tmp , modulus , fp_cache [idx ].LUT [x ]-> y )) != CRYPT_OK ) { goto ERR ; }
749
773
750
- /* free z */
751
- mp_clear (fp_cache [idx ].LUT [x ]-> z );
752
- fp_cache [idx ].LUT [x ]-> z = NULL ;
774
+ /* fix z */
775
+ if ((err = mp_set (fp_cache [idx ].LUT [x ]-> z , 1 )) != CRYPT_OK ) { goto ERR ; }
753
776
}
754
777
mp_clear (tmp );
755
778
@@ -775,7 +798,7 @@ static int _build_lut(int idx, void *a, void *modulus, void *mp, void *mu)
775
798
}
776
799
777
800
/* perform a fixed point ECC mulmod */
778
- static int _accel_fp_mul (int idx , void * k , ecc_point * R , void * a , void * modulus , void * mp , int map )
801
+ static int _accel_fp_mul (int idx , void * k , ecc_point * R , void * ma , void * modulus , void * mp , int map )
779
802
{
780
803
unsigned char kb [128 ];
781
804
int x ;
@@ -785,19 +808,7 @@ static int _accel_fp_mul(int idx, void *k, ecc_point *R, void *a, void *modulus,
785
808
/* if it's smaller than modulus we fine */
786
809
if (mp_unsigned_bin_size (k ) > mp_unsigned_bin_size (modulus )) {
787
810
/* find order */
788
- y = mp_unsigned_bin_size (modulus );
789
- for (x = 0 ; ltc_ecc_sets [x ].size ; x ++ ) {
790
- if (y <= (unsigned )ltc_ecc_sets [x ].size ) break ;
791
- }
792
-
793
- /* back off if we are on the 521 bit curve */
794
- if (y == 66 ) -- x ;
795
-
796
- if ((err = mp_init (& order )) != CRYPT_OK ) {
797
- return err ;
798
- }
799
- if ((err = mp_read_radix (order , ltc_ecc_sets [x ].order , 16 )) != CRYPT_OK ) {
800
- mp_clear (& order );
811
+ if ((err = _find_order_for_modulus (modulus , & order )) != CRYPT_OK ) {
801
812
return err ;
802
813
}
803
814
@@ -868,14 +879,14 @@ static int _accel_fp_mul(int idx, void *k, ecc_point *R, void *a, void *modulus,
868
879
869
880
/* double if not first */
870
881
if (!first ) {
871
- if ((err = ltc_mp .ecc_ptdbl (R , R , a , modulus , mp )) != CRYPT_OK ) {
882
+ if ((err = ltc_mp .ecc_ptdbl (R , R , ma , modulus , mp )) != CRYPT_OK ) {
872
883
return err ;
873
884
}
874
885
}
875
886
876
887
/* add if not first, otherwise copy */
877
888
if (!first && z ) {
878
- if ((err = ltc_mp .ecc_ptadd (R , fp_cache [idx ].LUT [z ], R , a , modulus , mp )) != CRYPT_OK ) {
889
+ if ((err = ltc_mp .ecc_ptadd (R , fp_cache [idx ].LUT [z ], R , ma , modulus , mp )) != CRYPT_OK ) {
879
890
return err ;
880
891
}
881
892
} else if (z ) {
@@ -910,19 +921,7 @@ static int _accel_fp_mul2add(int idx1, int idx2,
910
921
/* if it's smaller than modulus we fine */
911
922
if (mp_unsigned_bin_size (kA ) > mp_unsigned_bin_size (modulus )) {
912
923
/* find order */
913
- y = mp_unsigned_bin_size (modulus );
914
- for (x = 0 ; ltc_ecc_sets [x ].size ; x ++ ) {
915
- if (y <= (unsigned )ltc_ecc_sets [x ].size ) break ;
916
- }
917
-
918
- /* back off if we are on the 521 bit curve */
919
- if (y == 66 ) -- x ;
920
-
921
- if ((err = mp_init (& order )) != CRYPT_OK ) {
922
- return err ;
923
- }
924
- if ((err = mp_read_radix (order , ltc_ecc_sets [x ].order , 16 )) != CRYPT_OK ) {
925
- mp_clear (& order );
924
+ if ((err = _find_order_for_modulus (modulus , & order )) != CRYPT_OK ) {
926
925
return err ;
927
926
}
928
927
@@ -948,19 +947,7 @@ static int _accel_fp_mul2add(int idx1, int idx2,
948
947
/* if it's smaller than modulus we fine */
949
948
if (mp_unsigned_bin_size (kB ) > mp_unsigned_bin_size (modulus )) {
950
949
/* find order */
951
- y = mp_unsigned_bin_size (modulus );
952
- for (x = 0 ; ltc_ecc_sets [x ].size ; x ++ ) {
953
- if (y <= (unsigned )ltc_ecc_sets [x ].size ) break ;
954
- }
955
-
956
- /* back off if we are on the 521 bit curve */
957
- if (y == 66 ) -- x ;
958
-
959
- if ((err = mp_init (& order )) != CRYPT_OK ) {
960
- return err ;
961
- }
962
- if ((err = mp_read_radix (order , ltc_ecc_sets [x ].order , 16 )) != CRYPT_OK ) {
963
- mp_clear (& order );
950
+ if ((err = _find_order_for_modulus (modulus , & order )) != CRYPT_OK ) {
964
951
return err ;
965
952
}
966
953
@@ -1105,14 +1092,15 @@ static int _accel_fp_mul2add(int idx1, int idx2,
1105
1092
@param B Second point to multiply
1106
1093
@param kB What to multiple B by
1107
1094
@param C [out] Destination point (can overlap with A or B)
1095
+ @param ma ECC curve parameter a in montgomery form
1108
1096
@param modulus Modulus for curve
1109
1097
@return CRYPT_OK on success
1110
1098
*/
1111
- int ltc_ecc_fp_mul2add (ecc_point * A , void * kA ,
1112
- ecc_point * B , void * kB ,
1113
- ecc_point * C ,
1114
- void * a ,
1115
- void * modulus )
1099
+ int ltc_ecc_fp_mul2add (const ecc_point * A , void * kA ,
1100
+ const ecc_point * B , void * kB ,
1101
+ ecc_point * C ,
1102
+ void * ma ,
1103
+ void * modulus )
1116
1104
{
1117
1105
int idx1 , idx2 , err ;
1118
1106
void * mp , * mu ;
@@ -1168,7 +1156,7 @@ int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
1168
1156
}
1169
1157
1170
1158
/* build the LUT */
1171
- if ((err = _build_lut (idx1 , a , modulus , mp , mu )) != CRYPT_OK ) {
1159
+ if ((err = _build_lut (idx1 , ma , modulus , mp , mu )) != CRYPT_OK ) {
1172
1160
goto LBL_ERR ;;
1173
1161
}
1174
1162
}
@@ -1189,7 +1177,7 @@ int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
1189
1177
}
1190
1178
1191
1179
/* build the LUT */
1192
- if ((err = _build_lut (idx2 , a , modulus , mp , mu )) != CRYPT_OK ) {
1180
+ if ((err = _build_lut (idx2 , ma , modulus , mp , mu )) != CRYPT_OK ) {
1193
1181
goto LBL_ERR ;;
1194
1182
}
1195
1183
}
@@ -1200,9 +1188,9 @@ int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
1200
1188
/* compute mp */
1201
1189
if ((err = mp_montgomery_setup (modulus , & mp )) != CRYPT_OK ) { goto LBL_ERR ; }
1202
1190
}
1203
- err = _accel_fp_mul2add (idx1 , idx2 , kA , kB , C , a , modulus , mp );
1191
+ err = _accel_fp_mul2add (idx1 , idx2 , kA , kB , C , ma , modulus , mp );
1204
1192
} else {
1205
- err = ltc_ecc_mul2add (A , kA , B , kB , C , a , modulus );
1193
+ err = ltc_ecc_mul2add (A , kA , B , kB , C , ma , modulus );
1206
1194
}
1207
1195
LBL_ERR :
1208
1196
LTC_MUTEX_UNLOCK (& ltc_ecc_fp_lock );
@@ -1220,12 +1208,12 @@ int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
1220
1208
@param k The multiplicand
1221
1209
@param G Base point to multiply
1222
1210
@param R [out] Destination of product
1223
- @param a ECC curve parameter a
1211
+ @param ma ECC curve parameter a in montgomery form
1224
1212
@param modulus The modulus for the curve
1225
1213
@param map [boolean] If non-zero maps the point back to affine co-ordinates, otherwise it's left in jacobian-montgomery form
1226
1214
@return CRYPT_OK if successful
1227
1215
*/
1228
- int ltc_ecc_fp_mulmod (void * k , ecc_point * G , ecc_point * R , void * a , void * modulus , int map )
1216
+ int ltc_ecc_fp_mulmod (void * k , const ecc_point * G , ecc_point * R , void * ma , void * modulus , int map )
1229
1217
{
1230
1218
int idx , err ;
1231
1219
void * mp , * mu ;
@@ -1267,7 +1255,7 @@ int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulu
1267
1255
}
1268
1256
1269
1257
/* build the LUT */
1270
- if ((err = _build_lut (idx , a , modulus , mp , mu )) != CRYPT_OK ) {
1258
+ if ((err = _build_lut (idx , ma , modulus , mp , mu )) != CRYPT_OK ) {
1271
1259
goto LBL_ERR ;;
1272
1260
}
1273
1261
}
@@ -1277,9 +1265,9 @@ int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulu
1277
1265
/* compute mp */
1278
1266
if ((err = mp_montgomery_setup (modulus , & mp )) != CRYPT_OK ) { goto LBL_ERR ; }
1279
1267
}
1280
- err = _accel_fp_mul (idx , k , R , a , modulus , mp , map );
1268
+ err = _accel_fp_mul (idx , k , R , ma , modulus , mp , map );
1281
1269
} else {
1282
- err = ltc_ecc_mulmod (k , G , R , a , modulus , map );
1270
+ err = ltc_ecc_mulmod (k , G , R , ma , modulus , map );
1283
1271
}
1284
1272
LBL_ERR :
1285
1273
LTC_MUTEX_UNLOCK (& ltc_ecc_fp_lock );
@@ -1329,7 +1317,7 @@ void ltc_ecc_fp_free(void)
1329
1317
@return CRYPT_OK on success
1330
1318
*/
1331
1319
int
1332
- ltc_ecc_fp_add_point (ecc_point * g , void * modulus , int lock )
1320
+ ltc_ecc_fp_add_point (const ecc_point * g , void * ma , void * modulus , int lock )
1333
1321
{
1334
1322
int idx ;
1335
1323
int err ;
@@ -1366,7 +1354,7 @@ ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock)
1366
1354
}
1367
1355
1368
1356
/* build the LUT */
1369
- if ((err = _build_lut (idx , a , modulus , mp , mu )) != CRYPT_OK ) {
1357
+ if ((err = _build_lut (idx , ma , modulus , mp , mu )) != CRYPT_OK ) {
1370
1358
goto LBL_ERR ;
1371
1359
}
1372
1360
fp_cache [idx ].lru_count = 2 ;
0 commit comments