1 /**********************************************************************
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * Implementation of GOST R 34.10-94 signature algorithm *
8 * Requires OpenSSL 0.9.9 for compilation *
9 **********************************************************************/
11 #include <openssl/rand.h>
12 #include <openssl/bn.h>
13 #include <openssl/dsa.h>
14 #include <openssl/err.h>
15 #include <openssl/evp.h>
16 #include <openssl/err.h>
18 #include "gost_params.h"
20 #include "e_gost_err.h"
23 void dump_signature(const char *message, const unsigned char *buffer,
27 fprintf(stderr, "signature %s Length=%d", message, len);
28 for (i = 0; i < len; i++) {
31 fprintf(stderr, " %02x", buffer[i]);
33 fprintf(stderr, "\nEnd of signature\n");
36 void dump_dsa_sig(const char *message, DSA_SIG *sig)
38 fprintf(stderr, "%s\nR=", message);
39 BN_print_fp(stderr, sig->r);
40 fprintf(stderr, "\nS=");
41 BN_print_fp(stderr, sig->s);
42 fprintf(stderr, "\n");
47 # define dump_signature(a,b,c)
48 # define dump_dsa_sig(a,b)
52 * Computes signature and returns it as DSA_SIG structure
54 DSA_SIG *gost_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
56 BIGNUM *k = NULL, *tmp = NULL, *tmp2 = NULL;
57 DSA_SIG *newsig, *ret = NULL;
58 BIGNUM *md = hashsum2bn(dgst);
59 /* check if H(M) mod q is zero */
60 BN_CTX *ctx = BN_CTX_new();
62 GOSTerr(GOST_F_GOST_DO_SIGN, ERR_R_MALLOC_FAILURE);
66 newsig = DSA_SIG_new();
68 GOSTerr(GOST_F_GOST_DO_SIGN, ERR_R_MALLOC_FAILURE);
71 tmp = BN_CTX_get(ctx);
73 tmp2 = BN_CTX_get(ctx);
74 if(!tmp || !k || !tmp2) {
75 GOSTerr(GOST_F_GOST_DO_SIGN, ERR_R_MALLOC_FAILURE);
78 BN_mod(tmp, md, dsa->q, ctx);
79 if (BN_is_zero(tmp)) {
85 * Generate random number k less than q
87 BN_rand_range(k, dsa->q);
88 /* generate r = (a^x mod p) mod q */
89 BN_mod_exp(tmp, dsa->g, k, dsa->p, ctx);
93 GOSTerr(GOST_F_GOST_DO_SIGN, ERR_R_MALLOC_FAILURE);
97 BN_mod(newsig->r, tmp, dsa->q, ctx);
99 while (BN_is_zero(newsig->r));
100 /* generate s = (xr + k(Hm)) mod q */
101 BN_mod_mul(tmp, dsa->priv_key, newsig->r, dsa->q, ctx);
102 BN_mod_mul(tmp2, k, md, dsa->q, ctx);
104 newsig->s = BN_new();
106 GOSTerr(GOST_F_GOST_DO_SIGN, ERR_R_MALLOC_FAILURE);
110 BN_mod_add(newsig->s, tmp, tmp2, dsa->q, ctx);
112 while (BN_is_zero(newsig->s));
122 DSA_SIG_free(newsig);
128 * Packs signature according to Cryptocom rules
129 * and frees up DSA_SIG structure
132 int pack_sign_cc(DSA_SIG *s,int order,unsigned char *sig, size_t *siglen)
135 memset(sig,0,*siglen);
136 store_bignum(s->r, sig,order);
137 store_bignum(s->s, sig + order,order);
138 dump_signature("serialized",sig,*siglen);
144 * Packs signature according to Cryptopro rules
145 * and frees up DSA_SIG structure
147 int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
150 memset(sig, 0, *siglen);
151 store_bignum(s->s, sig, order);
152 store_bignum(s->r, sig + order, order);
153 dump_signature("serialized", sig, *siglen);
159 * Verifies signature passed as DSA_SIG structure
163 int gost_do_verify(const unsigned char *dgst, int dgst_len,
164 DSA_SIG *sig, DSA *dsa)
166 BIGNUM *md = NULL, *tmp = NULL;
168 BIGNUM *u = NULL, *v = NULL, *z1 = NULL, *z2 = NULL;
169 BIGNUM *tmp2 = NULL, *tmp3 = NULL;
171 BN_CTX *ctx = BN_CTX_new();
173 GOSTerr(GOST_F_GOST_DO_VERIFY, ERR_R_MALLOC_FAILURE);
178 if (BN_cmp(sig->s, dsa->q) >= 1 || BN_cmp(sig->r, dsa->q) >= 1) {
179 GOSTerr(GOST_F_GOST_DO_VERIFY, GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
182 md = hashsum2bn(dgst);
184 tmp = BN_CTX_get(ctx);
186 q2 = BN_CTX_get(ctx);
187 z1 = BN_CTX_get(ctx);
188 z2 = BN_CTX_get(ctx);
189 tmp2 = BN_CTX_get(ctx);
190 tmp3 = BN_CTX_get(ctx);
192 if(!tmp || !v || !q2 || !z1 || !z2 || !tmp2 || !tmp3 || !u) {
193 GOSTerr(GOST_F_GOST_DO_VERIFY, ERR_R_MALLOC_FAILURE);
197 BN_mod(tmp, md, dsa->q, ctx);
198 if (BN_is_zero(tmp)) {
203 BN_mod_exp(v, md, q2, dsa->q, ctx);
204 BN_mod_mul(z1, sig->s, v, dsa->q, ctx);
205 BN_sub(tmp, dsa->q, sig->r);
206 BN_mod_mul(z2, tmp, v, dsa->p, ctx);
207 BN_mod_exp(tmp, dsa->g, z1, dsa->p, ctx);
208 BN_mod_exp(tmp2, dsa->pub_key, z2, dsa->p, ctx);
209 BN_mod_mul(tmp3, tmp, tmp2, dsa->p, ctx);
210 BN_mod(u, tmp3, dsa->q, ctx);
211 ok = BN_cmp(u, sig->r);
214 GOSTerr(GOST_F_GOST_DO_VERIFY, GOST_R_SIGNATURE_MISMATCH);
226 * Computes public keys for GOST R 34.10-94 algorithm
229 int gost94_compute_public(DSA *dsa)
231 /* Now fill algorithm parameters with correct values */
234 GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, GOST_R_KEY_IS_NOT_INITALIZED);
239 GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
243 dsa->pub_key = BN_new();
245 GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
249 /* Compute public key y = a^x mod p */
250 BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx);
256 * Fill GOST 94 params, searching them in R3410_paramset array
260 int fill_GOST94_params(DSA *dsa, int nid)
262 R3410_params *params = R3410_paramset;
263 while (params->nid != NID_undef && params->nid != nid)
265 if (params->nid == NID_undef) {
266 GOSTerr(GOST_F_FILL_GOST94_PARAMS, GOST_R_UNSUPPORTED_PARAMETER_SET);
269 #define dump_signature(a,b,c)
274 BN_dec2bn(&(dsa->p), params->p);
279 BN_dec2bn(&(dsa->q), params->q);
284 BN_dec2bn(&(dsa->g), params->a);
289 * Generate GOST R 34.10-94 keypair
293 int gost_sign_keygen(DSA *dsa)
295 dsa->priv_key = BN_new();
297 GOSTerr(GOST_F_GOST_SIGN_KEYGEN, ERR_R_MALLOC_FAILURE);
300 BN_rand_range(dsa->priv_key, dsa->q);
301 return gost94_compute_public(dsa);
304 /* Unpack signature according to cryptocom rules */
306 DSA_SIG *unpack_cc_signature(const unsigned char *sig,size_t siglen)
312 GOSTerr(GOST_F_UNPACK_CC_SIGNATURE,ERR_R_MALLOC_FAILURE);
315 s->r = getbnfrombuf(sig, siglen/2);
316 s->s = getbnfrombuf(sig + siglen/2, siglen/2);
320 /* Unpack signature according to cryptopro rules */
321 DSA_SIG *unpack_cp_signature(const unsigned char *sig, size_t siglen)
327 GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, ERR_R_MALLOC_FAILURE);
330 s->s = getbnfrombuf(sig, siglen / 2);
331 s->r = getbnfrombuf(sig + siglen / 2, siglen / 2);
335 /* Convert little-endian byte array into bignum */
336 BIGNUM *hashsum2bn(const unsigned char *dgst)
338 unsigned char buf[32];
340 for (i = 0; i < 32; i++) {
341 buf[31 - i] = dgst[i];
343 return getbnfrombuf(buf, 32);
346 /* Convert byte buffer to bignum, skipping leading zeros*/
347 BIGNUM *getbnfrombuf(const unsigned char *buf, size_t len)
349 while (*buf == 0 && len > 0) {
354 return BN_bin2bn(buf, len, NULL);
356 BIGNUM *b = BN_new();
363 * Pack bignum into byte buffer of given size, filling all leading bytes by
366 int store_bignum(BIGNUM *bn, unsigned char *buf, int len)
368 int bytes = BN_num_bytes(bn);
372 BN_bn2bin(bn, buf + len - bytes);