PolarSSL v1.2.11
cipher_wrap.c
Go to the documentation of this file.
1 
30 #include "polarssl/config.h"
31 
32 #if defined(POLARSSL_CIPHER_C)
33 
34 #include "polarssl/cipher_wrap.h"
35 
36 #if defined(POLARSSL_AES_C)
37 #include "polarssl/aes.h"
38 #endif
39 
40 #if defined(POLARSSL_CAMELLIA_C)
41 #include "polarssl/camellia.h"
42 #endif
43 
44 #if defined(POLARSSL_DES_C)
45 #include "polarssl/des.h"
46 #endif
47 
48 #if defined(POLARSSL_BLOWFISH_C)
49 #include "polarssl/blowfish.h"
50 #endif
51 
52 #include <stdlib.h>
53 
54 /* Implementation that should never be optimized out by the compiler */
55 static void polarssl_zeroize( void *v, size_t n ) {
56  volatile unsigned char *p = v; while( n-- ) *p++ = 0;
57 }
58 
59 #if defined(POLARSSL_AES_C)
60 
61 static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
62  unsigned char *iv, const unsigned char *input, unsigned char *output )
63 {
64  return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
65 }
66 
67 static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
68  size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
69 {
70 #if defined(POLARSSL_CIPHER_MODE_CFB)
71  return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
72 #else
73  ((void) ctx);
74  ((void) operation);
75  ((void) length);
76  ((void) iv_off);
77  ((void) iv);
78  ((void) input);
79  ((void) output);
80 
82 #endif
83 }
84 
85 static int aes_crypt_ctr_wrap( void *ctx, size_t length,
86  size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
87  const unsigned char *input, unsigned char *output )
88 {
89 #if defined(POLARSSL_CIPHER_MODE_CTR)
90  return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
91  stream_block, input, output );
92 #else
93  ((void) ctx);
94  ((void) length);
95  ((void) nc_off);
96  ((void) nonce_counter);
97  ((void) stream_block);
98  ((void) input);
99  ((void) output);
100 
102 #endif
103 }
104 
105 static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
106 {
107  return aes_setkey_dec( (aes_context *) ctx, key, key_length );
108 }
109 
110 static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
111 {
112  return aes_setkey_enc( (aes_context *) ctx, key, key_length );
113 }
114 
115 static void * aes_ctx_alloc( void )
116 {
117  return malloc( sizeof( aes_context ) );
118 }
119 
120 static void aes_ctx_free( void *ctx )
121 {
122  polarssl_zeroize( ctx, sizeof( aes_context ) );
123  free( ctx );
124 }
125 
126 const cipher_base_t aes_info = {
128  aes_crypt_cbc_wrap,
129  aes_crypt_cfb128_wrap,
130  aes_crypt_ctr_wrap,
131  aes_setkey_enc_wrap,
132  aes_setkey_dec_wrap,
133  aes_ctx_alloc,
134  aes_ctx_free
135 };
136 
140  128,
141  "AES-128-CBC",
142  16,
143  16,
144  &aes_info
145 };
146 
150  192,
151  "AES-192-CBC",
152  16,
153  16,
154  &aes_info
155 };
156 
160  256,
161  "AES-256-CBC",
162  16,
163  16,
164  &aes_info
165 };
166 
167 #if defined(POLARSSL_CIPHER_MODE_CFB)
171  128,
172  "AES-128-CFB128",
173  16,
174  16,
175  &aes_info
176 };
177 
181  192,
182  "AES-192-CFB128",
183  16,
184  16,
185  &aes_info
186 };
187 
191  256,
192  "AES-256-CFB128",
193  16,
194  16,
195  &aes_info
196 };
197 #endif /* POLARSSL_CIPHER_MODE_CFB */
198 
199 #if defined(POLARSSL_CIPHER_MODE_CTR)
203  128,
204  "AES-128-CTR",
205  16,
206  16,
207  &aes_info
208 };
209 
213  192,
214  "AES-192-CTR",
215  16,
216  16,
217  &aes_info
218 };
219 
223  256,
224  "AES-256-CTR",
225  16,
226  16,
227  &aes_info
228 };
229 #endif /* POLARSSL_CIPHER_MODE_CTR */
230 
231 #endif
232 
233 #if defined(POLARSSL_CAMELLIA_C)
234 
235 static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
236  unsigned char *iv, const unsigned char *input, unsigned char *output )
237 {
238  return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
239 }
240 
241 static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
242  size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
243 {
244 #if defined(POLARSSL_CIPHER_MODE_CFB)
245  return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
246 #else
247  ((void) ctx);
248  ((void) operation);
249  ((void) length);
250  ((void) iv_off);
251  ((void) iv);
252  ((void) input);
253  ((void) output);
254 
256 #endif
257 }
258 
259 static int camellia_crypt_ctr_wrap( void *ctx, size_t length,
260  size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
261  const unsigned char *input, unsigned char *output )
262 {
263 #if defined(POLARSSL_CIPHER_MODE_CTR)
264  return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
265  stream_block, input, output );
266 #else
267  ((void) ctx);
268  ((void) length);
269  ((void) nc_off);
270  ((void) nonce_counter);
271  ((void) stream_block);
272  ((void) input);
273  ((void) output);
274 
276 #endif
277 }
278 
279 static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
280 {
281  return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
282 }
283 
284 static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
285 {
286  return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
287 }
288 
289 static void * camellia_ctx_alloc( void )
290 {
291  return malloc( sizeof( camellia_context ) );
292 }
293 
294 static void camellia_ctx_free( void *ctx )
295 {
296  polarssl_zeroize( ctx, sizeof( camellia_context ) );
297  free( ctx );
298 }
299 
300 const cipher_base_t camellia_info = {
302  camellia_crypt_cbc_wrap,
303  camellia_crypt_cfb128_wrap,
304  camellia_crypt_ctr_wrap,
305  camellia_setkey_enc_wrap,
306  camellia_setkey_dec_wrap,
307  camellia_ctx_alloc,
308  camellia_ctx_free
309 };
310 
314  128,
315  "CAMELLIA-128-CBC",
316  16,
317  16,
318  &camellia_info
319 };
320 
324  192,
325  "CAMELLIA-192-CBC",
326  16,
327  16,
328  &camellia_info
329 };
330 
334  256,
335  "CAMELLIA-256-CBC",
336  16,
337  16,
338  &camellia_info
339 };
340 
341 #if defined(POLARSSL_CIPHER_MODE_CFB)
345  128,
346  "CAMELLIA-128-CFB128",
347  16,
348  16,
349  &camellia_info
350 };
351 
355  192,
356  "CAMELLIA-192-CFB128",
357  16,
358  16,
359  &camellia_info
360 };
361 
365  256,
366  "CAMELLIA-256-CFB128",
367  16,
368  16,
369  &camellia_info
370 };
371 #endif /* POLARSSL_CIPHER_MODE_CFB */
372 
373 #if defined(POLARSSL_CIPHER_MODE_CTR)
377  128,
378  "CAMELLIA-128-CTR",
379  16,
380  16,
381  &camellia_info
382 };
383 
387  192,
388  "CAMELLIA-192-CTR",
389  16,
390  16,
391  &camellia_info
392 };
393 
397  256,
398  "CAMELLIA-256-CTR",
399  16,
400  16,
401  &camellia_info
402 };
403 #endif /* POLARSSL_CIPHER_MODE_CTR */
404 
405 #endif
406 
407 #if defined(POLARSSL_DES_C)
408 
409 static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
410  unsigned char *iv, const unsigned char *input, unsigned char *output )
411 {
412  return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
413 }
414 
415 static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
416  unsigned char *iv, const unsigned char *input, unsigned char *output )
417 {
418  return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
419 }
420 
421 static int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
422  size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
423 {
424  ((void) ctx);
425  ((void) operation);
426  ((void) length);
427  ((void) iv_off);
428  ((void) iv);
429  ((void) input);
430  ((void) output);
431 
433 }
434 
435 static int des_crypt_ctr_wrap( void *ctx, size_t length,
436  size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
437  const unsigned char *input, unsigned char *output )
438 {
439  ((void) ctx);
440  ((void) length);
441  ((void) nc_off);
442  ((void) nonce_counter);
443  ((void) stream_block);
444  ((void) input);
445  ((void) output);
446 
448 }
449 
450 
451 static int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
452 {
453  ((void) key_length);
454 
455  return des_setkey_dec( (des_context *) ctx, key );
456 }
457 
458 static int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
459 {
460  ((void) key_length);
461 
462  return des_setkey_enc( (des_context *) ctx, key );
463 }
464 
465 static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
466 {
467  ((void) key_length);
468 
469  return des3_set2key_dec( (des3_context *) ctx, key );
470 }
471 
472 static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
473 {
474  ((void) key_length);
475 
476  return des3_set2key_enc( (des3_context *) ctx, key );
477 }
478 
479 static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
480 {
481  ((void) key_length);
482 
483  return des3_set3key_dec( (des3_context *) ctx, key );
484 }
485 
486 static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
487 {
488  ((void) key_length);
489 
490  return des3_set3key_enc( (des3_context *) ctx, key );
491 }
492 
493 static void * des_ctx_alloc( void )
494 {
495  return malloc( sizeof( des_context ) );
496 }
497 
498 static void * des3_ctx_alloc( void )
499 {
500  return malloc( sizeof( des3_context ) );
501 }
502 
503 static void des_ctx_free( void *ctx )
504 {
505  polarssl_zeroize( ctx, sizeof( des_context ) );
506  free( ctx );
507 }
508 
509 static void des3_ctx_free( void *ctx )
510 {
511  polarssl_zeroize( ctx, sizeof( des3_context ) );
512  free( ctx );
513 }
514 
515 const cipher_base_t des_info = {
517  des_crypt_cbc_wrap,
518  des_crypt_cfb128_wrap,
519  des_crypt_ctr_wrap,
520  des_setkey_enc_wrap,
521  des_setkey_dec_wrap,
522  des_ctx_alloc,
523  des_ctx_free
524 };
525 
526 const cipher_info_t des_cbc_info = {
530  "DES-CBC",
531  8,
532  8,
533  &des_info
534 };
535 
536 const cipher_base_t des_ede_info = {
538  des3_crypt_cbc_wrap,
539  des_crypt_cfb128_wrap,
540  des_crypt_ctr_wrap,
541  des3_set2key_enc_wrap,
542  des3_set2key_dec_wrap,
543  des3_ctx_alloc,
544  des3_ctx_free
545 };
546 
551  "DES-EDE-CBC",
552  8,
553  8,
554  &des_ede_info
555 };
556 
557 const cipher_base_t des_ede3_info = {
559  des3_crypt_cbc_wrap,
560  des_crypt_cfb128_wrap,
561  des_crypt_ctr_wrap,
562  des3_set3key_enc_wrap,
563  des3_set3key_dec_wrap,
564  des3_ctx_alloc,
565  des3_ctx_free
566 };
567 
572  "DES-EDE3-CBC",
573  8,
574  8,
575  &des_ede3_info
576 };
577 #endif
578 
579 #if defined(POLARSSL_BLOWFISH_C)
580 
581 static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
582  unsigned char *iv, const unsigned char *input, unsigned char *output )
583 {
584  return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output );
585 }
586 
587 static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length,
588  size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
589 {
590 #if defined(POLARSSL_CIPHER_MODE_CFB)
591  return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output );
592 #else
593  ((void) ctx);
594  ((void) operation);
595  ((void) length);
596  ((void) iv_off);
597  ((void) iv);
598  ((void) input);
599  ((void) output);
600 
602 #endif
603 }
604 
605 static int blowfish_crypt_ctr_wrap( void *ctx, size_t length,
606  size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
607  const unsigned char *input, unsigned char *output )
608 {
609 #if defined(POLARSSL_CIPHER_MODE_CTR)
610  return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter,
611  stream_block, input, output );
612 #else
613  ((void) ctx);
614  ((void) length);
615  ((void) nc_off);
616  ((void) nonce_counter);
617  ((void) stream_block);
618  ((void) input);
619  ((void) output);
620 
622 #endif
623 }
624 
625 static int blowfish_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
626 {
627  return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
628 }
629 
630 static int blowfish_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
631 {
632  return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
633 }
634 
635 static void * blowfish_ctx_alloc( void )
636 {
637  return malloc( sizeof( blowfish_context ) );
638 }
639 
640 static void blowfish_ctx_free( void *ctx )
641 {
642  polarssl_zeroize( ctx, sizeof( blowfish_context ) );
643  free( ctx );
644 }
645 
646 const cipher_base_t blowfish_info = {
648  blowfish_crypt_cbc_wrap,
649  blowfish_crypt_cfb64_wrap,
650  blowfish_crypt_ctr_wrap,
651  blowfish_setkey_enc_wrap,
652  blowfish_setkey_dec_wrap,
653  blowfish_ctx_alloc,
654  blowfish_ctx_free
655 };
656 
660  128,
661  "BLOWFISH-CBC",
662  8,
663  8,
664  &blowfish_info
665 };
666 
667 #if defined(POLARSSL_CIPHER_MODE_CFB)
671  128,
672  "BLOWFISH-CFB64",
673  8,
674  8,
675  &blowfish_info
676 };
677 #endif /* POLARSSL_CIPHER_MODE_CFB */
678 
679 #if defined(POLARSSL_CIPHER_MODE_CTR)
683  128,
684  "BLOWFISH-CTR",
685  8,
686  8,
687  &blowfish_info
688 };
689 #endif /* POLARSSL_CIPHER_MODE_CTR */
690 #endif /* POLARSSL_BLOWFISH_C */
691 
692 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
693 static void * null_ctx_alloc( void )
694 {
695  return (void *) 1;
696 }
697 
698 
699 static void null_ctx_free( void *ctx )
700 {
701  ((void) ctx);
702 }
703 
704 const cipher_base_t null_base_info = {
706  NULL,
707  NULL,
708  NULL,
709  NULL,
710  NULL,
711  null_ctx_alloc,
712  null_ctx_free
713 };
714 
715 const cipher_info_t null_cipher_info = {
718  0,
719  "NULL",
720  1,
721  1,
722  &null_base_info
723 };
724 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
725 
726 #endif
const cipher_info_t blowfish_ctr_info
int blowfish_setkey(blowfish_context *ctx, const unsigned char *key, unsigned int keysize)
Blowfish key schedule.
const cipher_info_t blowfish_cbc_info
#define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
The selected feature is not available.
Definition: cipher.h:43
const cipher_info_t camellia_192_ctr_info
const cipher_info_t aes_128_ctr_info
const cipher_info_t camellia_192_cbc_info
Key length, in bits (including parity), for DES keys.
Definition: cipher.h:107
const cipher_info_t aes_256_ctr_info
int camellia_crypt_cbc(camellia_context *ctx, int mode, size_t length, unsigned char iv[16], const unsigned char *input, unsigned char *output)
CAMELLIA-CBC buffer encryption/decryption Length should be a multiple of the block size (16 bytes) ...
Cipher information.
Definition: cipher.h:153
int aes_crypt_cfb128(aes_context *ctx, int mode, size_t length, size_t *iv_off, unsigned char iv[16], const unsigned char *input, unsigned char *output)
AES-CFB128 buffer encryption/decryption.
const cipher_info_t aes_128_cfb128_info
const cipher_info_t des_cbc_info
AES context structure.
Definition: aes.h:54
Configuration options (set of defines)
const cipher_info_t aes_256_cbc_info
int aes_setkey_dec(aes_context *ctx, const unsigned char *key, unsigned int keysize)
AES key schedule (decryption)
Camellia block cipher.
const cipher_info_t camellia_256_ctr_info
int camellia_setkey_enc(camellia_context *ctx, const unsigned char *key, unsigned int keysize)
CAMELLIA key schedule (encryption)
int camellia_crypt_ctr(camellia_context *ctx, size_t length, size_t *nc_off, unsigned char nonce_counter[16], unsigned char stream_block[16], const unsigned char *input, unsigned char *output)
CAMELLIA-CTR buffer encryption/decryption.
const cipher_info_t aes_192_ctr_info
DES context structure.
Definition: des.h:55
const cipher_info_t camellia_128_ctr_info
const cipher_info_t blowfish_cfb64_info
const cipher_info_t des_ede3_cbc_info
int camellia_crypt_cfb128(camellia_context *ctx, int mode, size_t length, size_t *iv_off, unsigned char iv[16], const unsigned char *input, unsigned char *output)
CAMELLIA-CFB128 buffer encryption/decryption.
Cipher wrappers.
int des3_set3key_enc(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *3])
Triple-DES key schedule (168-bit, encryption)
int des3_set3key_dec(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *3])
Triple-DES key schedule (168-bit, decryption)
Blowfish block cipher.
const cipher_info_t aes_128_cbc_info
const cipher_info_t des_ede_cbc_info
Triple-DES context structure.
Definition: des.h:65
int des3_set2key_enc(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *2])
Triple-DES key schedule (112-bit, encryption)
int aes_crypt_cbc(aes_context *ctx, int mode, size_t length, unsigned char iv[16], const unsigned char *input, unsigned char *output)
AES-CBC buffer encryption/decryption Length should be a multiple of the block size (16 bytes) ...
operation_t
Definition: cipher.h:97
AES block cipher.
Key length, in bits (including parity), for DES in three-key EDE.
Definition: cipher.h:111
const cipher_info_t camellia_256_cfb128_info
const cipher_info_t camellia_256_cbc_info
CAMELLIA context structure.
Definition: camellia.h:54
const cipher_info_t camellia_128_cbc_info
const cipher_info_t aes_192_cbc_info
int des_crypt_cbc(des_context *ctx, int mode, size_t length, unsigned char iv[8], const unsigned char *input, unsigned char *output)
DES-CBC buffer encryption/decryption.
DES block cipher.
int camellia_setkey_dec(camellia_context *ctx, const unsigned char *key, unsigned int keysize)
CAMELLIA key schedule (decryption)
int des_setkey_enc(des_context *ctx, const unsigned char key[DES_KEY_SIZE])
DES key schedule (56-bit, encryption)
const cipher_info_t aes_256_cfb128_info
int des_setkey_dec(des_context *ctx, const unsigned char key[DES_KEY_SIZE])
DES key schedule (56-bit, decryption)
Base cipher information.
Definition: cipher.h:119
const cipher_info_t aes_192_cfb128_info
int blowfish_crypt_ctr(blowfish_context *ctx, size_t length, size_t *nc_off, unsigned char nonce_counter[BLOWFISH_BLOCKSIZE], unsigned char stream_block[BLOWFISH_BLOCKSIZE], const unsigned char *input, unsigned char *output)
Blowfish-CTR buffer encryption/decryption.
int blowfish_crypt_cfb64(blowfish_context *ctx, int mode, size_t length, size_t *iv_off, unsigned char iv[BLOWFISH_BLOCKSIZE], const unsigned char *input, unsigned char *output)
Blowfish CFB buffer encryption/decryption.
Blowfish context structure.
Definition: blowfish.h:58
const cipher_info_t camellia_192_cfb128_info
int aes_setkey_enc(aes_context *ctx, const unsigned char *key, unsigned int keysize)
AES key schedule (encryption)
Key length, in bits (including parity), for DES in two key EDE.
Definition: cipher.h:109
int aes_crypt_ctr(aes_context *ctx, size_t length, size_t *nc_off, unsigned char nonce_counter[16], unsigned char stream_block[16], const unsigned char *input, unsigned char *output)
AES-CTR buffer encryption/decryption.
int blowfish_crypt_cbc(blowfish_context *ctx, int mode, size_t length, unsigned char iv[BLOWFISH_BLOCKSIZE], const unsigned char *input, unsigned char *output)
Blowfish-CBC buffer encryption/decryption Length should be a multiple of the block size (8 bytes) ...
const cipher_info_t camellia_128_cfb128_info
int des3_set2key_dec(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *2])
Triple-DES key schedule (112-bit, decryption)
int des3_crypt_cbc(des3_context *ctx, int mode, size_t length, unsigned char iv[8], const unsigned char *input, unsigned char *output)
3DES-CBC buffer encryption/decryption