2006-09-17 13:00:18 +00:00
|
|
|
/**********************************************************************
|
|
|
|
* gost_sign.c *
|
|
|
|
* Copyright (c) 2005-2006 Cryptocom LTD *
|
|
|
|
* This file is distributed under the same license as OpenSSL *
|
|
|
|
* *
|
|
|
|
* Implementation of GOST R 34.10-94 signature algoritgthm *
|
|
|
|
* for OpenSSL *
|
|
|
|
* Requires OpenSSL 0.9.9 for compilation *
|
|
|
|
**********************************************************************/
|
|
|
|
#include <string.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/dsa.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
|
2006-09-21 13:04:43 +00:00
|
|
|
#include "gost_params.h"
|
|
|
|
#include "gost_lcl.h"
|
2006-09-17 13:00:18 +00:00
|
|
|
#include "e_gost_err.h"
|
|
|
|
|
2006-09-21 13:04:43 +00:00
|
|
|
#ifdef DEBUG_SIGN
|
|
|
|
void dump_signature(const char *message,const unsigned char *buffer,size_t len)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
fprintf(stderr,"signature %s Length=%d",message,len);
|
|
|
|
for (i=0; i<len; i++)
|
|
|
|
{
|
|
|
|
if (i% 16 ==0) fputc('\n',stderr);
|
|
|
|
fprintf (stderr," %02x",buffer[i]);
|
2006-09-17 13:00:18 +00:00
|
|
|
}
|
2006-09-21 13:04:43 +00:00
|
|
|
fprintf(stderr,"\nEnd of signature\n");
|
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
|
2006-09-21 13:04:43 +00:00
|
|
|
void dump_dsa_sig(const char *message, DSA_SIG *sig)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
fprintf(stderr,"%s\nR=",message);
|
|
|
|
BN_print_fp(stderr,sig->r);
|
|
|
|
fprintf(stderr,"\nS=");
|
|
|
|
BN_print_fp(stderr,sig->s);
|
|
|
|
fprintf(stderr,"\n");
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define dump_signature(a,b,c)
|
|
|
|
#define dump_dsa_sig(a,b)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Computes signature and returns it as DSA_SIG structure
|
|
|
|
*/
|
2006-09-21 13:04:43 +00:00
|
|
|
DSA_SIG *gost_do_sign(const unsigned char *dgst,int dlen, DSA *dsa)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
BIGNUM *k=NULL,*tmp=NULL,*tmp2=NULL;
|
|
|
|
DSA_SIG *newsig = DSA_SIG_new();
|
|
|
|
BIGNUM *md = hashsum2bn(dgst);
|
|
|
|
/* check if H(M) mod q is zero */
|
|
|
|
BN_CTX *ctx=BN_CTX_new();
|
|
|
|
BN_CTX_start(ctx);
|
2006-09-21 13:04:43 +00:00
|
|
|
if (!newsig)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
GOSTerr(GOST_F_GOST_DO_SIGN,GOST_R_NO_MEMORY);
|
|
|
|
goto err;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
tmp=BN_CTX_get(ctx);
|
|
|
|
k = BN_CTX_get(ctx);
|
|
|
|
tmp2 = BN_CTX_get(ctx);
|
|
|
|
BN_mod(tmp,md,dsa->q,ctx);
|
2006-09-21 13:04:43 +00:00
|
|
|
if (BN_is_zero(tmp))
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
BN_one(md);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
do
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
/*Generate random number k less than q*/
|
|
|
|
BN_rand_range(k,dsa->q);
|
|
|
|
/* generate r = (a^x mod p) mod q */
|
|
|
|
BN_mod_exp(tmp,dsa->g, k, dsa->p,ctx);
|
|
|
|
if (!(newsig->r)) newsig->r=BN_new();
|
|
|
|
BN_mod(newsig->r,tmp,dsa->q,ctx);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
while (BN_is_zero(newsig->r));
|
2006-09-17 13:00:18 +00:00
|
|
|
/* generate s = (xr + k(Hm)) mod q */
|
|
|
|
BN_mod_mul(tmp,dsa->priv_key,newsig->r,dsa->q,ctx);
|
|
|
|
BN_mod_mul(tmp2,k,md,dsa->q,ctx);
|
|
|
|
if (!newsig->s) newsig->s=BN_new();
|
|
|
|
BN_mod_add(newsig->s,tmp,tmp2,dsa->q,ctx);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
while (BN_is_zero(newsig->s));
|
|
|
|
err:
|
2006-09-17 13:00:18 +00:00
|
|
|
BN_free(md);
|
|
|
|
BN_CTX_end(ctx);
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
return newsig;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Packs signature according to Cryptocom rules
|
|
|
|
* and frees up DSA_SIG structure
|
|
|
|
*/
|
|
|
|
|
2006-09-21 13:04:43 +00:00
|
|
|
int pack_sign_cc(DSA_SIG *s,int order,unsigned char *sig, size_t *siglen)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
*siglen = 2*order;
|
|
|
|
memset(sig,0,*siglen);
|
|
|
|
store_bignum(s->r, sig,order);
|
|
|
|
store_bignum(s->s, sig + order,order);
|
|
|
|
dump_signature("serialized",sig,*siglen);
|
|
|
|
DSA_SIG_free(s);
|
|
|
|
return 1;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
/*
|
|
|
|
* Packs signature according to Cryptopro rules
|
|
|
|
* and frees up DSA_SIG structure
|
|
|
|
*/
|
2006-09-21 13:04:43 +00:00
|
|
|
int pack_sign_cp(DSA_SIG *s,int order,unsigned char *sig, size_t *siglen)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
*siglen = 2*order;
|
|
|
|
memset(sig,0,*siglen);
|
|
|
|
store_bignum(s->s, sig, order);
|
|
|
|
store_bignum(s->r, sig+order,order);
|
|
|
|
dump_signature("serialized",sig,*siglen);
|
|
|
|
DSA_SIG_free(s);
|
|
|
|
return 1;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Verifies signature passed as DSA_SIG structure
|
|
|
|
*
|
2006-09-21 13:04:43 +00:00
|
|
|
*/
|
2006-09-17 13:00:18 +00:00
|
|
|
|
|
|
|
int gost_do_verify(const unsigned char *dgst, int dgst_len,
|
2006-09-21 13:04:43 +00:00
|
|
|
DSA_SIG *sig, DSA *dsa)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
BIGNUM *md, *tmp=NULL;
|
|
|
|
BIGNUM *q2=NULL;
|
|
|
|
BIGNUM *u=NULL,*v=NULL,*z1=NULL,*z2=NULL;
|
|
|
|
BIGNUM *tmp2=NULL,*tmp3=NULL;
|
|
|
|
int ok;
|
|
|
|
BN_CTX *ctx = BN_CTX_new();
|
|
|
|
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
if (BN_cmp(sig->s,dsa->q)>=1||
|
2006-09-21 13:04:43 +00:00
|
|
|
BN_cmp(sig->r,dsa->q)>=1)
|
|
|
|
{
|
|
|
|
GOSTerr(GOST_F_GOST_DO_VERIFY,GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
md=hashsum2bn(dgst);
|
|
|
|
|
|
|
|
tmp=BN_CTX_get(ctx);
|
|
|
|
v=BN_CTX_get(ctx);
|
|
|
|
q2=BN_CTX_get(ctx);
|
|
|
|
z1=BN_CTX_get(ctx);
|
|
|
|
z2=BN_CTX_get(ctx);
|
|
|
|
tmp2=BN_CTX_get(ctx);
|
|
|
|
tmp3=BN_CTX_get(ctx);
|
|
|
|
u = BN_CTX_get(ctx);
|
|
|
|
|
|
|
|
BN_mod(tmp,md,dsa->q,ctx);
|
2006-09-21 13:04:43 +00:00
|
|
|
if (BN_is_zero(tmp))
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
BN_one(md);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
BN_copy(q2,dsa->q);
|
|
|
|
BN_sub_word(q2,2);
|
|
|
|
BN_mod_exp(v,md,q2,dsa->q,ctx);
|
|
|
|
BN_mod_mul(z1,sig->s,v,dsa->q,ctx);
|
|
|
|
BN_sub(tmp,dsa->q,sig->r);
|
|
|
|
BN_mod_mul(z2,tmp,v,dsa->p,ctx);
|
|
|
|
BN_mod_exp(tmp,dsa->g,z1,dsa->p,ctx);
|
|
|
|
BN_mod_exp(tmp2,dsa->pub_key,z2,dsa->p,ctx);
|
|
|
|
BN_mod_mul(tmp3,tmp,tmp2,dsa->p,ctx);
|
|
|
|
BN_mod(u,tmp3,dsa->q,ctx);
|
|
|
|
ok= BN_cmp(u,sig->r);
|
|
|
|
|
|
|
|
BN_free(md);
|
|
|
|
BN_CTX_end(ctx);
|
|
|
|
BN_CTX_free(ctx);
|
2006-09-21 13:04:43 +00:00
|
|
|
if (ok!=0)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
GOSTerr(GOST_F_GOST_DO_VERIFY,GOST_R_SIGNATURE_MISMATCH);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
return (ok==0);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Computes public keys for GOST R 34.10-94 algorithm
|
2006-09-21 13:04:43 +00:00
|
|
|
*
|
2006-09-17 13:00:18 +00:00
|
|
|
*/
|
|
|
|
int gost94_compute_public(DSA *dsa)
|
2006-09-21 13:04:43 +00:00
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Now fill algorithm parameters with correct values */
|
|
|
|
BN_CTX *ctx = BN_CTX_new();
|
2006-09-21 13:04:43 +00:00
|
|
|
if (!dsa->g)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
GOSTerr(GOST_F_GOST_COMPUTE_PUBLIC,GOST_R_KEY_IS_NOT_INITALIZED);
|
|
|
|
return 0;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Compute public key y = a^x mod p */
|
|
|
|
dsa->pub_key=BN_new();
|
|
|
|
BN_mod_exp(dsa->pub_key, dsa->g,dsa->priv_key,dsa->p,ctx);
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
return 1;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill GOST 94 params, searching them in R3410_paramset array
|
|
|
|
* by nid of paramset
|
2006-09-21 13:04:43 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
int fill_GOST94_params(DSA *dsa,int nid)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
R3410_params *params=R3410_paramset;
|
|
|
|
while (params->nid!=NID_undef && params->nid !=nid) params++;
|
2006-09-21 13:04:43 +00:00
|
|
|
if (params->nid == NID_undef)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
GOSTerr(GOST_F_FILL_GOST94_PARAMS,GOST_R_UNSUPPORTED_PARAMETER_SET);
|
|
|
|
return 0;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
#define dump_signature(a,b,c)
|
|
|
|
if (dsa->p) { BN_free(dsa->p); }
|
|
|
|
dsa->p=NULL;
|
|
|
|
BN_dec2bn(&(dsa->p),params->p);
|
|
|
|
if (dsa->q) { BN_free(dsa->q); }
|
|
|
|
dsa->q=NULL;
|
|
|
|
BN_dec2bn(&(dsa->q),params->q);
|
|
|
|
if (dsa->g) { BN_free(dsa->g); }
|
|
|
|
dsa->g=NULL;
|
|
|
|
BN_dec2bn(&(dsa->g),params->a);
|
|
|
|
return 1;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate GOST R 34.10-94 keypair
|
|
|
|
*
|
2006-09-21 13:04:43 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
int gost_sign_keygen(DSA *dsa)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
dsa->priv_key = BN_new();
|
|
|
|
BN_rand_range(dsa->priv_key,dsa->q);
|
|
|
|
return gost94_compute_public( dsa);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Unpack signature according to cryptocom rules */
|
|
|
|
|
2006-09-21 13:04:43 +00:00
|
|
|
DSA_SIG *unpack_cc_signature(const unsigned char *sig,size_t siglen)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
DSA_SIG *s;
|
|
|
|
s = DSA_SIG_new();
|
2006-09-21 13:04:43 +00:00
|
|
|
if (s == NULL)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
GOSTerr(GOST_F_UNPACK_CC_SIGNATURE,GOST_R_NO_MEMORY);
|
|
|
|
return(NULL);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
s->r = getbnfrombuf(sig, siglen/2);
|
|
|
|
s->s = getbnfrombuf(sig + siglen/2, siglen/2);
|
|
|
|
return s;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Unpack signature according to cryptopro rules */
|
2006-09-21 13:04:43 +00:00
|
|
|
DSA_SIG *unpack_cp_signature(const unsigned char *sig,size_t siglen)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
DSA_SIG *s;
|
|
|
|
|
|
|
|
s = DSA_SIG_new();
|
2006-09-21 13:04:43 +00:00
|
|
|
if (s == NULL)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
GOSTerr(GOST_F_UNPACK_CP_SIGNATURE,GOST_R_NO_MEMORY);
|
|
|
|
return NULL;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
s->s = getbnfrombuf(sig , siglen/2);
|
|
|
|
s->r = getbnfrombuf(sig + siglen/2, siglen/2);
|
|
|
|
return s;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Convert little-endian byte array into bignum */
|
2006-09-21 13:04:43 +00:00
|
|
|
BIGNUM *hashsum2bn(const unsigned char *dgst)
|
|
|
|
{
|
|
|
|
unsigned char buf[32];
|
|
|
|
int i;
|
|
|
|
for (i=0;i<32;i++)
|
|
|
|
{
|
|
|
|
buf[31-i]=dgst[i];
|
|
|
|
}
|
|
|
|
return getbnfrombuf(buf,32);
|
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
|
|
|
|
/* Convert byte buffer to bignum, skipping leading zeros*/
|
2006-09-21 13:04:43 +00:00
|
|
|
BIGNUM *getbnfrombuf(const unsigned char *buf,size_t len)
|
|
|
|
{
|
|
|
|
while (*buf==0&&len>0)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
buf++; len--;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
if (len)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
return BN_bin2bn(buf,len,NULL);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
BIGNUM *b=BN_new();
|
|
|
|
BN_zero(b);
|
|
|
|
return b;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
}
|
2006-09-21 13:04:43 +00:00
|
|
|
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Pack bignum into byte buffer of given size, filling all leading bytes
|
|
|
|
* by zeros */
|
2006-09-21 13:04:43 +00:00
|
|
|
int store_bignum(BIGNUM *bn, unsigned char *buf,int len)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
int bytes = BN_num_bytes(bn);
|
|
|
|
if (bytes>len) return 0;
|
|
|
|
memset(buf,0,len);
|
|
|
|
BN_bn2bin(bn,buf+len-bytes);
|
|
|
|
return 1;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|