27 #ifndef POLARSSL_CIPHER_H
28 #define POLARSSL_CIPHER_H
30 #if !defined(POLARSSL_CONFIG_FILE)
33 #include POLARSSL_CONFIG_FILE
38 #if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
39 #define POLARSSL_CIPHER_MODE_AEAD
42 #if defined(POLARSSL_CIPHER_MODE_CBC)
43 #define POLARSSL_CIPHER_MODE_WITH_PADDING
46 #if defined(POLARSSL_ARC4_C)
47 #define POLARSSL_CIPHER_MODE_STREAM
50 #if defined(_MSC_VER) && !defined(inline)
51 #define inline _inline
53 #if defined(__ARMCC_VERSION) && !defined(inline)
54 #define inline __inline
58 #define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
59 #define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100
60 #define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180
61 #define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200
62 #define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
63 #define POLARSSL_ERR_CIPHER_AUTH_FAILED -0x6300
65 #define POLARSSL_CIPHER_VARIABLE_IV_LEN 0x01
66 #define POLARSSL_CIPHER_VARIABLE_KEY_LEN 0x02
173 #define POLARSSL_MAX_IV_LENGTH 16
175 #define POLARSSL_MAX_BLOCK_LENGTH 16
187 const unsigned char *input,
unsigned char *output );
189 #if defined(POLARSSL_CIPHER_MODE_CBC)
192 unsigned char *iv,
const unsigned char *input,
193 unsigned char *output );
196 #if defined(POLARSSL_CIPHER_MODE_CFB)
198 int (*cfb_func)(
void *ctx,
operation_t mode,
size_t length,
size_t *iv_off,
199 unsigned char *iv,
const unsigned char *input,
200 unsigned char *output );
203 #if defined(POLARSSL_CIPHER_MODE_CTR)
205 int (*ctr_func)(
void *ctx,
size_t length,
size_t *nc_off,
206 unsigned char *nonce_counter,
unsigned char *stream_block,
207 const unsigned char *input,
unsigned char *output );
210 #if defined(POLARSSL_CIPHER_MODE_STREAM)
212 int (*stream_func)(
void *ctx,
size_t length,
213 const unsigned char *input,
unsigned char *output );
217 int (*setkey_enc_func)(
void *ctx,
const unsigned char *key,
218 unsigned int key_length );
221 int (*setkey_dec_func)(
void *ctx,
const unsigned char *key,
222 unsigned int key_length);
225 void * (*ctx_alloc_func)( void );
228 void (*ctx_free_func)(
void *ctx );
277 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
279 void (*add_padding)(
unsigned char *output,
size_t olen,
size_t data_len );
280 int (*get_padding)(
unsigned char *input,
size_t ilen,
size_t *data_len );
375 #if ! defined(POLARSSL_DEPRECATED_REMOVED)
376 #if defined(POLARSSL_DEPRECATED_WARNING)
377 #define DEPRECATED __attribute__((deprecated))
531 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
561 const unsigned char *iv,
size_t iv_len );
573 #if defined(POLARSSL_GCM_C)
586 const unsigned char *ad,
size_t ad_len );
619 size_t ilen,
unsigned char *output,
size_t *olen );
639 unsigned char *output,
size_t *olen );
641 #if defined(POLARSSL_GCM_C)
654 unsigned char *tag,
size_t tag_len );
668 const unsigned char *tag,
size_t tag_len );
699 const unsigned char *iv,
size_t iv_len,
700 const unsigned char *input,
size_t ilen,
701 unsigned char *output,
size_t *olen );
703 #if defined(POLARSSL_CIPHER_MODE_AEAD)
727 const unsigned char *iv,
size_t iv_len,
728 const unsigned char *ad,
size_t ad_len,
729 const unsigned char *input,
size_t ilen,
730 unsigned char *output,
size_t *olen,
731 unsigned char *tag,
size_t tag_len );
761 const unsigned char *iv,
size_t iv_len,
762 const unsigned char *ad,
size_t ad_len,
763 const unsigned char *input,
size_t ilen,
764 unsigned char *output,
size_t *olen,
765 const unsigned char *tag,
size_t tag_len );
int key_length
Key length to use.
int cipher_finish(cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
static int cipher_get_iv_size(const cipher_context_t *ctx)
Returns the size of the cipher's IV/NONCE in bytes.
Key length, in bits (including parity), for DES keys.
cipher_type_t type
Full cipher identifier (e.g.
void cipher_init(cipher_context_t *ctx)
Initialize a cipher_context (as NONE)
static cipher_mode_t cipher_get_cipher_mode(const cipher_context_t *ctx)
Returns the mode of operation for the cipher.
int cipher_write_tag(cipher_context_t *ctx, unsigned char *tag, size_t tag_len)
Write tag for AEAD ciphers.
zero padding (not reversible!)
const cipher_info_t * cipher_info_from_type(const cipher_type_t cipher_type)
Returns the cipher information structure associated with the given cipher type.
static unsigned int cipher_get_block_size(const cipher_context_t *ctx)
Returns the block size of the given cipher.
const cipher_info_t * cipher_info_from_string(const char *cipher_name)
Returns the cipher information structure associated with the given cipher name.
Configuration options (set of defines)
static const char * cipher_get_name(const cipher_context_t *ctx)
Returns the name of the given cipher, as a string.
static cipher_type_t cipher_get_type(const cipher_context_t *ctx)
Returns the type of the given cipher.
int cipher_crypt(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).
const cipher_info_t * cipher_info
Information about the associated cipher.
operation_t operation
Operation that the context's key has been initialised for.
#define POLARSSL_MAX_BLOCK_LENGTH
Maximum block size of any cipher, in bytes.
size_t unprocessed_len
Number of bytes that still need processing.
int cipher_update_ad(cipher_context_t *ctx, const unsigned char *ad, size_t ad_len)
Add additional data (for AEAD ciphers).
unsigned int key_length
Cipher key length, in bits (default length for variable sized ciphers) (Includes parity bits for ciph...
int cipher_set_iv(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
Set the initialization vector (IV) or nonce.
int cipher_auth_encrypt(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 cipher_update(cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic cipher update function.
int cipher_auth_decrypt(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).
Key length, in bits (including parity), for DES in three-key EDE.
const char * name
Name of the cipher.
#define POLARSSL_MAX_IV_LENGTH
Maximum length of any IV, in bytes.
int cipher_reset(cipher_context_t *ctx)
Finish preparation of the given context.
cipher_id_t cipher
Base Cipher type (e.g.
int cipher_free_ctx(cipher_context_t *ctx) DEPRECATED
Free the cipher-specific context of ctx.
void cipher_free(cipher_context_t *ctx)
Free and clear the cipher-specific context of ctx.
int cipher_set_padding_mode(cipher_context_t *ctx, cipher_padding_t mode)
Set padding mode, for cipher modes that use padding.
cipher_mode_t mode
Cipher mode (e.g.
static operation_t cipher_get_operation(const cipher_context_t *ctx)
Returns the operation of the given cipher.
int cipher_init_ctx(cipher_context_t *ctx, const cipher_info_t *cipher_info)
Initialises and fills the cipher context structure with the appropriate values.
int cipher_setkey(cipher_context_t *ctx, const unsigned char *key, int key_length, const operation_t operation)
Set the key to use with the given context.
never pad (full blocks only)
const cipher_base_t * base
Base cipher information and functions.
const int * cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
unsigned int block_size
block size, in bytes
void * cipher_ctx
Cipher-specific context.
static int cipher_get_key_size(const cipher_context_t *ctx)
Returns the key length of the cipher.
int cipher_self_test(int verbose)
Checkup routine.
size_t iv_size
IV size in bytes (for ciphers with variable-length IVs)
int flags
Flags for variable IV size, variable key size, etc.
int cipher_check_tag(cipher_context_t *ctx, const unsigned char *tag, size_t tag_len)
Check tag for AEAD ciphers.
unsigned int iv_size
IV/NONCE size, in bytes.
Key length, in bits (including parity), for DES in two key EDE.
const cipher_info_t * cipher_info_from_values(const cipher_id_t cipher_id, int key_length, const cipher_mode_t mode)
Returns the cipher information structure associated with the given cipher id, key size and mode...