30 #ifndef POLARSSL_CIPHER_H
31 #define POLARSSL_CIPHER_H
33 #if !defined(POLARSSL_CONFIG_FILE)
36 #include POLARSSL_CONFIG_FILE
39 #if defined(POLARSSL_GCM_C)
40 #define POLARSSL_CIPHER_MODE_AEAD
43 #if defined(POLARSSL_CIPHER_MODE_CBC)
44 #define POLARSSL_CIPHER_MODE_WITH_PADDING
49 #if defined(_MSC_VER) && !defined(inline)
50 #define inline _inline
52 #if defined(__ARMCC_VERSION) && !defined(inline)
53 #define inline __inline
57 #define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
58 #define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100
59 #define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180
60 #define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200
61 #define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
62 #define POLARSSL_ERR_CIPHER_AUTH_FAILED -0x6300
162 #define POLARSSL_MAX_IV_LENGTH 16
164 #define POLARSSL_MAX_BLOCK_LENGTH 16
176 const unsigned char *input,
unsigned char *output );
180 unsigned char *iv,
const unsigned char *input,
181 unsigned char *output );
184 int (*cfb_func)(
void *ctx,
operation_t mode,
size_t length,
size_t *iv_off,
185 unsigned char *iv,
const unsigned char *input,
186 unsigned char *output );
189 int (*ctr_func)(
void *ctx,
size_t length,
size_t *nc_off,
190 unsigned char *nonce_counter,
unsigned char *stream_block,
191 const unsigned char *input,
unsigned char *output );
194 int (*stream_func)(
void *ctx,
size_t length,
195 const unsigned char *input,
unsigned char *output );
198 int (*setkey_enc_func)(
void *ctx,
const unsigned char *key,
199 unsigned int key_length );
202 int (*setkey_dec_func)(
void *ctx,
const unsigned char *key,
203 unsigned int key_length);
206 void * (*ctx_alloc_func)( void );
209 void (*ctx_free_func)(
void *ctx );
259 void (*add_padding)(
unsigned char *output,
size_t olen,
size_t data_len );
260 int (*get_padding)(
unsigned char *input,
size_t ilen,
size_t *data_len );
485 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
515 const unsigned char *iv,
size_t iv_len );
527 #if defined(POLARSSL_CIPHER_MODE_AEAD)
544 const unsigned char *ad,
size_t ad_len );
577 size_t ilen,
unsigned char *output,
size_t *olen );
597 unsigned char *output,
size_t *olen );
599 #if defined(POLARSSL_CIPHER_MODE_AEAD)
612 unsigned char *tag,
size_t tag_len );
627 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.
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.
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_free_ctx(cipher_context_t *ctx)
Free the cipher-specific context of ctx.
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_update(cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic cipher update function.
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_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 accepts_variable_iv_size
Flag for ciphers that accept many sizes of IV/NONCE.
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...