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 | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 4 | * \brief The Advanced Encryption Standard (AES) specifies a FIPS-approved |
| 5 | * cryptographic algorithm that can be used to protect electronic |
| 6 | * data. |
| 7 | * |
| 8 | * The AES algorithm is a symmetric block cipher that can |
| 9 | * encrypt and decrypt information. For more information, see |
| 10 | * <em>FIPS Publication 197: Advanced Encryption Standard</em> and |
| 11 | * <em>ISO/IEC 18033-2:2006: Information technology -- Security |
| 12 | * techniques -- Encryption algorithms -- Part 2: Asymmetric |
| 13 | * ciphers</em>. |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 14 | */ |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 15 | /* |
| 16 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved. |
| 17 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 18 | * |
| 19 | * This file is provided under the Apache License 2.0, or the |
| 20 | * GNU General Public License v2.0 or later. |
| 21 | * |
| 22 | * ********** |
| 23 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 24 | * |
| 25 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 26 | * not use this file except in compliance with the License. |
| 27 | * You may obtain a copy of the License at |
| 28 | * |
| 29 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 30 | * |
| 31 | * Unless required by applicable law or agreed to in writing, software |
| 32 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 33 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 34 | * See the License for the specific language governing permissions and |
| 35 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 36 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 37 | * ********** |
| 38 | * |
| 39 | * ********** |
| 40 | * GNU General Public License v2.0 or later: |
| 41 | * |
| 42 | * This program is free software; you can redistribute it and/or modify |
| 43 | * it under the terms of the GNU General Public License as published by |
| 44 | * the Free Software Foundation; either version 2 of the License, or |
| 45 | * (at your option) any later version. |
| 46 | * |
| 47 | * This program is distributed in the hope that it will be useful, |
| 48 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 49 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 50 | * GNU General Public License for more details. |
| 51 | * |
| 52 | * You should have received a copy of the GNU General Public License along |
| 53 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 54 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 55 | * |
| 56 | * ********** |
| 57 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 58 | * This file is part of Mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | */ |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 60 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | #ifndef MBEDTLS_AES_H |
| 62 | #define MBEDTLS_AES_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 63 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 65 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 66 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 68 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 69 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 70 | #include <stddef.h> |
Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 71 | #include <stdint.h> |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 72 | |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 73 | /* padlock.c and aesni.c rely on these values! */ |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 74 | #define MBEDTLS_AES_ENCRYPT 1 /**< AES encryption. */ |
| 75 | #define MBEDTLS_AES_DECRYPT 0 /**< AES decryption. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | |
Andres Amaya Garcia | c538064 | 2017-11-28 19:57:51 +0000 | [diff] [blame] | 77 | /* Error codes in range 0x0020-0x0022 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | #define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */ |
| 79 | #define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */ |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 80 | |
Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame] | 81 | /* Error codes in range 0x0023-0x0025 */ |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 82 | #define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE -0x0023 /**< Feature not available. For example, an unsupported AES key size. */ |
Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame] | 83 | #define MBEDTLS_ERR_AES_HW_ACCEL_FAILED -0x0025 /**< AES hardware accelerator failed. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 84 | |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 85 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ |
| 86 | !defined(inline) && !defined(__cplusplus) |
| 87 | #define inline __inline |
| 88 | #endif |
| 89 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | #if !defined(MBEDTLS_AES_ALT) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 91 | // Regular implementation |
| 92 | // |
| 93 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 94 | #ifdef __cplusplus |
| 95 | extern "C" { |
| 96 | #endif |
| 97 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 98 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 99 | * \brief The AES context-type definition. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 | */ |
| 101 | typedef struct |
| 102 | { |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 103 | int nr; /*!< The number of rounds. */ |
| 104 | uint32_t *rk; /*!< AES round keys. */ |
| 105 | uint32_t buf[68]; /*!< Unaligned data buffer. This buffer can |
| 106 | hold 32 extra Bytes, which can be used for |
| 107 | one of the following purposes: |
| 108 | <ul><li>Alignment if VIA padlock is |
| 109 | used.</li> |
| 110 | <li>Simplifying key expansion in the 256-bit |
| 111 | case by generating an extra round key. |
| 112 | </li></ul> */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 114 | mbedtls_aes_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 115 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 117 | * \brief This function initializes the specified AES context. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 118 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 119 | * It must be the first API called before using |
| 120 | * the context. |
| 121 | * |
| 122 | * \param ctx The AES context to initialize. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 123 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | void mbedtls_aes_init( mbedtls_aes_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 125 | |
| 126 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 127 | * \brief This function releases and clears the specified AES context. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 128 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 129 | * \param ctx The AES context to clear. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 130 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | void mbedtls_aes_free( mbedtls_aes_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 132 | |
| 133 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 134 | * \brief This function sets the encryption key. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 135 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 136 | * \param ctx The AES context to which the key should be bound. |
| 137 | * \param key The encryption key. |
| 138 | * \param keybits The size of data passed in bits. Valid options are: |
| 139 | * <ul><li>128 bits</li> |
| 140 | * <li>192 bits</li> |
| 141 | * <li>256 bits</li></ul> |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 142 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 143 | * \return \c 0 on success or #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH |
| 144 | * on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 145 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 146 | 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] | 147 | unsigned int keybits ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | |
| 149 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 150 | * \brief This function sets the decryption key. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 151 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 152 | * \param ctx The AES context to which the key should be bound. |
| 153 | * \param key The decryption key. |
| 154 | * \param keybits The size of data passed. Valid options are: |
| 155 | * <ul><li>128 bits</li> |
| 156 | * <li>192 bits</li> |
| 157 | * <li>256 bits</li></ul> |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 158 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 159 | * \return \c 0 on success, or #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | 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] | 162 | unsigned int keybits ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | |
| 164 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 165 | * \brief This function performs an AES single-block encryption or |
| 166 | * decryption operation. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 167 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 168 | * It performs the operation defined in the \p mode parameter |
| 169 | * (encrypt or decrypt), on the input data buffer defined in |
| 170 | * the \p input parameter. |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 171 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 172 | * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or |
| 173 | * mbedtls_aes_setkey_dec() must be called before the first |
| 174 | * call to this API with the same context. |
| 175 | * |
| 176 | * \param ctx The AES context to use for encryption or decryption. |
| 177 | * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or |
| 178 | * #MBEDTLS_AES_DECRYPT. |
| 179 | * \param input The 16-Byte buffer holding the input data. |
| 180 | * \param output The 16-Byte buffer holding the output data. |
| 181 | |
| 182 | * \return \c 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 183 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 185 | int mode, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 186 | const unsigned char input[16], |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 187 | unsigned char output[16] ); |
| 188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 190 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 191 | * \brief This function performs an AES-CBC encryption or decryption operation |
| 192 | * on full blocks. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 193 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 194 | * It performs the operation defined in the \p mode |
| 195 | * parameter (encrypt/decrypt), on the input data buffer defined in |
| 196 | * the \p input parameter. |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 197 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 198 | * It can be called as many times as needed, until all the input |
| 199 | * data is processed. mbedtls_aes_init(), and either |
| 200 | * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called |
| 201 | * before the first call to this API with the same context. |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 202 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 203 | * \note This function operates on aligned blocks, that is, the input size |
| 204 | * must be a multiple of the AES block size of 16 Bytes. |
| 205 | * |
| 206 | * \note Upon exit, the content of the IV is updated so that you can |
| 207 | * call the same function again on the next |
| 208 | * block(s) of data and get the same result as if it was |
| 209 | * encrypted in one call. This allows a "streaming" usage. |
| 210 | * If you need to retain the contents of the IV, you should |
| 211 | * either save it manually or use the cipher module instead. |
| 212 | * |
| 213 | * |
| 214 | * \param ctx The AES context to use for encryption or decryption. |
| 215 | * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or |
| 216 | * #MBEDTLS_AES_DECRYPT. |
| 217 | * \param length The length of the input data in Bytes. This must be a |
| 218 | * multiple of the block size (16 Bytes). |
| 219 | * \param iv Initialization vector (updated after use). |
| 220 | * \param input The buffer holding the input data. |
| 221 | * \param output The buffer holding the output data. |
| 222 | * |
| 223 | * \return \c 0 on success, or #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH |
| 224 | * on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 225 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 226 | int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 227 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 228 | size_t length, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 229 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 230 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 231 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 232 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 233 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 235 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 236 | * \brief This function performs an AES-CFB128 encryption or decryption |
| 237 | * operation. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 238 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 239 | * It performs the operation defined in the \p mode |
| 240 | * parameter (encrypt or decrypt), on the input data buffer |
| 241 | * defined in the \p input parameter. |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 242 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 243 | * For CFB, you must set up the context with mbedtls_aes_setkey_enc(), |
| 244 | * regardless of whether you are performing an encryption or decryption |
| 245 | * operation, that is, regardless of the \p mode parameter. This is |
| 246 | * because CFB mode uses the same key schedule for encryption and |
| 247 | * decryption. |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 248 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 249 | * \note Upon exit, the content of the IV is updated so that you can |
| 250 | * call the same function again on the next |
| 251 | * block(s) of data and get the same result as if it was |
| 252 | * encrypted in one call. This allows a "streaming" usage. |
| 253 | * If you need to retain the contents of the |
| 254 | * IV, you must either save it manually or use the cipher |
| 255 | * module instead. |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 256 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 257 | * |
| 258 | * \param ctx The AES context to use for encryption or decryption. |
| 259 | * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or |
| 260 | * #MBEDTLS_AES_DECRYPT. |
| 261 | * \param length The length of the input data. |
| 262 | * \param iv_off The offset in IV (updated after use). |
| 263 | * \param iv The initialization vector (updated after use). |
| 264 | * \param input The buffer holding the input data. |
| 265 | * \param output The buffer holding the output data. |
| 266 | * |
| 267 | * \return \c 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 268 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 269 | int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 270 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 271 | size_t length, |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 272 | size_t *iv_off, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 273 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 274 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 275 | unsigned char *output ); |
| 276 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 277 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 278 | * \brief This function performs an AES-CFB8 encryption or decryption |
| 279 | * operation. |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 280 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 281 | * It performs the operation defined in the \p mode |
| 282 | * parameter (encrypt/decrypt), on the input data buffer defined |
| 283 | * in the \p input parameter. |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 284 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 285 | * Due to the nature of CFB, you must use the same key schedule for |
| 286 | * both encryption and decryption operations. Therefore, you must |
| 287 | * use the context initialized with mbedtls_aes_setkey_enc() for |
| 288 | * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT. |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 289 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 290 | * \note Upon exit, the content of the IV is updated so that you can |
| 291 | * call the same function again on the next |
| 292 | * block(s) of data and get the same result as if it was |
| 293 | * encrypted in one call. This allows a "streaming" usage. |
| 294 | * If you need to retain the contents of the |
| 295 | * IV, you should either save it manually or use the cipher |
| 296 | * module instead. |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 297 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 298 | * |
| 299 | * \param ctx The AES context to use for encryption or decryption. |
| 300 | * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or |
| 301 | * #MBEDTLS_AES_DECRYPT |
| 302 | * \param length The length of the input data. |
| 303 | * \param iv The initialization vector (updated after use). |
| 304 | * \param input The buffer holding the input data. |
| 305 | * \param output The buffer holding the output data. |
| 306 | * |
| 307 | * \return \c 0 on success. |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 308 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 309 | int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx, |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 310 | int mode, |
| 311 | size_t length, |
| 312 | unsigned char iv[16], |
| 313 | const unsigned char *input, |
| 314 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 315 | #endif /*MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 318 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 319 | * \brief This function performs an AES-CTR encryption or decryption |
| 320 | * operation. |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 321 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 322 | * This function performs the operation defined in the \p mode |
| 323 | * parameter (encrypt/decrypt), on the input data buffer |
| 324 | * defined in the \p input parameter. |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 325 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 326 | * Due to the nature of CTR, you must use the same key schedule |
| 327 | * for both encryption and decryption operations. Therefore, you |
| 328 | * must use the context initialized with mbedtls_aes_setkey_enc() |
| 329 | * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT. |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 330 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 331 | * \warning You must keep the maximum use of your counter in mind. |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 332 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 333 | * \param ctx The AES context to use for encryption or decryption. |
| 334 | * \param length The length of the input data. |
| 335 | * \param nc_off The offset in the current \p stream_block, for |
| 336 | * resuming within the current cipher stream. The |
| 337 | * offset pointer should be 0 at the start of a stream. |
| 338 | * \param nonce_counter The 128-bit nonce and counter. |
| 339 | * \param stream_block The saved stream block for resuming. This is |
| 340 | * overwritten by the function. |
| 341 | * \param input The buffer holding the input data. |
| 342 | * \param output The buffer holding the output data. |
| 343 | * |
| 344 | * \return \c 0 on success. |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 345 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 346 | int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx, |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 347 | size_t length, |
| 348 | size_t *nc_off, |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 349 | unsigned char nonce_counter[16], |
| 350 | unsigned char stream_block[16], |
| 351 | const unsigned char *input, |
| 352 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 353 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 354 | |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 355 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 356 | * \brief Internal AES block encryption function. This is only |
| 357 | * exposed to allow overriding it using |
| 358 | * \c MBEDTLS_AES_ENCRYPT_ALT. |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 359 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 360 | * \param ctx The AES context to use for encryption. |
| 361 | * \param input The plaintext block. |
| 362 | * \param output The output (ciphertext) block. |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 363 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 364 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 365 | */ |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 366 | int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx, |
| 367 | const unsigned char input[16], |
| 368 | unsigned char output[16] ); |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 369 | |
| 370 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 371 | * \brief Internal AES block decryption function. This is only |
| 372 | * exposed to allow overriding it using see |
| 373 | * \c MBEDTLS_AES_DECRYPT_ALT. |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 374 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 375 | * \param ctx The AES context to use for decryption. |
| 376 | * \param input The ciphertext block. |
| 377 | * \param output The output (plaintext) block. |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 378 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 379 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 380 | */ |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 381 | int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx, |
| 382 | const unsigned char input[16], |
| 383 | unsigned char output[16] ); |
| 384 | |
| 385 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 386 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 387 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 388 | #else |
| 389 | #define MBEDTLS_DEPRECATED |
| 390 | #endif |
| 391 | /** |
Hanno Becker | ca1cdb2 | 2017-07-20 09:50:59 +0100 | [diff] [blame] | 392 | * \brief Deprecated internal AES block encryption function |
| 393 | * without return value. |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 394 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 395 | * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0. |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 396 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 397 | * \param ctx The AES context to use for encryption. |
| 398 | * \param input Plaintext block. |
| 399 | * \param output Output (ciphertext) block. |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 400 | */ |
Hanno Becker | bedc205 | 2017-06-26 12:46:56 +0100 | [diff] [blame] | 401 | MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx, |
| 402 | const unsigned char input[16], |
| 403 | unsigned char output[16] ); |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 404 | |
| 405 | /** |
Hanno Becker | ca1cdb2 | 2017-07-20 09:50:59 +0100 | [diff] [blame] | 406 | * \brief Deprecated internal AES block decryption function |
| 407 | * without return value. |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 408 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 409 | * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0. |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 410 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 411 | * \param ctx The AES context to use for decryption. |
| 412 | * \param input Ciphertext block. |
| 413 | * \param output Output (plaintext) block. |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 414 | */ |
Hanno Becker | bedc205 | 2017-06-26 12:46:56 +0100 | [diff] [blame] | 415 | MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx, |
| 416 | const unsigned char input[16], |
| 417 | unsigned char output[16] ); |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 418 | |
| 419 | #undef MBEDTLS_DEPRECATED |
| 420 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 421 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 422 | #ifdef __cplusplus |
| 423 | } |
| 424 | #endif |
| 425 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | #else /* MBEDTLS_AES_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 427 | #include "aes_alt.h" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 428 | #endif /* MBEDTLS_AES_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 429 | |
| 430 | #ifdef __cplusplus |
| 431 | extern "C" { |
| 432 | #endif |
| 433 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 434 | /** |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 435 | * \brief Checkup routine. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 436 | * |
Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 437 | * \return \c 0 on success, or \c 1 on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 438 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 439 | int mbedtls_aes_self_test( int verbose ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 440 | |
| 441 | #ifdef __cplusplus |
| 442 | } |
| 443 | #endif |
| 444 | |
| 445 | #endif /* aes.h */ |