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) || defined(POLARSSL_CCM_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 64 #define POLARSSL_CIPHER_VARIABLE_IV_LEN 0x01 65 #define POLARSSL_CIPHER_VARIABLE_KEY_LEN 0x02 172 #define POLARSSL_MAX_IV_LENGTH 16 174 #define POLARSSL_MAX_BLOCK_LENGTH 16 185 int (*ecb_func)(
void *ctx, operation_t mode,
186 const unsigned char *input,
unsigned char *output );
189 int (*cbc_func)(
void *ctx, operation_t mode,
size_t length,
190 unsigned char *iv,
const unsigned char *input,
191 unsigned char *output );
194 int (*cfb_func)(
void *ctx, operation_t mode,
size_t length,
size_t *iv_off,
195 unsigned char *iv,
const unsigned char *input,
196 unsigned char *output );
199 int (*ctr_func)(
void *ctx,
size_t length,
size_t *nc_off,
200 unsigned char *nonce_counter,
unsigned char *stream_block,
201 const unsigned char *input,
unsigned char *output );
204 int (*stream_func)(
void *ctx,
size_t length,
205 const unsigned char *input,
unsigned char *output );
208 int (*setkey_enc_func)(
void *ctx,
const unsigned char *key,
209 unsigned int key_length );
212 int (*setkey_dec_func)(
void *ctx,
const unsigned char *key,
213 unsigned int key_length);
216 void * (*ctx_alloc_func)( void );
219 void (*ctx_free_func)(
void *ctx );
269 void (*add_padding)(
unsigned char *output,
size_t olen,
size_t data_len );
270 int (*get_padding)(
unsigned char *input,
size_t ilen,
size_t *data_len );
332 const cipher_mode_t mode );
510 int key_length,
const operation_t operation );
512 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) 542 const unsigned char *iv,
size_t iv_len );
554 #if defined(POLARSSL_GCM_C) 567 const unsigned char *ad,
size_t ad_len );
600 size_t ilen,
unsigned char *output,
size_t *olen );
620 unsigned char *output,
size_t *olen );
622 #if defined(POLARSSL_GCM_C) 635 unsigned char *tag,
size_t tag_len );
649 const unsigned char *tag,
size_t tag_len );
680 const unsigned char *iv,
size_t iv_len,
681 const unsigned char *input,
size_t ilen,
682 unsigned char *output,
size_t *olen );
684 #if defined(POLARSSL_CIPHER_MODE_AEAD) 708 const unsigned char *iv,
size_t iv_len,
709 const unsigned char *ad,
size_t ad_len,
710 const unsigned char *input,
size_t ilen,
711 unsigned char *output,
size_t *olen,
712 unsigned char *tag,
size_t tag_len );
742 const unsigned char *iv,
size_t iv_len,
743 const unsigned char *ad,
size_t ad_len,
744 const unsigned char *input,
size_t ilen,
745 unsigned char *output,
size_t *olen,
746 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_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_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.
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...