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