Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.  * \file gcm.h
  3.  *
  4.  * \brief This file contains GCM definitions and functions.
  5.  *
  6.  * The Galois/Counter Mode (GCM) for 128-bit block ciphers is defined
  7.  * in <em>D. McGrew, J. Viega, The Galois/Counter Mode of Operation
  8.  * (GCM), Natl. Inst. Stand. Technol.</em>
  9.  *
  10.  * For more information on GCM, see <em>NIST SP 800-38D: Recommendation for
  11.  * Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC</em>.
  12.  *
  13.  */
  14. /*
  15.  *  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  16.  *  SPDX-License-Identifier: GPL-2.0
  17.  *
  18.  *  This program is free software; you can redistribute it and/or modify
  19.  *  it under the terms of the GNU General Public License as published by
  20.  *  the Free Software Foundation; either version 2 of the License, or
  21.  *  (at your option) any later version.
  22.  *
  23.  *  This program is distributed in the hope that it will be useful,
  24.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  25.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26.  *  GNU General Public License for more details.
  27.  *
  28.  *  You should have received a copy of the GNU General Public License along
  29.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  30.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  31.  *
  32.  *  This file is part of Mbed TLS (https://tls.mbed.org)
  33.  */
  34.  
  35. #ifndef MBEDTLS_GCM_H
  36. #define MBEDTLS_GCM_H
  37.  
  38. #if !defined(MBEDTLS_CONFIG_FILE)
  39. #include "config.h"
  40. #else
  41. #include MBEDTLS_CONFIG_FILE
  42. #endif
  43.  
  44. #include "cipher.h"
  45.  
  46. #include <stdint.h>
  47.  
  48. #define MBEDTLS_GCM_ENCRYPT     1
  49. #define MBEDTLS_GCM_DECRYPT     0
  50.  
  51. #define MBEDTLS_ERR_GCM_AUTH_FAILED                       -0x0012  /**< Authenticated decryption failed. */
  52.  
  53. /* MBEDTLS_ERR_GCM_HW_ACCEL_FAILED is deprecated and should not be used. */
  54. #define MBEDTLS_ERR_GCM_HW_ACCEL_FAILED                   -0x0013  /**< GCM hardware accelerator failed. */
  55.  
  56. #define MBEDTLS_ERR_GCM_BAD_INPUT                         -0x0014  /**< Bad input parameters to function. */
  57.  
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61.  
  62. #if !defined(MBEDTLS_GCM_ALT)
  63.  
  64. /**
  65.  * \brief          The GCM context structure.
  66.  */
  67. typedef struct mbedtls_gcm_context
  68. {
  69.     mbedtls_cipher_context_t cipher_ctx;  /*!< The cipher context used. */
  70.     uint64_t HL[16];                      /*!< Precalculated HTable low. */
  71.     uint64_t HH[16];                      /*!< Precalculated HTable high. */
  72.     uint64_t len;                         /*!< The total length of the encrypted data. */
  73.     uint64_t add_len;                     /*!< The total length of the additional data. */
  74.     unsigned char base_ectr[16];          /*!< The first ECTR for tag. */
  75.     unsigned char y[16];                  /*!< The Y working value. */
  76.     unsigned char buf[16];                /*!< The buf working value. */
  77.     int mode;                             /*!< The operation to perform:
  78.                                                #MBEDTLS_GCM_ENCRYPT or
  79.                                                #MBEDTLS_GCM_DECRYPT. */
  80. }
  81. mbedtls_gcm_context;
  82.  
  83. #else  /* !MBEDTLS_GCM_ALT */
  84. #include "gcm_alt.h"
  85. #endif /* !MBEDTLS_GCM_ALT */
  86.  
  87. /**
  88.  * \brief           This function initializes the specified GCM context,
  89.  *                  to make references valid, and prepares the context
  90.  *                  for mbedtls_gcm_setkey() or mbedtls_gcm_free().
  91.  *
  92.  *                  The function does not bind the GCM context to a particular
  93.  *                  cipher, nor set the key. For this purpose, use
  94.  *                  mbedtls_gcm_setkey().
  95.  *
  96.  * \param ctx       The GCM context to initialize. This must not be \c NULL.
  97.  */
  98. void mbedtls_gcm_init( mbedtls_gcm_context *ctx );
  99.  
  100. /**
  101.  * \brief           This function associates a GCM context with a
  102.  *                  cipher algorithm and a key.
  103.  *
  104.  * \param ctx       The GCM context. This must be initialized.
  105.  * \param cipher    The 128-bit block cipher to use.
  106.  * \param key       The encryption key. This must be a readable buffer of at
  107.  *                  least \p keybits bits.
  108.  * \param keybits   The key size in bits. Valid options are:
  109.  *                  <ul><li>128 bits</li>
  110.  *                  <li>192 bits</li>
  111.  *                  <li>256 bits</li></ul>
  112.  *
  113.  * \return          \c 0 on success.
  114.  * \return          A cipher-specific error code on failure.
  115.  */
  116. int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
  117.                         mbedtls_cipher_id_t cipher,
  118.                         const unsigned char *key,
  119.                         unsigned int keybits );
  120.  
  121. /**
  122.  * \brief           This function performs GCM encryption or decryption of a buffer.
  123.  *
  124.  * \note            For encryption, the output buffer can be the same as the
  125.  *                  input buffer. For decryption, the output buffer cannot be
  126.  *                  the same as input buffer. If the buffers overlap, the output
  127.  *                  buffer must trail at least 8 Bytes behind the input buffer.
  128.  *
  129.  * \warning         When this function performs a decryption, it outputs the
  130.  *                  authentication tag and does not verify that the data is
  131.  *                  authentic. You should use this function to perform encryption
  132.  *                  only. For decryption, use mbedtls_gcm_auth_decrypt() instead.
  133.  *
  134.  * \param ctx       The GCM context to use for encryption or decryption. This
  135.  *                  must be initialized.
  136.  * \param mode      The operation to perform:
  137.  *                  - #MBEDTLS_GCM_ENCRYPT to perform authenticated encryption.
  138.  *                    The ciphertext is written to \p output and the
  139.  *                    authentication tag is written to \p tag.
  140.  *                  - #MBEDTLS_GCM_DECRYPT to perform decryption.
  141.  *                    The plaintext is written to \p output and the
  142.  *                    authentication tag is written to \p tag.
  143.  *                    Note that this mode is not recommended, because it does
  144.  *                    not verify the authenticity of the data. For this reason,
  145.  *                    you should use mbedtls_gcm_auth_decrypt() instead of
  146.  *                    calling this function in decryption mode.
  147.  * \param length    The length of the input data, which is equal to the length
  148.  *                  of the output data.
  149.  * \param iv        The initialization vector. This must be a readable buffer of
  150.  *                  at least \p iv_len Bytes.
  151.  * \param iv_len    The length of the IV.
  152.  * \param add       The buffer holding the additional data. This must be of at
  153.  *                  least that size in Bytes.
  154.  * \param add_len   The length of the additional data.
  155.  * \param input     The buffer holding the input data. If \p length is greater
  156.  *                  than zero, this must be a readable buffer of at least that
  157.  *                  size in Bytes.
  158.  * \param output    The buffer for holding the output data. If \p length is greater
  159.  *                  than zero, this must be a writable buffer of at least that
  160.  *                  size in Bytes.
  161.  * \param tag_len   The length of the tag to generate.
  162.  * \param tag       The buffer for holding the tag. This must be a readable
  163.  *                  buffer of at least \p tag_len Bytes.
  164.  *
  165.  * \return          \c 0 if the encryption or decryption was performed
  166.  *                  successfully. Note that in #MBEDTLS_GCM_DECRYPT mode,
  167.  *                  this does not indicate that the data is authentic.
  168.  * \return          #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths or pointers are
  169.  *                  not valid or a cipher-specific error code if the encryption
  170.  *                  or decryption failed.
  171.  */
  172. int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
  173.                        int mode,
  174.                        size_t length,
  175.                        const unsigned char *iv,
  176.                        size_t iv_len,
  177.                        const unsigned char *add,
  178.                        size_t add_len,
  179.                        const unsigned char *input,
  180.                        unsigned char *output,
  181.                        size_t tag_len,
  182.                        unsigned char *tag );
  183.  
  184. /**
  185.  * \brief           This function performs a GCM authenticated decryption of a
  186.  *                  buffer.
  187.  *
  188.  * \note            For decryption, the output buffer cannot be the same as
  189.  *                  input buffer. If the buffers overlap, the output buffer
  190.  *                  must trail at least 8 Bytes behind the input buffer.
  191.  *
  192.  * \param ctx       The GCM context. This must be initialized.
  193.  * \param length    The length of the ciphertext to decrypt, which is also
  194.  *                  the length of the decrypted plaintext.
  195.  * \param iv        The initialization vector. This must be a readable buffer
  196.  *                  of at least \p iv_len Bytes.
  197.  * \param iv_len    The length of the IV.
  198.  * \param add       The buffer holding the additional data. This must be of at
  199.  *                  least that size in Bytes.
  200.  * \param add_len   The length of the additional data.
  201.  * \param tag       The buffer holding the tag to verify. This must be a
  202.  *                  readable buffer of at least \p tag_len Bytes.
  203.  * \param tag_len   The length of the tag to verify.
  204.  * \param input     The buffer holding the ciphertext. If \p length is greater
  205.  *                  than zero, this must be a readable buffer of at least that
  206.  *                  size.
  207.  * \param output    The buffer for holding the decrypted plaintext. If \p length
  208.  *                  is greater than zero, this must be a writable buffer of at
  209.  *                  least that size.
  210.  *
  211.  * \return          \c 0 if successful and authenticated.
  212.  * \return          #MBEDTLS_ERR_GCM_AUTH_FAILED if the tag does not match.
  213.  * \return          #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths or pointers are
  214.  *                  not valid or a cipher-specific error code if the decryption
  215.  *                  failed.
  216.  */
  217. int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
  218.                       size_t length,
  219.                       const unsigned char *iv,
  220.                       size_t iv_len,
  221.                       const unsigned char *add,
  222.                       size_t add_len,
  223.                       const unsigned char *tag,
  224.                       size_t tag_len,
  225.                       const unsigned char *input,
  226.                       unsigned char *output );
  227.  
  228. /**
  229.  * \brief           This function starts a GCM encryption or decryption
  230.  *                  operation.
  231.  *
  232.  * \param ctx       The GCM context. This must be initialized.
  233.  * \param mode      The operation to perform: #MBEDTLS_GCM_ENCRYPT or
  234.  *                  #MBEDTLS_GCM_DECRYPT.
  235.  * \param iv        The initialization vector. This must be a readable buffer of
  236.  *                  at least \p iv_len Bytes.
  237.  * \param iv_len    The length of the IV.
  238.  * \param add       The buffer holding the additional data, or \c NULL
  239.  *                  if \p add_len is \c 0.
  240.  * \param add_len   The length of the additional data. If \c 0,
  241.  *                  \p add may be \c NULL.
  242.  *
  243.  * \return          \c 0 on success.
  244.  */
  245. int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
  246.                 int mode,
  247.                 const unsigned char *iv,
  248.                 size_t iv_len,
  249.                 const unsigned char *add,
  250.                 size_t add_len );
  251.  
  252. /**
  253.  * \brief           This function feeds an input buffer into an ongoing GCM
  254.  *                  encryption or decryption operation.
  255.  *
  256.  *    `             The function expects input to be a multiple of 16
  257.  *                  Bytes. Only the last call before calling
  258.  *                  mbedtls_gcm_finish() can be less than 16 Bytes.
  259.  *
  260.  * \note            For decryption, the output buffer cannot be the same as
  261.  *                  input buffer. If the buffers overlap, the output buffer
  262.  *                  must trail at least 8 Bytes behind the input buffer.
  263.  *
  264.  * \param ctx       The GCM context. This must be initialized.
  265.  * \param length    The length of the input data. This must be a multiple of
  266.  *                  16 except in the last call before mbedtls_gcm_finish().
  267.  * \param input     The buffer holding the input data. If \p length is greater
  268.  *                  than zero, this must be a readable buffer of at least that
  269.  *                  size in Bytes.
  270.  * \param output    The buffer for holding the output data. If \p length is
  271.  *                  greater than zero, this must be a writable buffer of at
  272.  *                  least that size in Bytes.
  273.  *
  274.  * \return         \c 0 on success.
  275.  * \return         #MBEDTLS_ERR_GCM_BAD_INPUT on failure.
  276.  */
  277. int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
  278.                 size_t length,
  279.                 const unsigned char *input,
  280.                 unsigned char *output );
  281.  
  282. /**
  283.  * \brief           This function finishes the GCM operation and generates
  284.  *                  the authentication tag.
  285.  *
  286.  *                  It wraps up the GCM stream, and generates the
  287.  *                  tag. The tag can have a maximum length of 16 Bytes.
  288.  *
  289.  * \param ctx       The GCM context. This must be initialized.
  290.  * \param tag       The buffer for holding the tag. This must be a readable
  291.  *                  buffer of at least \p tag_len Bytes.
  292.  * \param tag_len   The length of the tag to generate. This must be at least
  293.  *                  four.
  294.  *
  295.  * \return          \c 0 on success.
  296.  * \return          #MBEDTLS_ERR_GCM_BAD_INPUT on failure.
  297.  */
  298. int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
  299.                 unsigned char *tag,
  300.                 size_t tag_len );
  301.  
  302. /**
  303.  * \brief           This function clears a GCM context and the underlying
  304.  *                  cipher sub-context.
  305.  *
  306.  * \param ctx       The GCM context to clear. If this is \c NULL, the call has
  307.  *                  no effect. Otherwise, this must be initialized.
  308.  */
  309. void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
  310.  
  311. #if defined(MBEDTLS_SELF_TEST)
  312.  
  313. /**
  314.  * \brief          The GCM checkup routine.
  315.  *
  316.  * \return         \c 0 on success.
  317.  * \return         \c 1 on failure.
  318.  */
  319. int mbedtls_gcm_self_test( int verbose );
  320.  
  321. #endif /* MBEDTLS_SELF_TEST */
  322.  
  323. #ifdef __cplusplus
  324. }
  325. #endif
  326.  
  327.  
  328. #endif /* gcm.h */
  329.