mbed TLS v2.4.2
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
60 #define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380
62 #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01
63 #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68 
69 typedef enum {
79 
80 typedef enum {
131 
132 typedef enum {
137  MBEDTLS_MODE_OFB, /* Unused! */
143 
144 typedef enum {
151 
152 typedef enum {
157 
158 enum {
167 };
168 
170 #define MBEDTLS_MAX_IV_LENGTH 16
171 
172 #define MBEDTLS_MAX_BLOCK_LENGTH 16
173 
178 
183 
187 typedef struct {
190 
193 
196  unsigned int key_bitlen;
197 
199  const char * name;
200 
203  unsigned int iv_size;
204 
206  int flags;
207 
209  unsigned int block_size;
210 
213 
215 
219 typedef struct {
222 
225 
228 
229 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
230 
231  void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
232  int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
233 #endif
234 
236  unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
237 
240 
242  unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
243 
245  size_t iv_size;
246 
248  void *cipher_ctx;
249 
250 #if defined(MBEDTLS_CMAC_C)
251 
252  mbedtls_cmac_context_t *cmac_ctx;
253 #endif
255 
262 const int *mbedtls_cipher_list( void );
263 
273 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
274 
285 
299  int key_bitlen,
300  const mbedtls_cipher_mode_t mode );
301 
306 
313 
331 
340 static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx )
341 {
342  if( NULL == ctx || NULL == ctx->cipher_info )
343  return 0;
344 
345  return ctx->cipher_info->block_size;
346 }
347 
358 {
359  if( NULL == ctx || NULL == ctx->cipher_info )
360  return MBEDTLS_MODE_NONE;
361 
362  return ctx->cipher_info->mode;
363 }
364 
375 {
376  if( NULL == ctx || NULL == ctx->cipher_info )
377  return 0;
378 
379  if( ctx->iv_size != 0 )
380  return (int) ctx->iv_size;
381 
382  return (int) ctx->cipher_info->iv_size;
383 }
384 
394 {
395  if( NULL == ctx || NULL == ctx->cipher_info )
396  return MBEDTLS_CIPHER_NONE;
397 
398  return ctx->cipher_info->type;
399 }
400 
408 static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx )
409 {
410  if( NULL == ctx || NULL == ctx->cipher_info )
411  return 0;
412 
413  return ctx->cipher_info->name;
414 }
415 
426 {
427  if( NULL == ctx || NULL == ctx->cipher_info )
429 
430  return (int) ctx->cipher_info->key_bitlen;
431 }
432 
443 {
444  if( NULL == ctx || NULL == ctx->cipher_info )
445  return MBEDTLS_OPERATION_NONE;
446 
447  return ctx->operation;
448 }
449 
465 int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
466  int key_bitlen, const mbedtls_operation_t operation );
467 
468 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
469 
482 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
483 
498  const unsigned char *iv, size_t iv_len );
499 
509 
510 #if defined(MBEDTLS_GCM_C)
511 
523  const unsigned char *ad, size_t ad_len );
524 #endif /* MBEDTLS_GCM_C */
525 
555 int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
556  size_t ilen, unsigned char *output, size_t *olen );
557 
576  unsigned char *output, size_t *olen );
577 
578 #if defined(MBEDTLS_GCM_C)
579 
591  unsigned char *tag, size_t tag_len );
592 
605  const unsigned char *tag, size_t tag_len );
606 #endif /* MBEDTLS_GCM_C */
607 
636  const unsigned char *iv, size_t iv_len,
637  const unsigned char *input, size_t ilen,
638  unsigned char *output, size_t *olen );
639 
640 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
641 
664  const unsigned char *iv, size_t iv_len,
665  const unsigned char *ad, size_t ad_len,
666  const unsigned char *input, size_t ilen,
667  unsigned char *output, size_t *olen,
668  unsigned char *tag, size_t tag_len );
669 
698  const unsigned char *iv, size_t iv_len,
699  const unsigned char *ad, size_t ad_len,
700  const unsigned char *input, size_t ilen,
701  unsigned char *output, size_t *olen,
702  const unsigned char *tag, size_t tag_len );
703 #endif /* MBEDTLS_CIPHER_MODE_AEAD */
704 
705 #ifdef __cplusplus
706 }
707 #endif
708 
709 #endif /* MBEDTLS_CIPHER_H */
mbedtls_operation_t
Definition: cipher.h:152
unsigned int iv_size
IV/NONCE size, in bytes.
Definition: cipher.h:203
Key length, in bits (including parity), for DES in two key EDE.
Definition: cipher.h:164
mbedtls_cipher_padding_t
Definition: cipher.h:144
never pad (full blocks only)
Definition: cipher.h:149
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:357
int flags
Flags for variable IV size, variable key size, etc.
Definition: cipher.h:206
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:340
mbedtls_cipher_mode_t
Definition: cipher.h:132
CMAC context structure - Contains internal state information only.
Definition: cmac.h:45
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:219
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:408
Configuration options (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:187
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:192
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:209
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:442
const int * mbedtls_cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
ANSI X.923 padding.
Definition: cipher.h:147
static int mbedtls_cipher_get_key_bitlen(const mbedtls_cipher_context_t *ctx)
Returns the key length of the cipher.
Definition: cipher.h:425
mbedtls_cipher_type_t
Definition: cipher.h:80
const mbedtls_cipher_info_t * cipher_info
Information about the associated cipher.
Definition: cipher.h:221
zero padding (not reversible!)
Definition: cipher.h:148
struct mbedtls_cipher_base_t mbedtls_cipher_base_t
Base cipher information (opaque struct).
Definition: cipher.h:177
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:166
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:393
Undefined key length.
Definition: cipher.h:160
void * cipher_ctx
Cipher-specific context.
Definition: cipher.h:248
ISO/IEC 7816-4 padding.
Definition: cipher.h:146
mbedtls_operation_t operation
Operation that the context&#39;s key has been initialised for.
Definition: cipher.h:227
mbedtls_cipher_id_t
Definition: cipher.h:69
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:170
size_t unprocessed_len
Number of bytes that still need processing.
Definition: cipher.h:239
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:199
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:145
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:224
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&#39;s IV/NONCE in bytes.
Definition: cipher.h:374
Key length, in bits (including parity), for DES keys.
Definition: cipher.h:162
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:245
#define MBEDTLS_MAX_BLOCK_LENGTH
Maximum block size of any cipher, in bytes.
Definition: cipher.h:172
unsigned int key_bitlen
Cipher key length, in bits (default length for variable sized ciphers) (Includes parity bits for ciph...
Definition: cipher.h:196
mbedtls_cipher_type_t type
Full cipher identifier (e.g.
Definition: cipher.h:189
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:212