[botan-devel] seg fault using rsa classes

Stuart Maclean stuart at apl.washington.edu
Tue Dec 3 15:39:58 EST 2013


Apologies for including code here, but I have a seg fault issue.  
valgrind suggests some form of 'unpad' error.  This is on Linux (64bit), 
using Botan 1.10.6.  The new 1.10.6 api for doing rsa encryption and 
decryption uses the notion of PK_Encryptor I think, which is new to me.  
Are there any docs describing their usage, other than doxygen??

The seg fault below is during PK_Decryptor.decrypt().

One other point while I am on the topic.  I have always been confused as 
to why there is no method on RSA_PrivateKey for deriving the 
corresponding public key.  I have to access n and e from the private key 
and pass those to the public key constructor.  That implies I could get 
that wrong, say pass e and n to RSA_PublicKey, instead of n and e, or 
use arbitrary values for them. My pub key then wouldn't work with the 
priv key.  Surely the two are a key 'pair' with tight coupling??


Anyways, here's the offending code.  I expect I have left out a very 
obvious call somewhere...


int main( int argc, char* argv[] ) {

   LibraryInitializer init;

   AutoSeeded_RNG rng;

   int bits[] = { 512, 768, 1024, 2048, 3072, 4096 };
   int bitCounts = sizeof( bits ) / sizeof( int );

   char* eme = "EME1(SHA-256)";

   for( int i = 0; i < bitCounts; i++ ) {
     printf( "%u\n", bits[i] );

     time_t t1 = time(NULL);
     RSA_PrivateKey priv( rng, bits[i] );
     time_t t2 = time(NULL);
     printf( "KPGen(%u): %u\n", priv.get_e().to_u32bit(), t2-t1 );

     RSA_PublicKey pub( priv.get_n(), priv.get_e() );

     byte m[32];
     //    rng.randomize( m, sizeof( m ) );

     time_t t3 = time(NULL);
     PK_Encryptor_EME pke( pub, eme );
     SecureVector<byte> c = pke.encrypt( m, 32, rng );
     time_t t4 = time(NULL);
     printf( "Encrypt: %u\n", t4-t3 );

     time_t t5 = time(NULL);
     PK_Decryptor_EME pkd( priv, eme );
     printf( "PKD\n" );
     SecureVector<byte> msv = pkd.decrypt( c );//, 32 );
     time_t t6 = time(NULL);
     printf( "Decrypt: %u\n", t6-t5 );
   }
}

Stuart


More information about the botan-devel mailing list