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/evp.h>
15 #include <openssl/err.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 = NULL, *ret = NULL;
57 BIGNUM *md = hashsum2bn(dgst);
58 /* check if H(M) mod q is zero */
59 BN_CTX *ctx = BN_CTX_new();
61 GOSTerr(GOST_F_GOST_DO_SIGN, ERR_R_MALLOC_FAILURE);
65 newsig = DSA_SIG_new();
67 GOSTerr(GOST_F_GOST_DO_SIGN, GOST_R_NO_MEMORY);
70 tmp = BN_CTX_get(ctx);
72 tmp2 = BN_CTX_get(ctx);
73 if(!tmp || !k || !tmp2) {
74 GOSTerr(GOST_F_GOST_DO_SIGN, ERR_R_MALLOC_FAILURE);
77 BN_mod(tmp, md, dsa->q, ctx);
78 if (BN_is_zero(tmp)) {
84 * Generate random number k less than q
86 BN_rand_range(k, dsa->q);
87 /* generate r = (a^x mod p) mod q */
88 BN_mod_exp(tmp, dsa->g, k, dsa->p, ctx);
92 GOSTerr(GOST_F_GOST_DO_SIGN, ERR_R_MALLOC_FAILURE);
96 BN_mod(newsig->r, tmp, dsa->q, ctx);
98 while (BN_is_zero(newsig->r));
99 /* generate s = (xr + k(Hm)) mod q */
100 BN_mod_mul(tmp, dsa->priv_key, newsig->r, dsa->q, ctx);
101 BN_mod_mul(tmp2, k, md, dsa->q, ctx);
103 newsig->s = BN_new();
105 GOSTerr(GOST_F_GOST_DO_SIGN, ERR_R_MALLOC_FAILURE);
109 BN_mod_add(newsig->s, tmp, tmp2, dsa->q, ctx);
111 while (BN_is_zero(newsig->s));
121 DSA_SIG_free(newsig);
127 * Packs signature according to Cryptocom rules
128 * and frees up DSA_SIG structure
131 int pack_sign_cc(DSA_SIG *s,int order,unsigned char *sig, size_t *siglen)
134 memset(sig,0,*siglen);
135 store_bignum(s->r, sig,order);
136 store_bignum(s->s, sig + order,order);
137 dump_signature("serialized",sig,*siglen);
143 * Packs signature according to Cryptopro rules
144 * and frees up DSA_SIG structure
146 int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
149 memset(sig, 0, *siglen);
150 store_bignum(s->s, sig, order);
151 store_bignum(s->r, sig + order, order);
152 dump_signature("serialized", sig, *siglen);
158 * Verifies signature passed as DSA_SIG structure
162 int gost_do_verify(const unsigned char *dgst, int dgst_len,
163 DSA_SIG *sig, DSA *dsa)
165 BIGNUM *md = NULL, *tmp = NULL;
167 BIGNUM *u = NULL, *v = NULL, *z1 = NULL, *z2 = NULL;
168 BIGNUM *tmp2 = NULL, *tmp3 = NULL;
170 BN_CTX *ctx = BN_CTX_new();
172 GOSTerr(GOST_F_GOST_DO_VERIFY, ERR_R_MALLOC_FAILURE);
177 if (BN_cmp(sig->s, dsa->q) >= 1 || BN_cmp(sig->r, dsa->q) >= 1) {
178 GOSTerr(GOST_F_GOST_DO_VERIFY, GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
181 md = hashsum2bn(dgst);
183 tmp = BN_CTX_get(ctx);
185 q2 = BN_CTX_get(ctx);
186 z1 = BN_CTX_get(ctx);
187 z2 = BN_CTX_get(ctx);
188 tmp2 = BN_CTX_get(ctx);
189 tmp3 = BN_CTX_get(ctx);
191 if(!tmp || !v || !q2 || !z1 || !z2 || !tmp2 || !tmp3 || !u) {
192 GOSTerr(GOST_F_GOST_DO_VERIFY, ERR_R_MALLOC_FAILURE);
196 BN_mod(tmp, md, dsa->q, ctx);
197 if (BN_is_zero(tmp)) {
202 BN_mod_exp(v, md, q2, dsa->q, ctx);
203 BN_mod_mul(z1, sig->s, v, dsa->q, ctx);
204 BN_sub(tmp, dsa->q, sig->r);
205 BN_mod_mul(z2, tmp, v, dsa->p, ctx);
206 BN_mod_exp(tmp, dsa->g, z1, dsa->p, ctx);
207 BN_mod_exp(tmp2, dsa->pub_key, z2, dsa->p, ctx);
208 BN_mod_mul(tmp3, tmp, tmp2, dsa->p, ctx);
209 BN_mod(u, tmp3, dsa->q, ctx);
210 ok = (BN_cmp(u, sig->r) == 0);
213 GOSTerr(GOST_F_GOST_DO_VERIFY, GOST_R_SIGNATURE_MISMATCH);
225 * Computes public keys for GOST R 34.10-94 algorithm
228 int gost94_compute_public(DSA *dsa)
230 /* Now fill algorithm parameters with correct values */
233 GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, GOST_R_KEY_IS_NOT_INITALIZED);
238 GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
242 dsa->pub_key = BN_new();
244 GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
248 /* Compute public key y = a^x mod p */
249 BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx);
255 * Fill GOST 94 params, searching them in R3410_paramset array
259 int fill_GOST94_params(DSA *dsa, int nid)
261 R3410_params *params = R3410_paramset;
262 while (params->nid != NID_undef && params->nid != nid)
264 if (params->nid == NID_undef) {
265 GOSTerr(GOST_F_FILL_GOST94_PARAMS, GOST_R_UNSUPPORTED_PARAMETER_SET);
268 #define dump_signature(a,b,c)
273 BN_dec2bn(&(dsa->p), params->p);
278 BN_dec2bn(&(dsa->q), params->q);
283 BN_dec2bn(&(dsa->g), params->a);
288 * Generate GOST R 34.10-94 keypair
292 int gost_sign_keygen(DSA *dsa)
294 dsa->priv_key = BN_new();
296 GOSTerr(GOST_F_GOST_SIGN_KEYGEN, ERR_R_MALLOC_FAILURE);
299 BN_rand_range(dsa->priv_key, dsa->q);
300 return gost94_compute_public(dsa);
303 /* Unpack signature according to cryptocom rules */
305 DSA_SIG *unpack_cc_signature(const unsigned char *sig,size_t siglen)
311 GOSTerr(GOST_F_UNPACK_CC_SIGNATURE,GOST_R_NO_MEMORY);
314 s->r = getbnfrombuf(sig, siglen/2);
315 s->s = getbnfrombuf(sig + siglen/2, siglen/2);
319 /* Unpack signature according to cryptopro rules */
320 DSA_SIG *unpack_cp_signature(const unsigned char *sig, size_t siglen)
326 GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, GOST_R_NO_MEMORY);
329 s->s = getbnfrombuf(sig, siglen / 2);
330 s->r = getbnfrombuf(sig + siglen / 2, siglen / 2);
334 /* Convert little-endian byte array into bignum */
335 BIGNUM *hashsum2bn(const unsigned char *dgst)
337 unsigned char buf[32];
339 for (i = 0; i < 32; i++) {
340 buf[31 - i] = dgst[i];
342 return getbnfrombuf(buf, 32);
345 /* Convert byte buffer to bignum, skipping leading zeros*/
346 BIGNUM *getbnfrombuf(const unsigned char *buf, size_t len)
348 while (*buf == 0 && len > 0) {
353 return BN_bin2bn(buf, len, NULL);
355 BIGNUM *b = BN_new();
362 * Pack bignum into byte buffer of given size, filling all leading bytes by
365 int store_bignum(BIGNUM *bn, unsigned char *buf, int len)
367 int bytes = BN_num_bytes(bn);
371 BN_bn2bin(bn, buf + len - bytes);