gcry_cipher_decrypt not functioning

D. Hazelton dhazelton at enter.net
Wed Apr 5 04:04:56 CEST 2006

I'm working on a small project that is using libgcrypt for encryption. The 
encryption works fine, but when the decryption code is called, no decryption 
takes place.

The following function is what I use to wrap the library calls. The input data 
is already aligned to the ciphers blocksize by the encryption code.

From watching the process in gdb it is certain that the decryption never 
happens. I have tried a version where I was creating a temporary buffer for 
the output, and even with that step the input stream matched the output 
stream from the function.

I'm not on the list, so could you please CC: me on the response?

gcry_error_t decrypt( unsigned char *outb, unsigned int *outs, 
		      unsigned int algo ) {
  gcry_error_t rval;
  gcry_cipher_hd_t hand;
  gcry_md_hd_t mdh;
  unsigned char *store;
  unsigned int i, k;

  store = (char *)malloc(32);
  blklen = gcry_cipher_get_algo_blklen(algo);
  rval = gcry_md_open( &mdh, GCRY_MD_SHA256, 0 );
  if( rval )
    return rval;

  k = strlen( password );
  for( i = 0; i < k; i++ ) {
    gcry_md_putc( mdh, password[i] );
  gcry_md_final( mdh );
  store = gcry_md_read( mdh, GCRY_MD_SHA256 );
  gcry_md_close( mdh );

  rval = gcry_cipher_open( &hand, algo, 0, 0 );
  if( rval ) 
    return rval;

  rval = gcry_cipher_setkey( hand, store, 32 );
  if( rval )
    return rval;

  rval = gcry_cipher_decrypt( hand, outb, outs[0], NULL, 0 );
  if( rval )
    return rval;

  gcry_cipher_close( hand );
  return 0;


More information about the Gcrypt-devel mailing list