initial commit; it works
[psslcertgen.git] / src / libpolarssl / cipher_wrap.c
blob4e85c8889d968605c151aa48c915c6d9a44ee877
1 /**
2 * \file md_wrap.c
3 *
4 * \brief Generic cipher wrapper for PolarSSL
6 * \author Adriaan de Jong <dejong@fox-it.com>
8 * Copyright (C) 2006-2012, Brainspark B.V.
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
13 * All rights reserved.
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.
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.
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.
30 #include "config.h"
32 #if defined(POLARSSL_CIPHER_C)
34 #include "cipher_wrap.h"
36 #if defined(POLARSSL_AES_C)
37 #include "aes.h"
38 #endif
40 #if defined(POLARSSL_CAMELLIA_C)
41 #include "camellia.h"
42 #endif
44 #if defined(POLARSSL_DES_C)
45 #include "des.h"
46 #endif
48 #if defined(POLARSSL_BLOWFISH_C)
49 #include "blowfish.h"
50 #endif
52 #include <stdlib.h>
54 #if defined(POLARSSL_AES_C)
56 int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
57 unsigned char *iv, const unsigned char *input, unsigned char *output )
59 return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
62 int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
63 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
65 #if defined(POLARSSL_CIPHER_MODE_CFB)
66 return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
67 #else
68 ((void) ctx);
69 ((void) operation);
70 ((void) length);
71 ((void) iv_off);
72 ((void) iv);
73 ((void) input);
74 ((void) output);
76 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
77 #endif
80 int aes_crypt_ctr_wrap( void *ctx, size_t length,
81 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
82 const unsigned char *input, unsigned char *output )
84 #if defined(POLARSSL_CIPHER_MODE_CTR)
85 return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
86 stream_block, input, output );
87 #else
88 ((void) ctx);
89 ((void) length);
90 ((void) nc_off);
91 ((void) nonce_counter);
92 ((void) stream_block);
93 ((void) input);
94 ((void) output);
96 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
97 #endif
100 int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
102 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
105 int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
107 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
110 static void * aes_ctx_alloc( void )
112 return malloc( sizeof( aes_context ) );
115 static void aes_ctx_free( void *ctx )
117 free( ctx );
120 const cipher_base_t aes_info = {
121 POLARSSL_CIPHER_ID_AES,
122 aes_crypt_cbc_wrap,
123 aes_crypt_cfb128_wrap,
124 aes_crypt_ctr_wrap,
125 aes_setkey_enc_wrap,
126 aes_setkey_dec_wrap,
127 aes_ctx_alloc,
128 aes_ctx_free
131 const cipher_info_t aes_128_cbc_info = {
132 POLARSSL_CIPHER_AES_128_CBC,
133 POLARSSL_MODE_CBC,
134 128,
135 "AES-128-CBC",
138 &aes_info
141 const cipher_info_t aes_192_cbc_info = {
142 POLARSSL_CIPHER_AES_192_CBC,
143 POLARSSL_MODE_CBC,
144 192,
145 "AES-192-CBC",
148 &aes_info
151 const cipher_info_t aes_256_cbc_info = {
152 POLARSSL_CIPHER_AES_256_CBC,
153 POLARSSL_MODE_CBC,
154 256,
155 "AES-256-CBC",
158 &aes_info
161 #if defined(POLARSSL_CIPHER_MODE_CFB)
162 const cipher_info_t aes_128_cfb128_info = {
163 POLARSSL_CIPHER_AES_128_CFB128,
164 POLARSSL_MODE_CFB,
165 128,
166 "AES-128-CFB128",
169 &aes_info
172 const cipher_info_t aes_192_cfb128_info = {
173 POLARSSL_CIPHER_AES_192_CFB128,
174 POLARSSL_MODE_CFB,
175 192,
176 "AES-192-CFB128",
179 &aes_info
182 const cipher_info_t aes_256_cfb128_info = {
183 POLARSSL_CIPHER_AES_256_CFB128,
184 POLARSSL_MODE_CFB,
185 256,
186 "AES-256-CFB128",
189 &aes_info
191 #endif /* POLARSSL_CIPHER_MODE_CFB */
193 #if defined(POLARSSL_CIPHER_MODE_CTR)
194 const cipher_info_t aes_128_ctr_info = {
195 POLARSSL_CIPHER_AES_128_CTR,
196 POLARSSL_MODE_CTR,
197 128,
198 "AES-128-CTR",
201 &aes_info
204 const cipher_info_t aes_192_ctr_info = {
205 POLARSSL_CIPHER_AES_192_CTR,
206 POLARSSL_MODE_CTR,
207 192,
208 "AES-192-CTR",
211 &aes_info
214 const cipher_info_t aes_256_ctr_info = {
215 POLARSSL_CIPHER_AES_256_CTR,
216 POLARSSL_MODE_CTR,
217 256,
218 "AES-256-CTR",
221 &aes_info
223 #endif /* POLARSSL_CIPHER_MODE_CTR */
225 #endif
227 #if defined(POLARSSL_CAMELLIA_C)
229 int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
230 unsigned char *iv, const unsigned char *input, unsigned char *output )
232 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
235 int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
236 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
238 #if defined(POLARSSL_CIPHER_MODE_CFB)
239 return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
240 #else
241 ((void) ctx);
242 ((void) operation);
243 ((void) length);
244 ((void) iv_off);
245 ((void) iv);
246 ((void) input);
247 ((void) output);
249 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
250 #endif
253 int camellia_crypt_ctr_wrap( void *ctx, size_t length,
254 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
255 const unsigned char *input, unsigned char *output )
257 #if defined(POLARSSL_CIPHER_MODE_CTR)
258 return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
259 stream_block, input, output );
260 #else
261 ((void) ctx);
262 ((void) length);
263 ((void) nc_off);
264 ((void) nonce_counter);
265 ((void) stream_block);
266 ((void) input);
267 ((void) output);
269 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
270 #endif
273 int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
275 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
278 int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
280 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
283 static void * camellia_ctx_alloc( void )
285 return malloc( sizeof( camellia_context ) );
288 static void camellia_ctx_free( void *ctx )
290 free( ctx );
293 const cipher_base_t camellia_info = {
294 POLARSSL_CIPHER_ID_CAMELLIA,
295 camellia_crypt_cbc_wrap,
296 camellia_crypt_cfb128_wrap,
297 camellia_crypt_ctr_wrap,
298 camellia_setkey_enc_wrap,
299 camellia_setkey_dec_wrap,
300 camellia_ctx_alloc,
301 camellia_ctx_free
304 const cipher_info_t camellia_128_cbc_info = {
305 POLARSSL_CIPHER_CAMELLIA_128_CBC,
306 POLARSSL_MODE_CBC,
307 128,
308 "CAMELLIA-128-CBC",
311 &camellia_info
314 const cipher_info_t camellia_192_cbc_info = {
315 POLARSSL_CIPHER_CAMELLIA_192_CBC,
316 POLARSSL_MODE_CBC,
317 192,
318 "CAMELLIA-192-CBC",
321 &camellia_info
324 const cipher_info_t camellia_256_cbc_info = {
325 POLARSSL_CIPHER_CAMELLIA_256_CBC,
326 POLARSSL_MODE_CBC,
327 256,
328 "CAMELLIA-256-CBC",
331 &camellia_info
334 #if defined(POLARSSL_CIPHER_MODE_CFB)
335 const cipher_info_t camellia_128_cfb128_info = {
336 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
337 POLARSSL_MODE_CFB,
338 128,
339 "CAMELLIA-128-CFB128",
342 &camellia_info
345 const cipher_info_t camellia_192_cfb128_info = {
346 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
347 POLARSSL_MODE_CFB,
348 192,
349 "CAMELLIA-192-CFB128",
352 &camellia_info
355 const cipher_info_t camellia_256_cfb128_info = {
356 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
357 POLARSSL_MODE_CFB,
358 256,
359 "CAMELLIA-256-CFB128",
362 &camellia_info
364 #endif /* POLARSSL_CIPHER_MODE_CFB */
366 #if defined(POLARSSL_CIPHER_MODE_CTR)
367 const cipher_info_t camellia_128_ctr_info = {
368 POLARSSL_CIPHER_CAMELLIA_128_CTR,
369 POLARSSL_MODE_CTR,
370 128,
371 "CAMELLIA-128-CTR",
374 &camellia_info
377 const cipher_info_t camellia_192_ctr_info = {
378 POLARSSL_CIPHER_CAMELLIA_192_CTR,
379 POLARSSL_MODE_CTR,
380 192,
381 "CAMELLIA-192-CTR",
384 &camellia_info
387 const cipher_info_t camellia_256_ctr_info = {
388 POLARSSL_CIPHER_CAMELLIA_256_CTR,
389 POLARSSL_MODE_CTR,
390 256,
391 "CAMELLIA-256-CTR",
394 &camellia_info
396 #endif /* POLARSSL_CIPHER_MODE_CTR */
398 #endif
400 #if defined(POLARSSL_DES_C)
402 int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
403 unsigned char *iv, const unsigned char *input, unsigned char *output )
405 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
408 int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
409 unsigned char *iv, const unsigned char *input, unsigned char *output )
411 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
414 int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
415 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
417 ((void) ctx);
418 ((void) operation);
419 ((void) length);
420 ((void) iv_off);
421 ((void) iv);
422 ((void) input);
423 ((void) output);
425 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
428 int des_crypt_ctr_wrap( void *ctx, size_t length,
429 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
430 const unsigned char *input, unsigned char *output )
432 ((void) ctx);
433 ((void) length);
434 ((void) nc_off);
435 ((void) nonce_counter);
436 ((void) stream_block);
437 ((void) input);
438 ((void) output);
440 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
444 int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
446 ((void) key_length);
448 return des_setkey_dec( (des_context *) ctx, key );
451 int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
453 ((void) key_length);
455 return des_setkey_enc( (des_context *) ctx, key );
458 int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
460 ((void) key_length);
462 return des3_set2key_dec( (des3_context *) ctx, key );
465 int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
467 ((void) key_length);
469 return des3_set2key_enc( (des3_context *) ctx, key );
472 int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
474 ((void) key_length);
476 return des3_set3key_dec( (des3_context *) ctx, key );
479 int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
481 ((void) key_length);
483 return des3_set3key_enc( (des3_context *) ctx, key );
486 static void * des_ctx_alloc( void )
488 return malloc( sizeof( des_context ) );
491 static void * des3_ctx_alloc( void )
493 return malloc( sizeof( des3_context ) );
496 static void des_ctx_free( void *ctx )
498 free( ctx );
501 const cipher_base_t des_info = {
502 POLARSSL_CIPHER_ID_DES,
503 des_crypt_cbc_wrap,
504 des_crypt_cfb128_wrap,
505 des_crypt_ctr_wrap,
506 des_setkey_enc_wrap,
507 des_setkey_dec_wrap,
508 des_ctx_alloc,
509 des_ctx_free
512 const cipher_info_t des_cbc_info = {
513 POLARSSL_CIPHER_DES_CBC,
514 POLARSSL_MODE_CBC,
515 POLARSSL_KEY_LENGTH_DES,
516 "DES-CBC",
519 &des_info
522 const cipher_base_t des_ede_info = {
523 POLARSSL_CIPHER_ID_DES,
524 des3_crypt_cbc_wrap,
525 des_crypt_cfb128_wrap,
526 des_crypt_ctr_wrap,
527 des3_set2key_enc_wrap,
528 des3_set2key_dec_wrap,
529 des3_ctx_alloc,
530 des_ctx_free
533 const cipher_info_t des_ede_cbc_info = {
534 POLARSSL_CIPHER_DES_EDE_CBC,
535 POLARSSL_MODE_CBC,
536 POLARSSL_KEY_LENGTH_DES_EDE,
537 "DES-EDE-CBC",
540 &des_ede_info
543 const cipher_base_t des_ede3_info = {
544 POLARSSL_CIPHER_ID_DES,
545 des3_crypt_cbc_wrap,
546 des_crypt_cfb128_wrap,
547 des_crypt_ctr_wrap,
548 des3_set3key_enc_wrap,
549 des3_set3key_dec_wrap,
550 des3_ctx_alloc,
551 des_ctx_free
554 const cipher_info_t des_ede3_cbc_info = {
555 POLARSSL_CIPHER_DES_EDE3_CBC,
556 POLARSSL_MODE_CBC,
557 POLARSSL_KEY_LENGTH_DES_EDE3,
558 "DES-EDE3-CBC",
561 &des_ede3_info
563 #endif
565 #if defined(POLARSSL_BLOWFISH_C)
567 int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
568 unsigned char *iv, const unsigned char *input, unsigned char *output )
570 return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output );
573 int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length,
574 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
576 #if defined(POLARSSL_CIPHER_MODE_CFB)
577 return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output );
578 #else
579 ((void) ctx);
580 ((void) operation);
581 ((void) length);
582 ((void) iv_off);
583 ((void) iv);
584 ((void) input);
585 ((void) output);
587 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
588 #endif
591 int blowfish_crypt_ctr_wrap( void *ctx, size_t length,
592 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
593 const unsigned char *input, unsigned char *output )
595 #if defined(POLARSSL_CIPHER_MODE_CTR)
596 return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter,
597 stream_block, input, output );
598 #else
599 ((void) ctx);
600 ((void) length);
601 ((void) nc_off);
602 ((void) nonce_counter);
603 ((void) stream_block);
604 ((void) input);
605 ((void) output);
607 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
608 #endif
611 int blowfish_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
613 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
616 int blowfish_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
618 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
621 static void * blowfish_ctx_alloc( void )
623 return malloc( sizeof( blowfish_context ) );
626 static void blowfish_ctx_free( void *ctx )
628 free( ctx );
631 const cipher_base_t blowfish_info = {
632 POLARSSL_CIPHER_ID_BLOWFISH,
633 blowfish_crypt_cbc_wrap,
634 blowfish_crypt_cfb64_wrap,
635 blowfish_crypt_ctr_wrap,
636 blowfish_setkey_enc_wrap,
637 blowfish_setkey_dec_wrap,
638 blowfish_ctx_alloc,
639 blowfish_ctx_free
642 const cipher_info_t blowfish_cbc_info = {
643 POLARSSL_CIPHER_BLOWFISH_CBC,
644 POLARSSL_MODE_CBC,
645 128,
646 "BLOWFISH-CBC",
649 &blowfish_info
652 #if defined(POLARSSL_CIPHER_MODE_CFB)
653 const cipher_info_t blowfish_cfb64_info = {
654 POLARSSL_CIPHER_BLOWFISH_CFB64,
655 POLARSSL_MODE_CFB,
656 128,
657 "BLOWFISH-CFB64",
660 &blowfish_info
662 #endif /* POLARSSL_CIPHER_MODE_CFB */
664 #if defined(POLARSSL_CIPHER_MODE_CTR)
665 const cipher_info_t blowfish_ctr_info = {
666 POLARSSL_CIPHER_BLOWFISH_CTR,
667 POLARSSL_MODE_CTR,
668 128,
669 "BLOWFISH-CTR",
672 &blowfish_info
674 #endif /* POLARSSL_CIPHER_MODE_CTR */
675 #endif /* POLARSSL_BLOWFISH_C */
677 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
678 static void * null_ctx_alloc( void )
680 return (void *) 1;
684 static void null_ctx_free( void *ctx )
686 ((void) ctx);
689 const cipher_base_t null_base_info = {
690 POLARSSL_CIPHER_ID_NULL,
691 NULL,
692 NULL,
693 NULL,
694 NULL,
695 NULL,
696 null_ctx_alloc,
697 null_ctx_free
700 const cipher_info_t null_cipher_info = {
701 POLARSSL_CIPHER_NULL,
702 POLARSSL_MODE_NULL,
704 "NULL",
707 &null_base_info
709 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
711 #endif