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