blob: f1abf299a5612153eed53b085175fa257477627e [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
Paul Bakker8123e9d2011-01-06 15:37:30 +000039#include <stdlib.h>
40
41#if defined(POLARSSL_AES_C)
42
Paul Bakker23986e52011-04-24 08:57:21 +000043int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +000044 unsigned char *iv, const unsigned char *input, unsigned char *output )
45{
46 return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
47}
48
Paul Bakker23986e52011-04-24 08:57:21 +000049int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +000050{
51 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
52}
53
Paul Bakker23986e52011-04-24 08:57:21 +000054int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +000055{
56 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
57}
58
59static void * aes_ctx_alloc( void )
60{
61 return malloc( sizeof( aes_context ) );
62}
63
64static void aes_ctx_free( void *ctx )
65{
66 free( ctx );
67}
68
69const cipher_info_t aes_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +000070 POLARSSL_CIPHER_AES_128_CBC,
71 POLARSSL_CIPHER_ID_AES,
72 POLARSSL_MODE_CBC,
73 128,
74 "AES-128-CBC",
75 16,
76 16,
77 aes_crypt_cbc_wrap,
78 aes_setkey_enc_wrap,
79 aes_setkey_dec_wrap,
80 aes_ctx_alloc,
81 aes_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +000082};
83
84const cipher_info_t aes_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +000085 POLARSSL_CIPHER_AES_192_CBC,
86 POLARSSL_CIPHER_ID_AES,
87 POLARSSL_MODE_CBC,
88 192,
89 "AES-192-CBC",
90 16,
91 16,
92 aes_crypt_cbc_wrap,
93 aes_setkey_enc_wrap,
94 aes_setkey_dec_wrap,
95 aes_ctx_alloc,
96 aes_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +000097};
98
99const cipher_info_t aes_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000100 POLARSSL_CIPHER_AES_256_CBC,
101 POLARSSL_CIPHER_ID_AES,
102 POLARSSL_MODE_CBC,
103 256,
104 "AES-256-CBC",
105 16,
106 16,
107 aes_crypt_cbc_wrap,
108 aes_setkey_enc_wrap,
109 aes_setkey_dec_wrap,
110 aes_ctx_alloc,
111 aes_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000112};
113#endif
114
115#if defined(POLARSSL_CAMELLIA_C)
116
Paul Bakker23986e52011-04-24 08:57:21 +0000117int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000118 unsigned char *iv, const unsigned char *input, unsigned char *output )
119{
120 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
121}
122
Paul Bakker23986e52011-04-24 08:57:21 +0000123int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000124{
125 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
126}
127
Paul Bakker23986e52011-04-24 08:57:21 +0000128int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000129{
130 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
131}
132
133static void * camellia_ctx_alloc( void )
134{
135 return malloc( sizeof( camellia_context ) );
136}
137
138static void camellia_ctx_free( void *ctx )
139{
140 free( ctx );
141}
142
143const cipher_info_t camellia_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000144 POLARSSL_CIPHER_CAMELLIA_128_CBC,
145 POLARSSL_CIPHER_ID_CAMELLIA,
146 POLARSSL_MODE_CBC,
147 128,
148 "CAMELLIA-128-CBC",
149 16,
150 16,
151 camellia_crypt_cbc_wrap,
152 camellia_setkey_enc_wrap,
153 camellia_setkey_dec_wrap,
154 camellia_ctx_alloc,
155 camellia_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000156};
157
158const cipher_info_t camellia_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000159 POLARSSL_CIPHER_CAMELLIA_192_CBC,
160 POLARSSL_CIPHER_ID_CAMELLIA,
161 POLARSSL_MODE_CBC,
162 192,
163 "CAMELLIA-192-CBC",
164 16,
165 16,
166 camellia_crypt_cbc_wrap,
167 camellia_setkey_enc_wrap,
168 camellia_setkey_dec_wrap,
169 camellia_ctx_alloc,
170 camellia_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000171};
172
173const cipher_info_t camellia_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000174 POLARSSL_CIPHER_CAMELLIA_256_CBC,
175 POLARSSL_CIPHER_ID_CAMELLIA,
176 POLARSSL_MODE_CBC,
177 256,
178 "CAMELLIA-256-CBC",
179 16,
180 16,
181 camellia_crypt_cbc_wrap,
182 camellia_setkey_enc_wrap,
183 camellia_setkey_dec_wrap,
184 camellia_ctx_alloc,
185 camellia_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000186};
187#endif
188
189#if defined(POLARSSL_DES_C)
190
Paul Bakker23986e52011-04-24 08:57:21 +0000191int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192 unsigned char *iv, const unsigned char *input, unsigned char *output )
193{
194 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
195}
196
Paul Bakker23986e52011-04-24 08:57:21 +0000197int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000198 unsigned char *iv, const unsigned char *input, unsigned char *output )
199{
200 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
201}
202
Paul Bakker23986e52011-04-24 08:57:21 +0000203int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000204{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000205 ((void) key_length);
206
Paul Bakker8123e9d2011-01-06 15:37:30 +0000207 return des_setkey_dec( (des_context *) ctx, key );
208}
209
Paul Bakker23986e52011-04-24 08:57:21 +0000210int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000211{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000212 ((void) key_length);
213
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214 return des_setkey_enc( (des_context *) ctx, key );
215}
216
Paul Bakker23986e52011-04-24 08:57:21 +0000217int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000218{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000219 ((void) key_length);
220
Paul Bakker8123e9d2011-01-06 15:37:30 +0000221 return des3_set2key_dec( (des3_context *) ctx, key );
222}
223
Paul Bakker23986e52011-04-24 08:57:21 +0000224int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000225{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000226 ((void) key_length);
227
Paul Bakker8123e9d2011-01-06 15:37:30 +0000228 return des3_set2key_enc( (des3_context *) ctx, key );
229}
230
Paul Bakker23986e52011-04-24 08:57:21 +0000231int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000232{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000233 ((void) key_length);
234
Paul Bakker8123e9d2011-01-06 15:37:30 +0000235 return des3_set3key_dec( (des3_context *) ctx, key );
236}
237
Paul Bakker23986e52011-04-24 08:57:21 +0000238int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000239{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000240 ((void) key_length);
241
Paul Bakker8123e9d2011-01-06 15:37:30 +0000242 return des3_set3key_enc( (des3_context *) ctx, key );
243}
244
245static void * des_ctx_alloc( void )
246{
247 return malloc( sizeof( des_context ) );
248}
249
250static void * des3_ctx_alloc( void )
251{
252 return malloc( sizeof( des3_context ) );
253}
254
255static void des_ctx_free( void *ctx )
256{
257 free( ctx );
258}
259
260const cipher_info_t des_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000261 POLARSSL_CIPHER_DES_CBC,
262 POLARSSL_CIPHER_ID_DES,
263 POLARSSL_MODE_CBC,
264 POLARSSL_KEY_LENGTH_DES,
265 "DES-CBC",
266 8,
267 8,
268 des_crypt_cbc_wrap,
269 des_setkey_enc_wrap,
270 des_setkey_dec_wrap,
271 des_ctx_alloc,
272 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000273};
274
275const cipher_info_t des_ede_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000276 POLARSSL_CIPHER_DES_EDE_CBC,
277 POLARSSL_CIPHER_ID_DES,
278 POLARSSL_MODE_CBC,
279 POLARSSL_KEY_LENGTH_DES_EDE,
280 "DES-EDE-CBC",
281 16,
282 16,
283 des3_crypt_cbc_wrap,
284 des3_set2key_enc_wrap,
285 des3_set2key_dec_wrap,
286 des3_ctx_alloc,
287 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000288};
289
290const cipher_info_t des_ede3_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000291 POLARSSL_CIPHER_DES_EDE3_CBC,
292 POLARSSL_CIPHER_ID_DES,
293 POLARSSL_MODE_CBC,
294 POLARSSL_KEY_LENGTH_DES_EDE3,
295 "DES-EDE3-CBC",
296 8,
297 8,
298 des3_crypt_cbc_wrap,
299 des3_set3key_enc_wrap,
300 des3_set3key_dec_wrap,
301 des3_ctx_alloc,
302 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000303};
304#endif
305
306#endif