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