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