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