Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:37:32

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 "mbedtls/build_info.h"
0024 
0025 #include "mbedtls/cipher.h"
0026 
0027 #if defined(MBEDTLS_BLOCK_CIPHER_C)
0028 #include "mbedtls/block_cipher.h"
0029 #endif
0030 
0031 #include <stdint.h>
0032 
0033 #define MBEDTLS_GCM_ENCRYPT     1
0034 #define MBEDTLS_GCM_DECRYPT     0
0035 
0036 /** Authenticated decryption failed. */
0037 #define MBEDTLS_ERR_GCM_AUTH_FAILED                       -0x0012
0038 /** Bad input parameters to function. */
0039 #define MBEDTLS_ERR_GCM_BAD_INPUT                         -0x0014
0040 /** An output buffer is too small. */
0041 #define MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL                  -0x0016
0042 
0043 #ifdef __cplusplus
0044 extern "C" {
0045 #endif
0046 
0047 #if !defined(MBEDTLS_GCM_ALT)
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 #else  /* !MBEDTLS_GCM_ALT */
0078 #include "gcm_alt.h"
0079 #endif /* !MBEDTLS_GCM_ALT */
0080 
0081 /**
0082  * \brief           This function initializes the specified GCM context,
0083  *                  to make references valid, and prepares the context
0084  *                  for mbedtls_gcm_setkey() or mbedtls_gcm_free().
0085  *
0086  *                  The function does not bind the GCM context to a particular
0087  *                  cipher, nor set the key. For this purpose, use
0088  *                  mbedtls_gcm_setkey().
0089  *
0090  * \param ctx       The GCM context to initialize. This must not be \c NULL.
0091  */
0092 void mbedtls_gcm_init(mbedtls_gcm_context *ctx);
0093 
0094 /**
0095  * \brief           This function associates a GCM context with a
0096  *                  cipher algorithm and a key.
0097  *
0098  * \param ctx       The GCM context. This must be initialized.
0099  * \param cipher    The 128-bit block cipher to use.
0100  * \param key       The encryption key. This must be a readable buffer of at
0101  *                  least \p keybits bits.
0102  * \param keybits   The key size in bits. Valid options are:
0103  *                  <ul><li>128 bits</li>
0104  *                  <li>192 bits</li>
0105  *                  <li>256 bits</li></ul>
0106  *
0107  * \return          \c 0 on success.
0108  * \return          A cipher-specific error code on failure.
0109  */
0110 int mbedtls_gcm_setkey(mbedtls_gcm_context *ctx,
0111                        mbedtls_cipher_id_t cipher,
0112                        const unsigned char *key,
0113                        unsigned int keybits);
0114 
0115 /**
0116  * \brief           This function performs GCM encryption or decryption of a buffer.
0117  *
0118  * \note            For encryption, the output buffer can be the same as the
0119  *                  input buffer. For decryption, the output buffer cannot be
0120  *                  the same as input buffer. If the buffers overlap, the output
0121  *                  buffer must trail at least 8 Bytes behind the input buffer.
0122  *
0123  * \warning         When this function performs a decryption, it outputs the
0124  *                  authentication tag and does not verify that the data is
0125  *                  authentic. You should use this function to perform encryption
0126  *                  only. For decryption, use mbedtls_gcm_auth_decrypt() instead.
0127  *
0128  * \param ctx       The GCM context to use for encryption or decryption. This
0129  *                  must be initialized.
0130  * \param mode      The operation to perform:
0131  *                  - #MBEDTLS_GCM_ENCRYPT to perform authenticated encryption.
0132  *                    The ciphertext is written to \p output and the
0133  *                    authentication tag is written to \p tag.
0134  *                  - #MBEDTLS_GCM_DECRYPT to perform decryption.
0135  *                    The plaintext is written to \p output and the
0136  *                    authentication tag is written to \p tag.
0137  *                    Note that this mode is not recommended, because it does
0138  *                    not verify the authenticity of the data. For this reason,
0139  *                    you should use mbedtls_gcm_auth_decrypt() instead of
0140  *                    calling this function in decryption mode.
0141  * \param length    The length of the input data, which is equal to the length
0142  *                  of the output data.
0143  * \param iv        The initialization vector. This must be a readable buffer of
0144  *                  at least \p iv_len Bytes.
0145  * \param iv_len    The length of the IV.
0146  * \param add       The buffer holding the additional data. This must be of at
0147  *                  least that size in Bytes.
0148  * \param add_len   The length of the additional data.
0149  * \param input     The buffer holding the input data. If \p length is greater
0150  *                  than zero, this must be a readable buffer of at least that
0151  *                  size in Bytes.
0152  * \param output    The buffer for holding the output data. If \p length is greater
0153  *                  than zero, this must be a writable buffer of at least that
0154  *                  size in Bytes.
0155  * \param tag_len   The length of the tag to generate.
0156  * \param tag       The buffer for holding the tag. This must be a writable
0157  *                  buffer of at least \p tag_len Bytes.
0158  *
0159  * \return          \c 0 if the encryption or decryption was performed
0160  *                  successfully. Note that in #MBEDTLS_GCM_DECRYPT mode,
0161  *                  this does not indicate that the data is authentic.
0162  * \return          #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths or pointers are
0163  *                  not valid or a cipher-specific error code if the encryption
0164  *                  or decryption failed.
0165  */
0166 int mbedtls_gcm_crypt_and_tag(mbedtls_gcm_context *ctx,
0167                               int mode,
0168                               size_t length,
0169                               const unsigned char *iv,
0170                               size_t iv_len,
0171                               const unsigned char *add,
0172                               size_t add_len,
0173                               const unsigned char *input,
0174                               unsigned char *output,
0175                               size_t tag_len,
0176                               unsigned char *tag);
0177 
0178 /**
0179  * \brief           This function performs a GCM authenticated decryption of a
0180  *                  buffer.
0181  *
0182  * \note            For decryption, the output buffer cannot be the same as
0183  *                  input buffer. If the buffers overlap, the output buffer
0184  *                  must trail at least 8 Bytes behind the input buffer.
0185  *
0186  * \param ctx       The GCM context. This must be initialized.
0187  * \param length    The length of the ciphertext to decrypt, which is also
0188  *                  the length of the decrypted plaintext.
0189  * \param iv        The initialization vector. This must be a readable buffer
0190  *                  of at least \p iv_len Bytes.
0191  * \param iv_len    The length of the IV.
0192  * \param add       The buffer holding the additional data. This must be of at
0193  *                  least that size in Bytes.
0194  * \param add_len   The length of the additional data.
0195  * \param tag       The buffer holding the tag to verify. This must be a
0196  *                  readable buffer of at least \p tag_len Bytes.
0197  * \param tag_len   The length of the tag to verify.
0198  * \param input     The buffer holding the ciphertext. If \p length is greater
0199  *                  than zero, this must be a readable buffer of at least that
0200  *                  size.
0201  * \param output    The buffer for holding the decrypted plaintext. If \p length
0202  *                  is greater than zero, this must be a writable buffer of at
0203  *                  least that size.
0204  *
0205  * \return          \c 0 if successful and authenticated.
0206  * \return          #MBEDTLS_ERR_GCM_AUTH_FAILED if the tag does not match.
0207  * \return          #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths or pointers are
0208  *                  not valid or a cipher-specific error code if the decryption
0209  *                  failed.
0210  */
0211 int mbedtls_gcm_auth_decrypt(mbedtls_gcm_context *ctx,
0212                              size_t length,
0213                              const unsigned char *iv,
0214                              size_t iv_len,
0215                              const unsigned char *add,
0216                              size_t add_len,
0217                              const unsigned char *tag,
0218                              size_t tag_len,
0219                              const unsigned char *input,
0220                              unsigned char *output);
0221 
0222 /**
0223  * \brief           This function starts a GCM encryption or decryption
0224  *                  operation.
0225  *
0226  * \param ctx       The GCM context. This must be initialized.
0227  * \param mode      The operation to perform: #MBEDTLS_GCM_ENCRYPT or
0228  *                  #MBEDTLS_GCM_DECRYPT.
0229  * \param iv        The initialization vector. This must be a readable buffer of
0230  *                  at least \p iv_len Bytes.
0231  * \param iv_len    The length of the IV.
0232  *
0233  * \return          \c 0 on success.
0234  */
0235 int mbedtls_gcm_starts(mbedtls_gcm_context *ctx,
0236                        int mode,
0237                        const unsigned char *iv,
0238                        size_t iv_len);
0239 
0240 /**
0241  * \brief           This function feeds an input buffer as associated data
0242  *                  (authenticated but not encrypted data) in a GCM
0243  *                  encryption or decryption operation.
0244  *
0245  *                  Call this function after mbedtls_gcm_starts() to pass
0246  *                  the associated data. If the associated data is empty,
0247  *                  you do not need to call this function. You may not
0248  *                  call this function after calling mbedtls_cipher_update().
0249  *
0250  * \param ctx       The GCM context. This must have been started with
0251  *                  mbedtls_gcm_starts() and must not have yet received
0252  *                  any input with mbedtls_gcm_update().
0253  * \param add       The buffer holding the additional data, or \c NULL
0254  *                  if \p add_len is \c 0.
0255  * \param add_len   The length of the additional data. If \c 0,
0256  *                  \p add may be \c NULL.
0257  *
0258  * \return          \c 0 on success.
0259  */
0260 int mbedtls_gcm_update_ad(mbedtls_gcm_context *ctx,
0261                           const unsigned char *add,
0262                           size_t add_len);
0263 
0264 /**
0265  * \brief           This function feeds an input buffer into an ongoing GCM
0266  *                  encryption or decryption operation.
0267  *
0268  *                  You may call this function zero, one or more times
0269  *                  to pass successive parts of the input: the plaintext to
0270  *                  encrypt, or the ciphertext (not including the tag) to
0271  *                  decrypt. After the last part of the input, call
0272  *                  mbedtls_gcm_finish().
0273  *
0274  *                  This function may produce output in one of the following
0275  *                  ways:
0276  *                  - Immediate output: the output length is always equal
0277  *                    to the input length.
0278  *                  - Buffered output: the output consists of a whole number
0279  *                    of 16-byte blocks. If the total input length so far
0280  *                    (not including associated data) is 16 \* *B* + *A*
0281  *                    with *A* < 16 then the total output length is 16 \* *B*.
0282  *
0283  *                  In particular:
0284  *                  - It is always correct to call this function with
0285  *                    \p output_size >= \p input_length + 15.
0286  *                  - If \p input_length is a multiple of 16 for all the calls
0287  *                    to this function during an operation, then it is
0288  *                    correct to use \p output_size = \p input_length.
0289  *
0290  * \note            For decryption, the output buffer cannot be the same as
0291  *                  input buffer. If the buffers overlap, the output buffer
0292  *                  must trail at least 8 Bytes behind the input buffer.
0293  *
0294  * \param ctx           The GCM context. This must be initialized.
0295  * \param input         The buffer holding the input data. If \p input_length
0296  *                      is greater than zero, this must be a readable buffer
0297  *                      of at least \p input_length bytes.
0298  * \param input_length  The length of the input data in bytes.
0299  * \param output        The buffer for the output data. If \p output_size
0300  *                      is greater than zero, this must be a writable buffer of
0301  *                      of at least \p output_size bytes.
0302  * \param output_size   The size of the output buffer in bytes.
0303  *                      See the function description regarding the output size.
0304  * \param output_length On success, \p *output_length contains the actual
0305  *                      length of the output written in \p output.
0306  *                      On failure, the content of \p *output_length is
0307  *                      unspecified.
0308  *
0309  * \return         \c 0 on success.
0310  * \return         #MBEDTLS_ERR_GCM_BAD_INPUT on failure:
0311  *                 total input length too long,
0312  *                 unsupported input/output buffer overlap detected,
0313  *                 or \p output_size too small.
0314  */
0315 int mbedtls_gcm_update(mbedtls_gcm_context *ctx,
0316                        const unsigned char *input, size_t input_length,
0317                        unsigned char *output, size_t output_size,
0318                        size_t *output_length);
0319 
0320 /**
0321  * \brief           This function finishes the GCM operation and generates
0322  *                  the authentication tag.
0323  *
0324  *                  It wraps up the GCM stream, and generates the
0325  *                  tag. The tag can have a maximum length of 16 Bytes.
0326  *
0327  * \param ctx       The GCM context. This must be initialized.
0328  * \param tag       The buffer for holding the tag. This must be a writable
0329  *                  buffer of at least \p tag_len Bytes.
0330  * \param tag_len   The length of the tag to generate. This must be at least
0331  *                  four.
0332  * \param output    The buffer for the final output.
0333  *                  If \p output_size is nonzero, this must be a writable
0334  *                  buffer of at least \p output_size bytes.
0335  * \param output_size  The size of the \p output buffer in bytes.
0336  *                  This must be large enough for the output that
0337  *                  mbedtls_gcm_update() has not produced. In particular:
0338  *                  - If mbedtls_gcm_update() produces immediate output,
0339  *                    or if the total input size is a multiple of \c 16,
0340  *                    then mbedtls_gcm_finish() never produces any output,
0341  *                    so \p output_size can be \c 0.
0342  *                  - \p output_size never needs to be more than \c 15.
0343  * \param output_length On success, \p *output_length contains the actual
0344  *                      length of the output written in \p output.
0345  *                      On failure, the content of \p *output_length is
0346  *                      unspecified.
0347  *
0348  * \return          \c 0 on success.
0349  * \return          #MBEDTLS_ERR_GCM_BAD_INPUT on failure:
0350  *                  invalid value of \p tag_len,
0351  *                  or \p output_size too small.
0352  */
0353 int mbedtls_gcm_finish(mbedtls_gcm_context *ctx,
0354                        unsigned char *output, size_t output_size,
0355                        size_t *output_length,
0356                        unsigned char *tag, size_t tag_len);
0357 
0358 /**
0359  * \brief           This function clears a GCM context and the underlying
0360  *                  cipher sub-context.
0361  *
0362  * \param ctx       The GCM context to clear. If this is \c NULL, the call has
0363  *                  no effect. Otherwise, this must be initialized.
0364  */
0365 void mbedtls_gcm_free(mbedtls_gcm_context *ctx);
0366 
0367 #if defined(MBEDTLS_SELF_TEST)
0368 
0369 /**
0370  * \brief          The GCM checkup routine.
0371  *
0372  * \return         \c 0 on success.
0373  * \return         \c 1 on failure.
0374  */
0375 int mbedtls_gcm_self_test(int verbose);
0376 
0377 #endif /* MBEDTLS_SELF_TEST */
0378 
0379 #ifdef __cplusplus
0380 }
0381 #endif
0382 
0383 
0384 #endif /* gcm.h */