mbed TLS v2.1.0
cipher.h
Go to the documentation of this file.
1 
26 #ifndef MBEDTLS_CIPHER_H
27 #define MBEDTLS_CIPHER_H
28 
29 #if !defined(MBEDTLS_CONFIG_FILE)
30 #include "config.h"
31 #else
32 #include MBEDTLS_CONFIG_FILE
33 #endif
34 
35 #include <stddef.h>
36 
37 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
38 #define MBEDTLS_CIPHER_MODE_AEAD
39 #endif
40 
41 #if defined(MBEDTLS_CIPHER_MODE_CBC)
42 #define MBEDTLS_CIPHER_MODE_WITH_PADDING
43 #endif
44 
45 #if defined(MBEDTLS_ARC4_C)
46 #define MBEDTLS_CIPHER_MODE_STREAM
47 #endif
48 
49 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && !defined(inline)
50 #define inline __inline
51 #endif
52 
53 #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
54 #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100
55 #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180
56 #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200
57 #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
58 #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300
60 #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01
61 #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
66 
67 typedef enum {
77 
78 typedef enum {
129 
130 typedef enum {
135  MBEDTLS_MODE_OFB, /* Unused! */
141 
142 typedef enum {
149 
150 typedef enum {
155 
156 enum {
165 };
166 
168 #define MBEDTLS_MAX_IV_LENGTH 16
169 
170 #define MBEDTLS_MAX_BLOCK_LENGTH 16
171 
176 
180 typedef struct {
183 
186 
189  unsigned int key_bitlen;
190 
192  const char * name;
193 
196  unsigned int iv_size;
197 
199  int flags;
200 
202  unsigned int block_size;
203 
206 
208 
212 typedef struct {
215 
218 
221 
222 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
223 
224  void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
225  int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
226 #endif
227 
229  unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
230 
233 
235  unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
236 
238  size_t iv_size;
239 
241  void *cipher_ctx;
243 
250 const int *mbedtls_cipher_list( void );
251 
261 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
262 
273 
287  int key_bitlen,
288  const mbedtls_cipher_mode_t mode );
289 
294 
301 
319 
328 static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx )
329 {
330  if( NULL == ctx || NULL == ctx->cipher_info )
331  return 0;
332 
333  return ctx->cipher_info->block_size;
334 }
335 
346 {
347  if( NULL == ctx || NULL == ctx->cipher_info )
348  return MBEDTLS_MODE_NONE;
349 
350  return ctx->cipher_info->mode;
351 }
352 
363 {
364  if( NULL == ctx || NULL == ctx->cipher_info )
365  return 0;
366 
367  if( ctx->iv_size != 0 )
368  return (int) ctx->iv_size;
369 
370  return (int) ctx->cipher_info->iv_size;
371 }
372 
382 {
383  if( NULL == ctx || NULL == ctx->cipher_info )
384  return MBEDTLS_CIPHER_NONE;
385 
386  return ctx->cipher_info->type;
387 }
388 
396 static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx )
397 {
398  if( NULL == ctx || NULL == ctx->cipher_info )
399  return 0;
400 
401  return ctx->cipher_info->name;
402 }
403 
414 {
415  if( NULL == ctx || NULL == ctx->cipher_info )
417 
418  return (int) ctx->cipher_info->key_bitlen;
419 }
420 
431 {
432  if( NULL == ctx || NULL == ctx->cipher_info )
433  return MBEDTLS_OPERATION_NONE;
434 
435  return ctx->operation;
436 }
437 
453 int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
454  int key_bitlen, const mbedtls_operation_t operation );
455 
456 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
457 
470 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
471 
486  const unsigned char *iv, size_t iv_len );
487 
497 
498 #if defined(MBEDTLS_GCM_C)
499 
511  const unsigned char *ad, size_t ad_len );
512 #endif /* MBEDTLS_GCM_C */
513 
543 int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
544  size_t ilen, unsigned char *output, size_t *olen );
545 
564  unsigned char *output, size_t *olen );
565 
566 #if defined(MBEDTLS_GCM_C)
567 
579  unsigned char *tag, size_t tag_len );
580 
593  const unsigned char *tag, size_t tag_len );
594 #endif /* MBEDTLS_GCM_C */
595 
624  const unsigned char *iv, size_t iv_len,
625  const unsigned char *input, size_t ilen,
626  unsigned char *output, size_t *olen );
627 
628 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
629 
652  const unsigned char *iv, size_t iv_len,
653  const unsigned char *ad, size_t ad_len,
654  const unsigned char *input, size_t ilen,
655  unsigned char *output, size_t *olen,
656  unsigned char *tag, size_t tag_len );
657 
686  const unsigned char *iv, size_t iv_len,
687  const unsigned char *ad, size_t ad_len,
688  const unsigned char *input, size_t ilen,
689  unsigned char *output, size_t *olen,
690  const unsigned char *tag, size_t tag_len );
691 #endif /* MBEDTLS_CIPHER_MODE_AEAD */
692 
693 #ifdef __cplusplus
694 }
695 #endif
696 
697 #endif /* MBEDTLS_CIPHER_H */
mbedtls_operation_t
Definition: cipher.h:150
unsigned int iv_size
IV/NONCE size, in bytes.
Definition: cipher.h:196
Key length, in bits (including parity), for DES in two key EDE.
Definition: cipher.h:162
mbedtls_cipher_padding_t
Definition: cipher.h:142
never pad (full blocks only)
Definition: cipher.h:147
static mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(const mbedtls_cipher_context_t *ctx)
Returns the mode of operation for the cipher.
Definition: cipher.h:345
int flags
Flags for variable IV size, variable key size, etc.
Definition: cipher.h:199
static unsigned int mbedtls_cipher_get_block_size(const mbedtls_cipher_context_t *ctx)
Returns the block size of the given cipher.
Definition: cipher.h:328
mbedtls_cipher_mode_t
Definition: cipher.h:130
const mbedtls_cipher_info_t * mbedtls_cipher_info_from_string(const char *cipher_name)
Returns the cipher information structure associated with the given cipher name.
int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
Generic cipher context.
Definition: cipher.h:212
int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Finish preparation of the given context.
static const char * mbedtls_cipher_get_name(const mbedtls_cipher_context_t *ctx)
Returns the name of the given cipher, as a string.
Definition: cipher.h:396
Compatibility names (set of defines)
int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
Set the initialization vector (IV) or nonce.
Cipher information.
Definition: cipher.h:180
int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode)
Set padding mode, for cipher modes that use padding.
mbedtls_cipher_mode_t mode
Cipher mode (e.g.
Definition: cipher.h:185
int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic cipher update function.
unsigned int block_size
block size, in bytes
Definition: cipher.h:202
Base cipher information.
void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Free and clear the cipher-specific context of ctx.
static mbedtls_operation_t mbedtls_cipher_get_operation(const mbedtls_cipher_context_t *ctx)
Returns the operation of the given cipher.
Definition: cipher.h:430
const int * mbedtls_cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
ANSI X.923 padding.
Definition: cipher.h:145
static int mbedtls_cipher_get_key_bitlen(const mbedtls_cipher_context_t *ctx)
Returns the key length of the cipher.
Definition: cipher.h:413
mbedtls_cipher_type_t
Definition: cipher.h:78
const mbedtls_cipher_info_t * cipher_info
Information about the associated cipher.
Definition: cipher.h:214
zero padding (not reversible!)
Definition: cipher.h:146
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)
Returns the cipher information structure associated with the given cipher id, key size and mode...
Key length, in bits (including parity), for DES in three-key EDE.
Definition: cipher.h:164
static mbedtls_cipher_type_t mbedtls_cipher_get_type(const mbedtls_cipher_context_t *ctx)
Returns the type of the given cipher.
Definition: cipher.h:381
Undefined key length.
Definition: cipher.h:158
void * cipher_ctx
Cipher-specific context.
Definition: cipher.h:241
ISO/IEC 7816-4 padding.
Definition: cipher.h:144
mbedtls_operation_t operation
Operation that the context's key has been initialised for.
Definition: cipher.h:220
mbedtls_cipher_id_t
Definition: cipher.h:67
int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, const unsigned char *key, int key_bitlen, const mbedtls_operation_t operation)
Set the key to use with the given context.
#define MBEDTLS_MAX_IV_LENGTH
Maximum length of any IV, in bytes.
Definition: cipher.h:168
size_t unprocessed_len
Number of bytes that still need processing.
Definition: cipher.h:232
int mbedtls_cipher_auth_decrypt(mbedtls_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).
const char * name
Name of the cipher.
Definition: cipher.h:192
int mbedtls_cipher_auth_encrypt(mbedtls_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).
PKCS7 padding (default)
Definition: cipher.h:143
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)
Generic all-in-one encryption/decryption (for all ciphers except AEAD constructs).
int key_bitlen
Key length to use.
Definition: cipher.h:217
void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Initialize a cipher_context (as NONE)
int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx, const unsigned char *ad, size_t ad_len)
Add additional data (for AEAD ciphers).
int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info)
Initialises and fills the cipher context structure with the appropriate values.
int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx, const unsigned char *tag, size_t tag_len)
Check tag for AEAD ciphers.
static int mbedtls_cipher_get_iv_size(const mbedtls_cipher_context_t *ctx)
Returns the size of the cipher's IV/NONCE in bytes.
Definition: cipher.h:362
Key length, in bits (including parity), for DES keys.
Definition: cipher.h:160
int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx, unsigned char *tag, size_t tag_len)
Write tag for AEAD ciphers.
size_t iv_size
IV size in bytes (for ciphers with variable-length IVs)
Definition: cipher.h:238
#define MBEDTLS_MAX_BLOCK_LENGTH
Maximum block size of any cipher, in bytes.
Definition: cipher.h:170
unsigned int key_bitlen
Cipher key length, in bits (default length for variable sized ciphers) (Includes parity bits for ciph...
Definition: cipher.h:189
mbedtls_cipher_type_t type
Full cipher identifier (e.g.
Definition: cipher.h:182
const mbedtls_cipher_info_t * mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type)
Returns the cipher information structure associated with the given cipher type.
const mbedtls_cipher_base_t * base
Base cipher information and functions.
Definition: cipher.h:205