123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- #ifndef POLARSSL_CIPHER_H
- #define POLARSSL_CIPHER_H
- #include <string.h>
- #if defined(_MSC_VER) && !defined(inline)
- #define inline _inline
- #else
- #if defined(__ARMCC_VERSION) && !defined(inline)
- #define inline __inline
- #endif
- #endif
- #define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
- #define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100
- #define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180
- #define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200
- #define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
- typedef enum {
- POLARSSL_CIPHER_ID_NONE = 0,
- POLARSSL_CIPHER_ID_AES,
- POLARSSL_CIPHER_ID_DES,
- POLARSSL_CIPHER_ID_3DES,
- POLARSSL_CIPHER_ID_CAMELLIA,
- } cipher_id_t;
- typedef enum {
- POLARSSL_CIPHER_NONE = 0,
- POLARSSL_CIPHER_AES_128_CBC,
- POLARSSL_CIPHER_AES_192_CBC,
- POLARSSL_CIPHER_AES_256_CBC,
- POLARSSL_CIPHER_AES_128_CFB128,
- POLARSSL_CIPHER_AES_192_CFB128,
- POLARSSL_CIPHER_AES_256_CFB128,
- POLARSSL_CIPHER_AES_128_CTR,
- POLARSSL_CIPHER_AES_192_CTR,
- POLARSSL_CIPHER_AES_256_CTR,
- POLARSSL_CIPHER_CAMELLIA_128_CBC,
- POLARSSL_CIPHER_CAMELLIA_192_CBC,
- POLARSSL_CIPHER_CAMELLIA_256_CBC,
- POLARSSL_CIPHER_CAMELLIA_128_CFB128,
- POLARSSL_CIPHER_CAMELLIA_192_CFB128,
- POLARSSL_CIPHER_CAMELLIA_256_CFB128,
- POLARSSL_CIPHER_CAMELLIA_128_CTR,
- POLARSSL_CIPHER_CAMELLIA_192_CTR,
- POLARSSL_CIPHER_CAMELLIA_256_CTR,
- POLARSSL_CIPHER_DES_CBC,
- POLARSSL_CIPHER_DES_EDE_CBC,
- POLARSSL_CIPHER_DES_EDE3_CBC
- } cipher_type_t;
- typedef enum {
- POLARSSL_MODE_NONE = 0,
- POLARSSL_MODE_CBC,
- POLARSSL_MODE_CFB128,
- POLARSSL_MODE_OFB,
- POLARSSL_MODE_CTR,
- } cipher_mode_t;
- typedef enum {
- POLARSSL_DECRYPT = 0,
- POLARSSL_ENCRYPT,
- } operation_t;
- enum {
-
- POLARSSL_KEY_LENGTH_NONE = 0,
-
- POLARSSL_KEY_LENGTH_DES = 56,
-
- POLARSSL_KEY_LENGTH_DES_EDE = 112,
-
- POLARSSL_KEY_LENGTH_DES_EDE3 = 168,
-
- POLARSSL_MAX_IV_LENGTH = 16,
- };
- typedef struct {
-
- cipher_id_t cipher;
-
- int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv,
- const unsigned char *input, unsigned char *output );
-
- int (*cfb128_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off,
- unsigned char *iv, const unsigned char *input, unsigned char *output );
-
- int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter,
- unsigned char *stream_block, const unsigned char *input, unsigned char *output );
-
- int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length);
-
- int (*setkey_dec_func)( void *ctx, const unsigned char *key, unsigned int key_length);
-
- void * (*ctx_alloc_func)( void );
-
- void (*ctx_free_func)( void *ctx );
- } cipher_base_t;
- typedef struct {
-
- cipher_type_t type;
-
- cipher_mode_t mode;
-
- unsigned int key_length;
-
- const char * name;
-
- unsigned int iv_size;
-
- unsigned int block_size;
-
- const cipher_base_t *base;
- } cipher_info_t;
- typedef struct {
-
- const cipher_info_t *cipher_info;
-
- int key_length;
-
- operation_t operation;
-
- unsigned char unprocessed_data[POLARSSL_MAX_IV_LENGTH];
-
- size_t unprocessed_len;
-
- unsigned char iv[POLARSSL_MAX_IV_LENGTH];
-
- void *cipher_ctx;
- } cipher_context_t;
- #ifdef __cplusplus
- extern "C" {
- #endif
- const int *cipher_list( void );
- const cipher_info_t *cipher_info_from_string( const char *cipher_name );
- const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type );
- int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info );
- int cipher_free_ctx( cipher_context_t *ctx );
- static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx )
- {
- if( NULL == ctx || NULL == ctx->cipher_info )
- return 0;
- return ctx->cipher_info->block_size;
- }
- static inline int cipher_get_iv_size( const cipher_context_t *ctx )
- {
- if( NULL == ctx || NULL == ctx->cipher_info )
- return 0;
- return ctx->cipher_info->iv_size;
- }
- static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx )
- {
- if( NULL == ctx || NULL == ctx->cipher_info )
- return (cipher_type_t)0;
- return ctx->cipher_info->type;
- }
- static inline const char *cipher_get_name( const cipher_context_t *ctx )
- {
- if( NULL == ctx || NULL == ctx->cipher_info )
- return 0;
- return ctx->cipher_info->name;
- }
- static inline int cipher_get_key_size ( const cipher_context_t *ctx )
- {
- if( NULL == ctx )
- return POLARSSL_KEY_LENGTH_NONE;
- return ctx->key_length;
- }
- int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length,
- const operation_t operation );
- int cipher_reset( cipher_context_t *ctx, const unsigned char *iv );
- int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
- unsigned char *output, size_t *olen );
- int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen);
- int cipher_self_test( int verbose );
- #ifdef __cplusplus
- }
- #endif
- #endif
|