Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file openssl.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org> |
| 5 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 6 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 7 | * Joined copyright on original XySSL code with: Christophe Devine |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 22 | */ |
| 23 | /* |
| 24 | * OpenSSL wrapper contributed by David Barett |
| 25 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 26 | #ifndef POLARSSL_OPENSSL_H |
| 27 | #define POLARSSL_OPENSSL_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Paul Bakker | 8e831ed | 2009-01-03 21:24:11 +0000 | [diff] [blame] | 29 | #include "polarssl/aes.h" |
| 30 | #include "polarssl/md5.h" |
| 31 | #include "polarssl/rsa.h" |
| 32 | #include "polarssl/sha1.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 33 | |
| 34 | #define AES_SIZE 16 |
| 35 | #define AES_BLOCK_SIZE 16 |
| 36 | #define AES_KEY aes_context |
| 37 | #define MD5_CTX md5_context |
| 38 | #define SHA_CTX sha1_context |
| 39 | |
| 40 | #define SHA1_Init( CTX ) \ |
| 41 | sha1_starts( (CTX) ) |
| 42 | #define SHA1_Update( CTX, BUF, LEN ) \ |
| 43 | sha1_update( (CTX), (unsigned char *)(BUF), (LEN) ) |
| 44 | #define SHA1_Final( OUT, CTX ) \ |
| 45 | sha1_finish( (CTX), (OUT) ) |
| 46 | |
| 47 | #define MD5_Init( CTX ) \ |
| 48 | md5_starts( (CTX) ) |
| 49 | #define MD5_Update( CTX, BUF, LEN ) \ |
| 50 | md5_update( (CTX), (unsigned char *)(BUF), (LEN) ) |
| 51 | #define MD5_Final( OUT, CTX ) \ |
| 52 | md5_finish( (CTX), (OUT) ) |
| 53 | |
| 54 | #define AES_set_encrypt_key( KEY, KEYSIZE, CTX ) \ |
| 55 | aes_setkey_enc( (CTX), (KEY), (KEYSIZE) ) |
| 56 | #define AES_set_decrypt_key( KEY, KEYSIZE, CTX ) \ |
| 57 | aes_setkey_dec( (CTX), (KEY), (KEYSIZE) ) |
| 58 | #define AES_cbc_encrypt( INPUT, OUTPUT, LEN, CTX, IV, MODE ) \ |
| 59 | aes_crypt_cbc( (CTX), (MODE), (LEN), (IV), (INPUT), (OUTPUT) ) |
| 60 | |
| 61 | /* |
| 62 | * RSA stuff follows. TODO: needs cleanup |
| 63 | */ |
| 64 | inline int __RSA_Passthrough( void *output, void *input, int size ) |
| 65 | { |
| 66 | memcpy( output, input, size ); |
| 67 | return size; |
| 68 | } |
| 69 | |
| 70 | inline rsa_context* d2i_RSA_PUBKEY( void *ignore, unsigned char **bufptr, |
| 71 | int len ) |
| 72 | { |
| 73 | unsigned char *buffer = *(unsigned char **) bufptr; |
| 74 | rsa_context *rsa; |
| 75 | |
| 76 | /* |
| 77 | * Not a general-purpose parser: only parses public key from *exactly* |
| 78 | * openssl genrsa -out privkey.pem 512 (or 1024) |
| 79 | * openssl rsa -in privkey.pem -out privatekey.der -outform der |
| 80 | * openssl rsa -in privkey.pem -out pubkey.der -outform der -pubout |
| 81 | * |
| 82 | * TODO: make a general-purpose parse |
| 83 | */ |
| 84 | if( ignore != 0 || ( len != 94 && len != 162 ) ) |
| 85 | return( 0 ); |
| 86 | |
| 87 | rsa = (rsa_context *) malloc( sizeof( rsa_rsa ) ); |
| 88 | if( rsa == NULL ) |
| 89 | return( 0 ); |
| 90 | |
| 91 | memset( rsa, 0, sizeof( rsa_context ) ); |
| 92 | |
| 93 | if( ( len == 94 && |
| 94 | mpi_read_binary( &rsa->N, &buffer[ 25], 64 ) == 0 && |
| 95 | mpi_read_binary( &rsa->E, &buffer[ 91], 3 ) == 0 ) || |
| 96 | ( len == 162 && |
| 97 | mpi_read_binary( &rsa->N, &buffer[ 29], 128 ) == 0 ) && |
| 98 | mpi_read_binary( &rsa->E, &buffer[159], 3 ) == 0 ) |
| 99 | { |
| 100 | /* |
| 101 | * key read successfully |
| 102 | */ |
| 103 | rsa->len = ( mpi_msb( &rsa->N ) + 7 ) >> 3; |
| 104 | return( rsa ); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | memset( rsa, 0, sizeof( rsa_context ) ); |
| 109 | free( rsa ); |
| 110 | return( 0 ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #define RSA rsa_context |
| 115 | #define RSA_PKCS1_PADDING 1 /* ignored; always encrypt with this */ |
| 116 | #define RSA_size( CTX ) (CTX)->len |
| 117 | #define RSA_free( CTX ) rsa_free( CTX ) |
| 118 | #define ERR_get_error( ) "ERR_get_error() not supported" |
| 119 | #define RSA_blinding_off( IGNORE ) |
| 120 | |
| 121 | #define d2i_RSAPrivateKey( a, b, c ) new rsa_context /* TODO: C++ bleh */ |
| 122 | |
| 123 | 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; } |
| 124 | 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; } |
| 125 | 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; } |
| 126 | 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; } |
| 127 | |
| 128 | #ifdef __cplusplus |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | #endif /* openssl.h */ |