00001
00030 #include "polarssl/config.h"
00031
00032 #if defined(POLARSSL_CIPHER_C)
00033
00034 #include "polarssl/cipher_wrap.h"
00035
00036 #if defined(POLARSSL_AES_C)
00037 #include "polarssl/aes.h"
00038 #endif
00039
00040 #if defined(POLARSSL_ARC4_C)
00041 #include "polarssl/arc4.h"
00042 #endif
00043
00044 #if defined(POLARSSL_CAMELLIA_C)
00045 #include "polarssl/camellia.h"
00046 #endif
00047
00048 #if defined(POLARSSL_DES_C)
00049 #include "polarssl/des.h"
00050 #endif
00051
00052 #if defined(POLARSSL_BLOWFISH_C)
00053 #include "polarssl/blowfish.h"
00054 #endif
00055
00056 #if defined(POLARSSL_GCM_C)
00057 #include "polarssl/gcm.h"
00058 #endif
00059
00060 #if defined(POLARSSL_MEMORY_C)
00061 #include "polarssl/memory.h"
00062 #else
00063 #define polarssl_malloc malloc
00064 #define polarssl_free free
00065 #endif
00066
00067 #include <stdlib.h>
00068
00069 #if defined(POLARSSL_GCM_C)
00070
00071 static void *gcm_ctx_alloc( void )
00072 {
00073 return polarssl_malloc( sizeof( gcm_context ) );
00074 }
00075
00076 static void gcm_ctx_free( void *ctx )
00077 {
00078 gcm_free( ctx );
00079 polarssl_free( ctx );
00080 }
00081 #endif
00082
00083 #if defined(POLARSSL_AES_C)
00084
00085 static int aes_crypt_ecb_wrap( void *ctx, operation_t operation,
00086 const unsigned char *input, unsigned char *output )
00087 {
00088 return aes_crypt_ecb( (aes_context *) ctx, operation, input, output );
00089 }
00090
00091 static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
00092 unsigned char *iv, const unsigned char *input, unsigned char *output )
00093 {
00094 #if defined(POLARSSL_CIPHER_MODE_CBC)
00095 return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
00096 #else
00097 ((void) ctx);
00098 ((void) operation);
00099 ((void) length);
00100 ((void) iv);
00101 ((void) input);
00102 ((void) output);
00103
00104 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
00105 #endif
00106 }
00107
00108 static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
00109 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
00110 {
00111 #if defined(POLARSSL_CIPHER_MODE_CFB)
00112 return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
00113 #else
00114 ((void) ctx);
00115 ((void) operation);
00116 ((void) length);
00117 ((void) iv_off);
00118 ((void) iv);
00119 ((void) input);
00120 ((void) output);
00121
00122 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
00123 #endif
00124 }
00125
00126 static int aes_crypt_ctr_wrap( void *ctx, size_t length,
00127 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
00128 const unsigned char *input, unsigned char *output )
00129 {
00130 #if defined(POLARSSL_CIPHER_MODE_CTR)
00131 return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
00132 stream_block, input, output );
00133 #else
00134 ((void) ctx);
00135 ((void) length);
00136 ((void) nc_off);
00137 ((void) nonce_counter);
00138 ((void) stream_block);
00139 ((void) input);
00140 ((void) output);
00141
00142 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
00143 #endif
00144 }
00145
00146 static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
00147 {
00148 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
00149 }
00150
00151 static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
00152 {
00153 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
00154 }
00155
00156 static void * aes_ctx_alloc( void )
00157 {
00158 return polarssl_malloc( sizeof( aes_context ) );
00159 }
00160
00161 static void aes_ctx_free( void *ctx )
00162 {
00163 polarssl_free( ctx );
00164 }
00165
00166 const cipher_base_t aes_info = {
00167 POLARSSL_CIPHER_ID_AES,
00168 aes_crypt_ecb_wrap,
00169 aes_crypt_cbc_wrap,
00170 aes_crypt_cfb128_wrap,
00171 aes_crypt_ctr_wrap,
00172 NULL,
00173 aes_setkey_enc_wrap,
00174 aes_setkey_dec_wrap,
00175 aes_ctx_alloc,
00176 aes_ctx_free
00177 };
00178
00179 const cipher_info_t aes_128_ecb_info = {
00180 POLARSSL_CIPHER_AES_128_ECB,
00181 POLARSSL_MODE_ECB,
00182 128,
00183 "AES-128-ECB",
00184 16,
00185 0,
00186 16,
00187 &aes_info
00188 };
00189
00190 const cipher_info_t aes_192_ecb_info = {
00191 POLARSSL_CIPHER_AES_192_ECB,
00192 POLARSSL_MODE_ECB,
00193 192,
00194 "AES-192-ECB",
00195 16,
00196 0,
00197 16,
00198 &aes_info
00199 };
00200
00201 const cipher_info_t aes_256_ecb_info = {
00202 POLARSSL_CIPHER_AES_256_ECB,
00203 POLARSSL_MODE_ECB,
00204 256,
00205 "AES-256-ECB",
00206 16,
00207 0,
00208 16,
00209 &aes_info
00210 };
00211
00212 #if defined(POLARSSL_CIPHER_MODE_CBC)
00213 const cipher_info_t aes_128_cbc_info = {
00214 POLARSSL_CIPHER_AES_128_CBC,
00215 POLARSSL_MODE_CBC,
00216 128,
00217 "AES-128-CBC",
00218 16,
00219 0,
00220 16,
00221 &aes_info
00222 };
00223
00224 const cipher_info_t aes_192_cbc_info = {
00225 POLARSSL_CIPHER_AES_192_CBC,
00226 POLARSSL_MODE_CBC,
00227 192,
00228 "AES-192-CBC",
00229 16,
00230 0,
00231 16,
00232 &aes_info
00233 };
00234
00235 const cipher_info_t aes_256_cbc_info = {
00236 POLARSSL_CIPHER_AES_256_CBC,
00237 POLARSSL_MODE_CBC,
00238 256,
00239 "AES-256-CBC",
00240 16,
00241 0,
00242 16,
00243 &aes_info
00244 };
00245 #endif
00246
00247 #if defined(POLARSSL_CIPHER_MODE_CFB)
00248 const cipher_info_t aes_128_cfb128_info = {
00249 POLARSSL_CIPHER_AES_128_CFB128,
00250 POLARSSL_MODE_CFB,
00251 128,
00252 "AES-128-CFB128",
00253 16,
00254 0,
00255 16,
00256 &aes_info
00257 };
00258
00259 const cipher_info_t aes_192_cfb128_info = {
00260 POLARSSL_CIPHER_AES_192_CFB128,
00261 POLARSSL_MODE_CFB,
00262 192,
00263 "AES-192-CFB128",
00264 16,
00265 0,
00266 16,
00267 &aes_info
00268 };
00269
00270 const cipher_info_t aes_256_cfb128_info = {
00271 POLARSSL_CIPHER_AES_256_CFB128,
00272 POLARSSL_MODE_CFB,
00273 256,
00274 "AES-256-CFB128",
00275 16,
00276 0,
00277 16,
00278 &aes_info
00279 };
00280 #endif
00281
00282 #if defined(POLARSSL_CIPHER_MODE_CTR)
00283 const cipher_info_t aes_128_ctr_info = {
00284 POLARSSL_CIPHER_AES_128_CTR,
00285 POLARSSL_MODE_CTR,
00286 128,
00287 "AES-128-CTR",
00288 16,
00289 0,
00290 16,
00291 &aes_info
00292 };
00293
00294 const cipher_info_t aes_192_ctr_info = {
00295 POLARSSL_CIPHER_AES_192_CTR,
00296 POLARSSL_MODE_CTR,
00297 192,
00298 "AES-192-CTR",
00299 16,
00300 0,
00301 16,
00302 &aes_info
00303 };
00304
00305 const cipher_info_t aes_256_ctr_info = {
00306 POLARSSL_CIPHER_AES_256_CTR,
00307 POLARSSL_MODE_CTR,
00308 256,
00309 "AES-256-CTR",
00310 16,
00311 0,
00312 16,
00313 &aes_info
00314 };
00315 #endif
00316
00317 #if defined(POLARSSL_GCM_C)
00318 static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
00319 {
00320 return gcm_init( (gcm_context *) ctx, POLARSSL_CIPHER_ID_AES,
00321 key, key_length );
00322 }
00323
00324 const cipher_base_t gcm_aes_info = {
00325 POLARSSL_CIPHER_ID_AES,
00326 NULL,
00327 NULL,
00328 NULL,
00329 NULL,
00330 NULL,
00331 gcm_aes_setkey_wrap,
00332 gcm_aes_setkey_wrap,
00333 gcm_ctx_alloc,
00334 gcm_ctx_free,
00335 };
00336
00337 const cipher_info_t aes_128_gcm_info = {
00338 POLARSSL_CIPHER_AES_128_GCM,
00339 POLARSSL_MODE_GCM,
00340 128,
00341 "AES-128-GCM",
00342 12,
00343 1,
00344 16,
00345 &gcm_aes_info
00346 };
00347
00348 const cipher_info_t aes_192_gcm_info = {
00349 POLARSSL_CIPHER_AES_192_GCM,
00350 POLARSSL_MODE_GCM,
00351 192,
00352 "AES-192-GCM",
00353 12,
00354 1,
00355 16,
00356 &gcm_aes_info
00357 };
00358
00359 const cipher_info_t aes_256_gcm_info = {
00360 POLARSSL_CIPHER_AES_256_GCM,
00361 POLARSSL_MODE_GCM,
00362 256,
00363 "AES-256-GCM",
00364 12,
00365 1,
00366 16,
00367 &gcm_aes_info
00368 };
00369 #endif
00370
00371 #endif
00372
00373 #if defined(POLARSSL_CAMELLIA_C)
00374
00375 static int camellia_crypt_ecb_wrap( void *ctx, operation_t operation,
00376 const unsigned char *input, unsigned char *output )
00377 {
00378 return camellia_crypt_ecb( (camellia_context *) ctx, operation, input, output );
00379 }
00380
00381 static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
00382 unsigned char *iv, const unsigned char *input, unsigned char *output )
00383 {
00384 #if defined(POLARSSL_CIPHER_MODE_CBC)
00385 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
00386 #else
00387 ((void) ctx);
00388 ((void) operation);
00389 ((void) length);
00390 ((void) iv);
00391 ((void) input);
00392 ((void) output);
00393
00394 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
00395 #endif
00396 }
00397
00398 static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
00399 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
00400 {
00401 #if defined(POLARSSL_CIPHER_MODE_CFB)
00402 return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
00403 #else
00404 ((void) ctx);
00405 ((void) operation);
00406 ((void) length);
00407 ((void) iv_off);
00408 ((void) iv);
00409 ((void) input);
00410 ((void) output);
00411
00412 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
00413 #endif
00414 }
00415
00416 static int camellia_crypt_ctr_wrap( void *ctx, size_t length,
00417 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
00418 const unsigned char *input, unsigned char *output )
00419 {
00420 #if defined(POLARSSL_CIPHER_MODE_CTR)
00421 return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
00422 stream_block, input, output );
00423 #else
00424 ((void) ctx);
00425 ((void) length);
00426 ((void) nc_off);
00427 ((void) nonce_counter);
00428 ((void) stream_block);
00429 ((void) input);
00430 ((void) output);
00431
00432 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
00433 #endif
00434 }
00435
00436 static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
00437 {
00438 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
00439 }
00440
00441 static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
00442 {
00443 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
00444 }
00445
00446 static void * camellia_ctx_alloc( void )
00447 {
00448 return polarssl_malloc( sizeof( camellia_context ) );
00449 }
00450
00451 static void camellia_ctx_free( void *ctx )
00452 {
00453 polarssl_free( ctx );
00454 }
00455
00456 const cipher_base_t camellia_info = {
00457 POLARSSL_CIPHER_ID_CAMELLIA,
00458 camellia_crypt_ecb_wrap,
00459 camellia_crypt_cbc_wrap,
00460 camellia_crypt_cfb128_wrap,
00461 camellia_crypt_ctr_wrap,
00462 NULL,
00463 camellia_setkey_enc_wrap,
00464 camellia_setkey_dec_wrap,
00465 camellia_ctx_alloc,
00466 camellia_ctx_free
00467 };
00468
00469 const cipher_info_t camellia_128_ecb_info = {
00470 POLARSSL_CIPHER_CAMELLIA_128_ECB,
00471 POLARSSL_MODE_ECB,
00472 128,
00473 "CAMELLIA-128-ECB",
00474 16,
00475 0,
00476 16,
00477 &camellia_info
00478 };
00479
00480 const cipher_info_t camellia_192_ecb_info = {
00481 POLARSSL_CIPHER_CAMELLIA_192_ECB,
00482 POLARSSL_MODE_ECB,
00483 192,
00484 "CAMELLIA-192-ECB",
00485 16,
00486 0,
00487 16,
00488 &camellia_info
00489 };
00490
00491 const cipher_info_t camellia_256_ecb_info = {
00492 POLARSSL_CIPHER_CAMELLIA_256_ECB,
00493 POLARSSL_MODE_ECB,
00494 256,
00495 "CAMELLIA-256-ECB",
00496 16,
00497 0,
00498 16,
00499 &camellia_info
00500 };
00501
00502 #if defined(POLARSSL_CIPHER_MODE_CBC)
00503 const cipher_info_t camellia_128_cbc_info = {
00504 POLARSSL_CIPHER_CAMELLIA_128_CBC,
00505 POLARSSL_MODE_CBC,
00506 128,
00507 "CAMELLIA-128-CBC",
00508 16,
00509 0,
00510 16,
00511 &camellia_info
00512 };
00513
00514 const cipher_info_t camellia_192_cbc_info = {
00515 POLARSSL_CIPHER_CAMELLIA_192_CBC,
00516 POLARSSL_MODE_CBC,
00517 192,
00518 "CAMELLIA-192-CBC",
00519 16,
00520 0,
00521 16,
00522 &camellia_info
00523 };
00524
00525 const cipher_info_t camellia_256_cbc_info = {
00526 POLARSSL_CIPHER_CAMELLIA_256_CBC,
00527 POLARSSL_MODE_CBC,
00528 256,
00529 "CAMELLIA-256-CBC",
00530 16,
00531 0,
00532 16,
00533 &camellia_info
00534 };
00535 #endif
00536
00537 #if defined(POLARSSL_CIPHER_MODE_CFB)
00538 const cipher_info_t camellia_128_cfb128_info = {
00539 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
00540 POLARSSL_MODE_CFB,
00541 128,
00542 "CAMELLIA-128-CFB128",
00543 16,
00544 0,
00545 16,
00546 &camellia_info
00547 };
00548
00549 const cipher_info_t camellia_192_cfb128_info = {
00550 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
00551 POLARSSL_MODE_CFB,
00552 192,
00553 "CAMELLIA-192-CFB128",
00554 16,
00555 0,
00556 16,
00557 &camellia_info
00558 };
00559
00560 const cipher_info_t camellia_256_cfb128_info = {
00561 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
00562 POLARSSL_MODE_CFB,
00563 256,
00564 "CAMELLIA-256-CFB128",
00565 16,
00566 0,
00567 16,
00568 &camellia_info
00569 };
00570 #endif
00571
00572 #if defined(POLARSSL_CIPHER_MODE_CTR)
00573 const cipher_info_t camellia_128_ctr_info = {
00574 POLARSSL_CIPHER_CAMELLIA_128_CTR,
00575 POLARSSL_MODE_CTR,
00576 128,
00577 "CAMELLIA-128-CTR",
00578 16,
00579 0,
00580 16,
00581 &camellia_info
00582 };
00583
00584 const cipher_info_t camellia_192_ctr_info = {
00585 POLARSSL_CIPHER_CAMELLIA_192_CTR,
00586 POLARSSL_MODE_CTR,
00587 192,
00588 "CAMELLIA-192-CTR",
00589 16,
00590 0,
00591 16,
00592 &camellia_info
00593 };
00594
00595 const cipher_info_t camellia_256_ctr_info = {
00596 POLARSSL_CIPHER_CAMELLIA_256_CTR,
00597 POLARSSL_MODE_CTR,
00598 256,
00599 "CAMELLIA-256-CTR",
00600 16,
00601 0,
00602 16,
00603 &camellia_info
00604 };
00605 #endif
00606
00607 #if defined(POLARSSL_GCM_C)
00608 static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
00609 {
00610 return gcm_init( (gcm_context *) ctx, POLARSSL_CIPHER_ID_CAMELLIA,
00611 key, key_length );
00612 }
00613
00614 const cipher_base_t gcm_camellia_info = {
00615 POLARSSL_CIPHER_ID_CAMELLIA,
00616 NULL,
00617 NULL,
00618 NULL,
00619 NULL,
00620 NULL,
00621 gcm_camellia_setkey_wrap,
00622 gcm_camellia_setkey_wrap,
00623 gcm_ctx_alloc,
00624 gcm_ctx_free,
00625 };
00626
00627 const cipher_info_t camellia_128_gcm_info = {
00628 POLARSSL_CIPHER_CAMELLIA_128_GCM,
00629 POLARSSL_MODE_GCM,
00630 128,
00631 "CAMELLIA-128-GCM",
00632 12,
00633 1,
00634 16,
00635 &gcm_camellia_info
00636 };
00637
00638 const cipher_info_t camellia_192_gcm_info = {
00639 POLARSSL_CIPHER_CAMELLIA_192_GCM,
00640 POLARSSL_MODE_GCM,
00641 192,
00642 "CAMELLIA-192-GCM",
00643 12,
00644 1,
00645 16,
00646 &gcm_camellia_info
00647 };
00648
00649 const cipher_info_t camellia_256_gcm_info = {
00650 POLARSSL_CIPHER_CAMELLIA_256_GCM,
00651 POLARSSL_MODE_GCM,
00652 256,
00653 "CAMELLIA-256-GCM",
00654 12,
00655 1,
00656 16,
00657 &gcm_camellia_info
00658 };
00659 #endif
00660
00661 #endif
00662
00663 #if defined(POLARSSL_DES_C)
00664
00665 static int des_crypt_ecb_wrap( void *ctx, operation_t operation,
00666 const unsigned char *input, unsigned char *output )
00667 {
00668 ((void) operation);
00669 return des_crypt_ecb( (des_context *) ctx, input, output );
00670 }
00671
00672 static int des3_crypt_ecb_wrap( void *ctx, operation_t operation,
00673 const unsigned char *input, unsigned char *output )
00674 {
00675 ((void) operation);
00676 return des3_crypt_ecb( (des3_context *) ctx, input, output );
00677 }
00678
00679 static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
00680 unsigned char *iv, const unsigned char *input, unsigned char *output )
00681 {
00682 #if defined(POLARSSL_CIPHER_MODE_CBC)
00683 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
00684 #else
00685 ((void) ctx);
00686 ((void) operation);
00687 ((void) length);
00688 ((void) iv);
00689 ((void) input);
00690 ((void) output);
00691
00692 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
00693 #endif
00694 }
00695
00696 static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
00697 unsigned char *iv, const unsigned char *input, unsigned char *output )
00698 {
00699 #if defined(POLARSSL_CIPHER_MODE_CBC)
00700 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
00701 #else
00702 ((void) ctx);
00703 ((void) operation);
00704 ((void) length);
00705 ((void) iv);
00706 ((void) input);
00707 ((void) output);
00708
00709 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
00710 #endif
00711 }
00712
00713 static int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
00714 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
00715 {
00716 ((void) ctx);
00717 ((void) operation);
00718 ((void) length);
00719 ((void) iv_off);
00720 ((void) iv);
00721 ((void) input);
00722 ((void) output);
00723
00724 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
00725 }
00726
00727 static int des_crypt_ctr_wrap( void *ctx, size_t length,
00728 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
00729 const unsigned char *input, unsigned char *output )
00730 {
00731 ((void) ctx);
00732 ((void) length);
00733 ((void) nc_off);
00734 ((void) nonce_counter);
00735 ((void) stream_block);
00736 ((void) input);
00737 ((void) output);
00738
00739 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
00740 }
00741
00742 static int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
00743 {
00744 ((void) key_length);
00745
00746 return des_setkey_dec( (des_context *) ctx, key );
00747 }
00748
00749 static int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
00750 {
00751 ((void) key_length);
00752
00753 return des_setkey_enc( (des_context *) ctx, key );
00754 }
00755
00756 static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
00757 {
00758 ((void) key_length);
00759
00760 return des3_set2key_dec( (des3_context *) ctx, key );
00761 }
00762
00763 static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
00764 {
00765 ((void) key_length);
00766
00767 return des3_set2key_enc( (des3_context *) ctx, key );
00768 }
00769
00770 static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
00771 {
00772 ((void) key_length);
00773
00774 return des3_set3key_dec( (des3_context *) ctx, key );
00775 }
00776
00777 static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
00778 {
00779 ((void) key_length);
00780
00781 return des3_set3key_enc( (des3_context *) ctx, key );
00782 }
00783
00784 static void * des_ctx_alloc( void )
00785 {
00786 return polarssl_malloc( sizeof( des_context ) );
00787 }
00788
00789 static void * des3_ctx_alloc( void )
00790 {
00791 return polarssl_malloc( sizeof( des3_context ) );
00792 }
00793
00794 static void des_ctx_free( void *ctx )
00795 {
00796 polarssl_free( ctx );
00797 }
00798
00799 const cipher_base_t des_info = {
00800 POLARSSL_CIPHER_ID_DES,
00801 des_crypt_ecb_wrap,
00802 des_crypt_cbc_wrap,
00803 des_crypt_cfb128_wrap,
00804 des_crypt_ctr_wrap,
00805 NULL,
00806 des_setkey_enc_wrap,
00807 des_setkey_dec_wrap,
00808 des_ctx_alloc,
00809 des_ctx_free
00810 };
00811
00812 const cipher_info_t des_ecb_info = {
00813 POLARSSL_CIPHER_DES_ECB,
00814 POLARSSL_MODE_ECB,
00815 POLARSSL_KEY_LENGTH_DES,
00816 "DES-ECB",
00817 8,
00818 0,
00819 8,
00820 &des_info
00821 };
00822
00823 #if defined(POLARSSL_CIPHER_MODE_CBC)
00824 const cipher_info_t des_cbc_info = {
00825 POLARSSL_CIPHER_DES_CBC,
00826 POLARSSL_MODE_CBC,
00827 POLARSSL_KEY_LENGTH_DES,
00828 "DES-CBC",
00829 8,
00830 0,
00831 8,
00832 &des_info
00833 };
00834 #endif
00835
00836 const cipher_base_t des_ede_info = {
00837 POLARSSL_CIPHER_ID_DES,
00838 des3_crypt_ecb_wrap,
00839 des3_crypt_cbc_wrap,
00840 des_crypt_cfb128_wrap,
00841 des_crypt_ctr_wrap,
00842 NULL,
00843 des3_set2key_enc_wrap,
00844 des3_set2key_dec_wrap,
00845 des3_ctx_alloc,
00846 des_ctx_free
00847 };
00848
00849 const cipher_info_t des_ede_ecb_info = {
00850 POLARSSL_CIPHER_DES_EDE_ECB,
00851 POLARSSL_MODE_ECB,
00852 POLARSSL_KEY_LENGTH_DES_EDE,
00853 "DES-EDE-ECB",
00854 8,
00855 0,
00856 8,
00857 &des_ede_info
00858 };
00859
00860 #if defined(POLARSSL_CIPHER_MODE_CBC)
00861 const cipher_info_t des_ede_cbc_info = {
00862 POLARSSL_CIPHER_DES_EDE_CBC,
00863 POLARSSL_MODE_CBC,
00864 POLARSSL_KEY_LENGTH_DES_EDE,
00865 "DES-EDE-CBC",
00866 8,
00867 0,
00868 8,
00869 &des_ede_info
00870 };
00871 #endif
00872
00873 const cipher_base_t des_ede3_info = {
00874 POLARSSL_CIPHER_ID_DES,
00875 des3_crypt_ecb_wrap,
00876 des3_crypt_cbc_wrap,
00877 des_crypt_cfb128_wrap,
00878 des_crypt_ctr_wrap,
00879 NULL,
00880 des3_set3key_enc_wrap,
00881 des3_set3key_dec_wrap,
00882 des3_ctx_alloc,
00883 des_ctx_free
00884 };
00885
00886 const cipher_info_t des_ede3_ecb_info = {
00887 POLARSSL_CIPHER_DES_EDE3_ECB,
00888 POLARSSL_MODE_ECB,
00889 POLARSSL_KEY_LENGTH_DES_EDE3,
00890 "DES-EDE3-ECB",
00891 8,
00892 0,
00893 8,
00894 &des_ede3_info
00895 };
00896 #if defined(POLARSSL_CIPHER_MODE_CBC)
00897 const cipher_info_t des_ede3_cbc_info = {
00898 POLARSSL_CIPHER_DES_EDE3_CBC,
00899 POLARSSL_MODE_CBC,
00900 POLARSSL_KEY_LENGTH_DES_EDE3,
00901 "DES-EDE3-CBC",
00902 8,
00903 0,
00904 8,
00905 &des_ede3_info
00906 };
00907 #endif
00908 #endif
00909
00910 #if defined(POLARSSL_BLOWFISH_C)
00911
00912 static int blowfish_crypt_ecb_wrap( void *ctx, operation_t operation,
00913 const unsigned char *input, unsigned char *output )
00914 {
00915 return blowfish_crypt_ecb( (blowfish_context *) ctx, operation, input, output );
00916 }
00917
00918 static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
00919 unsigned char *iv, const unsigned char *input, unsigned char *output )
00920 {
00921 #if defined(POLARSSL_CIPHER_MODE_CBC)
00922 return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output );
00923 #else
00924 ((void) ctx);
00925 ((void) operation);
00926 ((void) length);
00927 ((void) iv);
00928 ((void) input);
00929 ((void) output);
00930
00931 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
00932 #endif
00933 }
00934
00935 static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length,
00936 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
00937 {
00938 #if defined(POLARSSL_CIPHER_MODE_CFB)
00939 return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output );
00940 #else
00941 ((void) ctx);
00942 ((void) operation);
00943 ((void) length);
00944 ((void) iv_off);
00945 ((void) iv);
00946 ((void) input);
00947 ((void) output);
00948
00949 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
00950 #endif
00951 }
00952
00953 static int blowfish_crypt_ctr_wrap( void *ctx, size_t length,
00954 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
00955 const unsigned char *input, unsigned char *output )
00956 {
00957 #if defined(POLARSSL_CIPHER_MODE_CTR)
00958 return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter,
00959 stream_block, input, output );
00960 #else
00961 ((void) ctx);
00962 ((void) length);
00963 ((void) nc_off);
00964 ((void) nonce_counter);
00965 ((void) stream_block);
00966 ((void) input);
00967 ((void) output);
00968
00969 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
00970 #endif
00971 }
00972
00973 static int blowfish_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
00974 {
00975 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
00976 }
00977
00978 static void * blowfish_ctx_alloc( void )
00979 {
00980 return polarssl_malloc( sizeof( blowfish_context ) );
00981 }
00982
00983 static void blowfish_ctx_free( void *ctx )
00984 {
00985 polarssl_free( ctx );
00986 }
00987
00988 const cipher_base_t blowfish_info = {
00989 POLARSSL_CIPHER_ID_BLOWFISH,
00990 blowfish_crypt_ecb_wrap,
00991 blowfish_crypt_cbc_wrap,
00992 blowfish_crypt_cfb64_wrap,
00993 blowfish_crypt_ctr_wrap,
00994 NULL,
00995 blowfish_setkey_wrap,
00996 blowfish_setkey_wrap,
00997 blowfish_ctx_alloc,
00998 blowfish_ctx_free
00999 };
01000
01001 const cipher_info_t blowfish_ecb_info = {
01002 POLARSSL_CIPHER_BLOWFISH_ECB,
01003 POLARSSL_MODE_ECB,
01004 128,
01005 "BLOWFISH-ECB",
01006 8,
01007 0,
01008 8,
01009 &blowfish_info
01010 };
01011
01012 #if defined(POLARSSL_CIPHER_MODE_CBC)
01013 const cipher_info_t blowfish_cbc_info = {
01014 POLARSSL_CIPHER_BLOWFISH_CBC,
01015 POLARSSL_MODE_CBC,
01016 128,
01017 "BLOWFISH-CBC",
01018 8,
01019 0,
01020 8,
01021 &blowfish_info
01022 };
01023 #endif
01024
01025 #if defined(POLARSSL_CIPHER_MODE_CFB)
01026 const cipher_info_t blowfish_cfb64_info = {
01027 POLARSSL_CIPHER_BLOWFISH_CFB64,
01028 POLARSSL_MODE_CFB,
01029 128,
01030 "BLOWFISH-CFB64",
01031 8,
01032 0,
01033 8,
01034 &blowfish_info
01035 };
01036 #endif
01037
01038 #if defined(POLARSSL_CIPHER_MODE_CTR)
01039 const cipher_info_t blowfish_ctr_info = {
01040 POLARSSL_CIPHER_BLOWFISH_CTR,
01041 POLARSSL_MODE_CTR,
01042 128,
01043 "BLOWFISH-CTR",
01044 8,
01045 0,
01046 8,
01047 &blowfish_info
01048 };
01049 #endif
01050 #endif
01051
01052 #if defined(POLARSSL_ARC4_C)
01053 static int arc4_crypt_stream_wrap( void *ctx, size_t length,
01054 const unsigned char *input,
01055 unsigned char *output )
01056 {
01057 return( arc4_crypt( (arc4_context *) ctx, length, input, output ) );
01058 }
01059
01060 static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
01061 unsigned int key_length )
01062 {
01063
01064 if( key_length % 8 != 0)
01065 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
01066
01067 arc4_setup( (arc4_context *) ctx, key, key_length / 8 );
01068 return( 0 );
01069 }
01070
01071 static void * arc4_ctx_alloc( void )
01072 {
01073 return polarssl_malloc( sizeof( arc4_context ) );
01074 }
01075
01076 static void arc4_ctx_free( void *ctx )
01077 {
01078 polarssl_free( ctx );
01079 }
01080
01081 const cipher_base_t arc4_base_info = {
01082 POLARSSL_CIPHER_ID_ARC4,
01083 NULL,
01084 NULL,
01085 NULL,
01086 NULL,
01087 arc4_crypt_stream_wrap,
01088 arc4_setkey_wrap,
01089 arc4_setkey_wrap,
01090 arc4_ctx_alloc,
01091 arc4_ctx_free
01092 };
01093
01094 const cipher_info_t arc4_128_info = {
01095 POLARSSL_CIPHER_ARC4_128,
01096 POLARSSL_MODE_STREAM,
01097 128,
01098 "ARC4-128",
01099 0,
01100 0,
01101 1,
01102 &arc4_base_info
01103 };
01104 #endif
01105
01106 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
01107 static int null_crypt_stream( void *ctx, size_t length,
01108 const unsigned char *input,
01109 unsigned char *output )
01110 {
01111 ((void) ctx);
01112 memmove( output, input, length );
01113 return( 0 );
01114 }
01115
01116 static int null_setkey( void *ctx, const unsigned char *key,
01117 unsigned int key_length )
01118 {
01119 ((void) ctx);
01120 ((void) key);
01121 ((void) key_length);
01122
01123 return( 0 );
01124 }
01125
01126 static void * null_ctx_alloc( void )
01127 {
01128 return (void *) 1;
01129 }
01130
01131 static void null_ctx_free( void *ctx )
01132 {
01133 ((void) ctx);
01134 }
01135
01136 const cipher_base_t null_base_info = {
01137 POLARSSL_CIPHER_ID_NULL,
01138 NULL,
01139 NULL,
01140 NULL,
01141 NULL,
01142 null_crypt_stream,
01143 null_setkey,
01144 null_setkey,
01145 null_ctx_alloc,
01146 null_ctx_free
01147 };
01148
01149 const cipher_info_t null_cipher_info = {
01150 POLARSSL_CIPHER_NULL,
01151 POLARSSL_MODE_STREAM,
01152 0,
01153 "NULL",
01154 0,
01155 0,
01156 1,
01157 &null_base_info
01158 };
01159 #endif
01160
01161 const cipher_definition_t cipher_definitions[] =
01162 {
01163 #if defined(POLARSSL_AES_C)
01164 { POLARSSL_CIPHER_AES_128_ECB, &aes_128_ecb_info },
01165 { POLARSSL_CIPHER_AES_192_ECB, &aes_192_ecb_info },
01166 { POLARSSL_CIPHER_AES_256_ECB, &aes_256_ecb_info },
01167 #if defined(POLARSSL_CIPHER_MODE_CBC)
01168 { POLARSSL_CIPHER_AES_128_CBC, &aes_128_cbc_info },
01169 { POLARSSL_CIPHER_AES_192_CBC, &aes_192_cbc_info },
01170 { POLARSSL_CIPHER_AES_256_CBC, &aes_256_cbc_info },
01171 #endif
01172 #if defined(POLARSSL_CIPHER_MODE_CFB)
01173 { POLARSSL_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
01174 { POLARSSL_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
01175 { POLARSSL_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
01176 #endif
01177 #if defined(POLARSSL_CIPHER_MODE_CTR)
01178 { POLARSSL_CIPHER_AES_128_CTR, &aes_128_ctr_info },
01179 { POLARSSL_CIPHER_AES_192_CTR, &aes_192_ctr_info },
01180 { POLARSSL_CIPHER_AES_256_CTR, &aes_256_ctr_info },
01181 #endif
01182 #if defined(POLARSSL_GCM_C)
01183 { POLARSSL_CIPHER_AES_128_GCM, &aes_128_gcm_info },
01184 { POLARSSL_CIPHER_AES_192_GCM, &aes_192_gcm_info },
01185 { POLARSSL_CIPHER_AES_256_GCM, &aes_256_gcm_info },
01186 #endif
01187 #endif
01188
01189 #if defined(POLARSSL_ARC4_C)
01190 { POLARSSL_CIPHER_ARC4_128, &arc4_128_info },
01191 #endif
01192
01193 #if defined(POLARSSL_BLOWFISH_C)
01194 { POLARSSL_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
01195 #if defined(POLARSSL_CIPHER_MODE_CBC)
01196 { POLARSSL_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
01197 #endif
01198 #if defined(POLARSSL_CIPHER_MODE_CFB)
01199 { POLARSSL_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
01200 #endif
01201 #if defined(POLARSSL_CIPHER_MODE_CTR)
01202 { POLARSSL_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
01203 #endif
01204 #endif
01205
01206 #if defined(POLARSSL_CAMELLIA_C)
01207 { POLARSSL_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
01208 { POLARSSL_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
01209 { POLARSSL_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
01210 #if defined(POLARSSL_CIPHER_MODE_CBC)
01211 { POLARSSL_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
01212 { POLARSSL_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
01213 { POLARSSL_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
01214 #endif
01215 #if defined(POLARSSL_CIPHER_MODE_CFB)
01216 { POLARSSL_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
01217 { POLARSSL_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
01218 { POLARSSL_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
01219 #endif
01220 #if defined(POLARSSL_CIPHER_MODE_CTR)
01221 { POLARSSL_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
01222 { POLARSSL_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
01223 { POLARSSL_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
01224 #endif
01225 #if defined(POLARSSL_GCM_C)
01226 { POLARSSL_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
01227 { POLARSSL_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
01228 { POLARSSL_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
01229 #endif
01230 #endif
01231
01232 #if defined(POLARSSL_DES_C)
01233 { POLARSSL_CIPHER_DES_ECB, &des_ecb_info },
01234 { POLARSSL_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
01235 { POLARSSL_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
01236 #if defined(POLARSSL_CIPHER_MODE_CBC)
01237 { POLARSSL_CIPHER_DES_CBC, &des_cbc_info },
01238 { POLARSSL_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
01239 { POLARSSL_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
01240 #endif
01241 #endif
01242
01243 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
01244 { POLARSSL_CIPHER_NULL, &null_cipher_info },
01245 #endif
01246
01247 { 0, NULL }
01248 };
01249
01250 #define NUM_CIPHERS sizeof cipher_definitions / sizeof cipher_definitions[0]
01251 int supported_ciphers[NUM_CIPHERS];
01252
01253 #endif