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