123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709 |
- #ifndef MBEDTLS_CIPHER_H
- #define MBEDTLS_CIPHER_H
- #if !defined(MBEDTLS_CONFIG_FILE)
- #include "config.h"
- #else
- #include MBEDTLS_CONFIG_FILE
- #endif
- #include <stddef.h>
- #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
- #define MBEDTLS_CIPHER_MODE_AEAD
- #endif
- #if defined(MBEDTLS_CIPHER_MODE_CBC)
- #define MBEDTLS_CIPHER_MODE_WITH_PADDING
- #endif
- #if defined(MBEDTLS_ARC4_C)
- #define MBEDTLS_CIPHER_MODE_STREAM
- #endif
- #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
- !defined(inline) && !defined(__cplusplus)
- #define inline __inline
- #endif
- #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
- #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100
- #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180
- #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200
- #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
- #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300
- #define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380
- #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01
- #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02
- #ifdef __cplusplus
- extern "C" {
- #endif
- typedef enum {
- MBEDTLS_CIPHER_ID_NONE = 0,
- MBEDTLS_CIPHER_ID_NULL,
- MBEDTLS_CIPHER_ID_AES,
- MBEDTLS_CIPHER_ID_DES,
- MBEDTLS_CIPHER_ID_3DES,
- MBEDTLS_CIPHER_ID_CAMELLIA,
- MBEDTLS_CIPHER_ID_BLOWFISH,
- MBEDTLS_CIPHER_ID_ARC4,
- } mbedtls_cipher_id_t;
- typedef enum {
- MBEDTLS_CIPHER_NONE = 0,
- MBEDTLS_CIPHER_NULL,
- MBEDTLS_CIPHER_AES_128_ECB,
- MBEDTLS_CIPHER_AES_192_ECB,
- MBEDTLS_CIPHER_AES_256_ECB,
- MBEDTLS_CIPHER_AES_128_CBC,
- MBEDTLS_CIPHER_AES_192_CBC,
- MBEDTLS_CIPHER_AES_256_CBC,
- MBEDTLS_CIPHER_AES_128_CFB128,
- MBEDTLS_CIPHER_AES_192_CFB128,
- MBEDTLS_CIPHER_AES_256_CFB128,
- MBEDTLS_CIPHER_AES_128_CTR,
- MBEDTLS_CIPHER_AES_192_CTR,
- MBEDTLS_CIPHER_AES_256_CTR,
- MBEDTLS_CIPHER_AES_128_GCM,
- MBEDTLS_CIPHER_AES_192_GCM,
- MBEDTLS_CIPHER_AES_256_GCM,
- MBEDTLS_CIPHER_CAMELLIA_128_ECB,
- MBEDTLS_CIPHER_CAMELLIA_192_ECB,
- MBEDTLS_CIPHER_CAMELLIA_256_ECB,
- MBEDTLS_CIPHER_CAMELLIA_128_CBC,
- MBEDTLS_CIPHER_CAMELLIA_192_CBC,
- MBEDTLS_CIPHER_CAMELLIA_256_CBC,
- MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
- MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
- MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
- MBEDTLS_CIPHER_CAMELLIA_128_CTR,
- MBEDTLS_CIPHER_CAMELLIA_192_CTR,
- MBEDTLS_CIPHER_CAMELLIA_256_CTR,
- MBEDTLS_CIPHER_CAMELLIA_128_GCM,
- MBEDTLS_CIPHER_CAMELLIA_192_GCM,
- MBEDTLS_CIPHER_CAMELLIA_256_GCM,
- MBEDTLS_CIPHER_DES_ECB,
- MBEDTLS_CIPHER_DES_CBC,
- MBEDTLS_CIPHER_DES_EDE_ECB,
- MBEDTLS_CIPHER_DES_EDE_CBC,
- MBEDTLS_CIPHER_DES_EDE3_ECB,
- MBEDTLS_CIPHER_DES_EDE3_CBC,
- MBEDTLS_CIPHER_BLOWFISH_ECB,
- MBEDTLS_CIPHER_BLOWFISH_CBC,
- MBEDTLS_CIPHER_BLOWFISH_CFB64,
- MBEDTLS_CIPHER_BLOWFISH_CTR,
- MBEDTLS_CIPHER_ARC4_128,
- MBEDTLS_CIPHER_AES_128_CCM,
- MBEDTLS_CIPHER_AES_192_CCM,
- MBEDTLS_CIPHER_AES_256_CCM,
- MBEDTLS_CIPHER_CAMELLIA_128_CCM,
- MBEDTLS_CIPHER_CAMELLIA_192_CCM,
- MBEDTLS_CIPHER_CAMELLIA_256_CCM,
- } mbedtls_cipher_type_t;
- typedef enum {
- MBEDTLS_MODE_NONE = 0,
- MBEDTLS_MODE_ECB,
- MBEDTLS_MODE_CBC,
- MBEDTLS_MODE_CFB,
- MBEDTLS_MODE_OFB,
- MBEDTLS_MODE_CTR,
- MBEDTLS_MODE_GCM,
- MBEDTLS_MODE_STREAM,
- MBEDTLS_MODE_CCM,
- } mbedtls_cipher_mode_t;
- typedef enum {
- MBEDTLS_PADDING_PKCS7 = 0,
- MBEDTLS_PADDING_ONE_AND_ZEROS,
- MBEDTLS_PADDING_ZEROS_AND_LEN,
- MBEDTLS_PADDING_ZEROS,
- MBEDTLS_PADDING_NONE,
- } mbedtls_cipher_padding_t;
- typedef enum {
- MBEDTLS_OPERATION_NONE = -1,
- MBEDTLS_DECRYPT = 0,
- MBEDTLS_ENCRYPT,
- } mbedtls_operation_t;
- enum {
-
- MBEDTLS_KEY_LENGTH_NONE = 0,
-
- MBEDTLS_KEY_LENGTH_DES = 64,
-
- MBEDTLS_KEY_LENGTH_DES_EDE = 128,
-
- MBEDTLS_KEY_LENGTH_DES_EDE3 = 192,
- };
- #define MBEDTLS_MAX_IV_LENGTH 16
- #define MBEDTLS_MAX_BLOCK_LENGTH 16
- typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t;
- typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t;
- typedef struct {
-
- mbedtls_cipher_type_t type;
-
- mbedtls_cipher_mode_t mode;
-
- unsigned int key_bitlen;
-
- const char * name;
-
- unsigned int iv_size;
-
- int flags;
-
- unsigned int block_size;
-
- const mbedtls_cipher_base_t *base;
- } mbedtls_cipher_info_t;
- typedef struct {
-
- const mbedtls_cipher_info_t *cipher_info;
-
- int key_bitlen;
-
- mbedtls_operation_t operation;
- #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
-
- void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
- int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
- #endif
-
- unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
-
- size_t unprocessed_len;
-
- unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
-
- size_t iv_size;
-
- void *cipher_ctx;
- #if defined(MBEDTLS_CMAC_C)
-
- mbedtls_cmac_context_t *cmac_ctx;
- #endif
- } mbedtls_cipher_context_t;
- const int *mbedtls_cipher_list( void );
- const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
- const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type );
- const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
- int key_bitlen,
- const mbedtls_cipher_mode_t mode );
- void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx );
- void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx );
- int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info );
- static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx )
- {
- if( NULL == ctx || NULL == ctx->cipher_info )
- return 0;
- return ctx->cipher_info->block_size;
- }
- static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( const mbedtls_cipher_context_t *ctx )
- {
- if( NULL == ctx || NULL == ctx->cipher_info )
- return MBEDTLS_MODE_NONE;
- return ctx->cipher_info->mode;
- }
- static inline int mbedtls_cipher_get_iv_size( const mbedtls_cipher_context_t *ctx )
- {
- if( NULL == ctx || NULL == ctx->cipher_info )
- return 0;
- if( ctx->iv_size != 0 )
- return (int) ctx->iv_size;
- return (int) ctx->cipher_info->iv_size;
- }
- static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( const mbedtls_cipher_context_t *ctx )
- {
- if( NULL == ctx || NULL == ctx->cipher_info )
- return MBEDTLS_CIPHER_NONE;
- return ctx->cipher_info->type;
- }
- static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx )
- {
- if( NULL == ctx || NULL == ctx->cipher_info )
- return 0;
- return ctx->cipher_info->name;
- }
- static inline int mbedtls_cipher_get_key_bitlen( const mbedtls_cipher_context_t *ctx )
- {
- if( NULL == ctx || NULL == ctx->cipher_info )
- return MBEDTLS_KEY_LENGTH_NONE;
- return (int) ctx->cipher_info->key_bitlen;
- }
- static inline mbedtls_operation_t mbedtls_cipher_get_operation( const mbedtls_cipher_context_t *ctx )
- {
- if( NULL == ctx || NULL == ctx->cipher_info )
- return MBEDTLS_OPERATION_NONE;
- return ctx->operation;
- }
- int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
- int key_bitlen, const mbedtls_operation_t operation );
- #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
- int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode );
- #endif
- int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
- const unsigned char *iv, size_t iv_len );
- int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx );
- #if defined(MBEDTLS_GCM_C)
- int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
- const unsigned char *ad, size_t ad_len );
- #endif
- int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
- size_t ilen, unsigned char *output, size_t *olen );
- int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
- unsigned char *output, size_t *olen );
- #if defined(MBEDTLS_GCM_C)
- int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
- unsigned char *tag, size_t tag_len );
- int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
- const unsigned char *tag, size_t tag_len );
- #endif
- int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
- const unsigned char *iv, size_t iv_len,
- const unsigned char *input, size_t ilen,
- unsigned char *output, size_t *olen );
- #if defined(MBEDTLS_CIPHER_MODE_AEAD)
- int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
- const unsigned char *iv, size_t iv_len,
- const unsigned char *ad, size_t ad_len,
- const unsigned char *input, size_t ilen,
- unsigned char *output, size_t *olen,
- unsigned char *tag, size_t tag_len );
- int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
- const unsigned char *iv, size_t iv_len,
- const unsigned char *ad, size_t ad_len,
- const unsigned char *input, size_t ilen,
- unsigned char *output, size_t *olen,
- const unsigned char *tag, size_t tag_len );
- #endif
- #ifdef __cplusplus
- }
- #endif
- #endif
|