blob: 0eb9ab5279d472508c3c3ca135fa2edc3f0ee521 [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 *
Paul Bakker530927b2015-02-13 14:24:10 +01006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * 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 Bakker5121ce52009-01-03 21:22:43 +000023 */
24/*
25 * OpenSSL wrapper contributed by David Barett
26 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_OPENSSL_H
28#define POLARSSL_OPENSSL_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker314052f2011-08-15 09:07:52 +000030#include "aes.h"
31#include "md5.h"
32#include "rsa.h"
33#include "sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
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
62/*
63 * RSA stuff follows. TODO: needs cleanup
64 */
65inline int __RSA_Passthrough( void *output, void *input, int size )
66{
67 memcpy( output, input, size );
68 return size;
69}
70
71inline rsa_context* d2i_RSA_PUBKEY( void *ignore, unsigned char **bufptr,
72 int len )
73{
74 unsigned char *buffer = *(unsigned char **) bufptr;
75 rsa_context *rsa;
76
77 /*
78 * Not a general-purpose parser: only parses public key from *exactly*
79 * openssl genrsa -out privkey.pem 512 (or 1024)
80 * openssl rsa -in privkey.pem -out privatekey.der -outform der
81 * openssl rsa -in privkey.pem -out pubkey.der -outform der -pubout
82 *
83 * TODO: make a general-purpose parse
84 */
85 if( ignore != 0 || ( len != 94 && len != 162 ) )
86 return( 0 );
87
88 rsa = (rsa_context *) malloc( sizeof( rsa_rsa ) );
89 if( rsa == NULL )
90 return( 0 );
91
92 memset( rsa, 0, sizeof( rsa_context ) );
93
94 if( ( len == 94 &&
95 mpi_read_binary( &rsa->N, &buffer[ 25], 64 ) == 0 &&
96 mpi_read_binary( &rsa->E, &buffer[ 91], 3 ) == 0 ) ||
97 ( len == 162 &&
98 mpi_read_binary( &rsa->N, &buffer[ 29], 128 ) == 0 ) &&
99 mpi_read_binary( &rsa->E, &buffer[159], 3 ) == 0 )
100 {
101 /*
102 * key read successfully
103 */
104 rsa->len = ( mpi_msb( &rsa->N ) + 7 ) >> 3;
105 return( rsa );
106 }
107 else
108 {
109 memset( rsa, 0, sizeof( rsa_context ) );
110 free( rsa );
111 return( 0 );
112 }
113}
114
115#define RSA rsa_context
116#define RSA_PKCS1_PADDING 1 /* ignored; always encrypt with this */
117#define RSA_size( CTX ) (CTX)->len
118#define RSA_free( CTX ) rsa_free( CTX )
119#define ERR_get_error( ) "ERR_get_error() not supported"
120#define RSA_blinding_off( IGNORE )
121
122#define d2i_RSAPrivateKey( a, b, c ) new rsa_context /* TODO: C++ bleh */
123
124inline 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; }
125inline 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; }
126inline 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; }
127inline 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; }
128
129#ifdef __cplusplus
130}
131#endif
132
133#endif /* openssl.h */