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