PolarSSL v1.2.10
cipher.h
Go to the documentation of this file.
1 
30 #ifndef POLARSSL_CIPHER_H
31 #define POLARSSL_CIPHER_H
32 
33 #include <string.h>
34 
35 #if defined(_MSC_VER) && !defined(inline)
36 #define inline _inline
37 #else
38 #if defined(__ARMCC_VERSION) && !defined(inline)
39 #define inline __inline
40 #endif /* __ARMCC_VERSION */
41 #endif /*_MSC_VER */
42 
43 #define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
44 #define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100
45 #define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180
46 #define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200
47 #define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
49 typedef enum {
57 } cipher_id_t;
58 
59 typedef enum {
87 
88 typedef enum {
96 
97 typedef enum {
101 } operation_t;
102 
103 enum {
114 };
115 
119 typedef struct {
120 
123 
125  int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv,
126  const unsigned char *input, unsigned char *output );
127 
129  int (*cfb_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off,
130  unsigned char *iv, const unsigned char *input, unsigned char *output );
131 
133  int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter,
134  unsigned char *stream_block, const unsigned char *input, unsigned char *output );
135 
137  int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length);
138 
140  int (*setkey_dec_func)( void *ctx, const unsigned char *key, unsigned int key_length);
141 
143  void * (*ctx_alloc_func)( void );
144 
146  void (*ctx_free_func)( void *ctx );
147 
148 } cipher_base_t;
149 
153 typedef struct {
156 
159 
162  unsigned int key_length;
163 
165  const char * name;
166 
168  unsigned int iv_size;
169 
171  unsigned int block_size;
172 
175 
176 } cipher_info_t;
177 
181 typedef struct {
184 
187 
190 
192  unsigned char unprocessed_data[POLARSSL_MAX_IV_LENGTH];
193 
196 
198  unsigned char iv[POLARSSL_MAX_IV_LENGTH];
199 
201  void *cipher_ctx;
203 
204 #ifdef __cplusplus
205 extern "C" {
206 #endif
207 
214 const int *cipher_list( void );
215 
225 const cipher_info_t *cipher_info_from_string( const char *cipher_name );
226 
236 const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type );
237 
250 int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info );
251 
262 
271 static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx )
272 {
273  if( NULL == ctx || NULL == ctx->cipher_info )
274  return 0;
275 
276  return ctx->cipher_info->block_size;
277 }
278 
289 {
290  if( NULL == ctx || NULL == ctx->cipher_info )
291  return POLARSSL_MODE_NONE;
292 
293  return ctx->cipher_info->mode;
294 }
295 
304 static inline int cipher_get_iv_size( const cipher_context_t *ctx )
305 {
306  if( NULL == ctx || NULL == ctx->cipher_info )
307  return 0;
308 
309  return ctx->cipher_info->iv_size;
310 }
311 
320 static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx )
321 {
322  if( NULL == ctx || NULL == ctx->cipher_info )
323  return POLARSSL_CIPHER_NONE;
324 
325  return ctx->cipher_info->type;
326 }
327 
335 static inline const char *cipher_get_name( const cipher_context_t *ctx )
336 {
337  if( NULL == ctx || NULL == ctx->cipher_info )
338  return 0;
339 
340  return ctx->cipher_info->name;
341 }
342 
352 static inline int cipher_get_key_size ( const cipher_context_t *ctx )
353 {
354  if( NULL == ctx )
356 
357  return ctx->key_length;
358 }
359 
370 {
371  if( NULL == ctx || NULL == ctx->cipher_info )
373 
374  return ctx->operation;
375 }
376 
392 int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length,
393  const operation_t operation );
394 
404 int cipher_reset( cipher_context_t *ctx, const unsigned char *iv );
405 
429 int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
430  unsigned char *output, size_t *olen );
431 
449 int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen);
450 
451 
457 int cipher_self_test( int verbose );
458 
459 #ifdef __cplusplus
460 }
461 #endif
462 
463 #endif /* POLARSSL_MD_H */
int key_length
Key length to use.
Definition: cipher.h:186
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.
Definition: cipher.h:304
Generic cipher context.
Definition: cipher.h:181
Key length, in bits (including parity), for DES keys.
Definition: cipher.h:107
cipher_type_t type
Full cipher identifier (e.g.
Definition: cipher.h:155
static cipher_mode_t cipher_get_cipher_mode(const cipher_context_t *ctx)
Returns the mode of operation for the cipher.
Definition: cipher.h:288
Cipher information.
Definition: cipher.h:153
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:271
const cipher_info_t * cipher_info_from_string(const char *cipher_name)
Returns the cipher information structure associated with the given cipher name.
static const char * cipher_get_name(const cipher_context_t *ctx)
Returns the name of the given cipher, as a string.
Definition: cipher.h:335
static cipher_type_t cipher_get_type(const cipher_context_t *ctx)
Returns the type of the given cipher.
Definition: cipher.h:320
const cipher_info_t * cipher_info
Information about the associated cipher.
Definition: cipher.h:183
operation_t operation
Operation that the context&#39;s key has been initialised for.
Definition: cipher.h:189
cipher_mode_t
Definition: cipher.h:88
cipher_type_t
Definition: cipher.h:59
size_t unprocessed_len
Number of bytes that still need processing.
Definition: cipher.h:195
int cipher_free_ctx(cipher_context_t *ctx)
Free the cipher-specific context of ctx.
unsigned int key_length
Cipher key length, in bits (default length for variable sized ciphers) (Includes parity bits for ciph...
Definition: cipher.h:162
operation_t
Definition: cipher.h:97
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:111
const char * name
Name of the cipher.
Definition: cipher.h:165
Maximum length of any IV, in bytes.
Definition: cipher.h:113
cipher_id_t
Definition: cipher.h:49
int cipher_reset(cipher_context_t *ctx, const unsigned char *iv)
Reset the given context, setting the IV to iv.
cipher_id_t cipher
Base Cipher type (e.g.
Definition: cipher.h:122
cipher_mode_t mode
Cipher mode (e.g.
Definition: cipher.h:158
static operation_t cipher_get_operation(const cipher_context_t *ctx)
Returns the operation of the given cipher.
Definition: cipher.h:369
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.
Base cipher information.
Definition: cipher.h:119
const cipher_base_t * base
Base cipher information and functions.
Definition: cipher.h:174
const int * cipher_list(void)
Returns the list of ciphers supported by the generic cipher module.
Undefined key length.
Definition: cipher.h:105
unsigned int block_size
block size, in bytes
Definition: cipher.h:171
void * cipher_ctx
Cipher-specific context.
Definition: cipher.h:201
static int cipher_get_key_size(const cipher_context_t *ctx)
Returns the key length of the cipher.
Definition: cipher.h:352
int cipher_self_test(int verbose)
Checkup routine.
unsigned int iv_size
IV size, in bytes.
Definition: cipher.h:168
Key length, in bits (including parity), for DES in two key EDE.
Definition: cipher.h:109