camellia: fix camellia_self_test failure on compilers where char is unsigned by default
[tropicssl.git] / include / tropicssl / openssl.h
blob792a39d670dd4c8fdc4d5e37f32f53006a2d5acb
1 /**
2 * \file openssl.h
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * OpenSSL wrapper contributed by David Barett
38 #ifndef TROPICSSL_OPENSSL_H
39 #define TROPICSSL_OPENSSL_H
41 #include "tropicssl/aes.h"
42 #include "tropicssl/md5.h"
43 #include "tropicssl/rsa.h"
44 #include "tropicssl/sha1.h"
46 #define AES_SIZE 16
47 #define AES_BLOCK_SIZE 16
48 #define AES_KEY aes_context
49 #define MD5_CTX md5_context
50 #define SHA_CTX sha1_context
52 #define SHA1_Init( CTX ) \
53 sha1_starts( (CTX) )
54 #define SHA1_Update( CTX, BUF, LEN ) \
55 sha1_update( (CTX), (unsigned char *)(BUF), (LEN) )
56 #define SHA1_Final( OUT, CTX ) \
57 sha1_finish( (CTX), (OUT) )
59 #define MD5_Init( CTX ) \
60 md5_starts( (CTX) )
61 #define MD5_Update( CTX, BUF, LEN ) \
62 md5_update( (CTX), (unsigned char *)(BUF), (LEN) )
63 #define MD5_Final( OUT, CTX ) \
64 md5_finish( (CTX), (OUT) )
66 #define AES_set_encrypt_key( KEY, KEYSIZE, CTX ) \
67 aes_setkey_enc( (CTX), (KEY), (KEYSIZE) )
68 #define AES_set_decrypt_key( KEY, KEYSIZE, CTX ) \
69 aes_setkey_dec( (CTX), (KEY), (KEYSIZE) )
70 #define AES_cbc_encrypt( INPUT, OUTPUT, LEN, CTX, IV, MODE ) \
71 aes_crypt_cbc( (CTX), (MODE), (LEN), (IV), (INPUT), (OUTPUT) )
74 * RSA stuff follows. TODO: needs cleanup
76 inline int __RSA_Passthrough(void *output, void *input, int size)
78 memcpy(output, input, size);
79 return size;
82 inline rsa_context *d2i_RSA_PUBKEY(void *ignore, unsigned char **bufptr,
83 int len)
85 unsigned char *buffer = *(unsigned char **)bufptr;
86 rsa_context *rsa;
89 * Not a general-purpose parser: only parses public key from *exactly*
90 * openssl genrsa -out privkey.pem 512 (or 1024)
91 * openssl rsa -in privkey.pem -out privatekey.der -outform der
92 * openssl rsa -in privkey.pem -out pubkey.der -outform der -pubout
94 * TODO: make a general-purpose parse
96 if (ignore != 0 || (len != 94 && len != 162))
97 return (0);
99 rsa = (rsa_context *) malloc(sizeof(rsa_rsa));
100 if (rsa == NULL)
101 return (0);
103 memset(rsa, 0, sizeof(rsa_context));
105 if ((len == 94 &&
106 mpi_read_binary(&rsa->N, &buffer[25], 64) == 0 &&
107 mpi_read_binary(&rsa->E, &buffer[91], 3) == 0) ||
108 (len == 162 &&
109 mpi_read_binary(&rsa->N, &buffer[29], 128) == 0) &&
110 mpi_read_binary(&rsa->E, &buffer[159], 3) == 0) {
112 * key read successfully
114 rsa->len = (mpi_msb(&rsa->N) + 7) >> 3;
115 return (rsa);
116 } else {
117 memset(rsa, 0, sizeof(rsa_context));
118 free(rsa);
119 return (0);
123 #define RSA rsa_context
124 #define RSA_PKCS1_PADDING 1 /* ignored; always encrypt with this */
125 #define RSA_size( CTX ) (CTX)->len
126 #define RSA_free( CTX ) rsa_free( CTX )
127 #define ERR_get_error( ) "ERR_get_error() not supported"
128 #define RSA_blinding_off( IGNORE )
130 #define d2i_RSAPrivateKey( a, b, c ) new rsa_context /* TODO: C++ bleh */
132 inline int RSA_public_decrypt(int size, unsigned char *input,
133 unsigned char *output, RSA * key, int ignore)
135 int outsize = size;
136 if (!rsa_pkcs1_decrypt(key, RSA_PUBLIC, &outsize, input, output))
137 return outsize;
138 else
139 return -1;
142 inline int RSA_private_decrypt(int size, unsigned char *input,
143 unsigned char *output, RSA * key, int ignore)
145 int outsize = size;
146 if (!rsa_pkcs1_decrypt(key, RSA_PRIVATE, &outsize, input, output))
147 return outsize;
148 else
149 return -1;
152 inline int RSA_public_encrypt(int size, unsigned char *input,
153 unsigned char *output, RSA * key, int ignore)
155 if (!rsa_pkcs1_encrypt(key, RSA_PUBLIC, size, input, output))
156 return RSA_size(key);
157 else
158 return -1;
161 inline int RSA_private_encrypt(int size, unsigned char *input,
162 unsigned char *output, RSA * key, int ignore)
164 if (!rsa_pkcs1_encrypt(key, RSA_PRIVATE, size, input, output))
165 return RSA_size(key);
166 else
167 return -1;
170 #endif /* openssl.h */