[Botan-devel] Trying to pass a RSA key from memory to RSA Private

Pete Toich torcher72 at yahoo.com
Fri Jun 11 16:21:43 EDT 2010


Hi Jack,

Thanks for you quick reply.  

At first glance it seemed like the fix you suggested worked.  I was able to compile and run the program.  When I tried to verify the result of my test however, the result was failing.  I isolated the section in question into the sample program which I attached a tarball of.  

Basically the test does:
1. opens a public key file using the X509 part of the API
2. reads the private key into memory
3. loads the private key via the memory source
4.  decrypts the data
5.  compares

In case the attachment does come through, I also included a  code snippet below

Thanks for any help,
Pete

======================= code snippet ===================================


    SecureVector<byte> encrypted_data;
    SecureVector<byte> decrypted_data;

    try 
    {
        Botan::AutoSeeded_RNG rng;

        std::auto_ptr<X509_PublicKey> key(X509::load_key(gPublicKeyFile));
        printf("%s, %i: Loaded public rsa key file\n", __FILE__, __LINE__ );

        RSA_PublicKey* rsakey = dynamic_cast<RSA_PublicKey*>(key.get());
        if(!rsakey)
        {
            std::cout << "The loaded key is not a RSA key!\n";
            exit(1);
        }
        printf("%s, %i: Key file is valid\n", __FILE__, __LINE__ );

        encrypted_data = rsakey->encrypt( 
                (Botan::byte *)&test_data, 
                sizeof(test_data),
                rng );
        
        printf("%s, %i: Original data:\n", __FILE__, __LINE__ );
        display_hex_data( test_data, sizeof(test_data) ); 

        printf("%s, %i: Encrypted data:\n", __FILE__, __LINE__ );
        display_hex_data( encrypted_data, sizeof(test_data) ); 
    }
    catch(std::exception& e)
    {
        std::cout << "Exception: " << e.what() << std::endl;
        exit(1);
    }


    const int max_file_size = 4096;

    // open the file
    int fd_priv = open ( gPrivateKeyFile, O_RDONLY, "r" );
    if ( fd_priv < 0 ) {
        printf("%s, %i: Could not open private key file %s, error: %s\n", 
                __FILE__, __LINE__, gPrivateKeyFile, strerror(errno));
        exit(1);
    }
    
    unsigned char * priv_exchange_key_p = (unsigned char *) malloc(max_file_size);
    int priv_exchange_key_len = read ( fd_priv, priv_exchange_key_p, max_file_size);
    if ( priv_exchange_key_len == max_file_size ) {
        printf("%s, %i: File read may have been truncated\n",
                __FILE__, __LINE__ );
        exit(1);
    }
        
    printf("%s, %i: Read in %d bytes from %s file\n", __FILE__, __LINE__, priv_exchange_key_len, gPrivateKeyFile );

    try 
    {
        Botan::AutoSeeded_RNG rng;

        DataSource_Memory key_data( priv_exchange_key_p, priv_exchange_key_len );

        std::auto_ptr<PKCS8_PrivateKey> key(PKCS8::load_key(key_data, rng ));
        printf("%s, %i: Loaded rsa private key file\n", __FILE__, __LINE__ );

        RSA_PrivateKey* rsakey = dynamic_cast<RSA_PrivateKey*>(key.get());
        if(!rsakey)
        {
            std::cout << "The loaded key is not a RSA key!\n";
            exit(1);
        }
        printf("%s, %i: Key file is valid\n", __FILE__, __LINE__ );

        decrypted_data = rsakey->decrypt( 
                encrypted_data,
                sizeof(test_data) );
        
        printf("%s, %i: Decrypted data:\n", __FILE__, __LINE__ );
        display_hex_data( decrypted_data, sizeof(test_data) );

    }
    catch(std::exception& e)
    {
        std::cout << "Exception: " << e.what() << std::endl;
        exit(1);
    }


    // verify data
    for ( unsigned int i = 0; i < sizeof(test_data); i ++ ) {
        if ( decrypted_data[i] != test_data[i] ) { 
            printf("%s, %i: Data miscompare at location %d, exp: 0x%02x, act: 0x%02x\n",
                    __FILE__, __LINE__, i, test_data[i], decrypted_data[i] );
            printf("%s, %i: Test failed\n", __FILE__, __LINE__ );
            exit(1);
        }
    }
            
    printf("%s, %i: Test passed\n", __FILE__, __LINE__ );

=============== end snippet ===================================
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_rsa.tgz
Type: application/x-compressed
Size: 37983 bytes
Desc: not available
URL: <http://lists.randombit.net/pipermail/botan-devel/attachments/20100611/194ba7d5/attachment.bin>


More information about the botan-devel mailing list