PolarSSL v1.3.7
cipher_wrap.c
Go to the documentation of this file.
1 
30 #if !defined(POLARSSL_CONFIG_FILE)
31 #include "polarssl/config.h"
32 #else
33 #include POLARSSL_CONFIG_FILE
34 #endif
35 
36 #if defined(POLARSSL_CIPHER_C)
37 
38 #include "polarssl/cipher_wrap.h"
39 
40 #if defined(POLARSSL_AES_C)
41 #include "polarssl/aes.h"
42 #endif
43 
44 #if defined(POLARSSL_ARC4_C)
45 #include "polarssl/arc4.h"
46 #endif
47 
48 #if defined(POLARSSL_CAMELLIA_C)
49 #include "polarssl/camellia.h"
50 #endif
51 
52 #if defined(POLARSSL_DES_C)
53 #include "polarssl/des.h"
54 #endif
55 
56 #if defined(POLARSSL_BLOWFISH_C)
57 #include "polarssl/blowfish.h"
58 #endif
59 
60 #if defined(POLARSSL_GCM_C)
61 #include "polarssl/gcm.h"
62 #endif
63 
64 #if defined(POLARSSL_PLATFORM_C)
65 #include "polarssl/platform.h"
66 #else
67 #define polarssl_malloc malloc
68 #define polarssl_free free
69 #endif
70 
71 #include <stdlib.h>
72 
73 #if defined(POLARSSL_GCM_C)
74 /* shared by all GCM ciphers */
75 static void *gcm_ctx_alloc( void )
76 {
77  return polarssl_malloc( sizeof( gcm_context ) );
78 }
79 
80 static void gcm_ctx_free( void *ctx )
81 {
82  gcm_free( ctx );
83  polarssl_free( ctx );
84 }
85 #endif /* POLARSSL_GCM_C */
86 
87 #if defined(POLARSSL_AES_C)
88 
89 static int aes_crypt_ecb_wrap( void *ctx, operation_t operation,
90  const unsigned char *input, unsigned char *output )
91 {
92  return aes_crypt_ecb( (aes_context *) ctx, operation, input, output );
93 }
94 
95 static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
96  unsigned char *iv, const unsigned char *input, unsigned char *output )
97 {
98 #if defined(POLARSSL_CIPHER_MODE_CBC)
99  return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input,
100  output );
101 #else
102  ((void) ctx);
103  ((void) operation);
104  ((void) length);
105  ((void) iv);
106  ((void) input);
107  ((void) output);
108 
110 #endif /* POLARSSL_CIPHER_MODE_CBC */
111 }
112 
113 static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation,
114  size_t length, size_t *iv_off, unsigned char *iv,
115  const unsigned char *input, unsigned char *output )
116 {
117 #if defined(POLARSSL_CIPHER_MODE_CFB)
118  return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv,
119  input, output );
120 #else
121  ((void) ctx);
122  ((void) operation);
123  ((void) length);
124  ((void) iv_off);
125  ((void) iv);
126  ((void) input);
127  ((void) output);
128 
130 #endif /* POLARSSL_CIPHER_MODE_CFB */
131 }
132 
133 static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
134  unsigned char *nonce_counter, unsigned char *stream_block,
135  const unsigned char *input, unsigned char *output )
136 {
137 #if defined(POLARSSL_CIPHER_MODE_CTR)
138  return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
139  stream_block, input, output );
140 #else
141  ((void) ctx);
142  ((void) length);
143  ((void) nc_off);
144  ((void) nonce_counter);
145  ((void) stream_block);
146  ((void) input);
147  ((void) output);
148 
150 #endif /* POLARSSL_CIPHER_MODE_CTR */
151 }
152 
153 static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
154  unsigned int key_length )
155 {
156  return aes_setkey_dec( (aes_context *) ctx, key, key_length );
157 }
158 
159 static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
160  unsigned int key_length )
161 {
162  return aes_setkey_enc( (aes_context *) ctx, key, key_length );
163 }
164 
165 static void * aes_ctx_alloc( void )
166 {
167  return polarssl_malloc( sizeof( aes_context ) );
168 }
169 
170 static void aes_ctx_free( void *ctx )
171 {
172  polarssl_free( ctx );
173 }
174 
175 const cipher_base_t aes_info = {
177  aes_crypt_ecb_wrap,
178  aes_crypt_cbc_wrap,
179  aes_crypt_cfb128_wrap,
180  aes_crypt_ctr_wrap,
181  NULL,
182  aes_setkey_enc_wrap,
183  aes_setkey_dec_wrap,
184  aes_ctx_alloc,
185  aes_ctx_free
186 };
187 
188 const cipher_info_t aes_128_ecb_info = {
191  128,
192  "AES-128-ECB",
193  16,
194  0,
195  16,
196  &aes_info
197 };
198 
199 const cipher_info_t aes_192_ecb_info = {
202  192,
203  "AES-192-ECB",
204  16,
205  0,
206  16,
207  &aes_info
208 };
209 
210 const cipher_info_t aes_256_ecb_info = {
213  256,
214  "AES-256-ECB",
215  16,
216  0,
217  16,
218  &aes_info
219 };
220 
221 #if defined(POLARSSL_CIPHER_MODE_CBC)
222 const cipher_info_t aes_128_cbc_info = {
225  128,
226  "AES-128-CBC",
227  16,
228  0,
229  16,
230  &aes_info
231 };
232 
233 const cipher_info_t aes_192_cbc_info = {
236  192,
237  "AES-192-CBC",
238  16,
239  0,
240  16,
241  &aes_info
242 };
243 
244 const cipher_info_t aes_256_cbc_info = {
247  256,
248  "AES-256-CBC",
249  16,
250  0,
251  16,
252  &aes_info
253 };
254 #endif /* POLARSSL_CIPHER_MODE_CBC */
255 
256 #if defined(POLARSSL_CIPHER_MODE_CFB)
257 const cipher_info_t aes_128_cfb128_info = {
260  128,
261  "AES-128-CFB128",
262  16,
263  0,
264  16,
265  &aes_info
266 };
267 
268 const cipher_info_t aes_192_cfb128_info = {
271  192,
272  "AES-192-CFB128",
273  16,
274  0,
275  16,
276  &aes_info
277 };
278 
279 const cipher_info_t aes_256_cfb128_info = {
282  256,
283  "AES-256-CFB128",
284  16,
285  0,
286  16,
287  &aes_info
288 };
289 #endif /* POLARSSL_CIPHER_MODE_CFB */
290 
291 #if defined(POLARSSL_CIPHER_MODE_CTR)
292 const cipher_info_t aes_128_ctr_info = {
295  128,
296  "AES-128-CTR",
297  16,
298  0,
299  16,
300  &aes_info
301 };
302 
303 const cipher_info_t aes_192_ctr_info = {
306  192,
307  "AES-192-CTR",
308  16,
309  0,
310  16,
311  &aes_info
312 };
313 
314 const cipher_info_t aes_256_ctr_info = {
317  256,
318  "AES-256-CTR",
319  16,
320  0,
321  16,
322  &aes_info
323 };
324 #endif /* POLARSSL_CIPHER_MODE_CTR */
325 
326 #if defined(POLARSSL_GCM_C)
327 static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
328  unsigned int key_length )
329 {
331  key, key_length );
332 }
333 
334 const cipher_base_t gcm_aes_info = {
336  NULL,
337  NULL,
338  NULL,
339  NULL,
340  NULL,
341  gcm_aes_setkey_wrap,
342  gcm_aes_setkey_wrap,
343  gcm_ctx_alloc,
344  gcm_ctx_free,
345 };
346 
347 const cipher_info_t aes_128_gcm_info = {
350  128,
351  "AES-128-GCM",
352  12,
353  1,
354  16,
355  &gcm_aes_info
356 };
357 
358 const cipher_info_t aes_192_gcm_info = {
361  192,
362  "AES-192-GCM",
363  12,
364  1,
365  16,
366  &gcm_aes_info
367 };
368 
369 const cipher_info_t aes_256_gcm_info = {
372  256,
373  "AES-256-GCM",
374  12,
375  1,
376  16,
377  &gcm_aes_info
378 };
379 #endif /* POLARSSL_GCM_C */
380 
381 #endif /* POLARSSL_AES_C */
382 
383 #if defined(POLARSSL_CAMELLIA_C)
384 
385 static int camellia_crypt_ecb_wrap( void *ctx, operation_t operation,
386  const unsigned char *input, unsigned char *output )
387 {
388  return camellia_crypt_ecb( (camellia_context *) ctx, operation, input,
389  output );
390 }
391 
392 static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation,
393  size_t length, unsigned char *iv,
394  const unsigned char *input, unsigned char *output )
395 {
396 #if defined(POLARSSL_CIPHER_MODE_CBC)
397  return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv,
398  input, output );
399 #else
400  ((void) ctx);
401  ((void) operation);
402  ((void) length);
403  ((void) iv);
404  ((void) input);
405  ((void) output);
406 
408 #endif /* POLARSSL_CIPHER_MODE_CBC */
409 }
410 
411 static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation,
412  size_t length, size_t *iv_off, unsigned char *iv,
413  const unsigned char *input, unsigned char *output )
414 {
415 #if defined(POLARSSL_CIPHER_MODE_CFB)
416  return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length,
417  iv_off, iv, input, output );
418 #else
419  ((void) ctx);
420  ((void) operation);
421  ((void) length);
422  ((void) iv_off);
423  ((void) iv);
424  ((void) input);
425  ((void) output);
426 
428 #endif /* POLARSSL_CIPHER_MODE_CFB */
429 }
430 
431 static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
432  unsigned char *nonce_counter, unsigned char *stream_block,
433  const unsigned char *input, unsigned char *output )
434 {
435 #if defined(POLARSSL_CIPHER_MODE_CTR)
436  return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off,
437  nonce_counter, stream_block, input, output );
438 #else
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 #endif /* POLARSSL_CIPHER_MODE_CTR */
449 }
450 
451 static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
452  unsigned int key_length )
453 {
454  return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
455 }
456 
457 static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
458  unsigned int key_length )
459 {
460  return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
461 }
462 
463 static void * camellia_ctx_alloc( void )
464 {
465  return polarssl_malloc( sizeof( camellia_context ) );
466 }
467 
468 static void camellia_ctx_free( void *ctx )
469 {
470  polarssl_free( ctx );
471 }
472 
473 const cipher_base_t camellia_info = {
475  camellia_crypt_ecb_wrap,
476  camellia_crypt_cbc_wrap,
477  camellia_crypt_cfb128_wrap,
478  camellia_crypt_ctr_wrap,
479  NULL,
480  camellia_setkey_enc_wrap,
481  camellia_setkey_dec_wrap,
482  camellia_ctx_alloc,
483  camellia_ctx_free
484 };
485 
486 const cipher_info_t camellia_128_ecb_info = {
489  128,
490  "CAMELLIA-128-ECB",
491  16,
492  0,
493  16,
494  &camellia_info
495 };
496 
497 const cipher_info_t camellia_192_ecb_info = {
500  192,
501  "CAMELLIA-192-ECB",
502  16,
503  0,
504  16,
505  &camellia_info
506 };
507 
508 const cipher_info_t camellia_256_ecb_info = {
511  256,
512  "CAMELLIA-256-ECB",
513  16,
514  0,
515  16,
516  &camellia_info
517 };
518 
519 #if defined(POLARSSL_CIPHER_MODE_CBC)
520 const cipher_info_t camellia_128_cbc_info = {
523  128,
524  "CAMELLIA-128-CBC",
525  16,
526  0,
527  16,
528  &camellia_info
529 };
530 
531 const cipher_info_t camellia_192_cbc_info = {
534  192,
535  "CAMELLIA-192-CBC",
536  16,
537  0,
538  16,
539  &camellia_info
540 };
541 
542 const cipher_info_t camellia_256_cbc_info = {
545  256,
546  "CAMELLIA-256-CBC",
547  16,
548  0,
549  16,
550  &camellia_info
551 };
552 #endif /* POLARSSL_CIPHER_MODE_CBC */
553 
554 #if defined(POLARSSL_CIPHER_MODE_CFB)
555 const cipher_info_t camellia_128_cfb128_info = {
558  128,
559  "CAMELLIA-128-CFB128",
560  16,
561  0,
562  16,
563  &camellia_info
564 };
565 
566 const cipher_info_t camellia_192_cfb128_info = {
569  192,
570  "CAMELLIA-192-CFB128",
571  16,
572  0,
573  16,
574  &camellia_info
575 };
576 
577 const cipher_info_t camellia_256_cfb128_info = {
580  256,
581  "CAMELLIA-256-CFB128",
582  16,
583  0,
584  16,
585  &camellia_info
586 };
587 #endif /* POLARSSL_CIPHER_MODE_CFB */
588 
589 #if defined(POLARSSL_CIPHER_MODE_CTR)
590 const cipher_info_t camellia_128_ctr_info = {
593  128,
594  "CAMELLIA-128-CTR",
595  16,
596  0,
597  16,
598  &camellia_info
599 };
600 
601 const cipher_info_t camellia_192_ctr_info = {
604  192,
605  "CAMELLIA-192-CTR",
606  16,
607  0,
608  16,
609  &camellia_info
610 };
611 
612 const cipher_info_t camellia_256_ctr_info = {
615  256,
616  "CAMELLIA-256-CTR",
617  16,
618  0,
619  16,
620  &camellia_info
621 };
622 #endif /* POLARSSL_CIPHER_MODE_CTR */
623 
624 #if defined(POLARSSL_GCM_C)
625 static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
626  unsigned int key_length )
627 {
629  key, key_length );
630 }
631 
632 const cipher_base_t gcm_camellia_info = {
634  NULL,
635  NULL,
636  NULL,
637  NULL,
638  NULL,
639  gcm_camellia_setkey_wrap,
640  gcm_camellia_setkey_wrap,
641  gcm_ctx_alloc,
642  gcm_ctx_free,
643 };
644 
645 const cipher_info_t camellia_128_gcm_info = {
648  128,
649  "CAMELLIA-128-GCM",
650  12,
651  1,
652  16,
653  &gcm_camellia_info
654 };
655 
656 const cipher_info_t camellia_192_gcm_info = {
659  192,
660  "CAMELLIA-192-GCM",
661  12,
662  1,
663  16,
664  &gcm_camellia_info
665 };
666 
667 const cipher_info_t camellia_256_gcm_info = {
670  256,
671  "CAMELLIA-256-GCM",
672  12,
673  1,
674  16,
675  &gcm_camellia_info
676 };
677 #endif /* POLARSSL_GCM_C */
678 
679 #endif /* POLARSSL_CAMELLIA_C */
680 
681 #if defined(POLARSSL_DES_C)
682 
683 static int des_crypt_ecb_wrap( void *ctx, operation_t operation,
684  const unsigned char *input, unsigned char *output )
685 {
686  ((void) operation);
687  return des_crypt_ecb( (des_context *) ctx, input, output );
688 }
689 
690 static int des3_crypt_ecb_wrap( void *ctx, operation_t operation,
691  const unsigned char *input, unsigned char *output )
692 {
693  ((void) operation);
694  return des3_crypt_ecb( (des3_context *) ctx, input, output );
695 }
696 
697 static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
698  unsigned char *iv, const unsigned char *input, unsigned char *output )
699 {
700 #if defined(POLARSSL_CIPHER_MODE_CBC)
701  return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input,
702  output );
703 #else
704  ((void) ctx);
705  ((void) operation);
706  ((void) length);
707  ((void) iv);
708  ((void) input);
709  ((void) output);
710 
712 #endif /* POLARSSL_CIPHER_MODE_CBC */
713 }
714 
715 static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
716  unsigned char *iv, const unsigned char *input, unsigned char *output )
717 {
718 #if defined(POLARSSL_CIPHER_MODE_CBC)
719  return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input,
720  output );
721 #else
722  ((void) ctx);
723  ((void) operation);
724  ((void) length);
725  ((void) iv);
726  ((void) input);
727  ((void) output);
728 
730 #endif /* POLARSSL_CIPHER_MODE_CBC */
731 }
732 
733 static int des_crypt_cfb128_wrap( void *ctx, operation_t operation,
734  size_t length, size_t *iv_off, unsigned char *iv,
735  const unsigned char *input, unsigned char *output )
736 {
737  ((void) ctx);
738  ((void) operation);
739  ((void) length);
740  ((void) iv_off);
741  ((void) iv);
742  ((void) input);
743  ((void) output);
744 
746 }
747 
748 static int des_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
749  unsigned char *nonce_counter, unsigned char *stream_block,
750  const unsigned char *input, unsigned char *output )
751 {
752  ((void) ctx);
753  ((void) length);
754  ((void) nc_off);
755  ((void) nonce_counter);
756  ((void) stream_block);
757  ((void) input);
758  ((void) output);
759 
761 }
762 
763 static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
764  unsigned int key_length )
765 {
766  ((void) key_length);
767 
768  return des_setkey_dec( (des_context *) ctx, key );
769 }
770 
771 static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
772  unsigned int key_length )
773 {
774  ((void) key_length);
775 
776  return des_setkey_enc( (des_context *) ctx, key );
777 }
778 
779 static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
780  unsigned int key_length )
781 {
782  ((void) key_length);
783 
784  return des3_set2key_dec( (des3_context *) ctx, key );
785 }
786 
787 static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
788  unsigned int key_length )
789 {
790  ((void) key_length);
791 
792  return des3_set2key_enc( (des3_context *) ctx, key );
793 }
794 
795 static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
796  unsigned int key_length )
797 {
798  ((void) key_length);
799 
800  return des3_set3key_dec( (des3_context *) ctx, key );
801 }
802 
803 static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
804  unsigned int key_length )
805 {
806  ((void) key_length);
807 
808  return des3_set3key_enc( (des3_context *) ctx, key );
809 }
810 
811 static void * des_ctx_alloc( void )
812 {
813  return polarssl_malloc( sizeof( des_context ) );
814 }
815 
816 static void * des3_ctx_alloc( void )
817 {
818  return polarssl_malloc( sizeof( des3_context ) );
819 }
820 
821 static void des_ctx_free( void *ctx )
822 {
823  polarssl_free( ctx );
824 }
825 
826 const cipher_base_t des_info = {
828  des_crypt_ecb_wrap,
829  des_crypt_cbc_wrap,
830  des_crypt_cfb128_wrap,
831  des_crypt_ctr_wrap,
832  NULL,
833  des_setkey_enc_wrap,
834  des_setkey_dec_wrap,
835  des_ctx_alloc,
836  des_ctx_free
837 };
838 
839 const cipher_info_t des_ecb_info = {
843  "DES-ECB",
844  8,
845  0,
846  8,
847  &des_info
848 };
849 
850 #if defined(POLARSSL_CIPHER_MODE_CBC)
851 const cipher_info_t des_cbc_info = {
855  "DES-CBC",
856  8,
857  0,
858  8,
859  &des_info
860 };
861 #endif /* POLARSSL_CIPHER_MODE_CBC */
862 
863 const cipher_base_t des_ede_info = {
865  des3_crypt_ecb_wrap,
866  des3_crypt_cbc_wrap,
867  des_crypt_cfb128_wrap,
868  des_crypt_ctr_wrap,
869  NULL,
870  des3_set2key_enc_wrap,
871  des3_set2key_dec_wrap,
872  des3_ctx_alloc,
873  des_ctx_free
874 };
875 
876 const cipher_info_t des_ede_ecb_info = {
880  "DES-EDE-ECB",
881  8,
882  0,
883  8,
884  &des_ede_info
885 };
886 
887 #if defined(POLARSSL_CIPHER_MODE_CBC)
888 const cipher_info_t des_ede_cbc_info = {
892  "DES-EDE-CBC",
893  8,
894  0,
895  8,
896  &des_ede_info
897 };
898 #endif /* POLARSSL_CIPHER_MODE_CBC */
899 
900 const cipher_base_t des_ede3_info = {
902  des3_crypt_ecb_wrap,
903  des3_crypt_cbc_wrap,
904  des_crypt_cfb128_wrap,
905  des_crypt_ctr_wrap,
906  NULL,
907  des3_set3key_enc_wrap,
908  des3_set3key_dec_wrap,
909  des3_ctx_alloc,
910  des_ctx_free
911 };
912 
913 const cipher_info_t des_ede3_ecb_info = {
917  "DES-EDE3-ECB",
918  8,
919  0,
920  8,
921  &des_ede3_info
922 };
923 #if defined(POLARSSL_CIPHER_MODE_CBC)
924 const cipher_info_t des_ede3_cbc_info = {
928  "DES-EDE3-CBC",
929  8,
930  0,
931  8,
932  &des_ede3_info
933 };
934 #endif /* POLARSSL_CIPHER_MODE_CBC */
935 #endif /* POLARSSL_DES_C */
936 
937 #if defined(POLARSSL_BLOWFISH_C)
938 
939 static int blowfish_crypt_ecb_wrap( void *ctx, operation_t operation,
940  const unsigned char *input, unsigned char *output )
941 {
942  return blowfish_crypt_ecb( (blowfish_context *) ctx, operation, input,
943  output );
944 }
945 
946 static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation,
947  size_t length, unsigned char *iv, const unsigned char *input,
948  unsigned char *output )
949 {
950 #if defined(POLARSSL_CIPHER_MODE_CBC)
951  return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv,
952  input, output );
953 #else
954  ((void) ctx);
955  ((void) operation);
956  ((void) length);
957  ((void) iv);
958  ((void) input);
959  ((void) output);
960 
962 #endif /* POLARSSL_CIPHER_MODE_CBC */
963 }
964 
965 static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation,
966  size_t length, size_t *iv_off, unsigned char *iv,
967  const unsigned char *input, unsigned char *output )
968 {
969 #if defined(POLARSSL_CIPHER_MODE_CFB)
970  return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length,
971  iv_off, iv, input, output );
972 #else
973  ((void) ctx);
974  ((void) operation);
975  ((void) length);
976  ((void) iv_off);
977  ((void) iv);
978  ((void) input);
979  ((void) output);
980 
982 #endif /* POLARSSL_CIPHER_MODE_CFB */
983 }
984 
985 static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
986  unsigned char *nonce_counter, unsigned char *stream_block,
987  const unsigned char *input, unsigned char *output )
988 {
989 #if defined(POLARSSL_CIPHER_MODE_CTR)
990  return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off,
991  nonce_counter, stream_block, input, output );
992 #else
993  ((void) ctx);
994  ((void) length);
995  ((void) nc_off);
996  ((void) nonce_counter);
997  ((void) stream_block);
998  ((void) input);
999  ((void) output);
1000 
1002 #endif /* POLARSSL_CIPHER_MODE_CTR */
1003 }
1004 
1005 static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
1006  unsigned int key_length )
1007 {
1008  return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
1009 }
1010 
1011 static void * blowfish_ctx_alloc( void )
1012 {
1013  return polarssl_malloc( sizeof( blowfish_context ) );
1014 }
1015 
1016 static void blowfish_ctx_free( void *ctx )
1017 {
1018  polarssl_free( ctx );
1019 }
1020 
1021 const cipher_base_t blowfish_info = {
1023  blowfish_crypt_ecb_wrap,
1024  blowfish_crypt_cbc_wrap,
1025  blowfish_crypt_cfb64_wrap,
1026  blowfish_crypt_ctr_wrap,
1027  NULL,
1028  blowfish_setkey_wrap,
1029  blowfish_setkey_wrap,
1030  blowfish_ctx_alloc,
1031  blowfish_ctx_free
1032 };
1033 
1034 const cipher_info_t blowfish_ecb_info = {
1037  128,
1038  "BLOWFISH-ECB",
1039  8,
1040  0,
1041  8,
1042  &blowfish_info
1043 };
1044 
1045 #if defined(POLARSSL_CIPHER_MODE_CBC)
1046 const cipher_info_t blowfish_cbc_info = {
1049  128,
1050  "BLOWFISH-CBC",
1051  8,
1052  0,
1053  8,
1054  &blowfish_info
1055 };
1056 #endif /* POLARSSL_CIPHER_MODE_CBC */
1057 
1058 #if defined(POLARSSL_CIPHER_MODE_CFB)
1059 const cipher_info_t blowfish_cfb64_info = {
1062  128,
1063  "BLOWFISH-CFB64",
1064  8,
1065  0,
1066  8,
1067  &blowfish_info
1068 };
1069 #endif /* POLARSSL_CIPHER_MODE_CFB */
1070 
1071 #if defined(POLARSSL_CIPHER_MODE_CTR)
1072 const cipher_info_t blowfish_ctr_info = {
1075  128,
1076  "BLOWFISH-CTR",
1077  8,
1078  0,
1079  8,
1080  &blowfish_info
1081 };
1082 #endif /* POLARSSL_CIPHER_MODE_CTR */
1083 #endif /* POLARSSL_BLOWFISH_C */
1084 
1085 #if defined(POLARSSL_ARC4_C)
1086 static int arc4_crypt_stream_wrap( void *ctx, size_t length,
1087  const unsigned char *input,
1088  unsigned char *output )
1089 {
1090  return( arc4_crypt( (arc4_context *) ctx, length, input, output ) );
1091 }
1092 
1093 static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
1094  unsigned int key_length )
1095 {
1096  /* we get key_length in bits, arc4 expects it in bytes */
1097  if( key_length % 8 != 0)
1099 
1100  arc4_setup( (arc4_context *) ctx, key, key_length / 8 );
1101  return( 0 );
1102 }
1103 
1104 static void * arc4_ctx_alloc( void )
1105 {
1106  return polarssl_malloc( sizeof( arc4_context ) );
1107 }
1108 
1109 static void arc4_ctx_free( void *ctx )
1110 {
1111  polarssl_free( ctx );
1112 }
1113 
1114 const cipher_base_t arc4_base_info = {
1116  NULL,
1117  NULL,
1118  NULL,
1119  NULL,
1120  arc4_crypt_stream_wrap,
1121  arc4_setkey_wrap,
1122  arc4_setkey_wrap,
1123  arc4_ctx_alloc,
1124  arc4_ctx_free
1125 };
1126 
1127 const cipher_info_t arc4_128_info = {
1130  128,
1131  "ARC4-128",
1132  0,
1133  0,
1134  1,
1135  &arc4_base_info
1136 };
1137 #endif /* POLARSSL_ARC4_C */
1138 
1139 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
1140 static int null_crypt_stream( void *ctx, size_t length,
1141  const unsigned char *input,
1142  unsigned char *output )
1143 {
1144  ((void) ctx);
1145  memmove( output, input, length );
1146  return( 0 );
1147 }
1148 
1149 static int null_setkey( void *ctx, const unsigned char *key,
1150  unsigned int key_length )
1151 {
1152  ((void) ctx);
1153  ((void) key);
1154  ((void) key_length);
1155 
1156  return( 0 );
1157 }
1158 
1159 static void * null_ctx_alloc( void )
1160 {
1161  return (void *) 1;
1162 }
1163 
1164 static void null_ctx_free( void *ctx )
1165 {
1166  ((void) ctx);
1167 }
1168 
1169 const cipher_base_t null_base_info = {
1171  NULL,
1172  NULL,
1173  NULL,
1174  NULL,
1175  null_crypt_stream,
1176  null_setkey,
1177  null_setkey,
1178  null_ctx_alloc,
1179  null_ctx_free
1180 };
1181 
1182 const cipher_info_t null_cipher_info = {
1185  0,
1186  "NULL",
1187  0,
1188  0,
1189  1,
1190  &null_base_info
1191 };
1192 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
1193 
1195 {
1196 #if defined(POLARSSL_AES_C)
1197  { POLARSSL_CIPHER_AES_128_ECB, &aes_128_ecb_info },
1198  { POLARSSL_CIPHER_AES_192_ECB, &aes_192_ecb_info },
1199  { POLARSSL_CIPHER_AES_256_ECB, &aes_256_ecb_info },
1200 #if defined(POLARSSL_CIPHER_MODE_CBC)
1201  { POLARSSL_CIPHER_AES_128_CBC, &aes_128_cbc_info },
1202  { POLARSSL_CIPHER_AES_192_CBC, &aes_192_cbc_info },
1203  { POLARSSL_CIPHER_AES_256_CBC, &aes_256_cbc_info },
1204 #endif
1205 #if defined(POLARSSL_CIPHER_MODE_CFB)
1206  { POLARSSL_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
1207  { POLARSSL_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
1208  { POLARSSL_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
1209 #endif
1210 #if defined(POLARSSL_CIPHER_MODE_CTR)
1211  { POLARSSL_CIPHER_AES_128_CTR, &aes_128_ctr_info },
1212  { POLARSSL_CIPHER_AES_192_CTR, &aes_192_ctr_info },
1213  { POLARSSL_CIPHER_AES_256_CTR, &aes_256_ctr_info },
1214 #endif
1215 #if defined(POLARSSL_GCM_C)
1216  { POLARSSL_CIPHER_AES_128_GCM, &aes_128_gcm_info },
1217  { POLARSSL_CIPHER_AES_192_GCM, &aes_192_gcm_info },
1218  { POLARSSL_CIPHER_AES_256_GCM, &aes_256_gcm_info },
1219 #endif
1220 #endif /* POLARSSL_AES_C */
1221 
1222 #if defined(POLARSSL_ARC4_C)
1223  { POLARSSL_CIPHER_ARC4_128, &arc4_128_info },
1224 #endif
1225 
1226 #if defined(POLARSSL_BLOWFISH_C)
1227  { POLARSSL_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
1228 #if defined(POLARSSL_CIPHER_MODE_CBC)
1229  { POLARSSL_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
1230 #endif
1231 #if defined(POLARSSL_CIPHER_MODE_CFB)
1232  { POLARSSL_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
1233 #endif
1234 #if defined(POLARSSL_CIPHER_MODE_CTR)
1235  { POLARSSL_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
1236 #endif
1237 #endif /* POLARSSL_BLOWFISH_C */
1238 
1239 #if defined(POLARSSL_CAMELLIA_C)
1240  { POLARSSL_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
1241  { POLARSSL_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
1242  { POLARSSL_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
1243 #if defined(POLARSSL_CIPHER_MODE_CBC)
1244  { POLARSSL_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
1245  { POLARSSL_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
1246  { POLARSSL_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
1247 #endif
1248 #if defined(POLARSSL_CIPHER_MODE_CFB)
1249  { POLARSSL_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
1250  { POLARSSL_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
1251  { POLARSSL_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
1252 #endif
1253 #if defined(POLARSSL_CIPHER_MODE_CTR)
1254  { POLARSSL_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
1255  { POLARSSL_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
1256  { POLARSSL_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
1257 #endif
1258 #if defined(POLARSSL_GCM_C)
1259  { POLARSSL_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
1260  { POLARSSL_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
1261  { POLARSSL_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
1262 #endif
1263 #endif /* POLARSSL_CAMELLIA_C */
1264 
1265 #if defined(POLARSSL_DES_C)
1266  { POLARSSL_CIPHER_DES_ECB, &des_ecb_info },
1267  { POLARSSL_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
1268  { POLARSSL_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
1269 #if defined(POLARSSL_CIPHER_MODE_CBC)
1270  { POLARSSL_CIPHER_DES_CBC, &des_cbc_info },
1271  { POLARSSL_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
1272  { POLARSSL_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
1273 #endif
1274 #endif /* POLARSSL_DES_C */
1275 
1276 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
1277  { POLARSSL_CIPHER_NULL, &null_cipher_info },
1278 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
1279 
1280  { 0, NULL }
1281 };
1282 
1283 #define NUM_CIPHERS sizeof cipher_definitions / sizeof cipher_definitions[0]
1284 int supported_ciphers[NUM_CIPHERS];
1285 
1286 #endif /* POLARSSL_CIPHER_C */
#define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Bad input parameters to function.
Definition: cipher.h:58
int blowfish_setkey(blowfish_context *ctx, const unsigned char *key, unsigned int keysize)
Blowfish key schedule.
int supported_ciphers[]
#define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
The selected feature is not available.
Definition: cipher.h:57
int arc4_crypt(arc4_context *ctx, size_t length, const unsigned char *input, unsigned char *output)
ARC4 cipher function.
Key length, in bits (including parity), for DES keys.
Definition: cipher.h:154
void arc4_setup(arc4_context *ctx, const unsigned char *key, unsigned int keylen)
ARC4 key schedule.
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) ...
int des_crypt_ecb(des_context *ctx, const unsigned char input[8], unsigned char output[8])
DES-ECB block encryption/decryption.
Cipher information.
Definition: cipher.h:216
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.
#define polarssl_free
Definition: platform.h:91
AES context structure.
Definition: aes.h:68
Configuration options (set of defines)
int aes_setkey_dec(aes_context *ctx, const unsigned char *key, unsigned int keysize)
AES key schedule (decryption)
Camellia block cipher.
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.
PolarSSL Platform abstraction layer.
DES context structure.
Definition: des.h:63
const cipher_definition_t cipher_definitions[]
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.
int blowfish_crypt_ecb(blowfish_context *ctx, int mode, const unsigned char input[BLOWFISH_BLOCKSIZE], unsigned char output[BLOWFISH_BLOCKSIZE])
Blowfish-ECB block encryption/decryption.
Triple-DES context structure.
Definition: des.h:73
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:144
AES block cipher.
Key length, in bits (including parity), for DES in three-key EDE.
Definition: cipher.h:158
GCM context structure.
Definition: gcm.h:53
CAMELLIA context structure.
Definition: camellia.h:62
int gcm_init(gcm_context *ctx, cipher_id_t cipher, const unsigned char *key, unsigned int keysize)
GCM initialization (encryption)
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_crypt_ecb(camellia_context *ctx, int mode, const unsigned char input[16], unsigned char output[16])
CAMELLIA-ECB block encryption/decryption.
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)
int des_setkey_dec(des_context *ctx, const unsigned char key[DES_KEY_SIZE])
DES key schedule (56-bit, decryption)
void gcm_free(gcm_context *ctx)
Free a GCM context and underlying cipher sub-context.
Base cipher information.
Definition: cipher.h:169
Galois/Counter mode for 128-bit block ciphers.
ARC4 context structure.
Definition: arc4.h:49
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.
int des3_crypt_ecb(des3_context *ctx, const unsigned char input[8], unsigned char output[8])
3DES-ECB block encryption/decryption
Blowfish context structure.
Definition: blowfish.h:66
#define polarssl_malloc
Definition: platform.h:90
The ARCFOUR stream cipher.
int aes_setkey_enc(aes_context *ctx, const unsigned char *key, unsigned int keysize)
AES key schedule (encryption)
int aes_crypt_ecb(aes_context *ctx, int mode, const unsigned char input[16], unsigned char output[16])
AES-ECB block encryption/decryption.
Key length, in bits (including parity), for DES in two key EDE.
Definition: cipher.h:156
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) ...
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