blob: b77e7dacb72809696913e03aa30d937cdc5d80ed [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 Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
27/*
28 * OpenSSL wrapper contributed by David Barett
29 */
Paul Bakker40e46942009-01-03 21:51:57 +000030#ifndef POLARSSL_OPENSSL_H
31#define POLARSSL_OPENSSL_H
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakker314052f2011-08-15 09:07:52 +000033#include "aes.h"
34#include "md5.h"
35#include "rsa.h"
36#include "sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
38#define AES_SIZE 16
39#define AES_BLOCK_SIZE 16
40#define AES_KEY aes_context
41#define MD5_CTX md5_context
42#define SHA_CTX sha1_context
43
44#define SHA1_Init( CTX ) \
45 sha1_starts( (CTX) )
46#define SHA1_Update( CTX, BUF, LEN ) \
47 sha1_update( (CTX), (unsigned char *)(BUF), (LEN) )
48#define SHA1_Final( OUT, CTX ) \
49 sha1_finish( (CTX), (OUT) )
50
51#define MD5_Init( CTX ) \
52 md5_starts( (CTX) )
53#define MD5_Update( CTX, BUF, LEN ) \
54 md5_update( (CTX), (unsigned char *)(BUF), (LEN) )
55#define MD5_Final( OUT, CTX ) \
56 md5_finish( (CTX), (OUT) )
57
58#define AES_set_encrypt_key( KEY, KEYSIZE, CTX ) \
59 aes_setkey_enc( (CTX), (KEY), (KEYSIZE) )
60#define AES_set_decrypt_key( KEY, KEYSIZE, CTX ) \
61 aes_setkey_dec( (CTX), (KEY), (KEYSIZE) )
62#define AES_cbc_encrypt( INPUT, OUTPUT, LEN, CTX, IV, MODE ) \
63 aes_crypt_cbc( (CTX), (MODE), (LEN), (IV), (INPUT), (OUTPUT) )
64
Paul Bakker30b95fa2013-10-01 10:09:06 +020065#ifdef __cplusplus
66extern "C" {
67#endif
68
Paul Bakker5121ce52009-01-03 21:22:43 +000069/*
70 * RSA stuff follows. TODO: needs cleanup
71 */
72inline int __RSA_Passthrough( void *output, void *input, int size )
73{
74 memcpy( output, input, size );
75 return size;
76}
77
78inline rsa_context* d2i_RSA_PUBKEY( void *ignore, unsigned char **bufptr,
79 int len )
80{
81 unsigned char *buffer = *(unsigned char **) bufptr;
82 rsa_context *rsa;
Paul Bakker30b95fa2013-10-01 10:09:06 +020083
Paul Bakker5121ce52009-01-03 21:22:43 +000084 /*
85 * Not a general-purpose parser: only parses public key from *exactly*
86 * openssl genrsa -out privkey.pem 512 (or 1024)
87 * openssl rsa -in privkey.pem -out privatekey.der -outform der
88 * openssl rsa -in privkey.pem -out pubkey.der -outform der -pubout
89 *
90 * TODO: make a general-purpose parse
91 */
92 if( ignore != 0 || ( len != 94 && len != 162 ) )
93 return( 0 );
94
95 rsa = (rsa_context *) malloc( sizeof( rsa_rsa ) );
96 if( rsa == NULL )
97 return( 0 );
98
99 memset( rsa, 0, sizeof( rsa_context ) );
100
Paul Bakker9af723c2014-05-01 13:03:14 +0200101 if( ( len == 94 &&
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 mpi_read_binary( &rsa->N, &buffer[ 25], 64 ) == 0 &&
103 mpi_read_binary( &rsa->E, &buffer[ 91], 3 ) == 0 ) ||
104 ( len == 162 &&
105 mpi_read_binary( &rsa->N, &buffer[ 29], 128 ) == 0 ) &&
106 mpi_read_binary( &rsa->E, &buffer[159], 3 ) == 0 )
107 {
108 /*
109 * key read successfully
110 */
111 rsa->len = ( mpi_msb( &rsa->N ) + 7 ) >> 3;
112 return( rsa );
113 }
114 else
115 {
116 memset( rsa, 0, sizeof( rsa_context ) );
117 free( rsa );
118 return( 0 );
119 }
120}
121
122#define RSA rsa_context
123#define RSA_PKCS1_PADDING 1 /* ignored; always encrypt with this */
124#define RSA_size( CTX ) (CTX)->len
125#define RSA_free( CTX ) rsa_free( CTX )
126#define ERR_get_error( ) "ERR_get_error() not supported"
127#define RSA_blinding_off( IGNORE )
128
129#define d2i_RSAPrivateKey( a, b, c ) new rsa_context /* TODO: C++ bleh */
130
131inline 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; }
132inline 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; }
133inline 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; }
134inline 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; }
135
136#ifdef __cplusplus
137}
138#endif
139
140#endif /* openssl.h */