X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=gost_ec_sign.c;h=f7b8966d01801b4b057b7e80f1d31e5f85a77171;hb=refs%2Fheads%2Fossl_patched;hp=904b80c6b22512b185280757e1cb5dcfd9d31b80;hpb=a72a02c4dd5778eea83db6e9e17d89f0d2b278dd;p=openssl-gost%2Fengine.git diff --git a/gost_ec_sign.c b/gost_ec_sign.c index 904b80c..f7b8966 100644 --- a/gost_ec_sign.c +++ b/gost_ec_sign.c @@ -16,32 +16,29 @@ extern void dump_signature(const char *message, const unsigned char *buffer, size_t len); -void dump_dsa_sig(const char *message, DSA_SIG *sig); +void dump_dsa_sig(const char *message, ECDSA_SIG *sig); #else # define dump_signature(a,b,c) # define dump_dsa_sig(a,b) #endif -/* Convert little-endian byte array into bignum */ -BIGNUM *hashsum2bn(const unsigned char *dgst, int len) -{ - unsigned char buf[64]; - int i; - - if (len > sizeof(buf)) - return NULL; - - for (i = 0; i < len; i++) { - buf[len - i - 1] = dgst[i]; - } - return BN_bin2bn(buf, len, NULL); -} - static R3410_ec_params *gost_nid2params(int nid) { R3410_ec_params *params; + /* Map tc26-2012 256-bit parameters to cp-2001 parameters */ + switch (nid) { + case NID_id_tc26_gost_3410_2012_256_paramSetB: + nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet; + break; + case NID_id_tc26_gost_3410_2012_256_paramSetC: + nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet; + break; + case NID_id_tc26_gost_3410_2012_256_paramSetD: + nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet; + } + /* Search nid in 2012 paramset */ params = R3410_2012_512_paramset; while (params->nid != NID_undef) { @@ -74,7 +71,8 @@ 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 +93,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; } @@ -121,17 +121,17 @@ int fill_GOST_EC_params(EC_KEY *eckey, int nid) if (!BN_hex2bn(&x, params->x) || !BN_hex2bn(&y, params->y) - || !EC_POINT_set_affine_coordinates_GFp(grp, P, x, y, ctx) + || !EC_POINT_set_affine_coordinates(grp, P, x, y, ctx) || !BN_hex2bn(&q, params->q)) { GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR); 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; } - EC_GROUP_set_curve_name(grp, params->nid); + EC_GROUP_set_curve_name(grp, nid); if (!EC_KEY_set_group(eckey, grp)) { GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR); goto end; @@ -148,12 +148,12 @@ int fill_GOST_EC_params(EC_KEY *eckey, int nid) } /* - * Computes gost_ec signature as DSA_SIG structure + * Computes gost_ec signature as ECDSA_SIG structure * */ -DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) +ECDSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) { - DSA_SIG *newsig = NULL, *ret = NULL; + ECDSA_SIG *newsig = NULL, *ret = NULL; BIGNUM *md = NULL; BIGNUM *order = NULL; const EC_GROUP *group; @@ -161,22 +161,22 @@ DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) BIGNUM *r = NULL, *s = NULL, *X = NULL, *tmp = NULL, *tmp2 = NULL, *k = NULL, *e = NULL; - const BIGNUM *new_r = NULL, *new_s = NULL; + BIGNUM *new_r = NULL, *new_s = NULL; EC_POINT *C = NULL; BN_CTX *ctx; OPENSSL_assert(dgst != NULL && eckey != NULL); - if (!(ctx = BN_CTX_new())) { + if (!(ctx = BN_CTX_secure_new())) { GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE); return NULL; } BN_CTX_start(ctx); OPENSSL_assert(dlen == 32 || dlen == 64); - md = hashsum2bn(dgst, dlen); - newsig = DSA_SIG_new(); + md = BN_lebin2bn(dgst, dlen, NULL); + newsig = ECDSA_SIG_new(); if (!newsig || !md) { GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE); goto err; @@ -224,15 +224,7 @@ DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) GOSTerr(GOST_F_GOST_EC_SIGN, GOST_R_RNG_ERROR); goto err; } - /* - * To avoid timing information leaking the length of k, - * compute C*k using an equivalent scalar of fixed bit-length */ - if (!BN_add(k, k, order) - || (BN_num_bits(k) <= BN_num_bits(order) - && !BN_add(k, k, order))) { - goto err; - } - if (!EC_POINT_mul(group, C, k, NULL, NULL, ctx)) { + if (!gost_ec_point_mul(group, C, k, NULL, NULL, ctx)) { GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB); goto err; } @@ -244,7 +236,7 @@ DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE); goto err; } - if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) { + if (!EC_POINT_get_affine_coordinates(group, C, X, NULL, ctx)) { GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB); goto err; } @@ -276,13 +268,13 @@ DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) } while (BN_is_zero(s)); - DSA_SIG_get0(newsig, &new_r, &new_s); 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; } + ECDSA_SIG_set0(newsig, new_r, new_s); ret = newsig; err: @@ -293,7 +285,7 @@ DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) if (md) BN_free(md); if (!ret && newsig) { - DSA_SIG_free(newsig); + ECDSA_SIG_free(newsig); } return ret; } @@ -303,13 +295,12 @@ DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) * */ int gost_ec_verify(const unsigned char *dgst, int dgst_len, - DSA_SIG *sig, EC_KEY *ec) + ECDSA_SIG *sig, EC_KEY *ec) { BN_CTX *ctx; const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL; BIGNUM *order; - BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL, - *z1 = NULL, *z2 = NULL; + 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; @@ -343,7 +334,7 @@ int gost_ec_verify(const unsigned char *dgst, int dgst_len, goto err; } - DSA_SIG_get0(sig, &sig_r, &sig_s); + ECDSA_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)) { @@ -353,7 +344,7 @@ int gost_ec_verify(const unsigned char *dgst, int dgst_len, } OPENSSL_assert(dgst_len == 32 || dgst_len == 64); - md = hashsum2bn(dgst, dgst_len); + md = BN_lebin2bn(dgst, dgst_len, NULL); if (!md || !BN_mod(e, md, order, ctx)) { GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR); goto err; @@ -388,11 +379,11 @@ int gost_ec_verify(const unsigned char *dgst, int dgst_len, GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE); goto err; } - if (!EC_POINT_mul(group, C, z1, pub_key, z2, ctx)) { + if (!gost_ec_point_mul(group, C, z1, pub_key, z2, ctx)) { GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_EC_LIB); goto err; } - if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) { + if (!EC_POINT_get_affine_coordinates(group, C, X, NULL, ctx)) { GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_EC_LIB); goto err; } @@ -440,7 +431,7 @@ int gost_ec_compute_public(EC_KEY *ec) return 0; } - ctx = BN_CTX_new(); + ctx = BN_CTX_secure_new(); if (!ctx) { GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE); return 0; @@ -459,7 +450,7 @@ int gost_ec_compute_public(EC_KEY *ec) goto err; } - if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx)) { + if (!gost_ec_point_mul(group, pub_key, priv_key, NULL, NULL, ctx)) { GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB); goto err; } @@ -476,6 +467,101 @@ int gost_ec_compute_public(EC_KEY *ec) return ok; } +int gost_ec_point_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n, + const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx) +{ + if (group == NULL || r == NULL || ctx == NULL) + return 0; + + if (m != NULL && n != NULL) { + /* verification */ + if (q == NULL) + return 0; + switch(EC_GROUP_get_curve_name(group)) { + case NID_id_GostR3410_2001_CryptoPro_A_ParamSet: + case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet: + case NID_id_tc26_gost_3410_2012_256_paramSetB: + return point_mul_two_id_GostR3410_2001_CryptoPro_A_ParamSet(group, r, n, q, m, ctx); + case NID_id_GostR3410_2001_CryptoPro_B_ParamSet: + case NID_id_tc26_gost_3410_2012_256_paramSetC: + return point_mul_two_id_GostR3410_2001_CryptoPro_B_ParamSet(group, r, n, q, m, ctx); + case NID_id_GostR3410_2001_CryptoPro_C_ParamSet: + case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet: + case NID_id_tc26_gost_3410_2012_256_paramSetD: + return point_mul_two_id_GostR3410_2001_CryptoPro_C_ParamSet(group, r, n, q, m, ctx); + case NID_id_GostR3410_2001_TestParamSet: + return point_mul_two_id_GostR3410_2001_TestParamSet(group, r, n, q, m, ctx); + case NID_id_tc26_gost_3410_2012_256_paramSetA: + return point_mul_two_id_tc26_gost_3410_2012_256_paramSetA(group, r, n, q, m, ctx); + case NID_id_tc26_gost_3410_2012_512_paramSetA: + return point_mul_two_id_tc26_gost_3410_2012_512_paramSetA(group, r, n, q, m, ctx); + case NID_id_tc26_gost_3410_2012_512_paramSetB: + return point_mul_two_id_tc26_gost_3410_2012_512_paramSetB(group, r, n, q, m, ctx); + case NID_id_tc26_gost_3410_2012_512_paramSetC: + return point_mul_two_id_tc26_gost_3410_2012_512_paramSetC(group, r, n, q, m, ctx); + default: + return EC_POINT_mul(group, r, n, q, m, ctx); + } + } else if (n != NULL) { + /* mul g */ + switch(EC_GROUP_get_curve_name(group)) { + case NID_id_GostR3410_2001_CryptoPro_A_ParamSet: + case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet: + case NID_id_tc26_gost_3410_2012_256_paramSetB: + return point_mul_g_id_GostR3410_2001_CryptoPro_A_ParamSet(group, r, n, ctx); + case NID_id_GostR3410_2001_CryptoPro_B_ParamSet: + case NID_id_tc26_gost_3410_2012_256_paramSetC: + return point_mul_g_id_GostR3410_2001_CryptoPro_B_ParamSet(group, r, n, ctx); + case NID_id_GostR3410_2001_CryptoPro_C_ParamSet: + case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet: + case NID_id_tc26_gost_3410_2012_256_paramSetD: + return point_mul_g_id_GostR3410_2001_CryptoPro_C_ParamSet(group, r, n, ctx); + case NID_id_GostR3410_2001_TestParamSet: + return point_mul_g_id_GostR3410_2001_TestParamSet(group, r, n, ctx); + case NID_id_tc26_gost_3410_2012_256_paramSetA: + return point_mul_g_id_tc26_gost_3410_2012_256_paramSetA(group, r, n, ctx); + case NID_id_tc26_gost_3410_2012_512_paramSetA: + return point_mul_g_id_tc26_gost_3410_2012_512_paramSetA(group, r, n, ctx); + case NID_id_tc26_gost_3410_2012_512_paramSetB: + return point_mul_g_id_tc26_gost_3410_2012_512_paramSetB(group, r, n, ctx); + case NID_id_tc26_gost_3410_2012_512_paramSetC: + return point_mul_g_id_tc26_gost_3410_2012_512_paramSetC(group, r, n, ctx); + default: + return EC_POINT_mul(group, r, n, q, m, ctx); + } + } else if (m != NULL) { + if (q == NULL) + return 0; + /* mul */ + switch(EC_GROUP_get_curve_name(group)) { + case NID_id_GostR3410_2001_CryptoPro_A_ParamSet: + case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet: + case NID_id_tc26_gost_3410_2012_256_paramSetB: + return point_mul_id_GostR3410_2001_CryptoPro_A_ParamSet(group, r, q, m, ctx); + case NID_id_GostR3410_2001_CryptoPro_B_ParamSet: + case NID_id_tc26_gost_3410_2012_256_paramSetC: + return point_mul_id_GostR3410_2001_CryptoPro_B_ParamSet(group, r, q, m, ctx); + case NID_id_GostR3410_2001_CryptoPro_C_ParamSet: + case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet: + case NID_id_tc26_gost_3410_2012_256_paramSetD: + return point_mul_id_GostR3410_2001_CryptoPro_C_ParamSet(group, r, q, m, ctx); + case NID_id_GostR3410_2001_TestParamSet: + return point_mul_id_GostR3410_2001_TestParamSet(group, r, q, m, ctx); + case NID_id_tc26_gost_3410_2012_256_paramSetA: + return point_mul_id_tc26_gost_3410_2012_256_paramSetA(group, r, q, m, ctx); + case NID_id_tc26_gost_3410_2012_512_paramSetA: + return point_mul_id_tc26_gost_3410_2012_512_paramSetA(group, r, q, m, ctx); + case NID_id_tc26_gost_3410_2012_512_paramSetB: + return point_mul_id_tc26_gost_3410_2012_512_paramSetB(group, r, q, m, ctx); + case NID_id_tc26_gost_3410_2012_512_paramSetC: + return point_mul_id_tc26_gost_3410_2012_512_paramSetC(group, r, q, m, ctx); + default: + return EC_POINT_mul(group, r, n, q, m, ctx); + } + } + return 0; +} + /* * * Generates GOST R 34.10-2001 @@ -494,7 +580,7 @@ int gost_ec_keygen(EC_KEY *ec) } order = BN_new(); - d = BN_new(); + d = BN_secure_new(); if (!order || !d) { GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_MALLOC_FAILURE); goto end; @@ -527,3 +613,174 @@ int gost_ec_keygen(EC_KEY *ec) return (ok) ? gost_ec_compute_public(ec) : 0; } + +int gost_ec_oct2point(const EC_GROUP *group, EC_POINT *point, + const unsigned char *buf, size_t len) +{ + BN_CTX *ctx = NULL; + BIGNUM *x, *y, *p; + size_t field_len, enc_len; + int ret = 0; + + if (len == 0) { + GOSTerr(GOST_F_GOST_EC_OCT2POINT, EC_R_BUFFER_TOO_SMALL); + return 0; + } + + field_len = (EC_GROUP_get_degree(group) + 7) / 8; + enc_len = 2 * field_len; + + if (len != enc_len) { + GOSTerr(GOST_F_GOST_EC_OCT2POINT, EC_R_INVALID_ENCODING); + return 0; + } + + ctx = BN_CTX_new(); + if (ctx == NULL) + return 0; + + BN_CTX_start(ctx); + x = BN_CTX_get(ctx); + y = BN_CTX_get(ctx); + p = BN_CTX_get(ctx); + if (y == NULL) + goto err; + + if (!EC_GROUP_get_curve(group, p, NULL, NULL, NULL)) + goto err; + + if (!BN_lebin2bn(buf, field_len, x)) + goto err; + if (BN_ucmp(x, p) >= 0) { + GOSTerr(GOST_F_GOST_EC_OCT2POINT, EC_R_INVALID_ENCODING); + goto err; + } + + if (!BN_lebin2bn(buf + field_len, field_len, y)) + goto err; + if (BN_ucmp(y, p) >= 0) { + GOSTerr(GOST_F_GOST_EC_OCT2POINT, EC_R_INVALID_ENCODING); + goto err; + } + + /* + * EC_POINT_set_affine_coordinates is responsible for checking that + * the point is on the curve. + */ + if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) + goto err; + + ret = 1; + + err: + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; +} + + +size_t gost_ec_point2oct(const EC_GROUP *group, const EC_POINT *point, + unsigned char *buf, size_t len) +{ + size_t ret; + BN_CTX *ctx = NULL; + int used_ctx = 0; + BIGNUM *x, *y; + size_t field_len; + + field_len = (EC_GROUP_get_degree(group) + 7) / 8; + ret = 2 * field_len; + + if (buf != NULL) { + if (len < ret) { + GOSTerr(GOST_F_GOST_EC_POINT2OCT, EC_R_BUFFER_TOO_SMALL); + goto err; + } + + ctx = BN_CTX_new(); + if (ctx == NULL) + return 0; + + BN_CTX_start(ctx); + used_ctx = 1; + x = BN_CTX_get(ctx); + y = BN_CTX_get(ctx); + if (y == NULL) + goto err; + + if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx)) + goto err; + + if (BN_bn2lebinpad(x, buf, field_len) != field_len + || BN_bn2lebinpad(y, buf + field_len, field_len) != field_len) + goto err; + } + + if (used_ctx) + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return ret; + + err: + if (used_ctx) + BN_CTX_end(ctx); + BN_CTX_free(ctx); + return 0; +} + +size_t gost_ec_key2buf(const EC_KEY *key, unsigned char **pbuf) +{ + size_t len; + unsigned char *buf; + const EC_GROUP *grp = NULL; + const EC_POINT *pkey = NULL; + + pkey = EC_KEY_get0_public_key(key); + grp = EC_KEY_get0_group(key); + + if(pkey == NULL || grp == NULL) + return 0; + + len = gost_ec_point2oct(grp, pkey, NULL, 0); + if (len == 0) + return 0; + if ((buf = OPENSSL_malloc(len)) == NULL) { + GOSTerr(GOST_F_GOST_EC_KEY2BUF, ERR_R_MALLOC_FAILURE); + return 0; + } + len = gost_ec_point2oct(grp, pkey, buf, len); + if (len == 0) { + OPENSSL_free(buf); + return 0; + } + *pbuf = buf; + return len; +} + +int gost_ec_oct2key(EC_KEY *key, const unsigned char *buf, size_t len) +{ + const EC_GROUP *group = NULL; + const EC_POINT *pub_key = NULL; + EC_POINT *point = NULL; + int ok = 0; + + if (key == NULL || (group = EC_KEY_get0_group(key)) == NULL) + return 0; + + if ((pub_key = EC_KEY_get0_public_key(key)) == NULL) { + if((point = EC_POINT_new(group)) == NULL || + !EC_KEY_set_public_key(key, point)) + goto err; + pub_key = EC_KEY_get0_public_key(key); + } + + if (gost_ec_oct2point(group, (EC_POINT *)pub_key, buf, len) == 0) + goto err; + + ok = 1; + + err: + if (point) + EC_POINT_free(point); + return ok; +}