PolarSSL v1.1.4
openssl.h
Go to the documentation of this file.
00001 
00027 /*
00028  * OpenSSL wrapper contributed by David Barett
00029  */
00030 #ifndef POLARSSL_OPENSSL_H
00031 #define POLARSSL_OPENSSL_H
00032 
00033 #include "aes.h"
00034 #include "md5.h"
00035 #include "rsa.h"
00036 #include "sha1.h"
00037 
00038 #define AES_SIZE                16
00039 #define AES_BLOCK_SIZE          16
00040 #define AES_KEY                 aes_context
00041 #define MD5_CTX                 md5_context
00042 #define SHA_CTX                 sha1_context
00043 
00044 #define SHA1_Init( CTX ) \
00045         sha1_starts( (CTX) )
00046 #define SHA1_Update(  CTX, BUF, LEN ) \
00047         sha1_update( (CTX), (unsigned char *)(BUF), (LEN) )
00048 #define SHA1_Final( OUT, CTX ) \
00049         sha1_finish( (CTX), (OUT) )
00050 
00051 #define MD5_Init( CTX ) \
00052         md5_starts( (CTX) )
00053 #define MD5_Update( CTX, BUF, LEN ) \
00054         md5_update( (CTX), (unsigned char *)(BUF), (LEN) )
00055 #define MD5_Final( OUT, CTX ) \
00056         md5_finish( (CTX), (OUT) )
00057 
00058 #define AES_set_encrypt_key( KEY, KEYSIZE, CTX ) \
00059         aes_setkey_enc( (CTX), (KEY), (KEYSIZE) )
00060 #define AES_set_decrypt_key( KEY, KEYSIZE, CTX ) \
00061         aes_setkey_dec( (CTX), (KEY), (KEYSIZE) )
00062 #define AES_cbc_encrypt( INPUT, OUTPUT, LEN, CTX, IV, MODE ) \
00063         aes_crypt_cbc( (CTX), (MODE), (LEN), (IV), (INPUT), (OUTPUT) )
00064 
00065 /*
00066  * RSA stuff follows. TODO: needs cleanup
00067  */
00068 inline int __RSA_Passthrough( void *output, void *input, int size )
00069 {
00070     memcpy( output, input, size );
00071     return size;
00072 }
00073 
00074 inline rsa_context* d2i_RSA_PUBKEY( void *ignore, unsigned char **bufptr,
00075                                     int len )
00076 {
00077     unsigned char *buffer = *(unsigned char **) bufptr;
00078     rsa_context *rsa;
00079     
00080     /*
00081      * Not a general-purpose parser: only parses public key from *exactly*
00082      *   openssl genrsa -out privkey.pem 512 (or 1024)
00083      *   openssl rsa -in privkey.pem -out privatekey.der -outform der
00084      *   openssl rsa -in privkey.pem -out pubkey.der -outform der -pubout
00085      *
00086      * TODO: make a general-purpose parse
00087      */
00088     if( ignore != 0 || ( len != 94 && len != 162 ) )
00089         return( 0 );
00090 
00091     rsa = (rsa_context *) malloc( sizeof( rsa_rsa ) );
00092     if( rsa == NULL )
00093         return( 0 );
00094 
00095     memset( rsa, 0, sizeof( rsa_context ) );
00096 
00097     if( ( len ==  94 && 
00098           mpi_read_binary( &rsa->N, &buffer[ 25],  64 ) == 0 &&
00099           mpi_read_binary( &rsa->E, &buffer[ 91],   3 ) == 0 ) ||
00100         ( len == 162 &&
00101           mpi_read_binary( &rsa->N, &buffer[ 29], 128 ) == 0 ) &&
00102           mpi_read_binary( &rsa->E, &buffer[159],   3 ) == 0 )
00103     {
00104         /*
00105          * key read successfully
00106          */
00107         rsa->len = ( mpi_msb( &rsa->N ) + 7 ) >> 3;
00108         return( rsa );
00109     }
00110     else
00111     {
00112         memset( rsa, 0, sizeof( rsa_context ) );
00113         free( rsa );
00114         return( 0 );
00115     }
00116 }
00117 
00118 #define RSA                     rsa_context
00119 #define RSA_PKCS1_PADDING       1 /* ignored; always encrypt with this */
00120 #define RSA_size( CTX )         (CTX)->len
00121 #define RSA_free( CTX )         rsa_free( CTX )
00122 #define ERR_get_error( )        "ERR_get_error() not supported"
00123 #define RSA_blinding_off( IGNORE )
00124 
00125 #define d2i_RSAPrivateKey( a, b, c ) new rsa_context /* TODO: C++ bleh */
00126 
00127 inline int RSA_public_decrypt ( int size, unsigned char* input, unsigned char* output, RSA* key, int ignore ) { int outsize=size; if( !rsa_pkcs1_decrypt( key, RSA_PUBLIC,  &outsize, input, output ) ) return outsize; else return -1; }
00128 inline int RSA_private_decrypt( int size, unsigned char* input, unsigned char* output, RSA* key, int ignore ) { int outsize=size; if( !rsa_pkcs1_decrypt( key, RSA_PRIVATE, &outsize, input, output ) ) return outsize; else return -1; }
00129 inline int RSA_public_encrypt ( int size, unsigned char* input, unsigned char* output, RSA* key, int ignore ) { if( !rsa_pkcs1_encrypt( key, RSA_PUBLIC,  size, input, output ) ) return RSA_size(key); else return -1; }
00130 inline int RSA_private_encrypt( int size, unsigned char* input, unsigned char* output, RSA* key, int ignore ) { if( !rsa_pkcs1_encrypt( key, RSA_PRIVATE, size, input, output ) ) return RSA_size(key); else return -1; }
00131 
00132 #ifdef __cplusplus
00133 }
00134 #endif
00135 
00136 #endif /* openssl.h */