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