30 #if !defined(POLARSSL_CONFIG_FILE)
33 #include POLARSSL_CONFIG_FILE
36 #if defined(POLARSSL_CIPHER_C)
40 #if defined(POLARSSL_AES_C)
44 #if defined(POLARSSL_ARC4_C)
48 #if defined(POLARSSL_CAMELLIA_C)
52 #if defined(POLARSSL_DES_C)
56 #if defined(POLARSSL_BLOWFISH_C)
60 #if defined(POLARSSL_GCM_C)
64 #if defined(POLARSSL_CCM_C)
68 #if defined(POLARSSL_PLATFORM_C)
71 #define polarssl_malloc malloc
72 #define polarssl_free free
77 #if defined(POLARSSL_GCM_C)
79 static void *gcm_ctx_alloc(
void )
84 static void gcm_ctx_free(
void *ctx )
91 #if defined(POLARSSL_CCM_C)
93 static void *ccm_ctx_alloc(
void )
98 static void ccm_ctx_free(
void *ctx )
105 #if defined(POLARSSL_AES_C)
107 static int aes_crypt_ecb_wrap(
void *ctx,
operation_t operation,
108 const unsigned char *input,
unsigned char *output )
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 )
116 #if defined(POLARSSL_CIPHER_MODE_CBC)
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 )
135 #if defined(POLARSSL_CIPHER_MODE_CFB)
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 )
155 #if defined(POLARSSL_CIPHER_MODE_CTR)
157 stream_block, input, output );
162 ((void) nonce_counter);
163 ((void) stream_block);
171 static int aes_setkey_dec_wrap(
void *ctx,
const unsigned char *key,
172 unsigned int key_length )
177 static int aes_setkey_enc_wrap(
void *ctx,
const unsigned char *key,
178 unsigned int key_length )
183 static void * aes_ctx_alloc(
void )
195 static void aes_ctx_free(
void *ctx )
205 aes_crypt_cfb128_wrap,
247 #if defined(POLARSSL_CIPHER_MODE_CBC)
282 #if defined(POLARSSL_CIPHER_MODE_CFB)
317 #if defined(POLARSSL_CIPHER_MODE_CTR)
352 #if defined(POLARSSL_GCM_C)
353 static int gcm_aes_setkey_wrap(
void *ctx,
const unsigned char *key,
354 unsigned int key_length )
407 #if defined(POLARSSL_CCM_C)
408 static int ccm_aes_setkey_wrap(
void *ctx,
const unsigned char *key,
409 unsigned int key_length )
464 #if defined(POLARSSL_CAMELLIA_C)
466 static int camellia_crypt_ecb_wrap(
void *ctx,
operation_t operation,
467 const unsigned char *input,
unsigned char *output )
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 )
477 #if defined(POLARSSL_CIPHER_MODE_CBC)
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 )
496 #if defined(POLARSSL_CIPHER_MODE_CFB)
498 iv_off, iv, input, output );
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 )
516 #if defined(POLARSSL_CIPHER_MODE_CTR)
518 nonce_counter, stream_block, input, output );
523 ((void) nonce_counter);
524 ((void) stream_block);
532 static int camellia_setkey_dec_wrap(
void *ctx,
const unsigned char *key,
533 unsigned int key_length )
538 static int camellia_setkey_enc_wrap(
void *ctx,
const unsigned char *key,
539 unsigned int key_length )
544 static void * camellia_ctx_alloc(
void )
557 static void camellia_ctx_free(
void *ctx )
565 camellia_crypt_ecb_wrap,
566 camellia_crypt_cbc_wrap,
567 camellia_crypt_cfb128_wrap,
568 camellia_crypt_ctr_wrap,
570 camellia_setkey_enc_wrap,
571 camellia_setkey_dec_wrap,
609 #if defined(POLARSSL_CIPHER_MODE_CBC)
644 #if defined(POLARSSL_CIPHER_MODE_CFB)
649 "CAMELLIA-128-CFB128",
660 "CAMELLIA-192-CFB128",
671 "CAMELLIA-256-CFB128",
679 #if defined(POLARSSL_CIPHER_MODE_CTR)
714 #if defined(POLARSSL_GCM_C)
715 static int gcm_camellia_setkey_wrap(
void *ctx,
const unsigned char *key,
716 unsigned int key_length )
729 gcm_camellia_setkey_wrap,
730 gcm_camellia_setkey_wrap,
769 #if defined(POLARSSL_CCM_C)
770 static int ccm_camellia_setkey_wrap(
void *ctx,
const unsigned char *key,
771 unsigned int key_length )
784 ccm_camellia_setkey_wrap,
785 ccm_camellia_setkey_wrap,
826 #if defined(POLARSSL_DES_C)
828 static int des_crypt_ecb_wrap(
void *ctx,
operation_t operation,
829 const unsigned char *input,
unsigned char *output )
835 static int des3_crypt_ecb_wrap(
void *ctx,
operation_t operation,
836 const unsigned char *input,
unsigned char *output )
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 )
845 #if defined(POLARSSL_CIPHER_MODE_CBC)
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 )
863 #if defined(POLARSSL_CIPHER_MODE_CBC)
878 static int des_setkey_dec_wrap(
void *ctx,
const unsigned char *key,
879 unsigned int key_length )
886 static int des_setkey_enc_wrap(
void *ctx,
const unsigned char *key,
887 unsigned int key_length )
894 static int des3_set2key_dec_wrap(
void *ctx,
const unsigned char *key,
895 unsigned int key_length )
902 static int des3_set2key_enc_wrap(
void *ctx,
const unsigned char *key,
903 unsigned int key_length )
910 static int des3_set3key_dec_wrap(
void *ctx,
const unsigned char *key,
911 unsigned int key_length )
918 static int des3_set3key_enc_wrap(
void *ctx,
const unsigned char *key,
919 unsigned int key_length )
926 static void * des_ctx_alloc(
void )
938 static void des_ctx_free(
void *ctx )
944 static void * des3_ctx_alloc(
void )
957 static void des3_ctx_free(
void *ctx )
987 #if defined(POLARSSL_CIPHER_MODE_CBC)
1002 des3_crypt_ecb_wrap,
1003 des3_crypt_cbc_wrap,
1007 des3_set2key_enc_wrap,
1008 des3_set2key_dec_wrap,
1024 #if defined(POLARSSL_CIPHER_MODE_CBC)
1039 des3_crypt_ecb_wrap,
1040 des3_crypt_cbc_wrap,
1044 des3_set3key_enc_wrap,
1045 des3_set3key_dec_wrap,
1060 #if defined(POLARSSL_CIPHER_MODE_CBC)
1074 #if defined(POLARSSL_BLOWFISH_C)
1076 static int blowfish_crypt_ecb_wrap(
void *ctx,
operation_t operation,
1077 const unsigned char *input,
unsigned char *output )
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 )
1087 #if defined(POLARSSL_CIPHER_MODE_CBC)
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 )
1106 #if defined(POLARSSL_CIPHER_MODE_CFB)
1108 iv_off, iv, input, output );
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 )
1126 #if defined(POLARSSL_CIPHER_MODE_CTR)
1128 nonce_counter, stream_block, input, output );
1133 ((void) nonce_counter);
1134 ((void) stream_block);
1142 static int blowfish_setkey_wrap(
void *ctx,
const unsigned char *key,
1143 unsigned int key_length )
1148 static void * blowfish_ctx_alloc(
void )
1161 static void blowfish_ctx_free(
void *ctx )
1169 blowfish_crypt_ecb_wrap,
1170 blowfish_crypt_cbc_wrap,
1171 blowfish_crypt_cfb64_wrap,
1172 blowfish_crypt_ctr_wrap,
1174 blowfish_setkey_wrap,
1175 blowfish_setkey_wrap,
1191 #if defined(POLARSSL_CIPHER_MODE_CBC)
1204 #if defined(POLARSSL_CIPHER_MODE_CFB)
1217 #if defined(POLARSSL_CIPHER_MODE_CTR)
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 )
1239 static int arc4_setkey_wrap(
void *ctx,
const unsigned char *key,
1240 unsigned int key_length )
1243 if( key_length % 8 != 0 )
1250 static void * arc4_ctx_alloc(
void )
1263 static void arc4_ctx_free(
void *ctx )
1275 arc4_crypt_stream_wrap,
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 )
1300 memmove( output, input, length );
1304 static int null_setkey(
void *ctx,
const unsigned char *key,
1305 unsigned int key_length )
1309 ((void) key_length);
1314 static void * null_ctx_alloc(
void )
1316 return( (
void *) 1 )
1319 static void null_ctx_free(
void *ctx )
1351 #if defined(POLARSSL_AES_C)
1355 #if defined(POLARSSL_CIPHER_MODE_CBC)
1360 #if defined(POLARSSL_CIPHER_MODE_CFB)
1365 #if defined(POLARSSL_CIPHER_MODE_CTR)
1370 #if defined(POLARSSL_GCM_C)
1375 #if defined(POLARSSL_CCM_C)
1382 #if defined(POLARSSL_ARC4_C)
1386 #if defined(POLARSSL_BLOWFISH_C)
1388 #if defined(POLARSSL_CIPHER_MODE_CBC)
1391 #if defined(POLARSSL_CIPHER_MODE_CFB)
1394 #if defined(POLARSSL_CIPHER_MODE_CTR)
1399 #if defined(POLARSSL_CAMELLIA_C)
1403 #if defined(POLARSSL_CIPHER_MODE_CBC)
1408 #if defined(POLARSSL_CIPHER_MODE_CFB)
1413 #if defined(POLARSSL_CIPHER_MODE_CTR)
1418 #if defined(POLARSSL_GCM_C)
1423 #if defined(POLARSSL_CCM_C)
1430 #if defined(POLARSSL_DES_C)
1434 #if defined(POLARSSL_CIPHER_MODE_CBC)
1441 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
1448 #define NUM_CIPHERS sizeof cipher_definitions / sizeof cipher_definitions[0]
#define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Bad input parameters to function.
int blowfish_setkey(blowfish_context *ctx, const unsigned char *key, unsigned int keysize)
Blowfish key schedule.
void blowfish_init(blowfish_context *ctx)
Initialize Blowfish context.
#define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
The selected feature is not available.
void des3_init(des3_context *ctx)
Initialize Triple-DES context.
int arc4_crypt(arc4_context *ctx, size_t length, const unsigned char *input, unsigned char *output)
ARC4 cipher function.
void arc4_init(arc4_context *ctx)
Initialize ARC4 context.
Key length, in bits (including parity), for DES keys.
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.
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.
Configuration options (set of defines)
int aes_setkey_dec(aes_context *ctx, const unsigned char *key, unsigned int keysize)
AES key schedule (decryption)
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.
const cipher_definition_t cipher_definitions[]
void blowfish_free(blowfish_context *ctx)
Clear Blowfish context.
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.
int des3_set3key_enc(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *3])
Triple-DES key schedule (168-bit, encryption)
void arc4_free(arc4_context *ctx)
Clear ARC4 context.
void camellia_init(camellia_context *ctx)
Initialize CAMELLIA context.
void des3_free(des3_context *ctx)
Clear Triple-DES context.
int des3_set3key_dec(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *3])
Triple-DES key schedule (168-bit, decryption)
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.
int des3_set2key_enc(des3_context *ctx, const unsigned char key[DES_KEY_SIZE *2])
Triple-DES key schedule (112-bit, encryption)
void des_init(des_context *ctx)
Initialize DES context.
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) ...
#define POLARSSL_CIPHER_VARIABLE_IV_LEN
Cipher accepts IVs of variable length.
Key length, in bits (including parity), for DES in three-key EDE.
CAMELLIA context structure.
int gcm_init(gcm_context *ctx, cipher_id_t cipher, const unsigned char *key, unsigned int keysize)
GCM initialization (encryption)
int ccm_init(ccm_context *ctx, cipher_id_t cipher, const unsigned char *key, unsigned int keysize)
CCM initialization (encryption and decryption)
void camellia_free(camellia_context *ctx)
Clear CAMELLIA context.
void des_free(des_context *ctx)
Clear DES context.
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.
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.
void ccm_free(ccm_context *ctx)
Free a CCM context and underlying cipher sub-context.
Galois/Counter mode for 128-bit block ciphers.
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.
void aes_free(aes_context *ctx)
Clear AES context.
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.
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.
void aes_init(aes_context *ctx)
Initialize AES context.
Key length, in bits (including parity), for DES in two key EDE.
#define POLARSSL_CIPHER_VARIABLE_KEY_LEN
Cipher accepts keys of variable length.
Counter with CBC-MAC (CCM) for 128-bit block ciphers.
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