blob: a7abf2a24efb68eab2c925d156008f23c7fd4e23 [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 Bakker343a8702011-06-09 14:27:58 +000049int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
50 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
51{
52#if defined(POLARSSL_CIPHER_MODE_CFB)
53 return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
54#else
55 ((void) ctx);
56 ((void) operation);
57 ((void) length);
58 ((void) iv_off);
59 ((void) iv);
60 ((void) input);
61 ((void) output);
62
63 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
64#endif
65}
66
67int aes_crypt_ctr_wrap( void *ctx, size_t length,
68 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
69 const unsigned char *input, unsigned char *output )
70{
71#if defined(POLARSSL_CIPHER_MODE_CTR)
72 return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
73 stream_block, input, output );
74#else
75 ((void) ctx);
76 ((void) length);
77 ((void) nc_off);
78 ((void) nonce_counter);
79 ((void) stream_block);
80 ((void) input);
81 ((void) output);
82
83 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
84#endif
85}
86
Paul Bakker23986e52011-04-24 08:57:21 +000087int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +000088{
89 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
90}
91
Paul Bakker23986e52011-04-24 08:57:21 +000092int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +000093{
94 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
95}
96
97static void * aes_ctx_alloc( void )
98{
99 return malloc( sizeof( aes_context ) );
100}
101
102static void aes_ctx_free( void *ctx )
103{
104 free( ctx );
105}
106
Paul Bakker343a8702011-06-09 14:27:58 +0000107const cipher_base_t aes_info = {
108 POLARSSL_CIPHER_ID_AES,
109 aes_crypt_cbc_wrap,
110 aes_crypt_cfb128_wrap,
111 aes_crypt_ctr_wrap,
112 aes_setkey_enc_wrap,
113 aes_setkey_dec_wrap,
114 aes_ctx_alloc,
115 aes_ctx_free
116};
117
Paul Bakker8123e9d2011-01-06 15:37:30 +0000118const cipher_info_t aes_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000119 POLARSSL_CIPHER_AES_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000120 POLARSSL_MODE_CBC,
121 128,
122 "AES-128-CBC",
123 16,
124 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000125 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000126};
127
128const cipher_info_t aes_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000129 POLARSSL_CIPHER_AES_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000130 POLARSSL_MODE_CBC,
131 192,
132 "AES-192-CBC",
133 16,
134 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000135 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000136};
137
138const cipher_info_t aes_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000139 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000140 POLARSSL_MODE_CBC,
141 256,
142 "AES-256-CBC",
143 16,
144 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000145 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000146};
Paul Bakker343a8702011-06-09 14:27:58 +0000147
148#if defined(POLARSSL_CIPHER_MODE_CFB)
149const cipher_info_t aes_128_cfb128_info = {
150 POLARSSL_CIPHER_AES_128_CFB128,
151 POLARSSL_MODE_CFB128,
152 128,
153 "AES-128-CFB128",
154 16,
155 16,
156 &aes_info
157};
158
159const cipher_info_t aes_192_cfb128_info = {
160 POLARSSL_CIPHER_AES_192_CFB128,
161 POLARSSL_MODE_CFB128,
162 192,
163 "AES-192-CFB128",
164 16,
165 16,
166 &aes_info
167};
168
169const cipher_info_t aes_256_cfb128_info = {
170 POLARSSL_CIPHER_AES_256_CFB128,
171 POLARSSL_MODE_CFB128,
172 256,
173 "AES-256-CFB128",
174 16,
175 16,
176 &aes_info
177};
178#endif /* POLARSSL_CIPHER_MODE_CFB */
179
180#if defined(POLARSSL_CIPHER_MODE_CTR)
181const cipher_info_t aes_128_ctr_info = {
182 POLARSSL_CIPHER_AES_128_CTR,
183 POLARSSL_MODE_CTR,
184 128,
185 "AES-128-CTR",
186 16,
187 16,
188 &aes_info
189};
190
191const cipher_info_t aes_192_ctr_info = {
192 POLARSSL_CIPHER_AES_192_CTR,
193 POLARSSL_MODE_CTR,
194 192,
195 "AES-192-CTR",
196 16,
197 16,
198 &aes_info
199};
200
201const cipher_info_t aes_256_ctr_info = {
202 POLARSSL_CIPHER_AES_256_CTR,
203 POLARSSL_MODE_CTR,
204 256,
205 "AES-256-CTR",
206 16,
207 16,
208 &aes_info
209};
210#endif /* POLARSSL_CIPHER_MODE_CTR */
211
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212#endif
213
214#if defined(POLARSSL_CAMELLIA_C)
215
Paul Bakker23986e52011-04-24 08:57:21 +0000216int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217 unsigned char *iv, const unsigned char *input, unsigned char *output )
218{
219 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
220}
221
Paul Bakker343a8702011-06-09 14:27:58 +0000222int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
223 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
224{
225#if defined(POLARSSL_CIPHER_MODE_CFB)
226 return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
227#else
228 ((void) ctx);
229 ((void) operation);
230 ((void) length);
231 ((void) iv_off);
232 ((void) iv);
233 ((void) input);
234 ((void) output);
235
236 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
237#endif
238}
239
240int camellia_crypt_ctr_wrap( void *ctx, size_t length,
241 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
242 const unsigned char *input, unsigned char *output )
243{
244#if defined(POLARSSL_CIPHER_MODE_CTR)
245 return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
246 stream_block, input, output );
247#else
248 ((void) ctx);
249 ((void) length);
250 ((void) nc_off);
251 ((void) nonce_counter);
252 ((void) stream_block);
253 ((void) input);
254 ((void) output);
255
256 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
257#endif
258}
259
Paul Bakker23986e52011-04-24 08:57:21 +0000260int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000261{
262 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
263}
264
Paul Bakker23986e52011-04-24 08:57:21 +0000265int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000266{
267 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
268}
269
270static void * camellia_ctx_alloc( void )
271{
272 return malloc( sizeof( camellia_context ) );
273}
274
275static void camellia_ctx_free( void *ctx )
276{
277 free( ctx );
278}
279
Paul Bakker343a8702011-06-09 14:27:58 +0000280const cipher_base_t camellia_info = {
281 POLARSSL_CIPHER_ID_CAMELLIA,
282 camellia_crypt_cbc_wrap,
283 camellia_crypt_cfb128_wrap,
284 camellia_crypt_ctr_wrap,
285 camellia_setkey_enc_wrap,
286 camellia_setkey_dec_wrap,
287 camellia_ctx_alloc,
288 camellia_ctx_free
289};
290
Paul Bakker8123e9d2011-01-06 15:37:30 +0000291const cipher_info_t camellia_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000292 POLARSSL_CIPHER_CAMELLIA_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000293 POLARSSL_MODE_CBC,
294 128,
295 "CAMELLIA-128-CBC",
296 16,
297 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000298 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000299};
300
301const cipher_info_t camellia_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000302 POLARSSL_CIPHER_CAMELLIA_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000303 POLARSSL_MODE_CBC,
304 192,
305 "CAMELLIA-192-CBC",
306 16,
307 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000308 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000309};
310
311const cipher_info_t camellia_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000312 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000313 POLARSSL_MODE_CBC,
314 256,
315 "CAMELLIA-256-CBC",
316 16,
317 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000318 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000319};
Paul Bakker343a8702011-06-09 14:27:58 +0000320
321#if defined(POLARSSL_CIPHER_MODE_CFB)
322const cipher_info_t camellia_128_cfb128_info = {
323 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
324 POLARSSL_MODE_CFB128,
325 128,
326 "CAMELLIA-128-CFB128",
327 16,
328 16,
329 &camellia_info
330};
331
332const cipher_info_t camellia_192_cfb128_info = {
333 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
334 POLARSSL_MODE_CFB128,
335 192,
336 "CAMELLIA-192-CFB128",
337 16,
338 16,
339 &camellia_info
340};
341
342const cipher_info_t camellia_256_cfb128_info = {
343 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
344 POLARSSL_MODE_CFB128,
345 256,
346 "CAMELLIA-256-CFB128",
347 16,
348 16,
349 &camellia_info
350};
351#endif /* POLARSSL_CIPHER_MODE_CFB */
352
353#if defined(POLARSSL_CIPHER_MODE_CTR)
354const cipher_info_t camellia_128_ctr_info = {
355 POLARSSL_CIPHER_CAMELLIA_128_CTR,
356 POLARSSL_MODE_CTR,
357 128,
358 "CAMELLIA-128-CTR",
359 16,
360 16,
361 &camellia_info
362};
363
364const cipher_info_t camellia_192_ctr_info = {
365 POLARSSL_CIPHER_CAMELLIA_192_CTR,
366 POLARSSL_MODE_CTR,
367 192,
368 "CAMELLIA-192-CTR",
369 16,
370 16,
371 &camellia_info
372};
373
374const cipher_info_t camellia_256_ctr_info = {
375 POLARSSL_CIPHER_CAMELLIA_256_CTR,
376 POLARSSL_MODE_CTR,
377 256,
378 "CAMELLIA-256-CTR",
379 16,
380 16,
381 &camellia_info
382};
383#endif /* POLARSSL_CIPHER_MODE_CTR */
384
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385#endif
386
387#if defined(POLARSSL_DES_C)
388
Paul Bakker23986e52011-04-24 08:57:21 +0000389int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000390 unsigned char *iv, const unsigned char *input, unsigned char *output )
391{
392 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
393}
394
Paul Bakker23986e52011-04-24 08:57:21 +0000395int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000396 unsigned char *iv, const unsigned char *input, unsigned char *output )
397{
398 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
399}
400
Paul Bakker343a8702011-06-09 14:27:58 +0000401int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
402 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
403{
404 ((void) ctx);
405 ((void) operation);
406 ((void) length);
407 ((void) iv_off);
408 ((void) iv);
409 ((void) input);
410 ((void) output);
411
412 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
413}
414
415int des_crypt_ctr_wrap( void *ctx, size_t length,
416 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
417 const unsigned char *input, unsigned char *output )
418{
419 ((void) ctx);
420 ((void) length);
421 ((void) nc_off);
422 ((void) nonce_counter);
423 ((void) stream_block);
424 ((void) input);
425 ((void) output);
426
427 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
428}
429
430
Paul Bakker23986e52011-04-24 08:57:21 +0000431int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000432{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000433 ((void) key_length);
434
Paul Bakker8123e9d2011-01-06 15:37:30 +0000435 return des_setkey_dec( (des_context *) ctx, key );
436}
437
Paul Bakker23986e52011-04-24 08:57:21 +0000438int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000439{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000440 ((void) key_length);
441
Paul Bakker8123e9d2011-01-06 15:37:30 +0000442 return des_setkey_enc( (des_context *) ctx, key );
443}
444
Paul Bakker23986e52011-04-24 08:57:21 +0000445int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000446{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000447 ((void) key_length);
448
Paul Bakker8123e9d2011-01-06 15:37:30 +0000449 return des3_set2key_dec( (des3_context *) ctx, key );
450}
451
Paul Bakker23986e52011-04-24 08:57:21 +0000452int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000453{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000454 ((void) key_length);
455
Paul Bakker8123e9d2011-01-06 15:37:30 +0000456 return des3_set2key_enc( (des3_context *) ctx, key );
457}
458
Paul Bakker23986e52011-04-24 08:57:21 +0000459int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000460{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000461 ((void) key_length);
462
Paul Bakker8123e9d2011-01-06 15:37:30 +0000463 return des3_set3key_dec( (des3_context *) ctx, key );
464}
465
Paul Bakker23986e52011-04-24 08:57:21 +0000466int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000467{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000468 ((void) key_length);
469
Paul Bakker8123e9d2011-01-06 15:37:30 +0000470 return des3_set3key_enc( (des3_context *) ctx, key );
471}
472
473static void * des_ctx_alloc( void )
474{
475 return malloc( sizeof( des_context ) );
476}
477
478static void * des3_ctx_alloc( void )
479{
480 return malloc( sizeof( des3_context ) );
481}
482
483static void des_ctx_free( void *ctx )
484{
485 free( ctx );
486}
487
Paul Bakker343a8702011-06-09 14:27:58 +0000488const cipher_base_t des_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000489 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000490 des_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000491 des_crypt_cfb128_wrap,
492 des_crypt_ctr_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000493 des_setkey_enc_wrap,
494 des_setkey_dec_wrap,
495 des_ctx_alloc,
496 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497};
498
Paul Bakker343a8702011-06-09 14:27:58 +0000499const cipher_info_t des_cbc_info = {
500 POLARSSL_CIPHER_DES_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000501 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +0000502 POLARSSL_KEY_LENGTH_DES,
503 "DES-CBC",
504 8,
505 8,
506 &des_info
507};
508
509const cipher_base_t des_ede_info = {
510 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000511 des3_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000512 des_crypt_cfb128_wrap,
513 des_crypt_ctr_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000514 des3_set2key_enc_wrap,
515 des3_set2key_dec_wrap,
516 des3_ctx_alloc,
517 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000518};
519
Paul Bakker343a8702011-06-09 14:27:58 +0000520const cipher_info_t des_ede_cbc_info = {
521 POLARSSL_CIPHER_DES_EDE_CBC,
522 POLARSSL_MODE_CBC,
523 POLARSSL_KEY_LENGTH_DES_EDE,
524 "DES-EDE-CBC",
525 16,
526 16,
527 &des_ede_info
528};
529
530const cipher_base_t des_ede3_info = {
531 POLARSSL_CIPHER_ID_DES,
532 des3_crypt_cbc_wrap,
533 des_crypt_cfb128_wrap,
534 des_crypt_ctr_wrap,
535 des3_set3key_enc_wrap,
536 des3_set3key_dec_wrap,
537 des3_ctx_alloc,
538 des_ctx_free
539};
540
Paul Bakker8123e9d2011-01-06 15:37:30 +0000541const cipher_info_t des_ede3_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000542 POLARSSL_CIPHER_DES_EDE3_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000543 POLARSSL_MODE_CBC,
544 POLARSSL_KEY_LENGTH_DES_EDE3,
545 "DES-EDE3-CBC",
546 8,
547 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000548 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000549};
550#endif
551
552#endif