Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
cipher.h
00001 /** 00002 * \file cipher.h 00003 * 00004 * \brief This file contains an abstraction interface for use with the cipher 00005 * primitives provided by the library. It provides a common interface to all of 00006 * the available cipher operations. 00007 * 00008 * \author Adriaan de Jong <dejong@fox-it.com> 00009 */ 00010 /* 00011 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved 00012 * SPDX-License-Identifier: Apache-2.0 00013 * 00014 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00015 * not use this file except in compliance with the License. 00016 * You may obtain a copy of the License at 00017 * 00018 * http://www.apache.org/licenses/LICENSE-2.0 00019 * 00020 * Unless required by applicable law or agreed to in writing, software 00021 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00022 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00023 * See the License for the specific language governing permissions and 00024 * limitations under the License. 00025 * 00026 * This file is part of Mbed TLS (https://tls.mbed.org) 00027 */ 00028 00029 #ifndef MBEDTLS_CIPHER_H 00030 #define MBEDTLS_CIPHER_H 00031 00032 #if !defined(MBEDTLS_CONFIG_FILE) 00033 #include "config.h" 00034 #else 00035 #include MBEDTLS_CONFIG_FILE 00036 #endif 00037 00038 #include <stddef.h> 00039 00040 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) 00041 #define MBEDTLS_CIPHER_MODE_AEAD 00042 #endif 00043 00044 #if defined(MBEDTLS_CIPHER_MODE_CBC) 00045 #define MBEDTLS_CIPHER_MODE_WITH_PADDING 00046 #endif 00047 00048 #if defined(MBEDTLS_ARC4_C) 00049 #define MBEDTLS_CIPHER_MODE_STREAM 00050 #endif 00051 00052 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ 00053 !defined(inline) && !defined(__cplusplus) 00054 #define inline __inline 00055 #endif 00056 00057 #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */ 00058 #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters. */ 00059 #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */ 00060 #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */ 00061 #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */ 00062 #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */ 00063 #define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid. For example, because it was freed. */ 00064 #define MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED -0x6400 /**< Cipher hardware accelerator failed. */ 00065 00066 #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length. */ 00067 #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length. */ 00068 00069 #ifdef __cplusplus 00070 extern "C" { 00071 #endif 00072 00073 /** 00074 * \brief Supported cipher types. 00075 * 00076 * \warning RC4 and DES are considered weak ciphers and their use 00077 * constitutes a security risk. Arm recommends considering stronger 00078 * ciphers instead. 00079 */ 00080 typedef enum { 00081 MBEDTLS_CIPHER_ID_NONE = 0, /**< Placeholder to mark the end of cipher ID lists. */ 00082 MBEDTLS_CIPHER_ID_NULL, /**< The identity cipher, treated as a stream cipher. */ 00083 MBEDTLS_CIPHER_ID_AES, /**< The AES cipher. */ 00084 MBEDTLS_CIPHER_ID_DES, /**< The DES cipher. */ 00085 MBEDTLS_CIPHER_ID_3DES, /**< The Triple DES cipher. */ 00086 MBEDTLS_CIPHER_ID_CAMELLIA, /**< The Camellia cipher. */ 00087 MBEDTLS_CIPHER_ID_BLOWFISH, /**< The Blowfish cipher. */ 00088 MBEDTLS_CIPHER_ID_ARC4, /**< The RC4 cipher. */ 00089 MBEDTLS_CIPHER_ID_ARIA, /**< The Aria cipher. */ 00090 } mbedtls_cipher_id_t; 00091 00092 /** 00093 * \brief Supported {cipher type, cipher mode} pairs. 00094 * 00095 * \warning RC4 and DES are considered weak ciphers and their use 00096 * constitutes a security risk. Arm recommends considering stronger 00097 * ciphers instead. 00098 */ 00099 typedef enum { 00100 MBEDTLS_CIPHER_NONE = 0, /**< Placeholder to mark the end of cipher-pair lists. */ 00101 MBEDTLS_CIPHER_NULL, /**< The identity stream cipher. */ 00102 MBEDTLS_CIPHER_AES_128_ECB, /**< AES cipher with 128-bit ECB mode. */ 00103 MBEDTLS_CIPHER_AES_192_ECB, /**< AES cipher with 192-bit ECB mode. */ 00104 MBEDTLS_CIPHER_AES_256_ECB, /**< AES cipher with 256-bit ECB mode. */ 00105 MBEDTLS_CIPHER_AES_128_CBC, /**< AES cipher with 128-bit CBC mode. */ 00106 MBEDTLS_CIPHER_AES_192_CBC, /**< AES cipher with 192-bit CBC mode. */ 00107 MBEDTLS_CIPHER_AES_256_CBC, /**< AES cipher with 256-bit CBC mode. */ 00108 MBEDTLS_CIPHER_AES_128_CFB128, /**< AES cipher with 128-bit CFB128 mode. */ 00109 MBEDTLS_CIPHER_AES_192_CFB128, /**< AES cipher with 192-bit CFB128 mode. */ 00110 MBEDTLS_CIPHER_AES_256_CFB128, /**< AES cipher with 256-bit CFB128 mode. */ 00111 MBEDTLS_CIPHER_AES_128_CTR, /**< AES cipher with 128-bit CTR mode. */ 00112 MBEDTLS_CIPHER_AES_192_CTR, /**< AES cipher with 192-bit CTR mode. */ 00113 MBEDTLS_CIPHER_AES_256_CTR, /**< AES cipher with 256-bit CTR mode. */ 00114 MBEDTLS_CIPHER_AES_128_GCM, /**< AES cipher with 128-bit GCM mode. */ 00115 MBEDTLS_CIPHER_AES_192_GCM, /**< AES cipher with 192-bit GCM mode. */ 00116 MBEDTLS_CIPHER_AES_256_GCM, /**< AES cipher with 256-bit GCM mode. */ 00117 MBEDTLS_CIPHER_CAMELLIA_128_ECB, /**< Camellia cipher with 128-bit ECB mode. */ 00118 MBEDTLS_CIPHER_CAMELLIA_192_ECB, /**< Camellia cipher with 192-bit ECB mode. */ 00119 MBEDTLS_CIPHER_CAMELLIA_256_ECB, /**< Camellia cipher with 256-bit ECB mode. */ 00120 MBEDTLS_CIPHER_CAMELLIA_128_CBC, /**< Camellia cipher with 128-bit CBC mode. */ 00121 MBEDTLS_CIPHER_CAMELLIA_192_CBC, /**< Camellia cipher with 192-bit CBC mode. */ 00122 MBEDTLS_CIPHER_CAMELLIA_256_CBC, /**< Camellia cipher with 256-bit CBC mode. */ 00123 MBEDTLS_CIPHER_CAMELLIA_128_CFB128, /**< Camellia cipher with 128-bit CFB128 mode. */ 00124 MBEDTLS_CIPHER_CAMELLIA_192_CFB128, /**< Camellia cipher with 192-bit CFB128 mode. */ 00125 MBEDTLS_CIPHER_CAMELLIA_256_CFB128, /**< Camellia cipher with 256-bit CFB128 mode. */ 00126 MBEDTLS_CIPHER_CAMELLIA_128_CTR, /**< Camellia cipher with 128-bit CTR mode. */ 00127 MBEDTLS_CIPHER_CAMELLIA_192_CTR, /**< Camellia cipher with 192-bit CTR mode. */ 00128 MBEDTLS_CIPHER_CAMELLIA_256_CTR, /**< Camellia cipher with 256-bit CTR mode. */ 00129 MBEDTLS_CIPHER_CAMELLIA_128_GCM, /**< Camellia cipher with 128-bit GCM mode. */ 00130 MBEDTLS_CIPHER_CAMELLIA_192_GCM, /**< Camellia cipher with 192-bit GCM mode. */ 00131 MBEDTLS_CIPHER_CAMELLIA_256_GCM, /**< Camellia cipher with 256-bit GCM mode. */ 00132 MBEDTLS_CIPHER_DES_ECB, /**< DES cipher with ECB mode. */ 00133 MBEDTLS_CIPHER_DES_CBC, /**< DES cipher with CBC mode. */ 00134 MBEDTLS_CIPHER_DES_EDE_ECB, /**< DES cipher with EDE ECB mode. */ 00135 MBEDTLS_CIPHER_DES_EDE_CBC, /**< DES cipher with EDE CBC mode. */ 00136 MBEDTLS_CIPHER_DES_EDE3_ECB, /**< DES cipher with EDE3 ECB mode. */ 00137 MBEDTLS_CIPHER_DES_EDE3_CBC, /**< DES cipher with EDE3 CBC mode. */ 00138 MBEDTLS_CIPHER_BLOWFISH_ECB, /**< Blowfish cipher with ECB mode. */ 00139 MBEDTLS_CIPHER_BLOWFISH_CBC, /**< Blowfish cipher with CBC mode. */ 00140 MBEDTLS_CIPHER_BLOWFISH_CFB64, /**< Blowfish cipher with CFB64 mode. */ 00141 MBEDTLS_CIPHER_BLOWFISH_CTR, /**< Blowfish cipher with CTR mode. */ 00142 MBEDTLS_CIPHER_ARC4_128, /**< RC4 cipher with 128-bit mode. */ 00143 MBEDTLS_CIPHER_AES_128_CCM, /**< AES cipher with 128-bit CCM mode. */ 00144 MBEDTLS_CIPHER_AES_192_CCM, /**< AES cipher with 192-bit CCM mode. */ 00145 MBEDTLS_CIPHER_AES_256_CCM, /**< AES cipher with 256-bit CCM mode. */ 00146 MBEDTLS_CIPHER_CAMELLIA_128_CCM, /**< Camellia cipher with 128-bit CCM mode. */ 00147 MBEDTLS_CIPHER_CAMELLIA_192_CCM, /**< Camellia cipher with 192-bit CCM mode. */ 00148 MBEDTLS_CIPHER_CAMELLIA_256_CCM, /**< Camellia cipher with 256-bit CCM mode. */ 00149 MBEDTLS_CIPHER_ARIA_128_ECB, /**< Aria cipher with 128-bit key and ECB mode. */ 00150 MBEDTLS_CIPHER_ARIA_192_ECB, /**< Aria cipher with 192-bit key and ECB mode. */ 00151 MBEDTLS_CIPHER_ARIA_256_ECB, /**< Aria cipher with 256-bit key and ECB mode. */ 00152 MBEDTLS_CIPHER_ARIA_128_CBC, /**< Aria cipher with 128-bit key and CBC mode. */ 00153 MBEDTLS_CIPHER_ARIA_192_CBC, /**< Aria cipher with 192-bit key and CBC mode. */ 00154 MBEDTLS_CIPHER_ARIA_256_CBC, /**< Aria cipher with 256-bit key and CBC mode. */ 00155 MBEDTLS_CIPHER_ARIA_128_CFB128, /**< Aria cipher with 128-bit key and CFB-128 mode. */ 00156 MBEDTLS_CIPHER_ARIA_192_CFB128, /**< Aria cipher with 192-bit key and CFB-128 mode. */ 00157 MBEDTLS_CIPHER_ARIA_256_CFB128, /**< Aria cipher with 256-bit key and CFB-128 mode. */ 00158 MBEDTLS_CIPHER_ARIA_128_CTR, /**< Aria cipher with 128-bit key and CTR mode. */ 00159 MBEDTLS_CIPHER_ARIA_192_CTR, /**< Aria cipher with 192-bit key and CTR mode. */ 00160 MBEDTLS_CIPHER_ARIA_256_CTR, /**< Aria cipher with 256-bit key and CTR mode. */ 00161 MBEDTLS_CIPHER_ARIA_128_GCM, /**< Aria cipher with 128-bit key and GCM mode. */ 00162 MBEDTLS_CIPHER_ARIA_192_GCM, /**< Aria cipher with 192-bit key and GCM mode. */ 00163 MBEDTLS_CIPHER_ARIA_256_GCM, /**< Aria cipher with 256-bit key and GCM mode. */ 00164 MBEDTLS_CIPHER_ARIA_128_CCM, /**< Aria cipher with 128-bit key and CCM mode. */ 00165 MBEDTLS_CIPHER_ARIA_192_CCM, /**< Aria cipher with 192-bit key and CCM mode. */ 00166 MBEDTLS_CIPHER_ARIA_256_CCM, /**< Aria cipher with 256-bit key and CCM mode. */ 00167 } mbedtls_cipher_type_t; 00168 00169 /** Supported cipher modes. */ 00170 typedef enum { 00171 MBEDTLS_MODE_NONE = 0, /**< None. */ 00172 MBEDTLS_MODE_ECB, /**< The ECB cipher mode. */ 00173 MBEDTLS_MODE_CBC, /**< The CBC cipher mode. */ 00174 MBEDTLS_MODE_CFB, /**< The CFB cipher mode. */ 00175 MBEDTLS_MODE_OFB, /**< The OFB cipher mode - unsupported. */ 00176 MBEDTLS_MODE_CTR, /**< The CTR cipher mode. */ 00177 MBEDTLS_MODE_GCM, /**< The GCM cipher mode. */ 00178 MBEDTLS_MODE_STREAM, /**< The stream cipher mode. */ 00179 MBEDTLS_MODE_CCM, /**< The CCM cipher mode. */ 00180 } mbedtls_cipher_mode_t; 00181 00182 /** Supported cipher padding types. */ 00183 typedef enum { 00184 MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default). */ 00185 MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding. */ 00186 MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding. */ 00187 MBEDTLS_PADDING_ZEROS, /**< Zero padding (not reversible). */ 00188 MBEDTLS_PADDING_NONE, /**< Never pad (full blocks only). */ 00189 } mbedtls_cipher_padding_t; 00190 00191 /** Type of operation. */ 00192 typedef enum { 00193 MBEDTLS_OPERATION_NONE = -1, 00194 MBEDTLS_DECRYPT = 0, 00195 MBEDTLS_ENCRYPT, 00196 } mbedtls_operation_t; 00197 00198 enum { 00199 /** Undefined key length. */ 00200 MBEDTLS_KEY_LENGTH_NONE = 0, 00201 /** Key length, in bits (including parity), for DES keys. */ 00202 MBEDTLS_KEY_LENGTH_DES = 64, 00203 /** Key length in bits, including parity, for DES in two-key EDE. */ 00204 MBEDTLS_KEY_LENGTH_DES_EDE = 128, 00205 /** Key length in bits, including parity, for DES in three-key EDE. */ 00206 MBEDTLS_KEY_LENGTH_DES_EDE3 = 192, 00207 }; 00208 00209 /** Maximum length of any IV, in Bytes. */ 00210 #define MBEDTLS_MAX_IV_LENGTH 16 00211 /** Maximum block size of any cipher, in Bytes. */ 00212 #define MBEDTLS_MAX_BLOCK_LENGTH 16 00213 00214 /** 00215 * Base cipher information (opaque struct). 00216 */ 00217 typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t; 00218 00219 /** 00220 * CMAC context (opaque struct). 00221 */ 00222 typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t; 00223 00224 /** 00225 * Cipher information. Allows calling cipher functions 00226 * in a generic way. 00227 */ 00228 typedef struct { 00229 /** Full cipher identifier. For example, 00230 * MBEDTLS_CIPHER_AES_256_CBC. 00231 */ 00232 mbedtls_cipher_type_t type; 00233 00234 /** The cipher mode. For example, MBEDTLS_MODE_CBC. */ 00235 mbedtls_cipher_mode_t mode; 00236 00237 /** The cipher key length, in bits. This is the 00238 * default length for variable sized ciphers. 00239 * Includes parity bits for ciphers like DES. 00240 */ 00241 unsigned int key_bitlen; 00242 00243 /** Name of the cipher. */ 00244 const char * name; 00245 00246 /** IV or nonce size, in Bytes. 00247 * For ciphers that accept variable IV sizes, 00248 * this is the recommended size. 00249 */ 00250 unsigned int iv_size; 00251 00252 /** Bitflag comprised of MBEDTLS_CIPHER_VARIABLE_IV_LEN and 00253 * MBEDTLS_CIPHER_VARIABLE_KEY_LEN indicating whether the 00254 * cipher supports variable IV or variable key sizes, respectively. 00255 */ 00256 int flags; 00257 00258 /** The block size, in Bytes. */ 00259 unsigned int block_size; 00260 00261 /** Struct for base cipher information and functions. */ 00262 const mbedtls_cipher_base_t *base; 00263 00264 } mbedtls_cipher_info_t; 00265 00266 /** 00267 * Generic cipher context. 00268 */ 00269 typedef struct { 00270 /** Information about the associated cipher. */ 00271 const mbedtls_cipher_info_t *cipher_info; 00272 00273 /** Key length to use. */ 00274 int key_bitlen; 00275 00276 /** Operation that the key of the context has been 00277 * initialized for. 00278 */ 00279 mbedtls_operation_t operation; 00280 00281 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) 00282 /** Padding functions to use, if relevant for 00283 * the specific cipher mode. 00284 */ 00285 void (*add_padding)( unsigned char *output, size_t olen, size_t data_len ); 00286 int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len ); 00287 #endif 00288 00289 /** Buffer for input that has not been processed yet. */ 00290 unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH]; 00291 00292 /** Number of Bytes that have not been processed yet. */ 00293 size_t unprocessed_len; 00294 00295 /** Current IV or NONCE_COUNTER for CTR-mode. */ 00296 unsigned char iv[MBEDTLS_MAX_IV_LENGTH]; 00297 00298 /** IV size in Bytes, for ciphers with variable-length IVs. */ 00299 size_t iv_size; 00300 00301 /** The cipher-specific context. */ 00302 void *cipher_ctx; 00303 00304 #if defined(MBEDTLS_CMAC_C) 00305 /** CMAC-specific context. */ 00306 mbedtls_cmac_context_t *cmac_ctx; 00307 #endif 00308 } mbedtls_cipher_context_t; 00309 00310 /** 00311 * \brief This function retrieves the list of ciphers supported by the generic 00312 * cipher module. 00313 * 00314 * \return A statically-allocated array of ciphers. The last entry 00315 * is zero. 00316 */ 00317 const int *mbedtls_cipher_list( void ); 00318 00319 /** 00320 * \brief This function retrieves the cipher-information 00321 * structure associated with the given cipher name. 00322 * 00323 * \param cipher_name Name of the cipher to search for. 00324 * 00325 * \return The cipher information structure associated with the 00326 * given \p cipher_name. 00327 * \return NULL if the associated cipher information is not found. 00328 */ 00329 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name ); 00330 00331 /** 00332 * \brief This function retrieves the cipher-information 00333 * structure associated with the given cipher type. 00334 * 00335 * \param cipher_type Type of the cipher to search for. 00336 * 00337 * \return The cipher information structure associated with the 00338 * given \p cipher_type. 00339 * \return NULL if the associated cipher information is not found. 00340 */ 00341 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type ); 00342 00343 /** 00344 * \brief This function retrieves the cipher-information 00345 * structure associated with the given cipher ID, 00346 * key size and mode. 00347 * 00348 * \param cipher_id The ID of the cipher to search for. For example, 00349 * #MBEDTLS_CIPHER_ID_AES. 00350 * \param key_bitlen The length of the key in bits. 00351 * \param mode The cipher mode. For example, #MBEDTLS_MODE_CBC. 00352 * 00353 * \return The cipher information structure associated with the 00354 * given \p cipher_id. 00355 * \return NULL if the associated cipher information is not found. 00356 */ 00357 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id, 00358 int key_bitlen, 00359 const mbedtls_cipher_mode_t mode ); 00360 00361 /** 00362 * \brief This function initializes a \p cipher_context as NONE. 00363 */ 00364 void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx ); 00365 00366 /** 00367 * \brief This function frees and clears the cipher-specific 00368 * context of \p ctx. Freeing \p ctx itself remains the 00369 * responsibility of the caller. 00370 */ 00371 void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx ); 00372 00373 00374 /** 00375 * \brief This function initializes and fills the cipher-context 00376 * structure with the appropriate values. It also clears 00377 * the structure. 00378 * 00379 * \param ctx The context to initialize. May not be NULL. 00380 * \param cipher_info The cipher to use. 00381 * 00382 * \return \c 0 on success. 00383 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on 00384 * parameter-verification failure. 00385 * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the 00386 * cipher-specific context fails. 00387 * 00388 * \internal Currently, the function also clears the structure. 00389 * In future versions, the caller will be required to call 00390 * mbedtls_cipher_init() on the structure first. 00391 */ 00392 int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info ); 00393 00394 /** 00395 * \brief This function returns the block size of the given cipher. 00396 * 00397 * \param ctx The context of the cipher. Must be initialized. 00398 * 00399 * \return The size of the blocks of the cipher. 00400 * \return 0 if \p ctx has not been initialized. 00401 */ 00402 static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx ) 00403 { 00404 if( NULL == ctx || NULL == ctx->cipher_info ) 00405 return 0; 00406 00407 return ctx->cipher_info->block_size; 00408 } 00409 00410 /** 00411 * \brief This function returns the mode of operation for 00412 * the cipher. For example, MBEDTLS_MODE_CBC. 00413 * 00414 * \param ctx The context of the cipher. Must be initialized. 00415 * 00416 * \return The mode of operation. 00417 * \return #MBEDTLS_MODE_NONE if \p ctx has not been initialized. 00418 */ 00419 static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( const mbedtls_cipher_context_t *ctx ) 00420 { 00421 if( NULL == ctx || NULL == ctx->cipher_info ) 00422 return MBEDTLS_MODE_NONE; 00423 00424 return ctx->cipher_info->mode; 00425 } 00426 00427 /** 00428 * \brief This function returns the size of the IV or nonce 00429 * of the cipher, in Bytes. 00430 * 00431 * \param ctx The context of the cipher. Must be initialized. 00432 * 00433 * \return The recommended IV size if no IV has been set. 00434 * \return \c 0 for ciphers not using an IV or a nonce. 00435 * \return The actual size if an IV has been set. 00436 */ 00437 static inline int mbedtls_cipher_get_iv_size( const mbedtls_cipher_context_t *ctx ) 00438 { 00439 if( NULL == ctx || NULL == ctx->cipher_info ) 00440 return 0; 00441 00442 if( ctx->iv_size != 0 ) 00443 return (int) ctx->iv_size; 00444 00445 return (int) ctx->cipher_info->iv_size; 00446 } 00447 00448 /** 00449 * \brief This function returns the type of the given cipher. 00450 * 00451 * \param ctx The context of the cipher. Must be initialized. 00452 * 00453 * \return The type of the cipher. 00454 * \return #MBEDTLS_CIPHER_NONE if \p ctx has not been initialized. 00455 */ 00456 static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( const mbedtls_cipher_context_t *ctx ) 00457 { 00458 if( NULL == ctx || NULL == ctx->cipher_info ) 00459 return MBEDTLS_CIPHER_NONE; 00460 00461 return ctx->cipher_info->type; 00462 } 00463 00464 /** 00465 * \brief This function returns the name of the given cipher 00466 * as a string. 00467 * 00468 * \param ctx The context of the cipher. Must be initialized. 00469 * 00470 * \return The name of the cipher. 00471 * \return NULL if \p ctx has not been not initialized. 00472 */ 00473 static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx ) 00474 { 00475 if( NULL == ctx || NULL == ctx->cipher_info ) 00476 return 0; 00477 00478 return ctx->cipher_info->name; 00479 } 00480 00481 /** 00482 * \brief This function returns the key length of the cipher. 00483 * 00484 * \param ctx The context of the cipher. Must be initialized. 00485 * 00486 * \return The key length of the cipher in bits. 00487 * \return #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been 00488 * initialized. 00489 */ 00490 static inline int mbedtls_cipher_get_key_bitlen( const mbedtls_cipher_context_t *ctx ) 00491 { 00492 if( NULL == ctx || NULL == ctx->cipher_info ) 00493 return MBEDTLS_KEY_LENGTH_NONE; 00494 00495 return (int) ctx->cipher_info->key_bitlen; 00496 } 00497 00498 /** 00499 * \brief This function returns the operation of the given cipher. 00500 * 00501 * \param ctx The context of the cipher. Must be initialized. 00502 * 00503 * \return The type of operation: #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT. 00504 * \return #MBEDTLS_OPERATION_NONE if \p ctx has not been initialized. 00505 */ 00506 static inline mbedtls_operation_t mbedtls_cipher_get_operation( const mbedtls_cipher_context_t *ctx ) 00507 { 00508 if( NULL == ctx || NULL == ctx->cipher_info ) 00509 return MBEDTLS_OPERATION_NONE; 00510 00511 return ctx->operation; 00512 } 00513 00514 /** 00515 * \brief This function sets the key to use with the given context. 00516 * 00517 * \param ctx The generic cipher context. May not be NULL. Must have 00518 * been initialized using mbedtls_cipher_info_from_type() 00519 * or mbedtls_cipher_info_from_string(). 00520 * \param key The key to use. 00521 * \param key_bitlen The key length to use, in bits. 00522 * \param operation The operation that the key will be used for: 00523 * #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT. 00524 * 00525 * \return \c 0 on success. 00526 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on 00527 * parameter-verification failure. 00528 * \return A cipher-specific error code on failure. 00529 */ 00530 int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key, 00531 int key_bitlen, const mbedtls_operation_t operation ); 00532 00533 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) 00534 /** 00535 * \brief This function sets the padding mode, for cipher modes 00536 * that use padding. 00537 * 00538 * The default passing mode is PKCS7 padding. 00539 * 00540 * \param ctx The generic cipher context. 00541 * \param mode The padding mode. 00542 * 00543 * \return \c 0 on success. 00544 * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE 00545 * if the selected padding mode is not supported. 00546 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode 00547 * does not support padding. 00548 */ 00549 int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode ); 00550 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ 00551 00552 /** 00553 * \brief This function sets the initialization vector (IV) 00554 * or nonce. 00555 * 00556 * \note Some ciphers do not use IVs nor nonce. For these 00557 * ciphers, this function has no effect. 00558 * 00559 * \param ctx The generic cipher context. 00560 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. 00561 * \param iv_len The IV length for ciphers with variable-size IV. 00562 * This parameter is discarded by ciphers with fixed-size IV. 00563 * 00564 * \return \c 0 on success. 00565 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on 00566 * parameter-verification failure. 00567 */ 00568 int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx, 00569 const unsigned char *iv, size_t iv_len ); 00570 00571 /** 00572 * \brief This function resets the cipher state. 00573 * 00574 * \param ctx The generic cipher context. 00575 * 00576 * \return \c 0 on success. 00577 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on 00578 * parameter-verification failure. 00579 */ 00580 int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx ); 00581 00582 #if defined(MBEDTLS_GCM_C) 00583 /** 00584 * \brief This function adds additional data for AEAD ciphers. 00585 * Only supported with GCM. Must be called 00586 * exactly once, after mbedtls_cipher_reset(). 00587 * 00588 * \param ctx The generic cipher context. 00589 * \param ad The additional data to use. 00590 * \param ad_len the Length of \p ad. 00591 * 00592 * \return \c 0 on success. 00593 * \return A specific error code on failure. 00594 */ 00595 int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx, 00596 const unsigned char *ad, size_t ad_len ); 00597 #endif /* MBEDTLS_GCM_C */ 00598 00599 /** 00600 * \brief The generic cipher update function. It encrypts or 00601 * decrypts using the given cipher context. Writes as 00602 * many block-sized blocks of data as possible to output. 00603 * Any data that cannot be written immediately is either 00604 * added to the next block, or flushed when 00605 * mbedtls_cipher_finish() is called. 00606 * Exception: For MBEDTLS_MODE_ECB, expects a single block 00607 * in size. For example, 16 Bytes for AES. 00608 * 00609 * \note If the underlying cipher is used in GCM mode, all calls 00610 * to this function, except for the last one before 00611 * mbedtls_cipher_finish(), must have \p ilen as a 00612 * multiple of the block size of the cipher. 00613 * 00614 * \param ctx The generic cipher context. 00615 * \param input The buffer holding the input data. 00616 * \param ilen The length of the input data. 00617 * \param output The buffer for the output data. Must be able to hold at 00618 * least \p ilen + block_size. Must not be the same buffer 00619 * as input. 00620 * \param olen The length of the output data, to be updated with the 00621 * actual number of Bytes written. 00622 * 00623 * \return \c 0 on success. 00624 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on 00625 * parameter-verification failure. 00626 * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an 00627 * unsupported mode for a cipher. 00628 * \return A cipher-specific error code on failure. 00629 */ 00630 int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input, 00631 size_t ilen, unsigned char *output, size_t *olen ); 00632 00633 /** 00634 * \brief The generic cipher finalization function. If data still 00635 * needs to be flushed from an incomplete block, the data 00636 * contained in it is padded to the size of 00637 * the last block, and written to the \p output buffer. 00638 * 00639 * \param ctx The generic cipher context. 00640 * \param output The buffer to write data to. Needs block_size available. 00641 * \param olen The length of the data written to the \p output buffer. 00642 * 00643 * \return \c 0 on success. 00644 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on 00645 * parameter-verification failure. 00646 * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption 00647 * expecting a full block but not receiving one. 00648 * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding 00649 * while decrypting. 00650 * \return A cipher-specific error code on failure. 00651 */ 00652 int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx, 00653 unsigned char *output, size_t *olen ); 00654 00655 #if defined(MBEDTLS_GCM_C) 00656 /** 00657 * \brief This function writes a tag for AEAD ciphers. 00658 * Only supported with GCM. 00659 * Must be called after mbedtls_cipher_finish(). 00660 * 00661 * \param ctx The generic cipher context. 00662 * \param tag The buffer to write the tag to. 00663 * \param tag_len The length of the tag to write. 00664 * 00665 * \return \c 0 on success. 00666 * \return A specific error code on failure. 00667 */ 00668 int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx, 00669 unsigned char *tag, size_t tag_len ); 00670 00671 /** 00672 * \brief This function checks the tag for AEAD ciphers. 00673 * Only supported with GCM. 00674 * Must be called after mbedtls_cipher_finish(). 00675 * 00676 * \param ctx The generic cipher context. 00677 * \param tag The buffer holding the tag. 00678 * \param tag_len The length of the tag to check. 00679 * 00680 * \return \c 0 on success. 00681 * \return A specific error code on failure. 00682 */ 00683 int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx, 00684 const unsigned char *tag, size_t tag_len ); 00685 #endif /* MBEDTLS_GCM_C */ 00686 00687 /** 00688 * \brief The generic all-in-one encryption/decryption function, 00689 * for all ciphers except AEAD constructs. 00690 * 00691 * \param ctx The generic cipher context. 00692 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. 00693 * \param iv_len The IV length for ciphers with variable-size IV. 00694 * This parameter is discarded by ciphers with fixed-size 00695 * IV. 00696 * \param input The buffer holding the input data. 00697 * \param ilen The length of the input data. 00698 * \param output The buffer for the output data. Must be able to hold at 00699 * least \p ilen + block_size. Must not be the same buffer 00700 * as input. 00701 * \param olen The length of the output data, to be updated with the 00702 * actual number of Bytes written. 00703 * 00704 * \note Some ciphers do not use IVs nor nonce. For these 00705 * ciphers, use \p iv = NULL and \p iv_len = 0. 00706 * 00707 * \return \c 0 on success. 00708 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on 00709 * parameter-verification failure. 00710 * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption 00711 * expecting a full block but not receiving one. 00712 * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding 00713 * while decrypting. 00714 * \return A cipher-specific error code on failure. 00715 */ 00716 int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx, 00717 const unsigned char *iv, size_t iv_len, 00718 const unsigned char *input, size_t ilen, 00719 unsigned char *output, size_t *olen ); 00720 00721 #if defined(MBEDTLS_CIPHER_MODE_AEAD) 00722 /** 00723 * \brief The generic autenticated encryption (AEAD) function. 00724 * 00725 * \param ctx The generic cipher context. 00726 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. 00727 * \param iv_len The IV length for ciphers with variable-size IV. 00728 * This parameter is discarded by ciphers with fixed-size IV. 00729 * \param ad The additional data to authenticate. 00730 * \param ad_len The length of \p ad. 00731 * \param input The buffer holding the input data. 00732 * \param ilen The length of the input data. 00733 * \param output The buffer for the output data. 00734 * Must be able to hold at least \p ilen. 00735 * \param olen The length of the output data, to be updated with the 00736 * actual number of Bytes written. 00737 * \param tag The buffer for the authentication tag. 00738 * \param tag_len The desired length of the authentication tag. 00739 * 00740 * \return \c 0 on success. 00741 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on 00742 * parameter-verification failure. 00743 * \return A cipher-specific error code on failure. 00744 */ 00745 int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx, 00746 const unsigned char *iv, size_t iv_len, 00747 const unsigned char *ad, size_t ad_len, 00748 const unsigned char *input, size_t ilen, 00749 unsigned char *output, size_t *olen, 00750 unsigned char *tag, size_t tag_len ); 00751 00752 /** 00753 * \brief The generic autenticated decryption (AEAD) function. 00754 * 00755 * \note If the data is not authentic, then the output buffer 00756 * is zeroed out to prevent the unauthentic plaintext being 00757 * used, making this interface safer. 00758 * 00759 * \param ctx The generic cipher context. 00760 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. 00761 * \param iv_len The IV length for ciphers with variable-size IV. 00762 * This parameter is discarded by ciphers with fixed-size IV. 00763 * \param ad The additional data to be authenticated. 00764 * \param ad_len The length of \p ad. 00765 * \param input The buffer holding the input data. 00766 * \param ilen The length of the input data. 00767 * \param output The buffer for the output data. 00768 * Must be able to hold at least \p ilen. 00769 * \param olen The length of the output data, to be updated with the 00770 * actual number of Bytes written. 00771 * \param tag The buffer holding the authentication tag. 00772 * \param tag_len The length of the authentication tag. 00773 * 00774 * \return \c 0 on success. 00775 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on 00776 * parameter-verification failure. 00777 * \return #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic. 00778 * \return A cipher-specific error code on failure. 00779 */ 00780 int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx, 00781 const unsigned char *iv, size_t iv_len, 00782 const unsigned char *ad, size_t ad_len, 00783 const unsigned char *input, size_t ilen, 00784 unsigned char *output, size_t *olen, 00785 const unsigned char *tag, size_t tag_len ); 00786 #endif /* MBEDTLS_CIPHER_MODE_AEAD */ 00787 00788 #ifdef __cplusplus 00789 } 00790 #endif 00791 00792 #endif /* MBEDTLS_CIPHER_H */
Generated on Tue Jul 12 2022 12:43:39 by
1.7.2