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 | * |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 4 | * \brief AES block cipher |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 |
| 9 | * |
| 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 11 | * not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
| 13 | * |
| 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | * |
| 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | * See the License for the specific language governing permissions and |
| 20 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 21 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 22 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #ifndef MBEDTLS_AES_H |
| 25 | #define MBEDTLS_AES_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 28 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 32 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 33 | #include <stddef.h> |
Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 34 | #include <stdint.h> |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 36 | /* padlock.c and aesni.c rely on these values! */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #define MBEDTLS_AES_ENCRYPT 1 |
| 38 | #define MBEDTLS_AES_DECRYPT 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 39 | |
Andres Amaya Garcia | c538064 | 2017-11-28 19:57:51 +0000 | [diff] [blame] | 40 | /* Error codes in range 0x0020-0x0022 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | #define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */ |
| 42 | #define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */ |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 43 | |
Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame^] | 44 | /* Error codes in range 0x0023-0x0025 */ |
Andres Amaya Garcia | c538064 | 2017-11-28 19:57:51 +0000 | [diff] [blame] | 45 | #define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE -0x0023 /**< Feature not available, e.g. unsupported AES key size. */ |
Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame^] | 46 | #define MBEDTLS_ERR_AES_HW_ACCEL_FAILED -0x0025 /**< AES hardware accelerator failed. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 48 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ |
| 49 | !defined(inline) && !defined(__cplusplus) |
| 50 | #define inline __inline |
| 51 | #endif |
| 52 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #if !defined(MBEDTLS_AES_ALT) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 54 | // Regular implementation |
| 55 | // |
| 56 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 57 | #ifdef __cplusplus |
| 58 | extern "C" { |
| 59 | #endif |
| 60 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 61 | /** |
| 62 | * \brief AES context structure |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 63 | * |
| 64 | * \note buf is able to hold 32 extra bytes, which can be used: |
| 65 | * - for alignment purposes if VIA padlock is used, and/or |
| 66 | * - to simplify key expansion in the 256-bit case by |
| 67 | * generating an extra round key |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 68 | */ |
| 69 | typedef struct |
| 70 | { |
| 71 | int nr; /*!< number of rounds */ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 72 | uint32_t *rk; /*!< AES round keys */ |
| 73 | uint32_t buf[68]; /*!< unaligned data */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 74 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 75 | mbedtls_aes_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | /** |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 78 | * \brief Initialize AES context |
| 79 | * |
| 80 | * \param ctx AES context to be initialized |
| 81 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | void mbedtls_aes_init( mbedtls_aes_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * \brief Clear AES context |
| 86 | * |
| 87 | * \param ctx AES context to be cleared |
| 88 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 89 | void mbedtls_aes_free( mbedtls_aes_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 90 | |
| 91 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 92 | * \brief AES key schedule (encryption) |
| 93 | * |
| 94 | * \param ctx AES context to be initialized |
| 95 | * \param key encryption key |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 96 | * \param keybits must be 128, 192 or 256 |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 97 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 99 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | 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] | 101 | unsigned int keybits ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * \brief AES key schedule (decryption) |
| 105 | * |
| 106 | * \param ctx AES context to be initialized |
| 107 | * \param key decryption key |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 108 | * \param keybits must be 128, 192 or 256 |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 109 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 110 | * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | 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] | 113 | unsigned int keybits ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * \brief AES-ECB block encryption/decryption |
| 117 | * |
| 118 | * \param ctx AES context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | * \param input 16-byte input block |
| 121 | * \param output 16-byte output block |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 122 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 123 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 124 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 125 | int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 126 | int mode, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 127 | const unsigned char input[16], |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | unsigned char output[16] ); |
| 129 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | /** |
| 132 | * \brief AES-CBC buffer encryption/decryption |
Paul Bakker | 4c067eb | 2009-05-17 10:25:19 +0000 | [diff] [blame] | 133 | * Length should be a multiple of the block |
| 134 | * size (16 bytes) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 135 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 136 | * \note Upon exit, the content of the IV is updated so that you can |
| 137 | * call the function same function again on the following |
| 138 | * block(s) of data and get the same result as if it was |
| 139 | * encrypted in one call. This allows a "streaming" usage. |
| 140 | * If on the other hand you need to retain the contents of the |
| 141 | * IV, you should either save it manually or use the cipher |
| 142 | * module instead. |
| 143 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 144 | * \param ctx AES context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 146 | * \param length length of the input data |
| 147 | * \param iv initialization vector (updated after use) |
| 148 | * \param input buffer holding the input data |
| 149 | * \param output buffer holding the output data |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 150 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 152 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 155 | size_t length, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 156 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 157 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
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 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 162 | /** |
Paul Bakker | 4c067eb | 2009-05-17 10:25:19 +0000 | [diff] [blame] | 163 | * \brief AES-CFB128 buffer encryption/decryption. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | * |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 165 | * Note: Due to the nature of CFB you should use the same key schedule for |
| 166 | * both encryption and decryption. So a context initialized with |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT. |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 168 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 169 | * \note Upon exit, the content of the IV is updated so that you can |
| 170 | * call the function same function again on the following |
| 171 | * block(s) of data and get the same result as if it was |
| 172 | * encrypted in one call. This allows a "streaming" usage. |
| 173 | * If on the other hand you need to retain the contents of the |
| 174 | * IV, you should either save it manually or use the cipher |
| 175 | * module instead. |
| 176 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 177 | * \param ctx AES context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 179 | * \param length length of the input data |
| 180 | * \param iv_off offset in IV (updated after use) |
| 181 | * \param iv initialization vector (updated after use) |
| 182 | * \param input buffer holding the input data |
| 183 | * \param output buffer holding the output data |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 184 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 185 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 188 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 189 | size_t length, |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 190 | size_t *iv_off, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 191 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 192 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 193 | unsigned char *output ); |
| 194 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 195 | /** |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 196 | * \brief AES-CFB8 buffer encryption/decryption. |
| 197 | * |
| 198 | * Note: Due to the nature of CFB you should use the same key schedule for |
| 199 | * both encryption and decryption. So a context initialized with |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT. |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 201 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 202 | * \note Upon exit, the content of the IV is updated so that you can |
| 203 | * call the function same function again on the following |
| 204 | * block(s) of data and get the same result as if it was |
| 205 | * encrypted in one call. This allows a "streaming" usage. |
| 206 | * If on the other hand you need to retain the contents of the |
| 207 | * IV, you should either save it manually or use the cipher |
| 208 | * module instead. |
| 209 | * |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 210 | * \param ctx AES context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 212 | * \param length length of the input data |
| 213 | * \param iv initialization vector (updated after use) |
| 214 | * \param input buffer holding the input data |
| 215 | * \param output buffer holding the output data |
| 216 | * |
| 217 | * \return 0 if successful |
| 218 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 219 | int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx, |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 220 | int mode, |
| 221 | size_t length, |
| 222 | unsigned char iv[16], |
| 223 | const unsigned char *input, |
| 224 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | #endif /*MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 226 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 228 | /** |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 229 | * \brief AES-CTR buffer encryption/decryption |
| 230 | * |
| 231 | * Warning: You have to keep the maximum use of your counter in mind! |
| 232 | * |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 233 | * Note: Due to the nature of CTR you should use the same key schedule for |
| 234 | * both encryption and decryption. So a context initialized with |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 235 | * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT. |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 236 | * |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 237 | * \param ctx AES context |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 238 | * \param length The length of the data |
| 239 | * \param nc_off The offset in the current stream_block (for resuming |
| 240 | * within current cipher stream). The offset pointer to |
| 241 | * should be 0 at the start of a stream. |
| 242 | * \param nonce_counter The 128-bit nonce and counter. |
| 243 | * \param stream_block The saved stream-block for resuming. Is overwritten |
| 244 | * by the function. |
| 245 | * \param input The input data stream |
| 246 | * \param output The output data stream |
| 247 | * |
| 248 | * \return 0 if successful |
| 249 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx, |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 251 | size_t length, |
| 252 | size_t *nc_off, |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 253 | unsigned char nonce_counter[16], |
| 254 | unsigned char stream_block[16], |
| 255 | const unsigned char *input, |
| 256 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 258 | |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 259 | /** |
| 260 | * \brief Internal AES block encryption function |
| 261 | * (Only exposed to allow overriding it, |
| 262 | * see MBEDTLS_AES_ENCRYPT_ALT) |
| 263 | * |
| 264 | * \param ctx AES context |
| 265 | * \param input Plaintext block |
| 266 | * \param output Output (ciphertext) block |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 267 | * |
| 268 | * \return 0 if successful |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 269 | */ |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 270 | int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx, |
| 271 | const unsigned char input[16], |
| 272 | unsigned char output[16] ); |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * \brief Internal AES block decryption function |
| 276 | * (Only exposed to allow overriding it, |
| 277 | * see MBEDTLS_AES_DECRYPT_ALT) |
| 278 | * |
| 279 | * \param ctx AES context |
| 280 | * \param input Ciphertext block |
| 281 | * \param output Output (plaintext) block |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 282 | * |
| 283 | * \return 0 if successful |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 284 | */ |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 285 | int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx, |
| 286 | const unsigned char input[16], |
| 287 | unsigned char output[16] ); |
| 288 | |
| 289 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 290 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 291 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 292 | #else |
| 293 | #define MBEDTLS_DEPRECATED |
| 294 | #endif |
| 295 | /** |
Hanno Becker | ca1cdb2 | 2017-07-20 09:50:59 +0100 | [diff] [blame] | 296 | * \brief Deprecated internal AES block encryption function |
| 297 | * without return value. |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 298 | * |
| 299 | * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0 |
| 300 | * |
| 301 | * \param ctx AES context |
| 302 | * \param input Plaintext block |
| 303 | * \param output Output (ciphertext) block |
| 304 | */ |
Hanno Becker | bedc205 | 2017-06-26 12:46:56 +0100 | [diff] [blame] | 305 | MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx, |
| 306 | const unsigned char input[16], |
| 307 | unsigned char output[16] ); |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 308 | |
| 309 | /** |
Hanno Becker | ca1cdb2 | 2017-07-20 09:50:59 +0100 | [diff] [blame] | 310 | * \brief Deprecated internal AES block decryption function |
| 311 | * without return value. |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 312 | * |
| 313 | * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0 |
| 314 | * |
| 315 | * \param ctx AES context |
| 316 | * \param input Ciphertext block |
| 317 | * \param output Output (plaintext) block |
| 318 | */ |
Hanno Becker | bedc205 | 2017-06-26 12:46:56 +0100 | [diff] [blame] | 319 | MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx, |
| 320 | const unsigned char input[16], |
| 321 | unsigned char output[16] ); |
Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 322 | |
| 323 | #undef MBEDTLS_DEPRECATED |
| 324 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 325 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 326 | #ifdef __cplusplus |
| 327 | } |
| 328 | #endif |
| 329 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 330 | #else /* MBEDTLS_AES_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 331 | #include "aes_alt.h" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 332 | #endif /* MBEDTLS_AES_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 333 | |
| 334 | #ifdef __cplusplus |
| 335 | extern "C" { |
| 336 | #endif |
| 337 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 338 | /** |
| 339 | * \brief Checkup routine |
| 340 | * |
| 341 | * \return 0 if successful, or 1 if the test failed |
| 342 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 343 | int mbedtls_aes_self_test( int verbose ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 344 | |
| 345 | #ifdef __cplusplus |
| 346 | } |
| 347 | #endif |
| 348 | |
| 349 | #endif /* aes.h */ |