PolarSSL v1.3.1
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 {
116 } cipher_type_t;
117 
118 typedef enum {
127 } cipher_mode_t;
128 
129 typedef enum {
136 
137 typedef enum {
141 } operation_t;
142 
143 enum {
154 };
155 
159 typedef struct {
160 
163 
165  int (*ecb_func)( void *ctx, operation_t mode,
166  const unsigned char *input, unsigned char *output );
167 
169  int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv,
170  const unsigned char *input, unsigned char *output );
171 
173  int (*cfb_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off,
174  unsigned char *iv, const unsigned char *input, unsigned char *output );
175 
177  int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter,
178  unsigned char *stream_block, const unsigned char *input, unsigned char *output );
179 
181  int (*stream_func)( void *ctx, size_t length,
182  const unsigned char *input, unsigned char *output );
183 
185  int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length);
186 
188  int (*setkey_dec_func)( void *ctx, const unsigned char *key, unsigned int key_length);
189 
191  void * (*ctx_alloc_func)( void );
192 
194  void (*ctx_free_func)( void *ctx );
195 
196 } cipher_base_t;
197 
201 typedef struct {
204 
207 
210  unsigned int key_length;
211 
213  const char * name;
214 
217  unsigned int iv_size;
218 
221 
223  unsigned int block_size;
224 
227 
228 } cipher_info_t;
229 
233 typedef struct {
236 
239 
242 
244  void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
245  int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
246 
248  unsigned char unprocessed_data[POLARSSL_MAX_IV_LENGTH];
249 
252 
254  unsigned char iv[POLARSSL_MAX_IV_LENGTH];
255 
257  size_t iv_size;
258 
260  void *cipher_ctx;
262 
269 const int *cipher_list( void );
270 
280 const cipher_info_t *cipher_info_from_string( const char *cipher_name );
281 
291 const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type );
292 
305 const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
306  int key_length,
307  const cipher_mode_t mode );
308 
321 int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info );
322 
333 
342 static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx )
343 {
344  if( NULL == ctx || NULL == ctx->cipher_info )
345  return 0;
346 
347  return ctx->cipher_info->block_size;
348 }
349 
360 {
361  if( NULL == ctx || NULL == ctx->cipher_info )
362  return POLARSSL_MODE_NONE;
363 
364  return ctx->cipher_info->mode;
365 }
366 
376 static inline int cipher_get_iv_size( const cipher_context_t *ctx )
377 {
378  if( NULL == ctx || NULL == ctx->cipher_info )
379  return 0;
380 
381  if( ctx->iv_size != 0 )
382  return (int) ctx->iv_size;
383 
384  return ctx->cipher_info->iv_size;
385 }
386 
395 static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx )
396 {
397  if( NULL == ctx || NULL == ctx->cipher_info )
398  return POLARSSL_CIPHER_NONE;
399 
400  return ctx->cipher_info->type;
401 }
402 
410 static inline const char *cipher_get_name( const cipher_context_t *ctx )
411 {
412  if( NULL == ctx || NULL == ctx->cipher_info )
413  return 0;
414 
415  return ctx->cipher_info->name;
416 }
417 
427 static inline int cipher_get_key_size ( const cipher_context_t *ctx )
428 {
429  if( NULL == ctx || NULL == ctx->cipher_info )
431 
432  return ctx->cipher_info->key_length;
433 }
434 
445 {
446  if( NULL == ctx || NULL == ctx->cipher_info )
448 
449  return ctx->operation;
450 }
451 
467 int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length,
468  const operation_t operation );
469 
470 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
471 
484 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
485 
500  const unsigned char *iv, size_t iv_len );
501 
510 int cipher_reset( cipher_context_t *ctx );
511 
512 #if defined(POLARSSL_CIPHER_MODE_AEAD)
513 
529  const unsigned char *ad, size_t ad_len );
530 #endif /* POLARSSL_CIPHER_MODE_AEAD */
531 
561 int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
562  unsigned char *output, size_t *olen );
563 
582  unsigned char *output, size_t *olen );
583 
584 #if defined(POLARSSL_CIPHER_MODE_AEAD)
585 
597  unsigned char *tag, size_t tag_len );
598 
612  const unsigned char *tag, size_t tag_len );
613 #endif /* POLARSSL_CIPHER_MODE_AEAD */
614 
620 int cipher_self_test( int verbose );
621 
622 #ifdef __cplusplus
623 }
624 #endif
625 
626 #endif /* POLARSSL_CIPHER_H */
int key_length
Key length to use.
Definition: cipher.h:238
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:376
Generic cipher context.
Definition: cipher.h:233
Key length, in bits (including parity), for DES keys.
Definition: cipher.h:147
cipher_type_t type
Full cipher identifier (e.g.
Definition: cipher.h:203
static cipher_mode_t cipher_get_cipher_mode(const cipher_context_t *ctx)
Returns the mode of operation for the cipher.
Definition: cipher.h:359
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:201
zero padding (not reversible!)
Definition: cipher.h:133
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:342
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:410
static cipher_type_t cipher_get_type(const cipher_context_t *ctx)
Returns the type of the given cipher.
Definition: cipher.h:395
ISO/IEC 7816-4 padding.
Definition: cipher.h:131
const cipher_info_t * cipher_info
Information about the associated cipher.
Definition: cipher.h:235
operation_t operation
Operation that the context&#39;s key has been initialised for.
Definition: cipher.h:241
cipher_mode_t
Definition: cipher.h:118
cipher_type_t
Definition: cipher.h:75
size_t unprocessed_len
Number of bytes that still need processing.
Definition: cipher.h:251
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:210
operation_t
Definition: cipher.h:137
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:151
const char * name
Name of the cipher.
Definition: cipher.h:213
Maximum length of any IV, in bytes.
Definition: cipher.h:153
cipher_id_t
Definition: cipher.h:64
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:162
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:206
cipher_padding_t
Definition: cipher.h:129
static operation_t cipher_get_operation(const cipher_context_t *ctx)
Returns the operation of the given cipher.
Definition: cipher.h:444
PKCS7 padding (default)
Definition: cipher.h:130
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:134
Base cipher information.
Definition: cipher.h:159
const cipher_base_t * base
Base cipher information and functions.
Definition: cipher.h:226
const int * cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
Undefined key length.
Definition: cipher.h:145
ANSI X.923 padding.
Definition: cipher.h:132
unsigned int block_size
block size, in bytes
Definition: cipher.h:223
void * cipher_ctx
Cipher-specific context.
Definition: cipher.h:260
static int cipher_get_key_size(const cipher_context_t *ctx)
Returns the key length of the cipher.
Definition: cipher.h:427
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:257
int accepts_variable_iv_size
Flag for ciphers that accept many sizes of IV/NONCE.
Definition: cipher.h:220
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:217
Key length, in bits (including parity), for DES in two key EDE.
Definition: cipher.h:149
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...