PolarSSL v1.2.8
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 */