* Requires OpenSSL 1.0.0+ for compilation *
**********************************************************************/
#include "gost_lcl.h"
-#include "gost_params.h"
#include <string.h>
#include <openssl/rand.h>
#include <openssl/ecdsa.h>
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;
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;
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;
}
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;
}
goto end;
}
ok = 1;
-end:
+ end:
if (P)
EC_POINT_free(P);
if (grp)
}
/*
- * 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;
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;
BN_CTX_start(ctx);
OPENSSL_assert(dlen == 32 || dlen == 64);
md = hashsum2bn(dgst, dlen);
- newsig = DSA_SIG_new();
+ newsig = ECDSA_SIG_new();
if (!newsig || !md) {
- GOSTerr(GOST_F_GOST_EC_SIGN, GOST_R_NO_MEMORY);
+ GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
goto err;
}
group = EC_KEY_get0_group(eckey);
do {
do {
if (!BN_rand_range(k, order)) {
- GOSTerr(GOST_F_GOST_EC_SIGN,
- GOST_R_RANDOM_NUMBER_GENERATOR_FAILED);
+ GOSTerr(GOST_F_GOST_EC_SIGN, GOST_R_RNG_ERROR);
goto err;
}
/*
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;
}
}
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;
}
+ ECDSA_SIG_set0(newsig, new_r, new_s);
ret = newsig;
err:
if (md)
BN_free(md);
if (!ret && newsig) {
- DSA_SIG_free(newsig);
+ ECDSA_SIG_free(newsig);
}
return ret;
}
*
*/
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;
const EC_POINT *pub_key = NULL;
OPENSSL_assert(dgst != NULL && sig != NULL && group != NULL);
if (!(ctx = BN_CTX_new())) {
- GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_NO_MEMORY);
+ GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
return 0;
}
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)) {
+ 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)) {
GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
goto err;
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;
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;
}
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;
do {
if (!BN_rand_range(d, order)) {
- GOSTerr(GOST_F_GOST_EC_KEYGEN,
- GOST_R_RANDOM_NUMBER_GENERATOR_FAILED);
+ GOSTerr(GOST_F_GOST_EC_KEYGEN, GOST_R_RNG_ERROR);
goto end;
}
}
}
ok = 1;
-end:
+ end:
if (d)
BN_free(d);
if (order)
BN_free(order);
-
+
return (ok) ? gost_ec_compute_public(ec) : 0;
}