|
|
|||
File indexing completed on 2026-05-10 08:44:50
0001 /** 0002 * \file gcm.h 0003 * 0004 * \brief This file contains GCM definitions and functions. 0005 * 0006 * The Galois/Counter Mode (GCM) for 128-bit block ciphers is defined 0007 * in <em>D. McGrew, J. Viega, The Galois/Counter Mode of Operation 0008 * (GCM), Natl. Inst. Stand. Technol.</em> 0009 * 0010 * For more information on GCM, see <em>NIST SP 800-38D: Recommendation for 0011 * Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC</em>. 0012 * 0013 */ 0014 /* 0015 * Copyright The Mbed TLS Contributors 0016 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 0017 */ 0018 0019 #ifndef MBEDTLS_GCM_H 0020 #define MBEDTLS_GCM_H 0021 #include "mbedtls/private_access.h" 0022 0023 #include "tf-psa-crypto/build_info.h" 0024 0025 #include "mbedtls/private/cipher.h" 0026 0027 #if defined(MBEDTLS_BLOCK_CIPHER_C) 0028 #include "mbedtls/private/block_cipher.h" 0029 #endif 0030 0031 #include <stdint.h> 0032 0033 #if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS) 0034 #define MBEDTLS_GCM_ENCRYPT 1 0035 #define MBEDTLS_GCM_DECRYPT 0 0036 #endif /* MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS */ 0037 0038 /** Authenticated decryption failed. */ 0039 #define MBEDTLS_ERR_GCM_AUTH_FAILED PSA_ERROR_INVALID_SIGNATURE 0040 /** Bad input parameters to function. */ 0041 #define MBEDTLS_ERR_GCM_BAD_INPUT PSA_ERROR_INVALID_ARGUMENT 0042 /** An output buffer is too small. */ 0043 #define MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL PSA_ERROR_BUFFER_TOO_SMALL 0044 0045 #ifdef __cplusplus 0046 extern "C" { 0047 #endif 0048 0049 #if defined(MBEDTLS_GCM_LARGE_TABLE) 0050 #define MBEDTLS_GCM_HTABLE_SIZE 256 0051 #else 0052 #define MBEDTLS_GCM_HTABLE_SIZE 16 0053 #endif 0054 0055 /** 0056 * \brief The GCM context structure. 0057 */ 0058 typedef struct mbedtls_gcm_context { 0059 #if defined(MBEDTLS_BLOCK_CIPHER_C) 0060 mbedtls_block_cipher_context_t MBEDTLS_PRIVATE(block_cipher_ctx); /*!< The cipher context used. */ 0061 #else 0062 mbedtls_cipher_context_t MBEDTLS_PRIVATE(cipher_ctx); /*!< The cipher context used. */ 0063 #endif 0064 uint64_t MBEDTLS_PRIVATE(H)[MBEDTLS_GCM_HTABLE_SIZE][2]; /*!< Precalculated HTable. */ 0065 uint64_t MBEDTLS_PRIVATE(len); /*!< The total length of the encrypted data. */ 0066 uint64_t MBEDTLS_PRIVATE(add_len); /*!< The total length of the additional data. */ 0067 unsigned char MBEDTLS_PRIVATE(base_ectr)[16]; /*!< The first ECTR for tag. */ 0068 unsigned char MBEDTLS_PRIVATE(y)[16]; /*!< The Y working value. */ 0069 unsigned char MBEDTLS_PRIVATE(buf)[16]; /*!< The buf working value. */ 0070 unsigned char MBEDTLS_PRIVATE(mode); /*!< The operation to perform: 0071 MBEDTLS_GCM_ENCRYPT or 0072 MBEDTLS_GCM_DECRYPT. */ 0073 unsigned char MBEDTLS_PRIVATE(acceleration); /*!< The acceleration to use. */ 0074 } 0075 mbedtls_gcm_context; 0076 0077 #if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS) 0078 /** 0079 * \brief This function initializes the specified GCM context, 0080 * to make references valid, and prepares the context 0081 * for mbedtls_gcm_setkey() or mbedtls_gcm_free(). 0082 * 0083 * The function does not bind the GCM context to a particular 0084 * cipher, nor set the key. For this purpose, use 0085 * mbedtls_gcm_setkey(). 0086 * 0087 * \param ctx The GCM context to initialize. This must not be \c NULL. 0088 */ 0089 void mbedtls_gcm_init(mbedtls_gcm_context *ctx); 0090 0091 /** 0092 * \brief This function associates a GCM context with a 0093 * cipher algorithm and a key. 0094 * 0095 * \param ctx The GCM context. This must be initialized. 0096 * \param cipher The 128-bit block cipher to use. 0097 * \param key The encryption key. This must be a readable buffer of at 0098 * least \p keybits bits. 0099 * \param keybits The key size in bits. Valid options are: 0100 * <ul><li>128 bits</li> 0101 * <li>192 bits</li> 0102 * <li>256 bits</li></ul> 0103 * 0104 * \return \c 0 on success. 0105 * \return A cipher-specific error code on failure. 0106 */ 0107 int mbedtls_gcm_setkey(mbedtls_gcm_context *ctx, 0108 mbedtls_cipher_id_t cipher, 0109 const unsigned char *key, 0110 unsigned int keybits); 0111 0112 /** 0113 * \brief This function performs GCM encryption or decryption of a buffer. 0114 * 0115 * \note The output buffer \p output can be the same as the input 0116 * buffer \p input. If \p output is greater than \p input, they 0117 * cannot overlap. 0118 * 0119 * \warning When this function performs a decryption, it outputs the 0120 * authentication tag and does not verify that the data is 0121 * authentic. You should use this function to perform encryption 0122 * only. For decryption, use mbedtls_gcm_auth_decrypt() instead. 0123 * 0124 * \param ctx The GCM context to use for encryption or decryption. This 0125 * must be initialized. 0126 * \param mode The operation to perform: 0127 * - MBEDTLS_GCM_ENCRYPT to perform authenticated encryption. 0128 * The ciphertext is written to \p output and the 0129 * authentication tag is written to \p tag. 0130 * - MBEDTLS_GCM_DECRYPT to perform decryption. 0131 * The plaintext is written to \p output and the 0132 * authentication tag is written to \p tag. 0133 * Note that this mode is not recommended, because it does 0134 * not verify the authenticity of the data. For this reason, 0135 * you should use mbedtls_gcm_auth_decrypt() instead of 0136 * calling this function in decryption mode. 0137 * \param length The length of the input data, which is equal to the length 0138 * of the output data. 0139 * \param iv The initialization vector. This must be a readable buffer of 0140 * at least \p iv_len Bytes. 0141 * \param iv_len The length of the IV. 0142 * \param add The buffer holding the additional data. This must be of at 0143 * least that size in Bytes. 0144 * \param add_len The length of the additional data. 0145 * \param input The buffer holding the input data. If \p length is greater 0146 * than zero, this must be a readable buffer of at least that 0147 * size in Bytes. 0148 * \param output The buffer for holding the output data. If \p length is greater 0149 * than zero, this must be a writable buffer of at least that 0150 * size in Bytes. 0151 * \param tag_len The length of the tag to generate. 0152 * \param tag The buffer for holding the tag. This must be a writable 0153 * buffer of at least \p tag_len Bytes. 0154 * 0155 * \return \c 0 if the encryption or decryption was performed 0156 * successfully. Note that in MBEDTLS_GCM_DECRYPT mode, 0157 * this does not indicate that the data is authentic. 0158 * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths or pointers are 0159 * not valid or a cipher-specific error code if the encryption 0160 * or decryption failed. 0161 */ 0162 int mbedtls_gcm_crypt_and_tag(mbedtls_gcm_context *ctx, 0163 int mode, 0164 size_t length, 0165 const unsigned char *iv, 0166 size_t iv_len, 0167 const unsigned char *add, 0168 size_t add_len, 0169 const unsigned char *input, 0170 unsigned char *output, 0171 size_t tag_len, 0172 unsigned char *tag); 0173 0174 /** 0175 * \brief This function performs a GCM authenticated decryption of a 0176 * buffer. 0177 * 0178 * \note The output buffer \p output can be the same as the input 0179 * buffer \p input. If \p output is greater than \p input, they 0180 * cannot overlap. 0181 * 0182 * \param ctx The GCM context. This must be initialized. 0183 * \param length The length of the ciphertext to decrypt, which is also 0184 * the length of the decrypted plaintext. 0185 * \param iv The initialization vector. This must be a readable buffer 0186 * of at least \p iv_len Bytes. 0187 * \param iv_len The length of the IV. 0188 * \param add The buffer holding the additional data. This must be of at 0189 * least that size in Bytes. 0190 * \param add_len The length of the additional data. 0191 * \param tag The buffer holding the tag to verify. This must be a 0192 * readable buffer of at least \p tag_len Bytes. 0193 * \param tag_len The length of the tag to verify. 0194 * \param input The buffer holding the ciphertext. If \p length is greater 0195 * than zero, this must be a readable buffer of at least that 0196 * size. 0197 * \param output The buffer for holding the decrypted plaintext. If \p length 0198 * is greater than zero, this must be a writable buffer of at 0199 * least that size. 0200 * 0201 * \return \c 0 if successful and authenticated. 0202 * \return #MBEDTLS_ERR_GCM_AUTH_FAILED if the tag does not match. 0203 * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths or pointers are 0204 * not valid or a cipher-specific error code if the decryption 0205 * failed. 0206 */ 0207 int mbedtls_gcm_auth_decrypt(mbedtls_gcm_context *ctx, 0208 size_t length, 0209 const unsigned char *iv, 0210 size_t iv_len, 0211 const unsigned char *add, 0212 size_t add_len, 0213 const unsigned char *tag, 0214 size_t tag_len, 0215 const unsigned char *input, 0216 unsigned char *output); 0217 0218 /** 0219 * \brief This function starts a GCM encryption or decryption 0220 * operation. 0221 * 0222 * \param ctx The GCM context. This must be initialized. 0223 * \param mode The operation to perform: MBEDTLS_GCM_ENCRYPT or 0224 * MBEDTLS_GCM_DECRYPT. 0225 * \param iv The initialization vector. This must be a readable buffer of 0226 * at least \p iv_len Bytes. 0227 * \param iv_len The length of the IV. 0228 * 0229 * \return \c 0 on success. 0230 */ 0231 int mbedtls_gcm_starts(mbedtls_gcm_context *ctx, 0232 int mode, 0233 const unsigned char *iv, 0234 size_t iv_len); 0235 0236 /** 0237 * \brief This function feeds an input buffer as associated data 0238 * (authenticated but not encrypted data) in a GCM 0239 * encryption or decryption operation. 0240 * 0241 * Call this function after mbedtls_gcm_starts() to pass 0242 * the associated data. If the associated data is empty, 0243 * you do not need to call this function. You may not 0244 * call this function after calling mbedtls_cipher_update(). 0245 * 0246 * \param ctx The GCM context. This must have been started with 0247 * mbedtls_gcm_starts() and must not have yet received 0248 * any input with mbedtls_gcm_update(). 0249 * \param add The buffer holding the additional data, or \c NULL 0250 * if \p add_len is \c 0. 0251 * \param add_len The length of the additional data. If \c 0, 0252 * \p add may be \c NULL. 0253 * 0254 * \return \c 0 on success. 0255 */ 0256 int mbedtls_gcm_update_ad(mbedtls_gcm_context *ctx, 0257 const unsigned char *add, 0258 size_t add_len); 0259 0260 /** 0261 * \brief This function feeds an input buffer into an ongoing GCM 0262 * encryption or decryption operation. 0263 * 0264 * You may call this function zero, one or more times 0265 * to pass successive parts of the input: the plaintext to 0266 * encrypt, or the ciphertext (not including the tag) to 0267 * decrypt. After the last part of the input, call 0268 * mbedtls_gcm_finish(). 0269 * 0270 * This function may produce output in one of the following 0271 * ways: 0272 * - Immediate output: the output length is always equal 0273 * to the input length. 0274 * - Buffered output: the output consists of a whole number 0275 * of 16-byte blocks. If the total input length so far 0276 * (not including associated data) is 16 \* *B* + *A* 0277 * with *A* < 16 then the total output length is 16 \* *B*. 0278 * 0279 * In particular: 0280 * - It is always correct to call this function with 0281 * \p output_size >= \p input_length + 15. 0282 * - If \p input_length is a multiple of 16 for all the calls 0283 * to this function during an operation, then it is 0284 * correct to use \p output_size = \p input_length. 0285 * 0286 * \note The output buffer \p output can be the same as the input 0287 * buffer \p input. If \p output is greater than \p input, they 0288 * cannot overlap. 0289 * 0290 * \param ctx The GCM context. This must be initialized. 0291 * \param input The buffer holding the input data. If \p input_length 0292 * is greater than zero, this must be a readable buffer 0293 * of at least \p input_length bytes. 0294 * \param input_length The length of the input data in bytes. 0295 * \param output The buffer for the output data. If \p output_size 0296 * is greater than zero, this must be a writable buffer of 0297 * of at least \p output_size bytes. 0298 * \param output_size The size of the output buffer in bytes. 0299 * See the function description regarding the output size. 0300 * \param output_length On success, \p *output_length contains the actual 0301 * length of the output written in \p output. 0302 * On failure, the content of \p *output_length is 0303 * unspecified. 0304 * 0305 * \return \c 0 on success. 0306 * \return #MBEDTLS_ERR_GCM_BAD_INPUT on failure: 0307 * total input length too long, 0308 * unsupported input/output buffer overlap detected, 0309 * or \p output_size too small. 0310 */ 0311 int mbedtls_gcm_update(mbedtls_gcm_context *ctx, 0312 const unsigned char *input, size_t input_length, 0313 unsigned char *output, size_t output_size, 0314 size_t *output_length); 0315 0316 /** 0317 * \brief This function finishes the GCM operation and generates 0318 * the authentication tag. 0319 * 0320 * It wraps up the GCM stream, and generates the 0321 * tag. The tag can have a maximum length of 16 Bytes. 0322 * 0323 * \param ctx The GCM context. This must be initialized. 0324 * \param tag The buffer for holding the tag. This must be a writable 0325 * buffer of at least \p tag_len Bytes. 0326 * \param tag_len The length of the tag to generate. This must be at least 0327 * four. 0328 * \param output The buffer for the final output. 0329 * If \p output_size is nonzero, this must be a writable 0330 * buffer of at least \p output_size bytes. 0331 * \param output_size The size of the \p output buffer in bytes. 0332 * This must be large enough for the output that 0333 * mbedtls_gcm_update() has not produced. In particular: 0334 * - If mbedtls_gcm_update() produces immediate output, 0335 * or if the total input size is a multiple of \c 16, 0336 * then mbedtls_gcm_finish() never produces any output, 0337 * so \p output_size can be \c 0. 0338 * - \p output_size never needs to be more than \c 15. 0339 * \param output_length On success, \p *output_length contains the actual 0340 * length of the output written in \p output. 0341 * On failure, the content of \p *output_length is 0342 * unspecified. 0343 * 0344 * \return \c 0 on success. 0345 * \return #MBEDTLS_ERR_GCM_BAD_INPUT on failure: 0346 * invalid value of \p tag_len, 0347 * or \p output_size too small. 0348 */ 0349 int mbedtls_gcm_finish(mbedtls_gcm_context *ctx, 0350 unsigned char *output, size_t output_size, 0351 size_t *output_length, 0352 unsigned char *tag, size_t tag_len); 0353 0354 /** 0355 * \brief This function clears a GCM context and the underlying 0356 * cipher sub-context. 0357 * 0358 * \param ctx The GCM context to clear. If this is \c NULL, the call has 0359 * no effect. Otherwise, this must be initialized. 0360 */ 0361 void mbedtls_gcm_free(mbedtls_gcm_context *ctx); 0362 0363 #if defined(MBEDTLS_SELF_TEST) 0364 0365 /** 0366 * \brief The GCM checkup routine. 0367 * 0368 * \return \c 0 on success. 0369 * \return \c 1 on failure. 0370 */ 0371 int mbedtls_gcm_self_test(int verbose); 0372 0373 #endif /* MBEDTLS_SELF_TEST */ 0374 0375 #endif /* MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS */ 0376 0377 #ifdef __cplusplus 0378 } 0379 #endif 0380 0381 0382 #endif /* gcm.h */
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|