Subversion Repositories Kolibri OS

Rev

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

  1. /**
  2.  * \file aes.h
  3.  *
  4.  * \brief   This file contains AES definitions and functions.
  5.  *
  6.  *          The Advanced Encryption Standard (AES) specifies a FIPS-approved
  7.  *          cryptographic algorithm that can be used to protect electronic
  8.  *          data.
  9.  *
  10.  *          The AES algorithm is a symmetric block cipher that can
  11.  *          encrypt and decrypt information. For more information, see
  12.  *          <em>FIPS Publication 197: Advanced Encryption Standard</em> and
  13.  *          <em>ISO/IEC 18033-2:2006: Information technology -- Security
  14.  *          techniques -- Encryption algorithms -- Part 2: Asymmetric
  15.  *          ciphers</em>.
  16.  *
  17.  *          The AES-XTS block mode is standardized by NIST SP 800-38E
  18.  *          <https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38e.pdf>
  19.  *          and described in detail by IEEE P1619
  20.  *          <https://ieeexplore.ieee.org/servlet/opac?punumber=4375278>.
  21.  */
  22.  
  23. /*  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
  24.  *  SPDX-License-Identifier: GPL-2.0
  25.  *
  26.  *  This program is free software; you can redistribute it and/or modify
  27.  *  it under the terms of the GNU General Public License as published by
  28.  *  the Free Software Foundation; either version 2 of the License, or
  29.  *  (at your option) any later version.
  30.  *
  31.  *  This program is distributed in the hope that it will be useful,
  32.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  33.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34.  *  GNU General Public License for more details.
  35.  *
  36.  *  You should have received a copy of the GNU General Public License along
  37.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  38.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  39.  *
  40.  *  This file is part of Mbed TLS (https://tls.mbed.org)
  41.  */
  42.  
  43. #ifndef MBEDTLS_AES_H
  44. #define MBEDTLS_AES_H
  45.  
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51.  
  52. #include <stddef.h>
  53. #include <stdint.h>
  54.  
  55. /* padlock.c and aesni.c rely on these values! */
  56. #define MBEDTLS_AES_ENCRYPT     1 /**< AES encryption. */
  57. #define MBEDTLS_AES_DECRYPT     0 /**< AES decryption. */
  58.  
  59. /* Error codes in range 0x0020-0x0022 */
  60. #define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH                -0x0020  /**< Invalid key length. */
  61. #define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH              -0x0022  /**< Invalid data input length. */
  62.  
  63. /* Error codes in range 0x0021-0x0025 */
  64. #define MBEDTLS_ERR_AES_BAD_INPUT_DATA                    -0x0021  /**< Invalid input data. */
  65.  
  66. /* MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE is deprecated and should not be used. */
  67. #define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE               -0x0023  /**< Feature not available. For example, an unsupported AES key size. */
  68.  
  69. /* MBEDTLS_ERR_AES_HW_ACCEL_FAILED is deprecated and should not be used. */
  70. #define MBEDTLS_ERR_AES_HW_ACCEL_FAILED                   -0x0025  /**< AES hardware accelerator failed. */
  71.  
  72. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  73.     !defined(inline) && !defined(__cplusplus)
  74. #define inline __inline
  75. #endif
  76.  
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80.  
  81. #if !defined(MBEDTLS_AES_ALT)
  82. // Regular implementation
  83. //
  84.  
  85. /**
  86.  * \brief The AES context-type definition.
  87.  */
  88. typedef struct mbedtls_aes_context
  89. {
  90.     int nr;                     /*!< The number of rounds. */
  91.     uint32_t *rk;               /*!< AES round keys. */
  92.     uint32_t buf[68];           /*!< Unaligned data buffer. This buffer can
  93.                                      hold 32 extra Bytes, which can be used for
  94.                                      one of the following purposes:
  95.                                      <ul><li>Alignment if VIA padlock is
  96.                                              used.</li>
  97.                                      <li>Simplifying key expansion in the 256-bit
  98.                                          case by generating an extra round key.
  99.                                          </li></ul> */
  100. }
  101. mbedtls_aes_context;
  102.  
  103. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  104. /**
  105.  * \brief The AES XTS context-type definition.
  106.  */
  107. typedef struct mbedtls_aes_xts_context
  108. {
  109.     mbedtls_aes_context crypt; /*!< The AES context to use for AES block
  110.                                         encryption or decryption. */
  111.     mbedtls_aes_context tweak; /*!< The AES context used for tweak
  112.                                         computation. */
  113. } mbedtls_aes_xts_context;
  114. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  115.  
  116. #else  /* MBEDTLS_AES_ALT */
  117. #include "aes_alt.h"
  118. #endif /* MBEDTLS_AES_ALT */
  119.  
  120. /**
  121.  * \brief          This function initializes the specified AES context.
  122.  *
  123.  *                 It must be the first API called before using
  124.  *                 the context.
  125.  *
  126.  * \param ctx      The AES context to initialize. This must not be \c NULL.
  127.  */
  128. void mbedtls_aes_init( mbedtls_aes_context *ctx );
  129.  
  130. /**
  131.  * \brief          This function releases and clears the specified AES context.
  132.  *
  133.  * \param ctx      The AES context to clear.
  134.  *                 If this is \c NULL, this function does nothing.
  135.  *                 Otherwise, the context must have been at least initialized.
  136.  */
  137. void mbedtls_aes_free( mbedtls_aes_context *ctx );
  138.  
  139. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  140. /**
  141.  * \brief          This function initializes the specified AES XTS context.
  142.  *
  143.  *                 It must be the first API called before using
  144.  *                 the context.
  145.  *
  146.  * \param ctx      The AES XTS context to initialize. This must not be \c NULL.
  147.  */
  148. void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx );
  149.  
  150. /**
  151.  * \brief          This function releases and clears the specified AES XTS context.
  152.  *
  153.  * \param ctx      The AES XTS context to clear.
  154.  *                 If this is \c NULL, this function does nothing.
  155.  *                 Otherwise, the context must have been at least initialized.
  156.  */
  157. void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx );
  158. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  159.  
  160. /**
  161.  * \brief          This function sets the encryption key.
  162.  *
  163.  * \param ctx      The AES context to which the key should be bound.
  164.  *                 It must be initialized.
  165.  * \param key      The encryption key.
  166.  *                 This must be a readable buffer of size \p keybits bits.
  167.  * \param keybits  The size of data passed in bits. Valid options are:
  168.  *                 <ul><li>128 bits</li>
  169.  *                 <li>192 bits</li>
  170.  *                 <li>256 bits</li></ul>
  171.  *
  172.  * \return         \c 0 on success.
  173.  * \return         #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
  174.  */
  175. int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
  176.                     unsigned int keybits );
  177.  
  178. /**
  179.  * \brief          This function sets the decryption key.
  180.  *
  181.  * \param ctx      The AES context to which the key should be bound.
  182.  *                 It must be initialized.
  183.  * \param key      The decryption key.
  184.  *                 This must be a readable buffer of size \p keybits bits.
  185.  * \param keybits  The size of data passed. Valid options are:
  186.  *                 <ul><li>128 bits</li>
  187.  *                 <li>192 bits</li>
  188.  *                 <li>256 bits</li></ul>
  189.  *
  190.  * \return         \c 0 on success.
  191.  * \return         #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
  192.  */
  193. int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
  194.                     unsigned int keybits );
  195.  
  196. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  197. /**
  198.  * \brief          This function prepares an XTS context for encryption and
  199.  *                 sets the encryption key.
  200.  *
  201.  * \param ctx      The AES XTS context to which the key should be bound.
  202.  *                 It must be initialized.
  203.  * \param key      The encryption key. This is comprised of the XTS key1
  204.  *                 concatenated with the XTS key2.
  205.  *                 This must be a readable buffer of size \p keybits bits.
  206.  * \param keybits  The size of \p key passed in bits. Valid options are:
  207.  *                 <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
  208.  *                 <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
  209.  *
  210.  * \return         \c 0 on success.
  211.  * \return         #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
  212.  */
  213. int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
  214.                                 const unsigned char *key,
  215.                                 unsigned int keybits );
  216.  
  217. /**
  218.  * \brief          This function prepares an XTS context for decryption and
  219.  *                 sets the decryption key.
  220.  *
  221.  * \param ctx      The AES XTS context to which the key should be bound.
  222.  *                 It must be initialized.
  223.  * \param key      The decryption key. This is comprised of the XTS key1
  224.  *                 concatenated with the XTS key2.
  225.  *                 This must be a readable buffer of size \p keybits bits.
  226.  * \param keybits  The size of \p key passed in bits. Valid options are:
  227.  *                 <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
  228.  *                 <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
  229.  *
  230.  * \return         \c 0 on success.
  231.  * \return         #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
  232.  */
  233. int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
  234.                                 const unsigned char *key,
  235.                                 unsigned int keybits );
  236. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  237.  
  238. /**
  239.  * \brief          This function performs an AES single-block encryption or
  240.  *                 decryption operation.
  241.  *
  242.  *                 It performs the operation defined in the \p mode parameter
  243.  *                 (encrypt or decrypt), on the input data buffer defined in
  244.  *                 the \p input parameter.
  245.  *
  246.  *                 mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
  247.  *                 mbedtls_aes_setkey_dec() must be called before the first
  248.  *                 call to this API with the same context.
  249.  *
  250.  * \param ctx      The AES context to use for encryption or decryption.
  251.  *                 It must be initialized and bound to a key.
  252.  * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or
  253.  *                 #MBEDTLS_AES_DECRYPT.
  254.  * \param input    The buffer holding the input data.
  255.  *                 It must be readable and at least \c 16 Bytes long.
  256.  * \param output   The buffer where the output data will be written.
  257.  *                 It must be writeable and at least \c 16 Bytes long.
  258.  
  259.  * \return         \c 0 on success.
  260.  */
  261. int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
  262.                     int mode,
  263.                     const unsigned char input[16],
  264.                     unsigned char output[16] );
  265.  
  266. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  267. /**
  268.  * \brief  This function performs an AES-CBC encryption or decryption operation
  269.  *         on full blocks.
  270.  *
  271.  *         It performs the operation defined in the \p mode
  272.  *         parameter (encrypt/decrypt), on the input data buffer defined in
  273.  *         the \p input parameter.
  274.  *
  275.  *         It can be called as many times as needed, until all the input
  276.  *         data is processed. mbedtls_aes_init(), and either
  277.  *         mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
  278.  *         before the first call to this API with the same context.
  279.  *
  280.  * \note   This function operates on full blocks, that is, the input size
  281.  *         must be a multiple of the AES block size of \c 16 Bytes.
  282.  *
  283.  * \note   Upon exit, the content of the IV is updated so that you can
  284.  *         call the same function again on the next
  285.  *         block(s) of data and get the same result as if it was
  286.  *         encrypted in one call. This allows a "streaming" usage.
  287.  *         If you need to retain the contents of the IV, you should
  288.  *         either save it manually or use the cipher module instead.
  289.  *
  290.  *
  291.  * \param ctx      The AES context to use for encryption or decryption.
  292.  *                 It must be initialized and bound to a key.
  293.  * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or
  294.  *                 #MBEDTLS_AES_DECRYPT.
  295.  * \param length   The length of the input data in Bytes. This must be a
  296.  *                 multiple of the block size (\c 16 Bytes).
  297.  * \param iv       Initialization vector (updated after use).
  298.  *                 It must be a readable and writeable buffer of \c 16 Bytes.
  299.  * \param input    The buffer holding the input data.
  300.  *                 It must be readable and of size \p length Bytes.
  301.  * \param output   The buffer holding the output data.
  302.  *                 It must be writeable and of size \p length Bytes.
  303.  *
  304.  * \return         \c 0 on success.
  305.  * \return         #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
  306.  *                 on failure.
  307.  */
  308. int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
  309.                     int mode,
  310.                     size_t length,
  311.                     unsigned char iv[16],
  312.                     const unsigned char *input,
  313.                     unsigned char *output );
  314. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  315.  
  316. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  317. /**
  318.  * \brief      This function performs an AES-XTS encryption or decryption
  319.  *             operation for an entire XTS data unit.
  320.  *
  321.  *             AES-XTS encrypts or decrypts blocks based on their location as
  322.  *             defined by a data unit number. The data unit number must be
  323.  *             provided by \p data_unit.
  324.  *
  325.  *             NIST SP 800-38E limits the maximum size of a data unit to 2^20
  326.  *             AES blocks. If the data unit is larger than this, this function
  327.  *             returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
  328.  *
  329.  * \param ctx          The AES XTS context to use for AES XTS operations.
  330.  *                     It must be initialized and bound to a key.
  331.  * \param mode         The AES operation: #MBEDTLS_AES_ENCRYPT or
  332.  *                     #MBEDTLS_AES_DECRYPT.
  333.  * \param length       The length of a data unit in Bytes. This can be any
  334.  *                     length between 16 bytes and 2^24 bytes inclusive
  335.  *                     (between 1 and 2^20 block cipher blocks).
  336.  * \param data_unit    The address of the data unit encoded as an array of 16
  337.  *                     bytes in little-endian format. For disk encryption, this
  338.  *                     is typically the index of the block device sector that
  339.  *                     contains the data.
  340.  * \param input        The buffer holding the input data (which is an entire
  341.  *                     data unit). This function reads \p length Bytes from \p
  342.  *                     input.
  343.  * \param output       The buffer holding the output data (which is an entire
  344.  *                     data unit). This function writes \p length Bytes to \p
  345.  *                     output.
  346.  *
  347.  * \return             \c 0 on success.
  348.  * \return             #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
  349.  *                     smaller than an AES block in size (16 Bytes) or if \p
  350.  *                     length is larger than 2^20 blocks (16 MiB).
  351.  */
  352. int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
  353.                            int mode,
  354.                            size_t length,
  355.                            const unsigned char data_unit[16],
  356.                            const unsigned char *input,
  357.                            unsigned char *output );
  358. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  359.  
  360. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  361. /**
  362.  * \brief This function performs an AES-CFB128 encryption or decryption
  363.  *        operation.
  364.  *
  365.  *        It performs the operation defined in the \p mode
  366.  *        parameter (encrypt or decrypt), on the input data buffer
  367.  *        defined in the \p input parameter.
  368.  *
  369.  *        For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
  370.  *        regardless of whether you are performing an encryption or decryption
  371.  *        operation, that is, regardless of the \p mode parameter. This is
  372.  *        because CFB mode uses the same key schedule for encryption and
  373.  *        decryption.
  374.  *
  375.  * \note  Upon exit, the content of the IV is updated so that you can
  376.  *        call the same function again on the next
  377.  *        block(s) of data and get the same result as if it was
  378.  *        encrypted in one call. This allows a "streaming" usage.
  379.  *        If you need to retain the contents of the
  380.  *        IV, you must either save it manually or use the cipher
  381.  *        module instead.
  382.  *
  383.  *
  384.  * \param ctx      The AES context to use for encryption or decryption.
  385.  *                 It must be initialized and bound to a key.
  386.  * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or
  387.  *                 #MBEDTLS_AES_DECRYPT.
  388.  * \param length   The length of the input data in Bytes.
  389.  * \param iv_off   The offset in IV (updated after use).
  390.  *                 It must point to a valid \c size_t.
  391.  * \param iv       The initialization vector (updated after use).
  392.  *                 It must be a readable and writeable buffer of \c 16 Bytes.
  393.  * \param input    The buffer holding the input data.
  394.  *                 It must be readable and of size \p length Bytes.
  395.  * \param output   The buffer holding the output data.
  396.  *                 It must be writeable and of size \p length Bytes.
  397.  *
  398.  * \return         \c 0 on success.
  399.  */
  400. int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
  401.                        int mode,
  402.                        size_t length,
  403.                        size_t *iv_off,
  404.                        unsigned char iv[16],
  405.                        const unsigned char *input,
  406.                        unsigned char *output );
  407.  
  408. /**
  409.  * \brief This function performs an AES-CFB8 encryption or decryption
  410.  *        operation.
  411.  *
  412.  *        It performs the operation defined in the \p mode
  413.  *        parameter (encrypt/decrypt), on the input data buffer defined
  414.  *        in the \p input parameter.
  415.  *
  416.  *        Due to the nature of CFB, you must use the same key schedule for
  417.  *        both encryption and decryption operations. Therefore, you must
  418.  *        use the context initialized with mbedtls_aes_setkey_enc() for
  419.  *        both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
  420.  *
  421.  * \note  Upon exit, the content of the IV is updated so that you can
  422.  *        call the same function again on the next
  423.  *        block(s) of data and get the same result as if it was
  424.  *        encrypted in one call. This allows a "streaming" usage.
  425.  *        If you need to retain the contents of the
  426.  *        IV, you should either save it manually or use the cipher
  427.  *        module instead.
  428.  *
  429.  *
  430.  * \param ctx      The AES context to use for encryption or decryption.
  431.  *                 It must be initialized and bound to a key.
  432.  * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or
  433.  *                 #MBEDTLS_AES_DECRYPT
  434.  * \param length   The length of the input data.
  435.  * \param iv       The initialization vector (updated after use).
  436.  *                 It must be a readable and writeable buffer of \c 16 Bytes.
  437.  * \param input    The buffer holding the input data.
  438.  *                 It must be readable and of size \p length Bytes.
  439.  * \param output   The buffer holding the output data.
  440.  *                 It must be writeable and of size \p length Bytes.
  441.  *
  442.  * \return         \c 0 on success.
  443.  */
  444. int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
  445.                     int mode,
  446.                     size_t length,
  447.                     unsigned char iv[16],
  448.                     const unsigned char *input,
  449.                     unsigned char *output );
  450. #endif /*MBEDTLS_CIPHER_MODE_CFB */
  451.  
  452. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  453. /**
  454.  * \brief       This function performs an AES-OFB (Output Feedback Mode)
  455.  *              encryption or decryption operation.
  456.  *
  457.  *              For OFB, you must set up the context with
  458.  *              mbedtls_aes_setkey_enc(), regardless of whether you are
  459.  *              performing an encryption or decryption operation. This is
  460.  *              because OFB mode uses the same key schedule for encryption and
  461.  *              decryption.
  462.  *
  463.  *              The OFB operation is identical for encryption or decryption,
  464.  *              therefore no operation mode needs to be specified.
  465.  *
  466.  * \note        Upon exit, the content of iv, the Initialisation Vector, is
  467.  *              updated so that you can call the same function again on the next
  468.  *              block(s) of data and get the same result as if it was encrypted
  469.  *              in one call. This allows a "streaming" usage, by initialising
  470.  *              iv_off to 0 before the first call, and preserving its value
  471.  *              between calls.
  472.  *
  473.  *              For non-streaming use, the iv should be initialised on each call
  474.  *              to a unique value, and iv_off set to 0 on each call.
  475.  *
  476.  *              If you need to retain the contents of the initialisation vector,
  477.  *              you must either save it manually or use the cipher module
  478.  *              instead.
  479.  *
  480.  * \warning     For the OFB mode, the initialisation vector must be unique
  481.  *              every encryption operation. Reuse of an initialisation vector
  482.  *              will compromise security.
  483.  *
  484.  * \param ctx      The AES context to use for encryption or decryption.
  485.  *                 It must be initialized and bound to a key.
  486.  * \param length   The length of the input data.
  487.  * \param iv_off   The offset in IV (updated after use).
  488.  *                 It must point to a valid \c size_t.
  489.  * \param iv       The initialization vector (updated after use).
  490.  *                 It must be a readable and writeable buffer of \c 16 Bytes.
  491.  * \param input    The buffer holding the input data.
  492.  *                 It must be readable and of size \p length Bytes.
  493.  * \param output   The buffer holding the output data.
  494.  *                 It must be writeable and of size \p length Bytes.
  495.  *
  496.  * \return         \c 0 on success.
  497.  */
  498. int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
  499.                        size_t length,
  500.                        size_t *iv_off,
  501.                        unsigned char iv[16],
  502.                        const unsigned char *input,
  503.                        unsigned char *output );
  504.  
  505. #endif /* MBEDTLS_CIPHER_MODE_OFB */
  506.  
  507. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  508. /**
  509.  * \brief      This function performs an AES-CTR encryption or decryption
  510.  *             operation.
  511.  *
  512.  *             This function performs the operation defined in the \p mode
  513.  *             parameter (encrypt/decrypt), on the input data buffer
  514.  *             defined in the \p input parameter.
  515.  *
  516.  *             Due to the nature of CTR, you must use the same key schedule
  517.  *             for both encryption and decryption operations. Therefore, you
  518.  *             must use the context initialized with mbedtls_aes_setkey_enc()
  519.  *             for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
  520.  *
  521.  * \warning    You must never reuse a nonce value with the same key. Doing so
  522.  *             would void the encryption for the two messages encrypted with
  523.  *             the same nonce and key.
  524.  *
  525.  *             There are two common strategies for managing nonces with CTR:
  526.  *
  527.  *             1. You can handle everything as a single message processed over
  528.  *             successive calls to this function. In that case, you want to
  529.  *             set \p nonce_counter and \p nc_off to 0 for the first call, and
  530.  *             then preserve the values of \p nonce_counter, \p nc_off and \p
  531.  *             stream_block across calls to this function as they will be
  532.  *             updated by this function.
  533.  *
  534.  *             With this strategy, you must not encrypt more than 2**128
  535.  *             blocks of data with the same key.
  536.  *
  537.  *             2. You can encrypt separate messages by dividing the \p
  538.  *             nonce_counter buffer in two areas: the first one used for a
  539.  *             per-message nonce, handled by yourself, and the second one
  540.  *             updated by this function internally.
  541.  *
  542.  *             For example, you might reserve the first 12 bytes for the
  543.  *             per-message nonce, and the last 4 bytes for internal use. In that
  544.  *             case, before calling this function on a new message you need to
  545.  *             set the first 12 bytes of \p nonce_counter to your chosen nonce
  546.  *             value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
  547.  *             stream_block to be ignored). That way, you can encrypt at most
  548.  *             2**96 messages of up to 2**32 blocks each with the same key.
  549.  *
  550.  *             The per-message nonce (or information sufficient to reconstruct
  551.  *             it) needs to be communicated with the ciphertext and must be unique.
  552.  *             The recommended way to ensure uniqueness is to use a message
  553.  *             counter. An alternative is to generate random nonces, but this
  554.  *             limits the number of messages that can be securely encrypted:
  555.  *             for example, with 96-bit random nonces, you should not encrypt
  556.  *             more than 2**32 messages with the same key.
  557.  *
  558.  *             Note that for both stategies, sizes are measured in blocks and
  559.  *             that an AES block is 16 bytes.
  560.  *
  561.  * \warning    Upon return, \p stream_block contains sensitive data. Its
  562.  *             content must not be written to insecure storage and should be
  563.  *             securely discarded as soon as it's no longer needed.
  564.  *
  565.  * \param ctx              The AES context to use for encryption or decryption.
  566.  *                         It must be initialized and bound to a key.
  567.  * \param length           The length of the input data.
  568.  * \param nc_off           The offset in the current \p stream_block, for
  569.  *                         resuming within the current cipher stream. The
  570.  *                         offset pointer should be 0 at the start of a stream.
  571.  *                         It must point to a valid \c size_t.
  572.  * \param nonce_counter    The 128-bit nonce and counter.
  573.  *                         It must be a readable-writeable buffer of \c 16 Bytes.
  574.  * \param stream_block     The saved stream block for resuming. This is
  575.  *                         overwritten by the function.
  576.  *                         It must be a readable-writeable buffer of \c 16 Bytes.
  577.  * \param input            The buffer holding the input data.
  578.  *                         It must be readable and of size \p length Bytes.
  579.  * \param output           The buffer holding the output data.
  580.  *                         It must be writeable and of size \p length Bytes.
  581.  *
  582.  * \return                 \c 0 on success.
  583.  */
  584. int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
  585.                        size_t length,
  586.                        size_t *nc_off,
  587.                        unsigned char nonce_counter[16],
  588.                        unsigned char stream_block[16],
  589.                        const unsigned char *input,
  590.                        unsigned char *output );
  591. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  592.  
  593. /**
  594.  * \brief           Internal AES block encryption function. This is only
  595.  *                  exposed to allow overriding it using
  596.  *                  \c MBEDTLS_AES_ENCRYPT_ALT.
  597.  *
  598.  * \param ctx       The AES context to use for encryption.
  599.  * \param input     The plaintext block.
  600.  * \param output    The output (ciphertext) block.
  601.  *
  602.  * \return          \c 0 on success.
  603.  */
  604. int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
  605.                                   const unsigned char input[16],
  606.                                   unsigned char output[16] );
  607.  
  608. /**
  609.  * \brief           Internal AES block decryption function. This is only
  610.  *                  exposed to allow overriding it using see
  611.  *                  \c MBEDTLS_AES_DECRYPT_ALT.
  612.  *
  613.  * \param ctx       The AES context to use for decryption.
  614.  * \param input     The ciphertext block.
  615.  * \param output    The output (plaintext) block.
  616.  *
  617.  * \return          \c 0 on success.
  618.  */
  619. int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
  620.                                   const unsigned char input[16],
  621.                                   unsigned char output[16] );
  622.  
  623. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  624. #if defined(MBEDTLS_DEPRECATED_WARNING)
  625. #define MBEDTLS_DEPRECATED      __attribute__((deprecated))
  626. #else
  627. #define MBEDTLS_DEPRECATED
  628. #endif
  629. /**
  630.  * \brief           Deprecated internal AES block encryption function
  631.  *                  without return value.
  632.  *
  633.  * \deprecated      Superseded by mbedtls_internal_aes_encrypt()
  634.  *
  635.  * \param ctx       The AES context to use for encryption.
  636.  * \param input     Plaintext block.
  637.  * \param output    Output (ciphertext) block.
  638.  */
  639. MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
  640.                                              const unsigned char input[16],
  641.                                              unsigned char output[16] );
  642.  
  643. /**
  644.  * \brief           Deprecated internal AES block decryption function
  645.  *                  without return value.
  646.  *
  647.  * \deprecated      Superseded by mbedtls_internal_aes_decrypt()
  648.  *
  649.  * \param ctx       The AES context to use for decryption.
  650.  * \param input     Ciphertext block.
  651.  * \param output    Output (plaintext) block.
  652.  */
  653. MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
  654.                                              const unsigned char input[16],
  655.                                              unsigned char output[16] );
  656.  
  657. #undef MBEDTLS_DEPRECATED
  658. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  659.  
  660.  
  661. #if defined(MBEDTLS_SELF_TEST)
  662. /**
  663.  * \brief          Checkup routine.
  664.  *
  665.  * \return         \c 0 on success.
  666.  * \return         \c 1 on failure.
  667.  */
  668. int mbedtls_aes_self_test( int verbose );
  669.  
  670. #endif /* MBEDTLS_SELF_TEST */
  671.  
  672. #ifdef __cplusplus
  673. }
  674. #endif
  675.  
  676. #endif /* aes.h */
  677.