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