Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file aes.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Rose Zadik | 5ad7aea | 2018-03-26 12:00:09 +0100 | [diff] [blame] | 4 | * \brief This file contains AES definitions and functions. |
| 5 | * |
| 6 | * The Advanced Encryption Standard (AES) specifies a FIPS-approved |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 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>. |
Jaeden Amero | f167deb | 2018-05-30 19:20:48 +0100 | [diff] [blame] | 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>. |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 21 | */ |
Rose Zadik | 5ad7aea | 2018-03-26 12:00:09 +0100 | [diff] [blame] | 22 | |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 23 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 24 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame^] | 25 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | */ |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #ifndef MBEDTLS_AES_H |
| 29 | #define MBEDTLS_AES_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 30 | #include "mbedtls/private_access.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 32 | #include "mbedtls/build_info.h" |
Mateusz Starzyk | e35f8f6 | 2021-08-04 15:38:09 +0200 | [diff] [blame] | 33 | #include "mbedtls/platform_util.h" |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 34 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 35 | #include <stddef.h> |
Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 36 | #include <stdint.h> |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 37 | |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 38 | /* padlock.c and aesni.c rely on these values! */ |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 39 | #define MBEDTLS_AES_ENCRYPT 1 /**< AES encryption. */ |
| 40 | #define MBEDTLS_AES_DECRYPT 0 /**< AES decryption. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 41 | |
Andres Amaya Garcia | c538064 | 2017-11-28 19:57:51 +0000 | [diff] [blame] | 42 | /* Error codes in range 0x0020-0x0022 */ |
Gilles Peskine | d297157 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 43 | /** Invalid key length. */ |
| 44 | #define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 |
| 45 | /** Invalid data input length. */ |
| 46 | #define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 47 | |
Mohammad Azim Khan | e5b5bd7 | 2017-11-24 10:52:51 +0000 | [diff] [blame] | 48 | /* Error codes in range 0x0021-0x0025 */ |
Gilles Peskine | d297157 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 49 | /** Invalid input data. */ |
| 50 | #define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0021 |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 51 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 52 | #ifdef __cplusplus |
| 53 | extern "C" { |
| 54 | #endif |
| 55 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 56 | #if !defined(MBEDTLS_AES_ALT) |
| 57 | // Regular implementation |
| 58 | // |
| 59 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 60 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 61 | * \brief The AES context-type definition. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 63 | typedef struct mbedtls_aes_context { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 64 | int MBEDTLS_PRIVATE(nr); /*!< The number of rounds. */ |
Werner Lewis | 6d71944 | 2022-06-13 12:28:07 +0100 | [diff] [blame] | 65 | size_t MBEDTLS_PRIVATE(rk_offset); /*!< The offset in array elements to AES |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | round keys in the buffer. */ |
Arto Kinnunen | b1c626b | 2023-04-14 17:21:22 +0800 | [diff] [blame] | 67 | #if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH) && !defined(MBEDTLS_PADLOCK_C) |
Yanray Wang | 8b9877b | 2023-05-05 14:46:04 +0800 | [diff] [blame] | 68 | uint32_t MBEDTLS_PRIVATE(buf)[44]; /*!< Aligned data buffer to hold |
Yanray Wang | ab4fb0d | 2023-05-10 10:06:11 +0800 | [diff] [blame] | 69 | 10 round keys for 128-bit case. */ |
Arto Kinnunen | b1c626b | 2023-04-14 17:21:22 +0800 | [diff] [blame] | 70 | #else |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 71 | uint32_t MBEDTLS_PRIVATE(buf)[68]; /*!< Unaligned data buffer. This buffer can |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | hold 32 extra Bytes, which can be used for |
| 73 | one of the following purposes: |
| 74 | <ul><li>Alignment if VIA padlock is |
| 75 | used.</li> |
| 76 | <li>Simplifying key expansion in the 256-bit |
| 77 | case by generating an extra round key. |
| 78 | </li></ul> */ |
Arto Kinnunen | b1c626b | 2023-04-14 17:21:22 +0800 | [diff] [blame] | 79 | #endif /* MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH && !MBEDTLS_PADLOCK_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 80 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | mbedtls_aes_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 82 | |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 83 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
| 84 | /** |
| 85 | * \brief The AES XTS context-type definition. |
| 86 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | typedef struct mbedtls_aes_xts_context { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 88 | mbedtls_aes_context MBEDTLS_PRIVATE(crypt); /*!< The AES context to use for AES block |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 89 | encryption or decryption. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 90 | mbedtls_aes_context MBEDTLS_PRIVATE(tweak); /*!< The AES context used for tweak |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | computation. */ |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 92 | } mbedtls_aes_xts_context; |
| 93 | #endif /* MBEDTLS_CIPHER_MODE_XTS */ |
| 94 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 95 | #else /* MBEDTLS_AES_ALT */ |
| 96 | #include "aes_alt.h" |
| 97 | #endif /* MBEDTLS_AES_ALT */ |
| 98 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 99 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 100 | * \brief This function initializes the specified AES context. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 101 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 102 | * It must be the first API called before using |
| 103 | * the context. |
| 104 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 105 | * \param ctx The AES context to initialize. This must not be \c NULL. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 106 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | void mbedtls_aes_init(mbedtls_aes_context *ctx); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 108 | |
| 109 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 110 | * \brief This function releases and clears the specified AES context. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 111 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 112 | * \param ctx The AES context to clear. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 113 | * If this is \c NULL, this function does nothing. |
| 114 | * Otherwise, the context must have been at least initialized. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 115 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | void mbedtls_aes_free(mbedtls_aes_context *ctx); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 117 | |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 118 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
| 119 | /** |
| 120 | * \brief This function initializes the specified AES XTS context. |
| 121 | * |
| 122 | * It must be the first API called before using |
| 123 | * the context. |
| 124 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 125 | * \param ctx The AES XTS context to initialize. This must not be \c NULL. |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 126 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx); |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * \brief This function releases and clears the specified AES XTS context. |
| 131 | * |
| 132 | * \param ctx The AES XTS context to clear. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 133 | * If this is \c NULL, this function does nothing. |
| 134 | * Otherwise, the context must have been at least initialized. |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 135 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx); |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 137 | #endif /* MBEDTLS_CIPHER_MODE_XTS */ |
| 138 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 139 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 140 | * \brief This function sets the encryption key. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 142 | * \param ctx The AES context to which the key should be bound. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 143 | * It must be initialized. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 144 | * \param key The encryption key. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 145 | * This must be a readable buffer of size \p keybits bits. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 146 | * \param keybits The size of data passed in bits. Valid options are: |
| 147 | * <ul><li>128 bits</li> |
| 148 | * <li>192 bits</li> |
| 149 | * <li>256 bits</li></ul> |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 150 | * |
Rose Zadik | 5ad7aea | 2018-03-26 12:00:09 +0100 | [diff] [blame] | 151 | * \return \c 0 on success. |
Rose Zadik | 819d13d | 2018-04-16 09:35:15 +0100 | [diff] [blame] | 152 | * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 153 | */ |
Gilles Peskine | e41803a | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 154 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key, |
| 156 | unsigned int keybits); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 157 | |
| 158 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 159 | * \brief This function sets the decryption key. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 161 | * \param ctx The AES context to which the key should be bound. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 162 | * It must be initialized. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 163 | * \param key The decryption key. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 164 | * This must be a readable buffer of size \p keybits bits. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 165 | * \param keybits The size of data passed. Valid options are: |
| 166 | * <ul><li>128 bits</li> |
| 167 | * <li>192 bits</li> |
| 168 | * <li>256 bits</li></ul> |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 169 | * |
Rose Zadik | 5ad7aea | 2018-03-26 12:00:09 +0100 | [diff] [blame] | 170 | * \return \c 0 on success. |
| 171 | * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 172 | */ |
Gilles Peskine | e41803a | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 173 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key, |
| 175 | unsigned int keybits); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 177 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
| 178 | /** |
| 179 | * \brief This function prepares an XTS context for encryption and |
| 180 | * sets the encryption key. |
| 181 | * |
| 182 | * \param ctx The AES XTS context to which the key should be bound. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 183 | * It must be initialized. |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 184 | * \param key The encryption key. This is comprised of the XTS key1 |
| 185 | * concatenated with the XTS key2. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 186 | * This must be a readable buffer of size \p keybits bits. |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 187 | * \param keybits The size of \p key passed in bits. Valid options are: |
| 188 | * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li> |
| 189 | * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul> |
| 190 | * |
| 191 | * \return \c 0 on success. |
| 192 | * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure. |
| 193 | */ |
Gilles Peskine | e41803a | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 194 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx, |
| 196 | const unsigned char *key, |
| 197 | unsigned int keybits); |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * \brief This function prepares an XTS context for decryption and |
| 201 | * sets the decryption key. |
| 202 | * |
| 203 | * \param ctx The AES XTS context to which the key should be bound. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 204 | * It must be initialized. |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 205 | * \param key The decryption key. This is comprised of the XTS key1 |
| 206 | * concatenated with the XTS key2. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 207 | * This must be a readable buffer of size \p keybits bits. |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 208 | * \param keybits The size of \p key passed in bits. Valid options are: |
| 209 | * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li> |
| 210 | * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul> |
| 211 | * |
| 212 | * \return \c 0 on success. |
| 213 | * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure. |
| 214 | */ |
Gilles Peskine | e41803a | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 215 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 216 | int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx, |
| 217 | const unsigned char *key, |
| 218 | unsigned int keybits); |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 219 | #endif /* MBEDTLS_CIPHER_MODE_XTS */ |
| 220 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 221 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 222 | * \brief This function performs an AES single-block encryption or |
| 223 | * decryption operation. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 224 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 225 | * It performs the operation defined in the \p mode parameter |
| 226 | * (encrypt or decrypt), on the input data buffer defined in |
| 227 | * the \p input parameter. |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 228 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 229 | * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or |
| 230 | * mbedtls_aes_setkey_dec() must be called before the first |
| 231 | * call to this API with the same context. |
| 232 | * |
| 233 | * \param ctx The AES context to use for encryption or decryption. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 234 | * It must be initialized and bound to a key. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 235 | * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or |
| 236 | * #MBEDTLS_AES_DECRYPT. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 237 | * \param input The buffer holding the input data. |
| 238 | * It must be readable and at least \c 16 Bytes long. |
| 239 | * \param output The buffer where the output data will be written. |
| 240 | * It must be writeable and at least \c 16 Bytes long. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 241 | |
| 242 | * \return \c 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 243 | */ |
Gilles Peskine | e41803a | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 244 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 245 | int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx, |
| 246 | int mode, |
| 247 | const unsigned char input[16], |
| 248 | unsigned char output[16]); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 249 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 251 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 252 | * \brief This function performs an AES-CBC encryption or decryption operation |
| 253 | * on full blocks. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 254 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 255 | * It performs the operation defined in the \p mode |
| 256 | * parameter (encrypt/decrypt), on the input data buffer defined in |
| 257 | * the \p input parameter. |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 258 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 259 | * It can be called as many times as needed, until all the input |
| 260 | * data is processed. mbedtls_aes_init(), and either |
| 261 | * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called |
| 262 | * before the first call to this API with the same context. |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 263 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 264 | * \note This function operates on full blocks, that is, the input size |
| 265 | * must be a multiple of the AES block size of \c 16 Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 266 | * |
| 267 | * \note Upon exit, the content of the IV is updated so that you can |
| 268 | * call the same function again on the next |
| 269 | * block(s) of data and get the same result as if it was |
| 270 | * encrypted in one call. This allows a "streaming" usage. |
| 271 | * If you need to retain the contents of the IV, you should |
| 272 | * either save it manually or use the cipher module instead. |
| 273 | * |
| 274 | * |
| 275 | * \param ctx The AES context to use for encryption or decryption. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 276 | * It must be initialized and bound to a key. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 277 | * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or |
| 278 | * #MBEDTLS_AES_DECRYPT. |
| 279 | * \param length The length of the input data in Bytes. This must be a |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 280 | * multiple of the block size (\c 16 Bytes). |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 281 | * \param iv Initialization vector (updated after use). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 282 | * It must be a readable and writeable buffer of \c 16 Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 283 | * \param input The buffer holding the input data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 284 | * It must be readable and of size \p length Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 285 | * \param output The buffer holding the output data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 286 | * It must be writeable and of size \p length Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 287 | * |
Rose Zadik | 5ad7aea | 2018-03-26 12:00:09 +0100 | [diff] [blame] | 288 | * \return \c 0 on success. |
| 289 | * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 290 | * on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 291 | */ |
Gilles Peskine | e41803a | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 292 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx, |
| 294 | int mode, |
| 295 | size_t length, |
| 296 | unsigned char iv[16], |
| 297 | const unsigned char *input, |
| 298 | unsigned char *output); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 300 | |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 301 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
| 302 | /** |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 303 | * \brief This function performs an AES-XTS encryption or decryption |
| 304 | * operation for an entire XTS data unit. |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 305 | * |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 306 | * AES-XTS encrypts or decrypts blocks based on their location as |
| 307 | * defined by a data unit number. The data unit number must be |
Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 308 | * provided by \p data_unit. |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 309 | * |
Jaeden Amero | 0a8b020 | 2018-05-30 15:36:06 +0100 | [diff] [blame] | 310 | * NIST SP 800-38E limits the maximum size of a data unit to 2^20 |
| 311 | * AES blocks. If the data unit is larger than this, this function |
| 312 | * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH. |
| 313 | * |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 314 | * \param ctx The AES XTS context to use for AES XTS operations. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 315 | * It must be initialized and bound to a key. |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 316 | * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or |
| 317 | * #MBEDTLS_AES_DECRYPT. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 318 | * \param length The length of a data unit in Bytes. This can be any |
Jaeden Amero | 0a8b020 | 2018-05-30 15:36:06 +0100 | [diff] [blame] | 319 | * length between 16 bytes and 2^24 bytes inclusive |
| 320 | * (between 1 and 2^20 block cipher blocks). |
Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 321 | * \param data_unit The address of the data unit encoded as an array of 16 |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 322 | * bytes in little-endian format. For disk encryption, this |
| 323 | * is typically the index of the block device sector that |
| 324 | * contains the data. |
| 325 | * \param input The buffer holding the input data (which is an entire |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 326 | * data unit). This function reads \p length Bytes from \p |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 327 | * input. |
| 328 | * \param output The buffer holding the output data (which is an entire |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 329 | * data unit). This function writes \p length Bytes to \p |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 330 | * output. |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 331 | * |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 332 | * \return \c 0 on success. |
| 333 | * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 334 | * smaller than an AES block in size (16 Bytes) or if \p |
Jaeden Amero | 0a8b020 | 2018-05-30 15:36:06 +0100 | [diff] [blame] | 335 | * length is larger than 2^20 blocks (16 MiB). |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 336 | */ |
Gilles Peskine | e41803a | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 337 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 338 | int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx, |
| 339 | int mode, |
| 340 | size_t length, |
| 341 | const unsigned char data_unit[16], |
| 342 | const unsigned char *input, |
| 343 | unsigned char *output); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 344 | #endif /* MBEDTLS_CIPHER_MODE_XTS */ |
| 345 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 346 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 347 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 348 | * \brief This function performs an AES-CFB128 encryption or decryption |
| 349 | * operation. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 350 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 351 | * It performs the operation defined in the \p mode |
| 352 | * parameter (encrypt or decrypt), on the input data buffer |
| 353 | * defined in the \p input parameter. |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 354 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 355 | * For CFB, you must set up the context with mbedtls_aes_setkey_enc(), |
| 356 | * regardless of whether you are performing an encryption or decryption |
| 357 | * operation, that is, regardless of the \p mode parameter. This is |
| 358 | * because CFB mode uses the same key schedule for encryption and |
| 359 | * decryption. |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 360 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 361 | * \note Upon exit, the content of the IV is updated so that you can |
| 362 | * call the same function again on the next |
| 363 | * block(s) of data and get the same result as if it was |
| 364 | * encrypted in one call. This allows a "streaming" usage. |
| 365 | * If you need to retain the contents of the |
| 366 | * IV, you must either save it manually or use the cipher |
| 367 | * module instead. |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 368 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 369 | * |
| 370 | * \param ctx The AES context to use for encryption or decryption. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 371 | * It must be initialized and bound to a key. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 372 | * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or |
| 373 | * #MBEDTLS_AES_DECRYPT. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 374 | * \param length The length of the input data in Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 375 | * \param iv_off The offset in IV (updated after use). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 376 | * It must point to a valid \c size_t. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 377 | * \param iv The initialization vector (updated after use). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 378 | * It must be a readable and writeable buffer of \c 16 Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 379 | * \param input The buffer holding the input data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 380 | * It must be readable and of size \p length Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 381 | * \param output The buffer holding the output data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 382 | * It must be writeable and of size \p length Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 383 | * |
| 384 | * \return \c 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 385 | */ |
Gilles Peskine | e41803a | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 386 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 387 | int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx, |
| 388 | int mode, |
| 389 | size_t length, |
| 390 | size_t *iv_off, |
| 391 | unsigned char iv[16], |
| 392 | const unsigned char *input, |
| 393 | unsigned char *output); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 394 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 395 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 396 | * \brief This function performs an AES-CFB8 encryption or decryption |
| 397 | * operation. |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 398 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 399 | * It performs the operation defined in the \p mode |
| 400 | * parameter (encrypt/decrypt), on the input data buffer defined |
| 401 | * in the \p input parameter. |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 402 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 403 | * Due to the nature of CFB, you must use the same key schedule for |
| 404 | * both encryption and decryption operations. Therefore, you must |
| 405 | * use the context initialized with mbedtls_aes_setkey_enc() for |
| 406 | * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT. |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 407 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 408 | * \note Upon exit, the content of the IV is updated so that you can |
| 409 | * call the same function again on the next |
| 410 | * block(s) of data and get the same result as if it was |
| 411 | * encrypted in one call. This allows a "streaming" usage. |
| 412 | * If you need to retain the contents of the |
| 413 | * IV, you should either save it manually or use the cipher |
| 414 | * module instead. |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 415 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 416 | * |
| 417 | * \param ctx The AES context to use for encryption or decryption. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 418 | * It must be initialized and bound to a key. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 419 | * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or |
| 420 | * #MBEDTLS_AES_DECRYPT |
| 421 | * \param length The length of the input data. |
| 422 | * \param iv The initialization vector (updated after use). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 423 | * It must be a readable and writeable buffer of \c 16 Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 424 | * \param input The buffer holding the input data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 425 | * It must be readable and of size \p length Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 426 | * \param output The buffer holding the output data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 427 | * It must be writeable and of size \p length Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 428 | * |
| 429 | * \return \c 0 on success. |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 430 | */ |
Gilles Peskine | e41803a | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 431 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx, |
| 433 | int mode, |
| 434 | size_t length, |
| 435 | unsigned char iv[16], |
| 436 | const unsigned char *input, |
| 437 | unsigned char *output); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 438 | #endif /*MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 439 | |
Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 440 | #if defined(MBEDTLS_CIPHER_MODE_OFB) |
| 441 | /** |
Simon Butcher | 5db1362 | 2018-06-04 22:11:25 +0100 | [diff] [blame] | 442 | * \brief This function performs an AES-OFB (Output Feedback Mode) |
| 443 | * encryption or decryption operation. |
Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 444 | * |
Simon Butcher | 5db1362 | 2018-06-04 22:11:25 +0100 | [diff] [blame] | 445 | * For OFB, you must set up the context with |
| 446 | * mbedtls_aes_setkey_enc(), regardless of whether you are |
| 447 | * performing an encryption or decryption operation. This is |
| 448 | * because OFB mode uses the same key schedule for encryption and |
| 449 | * decryption. |
Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 450 | * |
Simon Butcher | 5db1362 | 2018-06-04 22:11:25 +0100 | [diff] [blame] | 451 | * The OFB operation is identical for encryption or decryption, |
| 452 | * therefore no operation mode needs to be specified. |
Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 453 | * |
Simon Butcher | 5db1362 | 2018-06-04 22:11:25 +0100 | [diff] [blame] | 454 | * \note Upon exit, the content of iv, the Initialisation Vector, is |
| 455 | * updated so that you can call the same function again on the next |
| 456 | * block(s) of data and get the same result as if it was encrypted |
| 457 | * in one call. This allows a "streaming" usage, by initialising |
| 458 | * iv_off to 0 before the first call, and preserving its value |
| 459 | * between calls. |
Simon Butcher | 968646c | 2018-06-02 18:27:04 +0100 | [diff] [blame] | 460 | * |
Simon Butcher | 5db1362 | 2018-06-04 22:11:25 +0100 | [diff] [blame] | 461 | * For non-streaming use, the iv should be initialised on each call |
| 462 | * to a unique value, and iv_off set to 0 on each call. |
Simon Butcher | 968646c | 2018-06-02 18:27:04 +0100 | [diff] [blame] | 463 | * |
Simon Butcher | 5db1362 | 2018-06-04 22:11:25 +0100 | [diff] [blame] | 464 | * If you need to retain the contents of the initialisation vector, |
| 465 | * you must either save it manually or use the cipher module |
| 466 | * instead. |
Simon Butcher | 968646c | 2018-06-02 18:27:04 +0100 | [diff] [blame] | 467 | * |
Jaeden Amero | cb2c935 | 2018-06-08 10:34:08 +0100 | [diff] [blame] | 468 | * \warning For the OFB mode, the initialisation vector must be unique |
| 469 | * every encryption operation. Reuse of an initialisation vector |
| 470 | * will compromise security. |
Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 471 | * |
| 472 | * \param ctx The AES context to use for encryption or decryption. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 473 | * It must be initialized and bound to a key. |
Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 474 | * \param length The length of the input data. |
| 475 | * \param iv_off The offset in IV (updated after use). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 476 | * It must point to a valid \c size_t. |
Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 477 | * \param iv The initialization vector (updated after use). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 478 | * It must be a readable and writeable buffer of \c 16 Bytes. |
Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 479 | * \param input The buffer holding the input data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 480 | * It must be readable and of size \p length Bytes. |
Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 481 | * \param output The buffer holding the output data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 482 | * It must be writeable and of size \p length Bytes. |
Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 483 | * |
| 484 | * \return \c 0 on success. |
| 485 | */ |
Gilles Peskine | e41803a | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 486 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 487 | int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx, |
| 488 | size_t length, |
| 489 | size_t *iv_off, |
| 490 | unsigned char iv[16], |
| 491 | const unsigned char *input, |
| 492 | unsigned char *output); |
Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 493 | |
| 494 | #endif /* MBEDTLS_CIPHER_MODE_OFB */ |
| 495 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 496 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 497 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 498 | * \brief This function performs an AES-CTR encryption or decryption |
| 499 | * operation. |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 500 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 501 | * Due to the nature of CTR, you must use the same key schedule |
| 502 | * for both encryption and decryption operations. Therefore, you |
| 503 | * must use the context initialized with mbedtls_aes_setkey_enc() |
| 504 | * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT. |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 505 | * |
Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 506 | * \warning You must never reuse a nonce value with the same key. Doing so |
| 507 | * would void the encryption for the two messages encrypted with |
| 508 | * the same nonce and key. |
| 509 | * |
| 510 | * There are two common strategies for managing nonces with CTR: |
| 511 | * |
Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 512 | * 1. You can handle everything as a single message processed over |
| 513 | * successive calls to this function. In that case, you want to |
| 514 | * set \p nonce_counter and \p nc_off to 0 for the first call, and |
| 515 | * then preserve the values of \p nonce_counter, \p nc_off and \p |
| 516 | * stream_block across calls to this function as they will be |
| 517 | * updated by this function. |
Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 518 | * |
Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 519 | * With this strategy, you must not encrypt more than 2**128 |
| 520 | * blocks of data with the same key. |
| 521 | * |
| 522 | * 2. You can encrypt separate messages by dividing the \p |
| 523 | * nonce_counter buffer in two areas: the first one used for a |
| 524 | * per-message nonce, handled by yourself, and the second one |
| 525 | * updated by this function internally. |
| 526 | * |
| 527 | * For example, you might reserve the first 12 bytes for the |
| 528 | * per-message nonce, and the last 4 bytes for internal use. In that |
| 529 | * case, before calling this function on a new message you need to |
| 530 | * set the first 12 bytes of \p nonce_counter to your chosen nonce |
| 531 | * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p |
| 532 | * stream_block to be ignored). That way, you can encrypt at most |
| 533 | * 2**96 messages of up to 2**32 blocks each with the same key. |
| 534 | * |
| 535 | * The per-message nonce (or information sufficient to reconstruct |
| 536 | * it) needs to be communicated with the ciphertext and must be unique. |
| 537 | * The recommended way to ensure uniqueness is to use a message |
| 538 | * counter. An alternative is to generate random nonces, but this |
| 539 | * limits the number of messages that can be securely encrypted: |
| 540 | * for example, with 96-bit random nonces, you should not encrypt |
| 541 | * more than 2**32 messages with the same key. |
| 542 | * |
Tom Cosgrove | 1e21144 | 2022-05-26 11:51:00 +0100 | [diff] [blame] | 543 | * Note that for both strategies, sizes are measured in blocks and |
Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 544 | * that an AES block is 16 bytes. |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 545 | * |
Manuel Pégourié-Gonnard | fa0c47d | 2018-05-24 19:02:06 +0200 | [diff] [blame] | 546 | * \warning Upon return, \p stream_block contains sensitive data. Its |
| 547 | * content must not be written to insecure storage and should be |
| 548 | * securely discarded as soon as it's no longer needed. |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 549 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 550 | * \param ctx The AES context to use for encryption or decryption. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 551 | * It must be initialized and bound to a key. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 552 | * \param length The length of the input data. |
| 553 | * \param nc_off The offset in the current \p stream_block, for |
| 554 | * resuming within the current cipher stream. The |
| 555 | * offset pointer should be 0 at the start of a stream. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 556 | * It must point to a valid \c size_t. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 557 | * \param nonce_counter The 128-bit nonce and counter. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 558 | * It must be a readable-writeable buffer of \c 16 Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 559 | * \param stream_block The saved stream block for resuming. This is |
| 560 | * overwritten by the function. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 561 | * It must be a readable-writeable buffer of \c 16 Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 562 | * \param input The buffer holding the input data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 563 | * It must be readable and of size \p length Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 564 | * \param output The buffer holding the output data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 565 | * It must be writeable and of size \p length Bytes. |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 566 | * |
Rose Zadik | 5ad7aea | 2018-03-26 12:00:09 +0100 | [diff] [blame] | 567 | * \return \c 0 on success. |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 568 | */ |
Gilles Peskine | e41803a | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 569 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 570 | int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx, |
| 571 | size_t length, |
| 572 | size_t *nc_off, |
| 573 | unsigned char nonce_counter[16], |
| 574 | unsigned char stream_block[16], |
| 575 | const unsigned char *input, |
| 576 | unsigned char *output); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 577 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 578 | |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 579 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 580 | * \brief Internal AES block encryption function. This is only |
| 581 | * exposed to allow overriding it using |
| 582 | * \c MBEDTLS_AES_ENCRYPT_ALT. |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 583 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 584 | * \param ctx The AES context to use for encryption. |
| 585 | * \param input The plaintext block. |
| 586 | * \param output The output (ciphertext) block. |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 587 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 588 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 589 | */ |
Gilles Peskine | e41803a | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 590 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 591 | int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx, |
| 592 | const unsigned char input[16], |
| 593 | unsigned char output[16]); |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 594 | |
| 595 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 596 | * \brief Internal AES block decryption function. This is only |
| 597 | * exposed to allow overriding it using see |
| 598 | * \c MBEDTLS_AES_DECRYPT_ALT. |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 599 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 600 | * \param ctx The AES context to use for decryption. |
| 601 | * \param input The ciphertext block. |
| 602 | * \param output The output (plaintext) block. |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 603 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 604 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 605 | */ |
Gilles Peskine | e41803a | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 606 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 607 | int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx, |
| 608 | const unsigned char input[16], |
| 609 | unsigned char output[16]); |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 610 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 611 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 612 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 613 | * \brief Checkup routine. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 614 | * |
Rose Zadik | 5ad7aea | 2018-03-26 12:00:09 +0100 | [diff] [blame] | 615 | * \return \c 0 on success. |
| 616 | * \return \c 1 on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 617 | */ |
Gilles Peskine | e41803a | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 618 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 619 | int mbedtls_aes_self_test(int verbose); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 620 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 621 | #endif /* MBEDTLS_SELF_TEST */ |
| 622 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 623 | #ifdef __cplusplus |
| 624 | } |
| 625 | #endif |
| 626 | |
| 627 | #endif /* aes.h */ |