PolarSSL v1.3.9
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_CCM_C)
65 #include "polarssl/ccm.h"
66 #endif
67 
68 #if defined(POLARSSL_PLATFORM_C)
69 #include "polarssl/platform.h"
70 #else
71 #define polarssl_malloc malloc
72 #define polarssl_free free
73 #endif
74 
75 #include <stdlib.h>
76 
77 #if defined(POLARSSL_GCM_C)
78 /* shared by all GCM ciphers */
79 static void *gcm_ctx_alloc( void )
80 {
81  return polarssl_malloc( sizeof( gcm_context ) );
82 }
83 
84 static void gcm_ctx_free( void *ctx )
85 {
86  gcm_free( ctx );
87  polarssl_free( ctx );
88 }
89 #endif /* POLARSSL_GCM_C */
90 
91 #if defined(POLARSSL_CCM_C)
92 /* shared by all CCM ciphers */
93 static void *ccm_ctx_alloc( void )
94 {
95  return polarssl_malloc( sizeof( ccm_context ) );
96 }
97 
98 static void ccm_ctx_free( void *ctx )
99 {
100  ccm_free( ctx );
101  polarssl_free( ctx );
102 }
103 #endif /* POLARSSL_CCM_C */
104 
105 #if defined(POLARSSL_AES_C)
106 
107 static int aes_crypt_ecb_wrap( void *ctx, operation_t operation,
108  const unsigned char *input, unsigned char *output )
109 {
110  return aes_crypt_ecb( (aes_context *) ctx, operation, input, output );
111 }
112 
113 static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
114  unsigned char *iv, const unsigned char *input, unsigned char *output )
115 {
116 #if defined(POLARSSL_CIPHER_MODE_CBC)
117  return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input,
118  output );
119 #else
120  ((void) ctx);
121  ((void) operation);
122  ((void) length);
123  ((void) iv);
124  ((void) input);
125  ((void) output);
126 
128 #endif /* POLARSSL_CIPHER_MODE_CBC */
129 }
130 
131 static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation,
132  size_t length, size_t *iv_off, unsigned char *iv,
133  const unsigned char *input, unsigned char *output )
134 {
135 #if defined(POLARSSL_CIPHER_MODE_CFB)
136  return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv,
137  input, output );
138 #else
139  ((void) ctx);
140  ((void) operation);
141  ((void) length);
142  ((void) iv_off);
143  ((void) iv);
144  ((void) input);
145  ((void) output);
146 
148 #endif /* POLARSSL_CIPHER_MODE_CFB */
149 }
150 
151 static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
152  unsigned char *nonce_counter, unsigned char *stream_block,
153  const unsigned char *input, unsigned char *output )
154 {
155 #if defined(POLARSSL_CIPHER_MODE_CTR)
156  return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
157  stream_block, input, output );
158 #else
159  ((void) ctx);
160  ((void) length);
161  ((void) nc_off);
162  ((void) nonce_counter);
163  ((void) stream_block);
164  ((void) input);
165  ((void) output);
166 
168 #endif /* POLARSSL_CIPHER_MODE_CTR */
169 }
170 
171 static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
172  unsigned int key_length )
173 {
174  return aes_setkey_dec( (aes_context *) ctx, key, key_length );
175 }
176 
177 static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
178  unsigned int key_length )
179 {
180  return aes_setkey_enc( (aes_context *) ctx, key, key_length );
181 }
182 
183 static void * aes_ctx_alloc( void )
184 {
185  aes_context *aes = (aes_context *) polarssl_malloc( sizeof( aes_context ) );
186 
187  if( aes == NULL )
188  return( NULL );
189 
190  aes_init( aes );
191 
192  return( aes );
193 }
194 
195 static void aes_ctx_free( void *ctx )
196 {
197  aes_free( (aes_context *) ctx );
198  polarssl_free( ctx );
199 }
200 
201 const cipher_base_t aes_info = {
203  aes_crypt_ecb_wrap,
204  aes_crypt_cbc_wrap,
205  aes_crypt_cfb128_wrap,
206  aes_crypt_ctr_wrap,
207  NULL,
208  aes_setkey_enc_wrap,
209  aes_setkey_dec_wrap,
210  aes_ctx_alloc,
211  aes_ctx_free
212 };
213 
214 const cipher_info_t aes_128_ecb_info = {
217  128,
218  "AES-128-ECB",
219  16,
220  0,
221  16,
222  &aes_info
223 };
224 
225 const cipher_info_t aes_192_ecb_info = {
228  192,
229  "AES-192-ECB",
230  16,
231  0,
232  16,
233  &aes_info
234 };
235 
236 const cipher_info_t aes_256_ecb_info = {
239  256,
240  "AES-256-ECB",
241  16,
242  0,
243  16,
244  &aes_info
245 };
246 
247 #if defined(POLARSSL_CIPHER_MODE_CBC)
248 const cipher_info_t aes_128_cbc_info = {
251  128,
252  "AES-128-CBC",
253  16,
254  0,
255  16,
256  &aes_info
257 };
258 
259 const cipher_info_t aes_192_cbc_info = {
262  192,
263  "AES-192-CBC",
264  16,
265  0,
266  16,
267  &aes_info
268 };
269 
270 const cipher_info_t aes_256_cbc_info = {
273  256,
274  "AES-256-CBC",
275  16,
276  0,
277  16,
278  &aes_info
279 };
280 #endif /* POLARSSL_CIPHER_MODE_CBC */
281 
282 #if defined(POLARSSL_CIPHER_MODE_CFB)
283 const cipher_info_t aes_128_cfb128_info = {
286  128,
287  "AES-128-CFB128",
288  16,
289  0,
290  16,
291  &aes_info
292 };
293 
294 const cipher_info_t aes_192_cfb128_info = {
297  192,
298  "AES-192-CFB128",
299  16,
300  0,
301  16,
302  &aes_info
303 };
304 
305 const cipher_info_t aes_256_cfb128_info = {
308  256,
309  "AES-256-CFB128",
310  16,
311  0,
312  16,
313  &aes_info
314 };
315 #endif /* POLARSSL_CIPHER_MODE_CFB */
316 
317 #if defined(POLARSSL_CIPHER_MODE_CTR)
318 const cipher_info_t aes_128_ctr_info = {
321  128,
322  "AES-128-CTR",
323  16,
324  0,
325  16,
326  &aes_info
327 };
328 
329 const cipher_info_t aes_192_ctr_info = {
332  192,
333  "AES-192-CTR",
334  16,
335  0,
336  16,
337  &aes_info
338 };
339 
340 const cipher_info_t aes_256_ctr_info = {
343  256,
344  "AES-256-CTR",
345  16,
346  0,
347  16,
348  &aes_info
349 };
350 #endif /* POLARSSL_CIPHER_MODE_CTR */
351 
352 #if defined(POLARSSL_GCM_C)
353 static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
354  unsigned int key_length )
355 {
357  key, key_length );
358 }
359 
360 const cipher_base_t gcm_aes_info = {
362  NULL,
363  NULL,
364  NULL,
365  NULL,
366  NULL,
367  gcm_aes_setkey_wrap,
368  gcm_aes_setkey_wrap,
369  gcm_ctx_alloc,
370  gcm_ctx_free,
371 };
372 
373 const cipher_info_t aes_128_gcm_info = {
376  128,
377  "AES-128-GCM",
378  12,
380  16,
381  &gcm_aes_info
382 };
383 
384 const cipher_info_t aes_192_gcm_info = {
387  192,
388  "AES-192-GCM",
389  12,
391  16,
392  &gcm_aes_info
393 };
394 
395 const cipher_info_t aes_256_gcm_info = {
398  256,
399  "AES-256-GCM",
400  12,
402  16,
403  &gcm_aes_info
404 };
405 #endif /* POLARSSL_GCM_C */
406 
407 #if defined(POLARSSL_CCM_C)
408 static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
409  unsigned int key_length )
410 {
412  key, key_length );
413 }
414 
415 const cipher_base_t ccm_aes_info = {
417  NULL,
418  NULL,
419  NULL,
420  NULL,
421  NULL,
422  ccm_aes_setkey_wrap,
423  ccm_aes_setkey_wrap,
424  ccm_ctx_alloc,
425  ccm_ctx_free,
426 };
427 
428 const cipher_info_t aes_128_ccm_info = {
431  128,
432  "AES-128-CCM",
433  12,
435  16,
436  &ccm_aes_info
437 };
438 
439 const cipher_info_t aes_192_ccm_info = {
442  192,
443  "AES-192-CCM",
444  12,
446  16,
447  &ccm_aes_info
448 };
449 
450 const cipher_info_t aes_256_ccm_info = {
453  256,
454  "AES-256-CCM",
455  12,
457  16,
458  &ccm_aes_info
459 };
460 #endif /* POLARSSL_CCM_C */
461 
462 #endif /* POLARSSL_AES_C */
463 
464 #if defined(POLARSSL_CAMELLIA_C)
465 
466 static int camellia_crypt_ecb_wrap( void *ctx, operation_t operation,
467  const unsigned char *input, unsigned char *output )
468 {
469  return camellia_crypt_ecb( (camellia_context *) ctx, operation, input,
470  output );
471 }
472 
473 static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation,
474  size_t length, unsigned char *iv,
475  const unsigned char *input, unsigned char *output )
476 {
477 #if defined(POLARSSL_CIPHER_MODE_CBC)
478  return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv,
479  input, output );
480 #else
481  ((void) ctx);
482  ((void) operation);
483  ((void) length);
484  ((void) iv);
485  ((void) input);
486  ((void) output);
487 
489 #endif /* POLARSSL_CIPHER_MODE_CBC */
490 }
491 
492 static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation,
493  size_t length, size_t *iv_off, unsigned char *iv,
494  const unsigned char *input, unsigned char *output )
495 {
496 #if defined(POLARSSL_CIPHER_MODE_CFB)
497  return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length,
498  iv_off, iv, input, output );
499 #else
500  ((void) ctx);
501  ((void) operation);
502  ((void) length);
503  ((void) iv_off);
504  ((void) iv);
505  ((void) input);
506  ((void) output);
507 
509 #endif /* POLARSSL_CIPHER_MODE_CFB */
510 }
511 
512 static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
513  unsigned char *nonce_counter, unsigned char *stream_block,
514  const unsigned char *input, unsigned char *output )
515 {
516 #if defined(POLARSSL_CIPHER_MODE_CTR)
517  return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off,
518  nonce_counter, stream_block, input, output );
519 #else
520  ((void) ctx);
521  ((void) length);
522  ((void) nc_off);
523  ((void) nonce_counter);
524  ((void) stream_block);
525  ((void) input);
526  ((void) output);
527 
529 #endif /* POLARSSL_CIPHER_MODE_CTR */
530 }
531 
532 static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
533  unsigned int key_length )
534 {
535  return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
536 }
537 
538 static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
539  unsigned int key_length )
540 {
541  return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
542 }
543 
544 static void * camellia_ctx_alloc( void )
545 {
546  camellia_context *ctx;
547  ctx = (camellia_context *) polarssl_malloc( sizeof( camellia_context ) );
548 
549  if( ctx == NULL )
550  return( NULL );
551 
552  camellia_init( ctx );
553 
554  return( ctx );
555 }
556 
557 static void camellia_ctx_free( void *ctx )
558 {
559  camellia_free( (camellia_context *) ctx );
560  polarssl_free( ctx );
561 }
562 
563 const cipher_base_t camellia_info = {
565  camellia_crypt_ecb_wrap,
566  camellia_crypt_cbc_wrap,
567  camellia_crypt_cfb128_wrap,
568  camellia_crypt_ctr_wrap,
569  NULL,
570  camellia_setkey_enc_wrap,
571  camellia_setkey_dec_wrap,
572  camellia_ctx_alloc,
573  camellia_ctx_free
574 };
575 
576 const cipher_info_t camellia_128_ecb_info = {
579  128,
580  "CAMELLIA-128-ECB",
581  16,
582  0,
583  16,
584  &camellia_info
585 };
586 
587 const cipher_info_t camellia_192_ecb_info = {
590  192,
591  "CAMELLIA-192-ECB",
592  16,
593  0,
594  16,
595  &camellia_info
596 };
597 
598 const cipher_info_t camellia_256_ecb_info = {
601  256,
602  "CAMELLIA-256-ECB",
603  16,
604  0,
605  16,
606  &camellia_info
607 };
608 
609 #if defined(POLARSSL_CIPHER_MODE_CBC)
610 const cipher_info_t camellia_128_cbc_info = {
613  128,
614  "CAMELLIA-128-CBC",
615  16,
616  0,
617  16,
618  &camellia_info
619 };
620 
621 const cipher_info_t camellia_192_cbc_info = {
624  192,
625  "CAMELLIA-192-CBC",
626  16,
627  0,
628  16,
629  &camellia_info
630 };
631 
632 const cipher_info_t camellia_256_cbc_info = {
635  256,
636  "CAMELLIA-256-CBC",
637  16,
638  0,
639  16,
640  &camellia_info
641 };
642 #endif /* POLARSSL_CIPHER_MODE_CBC */
643 
644 #if defined(POLARSSL_CIPHER_MODE_CFB)
645 const cipher_info_t camellia_128_cfb128_info = {
648  128,
649  "CAMELLIA-128-CFB128",
650  16,
651  0,
652  16,
653  &camellia_info
654 };
655 
656 const cipher_info_t camellia_192_cfb128_info = {
659  192,
660  "CAMELLIA-192-CFB128",
661  16,
662  0,
663  16,
664  &camellia_info
665 };
666 
667 const cipher_info_t camellia_256_cfb128_info = {
670  256,
671  "CAMELLIA-256-CFB128",
672  16,
673  0,
674  16,
675  &camellia_info
676 };
677 #endif /* POLARSSL_CIPHER_MODE_CFB */
678 
679 #if defined(POLARSSL_CIPHER_MODE_CTR)
680 const cipher_info_t camellia_128_ctr_info = {
683  128,
684  "CAMELLIA-128-CTR",
685  16,
686  0,
687  16,
688  &camellia_info
689 };
690 
691 const cipher_info_t camellia_192_ctr_info = {
694  192,
695  "CAMELLIA-192-CTR",
696  16,
697  0,
698  16,
699  &camellia_info
700 };
701 
702 const cipher_info_t camellia_256_ctr_info = {
705  256,
706  "CAMELLIA-256-CTR",
707  16,
708  0,
709  16,
710  &camellia_info
711 };
712 #endif /* POLARSSL_CIPHER_MODE_CTR */
713 
714 #if defined(POLARSSL_GCM_C)
715 static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
716  unsigned int key_length )
717 {
719  key, key_length );
720 }
721 
722 const cipher_base_t gcm_camellia_info = {
724  NULL,
725  NULL,
726  NULL,
727  NULL,
728  NULL,
729  gcm_camellia_setkey_wrap,
730  gcm_camellia_setkey_wrap,
731  gcm_ctx_alloc,
732  gcm_ctx_free,
733 };
734 
735 const cipher_info_t camellia_128_gcm_info = {
738  128,
739  "CAMELLIA-128-GCM",
740  12,
742  16,
743  &gcm_camellia_info
744 };
745 
746 const cipher_info_t camellia_192_gcm_info = {
749  192,
750  "CAMELLIA-192-GCM",
751  12,
753  16,
754  &gcm_camellia_info
755 };
756 
757 const cipher_info_t camellia_256_gcm_info = {
760  256,
761  "CAMELLIA-256-GCM",
762  12,
764  16,
765  &gcm_camellia_info
766 };
767 #endif /* POLARSSL_GCM_C */
768 
769 #if defined(POLARSSL_CCM_C)
770 static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
771  unsigned int key_length )
772 {
774  key, key_length );
775 }
776 
777 const cipher_base_t ccm_camellia_info = {
779  NULL,
780  NULL,
781  NULL,
782  NULL,
783  NULL,
784  ccm_camellia_setkey_wrap,
785  ccm_camellia_setkey_wrap,
786  ccm_ctx_alloc,
787  ccm_ctx_free,
788 };
789 
790 const cipher_info_t camellia_128_ccm_info = {
793  128,
794  "CAMELLIA-128-CCM",
795  12,
797  16,
798  &ccm_camellia_info
799 };
800 
801 const cipher_info_t camellia_192_ccm_info = {
804  192,
805  "CAMELLIA-192-CCM",
806  12,
808  16,
809  &ccm_camellia_info
810 };
811 
812 const cipher_info_t camellia_256_ccm_info = {
815  256,
816  "CAMELLIA-256-CCM",
817  12,
819  16,
820  &ccm_camellia_info
821 };
822 #endif /* POLARSSL_CCM_C */
823 
824 #endif /* POLARSSL_CAMELLIA_C */
825 
826 #if defined(POLARSSL_DES_C)
827 
828 static int des_crypt_ecb_wrap( void *ctx, operation_t operation,
829  const unsigned char *input, unsigned char *output )
830 {
831  ((void) operation);
832  return des_crypt_ecb( (des_context *) ctx, input, output );
833 }
834 
835 static int des3_crypt_ecb_wrap( void *ctx, operation_t operation,
836  const unsigned char *input, unsigned char *output )
837 {
838  ((void) operation);
839  return des3_crypt_ecb( (des3_context *) ctx, input, output );
840 }
841 
842 static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
843  unsigned char *iv, const unsigned char *input, unsigned char *output )
844 {
845 #if defined(POLARSSL_CIPHER_MODE_CBC)
846  return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input,
847  output );
848 #else
849  ((void) ctx);
850  ((void) operation);
851  ((void) length);
852  ((void) iv);
853  ((void) input);
854  ((void) output);
855 
857 #endif /* POLARSSL_CIPHER_MODE_CBC */
858 }
859 
860 static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
861  unsigned char *iv, const unsigned char *input, unsigned char *output )
862 {
863 #if defined(POLARSSL_CIPHER_MODE_CBC)
864  return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input,
865  output );
866 #else
867  ((void) ctx);
868  ((void) operation);
869  ((void) length);
870  ((void) iv);
871  ((void) input);
872  ((void) output);
873 
875 #endif /* POLARSSL_CIPHER_MODE_CBC */
876 }
877 
878 static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
879  unsigned int key_length )
880 {
881  ((void) key_length);
882 
883  return des_setkey_dec( (des_context *) ctx, key );
884 }
885 
886 static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
887  unsigned int key_length )
888 {
889  ((void) key_length);
890 
891  return des_setkey_enc( (des_context *) ctx, key );
892 }
893 
894 static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
895  unsigned int key_length )
896 {
897  ((void) key_length);
898 
899  return des3_set2key_dec( (des3_context *) ctx, key );
900 }
901 
902 static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
903  unsigned int key_length )
904 {
905  ((void) key_length);
906 
907  return des3_set2key_enc( (des3_context *) ctx, key );
908 }
909 
910 static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
911  unsigned int key_length )
912 {
913  ((void) key_length);
914 
915  return des3_set3key_dec( (des3_context *) ctx, key );
916 }
917 
918 static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
919  unsigned int key_length )
920 {
921  ((void) key_length);
922 
923  return des3_set3key_enc( (des3_context *) ctx, key );
924 }
925 
926 static void * des_ctx_alloc( void )
927 {
928  des_context *des = (des_context *) polarssl_malloc( sizeof( des_context ) );
929 
930  if( des == NULL )
931  return( NULL );
932 
933  des_init( des );
934 
935  return( des );
936 }
937 
938 static void des_ctx_free( void *ctx )
939 {
940  des_free( (des_context *) ctx );
941  polarssl_free( ctx );
942 }
943 
944 static void * des3_ctx_alloc( void )
945 {
946  des3_context *des3;
947  des3 = (des3_context *) polarssl_malloc( sizeof( des3_context ) );
948 
949  if( des3 == NULL )
950  return( NULL );
951 
952  des3_init( des3 );
953 
954  return( des3 );
955 }
956 
957 static void des3_ctx_free( void *ctx )
958 {
959  des3_free( (des3_context *) ctx );
960  polarssl_free( ctx );
961 }
962 
963 const cipher_base_t des_info = {
965  des_crypt_ecb_wrap,
966  des_crypt_cbc_wrap,
967  NULL,
968  NULL,
969  NULL,
970  des_setkey_enc_wrap,
971  des_setkey_dec_wrap,
972  des_ctx_alloc,
973  des_ctx_free
974 };
975 
976 const cipher_info_t des_ecb_info = {
980  "DES-ECB",
981  8,
982  0,
983  8,
984  &des_info
985 };
986 
987 #if defined(POLARSSL_CIPHER_MODE_CBC)
988 const cipher_info_t des_cbc_info = {
992  "DES-CBC",
993  8,
994  0,
995  8,
996  &des_info
997 };
998 #endif /* POLARSSL_CIPHER_MODE_CBC */
999 
1000 const cipher_base_t des_ede_info = {
1002  des3_crypt_ecb_wrap,
1003  des3_crypt_cbc_wrap,
1004  NULL,
1005  NULL,
1006  NULL,
1007  des3_set2key_enc_wrap,
1008  des3_set2key_dec_wrap,
1009  des3_ctx_alloc,
1010  des3_ctx_free
1011 };
1012 
1013 const cipher_info_t des_ede_ecb_info = {
1017  "DES-EDE-ECB",
1018  8,
1019  0,
1020  8,
1021  &des_ede_info
1022 };
1023 
1024 #if defined(POLARSSL_CIPHER_MODE_CBC)
1025 const cipher_info_t des_ede_cbc_info = {
1029  "DES-EDE-CBC",
1030  8,
1031  0,
1032  8,
1033  &des_ede_info
1034 };
1035 #endif /* POLARSSL_CIPHER_MODE_CBC */
1036 
1037 const cipher_base_t des_ede3_info = {
1039  des3_crypt_ecb_wrap,
1040  des3_crypt_cbc_wrap,
1041  NULL,
1042  NULL,
1043  NULL,
1044  des3_set3key_enc_wrap,
1045  des3_set3key_dec_wrap,
1046  des3_ctx_alloc,
1047  des3_ctx_free
1048 };
1049 
1050 const cipher_info_t des_ede3_ecb_info = {
1054  "DES-EDE3-ECB",
1055  8,
1056  0,
1057  8,
1058  &des_ede3_info
1059 };
1060 #if defined(POLARSSL_CIPHER_MODE_CBC)
1061 const cipher_info_t des_ede3_cbc_info = {
1065  "DES-EDE3-CBC",
1066  8,
1067  0,
1068  8,
1069  &des_ede3_info
1070 };
1071 #endif /* POLARSSL_CIPHER_MODE_CBC */
1072 #endif /* POLARSSL_DES_C */
1073 
1074 #if defined(POLARSSL_BLOWFISH_C)
1075 
1076 static int blowfish_crypt_ecb_wrap( void *ctx, operation_t operation,
1077  const unsigned char *input, unsigned char *output )
1078 {
1079  return blowfish_crypt_ecb( (blowfish_context *) ctx, operation, input,
1080  output );
1081 }
1082 
1083 static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation,
1084  size_t length, unsigned char *iv, const unsigned char *input,
1085  unsigned char *output )
1086 {
1087 #if defined(POLARSSL_CIPHER_MODE_CBC)
1088  return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv,
1089  input, output );
1090 #else
1091  ((void) ctx);
1092  ((void) operation);
1093  ((void) length);
1094  ((void) iv);
1095  ((void) input);
1096  ((void) output);
1097 
1099 #endif /* POLARSSL_CIPHER_MODE_CBC */
1100 }
1101 
1102 static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation,
1103  size_t length, size_t *iv_off, unsigned char *iv,
1104  const unsigned char *input, unsigned char *output )
1105 {
1106 #if defined(POLARSSL_CIPHER_MODE_CFB)
1107  return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length,
1108  iv_off, iv, input, output );
1109 #else
1110  ((void) ctx);
1111  ((void) operation);
1112  ((void) length);
1113  ((void) iv_off);
1114  ((void) iv);
1115  ((void) input);
1116  ((void) output);
1117 
1119 #endif /* POLARSSL_CIPHER_MODE_CFB */
1120 }
1121 
1122 static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
1123  unsigned char *nonce_counter, unsigned char *stream_block,
1124  const unsigned char *input, unsigned char *output )
1125 {
1126 #if defined(POLARSSL_CIPHER_MODE_CTR)
1127  return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off,
1128  nonce_counter, stream_block, input, output );
1129 #else
1130  ((void) ctx);
1131  ((void) length);
1132  ((void) nc_off);
1133  ((void) nonce_counter);
1134  ((void) stream_block);
1135  ((void) input);
1136  ((void) output);
1137 
1139 #endif /* POLARSSL_CIPHER_MODE_CTR */
1140 }
1141 
1142 static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
1143  unsigned int key_length )
1144 {
1145  return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
1146 }
1147 
1148 static void * blowfish_ctx_alloc( void )
1149 {
1150  blowfish_context *ctx;
1151  ctx = (blowfish_context *) polarssl_malloc( sizeof( blowfish_context ) );
1152 
1153  if( ctx == NULL )
1154  return( NULL );
1155 
1156  blowfish_init( ctx );
1157 
1158  return( ctx );
1159 }
1160 
1161 static void blowfish_ctx_free( void *ctx )
1162 {
1163  blowfish_free( (blowfish_context *) ctx );
1164  polarssl_free( ctx );
1165 }
1166 
1167 const cipher_base_t blowfish_info = {
1169  blowfish_crypt_ecb_wrap,
1170  blowfish_crypt_cbc_wrap,
1171  blowfish_crypt_cfb64_wrap,
1172  blowfish_crypt_ctr_wrap,
1173  NULL,
1174  blowfish_setkey_wrap,
1175  blowfish_setkey_wrap,
1176  blowfish_ctx_alloc,
1177  blowfish_ctx_free
1178 };
1179 
1180 const cipher_info_t blowfish_ecb_info = {
1183  128,
1184  "BLOWFISH-ECB",
1185  8,
1187  8,
1188  &blowfish_info
1189 };
1190 
1191 #if defined(POLARSSL_CIPHER_MODE_CBC)
1192 const cipher_info_t blowfish_cbc_info = {
1195  128,
1196  "BLOWFISH-CBC",
1197  8,
1199  8,
1200  &blowfish_info
1201 };
1202 #endif /* POLARSSL_CIPHER_MODE_CBC */
1203 
1204 #if defined(POLARSSL_CIPHER_MODE_CFB)
1205 const cipher_info_t blowfish_cfb64_info = {
1208  128,
1209  "BLOWFISH-CFB64",
1210  8,
1212  8,
1213  &blowfish_info
1214 };
1215 #endif /* POLARSSL_CIPHER_MODE_CFB */
1216 
1217 #if defined(POLARSSL_CIPHER_MODE_CTR)
1218 const cipher_info_t blowfish_ctr_info = {
1221  128,
1222  "BLOWFISH-CTR",
1223  8,
1225  8,
1226  &blowfish_info
1227 };
1228 #endif /* POLARSSL_CIPHER_MODE_CTR */
1229 #endif /* POLARSSL_BLOWFISH_C */
1230 
1231 #if defined(POLARSSL_ARC4_C)
1232 static int arc4_crypt_stream_wrap( void *ctx, size_t length,
1233  const unsigned char *input,
1234  unsigned char *output )
1235 {
1236  return( arc4_crypt( (arc4_context *) ctx, length, input, output ) );
1237 }
1238 
1239 static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
1240  unsigned int key_length )
1241 {
1242  /* we get key_length in bits, arc4 expects it in bytes */
1243  if( key_length % 8 != 0 )
1245 
1246  arc4_setup( (arc4_context *) ctx, key, key_length / 8 );
1247  return( 0 );
1248 }
1249 
1250 static void * arc4_ctx_alloc( void )
1251 {
1252  arc4_context *ctx;
1253  ctx = (arc4_context *) polarssl_malloc( sizeof( arc4_context ) );
1254 
1255  if( ctx == NULL )
1256  return( NULL );
1257 
1258  arc4_init( ctx );
1259 
1260  return( ctx );
1261 }
1262 
1263 static void arc4_ctx_free( void *ctx )
1264 {
1265  arc4_free( (arc4_context *) ctx );
1266  polarssl_free( ctx );
1267 }
1268 
1269 const cipher_base_t arc4_base_info = {
1271  NULL,
1272  NULL,
1273  NULL,
1274  NULL,
1275  arc4_crypt_stream_wrap,
1276  arc4_setkey_wrap,
1277  arc4_setkey_wrap,
1278  arc4_ctx_alloc,
1279  arc4_ctx_free
1280 };
1281 
1282 const cipher_info_t arc4_128_info = {
1285  128,
1286  "ARC4-128",
1287  0,
1288  0,
1289  1,
1290  &arc4_base_info
1291 };
1292 #endif /* POLARSSL_ARC4_C */
1293 
1294 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
1295 static int null_crypt_stream( void *ctx, size_t length,
1296  const unsigned char *input,
1297  unsigned char *output )
1298 {
1299  ((void) ctx);
1300  memmove( output, input, length );
1301  return( 0 );
1302 }
1303 
1304 static int null_setkey( void *ctx, const unsigned char *key,
1305  unsigned int key_length )
1306 {
1307  ((void) ctx);
1308  ((void) key);
1309  ((void) key_length);
1310 
1311  return( 0 );
1312 }
1313 
1314 static void * null_ctx_alloc( void )
1315 {
1316  return( (void *) 1 );
1317 }
1318 
1319 static void null_ctx_free( void *ctx )
1320 {
1321  ((void) ctx);
1322 }
1323 
1324 const cipher_base_t null_base_info = {
1326  NULL,
1327  NULL,
1328  NULL,
1329  NULL,
1330  null_crypt_stream,
1331  null_setkey,
1332  null_setkey,
1333  null_ctx_alloc,
1334  null_ctx_free
1335 };
1336 
1337 const cipher_info_t null_cipher_info = {
1340  0,
1341  "NULL",
1342  0,
1343  0,
1344  1,
1345  &null_base_info
1346 };
1347 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
1348 
1350 {
1351 #if defined(POLARSSL_AES_C)
1352  { POLARSSL_CIPHER_AES_128_ECB, &aes_128_ecb_info },
1353  { POLARSSL_CIPHER_AES_192_ECB, &aes_192_ecb_info },
1354  { POLARSSL_CIPHER_AES_256_ECB, &aes_256_ecb_info },
1355 #if defined(POLARSSL_CIPHER_MODE_CBC)
1356  { POLARSSL_CIPHER_AES_128_CBC, &aes_128_cbc_info },
1357  { POLARSSL_CIPHER_AES_192_CBC, &aes_192_cbc_info },
1358  { POLARSSL_CIPHER_AES_256_CBC, &aes_256_cbc_info },
1359 #endif
1360 #if defined(POLARSSL_CIPHER_MODE_CFB)
1361  { POLARSSL_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
1362  { POLARSSL_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
1363  { POLARSSL_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
1364 #endif
1365 #if defined(POLARSSL_CIPHER_MODE_CTR)
1366  { POLARSSL_CIPHER_AES_128_CTR, &aes_128_ctr_info },
1367  { POLARSSL_CIPHER_AES_192_CTR, &aes_192_ctr_info },
1368  { POLARSSL_CIPHER_AES_256_CTR, &aes_256_ctr_info },
1369 #endif
1370 #if defined(POLARSSL_GCM_C)
1371  { POLARSSL_CIPHER_AES_128_GCM, &aes_128_gcm_info },
1372  { POLARSSL_CIPHER_AES_192_GCM, &aes_192_gcm_info },
1373  { POLARSSL_CIPHER_AES_256_GCM, &aes_256_gcm_info },
1374 #endif
1375 #if defined(POLARSSL_CCM_C)
1376  { POLARSSL_CIPHER_AES_128_CCM, &aes_128_ccm_info },
1377  { POLARSSL_CIPHER_AES_192_CCM, &aes_192_ccm_info },
1378  { POLARSSL_CIPHER_AES_256_CCM, &aes_256_ccm_info },
1379 #endif
1380 #endif /* POLARSSL_AES_C */
1381 
1382 #if defined(POLARSSL_ARC4_C)
1383  { POLARSSL_CIPHER_ARC4_128, &arc4_128_info },
1384 #endif
1385 
1386 #if defined(POLARSSL_BLOWFISH_C)
1387  { POLARSSL_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
1388 #if defined(POLARSSL_CIPHER_MODE_CBC)
1389  { POLARSSL_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
1390 #endif
1391 #if defined(POLARSSL_CIPHER_MODE_CFB)
1392  { POLARSSL_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
1393 #endif
1394 #if defined(POLARSSL_CIPHER_MODE_CTR)
1395  { POLARSSL_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
1396 #endif
1397 #endif /* POLARSSL_BLOWFISH_C */
1398 
1399 #if defined(POLARSSL_CAMELLIA_C)
1400  { POLARSSL_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
1401  { POLARSSL_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
1402  { POLARSSL_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
1403 #if defined(POLARSSL_CIPHER_MODE_CBC)
1404  { POLARSSL_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
1405  { POLARSSL_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
1406  { POLARSSL_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
1407 #endif
1408 #if defined(POLARSSL_CIPHER_MODE_CFB)
1409  { POLARSSL_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
1410  { POLARSSL_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
1411  { POLARSSL_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
1412 #endif
1413 #if defined(POLARSSL_CIPHER_MODE_CTR)
1414  { POLARSSL_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
1415  { POLARSSL_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
1416  { POLARSSL_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
1417 #endif
1418 #if defined(POLARSSL_GCM_C)
1419  { POLARSSL_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
1420  { POLARSSL_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
1421  { POLARSSL_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
1422 #endif
1423 #if defined(POLARSSL_CCM_C)
1424  { POLARSSL_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
1425  { POLARSSL_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
1426  { POLARSSL_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
1427 #endif
1428 #endif /* POLARSSL_CAMELLIA_C */
1429 
1430 #if defined(POLARSSL_DES_C)
1431  { POLARSSL_CIPHER_DES_ECB, &des_ecb_info },
1432  { POLARSSL_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
1433  { POLARSSL_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
1434 #if defined(POLARSSL_CIPHER_MODE_CBC)
1435  { POLARSSL_CIPHER_DES_CBC, &des_cbc_info },
1436  { POLARSSL_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
1437  { POLARSSL_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
1438 #endif
1439 #endif /* POLARSSL_DES_C */
1440 
1441 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
1442  { POLARSSL_CIPHER_NULL, &null_cipher_info },
1443 #endif /* POLARSSL_CIPHER_NULL_CIPHER */
1444 
1445  { 0, NULL }
1446 };
1447 
1448 #define NUM_CIPHERS sizeof cipher_definitions / sizeof cipher_definitions[0]
1449 int supported_ciphers[NUM_CIPHERS];
1450 
1451 #endif /* POLARSSL_CIPHER_C */