PolarSSL v1.1.4
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 {
55 } cipher_id_t;
56 
57 typedef enum {
81 
82 typedef enum {
89 
90 typedef enum {
94 } operation_t;
95 
96 enum {
107 };
108 
112 typedef struct {
113 
116 
118  int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv,
119  const unsigned char *input, unsigned char *output );
120 
122  int (*cfb128_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off,
123  unsigned char *iv, const unsigned char *input, unsigned char *output );
124 
126  int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter,
127  unsigned char *stream_block, const unsigned char *input, unsigned char *output );
128 
130  int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length);
131 
133  int (*setkey_dec_func)( void *ctx, const unsigned char *key, unsigned int key_length);
134 
136  void * (*ctx_alloc_func)( void );
137 
139  void (*ctx_free_func)( void *ctx );
140 
141 } cipher_base_t;
142 
146 typedef struct {
149 
152 
155  unsigned int key_length;
156 
158  const char * name;
159 
161  unsigned int iv_size;
162 
164  unsigned int block_size;
165 
168 
169 } cipher_info_t;
170 
174 typedef struct {
177 
180 
183 
185  unsigned char unprocessed_data[POLARSSL_MAX_IV_LENGTH];
186 
189 
191  unsigned char iv[POLARSSL_MAX_IV_LENGTH];
192 
194  void *cipher_ctx;
196 
197 #ifdef __cplusplus
198 extern "C" {
199 #endif
200 
207 const int *cipher_list( void );
208 
218 const cipher_info_t *cipher_info_from_string( const char *cipher_name );
219 
229 const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type );
230 
243 int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info );
244 
255 
264 static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx )
265 {
266  if( NULL == ctx || NULL == ctx->cipher_info )
267  return 0;
268 
269  return ctx->cipher_info->block_size;
270 }
271 
282 {
283  if( NULL == ctx || NULL == ctx->cipher_info )
284  return POLARSSL_MODE_NONE;
285 
286  return ctx->cipher_info->mode;
287 }
288 
297 static inline int cipher_get_iv_size( const cipher_context_t *ctx )
298 {
299  if( NULL == ctx || NULL == ctx->cipher_info )
300  return 0;
301 
302  return ctx->cipher_info->iv_size;
303 }
304 
313 static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx )
314 {
315  if( NULL == ctx || NULL == ctx->cipher_info )
316  return 0;
317 
318  return ctx->cipher_info->type;
319 }
320 
328 static inline const char *cipher_get_name( const cipher_context_t *ctx )
329 {
330  if( NULL == ctx || NULL == ctx->cipher_info )
331  return 0;
332 
333  return ctx->cipher_info->name;
334 }
335 
345 static inline int cipher_get_key_size ( const cipher_context_t *ctx )
346 {
347  if( NULL == ctx )
349 
350  return ctx->key_length;
351 }
352 
363 {
364  if( NULL == ctx || NULL == ctx->cipher_info )
366 
367  return ctx->operation;
368 }
369 
385 int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length,
386  const operation_t operation );
387 
397 int cipher_reset( cipher_context_t *ctx, const unsigned char *iv );
398 
422 int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
423  unsigned char *output, size_t *olen );
424 
442 int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen);
443 
444 
450 int cipher_self_test( int verbose );
451 
452 #ifdef __cplusplus
453 }
454 #endif
455 
456 #endif /* POLARSSL_MD_H */