- Main Page
- Related Pages
- Data Structures
- Files

# openssl.h

Go to the documentation of this file.

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