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