Fatal: memory at 0x804a3dc corrupted (underflow=40)

Ruediger Sonderfeld cplusplushelp@gmx.net
Tue, 22 Oct 2002 17:58:04 +0200


hi,
I'm trying to encrypt some data with RSA using the libgcrypt CVS snapshot.

I'm using Code I extracted and modified from gnupg-1.1.2 to encrypt the data

 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 */ ))
      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 */        
  if(! (list=gcry_sexp_find_token(s_ciph,"a",0)))
    return -1;
  if(! (resarr[0]=gcry_sexp_nth_mpi(list,1,0)))
    return -1;
  gcry_sexp_release ( list );

  if(alg==GCRY_PK_ELG||alg==GCRY_PK_ELG_E)
  {
    if(! (list=gcry_sexp_find_token(s_ciph,"b",0)))
      return -1;
    if(! (resarr[1]=gcry_sexp_nth_mpi(list,1,0)))
      return -1;
  }
  gcry_sexp_release ( list );
  gcry_mpi_print(GCRYMPI_FMT_USG,to,&nto,resarr[0]);
  return 0;

In the gcry_sexp_release ( list ); function the program exits always with the 
error code 02 and this message is printed to stderr

Fatal: memory at 0x804a3dc corrupted (underflow=40)

what is wrong?