mbed TLS v2.2.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) ) && \
50  !defined(inline) && !defined(__cplusplus)
51 #define inline __inline
52 #endif
53 
54 #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
55 #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100
56 #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180
57 #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200
58 #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
59 #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300
61 #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01
62 #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 typedef enum {
78 
79 typedef enum {
130 
131 typedef enum {
136  MBEDTLS_MODE_OFB, /* Unused! */
142 
143 typedef enum {
150 
151 typedef enum {
156 
157 enum {
166 };
167 
169 #define MBEDTLS_MAX_IV_LENGTH 16
170 
171 #define MBEDTLS_MAX_BLOCK_LENGTH 16
172 
177 
181 typedef struct {
184 
187 
190  unsigned int key_bitlen;
191 
193  const char * name;
194 
197  unsigned int iv_size;
198 
200  int flags;
201 
203  unsigned int block_size;
204 
207 
209 
213 typedef struct {
216 
219 
222 
223 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
224 
225  void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
226  int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
227 #endif
228 
230  unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
231 
234 
236  unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
237 
239  size_t iv_size;
240 
242  void *cipher_ctx;
244 
251 const int *mbedtls_cipher_list( void );
252 
262 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
263 
274 
288  int key_bitlen,
289  const mbedtls_cipher_mode_t mode );
290 
295 
302 
320 
329 static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx )
330 {
331  if( NULL == ctx || NULL == ctx->cipher_info )
332  return 0;
333 
334  return ctx->cipher_info->block_size;
335 }
336 
347 {
348  if( NULL == ctx || NULL == ctx->cipher_info )
349  return MBEDTLS_MODE_NONE;
350 
351  return ctx->cipher_info->mode;
352 }
353 
364 {
365  if( NULL == ctx || NULL == ctx->cipher_info )
366  return 0;
367 
368  if( ctx->iv_size != 0 )
369  return (int) ctx->iv_size;
370 
371  return (int) ctx->cipher_info->iv_size;
372 }
373 
383 {
384  if( NULL == ctx || NULL == ctx->cipher_info )
385  return MBEDTLS_CIPHER_NONE;
386 
387  return ctx->cipher_info->type;
388 }
389 
397 static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx )
398 {
399  if( NULL == ctx || NULL == ctx->cipher_info )
400  return 0;
401 
402  return ctx->cipher_info->name;
403 }
404 
415 {
416  if( NULL == ctx || NULL == ctx->cipher_info )
418 
419  return (int) ctx->cipher_info->key_bitlen;
420 }
421 
432 {
433  if( NULL == ctx || NULL == ctx->cipher_info )
434  return MBEDTLS_OPERATION_NONE;
435 
436  return ctx->operation;
437 }
438 
454 int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
455  int key_bitlen, const mbedtls_operation_t operation );
456 
457 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
458 
470 int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode );
471 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
472 
487  const unsigned char *iv, size_t iv_len );
488 
498 
499 #if defined(MBEDTLS_GCM_C)
500 
511 int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
512  const unsigned char *ad, size_t ad_len );
513 #endif /* MBEDTLS_GCM_C */
514 
544 int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
545  size_t ilen, unsigned char *output, size_t *olen );
546 
565  unsigned char *output, size_t *olen );
566 
567 #if defined(MBEDTLS_GCM_C)
568 
579 int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
580  unsigned char *tag, size_t tag_len );
581 
593 int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
594  const unsigned char *tag, size_t tag_len );
595 #endif /* MBEDTLS_GCM_C */
596 
625  const unsigned char *iv, size_t iv_len,
626  const unsigned char *input, size_t ilen,
627  unsigned char *output, size_t *olen );
628 
629 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
630 
652 int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
653  const unsigned char *iv, size_t iv_len,
654  const unsigned char *ad, size_t ad_len,
655  const unsigned char *input, size_t ilen,
656  unsigned char *output, size_t *olen,
657  unsigned char *tag, size_t tag_len );
658 
686 int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
687  const unsigned char *iv, size_t iv_len,
688  const unsigned char *ad, size_t ad_len,
689  const unsigned char *input, size_t ilen,
690  unsigned char *output, size_t *olen,
691  const unsigned char *tag, size_t tag_len );
692 #endif /* MBEDTLS_CIPHER_MODE_AEAD */
693 
694 #ifdef __cplusplus
695 }
696 #endif
697 
698 #endif /* MBEDTLS_CIPHER_H */
mbedtls_operation_t
Definition: cipher.h:151
unsigned int iv_size
IV/NONCE size, in bytes.
Definition: cipher.h:197
Key length, in bits (including parity), for DES in two key EDE.
Definition: cipher.h:163
mbedtls_cipher_padding_t
Definition: cipher.h:143
never pad (full blocks only)
Definition: cipher.h:148
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:346
int flags
Flags for variable IV size, variable key size, etc.
Definition: cipher.h:200
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:329
mbedtls_cipher_mode_t
Definition: cipher.h:131
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:213
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:397
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:181
mbedtls_cipher_mode_t mode
Cipher mode (e.g.
Definition: cipher.h:186
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:203
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:431
const int * mbedtls_cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
ANSI X.923 padding.
Definition: cipher.h:146
static int mbedtls_cipher_get_key_bitlen(const mbedtls_cipher_context_t *ctx)
Returns the key length of the cipher.
Definition: cipher.h:414
mbedtls_cipher_type_t
Definition: cipher.h:79
const mbedtls_cipher_info_t * cipher_info
Information about the associated cipher.
Definition: cipher.h:215
zero padding (not reversible!)
Definition: cipher.h:147
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:165
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:382
Undefined key length.
Definition: cipher.h:159
void * cipher_ctx
Cipher-specific context.
Definition: cipher.h:242
ISO/IEC 7816-4 padding.
Definition: cipher.h:145
mbedtls_operation_t operation
Operation that the context's key has been initialised for.
Definition: cipher.h:221
mbedtls_cipher_id_t
Definition: cipher.h:68
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:169
size_t unprocessed_len
Number of bytes that still need processing.
Definition: cipher.h:233
const char * name
Name of the cipher.
Definition: cipher.h:193
PKCS7 padding (default)
Definition: cipher.h:144
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:218
void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Initialize a cipher_context (as NONE)
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.
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:363
Key length, in bits (including parity), for DES keys.
Definition: cipher.h:161
size_t iv_size
IV size in bytes (for ciphers with variable-length IVs)
Definition: cipher.h:239
#define MBEDTLS_MAX_BLOCK_LENGTH
Maximum block size of any cipher, in bytes.
Definition: cipher.h:171
unsigned int key_bitlen
Cipher key length, in bits (default length for variable sized ciphers) (Includes parity bits for ciph...
Definition: cipher.h:190
mbedtls_cipher_type_t type
Full cipher identifier (e.g.
Definition: cipher.h:183
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:206