2003-03-02 19:59:24 -05:00
|
|
|
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
|
|
|
|
*
|
|
|
|
* All curves taken from NIST recommendation paper of July 1999
|
|
|
|
* Available at http://csrc.nist.gov/cryptval/dss.htm
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mycrypt.h"
|
|
|
|
|
|
|
|
#ifdef MECC
|
|
|
|
|
2003-03-02 20:01:00 -05:00
|
|
|
/* This holds the key settings. ***MUST*** be organized by size from smallest to largest. */
|
2003-03-02 19:59:24 -05:00
|
|
|
static const struct {
|
|
|
|
int size;
|
|
|
|
char *name, *prime, *B, *order, *Gx, *Gy;
|
|
|
|
} sets[] = {
|
|
|
|
#ifdef ECC160
|
|
|
|
{
|
|
|
|
20,
|
|
|
|
"ECC-160",
|
|
|
|
/* prime */
|
2003-03-02 20:02:10 -05:00
|
|
|
"G00000000000000000000000007",
|
2003-03-02 19:59:24 -05:00
|
|
|
/* B */
|
2003-03-02 20:02:10 -05:00
|
|
|
"1oUV2vOaSlWbxr6",
|
2003-03-02 19:59:24 -05:00
|
|
|
/* order */
|
2003-03-02 20:02:10 -05:00
|
|
|
"G0000000000004sCQUtDxaqDUN5",
|
2003-03-02 19:59:24 -05:00
|
|
|
/* Gx */
|
2003-03-02 20:02:10 -05:00
|
|
|
"jpqOf1BHus6Yd/pyhyVpP",
|
2003-03-02 19:59:24 -05:00
|
|
|
/* Gy */
|
2003-03-02 20:02:10 -05:00
|
|
|
"D/wykuuIFfr+vPyx7kQEPu8MixO",
|
2003-03-02 19:59:24 -05:00
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#ifdef ECC192
|
|
|
|
{
|
|
|
|
24,
|
|
|
|
"ECC-192",
|
|
|
|
/* prime */
|
2003-03-02 20:02:10 -05:00
|
|
|
"/////////////////////l//////////",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* B */
|
2003-03-02 20:02:10 -05:00
|
|
|
"P2456UMSWESFf+chSYGmIVwutkp1Hhcn",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* order */
|
2003-03-02 20:02:10 -05:00
|
|
|
"////////////////cTxuDXHhoR6qqYWn",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* Gx */
|
2003-03-02 20:02:10 -05:00
|
|
|
"68se3h0maFPylo3hGw680FJ/2ls2/n0I",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* Gy */
|
2003-03-02 20:02:10 -05:00
|
|
|
"1nahbV/8sdXZ417jQoJDrNFvTw4UUKWH"
|
2003-03-02 19:59:24 -05:00
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#ifdef ECC224
|
|
|
|
{
|
|
|
|
28,
|
|
|
|
"ECC-224",
|
|
|
|
|
|
|
|
/* prime */
|
2003-03-02 20:02:10 -05:00
|
|
|
"400000000000000000000000000000000000BV",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* B */
|
2003-03-02 20:02:10 -05:00
|
|
|
"21HkWGL2CxJIp",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* order */
|
2003-03-02 20:02:10 -05:00
|
|
|
"4000000000000000000Kxnixk9t8MLzMiV264/",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* Gx */
|
2003-03-02 20:02:10 -05:00
|
|
|
"jpqOf1BHus6Yd/pyhyVpP",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* Gy */
|
2003-03-02 20:02:10 -05:00
|
|
|
"3FCtyo2yHA5SFjkCGbYxbOvNeChwS+j6wSIwck",
|
2003-03-02 19:59:24 -05:00
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#ifdef ECC256
|
|
|
|
{
|
|
|
|
32,
|
|
|
|
"ECC-256",
|
|
|
|
/* Prime */
|
2003-03-02 20:02:10 -05:00
|
|
|
"F////y000010000000000000000////////////////",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* B */
|
2003-03-02 20:02:10 -05:00
|
|
|
"5h6DTYgEfFdi+kzLNQOXhnb7GQmp5EmzZlEF3udqc1B",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* Order */
|
2003-03-02 20:02:10 -05:00
|
|
|
"F////y00000//////////+yvlgjfnUUXFEvoiByOoLH",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* Gx */
|
2003-03-02 20:02:10 -05:00
|
|
|
"6iNqVBXB497+BpcvMEaGF9t0ts1BUipeFIXEKNOcCAM",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* Gy */
|
2003-03-02 20:02:10 -05:00
|
|
|
"4/ZGkB+6d+RZkVhIdmFdXOhpZDNQp5UpiksG6Wtlr7r"
|
2003-03-02 19:59:24 -05:00
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#ifdef ECC384
|
|
|
|
{
|
|
|
|
48,
|
|
|
|
"ECC-384",
|
|
|
|
/* prime */
|
2003-03-02 20:02:10 -05:00
|
|
|
"//////////////////////////////////////////x/////00000000003/"
|
|
|
|
"////",
|
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
/* B */
|
2003-03-02 20:02:10 -05:00
|
|
|
"ip4lf+8+v+IOZWLhu/Wj6HWTd6x+WK4I0nG8Zr0JXrh6LZcDYYxHdIg5oEtJ"
|
|
|
|
"x2hl",
|
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
/* Order */
|
2003-03-02 20:02:10 -05:00
|
|
|
"////////////////////////////////nsDDWVGtBTzO6WsoIB2dUkpi6MhC"
|
|
|
|
"nIbp",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* Gx and Gy */
|
2003-03-02 20:02:10 -05:00
|
|
|
"geVA8hwB1JUEiSSUyo2jT6uTEsABfvkOMVT1u89KAZXL0l9TlrKfR3fKNZXo"
|
|
|
|
"TWgt",
|
|
|
|
|
|
|
|
"DXVUIfOcB6zTdfY/afBSAVZq7RqecXHywTen4xNmkC0AOB7E7Nw1dNf37NoG"
|
|
|
|
"wWvV"
|
2003-03-02 19:59:24 -05:00
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#ifdef ECC521
|
|
|
|
{
|
|
|
|
65,
|
|
|
|
"ECC-521",
|
|
|
|
/* prime */
|
2003-03-02 20:02:10 -05:00
|
|
|
"V///////////////////////////////////////////////////////////"
|
|
|
|
"///////////////////////////",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* B */
|
2003-03-02 20:02:10 -05:00
|
|
|
"56LFhbXZXoQ7vAQ8Q2sXK3kejfoMvcp5VEuj8cHZl49uLOPEL7iVfDx5bB0l"
|
|
|
|
"JknlmSrSz+8FImqyUz57zHhK3y0",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* Order */
|
2003-03-02 20:02:10 -05:00
|
|
|
"V//////////////////////////////////////////+b66XuE/BvPhVym1I"
|
|
|
|
"FS9fT0xjScuYPn7hhjljnwHE6G9",
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* Gx and Gy */
|
2003-03-02 20:02:10 -05:00
|
|
|
"CQ5ZWQt10JfpPu+osOZbRH2d6I1EGK/jI7uAAzWQqqzkg5BNdVlvrae/Xt19"
|
|
|
|
"wB/gDupIBF1XMf2c/b+VZ72vRrc",
|
|
|
|
|
|
|
|
"HWvAMfucZl015oANxGiVHlPcFL4ILURH6WNhxqN9pvcB9VkSfbUz2P0nL2v0"
|
|
|
|
"J+j1s4rF726edB2G8Y+b7QVqMPG",
|
2003-03-02 19:59:24 -05:00
|
|
|
},
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
/* you plug in a prime and B value and it finds a pseudo-random base point */
|
|
|
|
void ecc_find_base(void)
|
|
|
|
{
|
|
|
|
static char *prime = "26959946667150639794667015087019630673637144422540572481103610249951";
|
|
|
|
static char *order = "26959946667150639794667015087019637467111563745054605861463538557247";
|
|
|
|
static char *b = "9538957348957353489587";
|
|
|
|
mp_int pp, p, r, B, tmp1, tmp2, tx, ty, x, y;
|
|
|
|
char buf[4096];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mp_init_multi(&tx, &ty, &x, &y, &p, &pp, &r, &B, &tmp1, &tmp2, NULL);
|
|
|
|
mp_read_radix(&p, prime, 10);
|
|
|
|
mp_read_radix(&r, order, 10);
|
|
|
|
mp_read_radix(&B, b, 10);
|
|
|
|
|
|
|
|
/* get (p+1)/4 */
|
|
|
|
mp_add_d(&p, 1, &pp);
|
|
|
|
mp_div_2(&pp, &pp);
|
|
|
|
mp_div_2(&pp, &pp);
|
|
|
|
|
|
|
|
buf[0] = 0;
|
|
|
|
do {
|
|
|
|
printf("."); fflush(stdout);
|
|
|
|
/* make a random value of x */
|
|
|
|
for (i = 0; i < 16; i++) buf[i+1] = rand() & 255;
|
|
|
|
mp_read_raw(&x, buf, 17);
|
|
|
|
mp_copy(&x, &tx);
|
|
|
|
|
|
|
|
/* now compute x^3 - 3x + b */
|
|
|
|
mp_expt_d(&x, 3, &tmp1);
|
|
|
|
mp_mul_d(&x, 3, &tmp2);
|
|
|
|
mp_sub(&tmp1, &tmp2, &tmp1);
|
|
|
|
mp_add(&tmp1, &B, &tmp1);
|
|
|
|
mp_mod(&tmp1, &p, &tmp1);
|
|
|
|
|
|
|
|
/* now compute sqrt via x^((p+1)/4) */
|
|
|
|
mp_exptmod(&tmp1, &pp, &p, &tmp2);
|
|
|
|
mp_copy(&tmp2, &ty);
|
|
|
|
|
|
|
|
/* now square it */
|
|
|
|
mp_sqrmod(&tmp2, &p, &tmp2);
|
|
|
|
|
|
|
|
/* tmp2 should equal tmp1 */
|
|
|
|
} while (mp_cmp(&tmp1, &tmp2));
|
|
|
|
|
|
|
|
/* now output values in way that libtomcrypt wants */
|
|
|
|
mp_todecimal(&p, buf);
|
|
|
|
printf("\n\np==%s\n", buf);
|
|
|
|
mp_tohex(&B, buf);
|
|
|
|
printf("b==%s\n", buf);
|
|
|
|
mp_todecimal(&r, buf);
|
|
|
|
printf("r==%s\n", buf);
|
|
|
|
mp_tohex(&tx, buf);
|
|
|
|
printf("Gx==%s\n", buf);
|
|
|
|
mp_tohex(&ty, buf);
|
|
|
|
printf("Gy==%s\n", buf);
|
|
|
|
|
|
|
|
mp_clear_multi(&tx, &ty, &x, &y, &p, &pp, &r, &B, &tmp1, &tmp2, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int is_valid_idx(int n)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
|
2003-03-02 20:02:42 -05:00
|
|
|
for (x = 0; sets[x].size != 0; x++);
|
2003-03-02 19:59:24 -05:00
|
|
|
if ((n < 0) || (n >= x)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ecc_point *new_point(void)
|
|
|
|
{
|
|
|
|
ecc_point *p;
|
|
|
|
p = XMALLOC(sizeof(ecc_point));
|
|
|
|
if (p == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (mp_init_multi(&p->x, &p->y, NULL) != MP_OKAY) {
|
|
|
|
XFREE(p);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void del_point(ecc_point *p)
|
|
|
|
{
|
2003-03-29 13:19:07 -05:00
|
|
|
/* prevents free'ing null arguments */
|
|
|
|
if (p == NULL) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
mp_clear_multi(&p->x, &p->y, NULL);
|
|
|
|
XFREE(p);
|
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* double a point R = 2P, R can be P*/
|
2003-03-02 20:03:50 -05:00
|
|
|
static int dbl_point(ecc_point *P, ecc_point *R, mp_int *modulus, mp_int *mu)
|
2003-03-02 19:59:24 -05:00
|
|
|
{
|
|
|
|
mp_int s, tmp, tmpx;
|
|
|
|
int res;
|
2003-03-02 20:03:50 -05:00
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
if (mp_init_multi(&s, &tmp, &tmpx, NULL) != MP_OKAY) {
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* s = (3Xp^2 + a) / (2Yp) */
|
|
|
|
if (mp_mul_2(&P->y, &tmp) != MP_OKAY) { goto error; } /* tmp = 2*y */
|
|
|
|
if (mp_invmod(&tmp, modulus, &tmp) != MP_OKAY) { goto error; } /* tmp = 1/tmp mod modulus */
|
2003-03-02 20:02:10 -05:00
|
|
|
if (mp_sqr(&P->x, &s) != MP_OKAY) { goto error; } /* s = x^2 */
|
2003-03-12 21:12:16 -05:00
|
|
|
if (mp_reduce(&s, modulus, mu) != MP_OKAY) { goto error; }
|
2003-03-02 20:00:16 -05:00
|
|
|
if (mp_mul_d(&s,(mp_digit)3, &s) != MP_OKAY) { goto error; } /* s = 3*(x^2) */
|
|
|
|
if (mp_sub_d(&s,(mp_digit)3, &s) != MP_OKAY) { goto error; } /* s = 3*(x^2) - 3 */
|
2003-03-12 21:12:16 -05:00
|
|
|
if (mp_cmp_d(&s, 0) == MP_LT) { /* if s < 0 add modulus */
|
2003-03-02 20:03:50 -05:00
|
|
|
if (mp_add(&s, modulus, &s) != MP_OKAY) { goto error; }
|
|
|
|
}
|
|
|
|
if (mp_mul(&s, &tmp, &s) != MP_OKAY) { goto error; } /* s = tmp * s mod modulus */
|
2003-03-12 21:12:16 -05:00
|
|
|
if (mp_reduce(&s, modulus, mu) != MP_OKAY) { goto error; }
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* Xr = s^2 - 2Xp */
|
|
|
|
if (mp_sqr(&s, &tmpx) != MP_OKAY) { goto error; } /* tmpx = s^2 */
|
2003-03-12 21:12:16 -05:00
|
|
|
if (mp_reduce(&tmpx, modulus, mu) != MP_OKAY) { goto error; } /* tmpx = tmpx mod modulus */
|
2003-03-02 19:59:24 -05:00
|
|
|
if (mp_sub(&tmpx, &P->x, &tmpx) != MP_OKAY) { goto error; } /* tmpx = tmpx - x */
|
|
|
|
if (mp_submod(&tmpx, &P->x, modulus, &tmpx) != MP_OKAY) { goto error; } /* tmpx = tmpx - x mod modulus */
|
|
|
|
|
|
|
|
/* Yr = -Yp + s(Xp - Xr) */
|
|
|
|
if (mp_sub(&P->x, &tmpx, &tmp) != MP_OKAY) { goto error; } /* tmp = x - tmpx */
|
|
|
|
if (mp_mul(&tmp, &s, &tmp) != MP_OKAY) { goto error; } /* tmp = tmp * s */
|
|
|
|
if (mp_submod(&tmp, &P->y, modulus, &R->y) != MP_OKAY) { goto error; } /* y = tmp - y mod modulus */
|
|
|
|
if (mp_copy(&tmpx, &R->x) != MP_OKAY) { goto error; } /* x = tmpx */
|
|
|
|
|
|
|
|
res = CRYPT_OK;
|
|
|
|
goto done;
|
|
|
|
error:
|
|
|
|
res = CRYPT_MEM;
|
|
|
|
done:
|
|
|
|
mp_clear_multi(&tmpx, &tmp, &s, NULL);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add two different points over Z/pZ, R = P + Q, note R can equal either P or Q */
|
2003-03-02 20:03:50 -05:00
|
|
|
static int add_point(ecc_point *P, ecc_point *Q, ecc_point *R, mp_int *modulus, mp_int *mu)
|
2003-03-02 19:59:24 -05:00
|
|
|
{
|
|
|
|
mp_int s, tmp, tmpx;
|
|
|
|
int res;
|
2003-03-02 20:03:50 -05:00
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
if (mp_init(&tmp) != MP_OKAY) {
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* is P==Q or P==-Q? */
|
2003-03-02 20:02:42 -05:00
|
|
|
if (mp_neg(&Q->y, &tmp) != MP_OKAY || mp_mod(&tmp, modulus, &tmp) != MP_OKAY) {
|
|
|
|
mp_clear(&tmp);
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mp_cmp(&P->x, &Q->x) == MP_EQ)
|
|
|
|
if (mp_cmp(&P->y, &Q->y) == MP_EQ || mp_cmp(&P->y, &tmp) == MP_EQ) {
|
2003-03-02 19:59:24 -05:00
|
|
|
mp_clear(&tmp);
|
2003-03-02 20:03:50 -05:00
|
|
|
return dbl_point(P, R, modulus, mu);
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mp_init_multi(&tmpx, &s, NULL) != MP_OKAY) {
|
|
|
|
mp_clear(&tmp);
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get s = (Yp - Yq)/(Xp-Xq) mod p */
|
2003-03-02 20:03:50 -05:00
|
|
|
if (mp_sub(&P->x, &Q->x, &tmp) != MP_OKAY) { goto error; } /* tmp = Px - Qx mod modulus */
|
|
|
|
if (mp_cmp_d(&tmp, 0) == MP_LT) { /* if tmp<0 add modulus */
|
|
|
|
if (mp_add(&tmp, modulus, &tmp) != MP_OKAY) { goto error; }
|
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
if (mp_invmod(&tmp, modulus, &tmp) != MP_OKAY) { goto error; } /* tmp = 1/tmp mod modulus */
|
|
|
|
if (mp_sub(&P->y, &Q->y, &s) != MP_OKAY) { goto error; } /* s = Py - Qy mod modulus */
|
2003-03-02 20:03:50 -05:00
|
|
|
if (mp_cmp_d(&s, 0) == MP_LT) { /* if s<0 add modulus */
|
|
|
|
if (mp_add(&s, modulus, &s) != MP_OKAY) { goto error; }
|
|
|
|
}
|
|
|
|
if (mp_mul(&s, &tmp, &s) != MP_OKAY) { goto error; } /* s = s * tmp mod modulus */
|
2003-03-12 21:12:16 -05:00
|
|
|
if (mp_reduce(&s, modulus, mu) != MP_OKAY) { goto error; }
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* Xr = s^2 - Xp - Xq */
|
2003-03-02 20:03:50 -05:00
|
|
|
if (mp_sqr(&s, &tmp) != MP_OKAY) { goto error; } /* tmp = s^2 mod modulus */
|
2003-03-12 21:12:16 -05:00
|
|
|
if (mp_reduce(&tmp, modulus, mu) != MP_OKAY) { goto error; }
|
2003-03-02 19:59:24 -05:00
|
|
|
if (mp_sub(&tmp, &P->x, &tmp) != MP_OKAY) { goto error; } /* tmp = tmp - Px */
|
|
|
|
if (mp_sub(&tmp, &Q->x, &tmpx) != MP_OKAY) { goto error; } /* tmpx = tmp - Qx */
|
|
|
|
|
|
|
|
/* Yr = -Yp + s(Xp - Xr) */
|
|
|
|
if (mp_sub(&P->x, &tmpx, &tmp) != MP_OKAY) { goto error; } /* tmp = Px - tmpx */
|
|
|
|
if (mp_mul(&tmp, &s, &tmp) != MP_OKAY) { goto error; } /* tmp = tmp * s */
|
|
|
|
if (mp_submod(&tmp, &P->y, modulus, &R->y) != MP_OKAY) { goto error; } /* Ry = tmp - Py mod modulus */
|
|
|
|
if (mp_mod(&tmpx, modulus, &R->x) != MP_OKAY) { goto error; } /* Rx = tmpx mod modulus */
|
|
|
|
|
|
|
|
res = CRYPT_OK;
|
|
|
|
goto done;
|
|
|
|
error:
|
|
|
|
res = CRYPT_MEM;
|
|
|
|
done:
|
|
|
|
mp_clear_multi(&s, &tmpx, &tmp, NULL);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-03-29 13:19:07 -05:00
|
|
|
/* size of sliding window, don't change this! */
|
|
|
|
#define WINSIZE 4
|
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
/* perform R = kG where k == integer and G == ecc_point */
|
2003-03-02 20:02:42 -05:00
|
|
|
static int ecc_mulmod(mp_int *k, ecc_point *G, ecc_point *R, mp_int *modulus)
|
2003-03-02 19:59:24 -05:00
|
|
|
{
|
2003-03-29 13:19:07 -05:00
|
|
|
ecc_point *tG, *M[8];
|
|
|
|
int i, j, res;
|
2003-03-02 20:03:50 -05:00
|
|
|
mp_int mu;
|
2003-03-29 13:19:07 -05:00
|
|
|
mp_digit buf;
|
|
|
|
int first, bitbuf, bitcpy, bitcnt, mode, digidx;
|
2003-03-02 20:03:50 -05:00
|
|
|
|
|
|
|
/* init barrett reduction */
|
2003-03-29 13:19:07 -05:00
|
|
|
if (mp_init(&mu) != MP_OKAY) {
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
if (mp_reduce_setup(&mu, modulus) != MP_OKAY) {
|
|
|
|
mp_clear(&mu);
|
|
|
|
return CRYPT_MEM;
|
2003-03-02 20:03:50 -05:00
|
|
|
}
|
|
|
|
|
2003-03-29 13:19:07 -05:00
|
|
|
/* alloc ram for window temps */
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
M[i] = new_point();
|
|
|
|
if (M[i] == NULL) {
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
del_point(M[j]);
|
|
|
|
}
|
|
|
|
mp_clear(&mu);
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
}
|
2003-03-02 20:03:50 -05:00
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
/* make a copy of G incase R==G */
|
|
|
|
tG = new_point();
|
2003-03-29 13:19:07 -05:00
|
|
|
if (tG == NULL) { goto error; }
|
2003-03-02 19:59:24 -05:00
|
|
|
|
2003-03-29 13:19:07 -05:00
|
|
|
/* calc the M tab, which holds kG for k==8..15 */
|
|
|
|
/* M[0] == 8G */
|
2003-03-02 20:03:50 -05:00
|
|
|
if (dbl_point(G, M[0], modulus, &mu) != CRYPT_OK) { goto error; }
|
2003-03-29 13:19:07 -05:00
|
|
|
if (dbl_point(M[0], M[0], modulus, &mu) != CRYPT_OK) { goto error; }
|
|
|
|
if (dbl_point(M[0], M[0], modulus, &mu) != CRYPT_OK) { goto error; }
|
|
|
|
|
|
|
|
/* now find (8+k)G for k=1..7 */
|
|
|
|
for (j = 9; j < 16; j++) {
|
|
|
|
if (add_point(M[j-9], G, M[j-8], modulus, &mu) != CRYPT_OK) { goto error; }
|
2003-03-02 20:02:10 -05:00
|
|
|
}
|
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
/* tG = G */
|
2003-03-29 13:19:07 -05:00
|
|
|
if (mp_copy(&G->x, &tG->x) != MP_OKAY) { goto error; }
|
|
|
|
if (mp_copy(&G->y, &tG->y) != MP_OKAY) { goto error; }
|
|
|
|
|
|
|
|
/* setup sliding window */
|
|
|
|
mode = 0;
|
|
|
|
bitcnt = 1;
|
|
|
|
buf = 0;
|
|
|
|
digidx = k->used - 1;
|
|
|
|
bitcpy = bitbuf = 0;
|
|
|
|
first = 1;
|
2003-03-02 20:02:10 -05:00
|
|
|
|
2003-03-29 13:19:07 -05:00
|
|
|
/* perform ops */
|
|
|
|
for (;;) {
|
|
|
|
/* grab next digit as required */
|
|
|
|
if (--bitcnt == 0) {
|
|
|
|
if (digidx == -1) {
|
|
|
|
break;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
2003-03-29 13:19:07 -05:00
|
|
|
buf = k->dp[digidx--];
|
|
|
|
bitcnt = (int) DIGIT_BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* grab the next msb from the multiplicand */
|
|
|
|
i = (buf >> (DIGIT_BIT - 1)) & 1;
|
|
|
|
buf <<= 1;
|
|
|
|
|
|
|
|
/* skip leading zero bits */
|
|
|
|
if (mode == 0 && i == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* if the bit is zero and mode == 1 then we double */
|
|
|
|
if (mode == 1 && i == 0) {
|
|
|
|
if (dbl_point(R, R, modulus, &mu) != CRYPT_OK) { goto error; }
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* else we add it to the window */
|
|
|
|
bitbuf |= (i << (WINSIZE - ++bitcpy));
|
|
|
|
mode = 2;
|
|
|
|
|
|
|
|
if (bitcpy == WINSIZE) {
|
|
|
|
/* if this is the first window we do a simple copy */
|
|
|
|
if (first == 1) {
|
|
|
|
/* R = kG [k = first window] */
|
|
|
|
if (mp_copy(&M[bitbuf-8]->x, &R->x) != MP_OKAY) { goto error; }
|
|
|
|
if (mp_copy(&M[bitbuf-8]->y, &R->y) != MP_OKAY) { goto error; }
|
|
|
|
first = 0;
|
2003-03-02 20:02:10 -05:00
|
|
|
} else {
|
2003-03-29 13:19:07 -05:00
|
|
|
/* normal window */
|
|
|
|
/* ok window is filled so double as required and add */
|
|
|
|
/* double first */
|
|
|
|
for (j = 0; j < WINSIZE; j++) {
|
|
|
|
if (dbl_point(R, R, modulus, &mu) != CRYPT_OK) { goto error; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/* then add, bitbuf will be 1..15 [1..2^WINSIZE] guaranteed */
|
|
|
|
if (bitbuf == 1) {
|
|
|
|
if (add_point(R, tG, R, modulus, &mu) != CRYPT_OK) { goto error; }
|
|
|
|
} else {
|
|
|
|
if (add_point(R, M[bitbuf-8], R, modulus, &mu) != CRYPT_OK) { goto error; }
|
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
2003-03-29 13:19:07 -05:00
|
|
|
/* empty window and reset */
|
|
|
|
bitcpy = bitbuf = 0;
|
|
|
|
mode = 1;
|
|
|
|
}
|
|
|
|
}
|
2003-03-02 20:02:10 -05:00
|
|
|
|
2003-03-29 13:19:07 -05:00
|
|
|
/* if bits remain then double/add */
|
|
|
|
if (mode == 2 && bitcpy > 0) {
|
|
|
|
/* double then add */
|
|
|
|
for (j = 0; j < bitcpy; j++) {
|
|
|
|
/* only double if we have had at least one add first */
|
|
|
|
if (first == 0) {
|
|
|
|
if (dbl_point(R, R, modulus, &mu) != CRYPT_OK) { goto error; }
|
|
|
|
}
|
|
|
|
|
|
|
|
bitbuf <<= 1;
|
|
|
|
if ((bitbuf & (1 << WINSIZE)) != 0) {
|
|
|
|
if (first == 1){
|
|
|
|
/* first add, so copy */
|
|
|
|
if (mp_copy(&tG->x, &R->x) != MP_OKAY) { goto error; }
|
|
|
|
if (mp_copy(&tG->y, &R->y) != MP_OKAY) { goto error; }
|
|
|
|
first = 0;
|
|
|
|
} else {
|
|
|
|
/* then add */
|
|
|
|
if (add_point(R, tG, R, modulus, &mu) != CRYPT_OK) { goto error; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
res = CRYPT_OK;
|
|
|
|
goto done;
|
|
|
|
error:
|
|
|
|
res = CRYPT_MEM;
|
|
|
|
done:
|
|
|
|
del_point(tG);
|
2003-03-29 13:19:07 -05:00
|
|
|
for (i = 0; i < 8; i++) {
|
2003-03-02 20:02:10 -05:00
|
|
|
del_point(M[i]);
|
|
|
|
}
|
2003-03-02 20:03:50 -05:00
|
|
|
mp_clear(&mu);
|
2003-03-02 19:59:24 -05:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-03-29 13:19:07 -05:00
|
|
|
#undef WINSIZE
|
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
int ecc_test(void)
|
|
|
|
{
|
|
|
|
mp_int modulus, order;
|
|
|
|
ecc_point *G, *GG;
|
|
|
|
int i, res, primality;
|
|
|
|
|
|
|
|
if (mp_init_multi(&modulus, &order, NULL) != MP_OKAY) {
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
G = new_point();
|
|
|
|
if (G == NULL) {
|
|
|
|
mp_clear_multi(&modulus, &order, NULL);
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
GG = new_point();
|
|
|
|
if (GG == NULL) {
|
|
|
|
mp_clear_multi(&modulus, &order, NULL);
|
|
|
|
del_point(G);
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; sets[i].size; i++) {
|
2003-03-02 20:02:10 -05:00
|
|
|
#if 0
|
|
|
|
printf("Testing %d\n", sets[i].size);
|
|
|
|
#endif
|
|
|
|
if (mp_read_radix(&modulus, (unsigned char *)sets[i].prime, 64) != MP_OKAY) { goto error; }
|
|
|
|
if (mp_read_radix(&order, (unsigned char *)sets[i].order, 64) != MP_OKAY) { goto error; }
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* is prime actually prime? */
|
|
|
|
if (is_prime(&modulus, &primality) != CRYPT_OK) { goto error; }
|
|
|
|
if (primality == 0) {
|
|
|
|
res = CRYPT_FAIL_TESTVECTOR;
|
|
|
|
goto done1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* is order prime ? */
|
|
|
|
if (is_prime(&order, &primality) != CRYPT_OK) { goto error; }
|
|
|
|
if (primality == 0) {
|
|
|
|
res = CRYPT_FAIL_TESTVECTOR;
|
|
|
|
goto done1;
|
|
|
|
}
|
|
|
|
|
2003-03-02 20:02:10 -05:00
|
|
|
if (mp_read_radix(&G->x, (unsigned char *)sets[i].Gx, 64) != MP_OKAY) { goto error; }
|
|
|
|
if (mp_read_radix(&G->y, (unsigned char *)sets[i].Gy, 64) != MP_OKAY) { goto error; }
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* then we should have G == (order + 1)G */
|
|
|
|
if (mp_add_d(&order, 1, &order) != MP_OKAY) { goto error; }
|
2003-03-02 20:02:42 -05:00
|
|
|
if (ecc_mulmod(&order, G, GG, &modulus) != CRYPT_OK) { goto error; }
|
|
|
|
if (mp_cmp(&G->x, &GG->x) != 0 || mp_cmp(&G->y, &GG->y) != 0) {
|
2003-03-02 19:59:24 -05:00
|
|
|
res = CRYPT_FAIL_TESTVECTOR;
|
|
|
|
goto done1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = CRYPT_OK;
|
|
|
|
goto done1;
|
|
|
|
error:
|
|
|
|
res = CRYPT_MEM;
|
|
|
|
done1:
|
|
|
|
del_point(GG);
|
|
|
|
del_point(G);
|
|
|
|
mp_clear_multi(&order, &modulus, NULL);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ecc_sizes(int *low, int *high)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
_ARGCHK(low != NULL);
|
|
|
|
_ARGCHK(high != NULL);
|
|
|
|
|
|
|
|
*low = INT_MAX;
|
|
|
|
*high = 0;
|
2003-03-02 20:02:42 -05:00
|
|
|
for (i = 0; sets[i].size != 0; i++) {
|
2003-03-02 19:59:24 -05:00
|
|
|
if (sets[i].size < *low) {
|
|
|
|
*low = sets[i].size;
|
|
|
|
}
|
|
|
|
if (sets[i].size > *high) {
|
|
|
|
*high = sets[i].size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key)
|
|
|
|
{
|
2003-03-02 20:02:42 -05:00
|
|
|
int x, res, err;
|
2003-03-02 19:59:24 -05:00
|
|
|
ecc_point *base;
|
|
|
|
mp_int prime;
|
|
|
|
unsigned char buf[4096];
|
|
|
|
|
|
|
|
_ARGCHK(key != NULL);
|
|
|
|
|
|
|
|
/* good prng? */
|
2003-03-02 20:02:42 -05:00
|
|
|
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
|
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* find key size */
|
2003-03-02 20:02:42 -05:00
|
|
|
for (x = 0; (keysize > sets[x].size) && (sets[x].size != 0); x++);
|
2003-03-02 19:59:24 -05:00
|
|
|
keysize = sets[x].size;
|
|
|
|
|
|
|
|
if (sets[x].size == 0) {
|
|
|
|
return CRYPT_INVALID_KEYSIZE;
|
|
|
|
}
|
|
|
|
key->idx = x;
|
|
|
|
|
|
|
|
/* make up random string */
|
2003-03-02 20:02:42 -05:00
|
|
|
if (prng_descriptor[wprng].read(buf, (unsigned long)keysize, prng) != (unsigned long)keysize) {
|
2003-03-02 19:59:24 -05:00
|
|
|
return CRYPT_ERROR_READPRNG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup the key variables */
|
|
|
|
if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->k, &prime, NULL) != MP_OKAY) {
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
base = new_point();
|
|
|
|
if (base == NULL) {
|
|
|
|
mp_clear_multi(&key->pubkey.x, &key->pubkey.y, &key->k, &prime, NULL);
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read in the specs for this key */
|
2003-03-02 20:02:10 -05:00
|
|
|
if (mp_read_radix(&prime, (unsigned char *)sets[key->idx].prime, 64) != MP_OKAY) { goto error; }
|
|
|
|
if (mp_read_radix(&base->x, (unsigned char *)sets[key->idx].Gx, 64) != MP_OKAY) { goto error; }
|
|
|
|
if (mp_read_radix(&base->y, (unsigned char *)sets[key->idx].Gy, 64) != MP_OKAY) { goto error; }
|
2003-03-02 20:02:42 -05:00
|
|
|
if (mp_read_unsigned_bin(&key->k, (unsigned char *)buf, keysize) != MP_OKAY) { goto error; }
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* make the public key */
|
2003-03-02 20:02:42 -05:00
|
|
|
if (ecc_mulmod(&key->k, base, &key->pubkey, &prime) != CRYPT_OK) { goto error; }
|
2003-03-02 19:59:24 -05:00
|
|
|
key->type = PK_PRIVATE;
|
2003-03-02 20:02:10 -05:00
|
|
|
|
|
|
|
/* shrink key */
|
|
|
|
if (mp_shrink(&key->k) != MP_OKAY) { goto error; }
|
|
|
|
if (mp_shrink(&key->pubkey.x) != MP_OKAY) { goto error; }
|
|
|
|
if (mp_shrink(&key->pubkey.y) != MP_OKAY) { goto error; }
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* free up ram */
|
|
|
|
res = CRYPT_OK;
|
|
|
|
goto done;
|
|
|
|
error:
|
|
|
|
res = CRYPT_MEM;
|
|
|
|
done:
|
|
|
|
del_point(base);
|
|
|
|
mp_clear(&prime);
|
|
|
|
#ifdef CLEAN_STACK
|
|
|
|
zeromem(buf, sizeof(buf));
|
|
|
|
#endif
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ecc_free(ecc_key *key)
|
|
|
|
{
|
|
|
|
_ARGCHK(key != NULL);
|
|
|
|
mp_clear_multi(&key->pubkey.x, &key->pubkey.y, &key->k, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compress_y_point(ecc_point *pt, int idx, int *result)
|
|
|
|
{
|
|
|
|
mp_int tmp, tmp2, p;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
_ARGCHK(pt != NULL);
|
|
|
|
_ARGCHK(result != NULL);
|
|
|
|
|
|
|
|
if (mp_init_multi(&tmp, &tmp2, &p, NULL) != MP_OKAY) {
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get x^3 - 3x + b */
|
2003-03-02 20:02:10 -05:00
|
|
|
if (mp_read_radix(&p, (unsigned char *)sets[idx].B, 64) != MP_OKAY) { goto error; } /* p = B */
|
2003-03-02 19:59:24 -05:00
|
|
|
if (mp_expt_d(&pt->x, 3, &tmp) != MP_OKAY) { goto error; } /* tmp = pX^3 */
|
|
|
|
if (mp_mul_d(&pt->x, 3, &tmp2) != MP_OKAY) { goto error; } /* tmp2 = 3*pX^3 */
|
|
|
|
if (mp_sub(&tmp, &tmp2, &tmp) != MP_OKAY) { goto error; } /* tmp = tmp - tmp2 */
|
|
|
|
if (mp_add(&tmp, &p, &tmp) != MP_OKAY) { goto error; } /* tmp = tmp + p */
|
2003-03-02 20:02:10 -05:00
|
|
|
if (mp_read_radix(&p, (unsigned char *)sets[idx].prime, 64) != MP_OKAY) { goto error; } /* p = prime */
|
2003-03-02 19:59:24 -05:00
|
|
|
if (mp_mod(&tmp, &p, &tmp) != MP_OKAY) { goto error; } /* tmp = tmp mod p */
|
|
|
|
|
|
|
|
/* now find square root */
|
|
|
|
if (mp_add_d(&p, 1, &tmp2) != MP_OKAY) { goto error; } /* tmp2 = p + 1 */
|
|
|
|
if (mp_div_2(&tmp2, &tmp2) != MP_OKAY) { goto error; } /* tmp2 = tmp2/2 */
|
|
|
|
if (mp_div_2(&tmp2, &tmp2) != MP_OKAY) { goto error; } /* tmp2 = (p+1)/4 */
|
|
|
|
if (mp_exptmod(&tmp, &tmp2, &p, &tmp) != MP_OKAY) { goto error; } /* tmp = (x^3 - 3x + b)^((p+1)/4) mod p */
|
|
|
|
|
|
|
|
/* if tmp equals the y point give a 0, otherwise 1 */
|
2003-03-02 20:00:16 -05:00
|
|
|
if (mp_cmp(&tmp, &pt->y) == 0) {
|
2003-03-02 19:59:24 -05:00
|
|
|
*result = 0;
|
2003-03-02 20:00:16 -05:00
|
|
|
} else {
|
2003-03-02 19:59:24 -05:00
|
|
|
*result = 1;
|
2003-03-02 20:00:16 -05:00
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
res = CRYPT_OK;
|
|
|
|
goto done;
|
|
|
|
error:
|
|
|
|
res = CRYPT_MEM;
|
|
|
|
done:
|
|
|
|
mp_clear_multi(&p, &tmp, &tmp2, NULL);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int expand_y_point(ecc_point *pt, int idx, int result)
|
|
|
|
{
|
|
|
|
mp_int tmp, tmp2, p;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
_ARGCHK(pt != NULL);
|
|
|
|
|
|
|
|
if (mp_init_multi(&tmp, &tmp2, &p, NULL) != MP_OKAY) {
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get x^3 - 3x + b */
|
2003-03-02 20:02:10 -05:00
|
|
|
if (mp_read_radix(&p, (unsigned char *)sets[idx].B, 64) != MP_OKAY) { goto error; } /* p = B */
|
2003-03-02 19:59:24 -05:00
|
|
|
if (mp_expt_d(&pt->x, 3, &tmp) != MP_OKAY) { goto error; } /* tmp = pX^3 */
|
|
|
|
if (mp_mul_d(&pt->x, 3, &tmp2) != MP_OKAY) { goto error; } /* tmp2 = 3*pX^3 */
|
|
|
|
if (mp_sub(&tmp, &tmp2, &tmp) != MP_OKAY) { goto error; } /* tmp = tmp - tmp2 */
|
|
|
|
if (mp_add(&tmp, &p, &tmp) != MP_OKAY) { goto error; } /* tmp = tmp + p */
|
2003-03-02 20:02:10 -05:00
|
|
|
if (mp_read_radix(&p, (unsigned char *)sets[idx].prime, 64) != MP_OKAY) { goto error; } /* p = prime */
|
2003-03-02 19:59:24 -05:00
|
|
|
if (mp_mod(&tmp, &p, &tmp) != MP_OKAY) { goto error; } /* tmp = tmp mod p */
|
|
|
|
|
|
|
|
/* now find square root */
|
|
|
|
if (mp_add_d(&p, 1, &tmp2) != MP_OKAY) { goto error; } /* tmp2 = p + 1 */
|
|
|
|
if (mp_div_2(&tmp2, &tmp2) != MP_OKAY) { goto error; } /* tmp2 = tmp2/2 */
|
|
|
|
if (mp_div_2(&tmp2, &tmp2) != MP_OKAY) { goto error; } /* tmp2 = (p+1)/4 */
|
|
|
|
if (mp_exptmod(&tmp, &tmp2, &p, &tmp) != MP_OKAY) { goto error; } /* tmp = (x^3 - 3x + b)^((p+1)/4) mod p */
|
|
|
|
|
|
|
|
/* if result==0, then y==tmp, otherwise y==p-tmp */
|
|
|
|
if (result == 0) {
|
|
|
|
if (mp_copy(&tmp, &pt->y) != MP_OKAY) { goto error; }
|
|
|
|
} else {
|
|
|
|
if (mp_sub(&p, &tmp, &pt->y) != MP_OKAY) { goto error; }
|
|
|
|
}
|
|
|
|
|
|
|
|
res = CRYPT_OK;
|
|
|
|
goto done;
|
|
|
|
error:
|
|
|
|
res = CRYPT_MEM;
|
|
|
|
done:
|
|
|
|
mp_clear_multi(&p, &tmp, &tmp2, NULL);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OUTPUT_BIGNUM(num, buf2, y, z) \
|
|
|
|
{ \
|
2003-03-02 20:02:42 -05:00
|
|
|
z = (unsigned long)mp_unsigned_bin_size(num); \
|
2003-03-02 19:59:24 -05:00
|
|
|
STORE32L(z, buf2+y); \
|
|
|
|
y += 4; \
|
2003-03-02 20:02:42 -05:00
|
|
|
(void)mp_to_unsigned_bin(num, buf2+y); \
|
2003-03-02 19:59:24 -05:00
|
|
|
y += z; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define INPUT_BIGNUM(num, in, x, y) \
|
|
|
|
{ \
|
|
|
|
/* load value */ \
|
2003-03-02 20:01:40 -05:00
|
|
|
if (y+4 > inlen) { \
|
2003-03-02 20:02:42 -05:00
|
|
|
err = CRYPT_INVALID_PACKET; \
|
2003-03-02 20:01:40 -05:00
|
|
|
goto error; \
|
|
|
|
} \
|
2003-03-02 19:59:24 -05:00
|
|
|
LOAD32L(x, in+y); \
|
|
|
|
y += 4; \
|
|
|
|
\
|
|
|
|
/* sanity check... */ \
|
2003-03-02 20:01:40 -05:00
|
|
|
if (y+x > inlen) { \
|
2003-03-02 20:02:42 -05:00
|
|
|
err = CRYPT_INVALID_PACKET; \
|
2003-03-02 19:59:24 -05:00
|
|
|
goto error; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
/* load it */ \
|
2003-03-02 20:02:42 -05:00
|
|
|
if (mp_read_unsigned_bin(num, (unsigned char *)in+y, (int)x) != MP_OKAY) {\
|
|
|
|
err = CRYPT_MEM; \
|
2003-03-02 19:59:24 -05:00
|
|
|
goto error; \
|
|
|
|
} \
|
|
|
|
y += x; \
|
2003-03-02 20:02:10 -05:00
|
|
|
if (mp_shrink(num) != MP_OKAY) { \
|
2003-03-02 20:02:42 -05:00
|
|
|
err = CRYPT_MEM; \
|
2003-03-02 20:02:10 -05:00
|
|
|
goto error; \
|
|
|
|
} \
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key)
|
|
|
|
{
|
|
|
|
unsigned long y, z;
|
2003-03-02 20:02:42 -05:00
|
|
|
int res, err;
|
2003-03-02 19:59:24 -05:00
|
|
|
unsigned char buf2[512];
|
|
|
|
|
|
|
|
_ARGCHK(out != NULL);
|
|
|
|
_ARGCHK(outlen != NULL);
|
|
|
|
_ARGCHK(key != NULL);
|
|
|
|
|
|
|
|
/* type valid? */
|
|
|
|
if (key->type != PK_PRIVATE && type == PK_PRIVATE) {
|
|
|
|
return CRYPT_PK_TYPE_MISMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output type and magic byte */
|
|
|
|
y = PACKET_SIZE;
|
2003-03-02 20:02:42 -05:00
|
|
|
buf2[y++] = (unsigned char)type;
|
|
|
|
buf2[y++] = (unsigned char)sets[key->idx].size;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* output x coordinate */
|
|
|
|
OUTPUT_BIGNUM(&(key->pubkey.x), buf2, y, z);
|
|
|
|
|
|
|
|
/* compress y and output it */
|
2003-03-02 20:02:42 -05:00
|
|
|
if ((err = compress_y_point(&key->pubkey, key->idx, &res)) != CRYPT_OK) {
|
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
2003-03-02 20:02:42 -05:00
|
|
|
buf2[y++] = (unsigned char)res;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
if (type == PK_PRIVATE) {
|
|
|
|
OUTPUT_BIGNUM(&key->k, buf2, y, z);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check size */
|
|
|
|
if (*outlen < y) {
|
|
|
|
return CRYPT_BUFFER_OVERFLOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store header */
|
2003-03-02 20:02:10 -05:00
|
|
|
packet_store_header(buf2, PACKET_SECT_ECC, PACKET_SUB_KEY);
|
2003-03-02 19:59:24 -05:00
|
|
|
|
2003-03-02 20:02:42 -05:00
|
|
|
memcpy(out, buf2, (size_t)y);
|
2003-03-02 19:59:24 -05:00
|
|
|
*outlen = y;
|
|
|
|
|
|
|
|
#ifdef CLEAN_STACK
|
|
|
|
zeromem(buf2, sizeof(buf2));
|
|
|
|
#endif
|
|
|
|
return CRYPT_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-02 20:01:40 -05:00
|
|
|
int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key)
|
2003-03-02 19:59:24 -05:00
|
|
|
{
|
2003-03-02 20:01:00 -05:00
|
|
|
unsigned long x, y, s;
|
2003-03-02 20:02:42 -05:00
|
|
|
int err;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
_ARGCHK(in != NULL);
|
|
|
|
_ARGCHK(key != NULL);
|
|
|
|
|
|
|
|
/* check type */
|
2003-03-02 20:02:42 -05:00
|
|
|
if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_ECC, PACKET_SUB_KEY)) != CRYPT_OK) {
|
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
2003-03-02 20:01:40 -05:00
|
|
|
|
|
|
|
if (2+PACKET_SIZE > inlen) {
|
|
|
|
return CRYPT_INVALID_PACKET;
|
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* init key */
|
|
|
|
if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->k, NULL) != MP_OKAY) {
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
y = PACKET_SIZE;
|
2003-03-02 20:02:42 -05:00
|
|
|
key->type = (int)in[y++];
|
|
|
|
s = (unsigned long)in[y++];
|
2003-03-02 20:01:00 -05:00
|
|
|
|
2003-03-02 20:02:42 -05:00
|
|
|
for (x = 0; (s > (unsigned long)sets[x].size) && (sets[x].size != 0); x++);
|
2003-03-02 20:01:00 -05:00
|
|
|
if (sets[x].size == 0) {
|
2003-03-02 20:02:42 -05:00
|
|
|
err = CRYPT_INVALID_KEYSIZE;
|
2003-03-02 20:01:40 -05:00
|
|
|
goto error;
|
2003-03-02 20:01:00 -05:00
|
|
|
}
|
2003-03-02 20:02:42 -05:00
|
|
|
key->idx = (int)x;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
/* type check both values */
|
|
|
|
if ((key->type != PK_PUBLIC) && (key->type != PK_PRIVATE)) {
|
2003-03-02 20:02:42 -05:00
|
|
|
err = CRYPT_INVALID_PACKET;
|
2003-03-02 20:01:40 -05:00
|
|
|
goto error;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* is the key idx valid? */
|
2003-03-02 20:02:42 -05:00
|
|
|
if (is_valid_idx(key->idx) != 1) {
|
|
|
|
err = CRYPT_INVALID_PACKET;
|
2003-03-02 20:01:40 -05:00
|
|
|
goto error;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* load x coordinate */
|
|
|
|
INPUT_BIGNUM(&key->pubkey.x, in, x, y);
|
|
|
|
|
|
|
|
/* load y */
|
2003-03-02 20:02:42 -05:00
|
|
|
x = (unsigned long)in[y++];
|
|
|
|
if ((err = expand_y_point(&key->pubkey, key->idx, (int)x)) != CRYPT_OK) {
|
2003-03-02 20:01:40 -05:00
|
|
|
goto error;
|
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
if (key->type == PK_PRIVATE) {
|
|
|
|
/* load private key */
|
|
|
|
INPUT_BIGNUM(&key->k, in, x, y);
|
|
|
|
}
|
2003-03-02 20:02:10 -05:00
|
|
|
|
|
|
|
/* eliminate private key if public */
|
|
|
|
if (key->type == PK_PUBLIC) {
|
|
|
|
mp_clear(&key->k);
|
|
|
|
}
|
|
|
|
|
2003-03-02 20:01:40 -05:00
|
|
|
return CRYPT_OK;
|
2003-03-02 19:59:24 -05:00
|
|
|
error:
|
|
|
|
mp_clear_multi(&key->pubkey.x, &key->pubkey.y, &key->k, NULL);
|
2003-03-02 20:02:42 -05:00
|
|
|
return err;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
|
|
|
|
unsigned char *out, unsigned long *outlen)
|
|
|
|
{
|
|
|
|
unsigned long x, y;
|
|
|
|
ecc_point *result;
|
|
|
|
mp_int prime;
|
2003-03-02 20:02:42 -05:00
|
|
|
int res, err;
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
_ARGCHK(private_key != NULL);
|
|
|
|
_ARGCHK(public_key != NULL);
|
|
|
|
_ARGCHK(out != NULL);
|
|
|
|
_ARGCHK(outlen != NULL);
|
|
|
|
|
|
|
|
/* type valid? */
|
|
|
|
if (private_key->type != PK_PRIVATE) {
|
|
|
|
return CRYPT_PK_NOT_PRIVATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (private_key->idx != public_key->idx) {
|
|
|
|
return CRYPT_PK_TYPE_MISMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make new point */
|
|
|
|
result = new_point();
|
|
|
|
if (result == NULL) {
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mp_init(&prime) != MP_OKAY) {
|
|
|
|
del_point(result);
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
|
2003-03-02 20:02:10 -05:00
|
|
|
if (mp_read_radix(&prime, (unsigned char *)sets[private_key->idx].prime, 64) != MP_OKAY) { goto error; }
|
2003-03-02 20:02:42 -05:00
|
|
|
if ((err = ecc_mulmod(&private_key->k, &public_key->pubkey, result, &prime)) != CRYPT_OK) { res = err; goto done1; }
|
2003-03-02 19:59:24 -05:00
|
|
|
|
2003-03-02 20:02:42 -05:00
|
|
|
x = (unsigned long)mp_unsigned_bin_size(&result->x);
|
|
|
|
y = (unsigned long)mp_unsigned_bin_size(&result->y);
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
if (*outlen < (x+y)) {
|
|
|
|
res = CRYPT_BUFFER_OVERFLOW;
|
|
|
|
goto done1;
|
|
|
|
}
|
|
|
|
*outlen = x+y;
|
2003-03-02 20:02:42 -05:00
|
|
|
(void)mp_to_unsigned_bin(&result->x, out);
|
|
|
|
(void)mp_to_unsigned_bin(&result->y, out+x);
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
res = CRYPT_OK;
|
|
|
|
goto done1;
|
|
|
|
error:
|
|
|
|
res = CRYPT_MEM;
|
|
|
|
done1:
|
|
|
|
mp_clear(&prime);
|
|
|
|
del_point(result);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ecc_get_size(ecc_key *key)
|
|
|
|
{
|
|
|
|
_ARGCHK(key != NULL);
|
|
|
|
if (is_valid_idx(key->idx))
|
|
|
|
return sets[key->idx].size;
|
|
|
|
else
|
|
|
|
return INT_MAX; /* large value known to cause it to fail when passed to ecc_make_key() */
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "ecc_sys.c"
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|