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 */