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