X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=gost_ec_sign.c;h=2deb09324b50d15a50caaba367362d5f83a496b4;hb=445a097cfc4e18ad1634a4dce8bb9994dbf3ba15;hp=c700ac2f7d83db9478fc04e5cdaaa14141a50a1a;hpb=096f193c98ffbe23686f10c834b2c42092b65954;p=openssl-gost%2Fengine.git diff --git a/gost_ec_sign.c b/gost_ec_sign.c index c700ac2..2deb093 100644 --- a/gost_ec_sign.c +++ b/gost_ec_sign.c @@ -35,7 +35,7 @@ BIGNUM *hashsum2bn(const unsigned char *dgst, int len) for (i = 0; i < len; i++) { buf[len - i - 1] = dgst[i]; } - return getbnfrombuf(buf, len); + return BN_bin2bn(buf, len, NULL); } static R3410_ec_params *gost_nid2params(int nid) @@ -74,7 +74,7 @@ int fill_GOST_EC_params(EC_KEY *eckey, int nid) R3410_ec_params *params = gost_nid2params(nid); EC_GROUP *grp = NULL; EC_POINT *P = NULL; - BIGNUM *p = NULL, *q = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL; + BIGNUM *p = NULL, *q = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL, *cofactor = NULL; BN_CTX *ctx; int ok = 0; @@ -95,14 +95,16 @@ int fill_GOST_EC_params(EC_KEY *eckey, int nid) x = BN_CTX_get(ctx); y = BN_CTX_get(ctx); q = BN_CTX_get(ctx); - if (!p || !a || !b || !x || !y || !q) { + cofactor = BN_CTX_get(ctx); + if (!p || !a || !b || !x || !y || !q || !cofactor) { GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE); goto end; } if (!BN_hex2bn(&p, params->p) || !BN_hex2bn(&a, params->a) - || !BN_hex2bn(&b, params->b)) { + || !BN_hex2bn(&b, params->b) + || !BN_hex2bn(&cofactor, params->cofactor) ) { GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR); goto end; } @@ -127,7 +129,7 @@ int fill_GOST_EC_params(EC_KEY *eckey, int nid) goto end; } - if (!EC_GROUP_set_generator(grp, P, q, NULL)) { + if (!EC_GROUP_set_generator(grp, P, q, cofactor)) { GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR); goto end; } @@ -160,6 +162,9 @@ DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) const BIGNUM *priv_key; BIGNUM *r = NULL, *s = NULL, *X = NULL, *tmp = NULL, *tmp2 = NULL, *k = NULL, *e = NULL; + + BIGNUM *new_r = NULL, *new_s = NULL; + EC_POINT *C = NULL; BN_CTX *ctx; @@ -273,12 +278,13 @@ DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) } while (BN_is_zero(s)); - newsig->s = BN_dup(s); - newsig->r = BN_dup(r); - if (!newsig->s || !newsig->r) { + new_s = BN_dup(s); + new_r = BN_dup(r); + if (!new_s || !new_r) { GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE); goto err; } + DSA_SIG_set0(newsig, new_r, new_s); ret = newsig; err: @@ -306,6 +312,7 @@ int gost_ec_verify(const unsigned char *dgst, int dgst_len, BIGNUM *order; BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL, *z1 = NULL, *z2 = NULL; + const BIGNUM *sig_s = NULL, *sig_r = NULL; BIGNUM *X = NULL, *tmp = NULL; EC_POINT *C = NULL; const EC_POINT *pub_key = NULL; @@ -338,8 +345,10 @@ int gost_ec_verify(const unsigned char *dgst, int dgst_len, goto err; } - if (BN_is_zero(sig->s) || BN_is_zero(sig->r) || - (BN_cmp(sig->s, order) >= 1) || (BN_cmp(sig->r, order) >= 1)) { + DSA_SIG_get0(sig, &sig_r, &sig_s); + + if (BN_is_zero(sig_s) || BN_is_zero(sig_r) || + (BN_cmp(sig_s, order) >= 1) || (BN_cmp(sig_r, order) >= 1)) { GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q); goto err; @@ -362,8 +371,8 @@ int gost_ec_verify(const unsigned char *dgst, int dgst_len, goto err; } v = BN_mod_inverse(v, e, order, ctx); - if (!v || !BN_mod_mul(z1, sig->s, v, order, ctx) - || !BN_sub(tmp, order, sig->r) + if (!v || !BN_mod_mul(z1, sig_s, v, order, ctx) + || !BN_sub(tmp, order, sig_r) || !BN_mod_mul(z2, tmp, v, order, ctx)) { GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR); goto err; @@ -400,7 +409,7 @@ int gost_ec_verify(const unsigned char *dgst, int dgst_len, BN_print_fp(stderr, R); fprintf(stderr, "\n"); #endif - if (BN_cmp(R, sig->r) != 0) { + if (BN_cmp(R, sig_r) != 0) { GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_MISMATCH); } else { ok = 1;