PolarSSL v1.2.8
cipher_wrap.c
Go to the documentation of this file.
1 
30 #include "polarssl/config.h"
31 
32 #if defined(POLARSSL_CIPHER_C)
33 
34 #include "polarssl/cipher_wrap.h"
35 
36 #if defined(POLARSSL_AES_C)
37 #include "polarssl/aes.h"
38 #endif
39 
40 #if defined(POLARSSL_CAMELLIA_C)
41 #include "polarssl/camellia.h"
42 #endif
43 
44 #if defined(POLARSSL_DES_C)
45 #include "polarssl/des.h"
46 #endif
47 
48 #if defined(POLARSSL_BLOWFISH_C)
49 #include "polarssl/blowfish.h"
50 #endif
51 
52 #include <stdlib.h>
53 
54 #if defined(POLARSSL_AES_C)
55 
56 int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
57  unsigned char *iv, const unsigned char *input, unsigned char *output )
58 {
59  return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
60 }
61 
62 int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
63  size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
64 {
65 #if defined(POLARSSL_CIPHER_MODE_CFB)
66  return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
67 #else
68  ((void) ctx);
69  ((void) operation);
70  ((void) length);
71  ((void) iv_off);
72  ((void) iv);
73  ((void) input);
74  ((void) output);
75 
77 #endif
78 }
79 
80 int aes_crypt_ctr_wrap( void *ctx, size_t length,
81  size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
82  const unsigned char *input, unsigned char *output )
83 {
84 #if defined(POLARSSL_CIPHER_MODE_CTR)
85  return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
86  stream_block, input, output );
87 #else
88  ((void) ctx);
89  ((void) length);
90  ((void) nc_off);
91  ((void) nonce_counter);
92  ((void) stream_block);
93  ((void) input);
94  ((void) output);
95 
97 #endif
98 }
99 
100 int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
101 {
102  return aes_setkey_dec( (aes_context *) ctx, key, key_length );
103 }
104 
105 int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
106 {
107  return aes_setkey_enc( (aes_context *) ctx, key, key_length );
108 }
109 
110 static void * aes_ctx_alloc( void )
111 {
112  return malloc( sizeof( aes_context ) );
113 }
114 
115 static void aes_ctx_free( void *ctx )
116 {
117  free( ctx );
118 }
119 
120 const cipher_base_t aes_info = {
122  aes_crypt_cbc_wrap,
123  aes_crypt_cfb128_wrap,
124  aes_crypt_ctr_wrap,
125  aes_setkey_enc_wrap,
126  aes_setkey_dec_wrap,
127  aes_ctx_alloc,
128  aes_ctx_free
129 };
130 
134  128,
135  "AES-128-CBC",
136  16,
137  16,
138  &aes_info
139 };
140 
144  192,
145  "AES-192-CBC",
146  16,
147  16,
148  &aes_info
149 };
150 
154  256,
155  "AES-256-CBC",
156  16,
157  16,
158  &aes_info
159 };
160 
161 #if defined(POLARSSL_CIPHER_MODE_CFB)
165  128,
166  "AES-128-CFB128",
167  16,
168  16,
169  &aes_info
170 };
171 
175  192,
176  "AES-192-CFB128",
177  16,
178  16,
179  &aes_info
180 };
181 
185  256,
186  "AES-256-CFB128",
187  16,
188  16,
189  &aes_info
190 };
191 #endif /* POLARSSL_CIPHER_MODE_CFB */
192 
193 #if defined(POLARSSL_CIPHER_MODE_CTR)
197  128,
198  "AES-128-CTR",
199  16,
200  16,
201  &aes_info
202 };
203 
207  192,
208  "AES-192-CTR",
209  16,
210  16,
211  &aes_info
212 };
213 
217  256,
218  "AES-256-CTR",
219  16,
220  16,
221  &aes_info
222 };
223 #endif /* POLARSSL_CIPHER_MODE_CTR */
224 
225 #endif
226 
227 #if defined(POLARSSL_CAMELLIA_C)
228 
229 int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
230  unsigned char *iv, const unsigned char *input, unsigned char *output )
231 {
232  return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
233 }
234 
235 int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
236  size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
237 {
238 #if defined(POLARSSL_CIPHER_MODE_CFB)
239  return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
240 #else
241  ((void) ctx);
242  ((void) operation);
243  ((void) length);
244  ((void) iv_off);
245  ((void) iv);
246  ((void) input);
247  ((void) output);
248 
250 #endif
251 }
252 
253 int camellia_crypt_ctr_wrap( void *ctx, size_t length,
254  size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
255  const unsigned char *input, unsigned char *output )
256 {
257 #if defined(POLARSSL_CIPHER_MODE_CTR)
258  return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
259  stream_block, input, output );
260 #else
261  ((void) ctx);
262  ((void) length);
263  ((void) nc_off);
264  ((void) nonce_counter);
265  ((void) stream_block);
266  ((void) input);
267  ((void) output);
268 
270 #endif
271 }
272 
273 int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
274 {
275  return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
276 }
277 
278 int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
279 {
280  return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
281 }
282 
283 static void * camellia_ctx_alloc( void )
284 {
285  return malloc( sizeof( camellia_context ) );
286 }
287 
288 static void camellia_ctx_free( void *ctx )
289 {
290  free( ctx );
291 }
292 
293 const cipher_base_t camellia_info = {
295  camellia_crypt_cbc_wrap,
296  camellia_crypt_cfb128_wrap,
297  camellia_crypt_ctr_wrap,
298  camellia_setkey_enc_wrap,
299  camellia_setkey_dec_wrap,
300  camellia_ctx_alloc,
301  camellia_ctx_free
302 };
303 
307  128,
308  "CAMELLIA-128-CBC",
309  16,
310  16,
311  &camellia_info
312 };
313 
317  192,
318  "CAMELLIA-192-CBC",
319  16,
320  16,
321  &camellia_info
322 };
323 
327  256,
328  "CAMELLIA-256-CBC",
329  16,
330  16,
331  &camellia_info
332 };
333 
334 #if defined(POLARSSL_CIPHER_MODE_CFB)
338  128,
339  "CAMELLIA-128-CFB128",
340  16,
341  16,
342  &camellia_info
343 };
344 
348  192,
349  "CAMELLIA-192-CFB128",
350  16,
351  16,
352  &camellia_info
353 };
354 
358  256,
359  "CAMELLIA-256-CFB128",
360  16,
361  16,
362  &camellia_info
363 };
364 #endif /* POLARSSL_CIPHER_MODE_CFB */
365 
366 #if defined(POLARSSL_CIPHER_MODE_CTR)
370  128,
371  "CAMELLIA-128-CTR",
372  16,
373  16,
374  &camellia_info
375 };
376 
380  192,
381  "CAMELLIA-192-CTR",
382  16,
383  16,
384  &camellia_info
385 };
386 
390  256,
391  "CAMELLIA-256-CTR",
392  16,
393  16,
394  &camellia_info
395 };
396 #endif /* POLARSSL_CIPHER_MODE_CTR */
397 
398 #endif
399 
400 #if defined(POLARSSL_DES_C)
401 
402 int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
403  unsigned char *iv, const unsigned char *input, unsigned char *output )
404 {
405  return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
406 }
407 
408 int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
409  unsigned char *iv, const unsigned char *input, unsigned char *output )
410 {
411  return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
412 }
413 
414 int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
415  size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
416 {
417  ((void) ctx);
418  ((void) operation);
419  ((void) length);
420  ((void) iv_off);
421  ((void) iv);
422  ((void) input);
423  ((void) output);
424 
426 }
427 
428 int des_crypt_ctr_wrap( void *ctx, size_t length,
429  size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
430  const unsigned char *input, unsigned char *output )
431 {
432  ((void) ctx);
433  ((void) length);
434  ((void) nc_off);
435  ((void) nonce_counter);
436  ((void) stream_block);
437  ((void) input);
438  ((void) output);
439 
441 }
442 
443 
444 int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
445 {
446  ((void) key_length);
447 
448  return des_setkey_dec( (des_context *) ctx, key );
449 }
450 
451 int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
452 {
453  ((void) key_length);
454 
455  return des_setkey_enc( (des_context *) ctx, key );
456 }
457 
458 int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
459 {
460  ((void) key_length);
461 
462  return des3_set2key_dec( (des3_context *) ctx, key );
463 }
464 
465 int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
466 {
467  ((void) key_length);
468 
469  return des3_set2key_enc( (des3_context *) ctx, key );
470 }
471 
472 int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
473 {
474  ((void) key_length);
475 
476  return des3_set3key_dec( (des3_context *) ctx, key );
477 }
478 
479 int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
480 {
481  ((void) key_length);
482 
483  return des3_set3key_enc( (des3_context *) ctx, key );
484 }
485 
486 static void * des_ctx_alloc( void )
487 {
488  return malloc( sizeof( des_context ) );
489 }
490 
491 static void * des3_ctx_alloc( void )
492 {
493  return malloc( sizeof( des3_context ) );
494 }
495 
496 static void des_ctx_free( void *ctx )
497 {
498  free( ctx );
499 }
500 
501 const cipher_base_t des_info = {
503  des_crypt_cbc_wrap,
504  des_crypt_cfb128_wrap,
505  des_crypt_ctr_wrap,
506  des_setkey_enc_wrap,
507  des_setkey_dec_wrap,
508  des_ctx_alloc,
509  des_ctx_free
510 };
511 
512 const cipher_info_t des_cbc_info = {
516  "DES-CBC",
517  8,
518  8,
519  &des_info
520 };
521 
522 const cipher_base_t des_ede_info = {
524  des3_crypt_cbc_wrap,
525  des_crypt_cfb128_wrap,
526  des_crypt_ctr_wrap,
527  des3_set2key_enc_wrap,
528  des3_set2key_dec_wrap,
529  des3_ctx_alloc,
530  des_ctx_free
531 };
532 
537  "DES-EDE-CBC",
538  8,
539  8,
540  &des_ede_info
541 };
542 
543 const cipher_base_t des_ede3_info = {
545  des3_crypt_cbc_wrap,
546  des_crypt_cfb128_wrap,
547  des_crypt_ctr_wrap,
548  des3_set3key_enc_wrap,
549  des3_set3key_dec_wrap,
550  des3_ctx_alloc,
551  des_ctx_free
552 };
553 
558  "DES-EDE3-CBC",
559  8,
560  8,
561  &des_ede3_info
562 };
563 #endif
564 
565 #if defined(POLARSSL_BLOWFISH_C)
566 
567 int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
568  unsigned char *iv, const unsigned char *input, unsigned char *output )
569 {
570  return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output );
571 }
572 
573 int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length,
574  size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
575 {
576 #if defined(POLARSSL_CIPHER_MODE_CFB)
577  return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output );
578 #else
579  ((void) ctx);
580  ((void) operation);
581  ((void) length);
582  ((void) iv_off);
583  ((void) iv);
584  ((void) input);
585  ((void) output);
586 
588 #endif
589 }
590 
591 int blowfish_crypt_ctr_wrap( void *ctx, size_t length,
592  size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
593  const unsigned char *input, unsigned char *output )
594 {
595 #if defined(POLARSSL_CIPHER_MODE_CTR)
596  return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter,
597  stream_block, input, output );
598 #else
599  ((void) ctx);
600  ((void) length);
601  ((void) nc_off);
602  ((void) nonce_counter);
603  ((void) stream_block);
604  ((void) input);
605  ((void) output);
606 
608 #endif
609 }
610 
611 int blowfish_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
612 {
613  return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
614 }
615 
616 int blowfish_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
617 {
618  return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
619 }
620 
621 static void * blowfish_ctx_alloc( void )
622 {
623  return malloc( sizeof( blowfish_context ) );
624 }
625 
626 static void blowfish_ctx_free( void *ctx )
627 {
628  free( ctx );
629 }
630 
631 const cipher_base_t blowfish_info = {
633  blowfish_crypt_cbc_wrap,
634  blowfish_crypt_cfb64_wrap,
635  blowfish_crypt_ctr_wrap,
636  blowfish_setkey_enc_wrap,
637  blowfish_setkey_dec_wrap,
638  blowfish_ctx_alloc,
639  blowfish_ctx_free
640 };
641 
645  128,
646  "BLOWFISH-CBC",
647  8,
648  8,
649  &blowfish_info
650 };
651 
652 #if defined(POLARSSL_CIPHER_MODE_CFB)
656  128,
657  "BLOWFISH-CFB64",
658  8,
659  8,
660  &blowfish_info
661 };
662 #endif /* POLARSSL_CIPHER_MODE_CFB */
663 
664 #if defined(POLARSSL_CIPHER_MODE_CTR)
668  128,
669  "BLOWFISH-CTR",
670  8,
671  8,
672  &blowfish_info
673 };
674 #endif /* POLARSSL_CIPHER_MODE_CTR */
675 #endif /* POLARSSL_BLOWFISH_C */
676 
677 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
678 static void * null_ctx_alloc( void )
679 {
680  return (void *) 1;
681 }
682 
683 
684 static void null_ctx_free( void *ctx )
685 {
686  ((void) ctx);
687 }
688 
689 const cipher_base_t null_base_info = {
691  NULL,
692  NULL,
693  NULL,
694  NULL,
695  NULL,
696  null_ctx_alloc,
697  null_ctx_free
698 };
699 
700 const cipher_info_t null_cipher_info = {
703  0,
704  "NULL",
705  1,
706  1,
707  &null_base_info
708 };
709 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
710 
711 #endif