]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS-Labs/Source/mbedtls/include/mbedtls/aes.h
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Source / mbedtls / include / mbedtls / aes.h
diff --git a/FreeRTOS-Labs/Source/mbedtls/include/mbedtls/aes.h b/FreeRTOS-Labs/Source/mbedtls/include/mbedtls/aes.h
new file mode 100644 (file)
index 0000000..4131f1d
--- /dev/null
@@ -0,0 +1,674 @@
+/**\r
+ * \file aes.h\r
+ *\r
+ * \brief   This file contains AES definitions and functions.\r
+ *\r
+ *          The Advanced Encryption Standard (AES) specifies a FIPS-approved\r
+ *          cryptographic algorithm that can be used to protect electronic\r
+ *          data.\r
+ *\r
+ *          The AES algorithm is a symmetric block cipher that can\r
+ *          encrypt and decrypt information. For more information, see\r
+ *          <em>FIPS Publication 197: Advanced Encryption Standard</em> and\r
+ *          <em>ISO/IEC 18033-2:2006: Information technology -- Security\r
+ *          techniques -- Encryption algorithms -- Part 2: Asymmetric\r
+ *          ciphers</em>.\r
+ *\r
+ *          The AES-XTS block mode is standardized by NIST SP 800-38E\r
+ *          <https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38e.pdf>\r
+ *          and described in detail by IEEE P1619\r
+ *          <https://ieeexplore.ieee.org/servlet/opac?punumber=4375278>.\r
+ */\r
+\r
+/*  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.\r
+ *  SPDX-License-Identifier: Apache-2.0\r
+ *\r
+ *  Licensed under the Apache License, Version 2.0 (the "License"); you may\r
+ *  not use this file except in compliance with the License.\r
+ *  You may obtain a copy of the License at\r
+ *\r
+ *  http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *  Unless required by applicable law or agreed to in writing, software\r
+ *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\r
+ *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *  See the License for the specific language governing permissions and\r
+ *  limitations under the License.\r
+ *\r
+ *  This file is part of Mbed TLS (https://tls.mbed.org)\r
+ */\r
+\r
+#ifndef MBEDTLS_AES_H\r
+#define MBEDTLS_AES_H\r
+\r
+#if !defined(MBEDTLS_CONFIG_FILE)\r
+#include "config.h"\r
+#else\r
+#include MBEDTLS_CONFIG_FILE\r
+#endif\r
+\r
+#include <stddef.h>\r
+#include <stdint.h>\r
+\r
+/* padlock.c and aesni.c rely on these values! */\r
+#define MBEDTLS_AES_ENCRYPT     1 /**< AES encryption. */\r
+#define MBEDTLS_AES_DECRYPT     0 /**< AES decryption. */\r
+\r
+/* Error codes in range 0x0020-0x0022 */\r
+#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH                -0x0020  /**< Invalid key length. */\r
+#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH              -0x0022  /**< Invalid data input length. */\r
+\r
+/* Error codes in range 0x0021-0x0025 */\r
+#define MBEDTLS_ERR_AES_BAD_INPUT_DATA                    -0x0021  /**< Invalid input data. */\r
+\r
+/* MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE is deprecated and should not be used. */\r
+#define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE               -0x0023  /**< Feature not available. For example, an unsupported AES key size. */\r
+\r
+/* MBEDTLS_ERR_AES_HW_ACCEL_FAILED is deprecated and should not be used. */\r
+#define MBEDTLS_ERR_AES_HW_ACCEL_FAILED                   -0x0025  /**< AES hardware accelerator failed. */\r
+\r
+#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \\r
+    !defined(inline) && !defined(__cplusplus)\r
+#define inline __inline\r
+#endif\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+#if !defined(MBEDTLS_AES_ALT)\r
+// Regular implementation\r
+//\r
+\r
+/**\r
+ * \brief The AES context-type definition.\r
+ */\r
+typedef struct mbedtls_aes_context\r
+{\r
+    int nr;                     /*!< The number of rounds. */\r
+    uint32_t *rk;               /*!< AES round keys. */\r
+    uint32_t buf[68];           /*!< Unaligned data buffer. This buffer can\r
+                                     hold 32 extra Bytes, which can be used for\r
+                                     one of the following purposes:\r
+                                     <ul><li>Alignment if VIA padlock is\r
+                                             used.</li>\r
+                                     <li>Simplifying key expansion in the 256-bit\r
+                                         case by generating an extra round key.\r
+                                         </li></ul> */\r
+}\r
+mbedtls_aes_context;\r
+\r
+#if defined(MBEDTLS_CIPHER_MODE_XTS)\r
+/**\r
+ * \brief The AES XTS context-type definition.\r
+ */\r
+typedef struct mbedtls_aes_xts_context\r
+{\r
+    mbedtls_aes_context crypt; /*!< The AES context to use for AES block\r
+                                        encryption or decryption. */\r
+    mbedtls_aes_context tweak; /*!< The AES context used for tweak\r
+                                        computation. */\r
+} mbedtls_aes_xts_context;\r
+#endif /* MBEDTLS_CIPHER_MODE_XTS */\r
+\r
+#else  /* MBEDTLS_AES_ALT */\r
+#include "aes_alt.h"\r
+#endif /* MBEDTLS_AES_ALT */\r
+\r
+/**\r
+ * \brief          This function initializes the specified AES context.\r
+ *\r
+ *                 It must be the first API called before using\r
+ *                 the context.\r
+ *\r
+ * \param ctx      The AES context to initialize. This must not be \c NULL.\r
+ */\r
+void mbedtls_aes_init( mbedtls_aes_context *ctx );\r
+\r
+/**\r
+ * \brief          This function releases and clears the specified AES context.\r
+ *\r
+ * \param ctx      The AES context to clear.\r
+ *                 If this is \c NULL, this function does nothing.\r
+ *                 Otherwise, the context must have been at least initialized.\r
+ */\r
+void mbedtls_aes_free( mbedtls_aes_context *ctx );\r
+\r
+#if defined(MBEDTLS_CIPHER_MODE_XTS)\r
+/**\r
+ * \brief          This function initializes the specified AES XTS context.\r
+ *\r
+ *                 It must be the first API called before using\r
+ *                 the context.\r
+ *\r
+ * \param ctx      The AES XTS context to initialize. This must not be \c NULL.\r
+ */\r
+void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx );\r
+\r
+/**\r
+ * \brief          This function releases and clears the specified AES XTS context.\r
+ *\r
+ * \param ctx      The AES XTS context to clear.\r
+ *                 If this is \c NULL, this function does nothing.\r
+ *                 Otherwise, the context must have been at least initialized.\r
+ */\r
+void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx );\r
+#endif /* MBEDTLS_CIPHER_MODE_XTS */\r
+\r
+/**\r
+ * \brief          This function sets the encryption key.\r
+ *\r
+ * \param ctx      The AES context to which the key should be bound.\r
+ *                 It must be initialized.\r
+ * \param key      The encryption key.\r
+ *                 This must be a readable buffer of size \p keybits bits.\r
+ * \param keybits  The size of data passed in bits. Valid options are:\r
+ *                 <ul><li>128 bits</li>\r
+ *                 <li>192 bits</li>\r
+ *                 <li>256 bits</li></ul>\r
+ *\r
+ * \return         \c 0 on success.\r
+ * \return         #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.\r
+ */\r
+int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,\r
+                    unsigned int keybits );\r
+\r
+/**\r
+ * \brief          This function sets the decryption key.\r
+ *\r
+ * \param ctx      The AES context to which the key should be bound.\r
+ *                 It must be initialized.\r
+ * \param key      The decryption key.\r
+ *                 This must be a readable buffer of size \p keybits bits.\r
+ * \param keybits  The size of data passed. Valid options are:\r
+ *                 <ul><li>128 bits</li>\r
+ *                 <li>192 bits</li>\r
+ *                 <li>256 bits</li></ul>\r
+ *\r
+ * \return         \c 0 on success.\r
+ * \return         #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.\r
+ */\r
+int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,\r
+                    unsigned int keybits );\r
+\r
+#if defined(MBEDTLS_CIPHER_MODE_XTS)\r
+/**\r
+ * \brief          This function prepares an XTS context for encryption and\r
+ *                 sets the encryption key.\r
+ *\r
+ * \param ctx      The AES XTS context to which the key should be bound.\r
+ *                 It must be initialized.\r
+ * \param key      The encryption key. This is comprised of the XTS key1\r
+ *                 concatenated with the XTS key2.\r
+ *                 This must be a readable buffer of size \p keybits bits.\r
+ * \param keybits  The size of \p key passed in bits. Valid options are:\r
+ *                 <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>\r
+ *                 <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>\r
+ *\r
+ * \return         \c 0 on success.\r
+ * \return         #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.\r
+ */\r
+int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,\r
+                                const unsigned char *key,\r
+                                unsigned int keybits );\r
+\r
+/**\r
+ * \brief          This function prepares an XTS context for decryption and\r
+ *                 sets the decryption key.\r
+ *\r
+ * \param ctx      The AES XTS context to which the key should be bound.\r
+ *                 It must be initialized.\r
+ * \param key      The decryption key. This is comprised of the XTS key1\r
+ *                 concatenated with the XTS key2.\r
+ *                 This must be a readable buffer of size \p keybits bits.\r
+ * \param keybits  The size of \p key passed in bits. Valid options are:\r
+ *                 <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>\r
+ *                 <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>\r
+ *\r
+ * \return         \c 0 on success.\r
+ * \return         #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.\r
+ */\r
+int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,\r
+                                const unsigned char *key,\r
+                                unsigned int keybits );\r
+#endif /* MBEDTLS_CIPHER_MODE_XTS */\r
+\r
+/**\r
+ * \brief          This function performs an AES single-block encryption or\r
+ *                 decryption operation.\r
+ *\r
+ *                 It performs the operation defined in the \p mode parameter\r
+ *                 (encrypt or decrypt), on the input data buffer defined in\r
+ *                 the \p input parameter.\r
+ *\r
+ *                 mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or\r
+ *                 mbedtls_aes_setkey_dec() must be called before the first\r
+ *                 call to this API with the same context.\r
+ *\r
+ * \param ctx      The AES context to use for encryption or decryption.\r
+ *                 It must be initialized and bound to a key.\r
+ * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or\r
+ *                 #MBEDTLS_AES_DECRYPT.\r
+ * \param input    The buffer holding the input data.\r
+ *                 It must be readable and at least \c 16 Bytes long.\r
+ * \param output   The buffer where the output data will be written.\r
+ *                 It must be writeable and at least \c 16 Bytes long.\r
+\r
+ * \return         \c 0 on success.\r
+ */\r
+int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,\r
+                    int mode,\r
+                    const unsigned char input[16],\r
+                    unsigned char output[16] );\r
+\r
+#if defined(MBEDTLS_CIPHER_MODE_CBC)\r
+/**\r
+ * \brief  This function performs an AES-CBC encryption or decryption operation\r
+ *         on full blocks.\r
+ *\r
+ *         It performs the operation defined in the \p mode\r
+ *         parameter (encrypt/decrypt), on the input data buffer defined in\r
+ *         the \p input parameter.\r
+ *\r
+ *         It can be called as many times as needed, until all the input\r
+ *         data is processed. mbedtls_aes_init(), and either\r
+ *         mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called\r
+ *         before the first call to this API with the same context.\r
+ *\r
+ * \note   This function operates on full blocks, that is, the input size\r
+ *         must be a multiple of the AES block size of \c 16 Bytes.\r
+ *\r
+ * \note   Upon exit, the content of the IV is updated so that you can\r
+ *         call the same function again on the next\r
+ *         block(s) of data and get the same result as if it was\r
+ *         encrypted in one call. This allows a "streaming" usage.\r
+ *         If you need to retain the contents of the IV, you should\r
+ *         either save it manually or use the cipher module instead.\r
+ *\r
+ *\r
+ * \param ctx      The AES context to use for encryption or decryption.\r
+ *                 It must be initialized and bound to a key.\r
+ * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or\r
+ *                 #MBEDTLS_AES_DECRYPT.\r
+ * \param length   The length of the input data in Bytes. This must be a\r
+ *                 multiple of the block size (\c 16 Bytes).\r
+ * \param iv       Initialization vector (updated after use).\r
+ *                 It must be a readable and writeable buffer of \c 16 Bytes.\r
+ * \param input    The buffer holding the input data.\r
+ *                 It must be readable and of size \p length Bytes.\r
+ * \param output   The buffer holding the output data.\r
+ *                 It must be writeable and of size \p length Bytes.\r
+ *\r
+ * \return         \c 0 on success.\r
+ * \return         #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH\r
+ *                 on failure.\r
+ */\r
+int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,\r
+                    int mode,\r
+                    size_t length,\r
+                    unsigned char iv[16],\r
+                    const unsigned char *input,\r
+                    unsigned char *output );\r
+#endif /* MBEDTLS_CIPHER_MODE_CBC */\r
+\r
+#if defined(MBEDTLS_CIPHER_MODE_XTS)\r
+/**\r
+ * \brief      This function performs an AES-XTS encryption or decryption\r
+ *             operation for an entire XTS data unit.\r
+ *\r
+ *             AES-XTS encrypts or decrypts blocks based on their location as\r
+ *             defined by a data unit number. The data unit number must be\r
+ *             provided by \p data_unit.\r
+ *\r
+ *             NIST SP 800-38E limits the maximum size of a data unit to 2^20\r
+ *             AES blocks. If the data unit is larger than this, this function\r
+ *             returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.\r
+ *\r
+ * \param ctx          The AES XTS context to use for AES XTS operations.\r
+ *                     It must be initialized and bound to a key.\r
+ * \param mode         The AES operation: #MBEDTLS_AES_ENCRYPT or\r
+ *                     #MBEDTLS_AES_DECRYPT.\r
+ * \param length       The length of a data unit in Bytes. This can be any\r
+ *                     length between 16 bytes and 2^24 bytes inclusive\r
+ *                     (between 1 and 2^20 block cipher blocks).\r
+ * \param data_unit    The address of the data unit encoded as an array of 16\r
+ *                     bytes in little-endian format. For disk encryption, this\r
+ *                     is typically the index of the block device sector that\r
+ *                     contains the data.\r
+ * \param input        The buffer holding the input data (which is an entire\r
+ *                     data unit). This function reads \p length Bytes from \p\r
+ *                     input.\r
+ * \param output       The buffer holding the output data (which is an entire\r
+ *                     data unit). This function writes \p length Bytes to \p\r
+ *                     output.\r
+ *\r
+ * \return             \c 0 on success.\r
+ * \return             #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is\r
+ *                     smaller than an AES block in size (16 Bytes) or if \p\r
+ *                     length is larger than 2^20 blocks (16 MiB).\r
+ */\r
+int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,\r
+                           int mode,\r
+                           size_t length,\r
+                           const unsigned char data_unit[16],\r
+                           const unsigned char *input,\r
+                           unsigned char *output );\r
+#endif /* MBEDTLS_CIPHER_MODE_XTS */\r
+\r
+#if defined(MBEDTLS_CIPHER_MODE_CFB)\r
+/**\r
+ * \brief This function performs an AES-CFB128 encryption or decryption\r
+ *        operation.\r
+ *\r
+ *        It performs the operation defined in the \p mode\r
+ *        parameter (encrypt or decrypt), on the input data buffer\r
+ *        defined in the \p input parameter.\r
+ *\r
+ *        For CFB, you must set up the context with mbedtls_aes_setkey_enc(),\r
+ *        regardless of whether you are performing an encryption or decryption\r
+ *        operation, that is, regardless of the \p mode parameter. This is\r
+ *        because CFB mode uses the same key schedule for encryption and\r
+ *        decryption.\r
+ *\r
+ * \note  Upon exit, the content of the IV is updated so that you can\r
+ *        call the same function again on the next\r
+ *        block(s) of data and get the same result as if it was\r
+ *        encrypted in one call. This allows a "streaming" usage.\r
+ *        If you need to retain the contents of the\r
+ *        IV, you must either save it manually or use the cipher\r
+ *        module instead.\r
+ *\r
+ *\r
+ * \param ctx      The AES context to use for encryption or decryption.\r
+ *                 It must be initialized and bound to a key.\r
+ * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or\r
+ *                 #MBEDTLS_AES_DECRYPT.\r
+ * \param length   The length of the input data in Bytes.\r
+ * \param iv_off   The offset in IV (updated after use).\r
+ *                 It must point to a valid \c size_t.\r
+ * \param iv       The initialization vector (updated after use).\r
+ *                 It must be a readable and writeable buffer of \c 16 Bytes.\r
+ * \param input    The buffer holding the input data.\r
+ *                 It must be readable and of size \p length Bytes.\r
+ * \param output   The buffer holding the output data.\r
+ *                 It must be writeable and of size \p length Bytes.\r
+ *\r
+ * \return         \c 0 on success.\r
+ */\r
+int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,\r
+                       int mode,\r
+                       size_t length,\r
+                       size_t *iv_off,\r
+                       unsigned char iv[16],\r
+                       const unsigned char *input,\r
+                       unsigned char *output );\r
+\r
+/**\r
+ * \brief This function performs an AES-CFB8 encryption or decryption\r
+ *        operation.\r
+ *\r
+ *        It performs the operation defined in the \p mode\r
+ *        parameter (encrypt/decrypt), on the input data buffer defined\r
+ *        in the \p input parameter.\r
+ *\r
+ *        Due to the nature of CFB, you must use the same key schedule for\r
+ *        both encryption and decryption operations. Therefore, you must\r
+ *        use the context initialized with mbedtls_aes_setkey_enc() for\r
+ *        both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.\r
+ *\r
+ * \note  Upon exit, the content of the IV is updated so that you can\r
+ *        call the same function again on the next\r
+ *        block(s) of data and get the same result as if it was\r
+ *        encrypted in one call. This allows a "streaming" usage.\r
+ *        If you need to retain the contents of the\r
+ *        IV, you should either save it manually or use the cipher\r
+ *        module instead.\r
+ *\r
+ *\r
+ * \param ctx      The AES context to use for encryption or decryption.\r
+ *                 It must be initialized and bound to a key.\r
+ * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or\r
+ *                 #MBEDTLS_AES_DECRYPT\r
+ * \param length   The length of the input data.\r
+ * \param iv       The initialization vector (updated after use).\r
+ *                 It must be a readable and writeable buffer of \c 16 Bytes.\r
+ * \param input    The buffer holding the input data.\r
+ *                 It must be readable and of size \p length Bytes.\r
+ * \param output   The buffer holding the output data.\r
+ *                 It must be writeable and of size \p length Bytes.\r
+ *\r
+ * \return         \c 0 on success.\r
+ */\r
+int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,\r
+                    int mode,\r
+                    size_t length,\r
+                    unsigned char iv[16],\r
+                    const unsigned char *input,\r
+                    unsigned char *output );\r
+#endif /*MBEDTLS_CIPHER_MODE_CFB */\r
+\r
+#if defined(MBEDTLS_CIPHER_MODE_OFB)\r
+/**\r
+ * \brief       This function performs an AES-OFB (Output Feedback Mode)\r
+ *              encryption or decryption operation.\r
+ *\r
+ *              For OFB, you must set up the context with\r
+ *              mbedtls_aes_setkey_enc(), regardless of whether you are\r
+ *              performing an encryption or decryption operation. This is\r
+ *              because OFB mode uses the same key schedule for encryption and\r
+ *              decryption.\r
+ *\r
+ *              The OFB operation is identical for encryption or decryption,\r
+ *              therefore no operation mode needs to be specified.\r
+ *\r
+ * \note        Upon exit, the content of iv, the Initialisation Vector, is\r
+ *              updated so that you can call the same function again on the next\r
+ *              block(s) of data and get the same result as if it was encrypted\r
+ *              in one call. This allows a "streaming" usage, by initialising\r
+ *              iv_off to 0 before the first call, and preserving its value\r
+ *              between calls.\r
+ *\r
+ *              For non-streaming use, the iv should be initialised on each call\r
+ *              to a unique value, and iv_off set to 0 on each call.\r
+ *\r
+ *              If you need to retain the contents of the initialisation vector,\r
+ *              you must either save it manually or use the cipher module\r
+ *              instead.\r
+ *\r
+ * \warning     For the OFB mode, the initialisation vector must be unique\r
+ *              every encryption operation. Reuse of an initialisation vector\r
+ *              will compromise security.\r
+ *\r
+ * \param ctx      The AES context to use for encryption or decryption.\r
+ *                 It must be initialized and bound to a key.\r
+ * \param length   The length of the input data.\r
+ * \param iv_off   The offset in IV (updated after use).\r
+ *                 It must point to a valid \c size_t.\r
+ * \param iv       The initialization vector (updated after use).\r
+ *                 It must be a readable and writeable buffer of \c 16 Bytes.\r
+ * \param input    The buffer holding the input data.\r
+ *                 It must be readable and of size \p length Bytes.\r
+ * \param output   The buffer holding the output data.\r
+ *                 It must be writeable and of size \p length Bytes.\r
+ *\r
+ * \return         \c 0 on success.\r
+ */\r
+int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,\r
+                       size_t length,\r
+                       size_t *iv_off,\r
+                       unsigned char iv[16],\r
+                       const unsigned char *input,\r
+                       unsigned char *output );\r
+\r
+#endif /* MBEDTLS_CIPHER_MODE_OFB */\r
+\r
+#if defined(MBEDTLS_CIPHER_MODE_CTR)\r
+/**\r
+ * \brief      This function performs an AES-CTR encryption or decryption\r
+ *             operation.\r
+ *\r
+ *             This function performs the operation defined in the \p mode\r
+ *             parameter (encrypt/decrypt), on the input data buffer\r
+ *             defined in the \p input parameter.\r
+ *\r
+ *             Due to the nature of CTR, you must use the same key schedule\r
+ *             for both encryption and decryption operations. Therefore, you\r
+ *             must use the context initialized with mbedtls_aes_setkey_enc()\r
+ *             for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.\r
+ *\r
+ * \warning    You must never reuse a nonce value with the same key. Doing so\r
+ *             would void the encryption for the two messages encrypted with\r
+ *             the same nonce and key.\r
+ *\r
+ *             There are two common strategies for managing nonces with CTR:\r
+ *\r
+ *             1. You can handle everything as a single message processed over\r
+ *             successive calls to this function. In that case, you want to\r
+ *             set \p nonce_counter and \p nc_off to 0 for the first call, and\r
+ *             then preserve the values of \p nonce_counter, \p nc_off and \p\r
+ *             stream_block across calls to this function as they will be\r
+ *             updated by this function.\r
+ *\r
+ *             With this strategy, you must not encrypt more than 2**128\r
+ *             blocks of data with the same key.\r
+ *\r
+ *             2. You can encrypt separate messages by dividing the \p\r
+ *             nonce_counter buffer in two areas: the first one used for a\r
+ *             per-message nonce, handled by yourself, and the second one\r
+ *             updated by this function internally.\r
+ *\r
+ *             For example, you might reserve the first 12 bytes for the\r
+ *             per-message nonce, and the last 4 bytes for internal use. In that\r
+ *             case, before calling this function on a new message you need to\r
+ *             set the first 12 bytes of \p nonce_counter to your chosen nonce\r
+ *             value, the last 4 to 0, and \p nc_off to 0 (which will cause \p\r
+ *             stream_block to be ignored). That way, you can encrypt at most\r
+ *             2**96 messages of up to 2**32 blocks each with the same key.\r
+ *\r
+ *             The per-message nonce (or information sufficient to reconstruct\r
+ *             it) needs to be communicated with the ciphertext and must be unique.\r
+ *             The recommended way to ensure uniqueness is to use a message\r
+ *             counter. An alternative is to generate random nonces, but this\r
+ *             limits the number of messages that can be securely encrypted:\r
+ *             for example, with 96-bit random nonces, you should not encrypt\r
+ *             more than 2**32 messages with the same key.\r
+ *\r
+ *             Note that for both stategies, sizes are measured in blocks and\r
+ *             that an AES block is 16 bytes.\r
+ *\r
+ * \warning    Upon return, \p stream_block contains sensitive data. Its\r
+ *             content must not be written to insecure storage and should be\r
+ *             securely discarded as soon as it's no longer needed.\r
+ *\r
+ * \param ctx              The AES context to use for encryption or decryption.\r
+ *                         It must be initialized and bound to a key.\r
+ * \param length           The length of the input data.\r
+ * \param nc_off           The offset in the current \p stream_block, for\r
+ *                         resuming within the current cipher stream. The\r
+ *                         offset pointer should be 0 at the start of a stream.\r
+ *                         It must point to a valid \c size_t.\r
+ * \param nonce_counter    The 128-bit nonce and counter.\r
+ *                         It must be a readable-writeable buffer of \c 16 Bytes.\r
+ * \param stream_block     The saved stream block for resuming. This is\r
+ *                         overwritten by the function.\r
+ *                         It must be a readable-writeable buffer of \c 16 Bytes.\r
+ * \param input            The buffer holding the input data.\r
+ *                         It must be readable and of size \p length Bytes.\r
+ * \param output           The buffer holding the output data.\r
+ *                         It must be writeable and of size \p length Bytes.\r
+ *\r
+ * \return                 \c 0 on success.\r
+ */\r
+int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,\r
+                       size_t length,\r
+                       size_t *nc_off,\r
+                       unsigned char nonce_counter[16],\r
+                       unsigned char stream_block[16],\r
+                       const unsigned char *input,\r
+                       unsigned char *output );\r
+#endif /* MBEDTLS_CIPHER_MODE_CTR */\r
+\r
+/**\r
+ * \brief           Internal AES block encryption function. This is only\r
+ *                  exposed to allow overriding it using\r
+ *                  \c MBEDTLS_AES_ENCRYPT_ALT.\r
+ *\r
+ * \param ctx       The AES context to use for encryption.\r
+ * \param input     The plaintext block.\r
+ * \param output    The output (ciphertext) block.\r
+ *\r
+ * \return          \c 0 on success.\r
+ */\r
+int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,\r
+                                  const unsigned char input[16],\r
+                                  unsigned char output[16] );\r
+\r
+/**\r
+ * \brief           Internal AES block decryption function. This is only\r
+ *                  exposed to allow overriding it using see\r
+ *                  \c MBEDTLS_AES_DECRYPT_ALT.\r
+ *\r
+ * \param ctx       The AES context to use for decryption.\r
+ * \param input     The ciphertext block.\r
+ * \param output    The output (plaintext) block.\r
+ *\r
+ * \return          \c 0 on success.\r
+ */\r
+int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,\r
+                                  const unsigned char input[16],\r
+                                  unsigned char output[16] );\r
+\r
+#if !defined(MBEDTLS_DEPRECATED_REMOVED)\r
+#if defined(MBEDTLS_DEPRECATED_WARNING)\r
+#define MBEDTLS_DEPRECATED      __attribute__((deprecated))\r
+#else\r
+#define MBEDTLS_DEPRECATED\r
+#endif\r
+/**\r
+ * \brief           Deprecated internal AES block encryption function\r
+ *                  without return value.\r
+ *\r
+ * \deprecated      Superseded by mbedtls_internal_aes_encrypt()\r
+ *\r
+ * \param ctx       The AES context to use for encryption.\r
+ * \param input     Plaintext block.\r
+ * \param output    Output (ciphertext) block.\r
+ */\r
+MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,\r
+                                             const unsigned char input[16],\r
+                                             unsigned char output[16] );\r
+\r
+/**\r
+ * \brief           Deprecated internal AES block decryption function\r
+ *                  without return value.\r
+ *\r
+ * \deprecated      Superseded by mbedtls_internal_aes_decrypt()\r
+ *\r
+ * \param ctx       The AES context to use for decryption.\r
+ * \param input     Ciphertext block.\r
+ * \param output    Output (plaintext) block.\r
+ */\r
+MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,\r
+                                             const unsigned char input[16],\r
+                                             unsigned char output[16] );\r
+\r
+#undef MBEDTLS_DEPRECATED\r
+#endif /* !MBEDTLS_DEPRECATED_REMOVED */\r
+\r
+\r
+#if defined(MBEDTLS_SELF_TEST)\r
+/**\r
+ * \brief          Checkup routine.\r
+ *\r
+ * \return         \c 0 on success.\r
+ * \return         \c 1 on failure.\r
+ */\r
+int mbedtls_aes_self_test( int verbose );\r
+\r
+#endif /* MBEDTLS_SELF_TEST */\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+#endif /* aes.h */\r