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>
17 #include "gost_params.h"
19 #include "e_gost_err.h"
22 void dump_signature(const char *message, const unsigned char *buffer,
26 fprintf(stderr, "signature %s Length=%d", message, len);
27 for (i = 0; i < len; i++) {
30 fprintf(stderr, " %02x", buffer[i]);
32 fprintf(stderr, "\nEnd of signature\n");
35 void dump_dsa_sig(const char *message, DSA_SIG *sig)
37 fprintf(stderr, "%s\nR=", message);
38 BN_print_fp(stderr, sig->r);
39 fprintf(stderr, "\nS=");
40 BN_print_fp(stderr, sig->s);
41 fprintf(stderr, "\n");
46 # define dump_signature(a,b,c)
47 # define dump_dsa_sig(a,b)
51 * Computes signature and returns it as DSA_SIG structure
53 DSA_SIG *gost_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
55 BIGNUM *k = NULL, *tmp = NULL, *tmp2 = NULL;
56 DSA_SIG *newsig = DSA_SIG_new();
57 BIGNUM *md = hashsum2bn(dgst);
58 /* check if H(M) mod q is zero */
59 BN_CTX *ctx = BN_CTX_new();
62 GOSTerr(GOST_F_GOST_DO_SIGN, ERR_R_MALLOC_FAILURE);
65 tmp = BN_CTX_get(ctx);
67 tmp2 = BN_CTX_get(ctx);
68 BN_mod(tmp, md, dsa->q, ctx);
69 if (BN_is_zero(tmp)) {
75 * Generate random number k less than q
77 BN_rand_range(k, dsa->q);
78 /* generate r = (a^x mod p) mod q */
79 BN_mod_exp(tmp, dsa->g, k, dsa->p, ctx);
82 BN_mod(newsig->r, tmp, dsa->q, ctx);
84 while (BN_is_zero(newsig->r));
85 /* generate s = (xr + k(Hm)) mod q */
86 BN_mod_mul(tmp, dsa->priv_key, newsig->r, dsa->q, ctx);
87 BN_mod_mul(tmp2, k, md, dsa->q, ctx);
90 BN_mod_add(newsig->s, tmp, tmp2, dsa->q, ctx);
92 while (BN_is_zero(newsig->s));
101 * Packs signature according to Cryptocom rules
102 * and frees up DSA_SIG structure
105 int pack_sign_cc(DSA_SIG *s,int order,unsigned char *sig, size_t *siglen)
108 memset(sig,0,*siglen);
109 store_bignum(s->r, sig,order);
110 store_bignum(s->s, sig + order,order);
111 dump_signature("serialized",sig,*siglen);
117 * Packs signature according to Cryptopro rules
118 * and frees up DSA_SIG structure
120 int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
123 memset(sig, 0, *siglen);
124 store_bignum(s->s, sig, order);
125 store_bignum(s->r, sig + order, order);
126 dump_signature("serialized", sig, *siglen);
132 * Verifies signature passed as DSA_SIG structure
136 int gost_do_verify(const unsigned char *dgst, int dgst_len,
137 DSA_SIG *sig, DSA *dsa)
139 BIGNUM *md, *tmp = NULL;
141 BIGNUM *u = NULL, *v = NULL, *z1 = NULL, *z2 = NULL;
142 BIGNUM *tmp2 = NULL, *tmp3 = NULL;
144 BN_CTX *ctx = BN_CTX_new();
147 if (BN_cmp(sig->s, dsa->q) >= 1 || BN_cmp(sig->r, dsa->q) >= 1) {
148 GOSTerr(GOST_F_GOST_DO_VERIFY, GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
151 md = hashsum2bn(dgst);
153 tmp = BN_CTX_get(ctx);
155 q2 = BN_CTX_get(ctx);
156 z1 = BN_CTX_get(ctx);
157 z2 = BN_CTX_get(ctx);
158 tmp2 = BN_CTX_get(ctx);
159 tmp3 = BN_CTX_get(ctx);
162 BN_mod(tmp, md, dsa->q, ctx);
163 if (BN_is_zero(tmp)) {
168 BN_mod_exp(v, md, q2, dsa->q, ctx);
169 BN_mod_mul(z1, sig->s, v, dsa->q, ctx);
170 BN_sub(tmp, dsa->q, sig->r);
171 BN_mod_mul(z2, tmp, v, dsa->p, ctx);
172 BN_mod_exp(tmp, dsa->g, z1, dsa->p, ctx);
173 BN_mod_exp(tmp2, dsa->pub_key, z2, dsa->p, ctx);
174 BN_mod_mul(tmp3, tmp, tmp2, dsa->p, ctx);
175 BN_mod(u, tmp3, dsa->q, ctx);
176 ok = BN_cmp(u, sig->r);
182 GOSTerr(GOST_F_GOST_DO_VERIFY, GOST_R_SIGNATURE_MISMATCH);
188 * Computes public keys for GOST R 34.10-94 algorithm
191 int gost94_compute_public(DSA *dsa)
193 /* Now fill algorithm parameters with correct values */
194 BN_CTX *ctx = BN_CTX_new();
196 GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, GOST_R_KEY_IS_NOT_INITALIZED);
199 /* Compute public key y = a^x mod p */
200 dsa->pub_key = BN_new();
201 BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx);
207 * Fill GOST 94 params, searching them in R3410_paramset array
211 int fill_GOST94_params(DSA *dsa, int nid)
213 R3410_params *params = R3410_paramset;
214 while (params->nid != NID_undef && params->nid != nid)
216 if (params->nid == NID_undef) {
217 GOSTerr(GOST_F_FILL_GOST94_PARAMS, GOST_R_UNSUPPORTED_PARAMETER_SET);
220 #define dump_signature(a,b,c)
225 BN_dec2bn(&(dsa->p), params->p);
230 BN_dec2bn(&(dsa->q), params->q);
235 BN_dec2bn(&(dsa->g), params->a);
240 * Generate GOST R 34.10-94 keypair
244 int gost_sign_keygen(DSA *dsa)
246 dsa->priv_key = BN_new();
247 BN_rand_range(dsa->priv_key, dsa->q);
248 return gost94_compute_public(dsa);
251 /* Unpack signature according to cryptocom rules */
253 DSA_SIG *unpack_cc_signature(const unsigned char *sig,size_t siglen)
259 GOSTerr(GOST_F_UNPACK_CC_SIGNATURE,ERR_R_MALLOC_FAILURE);
262 s->r = getbnfrombuf(sig, siglen/2);
263 s->s = getbnfrombuf(sig + siglen/2, siglen/2);
267 /* Unpack signature according to cryptopro rules */
268 DSA_SIG *unpack_cp_signature(const unsigned char *sig, size_t siglen)
274 GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, ERR_R_MALLOC_FAILURE);
277 s->s = getbnfrombuf(sig, siglen / 2);
278 s->r = getbnfrombuf(sig + siglen / 2, siglen / 2);
282 /* Convert little-endian byte array into bignum */
283 BIGNUM *hashsum2bn(const unsigned char *dgst)
285 unsigned char buf[32];
287 for (i = 0; i < 32; i++) {
288 buf[31 - i] = dgst[i];
290 return getbnfrombuf(buf, 32);
293 /* Convert byte buffer to bignum, skipping leading zeros*/
294 BIGNUM *getbnfrombuf(const unsigned char *buf, size_t len)
296 while (*buf == 0 && len > 0) {
301 return BN_bin2bn(buf, len, NULL);
303 BIGNUM *b = BN_new();
310 * Pack bignum into byte buffer of given size, filling all leading bytes by
313 int store_bignum(BIGNUM *bn, unsigned char *buf, int len)
315 int bytes = BN_num_bytes(bn);
319 BN_bn2bin(bn, buf + len - bytes);