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