Jaeden Amero | e54e693 | 2018-08-06 16:19:58 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file cipher.h |
| 3 | * |
| 4 | * \brief This file contains an abstraction interface for use with the cipher |
| 5 | * primitives provided by the library. It provides a common interface to all of |
| 6 | * the available cipher operations. |
| 7 | * |
| 8 | * \author Adriaan de Jong <dejong@fox-it.com> |
| 9 | */ |
| 10 | /* |
| 11 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
| 12 | * SPDX-License-Identifier: Apache-2.0 |
| 13 | * |
| 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 15 | * not use this file except in compliance with the License. |
| 16 | * You may obtain a copy of the License at |
| 17 | * |
| 18 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | * |
| 20 | * Unless required by applicable law or agreed to in writing, software |
| 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 23 | * See the License for the specific language governing permissions and |
| 24 | * limitations under the License. |
| 25 | * |
| 26 | * This file is part of Mbed Crypto (https://tls.mbed.org) |
| 27 | */ |
| 28 | |
| 29 | #ifndef MBEDCRYPTO_CIPHER_H |
| 30 | #define MBEDCRYPTO_CIPHER_H |
| 31 | |
| 32 | #if !defined(MBEDCRYPTO_CONFIG_FILE) |
| 33 | #include "config.h" |
| 34 | #else |
| 35 | #include MBEDCRYPTO_CONFIG_FILE |
| 36 | #endif |
| 37 | |
| 38 | #include <stddef.h> |
| 39 | |
| 40 | #if defined(MBEDCRYPTO_GCM_C) || defined(MBEDCRYPTO_CCM_C) |
| 41 | #define MBEDCRYPTO_CIPHER_MODE_AEAD |
| 42 | #endif |
| 43 | |
| 44 | #if defined(MBEDCRYPTO_CIPHER_MODE_CBC) |
| 45 | #define MBEDCRYPTO_CIPHER_MODE_WITH_PADDING |
| 46 | #endif |
| 47 | |
| 48 | #if defined(MBEDCRYPTO_ARC4_C) |
| 49 | #define MBEDCRYPTO_CIPHER_MODE_STREAM |
| 50 | #endif |
| 51 | |
| 52 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ |
| 53 | !defined(inline) && !defined(__cplusplus) |
| 54 | #define inline __inline |
| 55 | #endif |
| 56 | |
| 57 | #define MBEDCRYPTO_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */ |
| 58 | #define MBEDCRYPTO_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters. */ |
| 59 | #define MBEDCRYPTO_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */ |
| 60 | #define MBEDCRYPTO_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */ |
| 61 | #define MBEDCRYPTO_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */ |
| 62 | #define MBEDCRYPTO_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */ |
| 63 | #define MBEDCRYPTO_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid. For example, because it was freed. */ |
| 64 | #define MBEDCRYPTO_ERR_CIPHER_HW_ACCEL_FAILED -0x6400 /**< Cipher hardware accelerator failed. */ |
| 65 | |
| 66 | #define MBEDCRYPTO_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length. */ |
| 67 | #define MBEDCRYPTO_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length. */ |
| 68 | |
| 69 | #ifdef __cplusplus |
| 70 | extern "C" { |
| 71 | #endif |
| 72 | |
| 73 | /** |
| 74 | * \brief Supported cipher types. |
| 75 | * |
| 76 | * \warning RC4 and DES are considered weak ciphers and their use |
| 77 | * constitutes a security risk. Arm recommends considering stronger |
| 78 | * ciphers instead. |
| 79 | */ |
| 80 | typedef enum { |
| 81 | MBEDCRYPTO_CIPHER_ID_NONE = 0, /**< Placeholder to mark the end of cipher ID lists. */ |
| 82 | MBEDCRYPTO_CIPHER_ID_NULL, /**< The identity cipher, treated as a stream cipher. */ |
| 83 | MBEDCRYPTO_CIPHER_ID_AES, /**< The AES cipher. */ |
| 84 | MBEDCRYPTO_CIPHER_ID_DES, /**< The DES cipher. */ |
| 85 | MBEDCRYPTO_CIPHER_ID_3DES, /**< The Triple DES cipher. */ |
| 86 | MBEDCRYPTO_CIPHER_ID_CAMELLIA, /**< The Camellia cipher. */ |
| 87 | MBEDCRYPTO_CIPHER_ID_BLOWFISH, /**< The Blowfish cipher. */ |
| 88 | MBEDCRYPTO_CIPHER_ID_ARC4, /**< The RC4 cipher. */ |
| 89 | } mbedcrypto_cipher_id_t; |
| 90 | |
| 91 | /** |
| 92 | * \brief Supported {cipher type, cipher mode} pairs. |
| 93 | * |
| 94 | * \warning RC4 and DES are considered weak ciphers and their use |
| 95 | * constitutes a security risk. Arm recommends considering stronger |
| 96 | * ciphers instead. |
| 97 | */ |
| 98 | typedef enum { |
| 99 | MBEDCRYPTO_CIPHER_NONE = 0, /**< Placeholder to mark the end of cipher-pair lists. */ |
| 100 | MBEDCRYPTO_CIPHER_NULL, /**< The identity stream cipher. */ |
| 101 | MBEDCRYPTO_CIPHER_AES_128_ECB, /**< AES cipher with 128-bit ECB mode. */ |
| 102 | MBEDCRYPTO_CIPHER_AES_192_ECB, /**< AES cipher with 192-bit ECB mode. */ |
| 103 | MBEDCRYPTO_CIPHER_AES_256_ECB, /**< AES cipher with 256-bit ECB mode. */ |
| 104 | MBEDCRYPTO_CIPHER_AES_128_CBC, /**< AES cipher with 128-bit CBC mode. */ |
| 105 | MBEDCRYPTO_CIPHER_AES_192_CBC, /**< AES cipher with 192-bit CBC mode. */ |
| 106 | MBEDCRYPTO_CIPHER_AES_256_CBC, /**< AES cipher with 256-bit CBC mode. */ |
| 107 | MBEDCRYPTO_CIPHER_AES_128_CFB128, /**< AES cipher with 128-bit CFB128 mode. */ |
| 108 | MBEDCRYPTO_CIPHER_AES_192_CFB128, /**< AES cipher with 192-bit CFB128 mode. */ |
| 109 | MBEDCRYPTO_CIPHER_AES_256_CFB128, /**< AES cipher with 256-bit CFB128 mode. */ |
| 110 | MBEDCRYPTO_CIPHER_AES_128_CTR, /**< AES cipher with 128-bit CTR mode. */ |
| 111 | MBEDCRYPTO_CIPHER_AES_192_CTR, /**< AES cipher with 192-bit CTR mode. */ |
| 112 | MBEDCRYPTO_CIPHER_AES_256_CTR, /**< AES cipher with 256-bit CTR mode. */ |
| 113 | MBEDCRYPTO_CIPHER_AES_128_GCM, /**< AES cipher with 128-bit GCM mode. */ |
| 114 | MBEDCRYPTO_CIPHER_AES_192_GCM, /**< AES cipher with 192-bit GCM mode. */ |
| 115 | MBEDCRYPTO_CIPHER_AES_256_GCM, /**< AES cipher with 256-bit GCM mode. */ |
| 116 | MBEDCRYPTO_CIPHER_CAMELLIA_128_ECB, /**< Camellia cipher with 128-bit ECB mode. */ |
| 117 | MBEDCRYPTO_CIPHER_CAMELLIA_192_ECB, /**< Camellia cipher with 192-bit ECB mode. */ |
| 118 | MBEDCRYPTO_CIPHER_CAMELLIA_256_ECB, /**< Camellia cipher with 256-bit ECB mode. */ |
| 119 | MBEDCRYPTO_CIPHER_CAMELLIA_128_CBC, /**< Camellia cipher with 128-bit CBC mode. */ |
| 120 | MBEDCRYPTO_CIPHER_CAMELLIA_192_CBC, /**< Camellia cipher with 192-bit CBC mode. */ |
| 121 | MBEDCRYPTO_CIPHER_CAMELLIA_256_CBC, /**< Camellia cipher with 256-bit CBC mode. */ |
| 122 | MBEDCRYPTO_CIPHER_CAMELLIA_128_CFB128, /**< Camellia cipher with 128-bit CFB128 mode. */ |
| 123 | MBEDCRYPTO_CIPHER_CAMELLIA_192_CFB128, /**< Camellia cipher with 192-bit CFB128 mode. */ |
| 124 | MBEDCRYPTO_CIPHER_CAMELLIA_256_CFB128, /**< Camellia cipher with 256-bit CFB128 mode. */ |
| 125 | MBEDCRYPTO_CIPHER_CAMELLIA_128_CTR, /**< Camellia cipher with 128-bit CTR mode. */ |
| 126 | MBEDCRYPTO_CIPHER_CAMELLIA_192_CTR, /**< Camellia cipher with 192-bit CTR mode. */ |
| 127 | MBEDCRYPTO_CIPHER_CAMELLIA_256_CTR, /**< Camellia cipher with 256-bit CTR mode. */ |
| 128 | MBEDCRYPTO_CIPHER_CAMELLIA_128_GCM, /**< Camellia cipher with 128-bit GCM mode. */ |
| 129 | MBEDCRYPTO_CIPHER_CAMELLIA_192_GCM, /**< Camellia cipher with 192-bit GCM mode. */ |
| 130 | MBEDCRYPTO_CIPHER_CAMELLIA_256_GCM, /**< Camellia cipher with 256-bit GCM mode. */ |
| 131 | MBEDCRYPTO_CIPHER_DES_ECB, /**< DES cipher with ECB mode. */ |
| 132 | MBEDCRYPTO_CIPHER_DES_CBC, /**< DES cipher with CBC mode. */ |
| 133 | MBEDCRYPTO_CIPHER_DES_EDE_ECB, /**< DES cipher with EDE ECB mode. */ |
| 134 | MBEDCRYPTO_CIPHER_DES_EDE_CBC, /**< DES cipher with EDE CBC mode. */ |
| 135 | MBEDCRYPTO_CIPHER_DES_EDE3_ECB, /**< DES cipher with EDE3 ECB mode. */ |
| 136 | MBEDCRYPTO_CIPHER_DES_EDE3_CBC, /**< DES cipher with EDE3 CBC mode. */ |
| 137 | MBEDCRYPTO_CIPHER_BLOWFISH_ECB, /**< Blowfish cipher with ECB mode. */ |
| 138 | MBEDCRYPTO_CIPHER_BLOWFISH_CBC, /**< Blowfish cipher with CBC mode. */ |
| 139 | MBEDCRYPTO_CIPHER_BLOWFISH_CFB64, /**< Blowfish cipher with CFB64 mode. */ |
| 140 | MBEDCRYPTO_CIPHER_BLOWFISH_CTR, /**< Blowfish cipher with CTR mode. */ |
| 141 | MBEDCRYPTO_CIPHER_ARC4_128, /**< RC4 cipher with 128-bit mode. */ |
| 142 | MBEDCRYPTO_CIPHER_AES_128_CCM, /**< AES cipher with 128-bit CCM mode. */ |
| 143 | MBEDCRYPTO_CIPHER_AES_192_CCM, /**< AES cipher with 192-bit CCM mode. */ |
| 144 | MBEDCRYPTO_CIPHER_AES_256_CCM, /**< AES cipher with 256-bit CCM mode. */ |
| 145 | MBEDCRYPTO_CIPHER_CAMELLIA_128_CCM, /**< Camellia cipher with 128-bit CCM mode. */ |
| 146 | MBEDCRYPTO_CIPHER_CAMELLIA_192_CCM, /**< Camellia cipher with 192-bit CCM mode. */ |
| 147 | MBEDCRYPTO_CIPHER_CAMELLIA_256_CCM, /**< Camellia cipher with 256-bit CCM mode. */ |
| 148 | } mbedcrypto_cipher_type_t; |
| 149 | |
| 150 | /** Supported cipher modes. */ |
| 151 | typedef enum { |
| 152 | MBEDCRYPTO_MODE_NONE = 0, /**< None. */ |
| 153 | MBEDCRYPTO_MODE_ECB, /**< The ECB cipher mode. */ |
| 154 | MBEDCRYPTO_MODE_CBC, /**< The CBC cipher mode. */ |
| 155 | MBEDCRYPTO_MODE_CFB, /**< The CFB cipher mode. */ |
| 156 | MBEDCRYPTO_MODE_OFB, /**< The OFB cipher mode - unsupported. */ |
| 157 | MBEDCRYPTO_MODE_CTR, /**< The CTR cipher mode. */ |
| 158 | MBEDCRYPTO_MODE_GCM, /**< The GCM cipher mode. */ |
| 159 | MBEDCRYPTO_MODE_STREAM, /**< The stream cipher mode. */ |
| 160 | MBEDCRYPTO_MODE_CCM, /**< The CCM cipher mode. */ |
| 161 | } mbedcrypto_cipher_mode_t; |
| 162 | |
| 163 | /** Supported cipher padding types. */ |
| 164 | typedef enum { |
| 165 | MBEDCRYPTO_PADDING_PKCS7 = 0, /**< PKCS7 padding (default). */ |
| 166 | MBEDCRYPTO_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding. */ |
| 167 | MBEDCRYPTO_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding. */ |
| 168 | MBEDCRYPTO_PADDING_ZEROS, /**< Zero padding (not reversible). */ |
| 169 | MBEDCRYPTO_PADDING_NONE, /**< Never pad (full blocks only). */ |
| 170 | } mbedcrypto_cipher_padding_t; |
| 171 | |
| 172 | /** Type of operation. */ |
| 173 | typedef enum { |
| 174 | MBEDCRYPTO_OPERATION_NONE = -1, |
| 175 | MBEDCRYPTO_DECRYPT = 0, |
| 176 | MBEDCRYPTO_ENCRYPT, |
| 177 | } mbedcrypto_operation_t; |
| 178 | |
| 179 | enum { |
| 180 | /** Undefined key length. */ |
| 181 | MBEDCRYPTO_KEY_LENGTH_NONE = 0, |
| 182 | /** Key length, in bits (including parity), for DES keys. */ |
| 183 | MBEDCRYPTO_KEY_LENGTH_DES = 64, |
| 184 | /** Key length in bits, including parity, for DES in two-key EDE. */ |
| 185 | MBEDCRYPTO_KEY_LENGTH_DES_EDE = 128, |
| 186 | /** Key length in bits, including parity, for DES in three-key EDE. */ |
| 187 | MBEDCRYPTO_KEY_LENGTH_DES_EDE3 = 192, |
| 188 | }; |
| 189 | |
| 190 | /** Maximum length of any IV, in Bytes. */ |
| 191 | #define MBEDCRYPTO_MAX_IV_LENGTH 16 |
| 192 | /** Maximum block size of any cipher, in Bytes. */ |
| 193 | #define MBEDCRYPTO_MAX_BLOCK_LENGTH 16 |
| 194 | |
| 195 | /** |
| 196 | * Base cipher information (opaque struct). |
| 197 | */ |
| 198 | typedef struct mbedcrypto_cipher_base_t mbedcrypto_cipher_base_t; |
| 199 | |
| 200 | /** |
| 201 | * CMAC context (opaque struct). |
| 202 | */ |
| 203 | typedef struct mbedcrypto_cmac_context_t mbedcrypto_cmac_context_t; |
| 204 | |
| 205 | /** |
| 206 | * Cipher information. Allows calling cipher functions |
| 207 | * in a generic way. |
| 208 | */ |
| 209 | typedef struct { |
| 210 | /** Full cipher identifier. For example, |
| 211 | * MBEDCRYPTO_CIPHER_AES_256_CBC. |
| 212 | */ |
| 213 | mbedcrypto_cipher_type_t type; |
| 214 | |
| 215 | /** The cipher mode. For example, MBEDCRYPTO_MODE_CBC. */ |
| 216 | mbedcrypto_cipher_mode_t mode; |
| 217 | |
| 218 | /** The cipher key length, in bits. This is the |
| 219 | * default length for variable sized ciphers. |
| 220 | * Includes parity bits for ciphers like DES. |
| 221 | */ |
| 222 | unsigned int key_bitlen; |
| 223 | |
| 224 | /** Name of the cipher. */ |
| 225 | const char * name; |
| 226 | |
| 227 | /** IV or nonce size, in Bytes. |
| 228 | * For ciphers that accept variable IV sizes, |
| 229 | * this is the recommended size. |
| 230 | */ |
| 231 | unsigned int iv_size; |
| 232 | |
| 233 | /** Bitflag comprised of MBEDCRYPTO_CIPHER_VARIABLE_IV_LEN and |
| 234 | * MBEDCRYPTO_CIPHER_VARIABLE_KEY_LEN indicating whether the |
| 235 | * cipher supports variable IV or variable key sizes, respectively. |
| 236 | */ |
| 237 | int flags; |
| 238 | |
| 239 | /** The block size, in Bytes. */ |
| 240 | unsigned int block_size; |
| 241 | |
| 242 | /** Struct for base cipher information and functions. */ |
| 243 | const mbedcrypto_cipher_base_t *base; |
| 244 | |
| 245 | } mbedcrypto_cipher_info_t; |
| 246 | |
| 247 | /** |
| 248 | * Generic cipher context. |
| 249 | */ |
| 250 | typedef struct { |
| 251 | /** Information about the associated cipher. */ |
| 252 | const mbedcrypto_cipher_info_t *cipher_info; |
| 253 | |
| 254 | /** Key length to use. */ |
| 255 | int key_bitlen; |
| 256 | |
| 257 | /** Operation that the key of the context has been |
| 258 | * initialized for. |
| 259 | */ |
| 260 | mbedcrypto_operation_t operation; |
| 261 | |
| 262 | #if defined(MBEDCRYPTO_CIPHER_MODE_WITH_PADDING) |
| 263 | /** Padding functions to use, if relevant for |
| 264 | * the specific cipher mode. |
| 265 | */ |
| 266 | void (*add_padding)( unsigned char *output, size_t olen, size_t data_len ); |
| 267 | int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len ); |
| 268 | #endif |
| 269 | |
| 270 | /** Buffer for input that has not been processed yet. */ |
| 271 | unsigned char unprocessed_data[MBEDCRYPTO_MAX_BLOCK_LENGTH]; |
| 272 | |
| 273 | /** Number of Bytes that have not been processed yet. */ |
| 274 | size_t unprocessed_len; |
| 275 | |
| 276 | /** Current IV or NONCE_COUNTER for CTR-mode. */ |
| 277 | unsigned char iv[MBEDCRYPTO_MAX_IV_LENGTH]; |
| 278 | |
| 279 | /** IV size in Bytes, for ciphers with variable-length IVs. */ |
| 280 | size_t iv_size; |
| 281 | |
| 282 | /** The cipher-specific context. */ |
| 283 | void *cipher_ctx; |
| 284 | |
| 285 | #if defined(MBEDCRYPTO_CMAC_C) |
| 286 | /** CMAC-specific context. */ |
| 287 | mbedcrypto_cmac_context_t *cmac_ctx; |
| 288 | #endif |
| 289 | } mbedcrypto_cipher_context_t; |
| 290 | |
| 291 | /** |
| 292 | * \brief This function retrieves the list of ciphers supported by the generic |
| 293 | * cipher module. |
| 294 | * |
| 295 | * \return A statically-allocated array of ciphers. The last entry |
| 296 | * is zero. |
| 297 | */ |
| 298 | const int *mbedcrypto_cipher_list( void ); |
| 299 | |
| 300 | /** |
| 301 | * \brief This function retrieves the cipher-information |
| 302 | * structure associated with the given cipher name. |
| 303 | * |
| 304 | * \param cipher_name Name of the cipher to search for. |
| 305 | * |
| 306 | * \return The cipher information structure associated with the |
| 307 | * given \p cipher_name. |
| 308 | * \return NULL if the associated cipher information is not found. |
| 309 | */ |
| 310 | const mbedcrypto_cipher_info_t *mbedcrypto_cipher_info_from_string( const char *cipher_name ); |
| 311 | |
| 312 | /** |
| 313 | * \brief This function retrieves the cipher-information |
| 314 | * structure associated with the given cipher type. |
| 315 | * |
| 316 | * \param cipher_type Type of the cipher to search for. |
| 317 | * |
| 318 | * \return The cipher information structure associated with the |
| 319 | * given \p cipher_type. |
| 320 | * \return NULL if the associated cipher information is not found. |
| 321 | */ |
| 322 | const mbedcrypto_cipher_info_t *mbedcrypto_cipher_info_from_type( const mbedcrypto_cipher_type_t cipher_type ); |
| 323 | |
| 324 | /** |
| 325 | * \brief This function retrieves the cipher-information |
| 326 | * structure associated with the given cipher ID, |
| 327 | * key size and mode. |
| 328 | * |
| 329 | * \param cipher_id The ID of the cipher to search for. For example, |
| 330 | * #MBEDCRYPTO_CIPHER_ID_AES. |
| 331 | * \param key_bitlen The length of the key in bits. |
| 332 | * \param mode The cipher mode. For example, #MBEDCRYPTO_MODE_CBC. |
| 333 | * |
| 334 | * \return The cipher information structure associated with the |
| 335 | * given \p cipher_id. |
| 336 | * \return NULL if the associated cipher information is not found. |
| 337 | */ |
| 338 | const mbedcrypto_cipher_info_t *mbedcrypto_cipher_info_from_values( const mbedcrypto_cipher_id_t cipher_id, |
| 339 | int key_bitlen, |
| 340 | const mbedcrypto_cipher_mode_t mode ); |
| 341 | |
| 342 | /** |
| 343 | * \brief This function initializes a \p cipher_context as NONE. |
| 344 | */ |
| 345 | void mbedcrypto_cipher_init( mbedcrypto_cipher_context_t *ctx ); |
| 346 | |
| 347 | /** |
| 348 | * \brief This function frees and clears the cipher-specific |
| 349 | * context of \p ctx. Freeing \p ctx itself remains the |
| 350 | * responsibility of the caller. |
| 351 | */ |
| 352 | void mbedcrypto_cipher_free( mbedcrypto_cipher_context_t *ctx ); |
| 353 | |
| 354 | |
| 355 | /** |
| 356 | * \brief This function initializes and fills the cipher-context |
| 357 | * structure with the appropriate values. It also clears |
| 358 | * the structure. |
| 359 | * |
| 360 | * \param ctx The context to initialize. May not be NULL. |
| 361 | * \param cipher_info The cipher to use. |
| 362 | * |
| 363 | * \return \c 0 on success. |
| 364 | * \return #MBEDCRYPTO_ERR_CIPHER_BAD_INPUT_DATA on |
| 365 | * parameter-verification failure. |
| 366 | * \return #MBEDCRYPTO_ERR_CIPHER_ALLOC_FAILED if allocation of the |
| 367 | * cipher-specific context fails. |
| 368 | * |
| 369 | * \internal Currently, the function also clears the structure. |
| 370 | * In future versions, the caller will be required to call |
| 371 | * mbedcrypto_cipher_init() on the structure first. |
| 372 | */ |
| 373 | int mbedcrypto_cipher_setup( mbedcrypto_cipher_context_t *ctx, const mbedcrypto_cipher_info_t *cipher_info ); |
| 374 | |
| 375 | /** |
| 376 | * \brief This function returns the block size of the given cipher. |
| 377 | * |
| 378 | * \param ctx The context of the cipher. Must be initialized. |
| 379 | * |
| 380 | * \return The size of the blocks of the cipher. |
| 381 | * \return 0 if \p ctx has not been initialized. |
| 382 | */ |
| 383 | static inline unsigned int mbedcrypto_cipher_get_block_size( const mbedcrypto_cipher_context_t *ctx ) |
| 384 | { |
| 385 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 386 | return 0; |
| 387 | |
| 388 | return ctx->cipher_info->block_size; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * \brief This function returns the mode of operation for |
| 393 | * the cipher. For example, MBEDCRYPTO_MODE_CBC. |
| 394 | * |
| 395 | * \param ctx The context of the cipher. Must be initialized. |
| 396 | * |
| 397 | * \return The mode of operation. |
| 398 | * \return #MBEDCRYPTO_MODE_NONE if \p ctx has not been initialized. |
| 399 | */ |
| 400 | static inline mbedcrypto_cipher_mode_t mbedcrypto_cipher_get_cipher_mode( const mbedcrypto_cipher_context_t *ctx ) |
| 401 | { |
| 402 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 403 | return MBEDCRYPTO_MODE_NONE; |
| 404 | |
| 405 | return ctx->cipher_info->mode; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * \brief This function returns the size of the IV or nonce |
| 410 | * of the cipher, in Bytes. |
| 411 | * |
| 412 | * \param ctx The context of the cipher. Must be initialized. |
| 413 | * |
| 414 | * \return The recommended IV size if no IV has been set. |
| 415 | * \return \c 0 for ciphers not using an IV or a nonce. |
| 416 | * \return The actual size if an IV has been set. |
| 417 | */ |
| 418 | static inline int mbedcrypto_cipher_get_iv_size( const mbedcrypto_cipher_context_t *ctx ) |
| 419 | { |
| 420 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 421 | return 0; |
| 422 | |
| 423 | if( ctx->iv_size != 0 ) |
| 424 | return (int) ctx->iv_size; |
| 425 | |
| 426 | return (int) ctx->cipher_info->iv_size; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * \brief This function returns the type of the given cipher. |
| 431 | * |
| 432 | * \param ctx The context of the cipher. Must be initialized. |
| 433 | * |
| 434 | * \return The type of the cipher. |
| 435 | * \return #MBEDCRYPTO_CIPHER_NONE if \p ctx has not been initialized. |
| 436 | */ |
| 437 | static inline mbedcrypto_cipher_type_t mbedcrypto_cipher_get_type( const mbedcrypto_cipher_context_t *ctx ) |
| 438 | { |
| 439 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 440 | return MBEDCRYPTO_CIPHER_NONE; |
| 441 | |
| 442 | return ctx->cipher_info->type; |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * \brief This function returns the name of the given cipher |
| 447 | * as a string. |
| 448 | * |
| 449 | * \param ctx The context of the cipher. Must be initialized. |
| 450 | * |
| 451 | * \return The name of the cipher. |
| 452 | * \return NULL if \p ctx has not been not initialized. |
| 453 | */ |
| 454 | static inline const char *mbedcrypto_cipher_get_name( const mbedcrypto_cipher_context_t *ctx ) |
| 455 | { |
| 456 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 457 | return 0; |
| 458 | |
| 459 | return ctx->cipher_info->name; |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * \brief This function returns the key length of the cipher. |
| 464 | * |
| 465 | * \param ctx The context of the cipher. Must be initialized. |
| 466 | * |
| 467 | * \return The key length of the cipher in bits. |
| 468 | * \return #MBEDCRYPTO_KEY_LENGTH_NONE if ctx \p has not been |
| 469 | * initialized. |
| 470 | */ |
| 471 | static inline int mbedcrypto_cipher_get_key_bitlen( const mbedcrypto_cipher_context_t *ctx ) |
| 472 | { |
| 473 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 474 | return MBEDCRYPTO_KEY_LENGTH_NONE; |
| 475 | |
| 476 | return (int) ctx->cipher_info->key_bitlen; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * \brief This function returns the operation of the given cipher. |
| 481 | * |
| 482 | * \param ctx The context of the cipher. Must be initialized. |
| 483 | * |
| 484 | * \return The type of operation: #MBEDCRYPTO_ENCRYPT or #MBEDCRYPTO_DECRYPT. |
| 485 | * \return #MBEDCRYPTO_OPERATION_NONE if \p ctx has not been initialized. |
| 486 | */ |
| 487 | static inline mbedcrypto_operation_t mbedcrypto_cipher_get_operation( const mbedcrypto_cipher_context_t *ctx ) |
| 488 | { |
| 489 | if( NULL == ctx || NULL == ctx->cipher_info ) |
| 490 | return MBEDCRYPTO_OPERATION_NONE; |
| 491 | |
| 492 | return ctx->operation; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * \brief This function sets the key to use with the given context. |
| 497 | * |
| 498 | * \param ctx The generic cipher context. May not be NULL. Must have |
| 499 | * been initialized using mbedcrypto_cipher_info_from_type() |
| 500 | * or mbedcrypto_cipher_info_from_string(). |
| 501 | * \param key The key to use. |
| 502 | * \param key_bitlen The key length to use, in bits. |
| 503 | * \param operation The operation that the key will be used for: |
| 504 | * #MBEDCRYPTO_ENCRYPT or #MBEDCRYPTO_DECRYPT. |
| 505 | * |
| 506 | * \return \c 0 on success. |
| 507 | * \return #MBEDCRYPTO_ERR_CIPHER_BAD_INPUT_DATA on |
| 508 | * parameter-verification failure. |
| 509 | * \return A cipher-specific error code on failure. |
| 510 | */ |
| 511 | int mbedcrypto_cipher_setkey( mbedcrypto_cipher_context_t *ctx, const unsigned char *key, |
| 512 | int key_bitlen, const mbedcrypto_operation_t operation ); |
| 513 | |
| 514 | #if defined(MBEDCRYPTO_CIPHER_MODE_WITH_PADDING) |
| 515 | /** |
| 516 | * \brief This function sets the padding mode, for cipher modes |
| 517 | * that use padding. |
| 518 | * |
| 519 | * The default passing mode is PKCS7 padding. |
| 520 | * |
| 521 | * \param ctx The generic cipher context. |
| 522 | * \param mode The padding mode. |
| 523 | * |
| 524 | * \return \c 0 on success. |
| 525 | * \return #MBEDCRYPTO_ERR_CIPHER_FEATURE_UNAVAILABLE |
| 526 | * if the selected padding mode is not supported. |
| 527 | * \return #MBEDCRYPTO_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode |
| 528 | * does not support padding. |
| 529 | */ |
| 530 | int mbedcrypto_cipher_set_padding_mode( mbedcrypto_cipher_context_t *ctx, mbedcrypto_cipher_padding_t mode ); |
| 531 | #endif /* MBEDCRYPTO_CIPHER_MODE_WITH_PADDING */ |
| 532 | |
| 533 | /** |
| 534 | * \brief This function sets the initialization vector (IV) |
| 535 | * or nonce. |
| 536 | * |
| 537 | * \note Some ciphers do not use IVs nor nonce. For these |
| 538 | * ciphers, this function has no effect. |
| 539 | * |
| 540 | * \param ctx The generic cipher context. |
| 541 | * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. |
| 542 | * \param iv_len The IV length for ciphers with variable-size IV. |
| 543 | * This parameter is discarded by ciphers with fixed-size IV. |
| 544 | * |
| 545 | * \return \c 0 on success. |
| 546 | * \return #MBEDCRYPTO_ERR_CIPHER_BAD_INPUT_DATA on |
| 547 | * parameter-verification failure. |
| 548 | */ |
| 549 | int mbedcrypto_cipher_set_iv( mbedcrypto_cipher_context_t *ctx, |
| 550 | const unsigned char *iv, size_t iv_len ); |
| 551 | |
| 552 | /** |
| 553 | * \brief This function resets the cipher state. |
| 554 | * |
| 555 | * \param ctx The generic cipher context. |
| 556 | * |
| 557 | * \return \c 0 on success. |
| 558 | * \return #MBEDCRYPTO_ERR_CIPHER_BAD_INPUT_DATA on |
| 559 | * parameter-verification failure. |
| 560 | */ |
| 561 | int mbedcrypto_cipher_reset( mbedcrypto_cipher_context_t *ctx ); |
| 562 | |
| 563 | #if defined(MBEDCRYPTO_GCM_C) |
| 564 | /** |
| 565 | * \brief This function adds additional data for AEAD ciphers. |
| 566 | * Only supported with GCM. Must be called |
| 567 | * exactly once, after mbedcrypto_cipher_reset(). |
| 568 | * |
| 569 | * \param ctx The generic cipher context. |
| 570 | * \param ad The additional data to use. |
| 571 | * \param ad_len the Length of \p ad. |
| 572 | * |
| 573 | * \return \c 0 on success. |
| 574 | * \return A specific error code on failure. |
| 575 | */ |
| 576 | int mbedcrypto_cipher_update_ad( mbedcrypto_cipher_context_t *ctx, |
| 577 | const unsigned char *ad, size_t ad_len ); |
| 578 | #endif /* MBEDCRYPTO_GCM_C */ |
| 579 | |
| 580 | /** |
| 581 | * \brief The generic cipher update function. It encrypts or |
| 582 | * decrypts using the given cipher context. Writes as |
| 583 | * many block-sized blocks of data as possible to output. |
| 584 | * Any data that cannot be written immediately is either |
| 585 | * added to the next block, or flushed when |
| 586 | * mbedcrypto_cipher_finish() is called. |
| 587 | * Exception: For MBEDCRYPTO_MODE_ECB, expects a single block |
| 588 | * in size. For example, 16 Bytes for AES. |
| 589 | * |
| 590 | * \note If the underlying cipher is used in GCM mode, all calls |
| 591 | * to this function, except for the last one before |
| 592 | * mbedcrypto_cipher_finish(), must have \p ilen as a |
| 593 | * multiple of the block size of the cipher. |
| 594 | * |
| 595 | * \param ctx The generic cipher context. |
| 596 | * \param input The buffer holding the input data. |
| 597 | * \param ilen The length of the input data. |
| 598 | * \param output The buffer for the output data. Must be able to hold at |
| 599 | * least \p ilen + block_size. Must not be the same buffer |
| 600 | * as input. |
| 601 | * \param olen The length of the output data, to be updated with the |
| 602 | * actual number of Bytes written. |
| 603 | * |
| 604 | * \return \c 0 on success. |
| 605 | * \return #MBEDCRYPTO_ERR_CIPHER_BAD_INPUT_DATA on |
| 606 | * parameter-verification failure. |
| 607 | * \return #MBEDCRYPTO_ERR_CIPHER_FEATURE_UNAVAILABLE on an |
| 608 | * unsupported mode for a cipher. |
| 609 | * \return A cipher-specific error code on failure. |
| 610 | */ |
| 611 | int mbedcrypto_cipher_update( mbedcrypto_cipher_context_t *ctx, const unsigned char *input, |
| 612 | size_t ilen, unsigned char *output, size_t *olen ); |
| 613 | |
| 614 | /** |
| 615 | * \brief The generic cipher finalization function. If data still |
| 616 | * needs to be flushed from an incomplete block, the data |
| 617 | * contained in it is padded to the size of |
| 618 | * the last block, and written to the \p output buffer. |
| 619 | * |
| 620 | * \param ctx The generic cipher context. |
| 621 | * \param output The buffer to write data to. Needs block_size available. |
| 622 | * \param olen The length of the data written to the \p output buffer. |
| 623 | * |
| 624 | * \return \c 0 on success. |
| 625 | * \return #MBEDCRYPTO_ERR_CIPHER_BAD_INPUT_DATA on |
| 626 | * parameter-verification failure. |
| 627 | * \return #MBEDCRYPTO_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption |
| 628 | * expecting a full block but not receiving one. |
| 629 | * \return #MBEDCRYPTO_ERR_CIPHER_INVALID_PADDING on invalid padding |
| 630 | * while decrypting. |
| 631 | * \return A cipher-specific error code on failure. |
| 632 | */ |
| 633 | int mbedcrypto_cipher_finish( mbedcrypto_cipher_context_t *ctx, |
| 634 | unsigned char *output, size_t *olen ); |
| 635 | |
| 636 | #if defined(MBEDCRYPTO_GCM_C) |
| 637 | /** |
| 638 | * \brief This function writes a tag for AEAD ciphers. |
| 639 | * Only supported with GCM. |
| 640 | * Must be called after mbedcrypto_cipher_finish(). |
| 641 | * |
| 642 | * \param ctx The generic cipher context. |
| 643 | * \param tag The buffer to write the tag to. |
| 644 | * \param tag_len The length of the tag to write. |
| 645 | * |
| 646 | * \return \c 0 on success. |
| 647 | * \return A specific error code on failure. |
| 648 | */ |
| 649 | int mbedcrypto_cipher_write_tag( mbedcrypto_cipher_context_t *ctx, |
| 650 | unsigned char *tag, size_t tag_len ); |
| 651 | |
| 652 | /** |
| 653 | * \brief This function checks the tag for AEAD ciphers. |
| 654 | * Only supported with GCM. |
| 655 | * Must be called after mbedcrypto_cipher_finish(). |
| 656 | * |
| 657 | * \param ctx The generic cipher context. |
| 658 | * \param tag The buffer holding the tag. |
| 659 | * \param tag_len The length of the tag to check. |
| 660 | * |
| 661 | * \return \c 0 on success. |
| 662 | * \return A specific error code on failure. |
| 663 | */ |
| 664 | int mbedcrypto_cipher_check_tag( mbedcrypto_cipher_context_t *ctx, |
| 665 | const unsigned char *tag, size_t tag_len ); |
| 666 | #endif /* MBEDCRYPTO_GCM_C */ |
| 667 | |
| 668 | /** |
| 669 | * \brief The generic all-in-one encryption/decryption function, |
| 670 | * for all ciphers except AEAD constructs. |
| 671 | * |
| 672 | * \param ctx The generic cipher context. |
| 673 | * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. |
| 674 | * \param iv_len The IV length for ciphers with variable-size IV. |
| 675 | * This parameter is discarded by ciphers with fixed-size |
| 676 | * IV. |
| 677 | * \param input The buffer holding the input data. |
| 678 | * \param ilen The length of the input data. |
| 679 | * \param output The buffer for the output data. Must be able to hold at |
| 680 | * least \p ilen + block_size. Must not be the same buffer |
| 681 | * as input. |
| 682 | * \param olen The length of the output data, to be updated with the |
| 683 | * actual number of Bytes written. |
| 684 | * |
| 685 | * \note Some ciphers do not use IVs nor nonce. For these |
| 686 | * ciphers, use \p iv = NULL and \p iv_len = 0. |
| 687 | * |
| 688 | * \return \c 0 on success. |
| 689 | * \return #MBEDCRYPTO_ERR_CIPHER_BAD_INPUT_DATA on |
| 690 | * parameter-verification failure. |
| 691 | * \return #MBEDCRYPTO_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption |
| 692 | * expecting a full block but not receiving one. |
| 693 | * \return #MBEDCRYPTO_ERR_CIPHER_INVALID_PADDING on invalid padding |
| 694 | * while decrypting. |
| 695 | * \return A cipher-specific error code on failure. |
| 696 | */ |
| 697 | int mbedcrypto_cipher_crypt( mbedcrypto_cipher_context_t *ctx, |
| 698 | const unsigned char *iv, size_t iv_len, |
| 699 | const unsigned char *input, size_t ilen, |
| 700 | unsigned char *output, size_t *olen ); |
| 701 | |
| 702 | #if defined(MBEDCRYPTO_CIPHER_MODE_AEAD) |
| 703 | /** |
| 704 | * \brief The generic autenticated encryption (AEAD) function. |
| 705 | * |
| 706 | * \param ctx The generic cipher context. |
| 707 | * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. |
| 708 | * \param iv_len The IV length for ciphers with variable-size IV. |
| 709 | * This parameter is discarded by ciphers with fixed-size IV. |
| 710 | * \param ad The additional data to authenticate. |
| 711 | * \param ad_len The length of \p ad. |
| 712 | * \param input The buffer holding the input data. |
| 713 | * \param ilen The length of the input data. |
| 714 | * \param output The buffer for the output data. |
| 715 | * Must be able to hold at least \p ilen. |
| 716 | * \param olen The length of the output data, to be updated with the |
| 717 | * actual number of Bytes written. |
| 718 | * \param tag The buffer for the authentication tag. |
| 719 | * \param tag_len The desired length of the authentication tag. |
| 720 | * |
| 721 | * \return \c 0 on success. |
| 722 | * \return #MBEDCRYPTO_ERR_CIPHER_BAD_INPUT_DATA on |
| 723 | * parameter-verification failure. |
| 724 | * \return A cipher-specific error code on failure. |
| 725 | */ |
| 726 | int mbedcrypto_cipher_auth_encrypt( mbedcrypto_cipher_context_t *ctx, |
| 727 | const unsigned char *iv, size_t iv_len, |
| 728 | const unsigned char *ad, size_t ad_len, |
| 729 | const unsigned char *input, size_t ilen, |
| 730 | unsigned char *output, size_t *olen, |
| 731 | unsigned char *tag, size_t tag_len ); |
| 732 | |
| 733 | /** |
| 734 | * \brief The generic autenticated decryption (AEAD) function. |
| 735 | * |
| 736 | * \note If the data is not authentic, then the output buffer |
| 737 | * is zeroed out to prevent the unauthentic plaintext being |
| 738 | * used, making this interface safer. |
| 739 | * |
| 740 | * \param ctx The generic cipher context. |
| 741 | * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. |
| 742 | * \param iv_len The IV length for ciphers with variable-size IV. |
| 743 | * This parameter is discarded by ciphers with fixed-size IV. |
| 744 | * \param ad The additional data to be authenticated. |
| 745 | * \param ad_len The length of \p ad. |
| 746 | * \param input The buffer holding the input data. |
| 747 | * \param ilen The length of the input data. |
| 748 | * \param output The buffer for the output data. |
| 749 | * Must be able to hold at least \p ilen. |
| 750 | * \param olen The length of the output data, to be updated with the |
| 751 | * actual number of Bytes written. |
| 752 | * \param tag The buffer holding the authentication tag. |
| 753 | * \param tag_len The length of the authentication tag. |
| 754 | * |
| 755 | * \return \c 0 on success. |
| 756 | * \return #MBEDCRYPTO_ERR_CIPHER_BAD_INPUT_DATA on |
| 757 | * parameter-verification failure. |
| 758 | * \return #MBEDCRYPTO_ERR_CIPHER_AUTH_FAILED if data is not authentic. |
| 759 | * \return A cipher-specific error code on failure. |
| 760 | */ |
| 761 | int mbedcrypto_cipher_auth_decrypt( mbedcrypto_cipher_context_t *ctx, |
| 762 | const unsigned char *iv, size_t iv_len, |
| 763 | const unsigned char *ad, size_t ad_len, |
| 764 | const unsigned char *input, size_t ilen, |
| 765 | unsigned char *output, size_t *olen, |
| 766 | const unsigned char *tag, size_t tag_len ); |
| 767 | #endif /* MBEDCRYPTO_CIPHER_MODE_AEAD */ |
| 768 | |
| 769 | #ifdef __cplusplus |
| 770 | } |
| 771 | #endif |
| 772 | |
| 773 | #endif /* MBEDCRYPTO_CIPHER_H */ |