blob: 233a093249fbb68d3d256cdbb6cf6443bb0285fa [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file openssl.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief OpenSSL wrapper (definitions, inline functions).
5 *
Manuel Pégourié-Gonnarde6581762015-03-20 17:21:21 +00006 * \deprecated Use native mbed TLS functions instead
7 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00008 * Copyright (C) 2006-2010, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00009 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000010 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +000011 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 * 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 Bakker5121ce52009-01-03 21:22:43 +000025 */
Manuel Pégourié-Gonnard8c3f0f42015-04-09 14:10:26 +020026
Paul Bakker5121ce52009-01-03 21:22:43 +000027/*
28 * OpenSSL wrapper contributed by David Barett
29 */
Manuel Pégourié-Gonnard8c3f0f42015-04-09 14:10:26 +020030
Manuel Pégourié-Gonnarda1e32412015-04-15 11:03:43 +020031#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard8c3f0f42015-04-09 14:10:26 +020032
Manuel Pégourié-Gonnarda1e32412015-04-15 11:03:43 +020033#if defined(POLARSSL_DEPRECATED_WARNING)
Manuel Pégourié-Gonnard8c3f0f42015-04-09 14:10:26 +020034#warning "Including openssl.h is deprecated"
35#endif
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#ifndef POLARSSL_OPENSSL_H
38#define POLARSSL_OPENSSL_H
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker314052f2011-08-15 09:07:52 +000040#include "aes.h"
41#include "md5.h"
42#include "rsa.h"
43#include "sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000044
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 Bakker30b95fa2013-10-01 10:09:06 +020072#ifdef __cplusplus
73extern "C" {
74#endif
75
Paul Bakker5121ce52009-01-03 21:22:43 +000076/*
77 * RSA stuff follows. TODO: needs cleanup
78 */
79inline int __RSA_Passthrough( void *output, void *input, int size )
80{
81 memcpy( output, input, size );
82 return size;
83}
84
85inline 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 Bakker30b95fa2013-10-01 10:09:06 +020090
Paul Bakker5121ce52009-01-03 21:22:43 +000091 /*
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 Bakker9af723c2014-05-01 13:03:14 +0200108 if( ( len == 94 &&
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 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
138inline 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; }
139inline 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; }
140inline 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; }
141inline 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é-Gonnarda1e32412015-04-15 11:03:43 +0200148#endif /* POLARSSL_DEPRECATED_REMOVED */