Subversion Repositories Kolibri OS

Rev

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

  1. /**
  2.  * \file cmac.h
  3.  *
  4.  * \brief This file contains CMAC definitions and functions.
  5.  *
  6.  * The Cipher-based Message Authentication Code (CMAC) Mode for
  7.  * Authentication is defined in <em>RFC-4493: The AES-CMAC Algorithm</em>.
  8.  */
  9. /*
  10.  *  Copyright (C) 2015-2018, Arm Limited (or its affiliates), All Rights Reserved
  11.  *  SPDX-License-Identifier: GPL-2.0
  12.  *
  13.  *  This program is free software; you can redistribute it and/or modify
  14.  *  it under the terms of the GNU General Public License as published by
  15.  *  the Free Software Foundation; either version 2 of the License, or
  16.  *  (at your option) any later version.
  17.  *
  18.  *  This program is distributed in the hope that it will be useful,
  19.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  *  GNU General Public License for more details.
  22.  *
  23.  *  You should have received a copy of the GNU General Public License along
  24.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  25.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26.  *
  27.  *  This file is part of Mbed TLS (https://tls.mbed.org)
  28.  */
  29.  
  30. #ifndef MBEDTLS_CMAC_H
  31. #define MBEDTLS_CMAC_H
  32.  
  33. #if !defined(MBEDTLS_CONFIG_FILE)
  34. #include "config.h"
  35. #else
  36. #include MBEDTLS_CONFIG_FILE
  37. #endif
  38.  
  39. #include "cipher.h"
  40.  
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44.  
  45. /* MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED is deprecated and should not be used. */
  46. #define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A  /**< CMAC hardware accelerator failed. */
  47.  
  48. #define MBEDTLS_AES_BLOCK_SIZE          16
  49. #define MBEDTLS_DES3_BLOCK_SIZE         8
  50.  
  51. #if defined(MBEDTLS_AES_C)
  52. #define MBEDTLS_CIPHER_BLKSIZE_MAX      16  /**< The longest block used by CMAC is that of AES. */
  53. #else
  54. #define MBEDTLS_CIPHER_BLKSIZE_MAX      8   /**< The longest block used by CMAC is that of 3DES. */
  55. #endif
  56.  
  57. #if !defined(MBEDTLS_CMAC_ALT)
  58.  
  59. /**
  60.  * The CMAC context structure.
  61.  */
  62. struct mbedtls_cmac_context_t
  63. {
  64.     /** The internal state of the CMAC algorithm.  */
  65.     unsigned char       state[MBEDTLS_CIPHER_BLKSIZE_MAX];
  66.  
  67.     /** Unprocessed data - either data that was not block aligned and is still
  68.      *  pending processing, or the final block. */
  69.     unsigned char       unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
  70.  
  71.     /** The length of data pending processing. */
  72.     size_t              unprocessed_len;
  73. };
  74.  
  75. #else  /* !MBEDTLS_CMAC_ALT */
  76. #include "cmac_alt.h"
  77. #endif /* !MBEDTLS_CMAC_ALT */
  78.  
  79. /**
  80.  * \brief               This function sets the CMAC key, and prepares to authenticate
  81.  *                      the input data.
  82.  *                      Must be called with an initialized cipher context.
  83.  *
  84.  * \param ctx           The cipher context used for the CMAC operation, initialized
  85.  *                      as one of the following types: MBEDTLS_CIPHER_AES_128_ECB,
  86.  *                      MBEDTLS_CIPHER_AES_192_ECB, MBEDTLS_CIPHER_AES_256_ECB,
  87.  *                      or MBEDTLS_CIPHER_DES_EDE3_ECB.
  88.  * \param key           The CMAC key.
  89.  * \param keybits       The length of the CMAC key in bits.
  90.  *                      Must be supported by the cipher.
  91.  *
  92.  * \return              \c 0 on success.
  93.  * \return              A cipher-specific error code on failure.
  94.  */
  95. int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
  96.                                 const unsigned char *key, size_t keybits );
  97.  
  98. /**
  99.  * \brief               This function feeds an input buffer into an ongoing CMAC
  100.  *                      computation.
  101.  *
  102.  *                      It is called between mbedtls_cipher_cmac_starts() or
  103.  *                      mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish().
  104.  *                      Can be called repeatedly.
  105.  *
  106.  * \param ctx           The cipher context used for the CMAC operation.
  107.  * \param input         The buffer holding the input data.
  108.  * \param ilen          The length of the input data.
  109.  *
  110.  * \return             \c 0 on success.
  111.  * \return             #MBEDTLS_ERR_MD_BAD_INPUT_DATA
  112.  *                     if parameter verification fails.
  113.  */
  114. int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
  115.                                 const unsigned char *input, size_t ilen );
  116.  
  117. /**
  118.  * \brief               This function finishes the CMAC operation, and writes
  119.  *                      the result to the output buffer.
  120.  *
  121.  *                      It is called after mbedtls_cipher_cmac_update().
  122.  *                      It can be followed by mbedtls_cipher_cmac_reset() and
  123.  *                      mbedtls_cipher_cmac_update(), or mbedtls_cipher_free().
  124.  *
  125.  * \param ctx           The cipher context used for the CMAC operation.
  126.  * \param output        The output buffer for the CMAC checksum result.
  127.  *
  128.  * \return              \c 0 on success.
  129.  * \return              #MBEDTLS_ERR_MD_BAD_INPUT_DATA
  130.  *                      if parameter verification fails.
  131.  */
  132. int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
  133.                                 unsigned char *output );
  134.  
  135. /**
  136.  * \brief               This function prepares the authentication of another
  137.  *                      message with the same key as the previous CMAC
  138.  *                      operation.
  139.  *
  140.  *                      It is called after mbedtls_cipher_cmac_finish()
  141.  *                      and before mbedtls_cipher_cmac_update().
  142.  *
  143.  * \param ctx           The cipher context used for the CMAC operation.
  144.  *
  145.  * \return              \c 0 on success.
  146.  * \return              #MBEDTLS_ERR_MD_BAD_INPUT_DATA
  147.  *                      if parameter verification fails.
  148.  */
  149. int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
  150.  
  151. /**
  152.  * \brief               This function calculates the full generic CMAC
  153.  *                      on the input buffer with the provided key.
  154.  *
  155.  *                      The function allocates the context, performs the
  156.  *                      calculation, and frees the context.
  157.  *
  158.  *                      The CMAC result is calculated as
  159.  *                      output = generic CMAC(cmac key, input buffer).
  160.  *
  161.  *
  162.  * \param cipher_info   The cipher information.
  163.  * \param key           The CMAC key.
  164.  * \param keylen        The length of the CMAC key in bits.
  165.  * \param input         The buffer holding the input data.
  166.  * \param ilen          The length of the input data.
  167.  * \param output        The buffer for the generic CMAC result.
  168.  *
  169.  * \return              \c 0 on success.
  170.  * \return              #MBEDTLS_ERR_MD_BAD_INPUT_DATA
  171.  *                      if parameter verification fails.
  172.  */
  173. int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
  174.                          const unsigned char *key, size_t keylen,
  175.                          const unsigned char *input, size_t ilen,
  176.                          unsigned char *output );
  177.  
  178. #if defined(MBEDTLS_AES_C)
  179. /**
  180.  * \brief           This function implements the AES-CMAC-PRF-128 pseudorandom
  181.  *                  function, as defined in
  182.  *                  <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
  183.  *                  Message Authentication Code-Pseudo-Random Function-128
  184.  *                  (AES-CMAC-PRF-128) Algorithm for the Internet Key
  185.  *                  Exchange Protocol (IKE).</em>
  186.  *
  187.  * \param key       The key to use.
  188.  * \param key_len   The key length in Bytes.
  189.  * \param input     The buffer holding the input data.
  190.  * \param in_len    The length of the input data in Bytes.
  191.  * \param output    The buffer holding the generated 16 Bytes of
  192.  *                  pseudorandom output.
  193.  *
  194.  * \return          \c 0 on success.
  195.  */
  196. int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
  197.                               const unsigned char *input, size_t in_len,
  198.                               unsigned char output[16] );
  199. #endif /* MBEDTLS_AES_C */
  200.  
  201. #if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
  202. /**
  203.  * \brief          The CMAC checkup routine.
  204.  *
  205.  * \return         \c 0 on success.
  206.  * \return         \c 1 on failure.
  207.  */
  208. int mbedtls_cmac_self_test( int verbose );
  209. #endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  210.  
  211. #ifdef __cplusplus
  212. }
  213. #endif
  214.  
  215. #endif /* MBEDTLS_CMAC_H */
  216.