Loading crypto/bn/bn_modfs.c +43 −43 Original line number Diff line number Diff line Loading @@ -27,17 +27,16 @@ int BN_legendre(BIGNUM *a, BIGNUM *p, BN_CTX *ctx) assert(a != NULL && p != NULL && ctx != NULL); x = ctx->bn[ctx->tos]; y = ctx->bn[ctx->tos + 1]; y2 = ctx->bn[ctx->tos + 2]; ctx->tos += 3; BN_CTX_start(ctx); x = BN_CTX_get(ctx); y = BN_CTX_get(ctx); y2 = BN_CTX_get(ctx); if (y2 == NULL) goto err; if (!BN_nnmod(x, a, p, ctx)) goto err; if (BN_is_zero(x)) { ctx->tos -= 3; BN_CTX_end(ctx); return 0; } Loading @@ -63,12 +62,12 @@ int BN_legendre(BIGNUM *a, BIGNUM *p, BN_CTX *ctx) } if (BN_is_one(x)) { ctx->tos -= 3; BN_CTX_end(ctx); return L; } if (BN_mod_word(x, 4) == 3 && BN_mod_word(y, 4) == 3) L = -L; if (!BN_swap(x, y)) goto err; BN_swap(x, y); if (!BN_nnmod(x, x, y, ctx)) goto err; Loading @@ -76,7 +75,7 @@ int BN_legendre(BIGNUM *a, BIGNUM *p, BN_CTX *ctx) err: ctx->tos -= 3; BN_CTX_end(ctx); return -2; } Loading @@ -99,9 +98,10 @@ int BN_mod_sqrt(BIGNUM *x, BIGNUM *a, BIGNUM *p, BN_CTX *ctx) return 1; } n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; ctx->tos += 2; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); if (n1 == NULL) goto err; if ((r = BN_new()) == NULL) goto err; if ((b = BN_new()) == NULL) goto err; Loading @@ -116,13 +116,14 @@ int BN_mod_sqrt(BIGNUM *x, BIGNUM *a, BIGNUM *p, BN_CTX *ctx) max = 0; do{ do { if (max++ > MAX_ROUNDS) goto err; /* if p is not prime could never stop*/ if (!BN_add_word(m, 1)) goto err; ret = BN_legendre(m, p, ctx); if (ret < -1 || ret > 1) goto err; }while(ret != -1); } while (ret != -1); if (BN_copy(n1, p) == NULL) goto err; if (!BN_sub_word(n1, 1)) goto err; Loading @@ -146,7 +147,6 @@ int BN_mod_sqrt(BIGNUM *x, BIGNUM *a, BIGNUM *p, BN_CTX *ctx) while (!BN_is_one(b)) { if (!BN_one(m)) goto err; if (!BN_mod_sqr(n1, b, p, ctx)) goto err; while(!BN_is_one(n1)) Loading Loading @@ -181,12 +181,12 @@ int BN_mod_sqrt(BIGNUM *x, BIGNUM *a, BIGNUM *p, BN_CTX *ctx) if (r != NULL) BN_clear_free(r); if (b != NULL) BN_clear_free(b); if (m != NULL) BN_clear_free(m); ctx->tos -= 2; BN_CTX_end(ctx); return 1; err: if (r != NULL) BN_clear_free(r); if (b != NULL) BN_clear_free(b); if (m != NULL) BN_clear_free(m); ctx->tos -= 2; BN_CTX_end(ctx); return 0; } crypto/ec/ec.h +1 −1 Original line number Diff line number Diff line Loading @@ -15,7 +15,7 @@ #include <openssl/bn.h> #include "bn_mont2.h" #include "../bn/bn_mont2.h" /* XXX */ typedef struct bn_ec_struct /* E: y^2 = x^3 + Ax + B (mod p) */ { Loading crypto/ec/ec_point.c +347 −330 Original line number Diff line number Diff line Loading @@ -16,10 +16,11 @@ #include <openssl/bn.h> #include "bn_modfs.h" #include "bn_mont2.h" #include "../bn/bn_modfs.h" /* XXX */ #include "../bn/bn_mont2.h" /* XXX */ #include "ec.h" EC_POINT *ECP_new() { EC_POINT *ret; Loading @@ -42,6 +43,7 @@ EC_POINT *ECP_new() return(ret); } void ECP_clear_free(EC_POINT *P) { if (P == NULL) return; Loading @@ -53,6 +55,7 @@ void ECP_clear_free(EC_POINT *P) free(P); } void ECP_clear_free_precompute(ECP_PRECOMPUTE *prec) { int i; Loading @@ -72,10 +75,12 @@ void ECP_clear_free_precompute(ECP_PRECOMPUTE *prec) free(prec); } int ECP_is_on_ec(EC_POINT *P, EC *E, BN_CTX *ctx) { BIGNUM *n0, *n1, *n2, *p; int Pnorm; int ret = -1; assert(P != NULL); assert(P->X != NULL && P->Y != NULL && P->Z != NULL); Loading @@ -89,11 +94,12 @@ int ECP_is_on_ec(EC_POINT *P, EC *E, BN_CTX *ctx) if (ECP_is_infty(P)) return 1; n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; n2 = ctx->bn[ctx->tos + 2]; ctx->tos += 3; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); n2 = BN_CTX_get(ctx); if (n2 == NULL) goto err; p = E->p; Loading Loading @@ -133,17 +139,13 @@ int ECP_is_on_ec(EC_POINT *P, EC *E, BN_CTX *ctx) if (!BN_mod_mul(n1, P->Y, P->Y, p, ctx)) goto err; if (BN_cmp(n0, n1)) { ctx->tos -= 3; return 0; } ctx->tos -= 3; return 1; ret = 0; else ret = 1; err: ctx->tos -= 3; return -1; BN_CTX_end(ctx); return ret; } Loading @@ -152,7 +154,7 @@ EC_POINT *ECP_generate(BIGNUM *x, BIGNUM *z,EC *E, BN_CTX *ctx) /* z == NULL || z = 1 -> normalized */ { BIGNUM *n0, *n1; EC_POINT *ret; EC_POINT *ret = NULL; int Pnorm, Pinfty, X0, A0; assert(E != NULL); Loading Loading @@ -186,12 +188,13 @@ EC_POINT *ECP_generate(BIGNUM *x, BIGNUM *z,EC *E, BN_CTX *ctx) ret->is_in_mont = 0; n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; if (!BN_zero(n0)) return NULL; if (!BN_zero(n1)) return NULL; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); if (n1 == NULL) goto err; ctx->tos += 2; if (!BN_zero(n0)) goto err; if (!BN_zero(n1)) goto err; if (!X0) { Loading Loading @@ -228,15 +231,16 @@ EC_POINT *ECP_generate(BIGNUM *x, BIGNUM *z,EC *E, BN_CTX *ctx) if (!ECP_is_on_ec(ret, E, ctx)) goto err; #endif ctx->tos -= 2; BN_CTX_end(ctx); return ret; err: if (ret != NULL) ECP_clear_free(ret); ctx->tos -= 2; BN_CTX_end(ctx); return NULL; } int ECP_ecp2bin(EC_POINT *P, unsigned char *to, int form) /* form = 1 ... compressed 2 ... uncompressed Loading Loading @@ -285,6 +289,7 @@ int ECP_ecp2bin(EC_POINT *P, unsigned char *to, int form) return bytes; } int ECP_bin2ecp(unsigned char *from, int len, EC_POINT *P, EC *E, BN_CTX *ctx) { int y; Loading Loading @@ -345,6 +350,7 @@ int ECP_bin2ecp(unsigned char *from, int len, EC_POINT *P, EC *E, BN_CTX *ctx) return 1; } int ECP_normalize(EC_POINT *P, EC *E, BN_CTX *ctx) { BIGNUM *z, *zm; Loading @@ -365,8 +371,9 @@ int ECP_normalize(EC_POINT *P, EC *E, BN_CTX *ctx) assert(!P->is_in_mont); z = ctx->bn[ctx->tos]; ctx->tos++; BN_CTX_start(ctx); z = BN_CTX_get(ctx); if (z == NULL) goto err; if (!BN_mod_mul(z, zm, zm, E->p, ctx)) goto err; if (!BN_mod_mul(P->X, P->X, z, E->p, ctx)) goto err; Loading @@ -378,15 +385,16 @@ int ECP_normalize(EC_POINT *P, EC *E, BN_CTX *ctx) if (zm != NULL) BN_clear_free(zm); ctx->tos--; BN_CTX_end(ctx); return 1; err: if (zm != NULL) BN_clear_free(zm); ctx->tos--; BN_CTX_end(ctx); return 0; } int ECP_copy(EC_POINT *R, EC_POINT *P) { assert(P != NULL); Loading @@ -403,6 +411,7 @@ int ECP_copy(EC_POINT *R, EC_POINT *P) return 1; } EC_POINT *ECP_dup(EC_POINT *P) { EC_POINT *ret; Loading Loading @@ -477,12 +486,13 @@ int ECP_cmp(EC_POINT *P, EC_POINT *Q, BIGNUM *p, BN_CTX *ctx) Pnorm = (ECP_is_norm(P)); Qnorm = (ECP_is_norm(Q)); n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; n2 = ctx->bn[ctx->tos + 2]; n3 = ctx->bn[ctx->tos + 3]; n4 = ctx->bn[ctx->tos + 4]; ctx->tos += 5; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); n2 = BN_CTX_get(ctx); n3 = BN_CTX_get(ctx); n4 = BN_CTX_get(ctx); if (n4 == NULL) goto err; if (Qnorm) { Loading Loading @@ -516,7 +526,7 @@ int ECP_cmp(EC_POINT *P, EC_POINT *Q, BIGNUM *p, BN_CTX *ctx) if (!BN_is_zero(n0)) { ctx->tos -= 5; BN_CTX_end(ctx); return 1; } Loading @@ -524,18 +534,19 @@ int ECP_cmp(EC_POINT *P, EC_POINT *Q, BIGNUM *p, BN_CTX *ctx) if (!BN_is_zero(n0)) { ctx->tos -= 5; BN_CTX_end(ctx); return -1; } ctx->tos -= 5; BN_CTX_end(ctx); return 0; err: ctx->tos -= 5; BN_CTX_end(ctx); return -2; } int ECP_double(EC_POINT *R, EC_POINT *P, EC *E, BN_CTX *ctx) /* R <- 2P (on E) */ { Loading Loading @@ -564,11 +575,12 @@ int ECP_double(EC_POINT *R, EC_POINT *P, EC *E, BN_CTX *ctx) Pnorm = (ECP_is_norm(P)); A0 = (BN_is_zero(E->A)); n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; n2 = ctx->bn[ctx->tos + 2]; n3 = ctx->bn[ctx->tos + 3]; ctx->tos += 4; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); n2 = BN_CTX_get(ctx); n3 = BN_CTX_get(ctx); if (n3 == NULL) goto err; p = E->p; Loading Loading @@ -628,14 +640,15 @@ int ECP_double(EC_POINT *R, EC_POINT *P, EC *E, BN_CTX *ctx) if (!ECP_is_on_ec(R, E, ctx)) return 0; #endif ctx->tos -= 4; BN_CTX_end(ctx); return 1; err: ctx->tos -= 4; BN_CTX_end(ctx); return 0; } int ECP_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_CTX *ctx) /* R <- P + Q (on E) */ { Loading Loading @@ -668,14 +681,16 @@ int ECP_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_CTX *ctx) Pnorm = (ECP_is_norm(P)); Qnorm = (ECP_is_norm(Q)); n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; n2 = ctx->bn[ctx->tos + 2]; n3 = ctx->bn[ctx->tos + 3]; n4 = ctx->bn[ctx->tos + 4]; n5 = ctx->bn[ctx->tos + 5]; n6 = ctx->bn[ctx->tos + 6]; ctx->tos += 7; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); n2 = BN_CTX_get(ctx); n3 = BN_CTX_get(ctx); n4 = BN_CTX_get(ctx); n5 = BN_CTX_get(ctx); n6 = BN_CTX_get(ctx); if (n6 == NULL) goto err; p = E->p; /* L1; L2 */ Loading Loading @@ -717,12 +732,12 @@ int ECP_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_CTX *ctx) { if (BN_is_zero(n6)) /* P = Q => P + Q = 2P */ { ctx->tos -= 7; BN_CTX_end(ctx); return ECP_double(R, P, E, ctx); } else /* P = -Q => P + Q = \infty */ { ctx->tos -= 7; BN_CTX_end(ctx); if (!BN_zero(R->Z)) return 0; return 1; } Loading Loading @@ -766,11 +781,11 @@ int ECP_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_CTX *ctx) if (!ECP_is_on_ec(R, E, ctx)) return 0; #endif ctx->tos -= 7; BN_CTX_end(ctx); return 1; err: ctx->tos -= 7; BN_CTX_end(cxt); return 0; } Loading Loading @@ -824,6 +839,7 @@ err: return NULL; } int ECP_multiply(EC_POINT *R, BIGNUM *k, ECP_PRECOMPUTE *prec, EC *E, BN_CTX *ctx) /* R = [k]P */ { Loading Loading @@ -873,9 +889,7 @@ int ECP_multiply(EC_POINT *R, BIGNUM *k, ECP_PRECOMPUTE *prec, EC *E, BN_CTX *ct if (nextw < -1) nextw = -1; t = nextw + 1; while(!BN_is_bit_set(k, t)) { t++; } if (!ECP_double(R, R, E, ctx)) return 0; Loading Loading @@ -909,11 +923,11 @@ int ECP_multiply(EC_POINT *R, BIGNUM *k, ECP_PRECOMPUTE *prec, EC *E, BN_CTX *ct #endif /* SIMPLE */ #ifdef MONTGOMERY int ECP_to_montgomery(EC_POINT *P, BN_MONTGOMERY *mont, BN_CTX *ctx) { assert(P != NULL); assert(P->X != NULL && P->Y != NULL && P->Z != NULL); Loading Loading @@ -959,6 +973,7 @@ int ECP_from_montgomery(EC_POINT *P, BN_MONTGOMERY *mont, BN_CTX *ctx) return 1; } int ECP_mont_cmp(EC_POINT *P, EC_POINT *Q, BN_MONTGOMERY *mont, BN_CTX *ctx) /* return values: -2 ... error Loading Loading @@ -991,13 +1006,15 @@ int ECP_mont_cmp(EC_POINT *P, EC_POINT *Q, BN_MONTGOMERY *mont, BN_CTX *ctx) if (ECP_is_infty(P) || ECP_is_infty(Q)) return 1; n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; n2 = ctx->bn[ctx->tos + 2]; n3 = ctx->bn[ctx->tos + 3]; n4 = ctx->bn[ctx->tos + 4]; n5 = ctx->bn[ctx->tos + 5]; ctx->tos += 6; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); n2 = BN_CTX_get(ctx); n3 = BN_CTX_get(ctx); n4 = BN_CTX_get(ctx); n5 = BN_CTX_get(ctx); if (n5 == 0) goto err; p = mont->p; Loading @@ -1019,7 +1036,7 @@ int ECP_mont_cmp(EC_POINT *P, EC_POINT *Q, BN_MONTGOMERY *mont, BN_CTX *ctx) if (!BN_is_zero(n0)) { ctx->tos -= 6; BN_CTX_end(ctx); return 1; } Loading @@ -1027,15 +1044,15 @@ int ECP_mont_cmp(EC_POINT *P, EC_POINT *Q, BN_MONTGOMERY *mont, BN_CTX *ctx) if (!BN_is_zero(n0)) { ctx->tos -= 6; BN_CTX_end(ctx); return -1; } ctx->tos -= 6; BN_CTX_end(ctx); return 0; err: ctx->tos -= 6; BN_CTX_end(ctx); return -2; } Loading Loading @@ -1071,12 +1088,12 @@ int ECP_mont_double(EC_POINT *R, EC_POINT *P, EC *E, BN_MONTGOMERY *mont, BN_CTX } n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; n2 = ctx->bn[ctx->tos + 2]; n3 = ctx->bn[ctx->tos + 3]; ctx->tos += 4; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); n2 = BN_CTX_get(ctx); n3 = BN_CTX_get(ctx); if (n3 == 0) goto err; p = E->p; Loading Loading @@ -1113,11 +1130,11 @@ int ECP_mont_double(EC_POINT *R, EC_POINT *P, EC *E, BN_MONTGOMERY *mont, BN_CTX if (!BN_mont_mod_mul(n0, n1, n2, mont, ctx)) goto err; if (!BN_mod_sub_quick(R->Y, n0, n3, p)) goto err; /* Y = L1 * (L2 - X) - L3 */ ctx->tos -= 4; BN_CTX_end(ctx); return 1; err: ctx->tos -= 4; BN_CTX_end(ctx); return 0; } Loading Loading @@ -1157,14 +1174,15 @@ int ECP_mont_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_MONTGOMERY *mo if (ECP_is_infty(Q)) return ECP_copy(R, P); n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; n2 = ctx->bn[ctx->tos + 2]; n3 = ctx->bn[ctx->tos + 3]; n4 = ctx->bn[ctx->tos + 4]; n5 = ctx->bn[ctx->tos + 5]; n6 = ctx->bn[ctx->tos + 6]; ctx->tos += 7; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); n2 = BN_CTX_get(ctx); n3 = BN_CTX_get(ctx); n4 = BN_CTX_get(ctx); n5 = BN_CTX_get(ctx); n6 = BN_CTX_get(ctx); if (n6 == NULL) goto err; p = E->p; Loading Loading @@ -1197,12 +1215,12 @@ int ECP_mont_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_MONTGOMERY *mo { if (BN_is_zero(n6)) /* P = Q => P + Q = 2P */ { ctx->tos -= 7; BN_CTX_end(ctx); return ECP_mont_double(R, P, E, mont, ctx); } else /* P = -Q => P + Q = \infty */ { ctx->tos -= 7; BN_CTX_end(ctx); if (!BN_zero(R->Z)) return 0; return 1; } Loading Loading @@ -1238,11 +1256,11 @@ int ECP_mont_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_MONTGOMERY *mo if (!BN_mont_mod_mul(R->Y, n0, E->h, mont, ctx)) goto err; /* Y = (L6 * L9 - L8 * L5^3) / 2 */ ctx->tos -= 7; BN_CTX_end(ctx); return 1; err: ctx->tos -= 7; BN_CTX_end(ctx); return 0; } Loading Loading @@ -1303,6 +1321,7 @@ err: return NULL; } int ECP_mont_multiply(EC_POINT *R, BIGNUM *k, ECP_PRECOMPUTE *prec, EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx) /* R = [k]P P = prec->Pi[0]*/ { Loading Loading @@ -1356,9 +1375,7 @@ int ECP_mont_multiply(EC_POINT *R, BIGNUM *k, ECP_PRECOMPUTE *prec, EC *E, BN_MO if (nextw < -1) nextw = -1; t = nextw + 1; while(!BN_is_bit_set(k, t)) { t++; } if (!ECP_mont_double(R, R, E, mont, ctx)) return 0; Loading Loading
crypto/bn/bn_modfs.c +43 −43 Original line number Diff line number Diff line Loading @@ -27,17 +27,16 @@ int BN_legendre(BIGNUM *a, BIGNUM *p, BN_CTX *ctx) assert(a != NULL && p != NULL && ctx != NULL); x = ctx->bn[ctx->tos]; y = ctx->bn[ctx->tos + 1]; y2 = ctx->bn[ctx->tos + 2]; ctx->tos += 3; BN_CTX_start(ctx); x = BN_CTX_get(ctx); y = BN_CTX_get(ctx); y2 = BN_CTX_get(ctx); if (y2 == NULL) goto err; if (!BN_nnmod(x, a, p, ctx)) goto err; if (BN_is_zero(x)) { ctx->tos -= 3; BN_CTX_end(ctx); return 0; } Loading @@ -63,12 +62,12 @@ int BN_legendre(BIGNUM *a, BIGNUM *p, BN_CTX *ctx) } if (BN_is_one(x)) { ctx->tos -= 3; BN_CTX_end(ctx); return L; } if (BN_mod_word(x, 4) == 3 && BN_mod_word(y, 4) == 3) L = -L; if (!BN_swap(x, y)) goto err; BN_swap(x, y); if (!BN_nnmod(x, x, y, ctx)) goto err; Loading @@ -76,7 +75,7 @@ int BN_legendre(BIGNUM *a, BIGNUM *p, BN_CTX *ctx) err: ctx->tos -= 3; BN_CTX_end(ctx); return -2; } Loading @@ -99,9 +98,10 @@ int BN_mod_sqrt(BIGNUM *x, BIGNUM *a, BIGNUM *p, BN_CTX *ctx) return 1; } n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; ctx->tos += 2; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); if (n1 == NULL) goto err; if ((r = BN_new()) == NULL) goto err; if ((b = BN_new()) == NULL) goto err; Loading @@ -116,13 +116,14 @@ int BN_mod_sqrt(BIGNUM *x, BIGNUM *a, BIGNUM *p, BN_CTX *ctx) max = 0; do{ do { if (max++ > MAX_ROUNDS) goto err; /* if p is not prime could never stop*/ if (!BN_add_word(m, 1)) goto err; ret = BN_legendre(m, p, ctx); if (ret < -1 || ret > 1) goto err; }while(ret != -1); } while (ret != -1); if (BN_copy(n1, p) == NULL) goto err; if (!BN_sub_word(n1, 1)) goto err; Loading @@ -146,7 +147,6 @@ int BN_mod_sqrt(BIGNUM *x, BIGNUM *a, BIGNUM *p, BN_CTX *ctx) while (!BN_is_one(b)) { if (!BN_one(m)) goto err; if (!BN_mod_sqr(n1, b, p, ctx)) goto err; while(!BN_is_one(n1)) Loading Loading @@ -181,12 +181,12 @@ int BN_mod_sqrt(BIGNUM *x, BIGNUM *a, BIGNUM *p, BN_CTX *ctx) if (r != NULL) BN_clear_free(r); if (b != NULL) BN_clear_free(b); if (m != NULL) BN_clear_free(m); ctx->tos -= 2; BN_CTX_end(ctx); return 1; err: if (r != NULL) BN_clear_free(r); if (b != NULL) BN_clear_free(b); if (m != NULL) BN_clear_free(m); ctx->tos -= 2; BN_CTX_end(ctx); return 0; }
crypto/ec/ec.h +1 −1 Original line number Diff line number Diff line Loading @@ -15,7 +15,7 @@ #include <openssl/bn.h> #include "bn_mont2.h" #include "../bn/bn_mont2.h" /* XXX */ typedef struct bn_ec_struct /* E: y^2 = x^3 + Ax + B (mod p) */ { Loading
crypto/ec/ec_point.c +347 −330 Original line number Diff line number Diff line Loading @@ -16,10 +16,11 @@ #include <openssl/bn.h> #include "bn_modfs.h" #include "bn_mont2.h" #include "../bn/bn_modfs.h" /* XXX */ #include "../bn/bn_mont2.h" /* XXX */ #include "ec.h" EC_POINT *ECP_new() { EC_POINT *ret; Loading @@ -42,6 +43,7 @@ EC_POINT *ECP_new() return(ret); } void ECP_clear_free(EC_POINT *P) { if (P == NULL) return; Loading @@ -53,6 +55,7 @@ void ECP_clear_free(EC_POINT *P) free(P); } void ECP_clear_free_precompute(ECP_PRECOMPUTE *prec) { int i; Loading @@ -72,10 +75,12 @@ void ECP_clear_free_precompute(ECP_PRECOMPUTE *prec) free(prec); } int ECP_is_on_ec(EC_POINT *P, EC *E, BN_CTX *ctx) { BIGNUM *n0, *n1, *n2, *p; int Pnorm; int ret = -1; assert(P != NULL); assert(P->X != NULL && P->Y != NULL && P->Z != NULL); Loading @@ -89,11 +94,12 @@ int ECP_is_on_ec(EC_POINT *P, EC *E, BN_CTX *ctx) if (ECP_is_infty(P)) return 1; n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; n2 = ctx->bn[ctx->tos + 2]; ctx->tos += 3; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); n2 = BN_CTX_get(ctx); if (n2 == NULL) goto err; p = E->p; Loading Loading @@ -133,17 +139,13 @@ int ECP_is_on_ec(EC_POINT *P, EC *E, BN_CTX *ctx) if (!BN_mod_mul(n1, P->Y, P->Y, p, ctx)) goto err; if (BN_cmp(n0, n1)) { ctx->tos -= 3; return 0; } ctx->tos -= 3; return 1; ret = 0; else ret = 1; err: ctx->tos -= 3; return -1; BN_CTX_end(ctx); return ret; } Loading @@ -152,7 +154,7 @@ EC_POINT *ECP_generate(BIGNUM *x, BIGNUM *z,EC *E, BN_CTX *ctx) /* z == NULL || z = 1 -> normalized */ { BIGNUM *n0, *n1; EC_POINT *ret; EC_POINT *ret = NULL; int Pnorm, Pinfty, X0, A0; assert(E != NULL); Loading Loading @@ -186,12 +188,13 @@ EC_POINT *ECP_generate(BIGNUM *x, BIGNUM *z,EC *E, BN_CTX *ctx) ret->is_in_mont = 0; n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; if (!BN_zero(n0)) return NULL; if (!BN_zero(n1)) return NULL; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); if (n1 == NULL) goto err; ctx->tos += 2; if (!BN_zero(n0)) goto err; if (!BN_zero(n1)) goto err; if (!X0) { Loading Loading @@ -228,15 +231,16 @@ EC_POINT *ECP_generate(BIGNUM *x, BIGNUM *z,EC *E, BN_CTX *ctx) if (!ECP_is_on_ec(ret, E, ctx)) goto err; #endif ctx->tos -= 2; BN_CTX_end(ctx); return ret; err: if (ret != NULL) ECP_clear_free(ret); ctx->tos -= 2; BN_CTX_end(ctx); return NULL; } int ECP_ecp2bin(EC_POINT *P, unsigned char *to, int form) /* form = 1 ... compressed 2 ... uncompressed Loading Loading @@ -285,6 +289,7 @@ int ECP_ecp2bin(EC_POINT *P, unsigned char *to, int form) return bytes; } int ECP_bin2ecp(unsigned char *from, int len, EC_POINT *P, EC *E, BN_CTX *ctx) { int y; Loading Loading @@ -345,6 +350,7 @@ int ECP_bin2ecp(unsigned char *from, int len, EC_POINT *P, EC *E, BN_CTX *ctx) return 1; } int ECP_normalize(EC_POINT *P, EC *E, BN_CTX *ctx) { BIGNUM *z, *zm; Loading @@ -365,8 +371,9 @@ int ECP_normalize(EC_POINT *P, EC *E, BN_CTX *ctx) assert(!P->is_in_mont); z = ctx->bn[ctx->tos]; ctx->tos++; BN_CTX_start(ctx); z = BN_CTX_get(ctx); if (z == NULL) goto err; if (!BN_mod_mul(z, zm, zm, E->p, ctx)) goto err; if (!BN_mod_mul(P->X, P->X, z, E->p, ctx)) goto err; Loading @@ -378,15 +385,16 @@ int ECP_normalize(EC_POINT *P, EC *E, BN_CTX *ctx) if (zm != NULL) BN_clear_free(zm); ctx->tos--; BN_CTX_end(ctx); return 1; err: if (zm != NULL) BN_clear_free(zm); ctx->tos--; BN_CTX_end(ctx); return 0; } int ECP_copy(EC_POINT *R, EC_POINT *P) { assert(P != NULL); Loading @@ -403,6 +411,7 @@ int ECP_copy(EC_POINT *R, EC_POINT *P) return 1; } EC_POINT *ECP_dup(EC_POINT *P) { EC_POINT *ret; Loading Loading @@ -477,12 +486,13 @@ int ECP_cmp(EC_POINT *P, EC_POINT *Q, BIGNUM *p, BN_CTX *ctx) Pnorm = (ECP_is_norm(P)); Qnorm = (ECP_is_norm(Q)); n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; n2 = ctx->bn[ctx->tos + 2]; n3 = ctx->bn[ctx->tos + 3]; n4 = ctx->bn[ctx->tos + 4]; ctx->tos += 5; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); n2 = BN_CTX_get(ctx); n3 = BN_CTX_get(ctx); n4 = BN_CTX_get(ctx); if (n4 == NULL) goto err; if (Qnorm) { Loading Loading @@ -516,7 +526,7 @@ int ECP_cmp(EC_POINT *P, EC_POINT *Q, BIGNUM *p, BN_CTX *ctx) if (!BN_is_zero(n0)) { ctx->tos -= 5; BN_CTX_end(ctx); return 1; } Loading @@ -524,18 +534,19 @@ int ECP_cmp(EC_POINT *P, EC_POINT *Q, BIGNUM *p, BN_CTX *ctx) if (!BN_is_zero(n0)) { ctx->tos -= 5; BN_CTX_end(ctx); return -1; } ctx->tos -= 5; BN_CTX_end(ctx); return 0; err: ctx->tos -= 5; BN_CTX_end(ctx); return -2; } int ECP_double(EC_POINT *R, EC_POINT *P, EC *E, BN_CTX *ctx) /* R <- 2P (on E) */ { Loading Loading @@ -564,11 +575,12 @@ int ECP_double(EC_POINT *R, EC_POINT *P, EC *E, BN_CTX *ctx) Pnorm = (ECP_is_norm(P)); A0 = (BN_is_zero(E->A)); n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; n2 = ctx->bn[ctx->tos + 2]; n3 = ctx->bn[ctx->tos + 3]; ctx->tos += 4; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); n2 = BN_CTX_get(ctx); n3 = BN_CTX_get(ctx); if (n3 == NULL) goto err; p = E->p; Loading Loading @@ -628,14 +640,15 @@ int ECP_double(EC_POINT *R, EC_POINT *P, EC *E, BN_CTX *ctx) if (!ECP_is_on_ec(R, E, ctx)) return 0; #endif ctx->tos -= 4; BN_CTX_end(ctx); return 1; err: ctx->tos -= 4; BN_CTX_end(ctx); return 0; } int ECP_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_CTX *ctx) /* R <- P + Q (on E) */ { Loading Loading @@ -668,14 +681,16 @@ int ECP_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_CTX *ctx) Pnorm = (ECP_is_norm(P)); Qnorm = (ECP_is_norm(Q)); n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; n2 = ctx->bn[ctx->tos + 2]; n3 = ctx->bn[ctx->tos + 3]; n4 = ctx->bn[ctx->tos + 4]; n5 = ctx->bn[ctx->tos + 5]; n6 = ctx->bn[ctx->tos + 6]; ctx->tos += 7; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); n2 = BN_CTX_get(ctx); n3 = BN_CTX_get(ctx); n4 = BN_CTX_get(ctx); n5 = BN_CTX_get(ctx); n6 = BN_CTX_get(ctx); if (n6 == NULL) goto err; p = E->p; /* L1; L2 */ Loading Loading @@ -717,12 +732,12 @@ int ECP_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_CTX *ctx) { if (BN_is_zero(n6)) /* P = Q => P + Q = 2P */ { ctx->tos -= 7; BN_CTX_end(ctx); return ECP_double(R, P, E, ctx); } else /* P = -Q => P + Q = \infty */ { ctx->tos -= 7; BN_CTX_end(ctx); if (!BN_zero(R->Z)) return 0; return 1; } Loading Loading @@ -766,11 +781,11 @@ int ECP_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_CTX *ctx) if (!ECP_is_on_ec(R, E, ctx)) return 0; #endif ctx->tos -= 7; BN_CTX_end(ctx); return 1; err: ctx->tos -= 7; BN_CTX_end(cxt); return 0; } Loading Loading @@ -824,6 +839,7 @@ err: return NULL; } int ECP_multiply(EC_POINT *R, BIGNUM *k, ECP_PRECOMPUTE *prec, EC *E, BN_CTX *ctx) /* R = [k]P */ { Loading Loading @@ -873,9 +889,7 @@ int ECP_multiply(EC_POINT *R, BIGNUM *k, ECP_PRECOMPUTE *prec, EC *E, BN_CTX *ct if (nextw < -1) nextw = -1; t = nextw + 1; while(!BN_is_bit_set(k, t)) { t++; } if (!ECP_double(R, R, E, ctx)) return 0; Loading Loading @@ -909,11 +923,11 @@ int ECP_multiply(EC_POINT *R, BIGNUM *k, ECP_PRECOMPUTE *prec, EC *E, BN_CTX *ct #endif /* SIMPLE */ #ifdef MONTGOMERY int ECP_to_montgomery(EC_POINT *P, BN_MONTGOMERY *mont, BN_CTX *ctx) { assert(P != NULL); assert(P->X != NULL && P->Y != NULL && P->Z != NULL); Loading Loading @@ -959,6 +973,7 @@ int ECP_from_montgomery(EC_POINT *P, BN_MONTGOMERY *mont, BN_CTX *ctx) return 1; } int ECP_mont_cmp(EC_POINT *P, EC_POINT *Q, BN_MONTGOMERY *mont, BN_CTX *ctx) /* return values: -2 ... error Loading Loading @@ -991,13 +1006,15 @@ int ECP_mont_cmp(EC_POINT *P, EC_POINT *Q, BN_MONTGOMERY *mont, BN_CTX *ctx) if (ECP_is_infty(P) || ECP_is_infty(Q)) return 1; n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; n2 = ctx->bn[ctx->tos + 2]; n3 = ctx->bn[ctx->tos + 3]; n4 = ctx->bn[ctx->tos + 4]; n5 = ctx->bn[ctx->tos + 5]; ctx->tos += 6; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); n2 = BN_CTX_get(ctx); n3 = BN_CTX_get(ctx); n4 = BN_CTX_get(ctx); n5 = BN_CTX_get(ctx); if (n5 == 0) goto err; p = mont->p; Loading @@ -1019,7 +1036,7 @@ int ECP_mont_cmp(EC_POINT *P, EC_POINT *Q, BN_MONTGOMERY *mont, BN_CTX *ctx) if (!BN_is_zero(n0)) { ctx->tos -= 6; BN_CTX_end(ctx); return 1; } Loading @@ -1027,15 +1044,15 @@ int ECP_mont_cmp(EC_POINT *P, EC_POINT *Q, BN_MONTGOMERY *mont, BN_CTX *ctx) if (!BN_is_zero(n0)) { ctx->tos -= 6; BN_CTX_end(ctx); return -1; } ctx->tos -= 6; BN_CTX_end(ctx); return 0; err: ctx->tos -= 6; BN_CTX_end(ctx); return -2; } Loading Loading @@ -1071,12 +1088,12 @@ int ECP_mont_double(EC_POINT *R, EC_POINT *P, EC *E, BN_MONTGOMERY *mont, BN_CTX } n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; n2 = ctx->bn[ctx->tos + 2]; n3 = ctx->bn[ctx->tos + 3]; ctx->tos += 4; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); n2 = BN_CTX_get(ctx); n3 = BN_CTX_get(ctx); if (n3 == 0) goto err; p = E->p; Loading Loading @@ -1113,11 +1130,11 @@ int ECP_mont_double(EC_POINT *R, EC_POINT *P, EC *E, BN_MONTGOMERY *mont, BN_CTX if (!BN_mont_mod_mul(n0, n1, n2, mont, ctx)) goto err; if (!BN_mod_sub_quick(R->Y, n0, n3, p)) goto err; /* Y = L1 * (L2 - X) - L3 */ ctx->tos -= 4; BN_CTX_end(ctx); return 1; err: ctx->tos -= 4; BN_CTX_end(ctx); return 0; } Loading Loading @@ -1157,14 +1174,15 @@ int ECP_mont_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_MONTGOMERY *mo if (ECP_is_infty(Q)) return ECP_copy(R, P); n0 = ctx->bn[ctx->tos]; n1 = ctx->bn[ctx->tos + 1]; n2 = ctx->bn[ctx->tos + 2]; n3 = ctx->bn[ctx->tos + 3]; n4 = ctx->bn[ctx->tos + 4]; n5 = ctx->bn[ctx->tos + 5]; n6 = ctx->bn[ctx->tos + 6]; ctx->tos += 7; BN_CTX_start(ctx); n0 = BN_CTX_get(ctx); n1 = BN_CTX_get(ctx); n2 = BN_CTX_get(ctx); n3 = BN_CTX_get(ctx); n4 = BN_CTX_get(ctx); n5 = BN_CTX_get(ctx); n6 = BN_CTX_get(ctx); if (n6 == NULL) goto err; p = E->p; Loading Loading @@ -1197,12 +1215,12 @@ int ECP_mont_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_MONTGOMERY *mo { if (BN_is_zero(n6)) /* P = Q => P + Q = 2P */ { ctx->tos -= 7; BN_CTX_end(ctx); return ECP_mont_double(R, P, E, mont, ctx); } else /* P = -Q => P + Q = \infty */ { ctx->tos -= 7; BN_CTX_end(ctx); if (!BN_zero(R->Z)) return 0; return 1; } Loading Loading @@ -1238,11 +1256,11 @@ int ECP_mont_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_MONTGOMERY *mo if (!BN_mont_mod_mul(R->Y, n0, E->h, mont, ctx)) goto err; /* Y = (L6 * L9 - L8 * L5^3) / 2 */ ctx->tos -= 7; BN_CTX_end(ctx); return 1; err: ctx->tos -= 7; BN_CTX_end(ctx); return 0; } Loading Loading @@ -1303,6 +1321,7 @@ err: return NULL; } int ECP_mont_multiply(EC_POINT *R, BIGNUM *k, ECP_PRECOMPUTE *prec, EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx) /* R = [k]P P = prec->Pi[0]*/ { Loading Loading @@ -1356,9 +1375,7 @@ int ECP_mont_multiply(EC_POINT *R, BIGNUM *k, ECP_PRECOMPUTE *prec, EC *E, BN_MO if (nextw < -1) nextw = -1; t = nextw + 1; while(!BN_is_bit_set(k, t)) { t++; } if (!ECP_mont_double(R, R, E, mont, ctx)) return 0; Loading