blob: 039f4989a39762273bd15c39e9fd03c99f8f70ce [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é-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2010, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +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
Paul Bakker30b95fa2013-10-01 10:09:06 +020062#ifdef __cplusplus
63extern "C" {
64#endif
65
Paul Bakker5121ce52009-01-03 21:22:43 +000066/*
67 * RSA stuff follows. TODO: needs cleanup
68 */
69inline int __RSA_Passthrough( void *output, void *input, int size )
70{
71 memcpy( output, input, size );
72 return size;
73}
74
75inline 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 Bakker30b95fa2013-10-01 10:09:06 +020080
Paul Bakker5121ce52009-01-03 21:22:43 +000081 /*
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 Bakker9af723c2014-05-01 13:03:14 +020098 if( ( len == 94 &&
Paul Bakker5121ce52009-01-03 21:22:43 +000099 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
128inline 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; }
129inline 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; }
130inline 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; }
131inline 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 */