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