27#ifndef MBEDTLS_CIPHER_H
28#define MBEDTLS_CIPHER_H
30#if !defined(MBEDTLS_CONFIG_FILE)
33#include MBEDTLS_CONFIG_FILE
39#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
40#define MBEDTLS_CIPHER_MODE_AEAD
43#if defined(MBEDTLS_CIPHER_MODE_CBC)
44#define MBEDTLS_CIPHER_MODE_WITH_PADDING
47#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
48 defined(MBEDTLS_CHACHA20_C)
49#define MBEDTLS_CIPHER_MODE_STREAM
52#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
53 !defined(inline) && !defined(__cplusplus)
54#define inline __inline
57#define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
58#define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100
59#define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180
60#define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200
61#define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
62#define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300
63#define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380
66#define MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED -0x6400
68#define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01
69#define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02
220 MBEDTLS_OPERATION_NONE = -1,
240#define MBEDTLS_MAX_IV_LENGTH 16
246#define MBEDTLS_MAX_BLOCK_LENGTH 16
255#if defined(MBEDTLS_CIPHER_MODE_XTS)
256#define MBEDTLS_MAX_KEY_LENGTH 64
258#define MBEDTLS_MAX_KEY_LENGTH 32
330#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
334 void (*add_padding)(
unsigned char *output,
size_t olen,
size_t data_len );
335 int (*get_padding)(
unsigned char *input,
size_t ilen,
size_t *data_len );
354#if defined(MBEDTLS_CMAC_C)
359#if defined(MBEDTLS_USE_PSA_CRYPTO)
367 unsigned char psa_enabled;
469#if defined(MBEDTLS_USE_PSA_CRYPTO)
504static inline unsigned int mbedtls_cipher_get_block_size(
507 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
543static inline int mbedtls_cipher_get_iv_size(
546 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
567 MBEDTLS_INTERNAL_VALIDATE_RET(
584static inline const char *mbedtls_cipher_get_name(
587 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
603static inline int mbedtls_cipher_get_key_bitlen(
606 MBEDTLS_INTERNAL_VALIDATE_RET(
625 MBEDTLS_INTERNAL_VALIDATE_RET(
626 ctx != NULL, MBEDTLS_OPERATION_NONE );
628 return MBEDTLS_OPERATION_NONE;
650 const unsigned char *key,
654#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
694 const unsigned char *iv,
708#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
724 const unsigned char *ad,
size_t ad_len );
762 const unsigned char *input,
763 size_t ilen,
unsigned char *output,
789 unsigned char *output,
size_t *olen );
791#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
809 unsigned char *tag,
size_t tag_len );
825 const unsigned char *tag,
size_t tag_len );
862 const unsigned char *iv,
size_t iv_len,
863 const unsigned char *input,
size_t ilen,
864 unsigned char *output,
size_t *olen );
866#if defined(MBEDTLS_CIPHER_MODE_AEAD)
867#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
868#if defined(MBEDTLS_DEPRECATED_WARNING)
869#define MBEDTLS_DEPRECATED __attribute__((deprecated))
871#define MBEDTLS_DEPRECATED
920 const unsigned char *iv,
size_t iv_len,
921 const unsigned char *ad,
size_t ad_len,
922 const unsigned char *input,
size_t ilen,
923 unsigned char *output,
size_t *olen,
924 unsigned char *tag,
size_t tag_len )
979 const unsigned char *iv,
size_t iv_len,
980 const unsigned char *ad,
size_t ad_len,
981 const unsigned char *input,
size_t ilen,
982 unsigned char *output,
size_t *olen,
983 const unsigned char *tag,
size_t tag_len )
985#undef MBEDTLS_DEPRECATED
989#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1035 const unsigned char *iv,
size_t iv_len,
1036 const unsigned char *ad,
size_t ad_len,
1037 const unsigned char *input,
size_t ilen,
1038 unsigned char *output,
size_t output_len,
1039 size_t *olen,
size_t tag_len );
1091 const unsigned char *iv,
size_t iv_len,
1092 const unsigned char *ad,
size_t ad_len,
1093 const unsigned char *input,
size_t ilen,
1094 unsigned char *output,
size_t output_len,
1095 size_t *olen,
size_t tag_len );
Configuration options (set of defines)
int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info)
This function initializes a cipher context for use with the given cipher primitive.
mbedtls_cipher_type_t
Supported {cipher type, cipher mode} pairs.
int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, const unsigned char *key, int key_bitlen, const mbedtls_operation_t operation)
This function sets the key to use with the given context.
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)
The generic all-in-one encryption/decryption function, for all ciphers except AEAD constructs.
const mbedtls_cipher_info_t * mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type)
This function retrieves the cipher-information structure associated with the given cipher type.
int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
This function resets the cipher state.
int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
This function sets the initialization vector (IV) or nonce.
mbedtls_cipher_padding_t
Supported cipher padding types.
int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx, unsigned char *output, size_t *olen)
The generic cipher finalization function.
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)
This function retrieves the cipher-information structure associated with the given cipher ID,...
void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
This function initializes a cipher_context as NONE.
void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
This function frees and clears the cipher-specific context of ctx.
int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
The generic cipher update function.
mbedtls_operation_t
Type of operation.
const mbedtls_cipher_info_t * mbedtls_cipher_info_from_string(const char *cipher_name)
This function retrieves the cipher-information structure associated with the given cipher name.
const int * mbedtls_cipher_list(void)
This function retrieves the list of ciphers supported by the generic cipher module.
#define MBEDTLS_MAX_BLOCK_LENGTH
Maximum block size of any cipher, in Bytes.
mbedtls_cipher_mode_t
Supported cipher modes.
#define MBEDTLS_MAX_IV_LENGTH
Maximum length of any IV, in Bytes.
mbedtls_cipher_id_t
Supported cipher types.
@ MBEDTLS_CIPHER_AES_128_ECB
AES cipher with 128-bit ECB mode.
@ MBEDTLS_CIPHER_ARIA_256_CTR
Aria cipher with 256-bit key and CTR mode.
@ MBEDTLS_CIPHER_CAMELLIA_128_GCM
Camellia cipher with 128-bit GCM mode.
@ MBEDTLS_CIPHER_AES_128_XTS
AES 128-bit cipher in XTS block mode.
@ MBEDTLS_CIPHER_CHACHA20
ChaCha20 stream cipher.
@ MBEDTLS_CIPHER_DES_EDE3_CBC
DES cipher with EDE3 CBC mode.
@ MBEDTLS_CIPHER_DES_ECB
DES cipher with ECB mode.
@ MBEDTLS_CIPHER_ARIA_128_GCM
Aria cipher with 128-bit key and GCM mode.
@ MBEDTLS_CIPHER_AES_128_CBC
AES cipher with 128-bit CBC mode.
@ MBEDTLS_CIPHER_AES_192_GCM
AES cipher with 192-bit GCM mode.
@ MBEDTLS_CIPHER_BLOWFISH_CTR
Blowfish cipher with CTR mode.
@ MBEDTLS_CIPHER_AES_128_OFB
AES 128-bit cipher in OFB mode.
@ MBEDTLS_CIPHER_ARIA_192_ECB
Aria cipher with 192-bit key and ECB mode.
@ MBEDTLS_CIPHER_CAMELLIA_256_GCM
Camellia cipher with 256-bit GCM mode.
@ MBEDTLS_CIPHER_DES_EDE_ECB
DES cipher with EDE ECB mode.
@ MBEDTLS_CIPHER_BLOWFISH_CFB64
Blowfish cipher with CFB64 mode.
@ MBEDTLS_CIPHER_ARIA_256_CFB128
Aria cipher with 256-bit key and CFB-128 mode.
@ MBEDTLS_CIPHER_ARIA_192_CBC
Aria cipher with 192-bit key and CBC mode.
@ MBEDTLS_CIPHER_CAMELLIA_192_CBC
Camellia cipher with 192-bit CBC mode.
@ MBEDTLS_CIPHER_ARIA_128_CTR
Aria cipher with 128-bit key and CTR mode.
@ MBEDTLS_CIPHER_ARIA_192_CCM
Aria cipher with 192-bit key and CCM mode.
@ MBEDTLS_CIPHER_CAMELLIA_192_GCM
Camellia cipher with 192-bit GCM mode.
@ MBEDTLS_CIPHER_AES_192_OFB
AES 192-bit cipher in OFB mode.
@ MBEDTLS_CIPHER_AES_256_ECB
AES cipher with 256-bit ECB mode.
@ MBEDTLS_CIPHER_AES_256_CTR
AES cipher with 256-bit CTR mode.
@ MBEDTLS_CIPHER_AES_192_CCM
AES cipher with 192-bit CCM mode.
@ MBEDTLS_CIPHER_AES_128_CFB128
AES cipher with 128-bit CFB128 mode.
@ MBEDTLS_CIPHER_CAMELLIA_192_CFB128
Camellia cipher with 192-bit CFB128 mode.
@ MBEDTLS_CIPHER_CAMELLIA_128_CCM
Camellia cipher with 128-bit CCM mode.
@ MBEDTLS_CIPHER_AES_128_CTR
AES cipher with 128-bit CTR mode.
@ MBEDTLS_CIPHER_ARIA_192_GCM
Aria cipher with 192-bit key and GCM mode.
@ MBEDTLS_CIPHER_AES_256_XTS
AES 256-bit cipher in XTS block mode.
@ MBEDTLS_CIPHER_AES_192_CFB128
AES cipher with 192-bit CFB128 mode.
@ MBEDTLS_CIPHER_ARIA_256_ECB
Aria cipher with 256-bit key and ECB mode.
@ MBEDTLS_CIPHER_CAMELLIA_256_CCM
Camellia cipher with 256-bit CCM mode.
@ MBEDTLS_CIPHER_AES_256_GCM
AES cipher with 256-bit GCM mode.
@ MBEDTLS_CIPHER_DES_CBC
DES cipher with CBC mode.
@ MBEDTLS_CIPHER_CAMELLIA_128_CFB128
Camellia cipher with 128-bit CFB128 mode.
@ MBEDTLS_CIPHER_CAMELLIA_128_CBC
Camellia cipher with 128-bit CBC mode.
@ MBEDTLS_CIPHER_AES_256_CCM
AES cipher with 256-bit CCM mode.
@ MBEDTLS_CIPHER_CAMELLIA_256_CFB128
Camellia cipher with 256-bit CFB128 mode.
@ MBEDTLS_CIPHER_ARIA_192_CTR
Aria cipher with 192-bit key and CTR mode.
@ MBEDTLS_CIPHER_BLOWFISH_CBC
Blowfish cipher with CBC mode.
@ MBEDTLS_CIPHER_CAMELLIA_256_ECB
Camellia cipher with 256-bit ECB mode.
@ MBEDTLS_CIPHER_AES_256_KW
AES cipher with 256-bit NIST KW mode.
@ MBEDTLS_CIPHER_AES_128_GCM
AES cipher with 128-bit GCM mode.
@ MBEDTLS_CIPHER_CAMELLIA_192_ECB
Camellia cipher with 192-bit ECB mode.
@ MBEDTLS_CIPHER_AES_256_CFB128
AES cipher with 256-bit CFB128 mode.
@ MBEDTLS_CIPHER_NONE
Placeholder to mark the end of cipher-pair lists.
@ MBEDTLS_CIPHER_CHACHA20_POLY1305
ChaCha20-Poly1305 AEAD cipher.
@ MBEDTLS_CIPHER_CAMELLIA_128_ECB
Camellia cipher with 128-bit ECB mode.
@ MBEDTLS_CIPHER_AES_192_CBC
AES cipher with 192-bit CBC mode.
@ MBEDTLS_CIPHER_CAMELLIA_192_CCM
Camellia cipher with 192-bit CCM mode.
@ MBEDTLS_CIPHER_ARIA_128_CCM
Aria cipher with 128-bit key and CCM mode.
@ MBEDTLS_CIPHER_AES_192_CTR
AES cipher with 192-bit CTR mode.
@ MBEDTLS_CIPHER_AES_128_CCM
AES cipher with 128-bit CCM mode.
@ MBEDTLS_CIPHER_DES_EDE_CBC
DES cipher with EDE CBC mode.
@ MBEDTLS_CIPHER_NULL
The identity stream cipher.
@ MBEDTLS_CIPHER_ARIA_256_CBC
Aria cipher with 256-bit key and CBC mode.
@ MBEDTLS_CIPHER_AES_256_OFB
AES 256-bit cipher in OFB mode.
@ MBEDTLS_CIPHER_ARIA_192_CFB128
Aria cipher with 192-bit key and CFB-128 mode.
@ MBEDTLS_CIPHER_CAMELLIA_128_CTR
Camellia cipher with 128-bit CTR mode.
@ MBEDTLS_CIPHER_BLOWFISH_ECB
Blowfish cipher with ECB mode.
@ MBEDTLS_CIPHER_AES_256_KWP
AES cipher with 256-bit NIST KWP mode.
@ MBEDTLS_CIPHER_AES_256_CBC
AES cipher with 256-bit CBC mode.
@ MBEDTLS_CIPHER_ARC4_128
RC4 cipher with 128-bit mode.
@ MBEDTLS_CIPHER_CAMELLIA_192_CTR
Camellia cipher with 192-bit CTR mode.
@ MBEDTLS_CIPHER_AES_128_KW
AES cipher with 128-bit NIST KW mode.
@ MBEDTLS_CIPHER_AES_192_KW
AES cipher with 192-bit NIST KW mode.
@ MBEDTLS_CIPHER_AES_192_KWP
AES cipher with 192-bit NIST KWP mode.
@ MBEDTLS_CIPHER_AES_192_ECB
AES cipher with 192-bit ECB mode.
@ MBEDTLS_CIPHER_ARIA_256_GCM
Aria cipher with 256-bit key and GCM mode.
@ MBEDTLS_CIPHER_AES_128_KWP
AES cipher with 128-bit NIST KWP mode.
@ MBEDTLS_CIPHER_DES_EDE3_ECB
DES cipher with EDE3 ECB mode.
@ MBEDTLS_CIPHER_ARIA_128_CBC
Aria cipher with 128-bit key and CBC mode.
@ MBEDTLS_CIPHER_CAMELLIA_256_CTR
Camellia cipher with 256-bit CTR mode.
@ MBEDTLS_CIPHER_ARIA_128_ECB
Aria cipher with 128-bit key and ECB mode.
@ MBEDTLS_CIPHER_CAMELLIA_256_CBC
Camellia cipher with 256-bit CBC mode.
@ MBEDTLS_CIPHER_ARIA_256_CCM
Aria cipher with 256-bit key and CCM mode.
@ MBEDTLS_CIPHER_ARIA_128_CFB128
Aria cipher with 128-bit key and CFB-128 mode.
@ MBEDTLS_PADDING_ZEROS
Zero padding (not reversible).
@ MBEDTLS_PADDING_ONE_AND_ZEROS
ISO/IEC 7816-4 padding.
@ MBEDTLS_PADDING_PKCS7
PKCS7 padding (default).
@ MBEDTLS_PADDING_ZEROS_AND_LEN
ANSI X.923 padding.
@ MBEDTLS_PADDING_NONE
Never pad (full blocks only).
@ MBEDTLS_DECRYPT
Operation type for decryption.
@ MBEDTLS_ENCRYPT
Operation type for encryption.
@ MBEDTLS_KEY_LENGTH_DES
Key length, in bits (including parity), for DES keys.
@ MBEDTLS_KEY_LENGTH_NONE
Undefined key length.
@ MBEDTLS_KEY_LENGTH_DES_EDE
Key length in bits, including parity, for DES in two-key EDE.
@ MBEDTLS_KEY_LENGTH_DES_EDE3
Key length in bits, including parity, for DES in three-key EDE.
@ MBEDTLS_MODE_ECB
The ECB cipher mode.
@ MBEDTLS_MODE_CCM
The CCM cipher mode.
@ MBEDTLS_MODE_STREAM
The stream cipher mode.
@ MBEDTLS_MODE_CFB
The CFB cipher mode.
@ MBEDTLS_MODE_CTR
The CTR cipher mode.
@ MBEDTLS_MODE_GCM
The GCM cipher mode.
@ MBEDTLS_MODE_KW
The SP800-38F KW mode.
@ MBEDTLS_MODE_CBC
The CBC cipher mode.
@ MBEDTLS_MODE_OFB
The OFB cipher mode.
@ MBEDTLS_MODE_KWP
The SP800-38F KWP mode.
@ MBEDTLS_MODE_CHACHAPOLY
The ChaCha-Poly cipher mode.
@ MBEDTLS_MODE_XTS
The XTS cipher mode.
@ MBEDTLS_CIPHER_ID_3DES
The Triple DES cipher.
@ MBEDTLS_CIPHER_ID_CAMELLIA
The Camellia cipher.
@ MBEDTLS_CIPHER_ID_DES
The DES cipher.
@ MBEDTLS_CIPHER_ID_ARC4
The RC4 cipher.
@ MBEDTLS_CIPHER_ID_NULL
The identity cipher, treated as a stream cipher.
@ MBEDTLS_CIPHER_ID_AES
The AES cipher.
@ MBEDTLS_CIPHER_ID_ARIA
The Aria cipher.
@ MBEDTLS_CIPHER_ID_NONE
Placeholder to mark the end of cipher ID lists.
@ MBEDTLS_CIPHER_ID_CHACHA20
The ChaCha20 cipher.
@ MBEDTLS_CIPHER_ID_BLOWFISH
The Blowfish cipher.
size_t iv_size
IV size in Bytes, for ciphers with variable-length IVs.
mbedtls_cmac_context_t * cmac_ctx
CMAC-specific context.
void * cipher_ctx
The cipher-specific context.
size_t unprocessed_len
Number of Bytes that have not been processed yet.
unsigned char unprocessed_data[16]
Buffer for input that has not been processed yet.
int key_bitlen
Key length to use.
const mbedtls_cipher_info_t * cipher_info
Information about the associated cipher.
mbedtls_operation_t operation
Operation that the key of the context has been initialized for.
unsigned char iv[16]
Current IV or NONCE_COUNTER for CTR-mode, data unit (or sector) number for XTS-mode.
mbedtls_cipher_type_t type
Full cipher identifier.
const mbedtls_cipher_base_t * base
Struct for base cipher information and functions.
unsigned int key_bitlen
The cipher key length, in bits.
const char * name
Name of the cipher.
int flags
Bitflag comprised of MBEDTLS_CIPHER_VARIABLE_IV_LEN and MBEDTLS_CIPHER_VARIABLE_KEY_LEN indicating wh...
unsigned int block_size
The block size, in Bytes.
unsigned int iv_size
IV or nonce size, in Bytes.
mbedtls_cipher_mode_t mode
The cipher mode.
The CMAC context structure.