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 | * |
Paul Bakker | 530927b | 2015-02-13 14:24:10 +0100 | [diff] [blame] | 8 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 9 | * |
Manuel Pégourié-Gonnard | e12abf9 | 2015-01-28 17:13:45 +0000 | [diff] [blame] | 10 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along |
| 23 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 25 | */ |
| 26 | |
| 27 | #ifndef POLARSSL_CIPHER_H |
| 28 | #define POLARSSL_CIPHER_H |
| 29 | |
| 30 | #include <string.h> |
| 31 | |
Manuel Pégourié-Gonnard | 0123405 | 2015-10-05 11:40:01 +0100 | [diff] [blame] | 32 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ |
| 33 | !defined(inline) && !defined(__cplusplus) |
Paul Bakker | 569df2c | 2011-06-21 07:48:07 +0000 | [diff] [blame] | 34 | #define inline __inline |
Manuel Pégourié-Gonnard | cfd1ba9 | 2015-10-05 14:57:01 +0100 | [diff] [blame] | 35 | #endif |
Paul Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 36 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 37 | #define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */ |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 38 | #define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters to function. */ |
| 39 | #define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */ |
| 40 | #define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */ |
| 41 | #define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */ |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 42 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 43 | typedef enum { |
| 44 | POLARSSL_CIPHER_ID_NONE = 0, |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 45 | POLARSSL_CIPHER_ID_NULL, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 46 | POLARSSL_CIPHER_ID_AES, |
| 47 | POLARSSL_CIPHER_ID_DES, |
| 48 | POLARSSL_CIPHER_ID_3DES, |
| 49 | POLARSSL_CIPHER_ID_CAMELLIA, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 50 | POLARSSL_CIPHER_ID_BLOWFISH, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 51 | } cipher_id_t; |
| 52 | |
| 53 | typedef enum { |
| 54 | POLARSSL_CIPHER_NONE = 0, |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 55 | POLARSSL_CIPHER_NULL, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 56 | POLARSSL_CIPHER_AES_128_CBC, |
| 57 | POLARSSL_CIPHER_AES_192_CBC, |
| 58 | POLARSSL_CIPHER_AES_256_CBC, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 59 | POLARSSL_CIPHER_AES_128_CFB128, |
| 60 | POLARSSL_CIPHER_AES_192_CFB128, |
| 61 | POLARSSL_CIPHER_AES_256_CFB128, |
| 62 | POLARSSL_CIPHER_AES_128_CTR, |
| 63 | POLARSSL_CIPHER_AES_192_CTR, |
| 64 | POLARSSL_CIPHER_AES_256_CTR, |
| 65 | POLARSSL_CIPHER_CAMELLIA_128_CBC, |
| 66 | POLARSSL_CIPHER_CAMELLIA_192_CBC, |
| 67 | POLARSSL_CIPHER_CAMELLIA_256_CBC, |
| 68 | POLARSSL_CIPHER_CAMELLIA_128_CFB128, |
| 69 | POLARSSL_CIPHER_CAMELLIA_192_CFB128, |
| 70 | POLARSSL_CIPHER_CAMELLIA_256_CFB128, |
| 71 | POLARSSL_CIPHER_CAMELLIA_128_CTR, |
| 72 | POLARSSL_CIPHER_CAMELLIA_192_CTR, |
| 73 | POLARSSL_CIPHER_CAMELLIA_256_CTR, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 74 | POLARSSL_CIPHER_DES_CBC, |
| 75 | POLARSSL_CIPHER_DES_EDE_CBC, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 76 | POLARSSL_CIPHER_DES_EDE3_CBC, |
| 77 | POLARSSL_CIPHER_BLOWFISH_CBC, |
| 78 | POLARSSL_CIPHER_BLOWFISH_CFB64, |
| 79 | POLARSSL_CIPHER_BLOWFISH_CTR, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 80 | } cipher_type_t; |
| 81 | |
| 82 | typedef enum { |
| 83 | POLARSSL_MODE_NONE = 0, |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 84 | POLARSSL_MODE_NULL, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 85 | POLARSSL_MODE_CBC, |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 86 | POLARSSL_MODE_CFB, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 87 | POLARSSL_MODE_OFB, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 88 | POLARSSL_MODE_CTR, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 89 | } cipher_mode_t; |
| 90 | |
| 91 | typedef enum { |
Paul Bakker | f7e5bb5 | 2011-11-11 10:53:37 +0000 | [diff] [blame] | 92 | POLARSSL_OPERATION_NONE = -1, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 93 | POLARSSL_DECRYPT = 0, |
| 94 | POLARSSL_ENCRYPT, |
| 95 | } operation_t; |
| 96 | |
| 97 | enum { |
| 98 | /** Undefined key length */ |
| 99 | POLARSSL_KEY_LENGTH_NONE = 0, |
Paul Bakker | 5e18aed | 2011-11-15 15:38:45 +0000 | [diff] [blame] | 100 | /** Key length, in bits (including parity), for DES keys */ |
| 101 | POLARSSL_KEY_LENGTH_DES = 64, |
| 102 | /** Key length, in bits (including parity), for DES in two key EDE */ |
| 103 | POLARSSL_KEY_LENGTH_DES_EDE = 128, |
| 104 | /** Key length, in bits (including parity), for DES in three-key EDE */ |
| 105 | POLARSSL_KEY_LENGTH_DES_EDE3 = 192, |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 106 | /** Maximum length of any IV, in bytes */ |
| 107 | POLARSSL_MAX_IV_LENGTH = 16, |
| 108 | }; |
| 109 | |
| 110 | /** |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 111 | * Base cipher information. The non-mode specific functions and values. |
| 112 | */ |
| 113 | typedef struct { |
| 114 | |
| 115 | /** Base Cipher type (e.g. POLARSSL_CIPHER_ID_AES) */ |
| 116 | cipher_id_t cipher; |
| 117 | |
| 118 | /** Encrypt using CBC */ |
| 119 | int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv, |
| 120 | const unsigned char *input, unsigned char *output ); |
| 121 | |
Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 122 | /** Encrypt using CFB (Full length) */ |
| 123 | int (*cfb_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off, |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 124 | unsigned char *iv, const unsigned char *input, unsigned char *output ); |
| 125 | |
| 126 | /** Encrypt using CTR */ |
| 127 | int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter, |
| 128 | unsigned char *stream_block, const unsigned char *input, unsigned char *output ); |
| 129 | |
| 130 | /** Set key for encryption purposes */ |
| 131 | int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length); |
| 132 | |
| 133 | /** Set key for decryption purposes */ |
| 134 | int (*setkey_dec_func)( void *ctx, const unsigned char *key, unsigned int key_length); |
| 135 | |
| 136 | /** Allocate a new context */ |
| 137 | void * (*ctx_alloc_func)( void ); |
| 138 | |
| 139 | /** Free the given context */ |
| 140 | void (*ctx_free_func)( void *ctx ); |
| 141 | |
| 142 | } cipher_base_t; |
| 143 | |
| 144 | /** |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 145 | * Cipher information. Allows cipher functions to be called in a generic way. |
| 146 | */ |
| 147 | typedef struct { |
| 148 | /** Full cipher identifier (e.g. POLARSSL_CIPHER_AES_256_CBC) */ |
| 149 | cipher_type_t type; |
| 150 | |
Paul Bakker | f7e5bb5 | 2011-11-11 10:53:37 +0000 | [diff] [blame] | 151 | /** Cipher mode (e.g. POLARSSL_MODE_CBC) */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 152 | cipher_mode_t mode; |
| 153 | |
Paul Bakker | 5e18aed | 2011-11-15 15:38:45 +0000 | [diff] [blame] | 154 | /** Cipher key length, in bits (default length for variable sized ciphers) |
| 155 | * (Includes parity bits for ciphers like DES) */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 156 | unsigned int key_length; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 157 | |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 158 | /** Name of the cipher */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 159 | const char * name; |
| 160 | |
| 161 | /** IV size, in bytes */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 162 | unsigned int iv_size; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 163 | |
| 164 | /** block size, in bytes */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 165 | unsigned int block_size; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 166 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 167 | /** Base cipher information and functions */ |
| 168 | const cipher_base_t *base; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 169 | |
| 170 | } cipher_info_t; |
| 171 | |
| 172 | /** |
Paul Bakker | 2028156 | 2011-11-11 10:34:04 +0000 | [diff] [blame] | 173 | * Generic cipher context. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 174 | */ |
| 175 | typedef struct { |
| 176 | /** Information about the associated cipher */ |
| 177 | const cipher_info_t *cipher_info; |
| 178 | |
| 179 | /** Key length to use */ |
| 180 | int key_length; |
| 181 | |
| 182 | /** Operation that the context's key has been initialised for */ |
| 183 | operation_t operation; |
| 184 | |
| 185 | /** Buffer for data that hasn't been encrypted yet */ |
| 186 | unsigned char unprocessed_data[POLARSSL_MAX_IV_LENGTH]; |
| 187 | |
| 188 | /** Number of bytes that still need processing */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 189 | size_t unprocessed_len; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 190 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 191 | /** Current IV or NONCE_COUNTER for CTR-mode */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 192 | unsigned char iv[POLARSSL_MAX_IV_LENGTH]; |
| 193 | |
| 194 | /** Cipher-specific context */ |
| 195 | void *cipher_ctx; |
| 196 | } cipher_context_t; |
| 197 | |
| 198 | #ifdef __cplusplus |
| 199 | extern "C" { |
| 200 | #endif |
| 201 | |
| 202 | /** |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 203 | * \brief Returns the list of ciphers supported by the generic cipher module. |
| 204 | * |
| 205 | * \return a statically allocated array of ciphers, the last entry |
| 206 | * is 0. |
| 207 | */ |
| 208 | const int *cipher_list( void ); |
| 209 | |
| 210 | /** |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 211 | * \brief Returns the cipher information structure associated |
| 212 | * with the given cipher name. |
| 213 | * |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 214 | * \param cipher_name Name of the cipher to search for. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 215 | * |
| 216 | * \return the cipher information structure associated with the |
| 217 | * given cipher_name, or NULL if not found. |
| 218 | */ |
| 219 | const cipher_info_t *cipher_info_from_string( const char *cipher_name ); |
| 220 | |
| 221 | /** |
| 222 | * \brief Returns the cipher information structure associated |
| 223 | * with the given cipher type. |
| 224 | * |
| 225 | * \param cipher_type Type of the cipher to search for. |
| 226 | * |
| 227 | * \return the cipher information structure associated with the |
| 228 | * given cipher_type, or NULL if not found. |
| 229 | */ |
| 230 | const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type ); |
| 231 | |
| 232 | /** |
| 233 | * \brief Initialises and fills the cipher context structure with |
| 234 | * the appropriate values. |
| 235 | * |
| 236 | * \param ctx context to initialise. May not be NULL. |
| 237 | * \param cipher_info cipher to use. |
| 238 | * |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 239 | * \return \c 0 on success, |
| 240 | * \c POLARSSL_ERR_CIPHER_BAD_INPUT_DATA on parameter failure, |
| 241 | * \c POLARSSL_ERR_CIPHER_ALLOC_FAILED if allocation of the |
| 242 | * cipher-specific context failed. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 243 | */ |
| 244 | int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info ); |
| 245 | |
| 246 | /** |
| 247 | * \brief Free the cipher-specific context of ctx. Freeing ctx |
| 248 | * itself remains the responsibility of the caller. |
| 249 | * |
| 250 | * \param ctx Free the cipher-specific context |
| 251 | * |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 252 | * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if |
| 253 | * parameter verification fails. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 254 | */ |
| 255 | int cipher_free_ctx( cipher_context_t *ctx ); |
| 256 | |
| 257 | /** |
| 258 | * \brief Returns the block size of the given cipher. |
| 259 | * |
| 260 | * \param ctx cipher's context. Must have been initialised. |
| 261 | * |
| 262 | * \return size of the cipher's blocks, or 0 if ctx has not been |
| 263 | * initialised. |
| 264 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 265 | static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx ) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 266 | { |
| 267 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 268 | return 0; |
| 269 | |
| 270 | return ctx->cipher_info->block_size; |
| 271 | } |
| 272 | |
| 273 | /** |
Paul Bakker | f7e5bb5 | 2011-11-11 10:53:37 +0000 | [diff] [blame] | 274 | * \brief Returns the mode of operation for the cipher. |
| 275 | * (e.g. POLARSSL_MODE_CBC) |
| 276 | * |
| 277 | * \param ctx cipher's context. Must have been initialised. |
| 278 | * |
| 279 | * \return mode of operation, or POLARSSL_MODE_NONE if ctx |
| 280 | * has not been initialised. |
| 281 | */ |
| 282 | static inline cipher_mode_t cipher_get_cipher_mode( const cipher_context_t *ctx ) |
| 283 | { |
| 284 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 285 | return POLARSSL_MODE_NONE; |
| 286 | |
| 287 | return ctx->cipher_info->mode; |
| 288 | } |
| 289 | |
| 290 | /** |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 291 | * \brief Returns the size of the cipher's IV. |
| 292 | * |
| 293 | * \param ctx cipher's context. Must have been initialised. |
| 294 | * |
| 295 | * \return size of the cipher's IV, or 0 if ctx has not been |
| 296 | * initialised. |
| 297 | */ |
| 298 | static inline int cipher_get_iv_size( const cipher_context_t *ctx ) |
| 299 | { |
| 300 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 301 | return 0; |
| 302 | |
| 303 | return ctx->cipher_info->iv_size; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * \brief Returns the type of the given cipher. |
| 308 | * |
| 309 | * \param ctx cipher's context. Must have been initialised. |
| 310 | * |
| 311 | * \return type of the cipher, or POLARSSL_CIPHER_NONE if ctx has |
| 312 | * not been initialised. |
| 313 | */ |
| 314 | static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx ) |
| 315 | { |
| 316 | if( NULL == ctx || NULL == ctx->cipher_info ) |
Paul Bakker | 894dece | 2012-08-23 08:34:32 +0000 | [diff] [blame] | 317 | return POLARSSL_CIPHER_NONE; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 318 | |
| 319 | return ctx->cipher_info->type; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * \brief Returns the name of the given cipher, as a string. |
| 324 | * |
| 325 | * \param ctx cipher's context. Must have been initialised. |
| 326 | * |
| 327 | * \return name of the cipher, or NULL if ctx was not initialised. |
| 328 | */ |
| 329 | static inline const char *cipher_get_name( const cipher_context_t *ctx ) |
| 330 | { |
| 331 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 332 | return 0; |
| 333 | |
| 334 | return ctx->cipher_info->name; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * \brief Returns the key length of the cipher. |
| 339 | * |
| 340 | * \param ctx cipher's context. Must have been initialised. |
| 341 | * |
| 342 | * \return cipher's key length, in bits, or |
| 343 | * POLARSSL_KEY_LENGTH_NONE if ctx has not been |
| 344 | * initialised. |
| 345 | */ |
| 346 | static inline int cipher_get_key_size ( const cipher_context_t *ctx ) |
| 347 | { |
| 348 | if( NULL == ctx ) |
| 349 | return POLARSSL_KEY_LENGTH_NONE; |
| 350 | |
| 351 | return ctx->key_length; |
| 352 | } |
| 353 | |
| 354 | /** |
Paul Bakker | f7e5bb5 | 2011-11-11 10:53:37 +0000 | [diff] [blame] | 355 | * \brief Returns the operation of the given cipher. |
| 356 | * |
| 357 | * \param ctx cipher's context. Must have been initialised. |
| 358 | * |
| 359 | * \return operation (POLARSSL_ENCRYPT or POLARSSL_DECRYPT), |
| 360 | * or POLARSSL_OPERATION_NONE if ctx has not been |
| 361 | * initialised. |
| 362 | */ |
| 363 | static inline operation_t cipher_get_operation( const cipher_context_t *ctx ) |
| 364 | { |
| 365 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 366 | return POLARSSL_OPERATION_NONE; |
| 367 | |
| 368 | return ctx->operation; |
| 369 | } |
| 370 | |
| 371 | /** |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 372 | * \brief Set the key to use with the given context. |
| 373 | * |
| 374 | * \param ctx generic cipher context. May not be NULL. Must have been |
| 375 | * initialised using cipher_context_from_type or |
Paul Bakker | 1f14d08 | 2011-01-20 16:33:24 +0000 | [diff] [blame] | 376 | * cipher_context_from_string. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 377 | * \param key The key to use. |
| 378 | * \param key_length key length to use, in bits. |
| 379 | * \param operation Operation that the key will be used for, either |
| 380 | * POLARSSL_ENCRYPT or POLARSSL_DECRYPT. |
| 381 | * |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 382 | * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if |
| 383 | * parameter verification fails or a cipher specific |
| 384 | * error code. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 385 | */ |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 386 | 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] | 387 | const operation_t operation ); |
| 388 | |
| 389 | /** |
| 390 | * \brief Reset the given context, setting the IV to iv |
| 391 | * |
| 392 | * \param ctx generic cipher context |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 393 | * \param iv IV to use or NONCE_COUNTER in the case of a CTR-mode cipher |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 394 | * |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 395 | * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA |
| 396 | * if parameter verification fails. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 397 | */ |
| 398 | int cipher_reset( cipher_context_t *ctx, const unsigned char *iv ); |
| 399 | |
| 400 | /** |
| 401 | * \brief Generic cipher update function. Encrypts/decrypts |
| 402 | * using the given cipher context. Writes as many block |
| 403 | * size'd blocks of data as possible to output. Any data |
| 404 | * that cannot be written immediately will either be added |
| 405 | * to the next block, or flushed when cipher_final is |
| 406 | * called. |
| 407 | * |
| 408 | * \param ctx generic cipher context |
| 409 | * \param input buffer holding the input data |
| 410 | * \param ilen length of the input data |
| 411 | * \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] | 412 | * least ilen + block_size. Cannot be the same buffer as |
| 413 | * input! |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 414 | * \param olen length of the output data, will be filled with the |
| 415 | * actual number of bytes written. |
| 416 | * |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 417 | * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if |
| 418 | * parameter verification fails, |
| 419 | * POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE on an |
| 420 | * unsupported mode for a cipher or a cipher specific |
| 421 | * error code. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 422 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 423 | int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen, |
| 424 | unsigned char *output, size_t *olen ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 425 | |
| 426 | /** |
| 427 | * \brief Generic cipher finalisation function. If data still |
| 428 | * needs to be flushed from an incomplete block, data |
| 429 | * contained within it will be padded with the size of |
| 430 | * the last block, and written to the output buffer. |
| 431 | * |
Paul Bakker | 2028156 | 2011-11-11 10:34:04 +0000 | [diff] [blame] | 432 | * \param ctx Generic cipher context |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 433 | * \param output buffer to write data to. Needs block_size data available. |
| 434 | * \param olen length of the data written to the output buffer. |
| 435 | * |
Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 436 | * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if |
| 437 | * parameter verification fails, |
| 438 | * POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption |
| 439 | * expected a full block but was not provided one, |
| 440 | * POLARSSL_ERR_CIPHER_INVALID_PADDING on invalid padding |
| 441 | * while decrypting or a cipher specific error code. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 442 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 443 | int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 444 | |
| 445 | |
| 446 | /** |
| 447 | * \brief Checkup routine |
| 448 | * |
| 449 | * \return 0 if successful, or 1 if the test failed |
| 450 | */ |
| 451 | int cipher_self_test( int verbose ); |
| 452 | |
| 453 | #ifdef __cplusplus |
| 454 | } |
| 455 | #endif |
| 456 | |
| 457 | #endif /* POLARSSL_MD_H */ |