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