blob: dd41a353b9590887e4b733d86fac784a0f0e7e87 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file md_wrap.c
3 *
4 * \brief Generic message digest wrapper for PolarSSL
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
8 * Copyright (C) 2006-2010, Brainspark B.V.
9 *
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
12 *
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29
30#include "polarssl/config.h"
31
32#if defined(POLARSSL_CIPHER_C)
33
34#include "polarssl/cipher_wrap.h"
35#include "polarssl/aes.h"
36#include "polarssl/camellia.h"
37#include "polarssl/des.h"
38
39#include <string.h>
40#include <stdlib.h>
41
42#if defined(POLARSSL_AES_C)
43
44int aes_crypt_cbc_wrap( void *ctx, operation_t operation, int length,
45 unsigned char *iv, const unsigned char *input, unsigned char *output )
46{
47 return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
48}
49
50int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, int key_length )
51{
52 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
53}
54
55int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, int key_length )
56{
57 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
58}
59
60static void * aes_ctx_alloc( void )
61{
62 return malloc( sizeof( aes_context ) );
63}
64
65static void aes_ctx_free( void *ctx )
66{
67 free( ctx );
68}
69
70const cipher_info_t aes_128_cbc_info = {
71 .type = POLARSSL_CIPHER_AES_128_CBC,
72 .cipher = POLARSSL_CIPHER_ID_AES,
73 .mode = POLARSSL_MODE_CBC,
74 .key_length = 128,
75 .name = "AES-128-CBC",
76 .iv_size = 16,
77 .block_size = 16,
78 .cbc_func = aes_crypt_cbc_wrap,
79 .setkey_enc_func = aes_setkey_enc_wrap,
80 .setkey_dec_func = aes_setkey_dec_wrap,
81 .ctx_alloc_func = aes_ctx_alloc,
82 .ctx_free_func = aes_ctx_free
83};
84
85const cipher_info_t aes_192_cbc_info = {
86 .type = POLARSSL_CIPHER_AES_192_CBC,
87 .cipher = POLARSSL_CIPHER_ID_AES,
88 .mode = POLARSSL_MODE_CBC,
89 .key_length = 192,
90 .name = "AES-192-CBC",
91 .iv_size = 16,
92 .block_size = 16,
93 .cbc_func = aes_crypt_cbc_wrap,
94 .setkey_enc_func = aes_setkey_enc_wrap,
95 .setkey_dec_func = aes_setkey_dec_wrap,
96 .ctx_alloc_func = aes_ctx_alloc,
97 .ctx_free_func = aes_ctx_free
98};
99
100const cipher_info_t aes_256_cbc_info = {
101 .type = POLARSSL_CIPHER_AES_256_CBC,
102 .cipher = POLARSSL_CIPHER_ID_AES,
103 .mode = POLARSSL_MODE_CBC,
104 .key_length = 256,
105 .name = "AES-256-CBC",
106 .iv_size = 16,
107 .block_size = 16,
108 .cbc_func = aes_crypt_cbc_wrap,
109 .setkey_enc_func = aes_setkey_enc_wrap,
110 .setkey_dec_func = aes_setkey_dec_wrap,
111 .ctx_alloc_func = aes_ctx_alloc,
112 .ctx_free_func = aes_ctx_free
113};
114#endif
115
116#if defined(POLARSSL_CAMELLIA_C)
117
118int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, int length,
119 unsigned char *iv, const unsigned char *input, unsigned char *output )
120{
121 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
122}
123
124int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, int key_length )
125{
126 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
127}
128
129int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, int key_length )
130{
131 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
132}
133
134static void * camellia_ctx_alloc( void )
135{
136 return malloc( sizeof( camellia_context ) );
137}
138
139static void camellia_ctx_free( void *ctx )
140{
141 free( ctx );
142}
143
144const cipher_info_t camellia_128_cbc_info = {
145 .type = POLARSSL_CIPHER_CAMELLIA_128_CBC,
146 .cipher = POLARSSL_CIPHER_ID_CAMELLIA,
147 .mode = POLARSSL_MODE_CBC,
148 .key_length = 128,
149 .name = "CAMELLIA-128-CBC",
150 .iv_size = 16,
151 .block_size = 16,
152 .cbc_func = camellia_crypt_cbc_wrap,
153 .setkey_enc_func = camellia_setkey_enc_wrap,
154 .setkey_dec_func = camellia_setkey_dec_wrap,
155 .ctx_alloc_func = camellia_ctx_alloc,
156 .ctx_free_func = camellia_ctx_free
157};
158
159const cipher_info_t camellia_192_cbc_info = {
160 .type = POLARSSL_CIPHER_CAMELLIA_192_CBC,
161 .cipher = POLARSSL_CIPHER_ID_CAMELLIA,
162 .mode = POLARSSL_MODE_CBC,
163 .key_length = 192,
164 .name = "CAMELLIA-192-CBC",
165 .iv_size = 16,
166 .block_size = 16,
167 .cbc_func = camellia_crypt_cbc_wrap,
168 .setkey_enc_func = camellia_setkey_enc_wrap,
169 .setkey_dec_func = camellia_setkey_dec_wrap,
170 .ctx_alloc_func = camellia_ctx_alloc,
171 .ctx_free_func = camellia_ctx_free
172};
173
174const cipher_info_t camellia_256_cbc_info = {
175 .type = POLARSSL_CIPHER_CAMELLIA_256_CBC,
176 .cipher = POLARSSL_CIPHER_ID_CAMELLIA,
177 .mode = POLARSSL_MODE_CBC,
178 .key_length = 256,
179 .name = "CAMELLIA-256-CBC",
180 .iv_size = 16,
181 .block_size = 16,
182 .cbc_func = camellia_crypt_cbc_wrap,
183 .setkey_enc_func = camellia_setkey_enc_wrap,
184 .setkey_dec_func = camellia_setkey_dec_wrap,
185 .ctx_alloc_func = camellia_ctx_alloc,
186 .ctx_free_func = camellia_ctx_free
187};
188#endif
189
190#if defined(POLARSSL_DES_C)
191
192int des_crypt_cbc_wrap( void *ctx, operation_t operation, int length,
193 unsigned char *iv, const unsigned char *input, unsigned char *output )
194{
195 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
196}
197
198int des3_crypt_cbc_wrap( void *ctx, operation_t operation, int length,
199 unsigned char *iv, const unsigned char *input, unsigned char *output )
200{
201 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
202}
203
204int des_setkey_dec_wrap( void *ctx, const unsigned char *key, int key_length )
205{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000206 ((void) key_length);
207
Paul Bakker8123e9d2011-01-06 15:37:30 +0000208 return des_setkey_dec( (des_context *) ctx, key );
209}
210
211int des_setkey_enc_wrap( void *ctx, const unsigned char *key, int key_length )
212{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000213 ((void) key_length);
214
Paul Bakker8123e9d2011-01-06 15:37:30 +0000215 return des_setkey_enc( (des_context *) ctx, key );
216}
217
218int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, int key_length )
219{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000220 ((void) key_length);
221
Paul Bakker8123e9d2011-01-06 15:37:30 +0000222 return des3_set2key_dec( (des3_context *) ctx, key );
223}
224
225int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, int key_length )
226{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000227 ((void) key_length);
228
Paul Bakker8123e9d2011-01-06 15:37:30 +0000229 return des3_set2key_enc( (des3_context *) ctx, key );
230}
231
232int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, int key_length )
233{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000234 ((void) key_length);
235
Paul Bakker8123e9d2011-01-06 15:37:30 +0000236 return des3_set3key_dec( (des3_context *) ctx, key );
237}
238
239int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, int key_length )
240{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000241 ((void) key_length);
242
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243 return des3_set3key_enc( (des3_context *) ctx, key );
244}
245
246static void * des_ctx_alloc( void )
247{
248 return malloc( sizeof( des_context ) );
249}
250
251static void * des3_ctx_alloc( void )
252{
253 return malloc( sizeof( des3_context ) );
254}
255
256static void des_ctx_free( void *ctx )
257{
258 free( ctx );
259}
260
261const cipher_info_t des_cbc_info = {
262 .type = POLARSSL_CIPHER_DES_CBC,
263 .cipher = POLARSSL_CIPHER_ID_DES,
264 .mode = POLARSSL_MODE_CBC,
265 .key_length = POLARSSL_KEY_LENGTH_DES,
266 .name = "DES-CBC",
267 .iv_size = 8,
268 .block_size = 8,
269 .cbc_func = des_crypt_cbc_wrap,
270 .setkey_enc_func = des_setkey_enc_wrap,
271 .setkey_dec_func = des_setkey_dec_wrap,
272 .ctx_alloc_func = des_ctx_alloc,
273 .ctx_free_func = des_ctx_free
274};
275
276const cipher_info_t des_ede_cbc_info = {
277 .type = POLARSSL_CIPHER_DES_EDE_CBC,
278 .cipher = POLARSSL_CIPHER_ID_DES,
279 .mode = POLARSSL_MODE_CBC,
280 .key_length = POLARSSL_KEY_LENGTH_DES_EDE,
281 .name = "DES-EDE-CBC",
282 .iv_size = 16,
283 .block_size = 16,
284 .cbc_func = des3_crypt_cbc_wrap,
285 .setkey_enc_func = des3_set2key_enc_wrap,
286 .setkey_dec_func = des3_set2key_dec_wrap,
287 .ctx_alloc_func = des3_ctx_alloc,
288 .ctx_free_func = des_ctx_free
289};
290
291const cipher_info_t des_ede3_cbc_info = {
292 .type = POLARSSL_CIPHER_DES_EDE3_CBC,
293 .cipher = POLARSSL_CIPHER_ID_DES,
294 .mode = POLARSSL_MODE_CBC,
295 .key_length = POLARSSL_KEY_LENGTH_DES_EDE3,
296 .name = "DES-EDE3-CBC",
297 .iv_size = 8,
298 .block_size = 8,
299 .cbc_func = des3_crypt_cbc_wrap,
300 .setkey_enc_func = des3_set3key_enc_wrap,
301 .setkey_dec_func = des3_set3key_dec_wrap,
302 .ctx_alloc_func = des3_ctx_alloc,
303 .ctx_free_func = des_ctx_free
304};
305#endif
306
307#endif