PolarSSL v1.1.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 #include "polarssl/aes.h"
36 #include "polarssl/camellia.h"
37 #include "polarssl/des.h"
38 
39 #include <stdlib.h>
40 
41 #if defined(POLARSSL_AES_C)
42 
43 int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
44  unsigned char *iv, const unsigned char *input, unsigned char *output )
45 {
46  return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
47 }
48 
49 int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
50  size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
51 {
52 #if defined(POLARSSL_CIPHER_MODE_CFB)
53  return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
54 #else
55  ((void) ctx);
56  ((void) operation);
57  ((void) length);
58  ((void) iv_off);
59  ((void) iv);
60  ((void) input);
61  ((void) output);
62 
64 #endif
65 }
66 
67 int aes_crypt_ctr_wrap( void *ctx, size_t length,
68  size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
69  const unsigned char *input, unsigned char *output )
70 {
71 #if defined(POLARSSL_CIPHER_MODE_CTR)
72  return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
73  stream_block, input, output );
74 #else
75  ((void) ctx);
76  ((void) length);
77  ((void) nc_off);
78  ((void) nonce_counter);
79  ((void) stream_block);
80  ((void) input);
81  ((void) output);
82 
84 #endif
85 }
86 
87 int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
88 {
89  return aes_setkey_dec( (aes_context *) ctx, key, key_length );
90 }
91 
92 int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
93 {
94  return aes_setkey_enc( (aes_context *) ctx, key, key_length );
95 }
96 
97 static void * aes_ctx_alloc( void )
98 {
99  return malloc( sizeof( aes_context ) );
100 }
101 
102 static void aes_ctx_free( void *ctx )
103 {
104  free( ctx );
105 }
106 
107 const cipher_base_t aes_info = {
109  aes_crypt_cbc_wrap,
110  aes_crypt_cfb128_wrap,
111  aes_crypt_ctr_wrap,
112  aes_setkey_enc_wrap,
113  aes_setkey_dec_wrap,
114  aes_ctx_alloc,
115  aes_ctx_free
116 };
117 
121  128,
122  "AES-128-CBC",
123  16,
124  16,
125  &aes_info
126 };
127 
131  192,
132  "AES-192-CBC",
133  16,
134  16,
135  &aes_info
136 };
137 
141  256,
142  "AES-256-CBC",
143  16,
144  16,
145  &aes_info
146 };
147 
148 #if defined(POLARSSL_CIPHER_MODE_CFB)
152  128,
153  "AES-128-CFB128",
154  16,
155  16,
156  &aes_info
157 };
158 
162  192,
163  "AES-192-CFB128",
164  16,
165  16,
166  &aes_info
167 };
168 
172  256,
173  "AES-256-CFB128",
174  16,
175  16,
176  &aes_info
177 };
178 #endif /* POLARSSL_CIPHER_MODE_CFB */
179 
180 #if defined(POLARSSL_CIPHER_MODE_CTR)
184  128,
185  "AES-128-CTR",
186  16,
187  16,
188  &aes_info
189 };
190 
194  192,
195  "AES-192-CTR",
196  16,
197  16,
198  &aes_info
199 };
200 
204  256,
205  "AES-256-CTR",
206  16,
207  16,
208  &aes_info
209 };
210 #endif /* POLARSSL_CIPHER_MODE_CTR */
211 
212 #endif
213 
214 #if defined(POLARSSL_CAMELLIA_C)
215 
216 int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
217  unsigned char *iv, const unsigned char *input, unsigned char *output )
218 {
219  return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
220 }
221 
222 int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
223  size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
224 {
225 #if defined(POLARSSL_CIPHER_MODE_CFB)
226  return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
227 #else
228  ((void) ctx);
229  ((void) operation);
230  ((void) length);
231  ((void) iv_off);
232  ((void) iv);
233  ((void) input);
234  ((void) output);
235 
237 #endif
238 }
239 
240 int camellia_crypt_ctr_wrap( void *ctx, size_t length,
241  size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
242  const unsigned char *input, unsigned char *output )
243 {
244 #if defined(POLARSSL_CIPHER_MODE_CTR)
245  return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
246  stream_block, input, output );
247 #else
248  ((void) ctx);
249  ((void) length);
250  ((void) nc_off);
251  ((void) nonce_counter);
252  ((void) stream_block);
253  ((void) input);
254  ((void) output);
255 
257 #endif
258 }
259 
260 int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
261 {
262  return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
263 }
264 
265 int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
266 {
267  return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
268 }
269 
270 static void * camellia_ctx_alloc( void )
271 {
272  return malloc( sizeof( camellia_context ) );
273 }
274 
275 static void camellia_ctx_free( void *ctx )
276 {
277  free( ctx );
278 }
279 
280 const cipher_base_t camellia_info = {
282  camellia_crypt_cbc_wrap,
283  camellia_crypt_cfb128_wrap,
284  camellia_crypt_ctr_wrap,
285  camellia_setkey_enc_wrap,
286  camellia_setkey_dec_wrap,
287  camellia_ctx_alloc,
288  camellia_ctx_free
289 };
290 
294  128,
295  "CAMELLIA-128-CBC",
296  16,
297  16,
298  &camellia_info
299 };
300 
304  192,
305  "CAMELLIA-192-CBC",
306  16,
307  16,
308  &camellia_info
309 };
310 
314  256,
315  "CAMELLIA-256-CBC",
316  16,
317  16,
318  &camellia_info
319 };
320 
321 #if defined(POLARSSL_CIPHER_MODE_CFB)
325  128,
326  "CAMELLIA-128-CFB128",
327  16,
328  16,
329  &camellia_info
330 };
331 
335  192,
336  "CAMELLIA-192-CFB128",
337  16,
338  16,
339  &camellia_info
340 };
341 
345  256,
346  "CAMELLIA-256-CFB128",
347  16,
348  16,
349  &camellia_info
350 };
351 #endif /* POLARSSL_CIPHER_MODE_CFB */
352 
353 #if defined(POLARSSL_CIPHER_MODE_CTR)
357  128,
358  "CAMELLIA-128-CTR",
359  16,
360  16,
361  &camellia_info
362 };
363 
367  192,
368  "CAMELLIA-192-CTR",
369  16,
370  16,
371  &camellia_info
372 };
373 
377  256,
378  "CAMELLIA-256-CTR",
379  16,
380  16,
381  &camellia_info
382 };
383 #endif /* POLARSSL_CIPHER_MODE_CTR */
384 
385 #endif
386 
387 #if defined(POLARSSL_DES_C)
388 
389 int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
390  unsigned char *iv, const unsigned char *input, unsigned char *output )
391 {
392  return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
393 }
394 
395 int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
396  unsigned char *iv, const unsigned char *input, unsigned char *output )
397 {
398  return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
399 }
400 
401 int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
402  size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
403 {
404  ((void) ctx);
405  ((void) operation);
406  ((void) length);
407  ((void) iv_off);
408  ((void) iv);
409  ((void) input);
410  ((void) output);
411 
413 }
414 
415 int des_crypt_ctr_wrap( void *ctx, size_t length,
416  size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
417  const unsigned char *input, unsigned char *output )
418 {
419  ((void) ctx);
420  ((void) length);
421  ((void) nc_off);
422  ((void) nonce_counter);
423  ((void) stream_block);
424  ((void) input);
425  ((void) output);
426 
428 }
429 
430 
431 int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
432 {
433  ((void) key_length);
434 
435  return des_setkey_dec( (des_context *) ctx, key );
436 }
437 
438 int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
439 {
440  ((void) key_length);
441 
442  return des_setkey_enc( (des_context *) ctx, key );
443 }
444 
445 int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
446 {
447  ((void) key_length);
448 
449  return des3_set2key_dec( (des3_context *) ctx, key );
450 }
451 
452 int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
453 {
454  ((void) key_length);
455 
456  return des3_set2key_enc( (des3_context *) ctx, key );
457 }
458 
459 int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
460 {
461  ((void) key_length);
462 
463  return des3_set3key_dec( (des3_context *) ctx, key );
464 }
465 
466 int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
467 {
468  ((void) key_length);
469 
470  return des3_set3key_enc( (des3_context *) ctx, key );
471 }
472 
473 static void * des_ctx_alloc( void )
474 {
475  return malloc( sizeof( des_context ) );
476 }
477 
478 static void * des3_ctx_alloc( void )
479 {
480  return malloc( sizeof( des3_context ) );
481 }
482 
483 static void des_ctx_free( void *ctx )
484 {
485  free( ctx );
486 }
487 
488 const cipher_base_t des_info = {
490  des_crypt_cbc_wrap,
491  des_crypt_cfb128_wrap,
492  des_crypt_ctr_wrap,
493  des_setkey_enc_wrap,
494  des_setkey_dec_wrap,
495  des_ctx_alloc,
496  des_ctx_free
497 };
498 
499 const cipher_info_t des_cbc_info = {
503  "DES-CBC",
504  8,
505  8,
506  &des_info
507 };
508 
509 const cipher_base_t des_ede_info = {
511  des3_crypt_cbc_wrap,
512  des_crypt_cfb128_wrap,
513  des_crypt_ctr_wrap,
514  des3_set2key_enc_wrap,
515  des3_set2key_dec_wrap,
516  des3_ctx_alloc,
517  des_ctx_free
518 };
519 
524  "DES-EDE-CBC",
525  16,
526  16,
527  &des_ede_info
528 };
529 
530 const cipher_base_t des_ede3_info = {
532  des3_crypt_cbc_wrap,
533  des_crypt_cfb128_wrap,
534  des_crypt_ctr_wrap,
535  des3_set3key_enc_wrap,
536  des3_set3key_dec_wrap,
537  des3_ctx_alloc,
538  des_ctx_free
539 };
540 
545  "DES-EDE3-CBC",
546  8,
547  8,
548  &des_ede3_info
549 };
550 #endif
551 
552 #endif