00001
00030 #ifndef POLARSSL_CIPHER_H
00031 #define POLARSSL_CIPHER_H
00032
00033 #include "config.h"
00034
00035 #if defined(POLARSSL_GCM_C)
00036 #define POLARSSL_CIPHER_MODE_AEAD
00037 #endif
00038
00039 #if defined(POLARSSL_CIPHER_MODE_CBC)
00040 #define POLARSSL_CIPHER_MODE_WITH_PADDING
00041 #endif
00042
00043 #include <string.h>
00044
00045 #if defined(_MSC_VER) && !defined(inline)
00046 #define inline _inline
00047 #else
00048 #if defined(__ARMCC_VERSION) && !defined(inline)
00049 #define inline __inline
00050 #endif
00051 #endif
00052
00053 #define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080
00054 #define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100
00055 #define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180
00056 #define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200
00057 #define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280
00058 #define POLARSSL_ERR_CIPHER_AUTH_FAILED -0x6300
00060 #ifdef __cplusplus
00061 extern "C" {
00062 #endif
00063
00064 typedef enum {
00065 POLARSSL_CIPHER_ID_NONE = 0,
00066 POLARSSL_CIPHER_ID_NULL,
00067 POLARSSL_CIPHER_ID_AES,
00068 POLARSSL_CIPHER_ID_DES,
00069 POLARSSL_CIPHER_ID_3DES,
00070 POLARSSL_CIPHER_ID_CAMELLIA,
00071 POLARSSL_CIPHER_ID_BLOWFISH,
00072 POLARSSL_CIPHER_ID_ARC4,
00073 } cipher_id_t;
00074
00075 typedef enum {
00076 POLARSSL_CIPHER_NONE = 0,
00077 POLARSSL_CIPHER_NULL,
00078 POLARSSL_CIPHER_AES_128_ECB,
00079 POLARSSL_CIPHER_AES_192_ECB,
00080 POLARSSL_CIPHER_AES_256_ECB,
00081 POLARSSL_CIPHER_AES_128_CBC,
00082 POLARSSL_CIPHER_AES_192_CBC,
00083 POLARSSL_CIPHER_AES_256_CBC,
00084 POLARSSL_CIPHER_AES_128_CFB128,
00085 POLARSSL_CIPHER_AES_192_CFB128,
00086 POLARSSL_CIPHER_AES_256_CFB128,
00087 POLARSSL_CIPHER_AES_128_CTR,
00088 POLARSSL_CIPHER_AES_192_CTR,
00089 POLARSSL_CIPHER_AES_256_CTR,
00090 POLARSSL_CIPHER_AES_128_GCM,
00091 POLARSSL_CIPHER_AES_192_GCM,
00092 POLARSSL_CIPHER_AES_256_GCM,
00093 POLARSSL_CIPHER_CAMELLIA_128_ECB,
00094 POLARSSL_CIPHER_CAMELLIA_192_ECB,
00095 POLARSSL_CIPHER_CAMELLIA_256_ECB,
00096 POLARSSL_CIPHER_CAMELLIA_128_CBC,
00097 POLARSSL_CIPHER_CAMELLIA_192_CBC,
00098 POLARSSL_CIPHER_CAMELLIA_256_CBC,
00099 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
00100 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
00101 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
00102 POLARSSL_CIPHER_CAMELLIA_128_CTR,
00103 POLARSSL_CIPHER_CAMELLIA_192_CTR,
00104 POLARSSL_CIPHER_CAMELLIA_256_CTR,
00105 POLARSSL_CIPHER_CAMELLIA_128_GCM,
00106 POLARSSL_CIPHER_CAMELLIA_192_GCM,
00107 POLARSSL_CIPHER_CAMELLIA_256_GCM,
00108 POLARSSL_CIPHER_DES_ECB,
00109 POLARSSL_CIPHER_DES_CBC,
00110 POLARSSL_CIPHER_DES_EDE_ECB,
00111 POLARSSL_CIPHER_DES_EDE_CBC,
00112 POLARSSL_CIPHER_DES_EDE3_ECB,
00113 POLARSSL_CIPHER_DES_EDE3_CBC,
00114 POLARSSL_CIPHER_BLOWFISH_ECB,
00115 POLARSSL_CIPHER_BLOWFISH_CBC,
00116 POLARSSL_CIPHER_BLOWFISH_CFB64,
00117 POLARSSL_CIPHER_BLOWFISH_CTR,
00118 POLARSSL_CIPHER_ARC4_128,
00119 } cipher_type_t;
00120
00121 typedef enum {
00122 POLARSSL_MODE_NONE = 0,
00123 POLARSSL_MODE_ECB,
00124 POLARSSL_MODE_CBC,
00125 POLARSSL_MODE_CFB,
00126 POLARSSL_MODE_OFB,
00127 POLARSSL_MODE_CTR,
00128 POLARSSL_MODE_GCM,
00129 POLARSSL_MODE_STREAM,
00130 } cipher_mode_t;
00131
00132 typedef enum {
00133 POLARSSL_PADDING_PKCS7 = 0,
00134 POLARSSL_PADDING_ONE_AND_ZEROS,
00135 POLARSSL_PADDING_ZEROS_AND_LEN,
00136 POLARSSL_PADDING_ZEROS,
00137 POLARSSL_PADDING_NONE,
00138 } cipher_padding_t;
00139
00140 typedef enum {
00141 POLARSSL_OPERATION_NONE = -1,
00142 POLARSSL_DECRYPT = 0,
00143 POLARSSL_ENCRYPT,
00144 } operation_t;
00145
00146 enum {
00148 POLARSSL_KEY_LENGTH_NONE = 0,
00150 POLARSSL_KEY_LENGTH_DES = 64,
00152 POLARSSL_KEY_LENGTH_DES_EDE = 128,
00154 POLARSSL_KEY_LENGTH_DES_EDE3 = 192,
00155 };
00156
00158 #define POLARSSL_MAX_IV_LENGTH 16
00159
00160 #define POLARSSL_MAX_BLOCK_LENGTH 16
00161
00165 typedef struct {
00166
00168 cipher_id_t cipher;
00169
00171 int (*ecb_func)( void *ctx, operation_t mode,
00172 const unsigned char *input, unsigned char *output );
00173
00175 int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv,
00176 const unsigned char *input, unsigned char *output );
00177
00179 int (*cfb_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off,
00180 unsigned char *iv, const unsigned char *input, unsigned char *output );
00181
00183 int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter,
00184 unsigned char *stream_block, const unsigned char *input, unsigned char *output );
00185
00187 int (*stream_func)( void *ctx, size_t length,
00188 const unsigned char *input, unsigned char *output );
00189
00191 int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length);
00192
00194 int (*setkey_dec_func)( void *ctx, const unsigned char *key, unsigned int key_length);
00195
00197 void * (*ctx_alloc_func)( void );
00198
00200 void (*ctx_free_func)( void *ctx );
00201
00202 } cipher_base_t;
00203
00207 typedef struct {
00209 cipher_type_t type;
00210
00212 cipher_mode_t mode;
00213
00216 unsigned int key_length;
00217
00219 const char * name;
00220
00223 unsigned int iv_size;
00224
00226 int accepts_variable_iv_size;
00227
00229 unsigned int block_size;
00230
00232 const cipher_base_t *base;
00233
00234 } cipher_info_t;
00235
00239 typedef struct {
00241 const cipher_info_t *cipher_info;
00242
00244 int key_length;
00245
00247 operation_t operation;
00248
00250 void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
00251 int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
00252
00254 unsigned char unprocessed_data[POLARSSL_MAX_BLOCK_LENGTH];
00255
00257 size_t unprocessed_len;
00258
00260 unsigned char iv[POLARSSL_MAX_IV_LENGTH];
00261
00263 size_t iv_size;
00264
00266 void *cipher_ctx;
00267 } cipher_context_t;
00268
00275 const int *cipher_list( void );
00276
00286 const cipher_info_t *cipher_info_from_string( const char *cipher_name );
00287
00297 const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type );
00298
00311 const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
00312 int key_length,
00313 const cipher_mode_t mode );
00314
00327 int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info );
00328
00338 int cipher_free_ctx( cipher_context_t *ctx );
00339
00348 static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx )
00349 {
00350 if( NULL == ctx || NULL == ctx->cipher_info )
00351 return 0;
00352
00353 return ctx->cipher_info->block_size;
00354 }
00355
00365 static inline cipher_mode_t cipher_get_cipher_mode( const cipher_context_t *ctx )
00366 {
00367 if( NULL == ctx || NULL == ctx->cipher_info )
00368 return POLARSSL_MODE_NONE;
00369
00370 return ctx->cipher_info->mode;
00371 }
00372
00382 static inline int cipher_get_iv_size( const cipher_context_t *ctx )
00383 {
00384 if( NULL == ctx || NULL == ctx->cipher_info )
00385 return 0;
00386
00387 if( ctx->iv_size != 0 )
00388 return (int) ctx->iv_size;
00389
00390 return ctx->cipher_info->iv_size;
00391 }
00392
00401 static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx )
00402 {
00403 if( NULL == ctx || NULL == ctx->cipher_info )
00404 return POLARSSL_CIPHER_NONE;
00405
00406 return ctx->cipher_info->type;
00407 }
00408
00416 static inline const char *cipher_get_name( const cipher_context_t *ctx )
00417 {
00418 if( NULL == ctx || NULL == ctx->cipher_info )
00419 return 0;
00420
00421 return ctx->cipher_info->name;
00422 }
00423
00433 static inline int cipher_get_key_size ( const cipher_context_t *ctx )
00434 {
00435 if( NULL == ctx || NULL == ctx->cipher_info )
00436 return POLARSSL_KEY_LENGTH_NONE;
00437
00438 return ctx->cipher_info->key_length;
00439 }
00440
00450 static inline operation_t cipher_get_operation( const cipher_context_t *ctx )
00451 {
00452 if( NULL == ctx || NULL == ctx->cipher_info )
00453 return POLARSSL_OPERATION_NONE;
00454
00455 return ctx->operation;
00456 }
00457
00473 int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length,
00474 const operation_t operation );
00475
00476 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
00477
00489 int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode );
00490 #endif
00491
00505 int cipher_set_iv( cipher_context_t *ctx,
00506 const unsigned char *iv, size_t iv_len );
00507
00516 int cipher_reset( cipher_context_t *ctx );
00517
00518 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00519
00534 int cipher_update_ad( cipher_context_t *ctx,
00535 const unsigned char *ad, size_t ad_len );
00536 #endif
00537
00567 int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
00568 unsigned char *output, size_t *olen );
00569
00587 int cipher_finish( cipher_context_t *ctx,
00588 unsigned char *output, size_t *olen );
00589
00590 #if defined(POLARSSL_CIPHER_MODE_AEAD)
00591
00602 int cipher_write_tag( cipher_context_t *ctx,
00603 unsigned char *tag, size_t tag_len );
00604
00617 int cipher_check_tag( cipher_context_t *ctx,
00618 const unsigned char *tag, size_t tag_len );
00619 #endif
00620
00626 int cipher_self_test( int verbose );
00627
00628 #ifdef __cplusplus
00629 }
00630 #endif
00631
00632 #endif