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