|
|
@@ -9,7 +9,7 @@
|
|
|
* Tom St Denis, [email protected], http://libtom.org
|
|
|
*/
|
|
|
|
|
|
-/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
|
|
+/* Implements ECC over Z/pZ for curve y^2 = x^3 + ax + b
|
|
|
*
|
|
|
* All curves taken from NIST recommendation paper of July 1999
|
|
|
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
|
|
@@ -33,10 +33,11 @@
|
|
|
@param G The base point
|
|
|
@param R [out] Destination for kG
|
|
|
@param modulus The modulus of the field the ECC curve is in
|
|
|
+ @param a The A parameter of the ECC curve
|
|
|
@param map Boolean whether to map back to affine or not (1==map, 0 == leave in projective)
|
|
|
@return CRYPT_OK on success
|
|
|
*/
|
|
|
-int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map)
|
|
|
+int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, void *a, int map)
|
|
|
{
|
|
|
ecc_point *tG, *M[8];
|
|
|
int i, j, err;
|
|
|
@@ -48,6 +49,7 @@ int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map)
|
|
|
LTC_ARGCHK(G != NULL);
|
|
|
LTC_ARGCHK(R != NULL);
|
|
|
LTC_ARGCHK(modulus != NULL);
|
|
|
+ LTC_ARGCHK(a != NULL);
|
|
|
|
|
|
/* init montgomery reduction */
|
|
|
if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) {
|
|
|
@@ -95,13 +97,13 @@ int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map)
|
|
|
|
|
|
/* calc the M tab, which holds kG for k==8..15 */
|
|
|
/* M[0] == 8G */
|
|
|
- if ((err = ltc_mp.ecc_ptdbl(tG, M[0], modulus, mp)) != CRYPT_OK) { goto done; }
|
|
|
- if ((err = ltc_mp.ecc_ptdbl(M[0], M[0], modulus, mp)) != CRYPT_OK) { goto done; }
|
|
|
- if ((err = ltc_mp.ecc_ptdbl(M[0], M[0], modulus, mp)) != CRYPT_OK) { goto done; }
|
|
|
+ if ((err = ltc_mp.ecc_ptdbl(tG, M[0], modulus, a, mp)) != CRYPT_OK) { goto done; }
|
|
|
+ if ((err = ltc_mp.ecc_ptdbl(M[0], M[0], modulus, a, mp)) != CRYPT_OK) { goto done; }
|
|
|
+ if ((err = ltc_mp.ecc_ptdbl(M[0], M[0], modulus, a, mp)) != CRYPT_OK) { goto done; }
|
|
|
|
|
|
/* now find (8+k)G for k=1..7 */
|
|
|
for (j = 9; j < 16; j++) {
|
|
|
- if ((err = ltc_mp.ecc_ptadd(M[j-9], tG, M[j-8], modulus, mp)) != CRYPT_OK) { goto done; }
|
|
|
+ if ((err = ltc_mp.ecc_ptadd(M[j-9], tG, M[j-8], modulus, a, mp)) != CRYPT_OK) { goto done; }
|
|
|
}
|
|
|
|
|
|
/* setup sliding window */
|
|
|
@@ -135,7 +137,7 @@ int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map)
|
|
|
|
|
|
/* if the bit is zero and mode == 1 then we double */
|
|
|
if (mode == 1 && i == 0) {
|
|
|
- if ((err = ltc_mp.ecc_ptdbl(R, R, modulus, mp)) != CRYPT_OK) { goto done; }
|
|
|
+ if ((err = ltc_mp.ecc_ptdbl(R, R, modulus, a, mp)) != CRYPT_OK) { goto done; }
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
@@ -156,11 +158,11 @@ int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map)
|
|
|
/* ok window is filled so double as required and add */
|
|
|
/* double first */
|
|
|
for (j = 0; j < WINSIZE; j++) {
|
|
|
- if ((err = ltc_mp.ecc_ptdbl(R, R, modulus, mp)) != CRYPT_OK) { goto done; }
|
|
|
+ if ((err = ltc_mp.ecc_ptdbl(R, R, modulus, a, mp)) != CRYPT_OK) { goto done; }
|
|
|
}
|
|
|
|
|
|
/* then add, bitbuf will be 8..15 [8..2^WINSIZE] guaranteed */
|
|
|
- if ((err = ltc_mp.ecc_ptadd(R, M[bitbuf-8], R, modulus, mp)) != CRYPT_OK) { goto done; }
|
|
|
+ if ((err = ltc_mp.ecc_ptadd(R, M[bitbuf-8], R, modulus, a, mp)) != CRYPT_OK) { goto done; }
|
|
|
}
|
|
|
/* empty window and reset */
|
|
|
bitcpy = bitbuf = 0;
|
|
|
@@ -174,7 +176,7 @@ int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map)
|
|
|
for (j = 0; j < bitcpy; j++) {
|
|
|
/* only double if we have had at least one add first */
|
|
|
if (first == 0) {
|
|
|
- if ((err = ltc_mp.ecc_ptdbl(R, R, modulus, mp)) != CRYPT_OK) { goto done; }
|
|
|
+ if ((err = ltc_mp.ecc_ptdbl(R, R, modulus, a, mp)) != CRYPT_OK) { goto done; }
|
|
|
}
|
|
|
|
|
|
bitbuf <<= 1;
|
|
|
@@ -187,7 +189,7 @@ int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map)
|
|
|
first = 0;
|
|
|
} else {
|
|
|
/* then add */
|
|
|
- if ((err = ltc_mp.ecc_ptadd(R, tG, R, modulus, mp)) != CRYPT_OK) { goto done; }
|
|
|
+ if ((err = ltc_mp.ecc_ptadd(R, tG, R, modulus, a, mp)) != CRYPT_OK) { goto done; }
|
|
|
}
|
|
|
}
|
|
|
}
|