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