Cryptography Reference
In-Depth Information
privkey->modulus = malloc( sizeof( huge ) );
privkey->exponent = malloc( sizeof( huge ) );
load_huge( privkey->modulus, modulus->data, modulus->length );
load_huge( privkey->exponent, private_exponent->data, private_exponent->length );
asn1free( &private_key );
return 0;
}
This is a regular ASN.1 parsing routine of the kind examined in Chapter 5.
It takes as input a DER-encoded buffer that it parses and uses to fi ll out the
privkey argument, a pointer to an rsa_key structure. Remember that an RSA
private key is structurally no different than an RSA public key, so the same
structure is used to represent both here. Notice that the input is DER-encoded;
the caller must ensure either that the fi le is loaded from the disk that way or
that it's passed through the pem_decode routine from Listing 5-7 before being
passed to parse_private_key .
The private key structure, as indicated by the comments to this function,
contains quite a bit more information than just the modulus and the private
exponent; these numbers in theory could be used to support a more optimized
rsa_decrypt routine than the one presented in Chapter 3.
If you want to see this in action, you can put together a test main routine as
shown in Listing 7-15.
Listing 7-15: “privkey.c” test main routine
#ifdef TEST_PRIVKEY
int main( int argc, char *argv[ ] )
{
rsa_key privkey;
unsigned char *buffer;
int buffer_length;
if ( argc < 3 )
{
fprintf( stderr, “Usage: %s [-pem|-der] <rsa private key file>\n”, argv[ 0 ] );
exit( 0 );
}
if ( !( buffer = load_file_into_memory( argv[ 2 ], &buffer_length ) ) )
{
perror( “Unable to load file” );
exit( 1 );
}
if ( !strcmp( argv[ 1 ], “-pem” ) )
{
// XXX this overallocates a bit, since it sets aside space for markers, etc.
(Continued)
 
Search WWH ::




Custom Search