PolarSSL v1.3.3
cipher.h
Go to the documentation of this file.
1 
30 #ifndef POLARSSL_CIPHER_H
31 #define POLARSSL_CIPHER_H
32 
33 #include "config.h"
34 
35 #if defined(POLARSSL_GCM_C)
36 #define POLARSSL_CIPHER_MODE_AEAD
37 #endif
38 
39 #if defined(POLARSSL_CIPHER_MODE_CBC)
40 #define POLARSSL_CIPHER_MODE_WITH_PADDING
41 #endif
42 
43 #include <string.h>
44 
45 #if defined(_MSC_VER) && !defined(inline)
46 #define inline _inline
47 #else
48 #if defined(__ARMCC_VERSION) && !defined(inline)
49 #define inline __inline
50 #endif /* __ARMCC_VERSION */
51 #endif /*_MSC_VER */
52 
53 #define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
54 #define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100
55 #define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180
56 #define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200
57 #define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
58 #define POLARSSL_ERR_CIPHER_AUTH_FAILED -0x6300
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
64 typedef enum {
73 } cipher_id_t;
74 
75 typedef enum {
119 } cipher_type_t;
120 
121 typedef enum {
130 } cipher_mode_t;
131 
132 typedef enum {
139 
140 typedef enum {
144 } operation_t;
145 
146 enum {
155 };
156 
158 #define POLARSSL_MAX_IV_LENGTH 16
159 
160 #define POLARSSL_MAX_BLOCK_LENGTH 16
161 
165 typedef struct {
166 
169 
171  int (*ecb_func)( void *ctx, operation_t mode,
172  const unsigned char *input, unsigned char *output );
173 
175  int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv,
176  const unsigned char *input, unsigned char *output );
177 
179  int (*cfb_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off,
180  unsigned char *iv, const unsigned char *input, unsigned char *output );
181 
183  int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter,
184  unsigned char *stream_block, const unsigned char *input, unsigned char *output );
185 
187  int (*stream_func)( void *ctx, size_t length,
188  const unsigned char *input, unsigned char *output );
189 
191  int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length);
192 
194  int (*setkey_dec_func)( void *ctx, const unsigned char *key, unsigned int key_length);
195 
197  void * (*ctx_alloc_func)( void );
198 
200  void (*ctx_free_func)( void *ctx );
201 
202 } cipher_base_t;
203 
207 typedef struct {
210 
213 
216  unsigned int key_length;
217 
219  const char * name;
220 
223  unsigned int iv_size;
224 
227 
229  unsigned int block_size;
230 
233 
234 } cipher_info_t;
235 
239 typedef struct {
242 
245 
248 
250  void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
251  int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
252 
254  unsigned char unprocessed_data[POLARSSL_MAX_BLOCK_LENGTH];
255 
258 
260  unsigned char iv[POLARSSL_MAX_IV_LENGTH];
261 
263  size_t iv_size;
264 
266  void *cipher_ctx;
268 
275 const int *cipher_list( void );
276 
286 const cipher_info_t *cipher_info_from_string( const char *cipher_name );
287 
297 const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type );
298 
311 const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
312  int key_length,
313  const cipher_mode_t mode );
314 
327 int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info );
328 
339 
348 static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx )
349 {
350  if( NULL == ctx || NULL == ctx->cipher_info )
351  return 0;
352 
353  return ctx->cipher_info->block_size;
354 }
355 
366 {
367  if( NULL == ctx || NULL == ctx->cipher_info )
368  return POLARSSL_MODE_NONE;
369 
370  return ctx->cipher_info->mode;
371 }
372 
382 static inline int cipher_get_iv_size( const cipher_context_t *ctx )
383 {
384  if( NULL == ctx || NULL == ctx->cipher_info )
385  return 0;
386 
387  if( ctx->iv_size != 0 )
388  return (int) ctx->iv_size;
389 
390  return ctx->cipher_info->iv_size;
391 }
392 
401 static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx )
402 {
403  if( NULL == ctx || NULL == ctx->cipher_info )
404  return POLARSSL_CIPHER_NONE;
405 
406  return ctx->cipher_info->type;
407 }
408 
416 static inline const char *cipher_get_name( const cipher_context_t *ctx )
417 {
418  if( NULL == ctx || NULL == ctx->cipher_info )
419  return 0;
420 
421  return ctx->cipher_info->name;
422 }
423 
433 static inline int cipher_get_key_size ( const cipher_context_t *ctx )
434 {
435  if( NULL == ctx || NULL == ctx->cipher_info )
437 
438  return ctx->cipher_info->key_length;
439 }
440 
451 {
452  if( NULL == ctx || NULL == ctx->cipher_info )
454 
455  return ctx->operation;
456 }
457 
473 int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length,
474  const operation_t operation );
475 
476 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
477 
490 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
491 
506  const unsigned char *iv, size_t iv_len );
507 
516 int cipher_reset( cipher_context_t *ctx );
517 
518 #if defined(POLARSSL_CIPHER_MODE_AEAD)
519 
535  const unsigned char *ad, size_t ad_len );
536 #endif /* POLARSSL_CIPHER_MODE_AEAD */
537 
567 int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
568  unsigned char *output, size_t *olen );
569 
588  unsigned char *output, size_t *olen );
589 
590 #if defined(POLARSSL_CIPHER_MODE_AEAD)
591 
603  unsigned char *tag, size_t tag_len );
604 
618  const unsigned char *tag, size_t tag_len );
619 #endif /* POLARSSL_CIPHER_MODE_AEAD */
620 
626 int cipher_self_test( int verbose );
627 
628 #ifdef __cplusplus
629 }
630 #endif
631 
632 #endif /* POLARSSL_CIPHER_H */
int key_length
Key length to use.
Definition: cipher.h:244
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:382
Generic cipher context.
Definition: cipher.h:239
Key length, in bits (including parity), for DES keys.
Definition: cipher.h:150
cipher_type_t type
Full cipher identifier (e.g.
Definition: cipher.h:209
static cipher_mode_t cipher_get_cipher_mode(const cipher_context_t *ctx)
Returns the mode of operation for the cipher.
Definition: cipher.h:365
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:207
zero padding (not reversible!)
Definition: cipher.h:136
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:348
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:416
static cipher_type_t cipher_get_type(const cipher_context_t *ctx)
Returns the type of the given cipher.
Definition: cipher.h:401
ISO/IEC 7816-4 padding.
Definition: cipher.h:134
const cipher_info_t * cipher_info
Information about the associated cipher.
Definition: cipher.h:241
operation_t operation
Operation that the context&#39;s key has been initialised for.
Definition: cipher.h:247
cipher_mode_t
Definition: cipher.h:121
cipher_type_t
Definition: cipher.h:75
#define POLARSSL_MAX_BLOCK_LENGTH
Maximum block size of any cipher, in bytes.
Definition: cipher.h:160
size_t unprocessed_len
Number of bytes that still need processing.
Definition: cipher.h:257
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:216
operation_t
Definition: cipher.h:140
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:154
const char * name
Name of the cipher.
Definition: cipher.h:219
cipher_id_t
Definition: cipher.h:64
#define POLARSSL_MAX_IV_LENGTH
Maximum length of any IV, in bytes.
Definition: cipher.h:158
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:168
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:212
cipher_padding_t
Definition: cipher.h:132
static operation_t cipher_get_operation(const cipher_context_t *ctx)
Returns the operation of the given cipher.
Definition: cipher.h:450
PKCS7 padding (default)
Definition: cipher.h:133
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:137
Base cipher information.
Definition: cipher.h:165
const cipher_base_t * base
Base cipher information and functions.
Definition: cipher.h:232
const int * cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
Undefined key length.
Definition: cipher.h:148
ANSI X.923 padding.
Definition: cipher.h:135
unsigned int block_size
block size, in bytes
Definition: cipher.h:229
void * cipher_ctx
Cipher-specific context.
Definition: cipher.h:266
static int cipher_get_key_size(const cipher_context_t *ctx)
Returns the key length of the cipher.
Definition: cipher.h:433
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:263
int accepts_variable_iv_size
Flag for ciphers that accept many sizes of IV/NONCE.
Definition: cipher.h:226
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:223
Key length, in bits (including parity), for DES in two key EDE.
Definition: cipher.h:152
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...