Subversion Repositories Kolibri OS

Rev

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

  1. /**
  2.  * \file ccm.h
  3.  *
  4.  * \brief This file provides an API for the CCM authenticated encryption
  5.  *        mode for block ciphers.
  6.  *
  7.  * CCM combines Counter mode encryption with CBC-MAC authentication
  8.  * for 128-bit block ciphers.
  9.  *
  10.  * Input to CCM includes the following elements:
  11.  * <ul><li>Payload - data that is both authenticated and encrypted.</li>
  12.  * <li>Associated data (Adata) - data that is authenticated but not
  13.  * encrypted, For example, a header.</li>
  14.  * <li>Nonce - A unique value that is assigned to the payload and the
  15.  * associated data.</li></ul>
  16.  *
  17.  * Definition of CCM:
  18.  * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
  19.  * RFC 3610 "Counter with CBC-MAC (CCM)"
  20.  *
  21.  * Related:
  22.  * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
  23.  *
  24.  * Definition of CCM*:
  25.  * IEEE 802.15.4 - IEEE Standard for Local and metropolitan area networks
  26.  * Integer representation is fixed most-significant-octet-first order and
  27.  * the representation of octets is most-significant-bit-first order. This is
  28.  * consistent with RFC 3610.
  29.  */
  30. /*
  31.  *  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  32.  *  SPDX-License-Identifier: GPL-2.0
  33.  *
  34.  *  This program is free software; you can redistribute it and/or modify
  35.  *  it under the terms of the GNU General Public License as published by
  36.  *  the Free Software Foundation; either version 2 of the License, or
  37.  *  (at your option) any later version.
  38.  *
  39.  *  This program is distributed in the hope that it will be useful,
  40.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  41.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  42.  *  GNU General Public License for more details.
  43.  *
  44.  *  You should have received a copy of the GNU General Public License along
  45.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  46.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  47.  *
  48.  *  This file is part of Mbed TLS (https://tls.mbed.org)
  49.  */
  50.  
  51. #ifndef MBEDTLS_CCM_H
  52. #define MBEDTLS_CCM_H
  53.  
  54. #if !defined(MBEDTLS_CONFIG_FILE)
  55. #include "config.h"
  56. #else
  57. #include MBEDTLS_CONFIG_FILE
  58. #endif
  59.  
  60. #include "cipher.h"
  61.  
  62. #define MBEDTLS_ERR_CCM_BAD_INPUT       -0x000D /**< Bad input parameters to the function. */
  63. #define MBEDTLS_ERR_CCM_AUTH_FAILED     -0x000F /**< Authenticated decryption failed. */
  64.  
  65. /* MBEDTLS_ERR_CCM_HW_ACCEL_FAILED is deprecated and should not be used. */
  66. #define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
  67.  
  68. #ifdef __cplusplus
  69. extern "C" {
  70. #endif
  71.  
  72. #if !defined(MBEDTLS_CCM_ALT)
  73. // Regular implementation
  74. //
  75.  
  76. /**
  77.  * \brief    The CCM context-type definition. The CCM context is passed
  78.  *           to the APIs called.
  79.  */
  80. typedef struct mbedtls_ccm_context
  81. {
  82.     mbedtls_cipher_context_t cipher_ctx;    /*!< The cipher context used. */
  83. }
  84. mbedtls_ccm_context;
  85.  
  86. #else  /* MBEDTLS_CCM_ALT */
  87. #include "ccm_alt.h"
  88. #endif /* MBEDTLS_CCM_ALT */
  89.  
  90. /**
  91.  * \brief           This function initializes the specified CCM context,
  92.  *                  to make references valid, and prepare the context
  93.  *                  for mbedtls_ccm_setkey() or mbedtls_ccm_free().
  94.  *
  95.  * \param ctx       The CCM context to initialize. This must not be \c NULL.
  96.  */
  97. void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
  98.  
  99. /**
  100.  * \brief           This function initializes the CCM context set in the
  101.  *                  \p ctx parameter and sets the encryption key.
  102.  *
  103.  * \param ctx       The CCM context to initialize. This must be an initialized
  104.  *                  context.
  105.  * \param cipher    The 128-bit block cipher to use.
  106.  * \param key       The encryption key. This must not be \c NULL.
  107.  * \param keybits   The key size in bits. This must be acceptable by the cipher.
  108.  *
  109.  * \return          \c 0 on success.
  110.  * \return          A CCM or cipher-specific error code on failure.
  111.  */
  112. int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
  113.                         mbedtls_cipher_id_t cipher,
  114.                         const unsigned char *key,
  115.                         unsigned int keybits );
  116.  
  117. /**
  118.  * \brief   This function releases and clears the specified CCM context
  119.  *          and underlying cipher sub-context.
  120.  *
  121.  * \param ctx       The CCM context to clear. If this is \c NULL, the function
  122.  *                  has no effect. Otherwise, this must be initialized.
  123.  */
  124. void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
  125.  
  126. /**
  127.  * \brief           This function encrypts a buffer using CCM.
  128.  *
  129.  * \note            The tag is written to a separate buffer. To concatenate
  130.  *                  the \p tag with the \p output, as done in <em>RFC-3610:
  131.  *                  Counter with CBC-MAC (CCM)</em>, use
  132.  *                  \p tag = \p output + \p length, and make sure that the
  133.  *                  output buffer is at least \p length + \p tag_len wide.
  134.  *
  135.  * \param ctx       The CCM context to use for encryption. This must be
  136.  *                  initialized and bound to a key.
  137.  * \param length    The length of the input data in Bytes.
  138.  * \param iv        The initialization vector (nonce). This must be a readable
  139.  *                  buffer of at least \p iv_len Bytes.
  140.  * \param iv_len    The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  141.  *                  or 13. The length L of the message length field is
  142.  *                  15 - \p iv_len.
  143.  * \param add       The additional data field. If \p add_len is greater than
  144.  *                  zero, \p add must be a readable buffer of at least that
  145.  *                  length.
  146.  * \param add_len   The length of additional data in Bytes.
  147.  *                  This must be less than `2^16 - 2^8`.
  148.  * \param input     The buffer holding the input data. If \p length is greater
  149.  *                  than zero, \p input must be a readable buffer of at least
  150.  *                  that length.
  151.  * \param output    The buffer holding the output data. If \p length is greater
  152.  *                  than zero, \p output must be a writable buffer of at least
  153.  *                  that length.
  154.  * \param tag       The buffer holding the authentication field. This must be a
  155.  *                  readable buffer of at least \p tag_len Bytes.
  156.  * \param tag_len   The length of the authentication field to generate in Bytes:
  157.  *                  4, 6, 8, 10, 12, 14 or 16.
  158.  *
  159.  * \return          \c 0 on success.
  160.  * \return          A CCM or cipher-specific error code on failure.
  161.  */
  162. int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  163.                          const unsigned char *iv, size_t iv_len,
  164.                          const unsigned char *add, size_t add_len,
  165.                          const unsigned char *input, unsigned char *output,
  166.                          unsigned char *tag, size_t tag_len );
  167.  
  168. /**
  169.  * \brief           This function encrypts a buffer using CCM*.
  170.  *
  171.  * \note            The tag is written to a separate buffer. To concatenate
  172.  *                  the \p tag with the \p output, as done in <em>RFC-3610:
  173.  *                  Counter with CBC-MAC (CCM)</em>, use
  174.  *                  \p tag = \p output + \p length, and make sure that the
  175.  *                  output buffer is at least \p length + \p tag_len wide.
  176.  *
  177.  * \note            When using this function in a variable tag length context,
  178.  *                  the tag length has to be encoded into the \p iv passed to
  179.  *                  this function.
  180.  *
  181.  * \param ctx       The CCM context to use for encryption. This must be
  182.  *                  initialized and bound to a key.
  183.  * \param length    The length of the input data in Bytes.
  184.  * \param iv        The initialization vector (nonce). This must be a readable
  185.  *                  buffer of at least \p iv_len Bytes.
  186.  * \param iv_len    The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  187.  *                  or 13. The length L of the message length field is
  188.  *                  15 - \p iv_len.
  189.  * \param add       The additional data field. This must be a readable buffer of
  190.  *                  at least \p add_len Bytes.
  191.  * \param add_len   The length of additional data in Bytes.
  192.  *                  This must be less than 2^16 - 2^8.
  193.  * \param input     The buffer holding the input data. If \p length is greater
  194.  *                  than zero, \p input must be a readable buffer of at least
  195.  *                  that length.
  196.  * \param output    The buffer holding the output data. If \p length is greater
  197.  *                  than zero, \p output must be a writable buffer of at least
  198.  *                  that length.
  199.  * \param tag       The buffer holding the authentication field. This must be a
  200.  *                  readable buffer of at least \p tag_len Bytes.
  201.  * \param tag_len   The length of the authentication field to generate in Bytes:
  202.  *                  0, 4, 6, 8, 10, 12, 14 or 16.
  203.  *
  204.  * \warning         Passing \c 0 as \p tag_len means that the message is no
  205.  *                  longer authenticated.
  206.  *
  207.  * \return          \c 0 on success.
  208.  * \return          A CCM or cipher-specific error code on failure.
  209.  */
  210. int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  211.                          const unsigned char *iv, size_t iv_len,
  212.                          const unsigned char *add, size_t add_len,
  213.                          const unsigned char *input, unsigned char *output,
  214.                          unsigned char *tag, size_t tag_len );
  215.  
  216. /**
  217.  * \brief           This function performs a CCM authenticated decryption of a
  218.  *                  buffer.
  219.  *
  220.  * \param ctx       The CCM context to use for decryption. This must be
  221.  *                  initialized and bound to a key.
  222.  * \param length    The length of the input data in Bytes.
  223.  * \param iv        The initialization vector (nonce). This must be a readable
  224.  *                  buffer of at least \p iv_len Bytes.
  225.  * \param iv_len    The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  226.  *                  or 13. The length L of the message length field is
  227.  *                  15 - \p iv_len.
  228.  * \param add       The additional data field. This must be a readable buffer
  229.  *                  of at least that \p add_len Bytes..
  230.  * \param add_len   The length of additional data in Bytes.
  231.  *                  This must be less than 2^16 - 2^8.
  232.  * \param input     The buffer holding the input data. If \p length is greater
  233.  *                  than zero, \p input must be a readable buffer of at least
  234.  *                  that length.
  235.  * \param output    The buffer holding the output data. If \p length is greater
  236.  *                  than zero, \p output must be a writable buffer of at least
  237.  *                  that length.
  238.  * \param tag       The buffer holding the authentication field. This must be a
  239.  *                  readable buffer of at least \p tag_len Bytes.
  240.  * \param tag_len   The length of the authentication field to generate in Bytes:
  241.  *                  4, 6, 8, 10, 12, 14 or 16.
  242.  *
  243.  * \return          \c 0 on success. This indicates that the message is authentic.
  244.  * \return          #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
  245.  * \return          A cipher-specific error code on calculation failure.
  246.  */
  247. int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  248.                       const unsigned char *iv, size_t iv_len,
  249.                       const unsigned char *add, size_t add_len,
  250.                       const unsigned char *input, unsigned char *output,
  251.                       const unsigned char *tag, size_t tag_len );
  252.  
  253. /**
  254.  * \brief           This function performs a CCM* authenticated decryption of a
  255.  *                  buffer.
  256.  *
  257.  * \note            When using this function in a variable tag length context,
  258.  *                  the tag length has to be decoded from \p iv and passed to
  259.  *                  this function as \p tag_len. (\p tag needs to be adjusted
  260.  *                  accordingly.)
  261.  *
  262.  * \param ctx       The CCM context to use for decryption. This must be
  263.  *                  initialized and bound to a key.
  264.  * \param length    The length of the input data in Bytes.
  265.  * \param iv        The initialization vector (nonce). This must be a readable
  266.  *                  buffer of at least \p iv_len Bytes.
  267.  * \param iv_len    The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
  268.  *                  or 13. The length L of the message length field is
  269.  *                  15 - \p iv_len.
  270.  * \param add       The additional data field. This must be a readable buffer of
  271.  *                  at least that \p add_len Bytes.
  272.  * \param add_len   The length of additional data in Bytes.
  273.  *                  This must be less than 2^16 - 2^8.
  274.  * \param input     The buffer holding the input data. If \p length is greater
  275.  *                  than zero, \p input must be a readable buffer of at least
  276.  *                  that length.
  277.  * \param output    The buffer holding the output data. If \p length is greater
  278.  *                  than zero, \p output must be a writable buffer of at least
  279.  *                  that length.
  280.  * \param tag       The buffer holding the authentication field. This must be a
  281.  *                  readable buffer of at least \p tag_len Bytes.
  282.  * \param tag_len   The length of the authentication field in Bytes.
  283.  *                  0, 4, 6, 8, 10, 12, 14 or 16.
  284.  *
  285.  * \warning         Passing \c 0 as \p tag_len means that the message is nos
  286.  *                  longer authenticated.
  287.  *
  288.  * \return          \c 0 on success.
  289.  * \return          #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
  290.  * \return          A cipher-specific error code on calculation failure.
  291.  */
  292. int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  293.                       const unsigned char *iv, size_t iv_len,
  294.                       const unsigned char *add, size_t add_len,
  295.                       const unsigned char *input, unsigned char *output,
  296.                       const unsigned char *tag, size_t tag_len );
  297.  
  298. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  299. /**
  300.  * \brief          The CCM checkup routine.
  301.  *
  302.  * \return         \c 0 on success.
  303.  * \return         \c 1 on failure.
  304.  */
  305. int mbedtls_ccm_self_test( int verbose );
  306. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  307.  
  308. #ifdef __cplusplus
  309. }
  310. #endif
  311.  
  312. #endif /* MBEDTLS_CCM_H */
  313.