00001
00027
00028
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 #ifdef __cplusplus
00066 extern "C" {
00067 #endif
00068
00069
00070
00071
00072 inline int __RSA_Passthrough( void *output, void *input, int size )
00073 {
00074 memcpy( output, input, size );
00075 return size;
00076 }
00077
00078 inline rsa_context* d2i_RSA_PUBKEY( void *ignore, unsigned char **bufptr,
00079 int len )
00080 {
00081 unsigned char *buffer = *(unsigned char **) bufptr;
00082 rsa_context *rsa;
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 if( ignore != 0 || ( len != 94 && len != 162 ) )
00093 return( 0 );
00094
00095 rsa = (rsa_context *) malloc( sizeof( rsa_rsa ) );
00096 if( rsa == NULL )
00097 return( 0 );
00098
00099 memset( rsa, 0, sizeof( rsa_context ) );
00100
00101 if( ( len == 94 &&
00102 mpi_read_binary( &rsa->N, &buffer[ 25], 64 ) == 0 &&
00103 mpi_read_binary( &rsa->E, &buffer[ 91], 3 ) == 0 ) ||
00104 ( len == 162 &&
00105 mpi_read_binary( &rsa->N, &buffer[ 29], 128 ) == 0 ) &&
00106 mpi_read_binary( &rsa->E, &buffer[159], 3 ) == 0 )
00107 {
00108
00109
00110
00111 rsa->len = ( mpi_msb( &rsa->N ) + 7 ) >> 3;
00112 return( rsa );
00113 }
00114 else
00115 {
00116 memset( rsa, 0, sizeof( rsa_context ) );
00117 free( rsa );
00118 return( 0 );
00119 }
00120 }
00121
00122 #define RSA rsa_context
00123 #define RSA_PKCS1_PADDING 1
00124 #define RSA_size( CTX ) (CTX)->len
00125 #define RSA_free( CTX ) rsa_free( CTX )
00126 #define ERR_get_error( ) "ERR_get_error() not supported"
00127 #define RSA_blinding_off( IGNORE )
00128
00129 #define d2i_RSAPrivateKey( a, b, c ) new rsa_context
00130
00131 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; }
00132 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; }
00133 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; }
00134 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; }
00135
00136 #ifdef __cplusplus
00137 }
00138 #endif
00139
00140 #endif