mbed TLS v2.3.0
cipher.h
Go to the documentation of this file.
1 
28 #ifndef MBEDTLS_CIPHER_H
29 #define MBEDTLS_CIPHER_H
30 
31 #if !defined(MBEDTLS_CONFIG_FILE)
32 #include "config.h"
33 #else
34 #include MBEDTLS_CONFIG_FILE
35 #endif
36 
37 #include <stddef.h>
38 
39 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
40 #define MBEDTLS_CIPHER_MODE_AEAD
41 #endif
42 
43 #if defined(MBEDTLS_CIPHER_MODE_CBC)
44 #define MBEDTLS_CIPHER_MODE_WITH_PADDING
45 #endif
46 
47 #if defined(MBEDTLS_ARC4_C)
48 #define MBEDTLS_CIPHER_MODE_STREAM
49 #endif
50 
51 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
52  !defined(inline) && !defined(__cplusplus)
53 #define inline __inline
54 #endif
55 
56 #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
57 #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100
58 #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180
59 #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200
60 #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
61 #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300
62 #define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380
64 #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01
65 #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70 
71 typedef enum {
81 
82 typedef enum {
133 
134 typedef enum {
139  MBEDTLS_MODE_OFB, /* Unused! */
145 
146 typedef enum {
153 
154 typedef enum {
159 
160 enum {
169 };
170 
172 #define MBEDTLS_MAX_IV_LENGTH 16
173 
174 #define MBEDTLS_MAX_BLOCK_LENGTH 16
175 
180 
184 typedef struct {
187 
190 
193  unsigned int key_bitlen;
194 
196  const char * name;
197 
200  unsigned int iv_size;
201 
203  int flags;
204 
206  unsigned int block_size;
207 
210 
212 
216 typedef struct {
219 
222 
225 
226 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
227 
228  void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
229  int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
230 #endif
231 
233  unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
234 
237 
239  unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
240 
242  size_t iv_size;
243 
245  void *cipher_ctx;
247 
254 const int *mbedtls_cipher_list( void );
255 
265 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
266 
277 
291  int key_bitlen,
292  const mbedtls_cipher_mode_t mode );
293 
298 
305 
323 
332 static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx )
333 {
334  if( NULL == ctx || NULL == ctx->cipher_info )
335  return 0;
336 
337  return ctx->cipher_info->block_size;
338 }
339 
350 {
351  if( NULL == ctx || NULL == ctx->cipher_info )
352  return MBEDTLS_MODE_NONE;
353 
354  return ctx->cipher_info->mode;
355 }
356 
367 {
368  if( NULL == ctx || NULL == ctx->cipher_info )
369  return 0;
370 
371  if( ctx->iv_size != 0 )
372  return (int) ctx->iv_size;
373 
374  return (int) ctx->cipher_info->iv_size;
375 }
376 
386 {
387  if( NULL == ctx || NULL == ctx->cipher_info )
388  return MBEDTLS_CIPHER_NONE;
389 
390  return ctx->cipher_info->type;
391 }
392 
400 static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx )
401 {
402  if( NULL == ctx || NULL == ctx->cipher_info )
403  return 0;
404 
405  return ctx->cipher_info->name;
406 }
407 
418 {
419  if( NULL == ctx || NULL == ctx->cipher_info )
421 
422  return (int) ctx->cipher_info->key_bitlen;
423 }
424 
435 {
436  if( NULL == ctx || NULL == ctx->cipher_info )
437  return MBEDTLS_OPERATION_NONE;
438 
439  return ctx->operation;
440 }
441 
457 int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
458  int key_bitlen, const mbedtls_operation_t operation );
459 
460 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
461 
474 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
475 
490  const unsigned char *iv, size_t iv_len );
491 
501 
502 #if defined(MBEDTLS_GCM_C)
503 
515  const unsigned char *ad, size_t ad_len );
516 #endif /* MBEDTLS_GCM_C */
517 
547 int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
548  size_t ilen, unsigned char *output, size_t *olen );
549 
568  unsigned char *output, size_t *olen );
569 
570 #if defined(MBEDTLS_GCM_C)
571 
583  unsigned char *tag, size_t tag_len );
584 
597  const unsigned char *tag, size_t tag_len );
598 #endif /* MBEDTLS_GCM_C */
599 
628  const unsigned char *iv, size_t iv_len,
629  const unsigned char *input, size_t ilen,
630  unsigned char *output, size_t *olen );
631 
632 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
633 
656  const unsigned char *iv, size_t iv_len,
657  const unsigned char *ad, size_t ad_len,
658  const unsigned char *input, size_t ilen,
659  unsigned char *output, size_t *olen,
660  unsigned char *tag, size_t tag_len );
661 
690  const unsigned char *iv, size_t iv_len,
691  const unsigned char *ad, size_t ad_len,
692  const unsigned char *input, size_t ilen,
693  unsigned char *output, size_t *olen,
694  const unsigned char *tag, size_t tag_len );
695 #endif /* MBEDTLS_CIPHER_MODE_AEAD */
696 
697 #ifdef __cplusplus
698 }
699 #endif
700 
701 #endif /* MBEDTLS_CIPHER_H */
mbedtls_operation_t
Definition: cipher.h:154
unsigned int iv_size
IV/NONCE size, in bytes.
Definition: cipher.h:200
Key length, in bits (including parity), for DES in two key EDE.
Definition: cipher.h:166
mbedtls_cipher_padding_t
Definition: cipher.h:146
never pad (full blocks only)
Definition: cipher.h:151
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:349
int flags
Flags for variable IV size, variable key size, etc.
Definition: cipher.h:203
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:332
mbedtls_cipher_mode_t
Definition: cipher.h:134
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:216
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:400
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:184
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:189
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:206
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:434
const int * mbedtls_cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
ANSI X.923 padding.
Definition: cipher.h:149
static int mbedtls_cipher_get_key_bitlen(const mbedtls_cipher_context_t *ctx)
Returns the key length of the cipher.
Definition: cipher.h:417
mbedtls_cipher_type_t
Definition: cipher.h:82
const mbedtls_cipher_info_t * cipher_info
Information about the associated cipher.
Definition: cipher.h:218
zero padding (not reversible!)
Definition: cipher.h:150
struct mbedtls_cipher_base_t mbedtls_cipher_base_t
Base cipher information (opaque struct).
Definition: cipher.h:179
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:168
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:385
Undefined key length.
Definition: cipher.h:162
void * cipher_ctx
Cipher-specific context.
Definition: cipher.h:245
ISO/IEC 7816-4 padding.
Definition: cipher.h:148
mbedtls_operation_t operation
Operation that the context's key has been initialised for.
Definition: cipher.h:224
mbedtls_cipher_id_t
Definition: cipher.h:71
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:172
size_t unprocessed_len
Number of bytes that still need processing.
Definition: cipher.h:236
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:196
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:147
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:221
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:366
Key length, in bits (including parity), for DES keys.
Definition: cipher.h:164
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:242
#define MBEDTLS_MAX_BLOCK_LENGTH
Maximum block size of any cipher, in bytes.
Definition: cipher.h:174
unsigned int key_bitlen
Cipher key length, in bits (default length for variable sized ciphers) (Includes parity bits for ciph...
Definition: cipher.h:193
mbedtls_cipher_type_t type
Full cipher identifier (e.g.
Definition: cipher.h:186
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:209