PolarSSL v1.3.7
cipher.h
Go to the documentation of this file.
1 
30 #ifndef POLARSSL_CIPHER_H
31 #define POLARSSL_CIPHER_H
32 
33 #if !defined(POLARSSL_CONFIG_FILE)
34 #include "config.h"
35 #else
36 #include POLARSSL_CONFIG_FILE
37 #endif
38 
39 #if defined(POLARSSL_GCM_C)
40 #define POLARSSL_CIPHER_MODE_AEAD
41 #endif
42 
43 #if defined(POLARSSL_CIPHER_MODE_CBC)
44 #define POLARSSL_CIPHER_MODE_WITH_PADDING
45 #endif
46 
47 #include <string.h>
48 
49 #if defined(_MSC_VER) && !defined(inline)
50 #define inline _inline
51 #else
52 #if defined(__ARMCC_VERSION) && !defined(inline)
53 #define inline __inline
54 #endif /* __ARMCC_VERSION */
55 #endif /*_MSC_VER */
56 
57 #define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
58 #define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100
59 #define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180
60 #define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200
61 #define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
62 #define POLARSSL_ERR_CIPHER_AUTH_FAILED -0x6300
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 typedef enum {
77 } cipher_id_t;
78 
79 typedef enum {
123 } cipher_type_t;
124 
125 typedef enum {
134 } cipher_mode_t;
135 
136 typedef enum {
143 
144 typedef enum {
148 } operation_t;
149 
150 enum {
159 };
160 
162 #define POLARSSL_MAX_IV_LENGTH 16
163 
164 #define POLARSSL_MAX_BLOCK_LENGTH 16
165 
169 typedef struct {
170 
173 
175  int (*ecb_func)( void *ctx, operation_t mode,
176  const unsigned char *input, unsigned char *output );
177 
179  int (*cbc_func)( void *ctx, operation_t mode, size_t length,
180  unsigned char *iv, const unsigned char *input,
181  unsigned char *output );
182 
184  int (*cfb_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off,
185  unsigned char *iv, const unsigned char *input,
186  unsigned char *output );
187 
189  int (*ctr_func)( void *ctx, size_t length, size_t *nc_off,
190  unsigned char *nonce_counter, unsigned char *stream_block,
191  const unsigned char *input, unsigned char *output );
192 
194  int (*stream_func)( void *ctx, size_t length,
195  const unsigned char *input, unsigned char *output );
196 
198  int (*setkey_enc_func)( void *ctx, const unsigned char *key,
199  unsigned int key_length );
200 
202  int (*setkey_dec_func)( void *ctx, const unsigned char *key,
203  unsigned int key_length);
204 
206  void * (*ctx_alloc_func)( void );
207 
209  void (*ctx_free_func)( void *ctx );
210 
211 } cipher_base_t;
212 
216 typedef struct {
219 
222 
225  unsigned int key_length;
226 
228  const char * name;
229 
232  unsigned int iv_size;
233 
236 
238  unsigned int block_size;
239 
242 
243 } cipher_info_t;
244 
248 typedef struct {
251 
254 
257 
259  void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
260  int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
261 
263  unsigned char unprocessed_data[POLARSSL_MAX_BLOCK_LENGTH];
264 
267 
269  unsigned char iv[POLARSSL_MAX_IV_LENGTH];
270 
272  size_t iv_size;
273 
275  void *cipher_ctx;
277 
284 const int *cipher_list( void );
285 
295 const cipher_info_t *cipher_info_from_string( const char *cipher_name );
296 
306 const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type );
307 
320 const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
321  int key_length,
322  const cipher_mode_t mode );
323 
336 int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info );
337 
348 
357 static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx )
358 {
359  if( NULL == ctx || NULL == ctx->cipher_info )
360  return 0;
361 
362  return ctx->cipher_info->block_size;
363 }
364 
375 {
376  if( NULL == ctx || NULL == ctx->cipher_info )
377  return POLARSSL_MODE_NONE;
378 
379  return ctx->cipher_info->mode;
380 }
381 
391 static inline int cipher_get_iv_size( const cipher_context_t *ctx )
392 {
393  if( NULL == ctx || NULL == ctx->cipher_info )
394  return 0;
395 
396  if( ctx->iv_size != 0 )
397  return (int) ctx->iv_size;
398 
399  return ctx->cipher_info->iv_size;
400 }
401 
410 static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx )
411 {
412  if( NULL == ctx || NULL == ctx->cipher_info )
413  return POLARSSL_CIPHER_NONE;
414 
415  return ctx->cipher_info->type;
416 }
417 
425 static inline const char *cipher_get_name( const cipher_context_t *ctx )
426 {
427  if( NULL == ctx || NULL == ctx->cipher_info )
428  return 0;
429 
430  return ctx->cipher_info->name;
431 }
432 
442 static inline int cipher_get_key_size ( const cipher_context_t *ctx )
443 {
444  if( NULL == ctx || NULL == ctx->cipher_info )
446 
447  return ctx->cipher_info->key_length;
448 }
449 
460 {
461  if( NULL == ctx || NULL == ctx->cipher_info )
463 
464  return ctx->operation;
465 }
466 
482 int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
483  int key_length, const operation_t operation );
484 
485 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
486 
499 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
500 
515  const unsigned char *iv, size_t iv_len );
516 
525 int cipher_reset( cipher_context_t *ctx );
526 
527 #if defined(POLARSSL_CIPHER_MODE_AEAD)
528 
544  const unsigned char *ad, size_t ad_len );
545 #endif /* POLARSSL_CIPHER_MODE_AEAD */
546 
576 int cipher_update( cipher_context_t *ctx, const unsigned char *input,
577  size_t ilen, unsigned char *output, size_t *olen );
578 
597  unsigned char *output, size_t *olen );
598 
599 #if defined(POLARSSL_CIPHER_MODE_AEAD)
600 
612  unsigned char *tag, size_t tag_len );
613 
627  const unsigned char *tag, size_t tag_len );
628 #endif /* POLARSSL_CIPHER_MODE_AEAD */
629 
635 int cipher_self_test( int verbose );
636 
637 #ifdef __cplusplus
638 }
639 #endif
640 
641 #endif /* POLARSSL_CIPHER_H */
int key_length
Key length to use.
Definition: cipher.h:253
int cipher_finish(cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
static int cipher_get_iv_size(const cipher_context_t *ctx)
Returns the size of the cipher&#39;s IV/NONCE in bytes.
Definition: cipher.h:391
Generic cipher context.
Definition: cipher.h:248
Key length, in bits (including parity), for DES keys.
Definition: cipher.h:154
cipher_type_t type
Full cipher identifier (e.g.
Definition: cipher.h:218
static cipher_mode_t cipher_get_cipher_mode(const cipher_context_t *ctx)
Returns the mode of operation for the cipher.
Definition: cipher.h:374
int cipher_write_tag(cipher_context_t *ctx, unsigned char *tag, size_t tag_len)
Write tag for AEAD ciphers.
Cipher information.
Definition: cipher.h:216
zero padding (not reversible!)
Definition: cipher.h:140
const cipher_info_t * cipher_info_from_type(const cipher_type_t cipher_type)
Returns the cipher information structure associated with the given cipher type.
static unsigned int cipher_get_block_size(const cipher_context_t *ctx)
Returns the block size of the given cipher.
Definition: cipher.h:357
const cipher_info_t * cipher_info_from_string(const char *cipher_name)
Returns the cipher information structure associated with the given cipher name.
Configuration options (set of defines)
static const char * cipher_get_name(const cipher_context_t *ctx)
Returns the name of the given cipher, as a string.
Definition: cipher.h:425
static cipher_type_t cipher_get_type(const cipher_context_t *ctx)
Returns the type of the given cipher.
Definition: cipher.h:410
ISO/IEC 7816-4 padding.
Definition: cipher.h:138
const cipher_info_t * cipher_info
Information about the associated cipher.
Definition: cipher.h:250
operation_t operation
Operation that the context&#39;s key has been initialised for.
Definition: cipher.h:256
cipher_mode_t
Definition: cipher.h:125
cipher_type_t
Definition: cipher.h:79
#define POLARSSL_MAX_BLOCK_LENGTH
Maximum block size of any cipher, in bytes.
Definition: cipher.h:164
size_t unprocessed_len
Number of bytes that still need processing.
Definition: cipher.h:266
int cipher_free_ctx(cipher_context_t *ctx)
Free the cipher-specific context of ctx.
int cipher_update_ad(cipher_context_t *ctx, const unsigned char *ad, size_t ad_len)
Add additional data (for AEAD ciphers).
unsigned int key_length
Cipher key length, in bits (default length for variable sized ciphers) (Includes parity bits for ciph...
Definition: cipher.h:225
operation_t
Definition: cipher.h:144
int cipher_set_iv(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
Set the initialization vector (IV) or nonce.
int cipher_update(cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic cipher update function.
Key length, in bits (including parity), for DES in three-key EDE.
Definition: cipher.h:158
const char * name
Name of the cipher.
Definition: cipher.h:228
cipher_id_t
Definition: cipher.h:68
#define POLARSSL_MAX_IV_LENGTH
Maximum length of any IV, in bytes.
Definition: cipher.h:162
int cipher_reset(cipher_context_t *ctx)
Finish preparation of the given context.
cipher_id_t cipher
Base Cipher type (e.g.
Definition: cipher.h:172
int cipher_set_padding_mode(cipher_context_t *ctx, cipher_padding_t mode)
Set padding mode, for cipher modes that use padding.
cipher_mode_t mode
Cipher mode (e.g.
Definition: cipher.h:221
cipher_padding_t
Definition: cipher.h:136
static operation_t cipher_get_operation(const cipher_context_t *ctx)
Returns the operation of the given cipher.
Definition: cipher.h:459
PKCS7 padding (default)
Definition: cipher.h:137
int cipher_init_ctx(cipher_context_t *ctx, const cipher_info_t *cipher_info)
Initialises and fills the cipher context structure with the appropriate values.
int cipher_setkey(cipher_context_t *ctx, const unsigned char *key, int key_length, const operation_t operation)
Set the key to use with the given context.
never pad (full blocks only)
Definition: cipher.h:141
Base cipher information.
Definition: cipher.h:169
const cipher_base_t * base
Base cipher information and functions.
Definition: cipher.h:241
const int * cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
Undefined key length.
Definition: cipher.h:152
ANSI X.923 padding.
Definition: cipher.h:139
unsigned int block_size
block size, in bytes
Definition: cipher.h:238
void * cipher_ctx
Cipher-specific context.
Definition: cipher.h:275
static int cipher_get_key_size(const cipher_context_t *ctx)
Returns the key length of the cipher.
Definition: cipher.h:442
int cipher_self_test(int verbose)
Checkup routine.
size_t iv_size
IV size in bytes (for ciphers with variable-length IVs)
Definition: cipher.h:272
int accepts_variable_iv_size
Flag for ciphers that accept many sizes of IV/NONCE.
Definition: cipher.h:235
int cipher_check_tag(cipher_context_t *ctx, const unsigned char *tag, size_t tag_len)
Check tag for AEAD ciphers.
unsigned int iv_size
IV/NONCE size, in bytes.
Definition: cipher.h:232
Key length, in bits (including parity), for DES in two key EDE.
Definition: cipher.h:156
const cipher_info_t * cipher_info_from_values(const cipher_id_t cipher_id, int key_length, const cipher_mode_t mode)
Returns the cipher information structure associated with the given cipher id, key size and mode...