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 = NULL, *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));
121 DSA_SIG_free(newsig);
126 * Packs signature according to Cryptocom rules
127 * and frees up DSA_SIG structure
130 int pack_sign_cc(DSA_SIG *s,int order,unsigned char *sig, size_t *siglen)
133 memset(sig,0,*siglen);
134 store_bignum(s->r, sig,order);
135 store_bignum(s->s, sig + order,order);
136 dump_signature("serialized",sig,*siglen);
142 * Packs signature according to Cryptopro rules
143 * and frees up DSA_SIG structure
145 int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
148 memset(sig, 0, *siglen);
149 store_bignum(s->s, sig, order);
150 store_bignum(s->r, sig + order, order);
151 dump_signature("serialized", sig, *siglen);
157 * Verifies signature passed as DSA_SIG structure
161 int gost_do_verify(const unsigned char *dgst, int dgst_len,
162 DSA_SIG *sig, DSA *dsa)
164 BIGNUM *md = NULL, *tmp = NULL;
166 BIGNUM *u = NULL, *v = NULL, *z1 = NULL, *z2 = NULL;
167 BIGNUM *tmp2 = NULL, *tmp3 = NULL;
169 BN_CTX *ctx = BN_CTX_new();
171 GOSTerr(GOST_F_GOST_DO_VERIFY, ERR_R_MALLOC_FAILURE);
176 if (BN_cmp(sig->s, dsa->q) >= 1 || BN_cmp(sig->r, dsa->q) >= 1) {
177 GOSTerr(GOST_F_GOST_DO_VERIFY, GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
180 md = hashsum2bn(dgst);
182 tmp = BN_CTX_get(ctx);
184 q2 = BN_CTX_get(ctx);
185 z1 = BN_CTX_get(ctx);
186 z2 = BN_CTX_get(ctx);
187 tmp2 = BN_CTX_get(ctx);
188 tmp3 = BN_CTX_get(ctx);
190 if (!tmp || !v || !q2 || !z1 || !z2 || !tmp2 || !tmp3 || !u) {
191 GOSTerr(GOST_F_GOST_DO_VERIFY, ERR_R_MALLOC_FAILURE);
195 BN_mod(tmp, md, dsa->q, ctx);
196 if (BN_is_zero(tmp)) {
201 BN_mod_exp(v, md, q2, dsa->q, ctx);
202 BN_mod_mul(z1, sig->s, v, dsa->q, ctx);
203 BN_sub(tmp, dsa->q, sig->r);
204 BN_mod_mul(z2, tmp, v, dsa->p, ctx);
205 BN_mod_exp(tmp, dsa->g, z1, dsa->p, ctx);
206 BN_mod_exp(tmp2, dsa->pub_key, z2, dsa->p, ctx);
207 BN_mod_mul(tmp3, tmp, tmp2, dsa->p, ctx);
208 BN_mod(u, tmp3, dsa->q, ctx);
209 ok = (BN_cmp(u, sig->r) == 0);
212 GOSTerr(GOST_F_GOST_DO_VERIFY, GOST_R_SIGNATURE_MISMATCH);
223 * Computes public keys for GOST R 34.10-94 algorithm
226 int gost94_compute_public(DSA *dsa)
228 /* Now fill algorithm parameters with correct values */
231 GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, GOST_R_KEY_IS_NOT_INITALIZED);
236 GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
240 dsa->pub_key = BN_new();
242 GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
246 /* Compute public key y = a^x mod p */
247 BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx);
253 * Fill GOST 94 params, searching them in R3410_paramset array
257 int fill_GOST94_params(DSA *dsa, int nid)
259 R3410_params *params = R3410_paramset;
260 while (params->nid != NID_undef && params->nid != nid)
262 if (params->nid == NID_undef) {
263 GOSTerr(GOST_F_FILL_GOST94_PARAMS, GOST_R_UNSUPPORTED_PARAMETER_SET);
266 #define dump_signature(a,b,c)
269 BN_dec2bn(&(dsa->p), params->p);
272 BN_dec2bn(&(dsa->q), params->q);
275 BN_dec2bn(&(dsa->g), params->a);
280 * Generate GOST R 34.10-94 keypair
284 int gost_sign_keygen(DSA *dsa)
286 dsa->priv_key = BN_new();
287 if (!dsa->priv_key) {
288 GOSTerr(GOST_F_GOST_SIGN_KEYGEN, ERR_R_MALLOC_FAILURE);
291 BN_rand_range(dsa->priv_key, dsa->q);
292 return gost94_compute_public(dsa);
295 /* Unpack signature according to cryptocom rules */
297 DSA_SIG *unpack_cc_signature(const unsigned char *sig,size_t siglen)
303 GOSTerr(GOST_F_UNPACK_CC_SIGNATURE,ERR_R_MALLOC_FAILURE);
306 s->r = getbnfrombuf(sig, siglen/2);
307 s->s = getbnfrombuf(sig + siglen/2, siglen/2);
311 /* Unpack signature according to cryptopro rules */
312 DSA_SIG *unpack_cp_signature(const unsigned char *sig, size_t siglen)
318 GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, ERR_R_MALLOC_FAILURE);
321 s->s = getbnfrombuf(sig, siglen / 2);
322 s->r = getbnfrombuf(sig + siglen / 2, siglen / 2);
326 /* Convert little-endian byte array into bignum */
327 BIGNUM *hashsum2bn(const unsigned char *dgst)
329 unsigned char buf[32];
331 for (i = 0; i < 32; i++) {
332 buf[31 - i] = dgst[i];
334 return getbnfrombuf(buf, 32);
337 /* Convert byte buffer to bignum, skipping leading zeros*/
338 BIGNUM *getbnfrombuf(const unsigned char *buf, size_t len)
340 while (*buf == 0 && len > 0) {
345 return BN_bin2bn(buf, len, NULL);
347 BIGNUM *b = BN_new();
354 * Pack bignum into byte buffer of given size, filling all leading bytes by
357 int store_bignum(BIGNUM *bn, unsigned char *buf, int len)
359 int bytes = BN_num_bytes(bn);
363 BN_bn2bin(bn, buf + len - bytes);