@@ -571,6 +571,30 @@ static const struct {
571571#endif
572572};
573573
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+
574598/* find a hole and free as required, return -1 if no hole found */
575599static int _find_hole (void )
576600{
@@ -608,7 +632,7 @@ static int _find_hole(void)
608632}
609633
610634/* 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 )
612636{
613637 int x ;
614638 for (x = 0 ; x < FP_ENTRIES ; x ++ ) {
@@ -626,7 +650,7 @@ static int _find_base(ecc_point *g)
626650}
627651
628652/* 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 )
630654{
631655 unsigned x , y ;
632656
@@ -668,7 +692,7 @@ static int _add_entry(int idx, ecc_point *g)
668692 * The algorithm builds patterns in increasing bit order by first making all
669693 * single bit input patterns, then all two bit input patterns and so on
670694 */
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 )
672696{
673697 unsigned x , y , err , bitlen , lut_gap ;
674698 void * tmp ;
@@ -707,7 +731,7 @@ static int _build_lut(int idx, void *a, void *modulus, void *mp, void *mu)
707731
708732 /* now double it bitlen/FP_LUT times */
709733 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 ) {
711735 goto ERR ;
712736 }
713737 }
@@ -720,7 +744,7 @@ static int _build_lut(int idx, void *a, void *modulus, void *mp, void *mu)
720744
721745 /* perform the add */
722746 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 ) {
724748 goto ERR ;
725749 }
726750 }
@@ -747,9 +771,8 @@ static int _build_lut(int idx, void *a, void *modulus, void *mp, void *mu)
747771 /* fix y */
748772 if ((err = mp_mulmod (fp_cache [idx ].LUT [x ]-> y , tmp , modulus , fp_cache [idx ].LUT [x ]-> y )) != CRYPT_OK ) { goto ERR ; }
749773
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 ; }
753776 }
754777 mp_clear (tmp );
755778
@@ -775,7 +798,7 @@ static int _build_lut(int idx, void *a, void *modulus, void *mp, void *mu)
775798}
776799
777800/* 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 )
779802{
780803 unsigned char kb [128 ];
781804 int x ;
@@ -785,19 +808,7 @@ static int _accel_fp_mul(int idx, void *k, ecc_point *R, void *a, void *modulus,
785808 /* if it's smaller than modulus we fine */
786809 if (mp_unsigned_bin_size (k ) > mp_unsigned_bin_size (modulus )) {
787810 /* 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 ) {
801812 return err ;
802813 }
803814
@@ -868,14 +879,14 @@ static int _accel_fp_mul(int idx, void *k, ecc_point *R, void *a, void *modulus,
868879
869880 /* double if not first */
870881 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 ) {
872883 return err ;
873884 }
874885 }
875886
876887 /* add if not first, otherwise copy */
877888 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 ) {
879890 return err ;
880891 }
881892 } else if (z ) {
@@ -910,19 +921,7 @@ static int _accel_fp_mul2add(int idx1, int idx2,
910921 /* if it's smaller than modulus we fine */
911922 if (mp_unsigned_bin_size (kA ) > mp_unsigned_bin_size (modulus )) {
912923 /* 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 ) {
926925 return err ;
927926 }
928927
@@ -948,19 +947,7 @@ static int _accel_fp_mul2add(int idx1, int idx2,
948947 /* if it's smaller than modulus we fine */
949948 if (mp_unsigned_bin_size (kB ) > mp_unsigned_bin_size (modulus )) {
950949 /* 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 ) {
964951 return err ;
965952 }
966953
@@ -1105,14 +1092,15 @@ static int _accel_fp_mul2add(int idx1, int idx2,
11051092 @param B Second point to multiply
11061093 @param kB What to multiple B by
11071094 @param C [out] Destination point (can overlap with A or B)
1095+ @param ma ECC curve parameter a in montgomery form
11081096 @param modulus Modulus for curve
11091097 @return CRYPT_OK on success
11101098*/
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 )
11161104{
11171105 int idx1 , idx2 , err ;
11181106 void * mp , * mu ;
@@ -1168,7 +1156,7 @@ int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
11681156 }
11691157
11701158 /* 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 ) {
11721160 goto LBL_ERR ;;
11731161 }
11741162 }
@@ -1189,7 +1177,7 @@ int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
11891177 }
11901178
11911179 /* 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 ) {
11931181 goto LBL_ERR ;;
11941182 }
11951183 }
@@ -1200,9 +1188,9 @@ int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
12001188 /* compute mp */
12011189 if ((err = mp_montgomery_setup (modulus , & mp )) != CRYPT_OK ) { goto LBL_ERR ; }
12021190 }
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 );
12041192 } 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 );
12061194 }
12071195LBL_ERR :
12081196 LTC_MUTEX_UNLOCK (& ltc_ecc_fp_lock );
@@ -1220,12 +1208,12 @@ int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
12201208 @param k The multiplicand
12211209 @param G Base point to multiply
12221210 @param R [out] Destination of product
1223- @param a ECC curve parameter a
1211+ @param ma ECC curve parameter a in montgomery form
12241212 @param modulus The modulus for the curve
12251213 @param map [boolean] If non-zero maps the point back to affine co-ordinates, otherwise it's left in jacobian-montgomery form
12261214 @return CRYPT_OK if successful
12271215*/
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 )
12291217{
12301218 int idx , err ;
12311219 void * mp , * mu ;
@@ -1267,7 +1255,7 @@ int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulu
12671255 }
12681256
12691257 /* 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 ) {
12711259 goto LBL_ERR ;;
12721260 }
12731261 }
@@ -1277,9 +1265,9 @@ int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulu
12771265 /* compute mp */
12781266 if ((err = mp_montgomery_setup (modulus , & mp )) != CRYPT_OK ) { goto LBL_ERR ; }
12791267 }
1280- err = _accel_fp_mul (idx , k , R , a , modulus , mp , map );
1268+ err = _accel_fp_mul (idx , k , R , ma , modulus , mp , map );
12811269 } else {
1282- err = ltc_ecc_mulmod (k , G , R , a , modulus , map );
1270+ err = ltc_ecc_mulmod (k , G , R , ma , modulus , map );
12831271 }
12841272LBL_ERR :
12851273 LTC_MUTEX_UNLOCK (& ltc_ecc_fp_lock );
@@ -1329,7 +1317,7 @@ void ltc_ecc_fp_free(void)
13291317 @return CRYPT_OK on success
13301318*/
13311319int
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 )
13331321{
13341322 int idx ;
13351323 int err ;
@@ -1366,7 +1354,7 @@ ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock)
13661354 }
13671355
13681356 /* 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 ) {
13701358 goto LBL_ERR ;
13711359 }
13721360 fp_cache [idx ].lru_count = 2 ;
0 commit comments