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