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 |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | 085ab04 | 2015-01-23 11:06:27 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 9 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 24 | #ifndef POLARSSL_AES_H |
| 25 | #define POLARSSL_AES_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 27 | #if !defined(POLARSSL_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 |
| 30 | #include POLARSSL_CONFIG_FILE |
| 31 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 32 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 33 | #include <string.h> |
| 34 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 35 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 36 | #include <basetsd.h> |
| 37 | typedef UINT32 uint32_t; |
| 38 | #else |
| 39 | #include <inttypes.h> |
| 40 | #endif |
| 41 | |
Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 42 | /* padlock.c and aesni.c rely on these values! */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 43 | #define AES_ENCRYPT 1 |
| 44 | #define AES_DECRYPT 0 |
| 45 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 46 | #define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */ |
| 47 | #define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */ |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 48 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 49 | #if !defined(POLARSSL_AES_ALT) |
| 50 | // Regular implementation |
| 51 | // |
| 52 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 53 | #ifdef __cplusplus |
| 54 | extern "C" { |
| 55 | #endif |
| 56 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | /** |
| 58 | * \brief AES context structure |
Manuel Pégourié-Gonnard | 4a5b995 | 2013-12-29 13:50:32 +0100 | [diff] [blame] | 59 | * |
| 60 | * \note buf is able to hold 32 extra bytes, which can be used: |
| 61 | * - for alignment purposes if VIA padlock is used, and/or |
| 62 | * - to simplify key expansion in the 256-bit case by |
| 63 | * generating an extra round key |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 64 | */ |
| 65 | typedef struct |
| 66 | { |
| 67 | int nr; /*!< number of rounds */ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 68 | uint32_t *rk; /*!< AES round keys */ |
| 69 | uint32_t buf[68]; /*!< unaligned data */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 70 | } |
| 71 | aes_context; |
| 72 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 73 | /** |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 74 | * \brief Initialize AES context |
| 75 | * |
| 76 | * \param ctx AES context to be initialized |
| 77 | */ |
| 78 | void aes_init( aes_context *ctx ); |
| 79 | |
| 80 | /** |
| 81 | * \brief Clear AES context |
| 82 | * |
| 83 | * \param ctx AES context to be cleared |
| 84 | */ |
| 85 | void aes_free( aes_context *ctx ); |
| 86 | |
| 87 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 88 | * \brief AES key schedule (encryption) |
| 89 | * |
| 90 | * \param ctx AES context to be initialized |
| 91 | * \param key encryption key |
| 92 | * \param keysize must be 128, 192 or 256 |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 93 | * |
| 94 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 96 | int aes_setkey_enc( aes_context *ctx, const unsigned char *key, |
| 97 | unsigned int keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * \brief AES key schedule (decryption) |
| 101 | * |
| 102 | * \param ctx AES context to be initialized |
| 103 | * \param key decryption key |
| 104 | * \param keysize must be 128, 192 or 256 |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 105 | * |
| 106 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 108 | int aes_setkey_dec( aes_context *ctx, const unsigned char *key, |
| 109 | unsigned int keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * \brief AES-ECB block encryption/decryption |
| 113 | * |
| 114 | * \param ctx AES context |
| 115 | * \param mode AES_ENCRYPT or AES_DECRYPT |
| 116 | * \param input 16-byte input block |
| 117 | * \param output 16-byte output block |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 118 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 119 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 121 | int aes_crypt_ecb( aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 122 | int mode, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 123 | const unsigned char input[16], |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 124 | unsigned char output[16] ); |
| 125 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 126 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 127 | /** |
| 128 | * \brief AES-CBC buffer encryption/decryption |
Paul Bakker | 4c067eb | 2009-05-17 10:25:19 +0000 | [diff] [blame] | 129 | * Length should be a multiple of the block |
| 130 | * size (16 bytes) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | * |
| 132 | * \param ctx AES context |
| 133 | * \param mode AES_ENCRYPT or AES_DECRYPT |
| 134 | * \param length length of the input data |
| 135 | * \param iv initialization vector (updated after use) |
| 136 | * \param input buffer holding the input data |
| 137 | * \param output buffer holding the output data |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 138 | * |
| 139 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 140 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 141 | int aes_crypt_cbc( aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 142 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 143 | size_t length, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 144 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 145 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 146 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 147 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | |
Manuel Pégourié-Gonnard | 1ec220b | 2014-03-10 11:20:17 +0100 | [diff] [blame] | 149 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 150 | /** |
Paul Bakker | 4c067eb | 2009-05-17 10:25:19 +0000 | [diff] [blame] | 151 | * \brief AES-CFB128 buffer encryption/decryption. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 152 | * |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 153 | * Note: Due to the nature of CFB you should use the same key schedule for |
| 154 | * both encryption and decryption. So a context initialized with |
| 155 | * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT. |
| 156 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 157 | * \param ctx AES context |
| 158 | * \param mode AES_ENCRYPT or AES_DECRYPT |
| 159 | * \param length length of the input data |
| 160 | * \param iv_off offset in IV (updated after use) |
| 161 | * \param iv initialization vector (updated after use) |
| 162 | * \param input buffer holding the input data |
| 163 | * \param output buffer holding the output data |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 164 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 165 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 167 | int aes_crypt_cfb128( aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 169 | size_t length, |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 170 | size_t *iv_off, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 171 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 172 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 173 | unsigned char *output ); |
| 174 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 175 | /** |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 176 | * \brief AES-CFB8 buffer encryption/decryption. |
| 177 | * |
| 178 | * Note: Due to the nature of CFB you should use the same key schedule for |
| 179 | * both encryption and decryption. So a context initialized with |
| 180 | * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT. |
| 181 | * |
| 182 | * \param ctx AES context |
| 183 | * \param mode AES_ENCRYPT or AES_DECRYPT |
| 184 | * \param length length of the input data |
| 185 | * \param iv initialization vector (updated after use) |
| 186 | * \param input buffer holding the input data |
| 187 | * \param output buffer holding the output data |
| 188 | * |
| 189 | * \return 0 if successful |
| 190 | */ |
| 191 | int aes_crypt_cfb8( aes_context *ctx, |
| 192 | int mode, |
| 193 | size_t length, |
| 194 | unsigned char iv[16], |
| 195 | const unsigned char *input, |
| 196 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 1ec220b | 2014-03-10 11:20:17 +0100 | [diff] [blame] | 197 | #endif /*POLARSSL_CIPHER_MODE_CFB */ |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 198 | |
Manuel Pégourié-Gonnard | 1ec220b | 2014-03-10 11:20:17 +0100 | [diff] [blame] | 199 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 200 | /** |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 201 | * \brief AES-CTR buffer encryption/decryption |
| 202 | * |
| 203 | * Warning: You have to keep the maximum use of your counter in mind! |
| 204 | * |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 205 | * Note: Due to the nature of CTR you should use the same key schedule for |
| 206 | * both encryption and decryption. So a context initialized with |
| 207 | * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT. |
| 208 | * |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 209 | * \param ctx AES context |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 210 | * \param length The length of the data |
| 211 | * \param nc_off The offset in the current stream_block (for resuming |
| 212 | * within current cipher stream). The offset pointer to |
| 213 | * should be 0 at the start of a stream. |
| 214 | * \param nonce_counter The 128-bit nonce and counter. |
| 215 | * \param stream_block The saved stream-block for resuming. Is overwritten |
| 216 | * by the function. |
| 217 | * \param input The input data stream |
| 218 | * \param output The output data stream |
| 219 | * |
| 220 | * \return 0 if successful |
| 221 | */ |
| 222 | int aes_crypt_ctr( aes_context *ctx, |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 223 | size_t length, |
| 224 | size_t *nc_off, |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 225 | unsigned char nonce_counter[16], |
| 226 | unsigned char stream_block[16], |
| 227 | const unsigned char *input, |
| 228 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 1ec220b | 2014-03-10 11:20:17 +0100 | [diff] [blame] | 229 | #endif /* POLARSSL_CIPHER_MODE_CTR */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 230 | |
| 231 | #ifdef __cplusplus |
| 232 | } |
| 233 | #endif |
| 234 | |
| 235 | #else /* POLARSSL_AES_ALT */ |
| 236 | #include "aes_alt.h" |
| 237 | #endif /* POLARSSL_AES_ALT */ |
| 238 | |
| 239 | #ifdef __cplusplus |
| 240 | extern "C" { |
| 241 | #endif |
| 242 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 243 | /** |
| 244 | * \brief Checkup routine |
| 245 | * |
| 246 | * \return 0 if successful, or 1 if the test failed |
| 247 | */ |
| 248 | int aes_self_test( int verbose ); |
| 249 | |
| 250 | #ifdef __cplusplus |
| 251 | } |
| 252 | #endif |
| 253 | |
| 254 | #endif /* aes.h */ |