RetroArch
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 #include <retro_inline.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 #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
52 #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100
53 #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180
54 #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200
55 #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
56 #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300
57 #define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380
59 #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01
60 #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65 
66 typedef enum {
76 
77 typedef enum {
128 
129 typedef enum {
134  MBEDTLS_MODE_OFB, /* Unused! */
140 
141 typedef enum {
148 
149 typedef enum {
154 
155 enum {
164 };
165 
167 #define MBEDTLS_MAX_IV_LENGTH 16
168 
169 #define MBEDTLS_MAX_BLOCK_LENGTH 16
170 
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;
246 
247 #if defined(MBEDTLS_CMAC_C)
248 
249  mbedtls_cmac_context_t *cmac_ctx;
250 #endif
252 
259 const int *mbedtls_cipher_list( void );
260 
270 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
271 
282 
296  int key_bitlen,
297  const mbedtls_cipher_mode_t mode );
298 
303 
310 
328 
338 {
339  if( NULL == ctx || NULL == ctx->cipher_info )
340  return 0;
341 
342  return ctx->cipher_info->block_size;
343 }
344 
355 {
356  if( NULL == ctx || NULL == ctx->cipher_info )
357  return MBEDTLS_MODE_NONE;
358 
359  return ctx->cipher_info->mode;
360 }
361 
372 {
373  if( NULL == ctx || NULL == ctx->cipher_info )
374  return 0;
375 
376  if( ctx->iv_size != 0 )
377  return (int) ctx->iv_size;
378 
379  return (int) ctx->cipher_info->iv_size;
380 }
381 
391 {
392  if( NULL == ctx || NULL == ctx->cipher_info )
393  return MBEDTLS_CIPHER_NONE;
394 
395  return ctx->cipher_info->type;
396 }
397 
406 {
407  if( NULL == ctx || NULL == ctx->cipher_info )
408  return 0;
409 
410  return ctx->cipher_info->name;
411 }
412 
423 {
424  if( NULL == ctx || NULL == ctx->cipher_info )
426 
427  return (int) ctx->cipher_info->key_bitlen;
428 }
429 
440 {
441  if( NULL == ctx || NULL == ctx->cipher_info )
442  return MBEDTLS_OPERATION_NONE;
443 
444  return ctx->operation;
445 }
446 
462 int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
463  int key_bitlen, const mbedtls_operation_t operation );
464 
465 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
466 
479 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
480 
495  const unsigned char *iv, size_t iv_len );
496 
506 
507 #if defined(MBEDTLS_GCM_C)
508 
520  const unsigned char *ad, size_t ad_len );
521 #endif /* MBEDTLS_GCM_C */
522 
552 int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
553  size_t ilen, unsigned char *output, size_t *olen );
554 
573  unsigned char *output, size_t *olen );
574 
575 #if defined(MBEDTLS_GCM_C)
576 
588  unsigned char *tag, size_t tag_len );
589 
602  const unsigned char *tag, size_t tag_len );
603 #endif /* MBEDTLS_GCM_C */
604 
633  const unsigned char *iv, size_t iv_len,
634  const unsigned char *input, size_t ilen,
635  unsigned char *output, size_t *olen );
636 
637 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
638 
661  const unsigned char *iv, size_t iv_len,
662  const unsigned char *ad, size_t ad_len,
663  const unsigned char *input, size_t ilen,
664  unsigned char *output, size_t *olen,
665  unsigned char *tag, size_t tag_len );
666 
695  const unsigned char *iv, size_t iv_len,
696  const unsigned char *ad, size_t ad_len,
697  const unsigned char *input, size_t ilen,
698  unsigned char *output, size_t *olen,
699  const unsigned char *tag, size_t tag_len );
700 #endif /* MBEDTLS_CIPHER_MODE_AEAD */
701 
702 #ifdef __cplusplus
703 }
704 #endif
705 
706 #endif /* MBEDTLS_CIPHER_H */
#define MBEDTLS_MAX_BLOCK_LENGTH
Definition: cipher.h:169
Definition: cipher.h:161
static INLINE 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:371
Definition: cipher.h:137
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).
Definition: cipher.c:870
Definition: cipher.h:80
Definition: cipher.h:123
static INLINE unsigned int mbedtls_cipher_get_block_size(const mbedtls_cipher_context_t *ctx)
Returns the block size of the given cipher.
Definition: cipher.h:337
Definition: cipher.h:113
Definition: cipher.h:70
Definition: cipher.h:82
Definition: cipher.h:98
Definition: cipher.h:105
mbedtls_cipher_type_t type
Definition: cipher.h:186
Definition: cipher.h:84
unsigned int iv_size
Definition: cipher.h:200
mbedtls_cipher_mode_t mode
Definition: cipher.h:189
GLenum mode
Definition: glext.h:6857
Definition: cipher.h:89
Definition: cipher.h:136
#define INLINE
Definition: retro_inline.h:35
Definition: cipher.h:92
static const unsigned char tag[MAX_TESTS *3][16]
Definition: gcm.c:696
Definition: cipher.h:94
Definition: cipher.h:163
Configuration options (set of defines)
Definition: cipher.h:100
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.
Definition: cipher.c:88
#define MBEDTLS_MAX_IV_LENGTH
Definition: cipher.h:167
mbedtls_cipher_padding_t
Definition: cipher.h:141
static INLINE int mbedtls_cipher_get_key_bitlen(const mbedtls_cipher_context_t *ctx)
Returns the key length of the cipher.
Definition: cipher.h:422
Definition: cipher.h:90
mbedtls_operation_t
Definition: cipher.h:149
Definition: cipher.h:159
Definition: cipher.h:146
Definition: cipher.h:152
static INLINE mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(const mbedtls_cipher_context_t *ctx)
Returns the mode of operation for the cipher. (e.g. MBEDTLS_MODE_CBC)
Definition: cipher.h:354
Definition: cipher.h:117
Definition: cipher.h:72
typedef void(__stdcall *PFN_DESTRUCTION_CALLBACK)(void *pData)
int key_bitlen
Definition: cipher.h:221
Definition: cipher.h:133
int flags
Definition: cipher.h:203
Definition: cipher.h:104
GLenum GLenum GLenum input
Definition: glext.h:9938
Definition: cipher.h:142
Definition: cipher.h:120
Definition: cipher.h:122
Definition: cipher.h:109
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. (Default: PKCS7 padding.)
Definition: cipher.c:703
Definition: cipher.h:116
mbedtls_operation_t operation
Definition: cipher.h:224
Definition: cipher.h:121
Definition: cipher.h:67
Definition: cipher.h:157
Definition: cipher.h:111
void * cipher_ctx
Definition: cipher.h:245
Definition: cipher.h:134
Definition: cipher.h:93
#define NULL
Pointer to 0.
Definition: gctypes.h:65
Definition: cipher.h:68
Definition: cipher.h:101
Definition: cipher.h:112
Definition: cipher.h:125
Definition: cipher.h:87
unsigned int block_size
Definition: cipher.h:206
Definition: cipher.h:97
Definition: cipher.h:99
Definition: cipher.h:106
Definition: cipher.h:130
AVFormatContext * ctx
Definition: record_ffmpeg.c:247
int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function. If data still needs to be flushed from an incomplete block,...
Definition: cipher.c:623
Definition: cipher.h:114
int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx, const unsigned char *tag, size_t tag_len)
Check tag for AEAD ciphers. Currently only supported with GCM. Must be called after mbedtls_cipher_fi...
Definition: cipher.c:766
mbedtls_cipher_mode_t
Definition: cipher.h:129
Definition: cipher_internal.h:43
static const unsigned char iv[]
Definition: ccm.c:364
const char * name
Definition: cipher.h:196
Definition: cipher.h:144
Definition: cipher.h:96
Definition: cipher.h:102
Definition: cipher.h:91
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).
Definition: cipher.c:838
static INLINE 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:405
Definition: cipher.h:124
Definition: cipher.h:150
int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Finish preparation of the given context.
Definition: cipher.c:240
Definition: cipher.h:69
const mbedtls_cipher_info_t * cipher_info
Definition: cipher.h:218
Definition: cipher.h:115
Definition: cipher.h:131
Definition: cipher.h:88
Definition: cipher.h:143
size_t iv_size
Definition: cipher.h:242
Definition: cipher.h:85
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.
Definition: cipher.c:211
Definition: cipher.h:78
Definition: cipher.h:151
Definition: cipher.h:138
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.
Definition: cipher.c:113
Definition: cipher.h:135
std::string output
Definition: Config.FromFile.cpp:44
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.
Definition: cipher.c:99
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.
Definition: cipher.c:178
const int * mbedtls_cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
Definition: cipher.c:67
Definition: cipher.h:108
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. Encrypts/decrypts using the given cipher context. Writes as many bloc...
Definition: cipher.c:267
Definition: cipher.h:103
void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Initialize a cipher_context (as NONE)
Definition: cipher.c:128
Definition: cipher.h:79
Definition: cmac.h:45
Definition: cipher.h:119
Definition: cipher.h:126
Definition: cipher.h:216
Definition: cipher.h:71
Definition: cipher.h:132
static const size_t iv_len[NB_TESTS]
Definition: ccm.c:381
Definition: cipher.h:110
Definition: cipher.h:73
Definition: cipher.h:81
int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx, const unsigned char *ad, size_t ad_len)
Add additional data (for AEAD ciphers). Currently only supported with GCM. Must be called exactly onc...
Definition: cipher.c:251
Definition: cipher.h:145
int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx, unsigned char *tag, size_t tag_len)
Write tag for AEAD ciphers. Currently only supported with GCM. Must be called after mbedtls_cipher_fi...
Definition: cipher.c:751
Definition: cipher.h:95
static INLINE mbedtls_cipher_type_t mbedtls_cipher_get_type(const mbedtls_cipher_context_t *ctx)
Returns the type of the given cipher.
Definition: cipher.h:390
Definition: cipher.h:107
Definition: cipher.h:86
const mbedtls_cipher_base_t * base
Definition: cipher.h:209
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.
Definition: cipher.c:152
Definition: cipher.h:184
static const size_t tag_len[NB_TESTS]
Definition: ccm.c:384
static INLINE mbedtls_operation_t mbedtls_cipher_get_operation(const mbedtls_cipher_context_t *ctx)
Returns the operation of the given cipher.
Definition: cipher.h:439
Definition: cipher.h:83
static const unsigned char ad[]
Definition: ccm.c:369
void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Free and clear the cipher-specific context of ctx. Freeing ctx itself remains the responsibility of t...
Definition: cipher.c:133
Definition: cipher.h:118
unsigned int key_bitlen
Definition: cipher.h:193
mbedtls_cipher_id_t
Definition: cipher.h:66
size_t unprocessed_len
Definition: cipher.h:236
Definition: cipher.h:74
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).
Definition: cipher.c:809
mbedtls_cipher_type_t
Definition: cipher.h:77