Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file cipher.h |
| 3 | * |
| 4 | * \brief Generic cipher wrapper. |
| 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
| 7 | * |
| 8 | * Copyright (C) 2006-2010, Brainspark B.V. |
| 9 | * |
| 10 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 11 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 12 | * |
| 13 | * All rights reserved. |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License along |
| 26 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 27 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 28 | */ |
| 29 | |
| 30 | #ifndef POLARSSL_CIPHER_H |
| 31 | #define POLARSSL_CIPHER_H |
| 32 | |
| 33 | #include <string.h> |
| 34 | |
Paul Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame^] | 35 | #ifdef _MSC_VER |
| 36 | #define inline _inline |
| 37 | #endif |
| 38 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 39 | typedef enum { |
| 40 | POLARSSL_CIPHER_ID_NONE = 0, |
| 41 | POLARSSL_CIPHER_ID_AES, |
| 42 | POLARSSL_CIPHER_ID_DES, |
| 43 | POLARSSL_CIPHER_ID_3DES, |
| 44 | POLARSSL_CIPHER_ID_CAMELLIA, |
| 45 | } cipher_id_t; |
| 46 | |
| 47 | typedef enum { |
| 48 | POLARSSL_CIPHER_NONE = 0, |
Paul Bakker | 1f14d08 | 2011-01-20 16:33:24 +0000 | [diff] [blame] | 49 | POLARSSL_CIPHER_CAMELLIA_128_CBC, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 50 | POLARSSL_CIPHER_CAMELLIA_192_CBC, |
| 51 | POLARSSL_CIPHER_CAMELLIA_256_CBC, |
| 52 | POLARSSL_CIPHER_AES_128_CBC, |
| 53 | POLARSSL_CIPHER_AES_192_CBC, |
| 54 | POLARSSL_CIPHER_AES_256_CBC, |
| 55 | POLARSSL_CIPHER_DES_CBC, |
| 56 | POLARSSL_CIPHER_DES_EDE_CBC, |
| 57 | POLARSSL_CIPHER_DES_EDE3_CBC |
| 58 | } cipher_type_t; |
| 59 | |
| 60 | typedef enum { |
| 61 | POLARSSL_MODE_NONE = 0, |
| 62 | POLARSSL_MODE_CBC, |
| 63 | POLARSSL_MODE_CFB, |
| 64 | POLARSSL_MODE_OFB, |
| 65 | } cipher_mode_t; |
| 66 | |
| 67 | typedef enum { |
| 68 | POLARSSL_DECRYPT = 0, |
| 69 | POLARSSL_ENCRYPT, |
| 70 | } operation_t; |
| 71 | |
| 72 | enum { |
| 73 | /** Undefined key length */ |
| 74 | POLARSSL_KEY_LENGTH_NONE = 0, |
| 75 | /** Key length, in bits, for DES keys */ |
| 76 | POLARSSL_KEY_LENGTH_DES = 56, |
| 77 | /** Key length, in bits, for DES in two key EDE */ |
| 78 | POLARSSL_KEY_LENGTH_DES_EDE = 112, |
| 79 | /** Key length, in bits, for DES in three-key EDE */ |
| 80 | POLARSSL_KEY_LENGTH_DES_EDE3 = 168, |
| 81 | /** Maximum length of any IV, in bytes */ |
| 82 | POLARSSL_MAX_IV_LENGTH = 16, |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * Cipher information. Allows cipher functions to be called in a generic way. |
| 87 | */ |
| 88 | typedef struct { |
| 89 | /** Full cipher identifier (e.g. POLARSSL_CIPHER_AES_256_CBC) */ |
| 90 | cipher_type_t type; |
| 91 | |
| 92 | /** Base Cipher type (e.g. POLARSSL_CIPHER_ID_AES) */ |
| 93 | cipher_id_t cipher; |
| 94 | |
| 95 | /** Cipher mode (e.g. POLARSSL_CIPHER_MODE_CBC) */ |
| 96 | cipher_mode_t mode; |
| 97 | |
| 98 | /** Cipher key length, in bits (default length for variable sized ciphers) */ |
| 99 | int key_length; |
| 100 | |
| 101 | /** Name of the message digest */ |
| 102 | const char * name; |
| 103 | |
| 104 | /** IV size, in bytes */ |
| 105 | int iv_size; |
| 106 | |
| 107 | /** block size, in bytes */ |
| 108 | int block_size; |
| 109 | |
| 110 | /** Encrypt using CBC */ |
| 111 | int (*cbc_func)( void *ctx, operation_t mode, int length, unsigned char *iv, |
| 112 | const unsigned char *input, unsigned char *output ); |
| 113 | |
| 114 | /** Set key for encryption purposes */ |
| 115 | int (*setkey_enc_func)( void *ctx, const unsigned char *key, int key_length); |
| 116 | |
| 117 | /** Set key for decryption purposes */ |
| 118 | int (*setkey_dec_func)( void *ctx, const unsigned char *key, int key_length); |
| 119 | |
| 120 | /** Allocate a new context */ |
| 121 | void * (*ctx_alloc_func)( void ); |
| 122 | |
| 123 | /** Free the given context */ |
| 124 | void (*ctx_free_func)( void *ctx ); |
| 125 | |
| 126 | } cipher_info_t; |
| 127 | |
| 128 | /** |
| 129 | * Generic message digest context. |
| 130 | */ |
| 131 | typedef struct { |
| 132 | /** Information about the associated cipher */ |
| 133 | const cipher_info_t *cipher_info; |
| 134 | |
| 135 | /** Key length to use */ |
| 136 | int key_length; |
| 137 | |
| 138 | /** Operation that the context's key has been initialised for */ |
| 139 | operation_t operation; |
| 140 | |
| 141 | /** Buffer for data that hasn't been encrypted yet */ |
| 142 | unsigned char unprocessed_data[POLARSSL_MAX_IV_LENGTH]; |
| 143 | |
| 144 | /** Number of bytes that still need processing */ |
| 145 | int unprocessed_len; |
| 146 | |
| 147 | /** Current IV */ |
| 148 | unsigned char iv[POLARSSL_MAX_IV_LENGTH]; |
| 149 | |
| 150 | /** Cipher-specific context */ |
| 151 | void *cipher_ctx; |
| 152 | } cipher_context_t; |
| 153 | |
| 154 | #ifdef __cplusplus |
| 155 | extern "C" { |
| 156 | #endif |
| 157 | |
| 158 | /** |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 159 | * \brief Returns the list of ciphers supported by the generic cipher module. |
| 160 | * |
| 161 | * \return a statically allocated array of ciphers, the last entry |
| 162 | * is 0. |
| 163 | */ |
| 164 | const int *cipher_list( void ); |
| 165 | |
| 166 | /** |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 167 | * \brief Returns the cipher information structure associated |
| 168 | * with the given cipher name. |
| 169 | * |
| 170 | * \param cipher_name Name of the cipher to search for. |
| 171 | * |
| 172 | * \return the cipher information structure associated with the |
| 173 | * given cipher_name, or NULL if not found. |
| 174 | */ |
| 175 | const cipher_info_t *cipher_info_from_string( const char *cipher_name ); |
| 176 | |
| 177 | /** |
| 178 | * \brief Returns the cipher information structure associated |
| 179 | * with the given cipher type. |
| 180 | * |
| 181 | * \param cipher_type Type of the cipher to search for. |
| 182 | * |
| 183 | * \return the cipher information structure associated with the |
| 184 | * given cipher_type, or NULL if not found. |
| 185 | */ |
| 186 | const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type ); |
| 187 | |
| 188 | /** |
| 189 | * \brief Initialises and fills the cipher context structure with |
| 190 | * the appropriate values. |
| 191 | * |
| 192 | * \param ctx context to initialise. May not be NULL. |
| 193 | * \param cipher_info cipher to use. |
| 194 | * |
| 195 | * \return \c 0 on success, \c 1 on parameter failure, \c 2 if |
| 196 | * allocation of the cipher-specific context failed. |
| 197 | */ |
| 198 | int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info ); |
| 199 | |
| 200 | /** |
| 201 | * \brief Free the cipher-specific context of ctx. Freeing ctx |
| 202 | * itself remains the responsibility of the caller. |
| 203 | * |
| 204 | * \param ctx Free the cipher-specific context |
| 205 | * |
| 206 | * \returns 0 on success, 1 if parameter verification fails. |
| 207 | */ |
| 208 | int cipher_free_ctx( cipher_context_t *ctx ); |
| 209 | |
| 210 | /** |
| 211 | * \brief Returns the block size of the given cipher. |
| 212 | * |
| 213 | * \param ctx cipher's context. Must have been initialised. |
| 214 | * |
| 215 | * \return size of the cipher's blocks, or 0 if ctx has not been |
| 216 | * initialised. |
| 217 | */ |
| 218 | static inline int cipher_get_block_size( const cipher_context_t *ctx ) |
| 219 | { |
| 220 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 221 | return 0; |
| 222 | |
| 223 | return ctx->cipher_info->block_size; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * \brief Returns the size of the cipher's IV. |
| 228 | * |
| 229 | * \param ctx cipher's context. Must have been initialised. |
| 230 | * |
| 231 | * \return size of the cipher's IV, or 0 if ctx has not been |
| 232 | * initialised. |
| 233 | */ |
| 234 | static inline int cipher_get_iv_size( const cipher_context_t *ctx ) |
| 235 | { |
| 236 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 237 | return 0; |
| 238 | |
| 239 | return ctx->cipher_info->iv_size; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * \brief Returns the type of the given cipher. |
| 244 | * |
| 245 | * \param ctx cipher's context. Must have been initialised. |
| 246 | * |
| 247 | * \return type of the cipher, or POLARSSL_CIPHER_NONE if ctx has |
| 248 | * not been initialised. |
| 249 | */ |
| 250 | static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx ) |
| 251 | { |
| 252 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 253 | return 0; |
| 254 | |
| 255 | return ctx->cipher_info->type; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * \brief Returns the name of the given cipher, as a string. |
| 260 | * |
| 261 | * \param ctx cipher's context. Must have been initialised. |
| 262 | * |
| 263 | * \return name of the cipher, or NULL if ctx was not initialised. |
| 264 | */ |
| 265 | static inline const char *cipher_get_name( const cipher_context_t *ctx ) |
| 266 | { |
| 267 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 268 | return 0; |
| 269 | |
| 270 | return ctx->cipher_info->name; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * \brief Returns the key length of the cipher. |
| 275 | * |
| 276 | * \param ctx cipher's context. Must have been initialised. |
| 277 | * |
| 278 | * \return cipher's key length, in bits, or |
| 279 | * POLARSSL_KEY_LENGTH_NONE if ctx has not been |
| 280 | * initialised. |
| 281 | */ |
| 282 | static inline int cipher_get_key_size ( const cipher_context_t *ctx ) |
| 283 | { |
| 284 | if( NULL == ctx ) |
| 285 | return POLARSSL_KEY_LENGTH_NONE; |
| 286 | |
| 287 | return ctx->key_length; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * \brief Set the key to use with the given context. |
| 292 | * |
| 293 | * \param ctx generic cipher context. May not be NULL. Must have been |
| 294 | * initialised using cipher_context_from_type or |
Paul Bakker | 1f14d08 | 2011-01-20 16:33:24 +0000 | [diff] [blame] | 295 | * cipher_context_from_string. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 296 | * \param key The key to use. |
| 297 | * \param key_length key length to use, in bits. |
| 298 | * \param operation Operation that the key will be used for, either |
| 299 | * POLARSSL_ENCRYPT or POLARSSL_DECRYPT. |
| 300 | * |
| 301 | * \returns 0 on success, 1 if parameter verification fails. |
| 302 | */ |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 303 | int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 304 | const operation_t operation ); |
| 305 | |
| 306 | /** |
| 307 | * \brief Reset the given context, setting the IV to iv |
| 308 | * |
| 309 | * \param ctx generic cipher context |
| 310 | * \param iv IV to use |
| 311 | * |
| 312 | * \returns 0 on success, 1 if parameter verification fails. |
| 313 | */ |
| 314 | int cipher_reset( cipher_context_t *ctx, const unsigned char *iv ); |
| 315 | |
| 316 | /** |
| 317 | * \brief Generic cipher update function. Encrypts/decrypts |
| 318 | * using the given cipher context. Writes as many block |
| 319 | * size'd blocks of data as possible to output. Any data |
| 320 | * that cannot be written immediately will either be added |
| 321 | * to the next block, or flushed when cipher_final is |
| 322 | * called. |
| 323 | * |
| 324 | * \param ctx generic cipher context |
| 325 | * \param input buffer holding the input data |
| 326 | * \param ilen length of the input data |
| 327 | * \param output buffer for the output data. Should be able to hold at |
Paul Bakker | 1f14d08 | 2011-01-20 16:33:24 +0000 | [diff] [blame] | 328 | * least ilen + block_size. Cannot be the same buffer as |
| 329 | * input! |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 330 | * \param olen length of the output data, will be filled with the |
| 331 | * actual number of bytes written. |
| 332 | * |
| 333 | * \returns 0 on success, 1 if parameter verification fails. |
| 334 | */ |
| 335 | int cipher_update( cipher_context_t *ctx, const unsigned char *input, int ilen, |
| 336 | unsigned char *output, int *olen ); |
| 337 | |
| 338 | /** |
| 339 | * \brief Generic cipher finalisation function. If data still |
| 340 | * needs to be flushed from an incomplete block, data |
| 341 | * contained within it will be padded with the size of |
| 342 | * the last block, and written to the output buffer. |
| 343 | * |
| 344 | * \param ctx Generic message digest context |
| 345 | * \param output buffer to write data to. Needs block_size data available. |
| 346 | * \param olen length of the data written to the output buffer. |
| 347 | * |
| 348 | * \returns 0 on success, 1 if parameter verification fails. |
| 349 | */ |
| 350 | int cipher_finish( cipher_context_t *ctx, unsigned char *output, int *olen); |
| 351 | |
| 352 | |
| 353 | /** |
| 354 | * \brief Checkup routine |
| 355 | * |
| 356 | * \return 0 if successful, or 1 if the test failed |
| 357 | */ |
| 358 | int cipher_self_test( int verbose ); |
| 359 | |
| 360 | #ifdef __cplusplus |
| 361 | } |
| 362 | #endif |
| 363 | |
| 364 | #endif /* POLARSSL_MD_H */ |