Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file openssl.h |
| 3 | */ |
| 4 | /* |
| 5 | * OpenSSL wrapper contributed by David Barett |
| 6 | */ |
| 7 | #ifndef XYSSL_OPENSSL_H |
| 8 | #define XYSSL_OPENSSL_H |
| 9 | |
Paul Bakker | 8e831ed | 2009-01-03 21:24:11 +0000 | [diff] [blame^] | 10 | #include "polarssl/aes.h" |
| 11 | #include "polarssl/md5.h" |
| 12 | #include "polarssl/rsa.h" |
| 13 | #include "polarssl/sha1.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 14 | |
| 15 | #define AES_SIZE 16 |
| 16 | #define AES_BLOCK_SIZE 16 |
| 17 | #define AES_KEY aes_context |
| 18 | #define MD5_CTX md5_context |
| 19 | #define SHA_CTX sha1_context |
| 20 | |
| 21 | #define SHA1_Init( CTX ) \ |
| 22 | sha1_starts( (CTX) ) |
| 23 | #define SHA1_Update( CTX, BUF, LEN ) \ |
| 24 | sha1_update( (CTX), (unsigned char *)(BUF), (LEN) ) |
| 25 | #define SHA1_Final( OUT, CTX ) \ |
| 26 | sha1_finish( (CTX), (OUT) ) |
| 27 | |
| 28 | #define MD5_Init( CTX ) \ |
| 29 | md5_starts( (CTX) ) |
| 30 | #define MD5_Update( CTX, BUF, LEN ) \ |
| 31 | md5_update( (CTX), (unsigned char *)(BUF), (LEN) ) |
| 32 | #define MD5_Final( OUT, CTX ) \ |
| 33 | md5_finish( (CTX), (OUT) ) |
| 34 | |
| 35 | #define AES_set_encrypt_key( KEY, KEYSIZE, CTX ) \ |
| 36 | aes_setkey_enc( (CTX), (KEY), (KEYSIZE) ) |
| 37 | #define AES_set_decrypt_key( KEY, KEYSIZE, CTX ) \ |
| 38 | aes_setkey_dec( (CTX), (KEY), (KEYSIZE) ) |
| 39 | #define AES_cbc_encrypt( INPUT, OUTPUT, LEN, CTX, IV, MODE ) \ |
| 40 | aes_crypt_cbc( (CTX), (MODE), (LEN), (IV), (INPUT), (OUTPUT) ) |
| 41 | |
| 42 | /* |
| 43 | * RSA stuff follows. TODO: needs cleanup |
| 44 | */ |
| 45 | inline int __RSA_Passthrough( void *output, void *input, int size ) |
| 46 | { |
| 47 | memcpy( output, input, size ); |
| 48 | return size; |
| 49 | } |
| 50 | |
| 51 | inline rsa_context* d2i_RSA_PUBKEY( void *ignore, unsigned char **bufptr, |
| 52 | int len ) |
| 53 | { |
| 54 | unsigned char *buffer = *(unsigned char **) bufptr; |
| 55 | rsa_context *rsa; |
| 56 | |
| 57 | /* |
| 58 | * Not a general-purpose parser: only parses public key from *exactly* |
| 59 | * openssl genrsa -out privkey.pem 512 (or 1024) |
| 60 | * openssl rsa -in privkey.pem -out privatekey.der -outform der |
| 61 | * openssl rsa -in privkey.pem -out pubkey.der -outform der -pubout |
| 62 | * |
| 63 | * TODO: make a general-purpose parse |
| 64 | */ |
| 65 | if( ignore != 0 || ( len != 94 && len != 162 ) ) |
| 66 | return( 0 ); |
| 67 | |
| 68 | rsa = (rsa_context *) malloc( sizeof( rsa_rsa ) ); |
| 69 | if( rsa == NULL ) |
| 70 | return( 0 ); |
| 71 | |
| 72 | memset( rsa, 0, sizeof( rsa_context ) ); |
| 73 | |
| 74 | if( ( len == 94 && |
| 75 | mpi_read_binary( &rsa->N, &buffer[ 25], 64 ) == 0 && |
| 76 | mpi_read_binary( &rsa->E, &buffer[ 91], 3 ) == 0 ) || |
| 77 | ( len == 162 && |
| 78 | mpi_read_binary( &rsa->N, &buffer[ 29], 128 ) == 0 ) && |
| 79 | mpi_read_binary( &rsa->E, &buffer[159], 3 ) == 0 ) |
| 80 | { |
| 81 | /* |
| 82 | * key read successfully |
| 83 | */ |
| 84 | rsa->len = ( mpi_msb( &rsa->N ) + 7 ) >> 3; |
| 85 | return( rsa ); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | memset( rsa, 0, sizeof( rsa_context ) ); |
| 90 | free( rsa ); |
| 91 | return( 0 ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | #define RSA rsa_context |
| 96 | #define RSA_PKCS1_PADDING 1 /* ignored; always encrypt with this */ |
| 97 | #define RSA_size( CTX ) (CTX)->len |
| 98 | #define RSA_free( CTX ) rsa_free( CTX ) |
| 99 | #define ERR_get_error( ) "ERR_get_error() not supported" |
| 100 | #define RSA_blinding_off( IGNORE ) |
| 101 | |
| 102 | #define d2i_RSAPrivateKey( a, b, c ) new rsa_context /* TODO: C++ bleh */ |
| 103 | |
| 104 | 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; } |
| 105 | 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; } |
| 106 | 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; } |
| 107 | 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; } |
| 108 | |
| 109 | #ifdef __cplusplus |
| 110 | } |
| 111 | #endif |
| 112 | |
| 113 | #endif /* openssl.h */ |