Problem with gcry_sexp_build
cplusplushelp@gmx.net
cplusplushelp@gmx.net
Thu, 1 Aug 2002 22:20:15 +0200 (MEST)
hi,
I'm using the following functions to encrypt a session key but I recive a
segmantation fault!
typedef struct
{
MPI *prikey; /*private key*/
MPI *pubkey; /*public key*/
} pkkey;
int wcrypt_rsagenkey(pkkey *key,int bits)
{
int ret;
GCRY_SEXP parms;
GCRY_SEXP key_;
gcry_sexp_build(&parms, NULL, "(genkey(rsa(nbits %d)))", bits); /*build
key*/
ret=gcry_pk_genkey(&key_,parms);
if( (key->pubkey=malloc(sizeof(MPI))) ==NULL ||
(key->prikey=malloc(sizeof(MPI))) ) /*?? is here the problem?*/
return -1;
/*extract public and private key from GCRY_SEXP structure*/
wcrypt_key_from_sexp(key->pubkey,key_,"public-key","ne"); /*the
key_from_sexp function from gnupg*/
wcrypt_key_from_sexp(key->prikey,key_,"private-key","ne");
gcry_sexp_release(parms);
gcry_sexp_dump(key_);
gcry_sexp_release(key_);
return ret;
}
int wcrypt_pkencrypt(enum pkalgo alg,unsigned char*sdata,size_t
ndata,unsigned char*to,size_t nto,pkkey key)
{
GCRY_SEXP list,s_pkey,s_data,s_ciph;
GCRY_MPI* pkey=key.pubkey;
GCRY_MPI data,resarr[2];
gcry_mpi_scan(&data,GCRYMPI_FMT_USG,sdata,&ndata);
switch(alg)
{
case GCRY_PK_ELG:
case GCRY_PK_ELG_E:
if(gcry_sexp_build ( &s_pkey, NULL,
"(public-key(elg(p%m)(g%m)(y%m)))",
pkey[0], pkey[1], pkey[2] ))
return 1;
break;
case GCRY_PK_RSA:
if(gcry_sexp_build( &s_pkey, NULL,
"(public-key(rsa(n%m)(e%m)))",
pkey[0] /* n */, pkey[1] /* e */ )) /*here the SEGFAULT happens!*/
return 1;
break;
default:
return 1;
}
/* put the data into a simple list */
if ( gcry_sexp_build( &s_data, NULL, "%m", data ) )
return 1;
/* pass it to libgcrypt */
gcry_pk_encrypt( &s_ciph, s_data, s_pkey );
gcry_sexp_release( s_data );
gcry_sexp_release( s_pkey );
/* extract the MPI values */
list = gcry_sexp_find_token( s_ciph, "a" , 0 );
if(!list)
return 1;
resarr[0] = gcry_sexp_nth_mpi( list, 1, 0 );
if(!resarr[0])
return 1;
gcry_sexp_release ( list );
list = gcry_sexp_find_token( s_ciph, "b" , 0 );
if(!list)
return 1;
resarr[1] = gcry_sexp_nth_mpi( list, 1, 0 );
if(!resarr[1])
return 1;
gcry_sexp_release ( list );
gcry_mpi_print(GCRYMPI_FMT_USG,to,&nto,resarr[0]);
return 0;
}
What is wrong with this code?
--
GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net