27 #ifndef MBEDTLS_CIPHER_H
28 #define MBEDTLS_CIPHER_H
30 #if !defined(MBEDTLS_CONFIG_FILE)
33 #include MBEDTLS_CONFIG_FILE
38 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
39 #define MBEDTLS_CIPHER_MODE_AEAD
42 #if defined(MBEDTLS_CIPHER_MODE_CBC)
43 #define MBEDTLS_CIPHER_MODE_WITH_PADDING
46 #if defined(MBEDTLS_ARC4_C)
47 #define MBEDTLS_CIPHER_MODE_STREAM
50 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && !defined(inline)
51 #define inline __inline
54 #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
55 #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100
56 #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180
57 #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200
58 #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
59 #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300
61 #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01
62 #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02
169 #define MBEDTLS_MAX_IV_LENGTH 16
171 #define MBEDTLS_MAX_BLOCK_LENGTH 16
223 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
225 void (*add_padding)(
unsigned char *output,
size_t olen,
size_t data_len );
226 int (*get_padding)(
unsigned char *input,
size_t ilen,
size_t *data_len );
457 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
487 const unsigned char *iv,
size_t iv_len );
499 #if defined(MBEDTLS_GCM_C)
512 const unsigned char *ad,
size_t ad_len );
545 size_t ilen,
unsigned char *output,
size_t *olen );
565 unsigned char *output,
size_t *olen );
567 #if defined(MBEDTLS_GCM_C)
580 unsigned char *tag,
size_t tag_len );
594 const unsigned char *tag,
size_t tag_len );
625 const unsigned char *iv,
size_t iv_len,
626 const unsigned char *input,
size_t ilen,
627 unsigned char *output,
size_t *olen );
629 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
653 const unsigned char *iv,
size_t iv_len,
654 const unsigned char *ad,
size_t ad_len,
655 const unsigned char *input,
size_t ilen,
656 unsigned char *output,
size_t *olen,
657 unsigned char *tag,
size_t tag_len );
687 const unsigned char *iv,
size_t iv_len,
688 const unsigned char *ad,
size_t ad_len,
689 const unsigned char *input,
size_t ilen,
690 unsigned char *output,
size_t *olen,
691 const unsigned char *tag,
size_t tag_len );
unsigned int iv_size
IV/NONCE size, in bytes.
Key length, in bits (including parity), for DES in two key EDE.
never pad (full blocks only)
static mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(const mbedtls_cipher_context_t *ctx)
Returns the mode of operation for the cipher.
int flags
Flags for variable IV size, variable key size, etc.
static unsigned int mbedtls_cipher_get_block_size(const mbedtls_cipher_context_t *ctx)
Returns the block size of the given cipher.
const mbedtls_cipher_info_t * mbedtls_cipher_info_from_string(const char *cipher_name)
Returns the cipher information structure associated with the given cipher name.
int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Finish preparation of the given context.
static const char * mbedtls_cipher_get_name(const mbedtls_cipher_context_t *ctx)
Returns the name of the given cipher, as a string.
Compatibility names (set of defines)
int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
Set the initialization vector (IV) or nonce.
int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode)
Set padding mode, for cipher modes that use padding.
mbedtls_cipher_mode_t mode
Cipher mode (e.g.
int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic cipher update function.
unsigned int block_size
block size, in bytes
void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Free and clear the cipher-specific context of ctx.
static mbedtls_operation_t mbedtls_cipher_get_operation(const mbedtls_cipher_context_t *ctx)
Returns the operation of the given cipher.
const int * mbedtls_cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
static int mbedtls_cipher_get_key_bitlen(const mbedtls_cipher_context_t *ctx)
Returns the key length of the cipher.
const mbedtls_cipher_info_t * cipher_info
Information about the associated cipher.
zero padding (not reversible!)
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)
Returns the cipher information structure associated with the given cipher id, key size and mode...
Key length, in bits (including parity), for DES in three-key EDE.
static mbedtls_cipher_type_t mbedtls_cipher_get_type(const mbedtls_cipher_context_t *ctx)
Returns the type of the given cipher.
void * cipher_ctx
Cipher-specific context.
mbedtls_operation_t operation
Operation that the context's key has been initialised for.
int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, const unsigned char *key, int key_bitlen, const mbedtls_operation_t operation)
Set the key to use with the given context.
#define MBEDTLS_MAX_IV_LENGTH
Maximum length of any IV, in bytes.
size_t unprocessed_len
Number of bytes that still need processing.
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)
Generic autenticated decryption (AEAD ciphers).
const char * name
Name of the cipher.
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)
Generic autenticated encryption (AEAD ciphers).
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)
Generic all-in-one encryption/decryption (for all ciphers except AEAD constructs).
int key_bitlen
Key length to use.
void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Initialize a cipher_context (as NONE)
int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx, const unsigned char *ad, size_t ad_len)
Add additional data (for AEAD ciphers).
int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info)
Initialises and fills the cipher context structure with the appropriate values.
int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx, const unsigned char *tag, size_t tag_len)
Check tag for AEAD ciphers.
static int mbedtls_cipher_get_iv_size(const mbedtls_cipher_context_t *ctx)
Returns the size of the cipher's IV/NONCE in bytes.
Key length, in bits (including parity), for DES keys.
int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx, unsigned char *tag, size_t tag_len)
Write tag for AEAD ciphers.
size_t iv_size
IV size in bytes (for ciphers with variable-length IVs)
#define MBEDTLS_MAX_BLOCK_LENGTH
Maximum block size of any cipher, in bytes.
unsigned int key_bitlen
Cipher key length, in bits (default length for variable sized ciphers) (Includes parity bits for ciph...
mbedtls_cipher_type_t type
Full cipher identifier (e.g.
const mbedtls_cipher_info_t * mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type)
Returns the cipher information structure associated with the given cipher type.
const mbedtls_cipher_base_t * base
Base cipher information and functions.