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