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(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
51 !defined(inline) && !defined(__cplusplus)
52 #define inline __inline
55 #define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
56 #define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100
57 #define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180
58 #define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200
59 #define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
60 #define POLARSSL_ERR_CIPHER_AUTH_FAILED -0x6300
62 #define POLARSSL_CIPHER_VARIABLE_IV_LEN 0x01
63 #define POLARSSL_CIPHER_VARIABLE_KEY_LEN 0x02
170 #define POLARSSL_MAX_IV_LENGTH 16
172 #define POLARSSL_MAX_BLOCK_LENGTH 16
184 const unsigned char *input,
unsigned char *output );
186 #if defined(POLARSSL_CIPHER_MODE_CBC)
189 unsigned char *iv,
const unsigned char *input,
190 unsigned char *output );
193 #if defined(POLARSSL_CIPHER_MODE_CFB)
195 int (*cfb_func)(
void *ctx,
operation_t mode,
size_t length,
size_t *iv_off,
196 unsigned char *iv,
const unsigned char *input,
197 unsigned char *output );
200 #if defined(POLARSSL_CIPHER_MODE_CTR)
202 int (*ctr_func)(
void *ctx,
size_t length,
size_t *nc_off,
203 unsigned char *nonce_counter,
unsigned char *stream_block,
204 const unsigned char *input,
unsigned char *output );
207 #if defined(POLARSSL_CIPHER_MODE_STREAM)
209 int (*stream_func)(
void *ctx,
size_t length,
210 const unsigned char *input,
unsigned char *output );
214 int (*setkey_enc_func)(
void *ctx,
const unsigned char *key,
215 unsigned int key_length );
218 int (*setkey_dec_func)(
void *ctx,
const unsigned char *key,
219 unsigned int key_length);
222 void * (*ctx_alloc_func)( void );
225 void (*ctx_free_func)(
void *ctx );
274 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
276 void (*add_padding)(
unsigned char *output,
size_t olen,
size_t data_len );
277 int (*get_padding)(
unsigned char *input,
size_t ilen,
size_t *data_len );
372 #if ! defined(POLARSSL_DEPRECATED_REMOVED)
373 #if defined(POLARSSL_DEPRECATED_WARNING)
374 #define DEPRECATED __attribute__((deprecated))
528 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
558 const unsigned char *iv,
size_t iv_len );
570 #if defined(POLARSSL_GCM_C)
583 const unsigned char *ad,
size_t ad_len );
616 size_t ilen,
unsigned char *output,
size_t *olen );
636 unsigned char *output,
size_t *olen );
638 #if defined(POLARSSL_GCM_C)
651 unsigned char *tag,
size_t tag_len );
665 const unsigned char *tag,
size_t tag_len );
696 const unsigned char *iv,
size_t iv_len,
697 const unsigned char *input,
size_t ilen,
698 unsigned char *output,
size_t *olen );
700 #if defined(POLARSSL_CIPHER_MODE_AEAD)
724 const unsigned char *iv,
size_t iv_len,
725 const unsigned char *ad,
size_t ad_len,
726 const unsigned char *input,
size_t ilen,
727 unsigned char *output,
size_t *olen,
728 unsigned char *tag,
size_t tag_len );
758 const unsigned char *iv,
size_t iv_len,
759 const unsigned char *ad,
size_t ad_len,
760 const unsigned char *input,
size_t ilen,
761 unsigned char *output,
size_t *olen,
762 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...