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 | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2010, 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 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | #define AES_ENCRYPT 1 |
| 33 | #define AES_DECRYPT 0 |
| 34 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 35 | #define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */ |
| 36 | #define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */ |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 37 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | /** |
| 39 | * \brief AES context structure |
| 40 | */ |
| 41 | typedef struct |
| 42 | { |
| 43 | int nr; /*!< number of rounds */ |
| 44 | unsigned long *rk; /*!< AES round keys */ |
| 45 | unsigned long buf[68]; /*!< unaligned data */ |
| 46 | } |
| 47 | aes_context; |
| 48 | |
| 49 | #ifdef __cplusplus |
| 50 | extern "C" { |
| 51 | #endif |
| 52 | |
| 53 | /** |
| 54 | * \brief AES key schedule (encryption) |
| 55 | * |
| 56 | * \param ctx AES context to be initialized |
| 57 | * \param key encryption key |
| 58 | * \param keysize must be 128, 192 or 256 |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 59 | * |
| 60 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 61 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 62 | int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * \brief AES key schedule (decryption) |
| 66 | * |
| 67 | * \param ctx AES context to be initialized |
| 68 | * \param key decryption key |
| 69 | * \param keysize must be 128, 192 or 256 |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 70 | * |
| 71 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 72 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 73 | int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * \brief AES-ECB block encryption/decryption |
| 77 | * |
| 78 | * \param ctx AES context |
| 79 | * \param mode AES_ENCRYPT or AES_DECRYPT |
| 80 | * \param input 16-byte input block |
| 81 | * \param output 16-byte output block |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 82 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 83 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 84 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 85 | int aes_crypt_ecb( aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 86 | int mode, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 87 | const unsigned char input[16], |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 88 | unsigned char output[16] ); |
| 89 | |
| 90 | /** |
| 91 | * \brief AES-CBC buffer encryption/decryption |
Paul Bakker | 4c067eb | 2009-05-17 10:25:19 +0000 | [diff] [blame] | 92 | * Length should be a multiple of the block |
| 93 | * size (16 bytes) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 94 | * |
| 95 | * \param ctx AES context |
| 96 | * \param mode AES_ENCRYPT or AES_DECRYPT |
| 97 | * \param length length of the input data |
| 98 | * \param iv initialization vector (updated after use) |
| 99 | * \param input buffer holding the input data |
| 100 | * \param output buffer holding the output data |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 101 | * |
| 102 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 104 | int aes_crypt_cbc( aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 106 | size_t length, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 108 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 109 | unsigned char *output ); |
| 110 | |
| 111 | /** |
Paul Bakker | 4c067eb | 2009-05-17 10:25:19 +0000 | [diff] [blame] | 112 | * \brief AES-CFB128 buffer encryption/decryption. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | * |
| 114 | * \param ctx AES context |
| 115 | * \param mode AES_ENCRYPT or AES_DECRYPT |
| 116 | * \param length length of the input data |
| 117 | * \param iv_off offset in IV (updated after use) |
| 118 | * \param iv initialization vector (updated after use) |
| 119 | * \param input buffer holding the input data |
| 120 | * \param output buffer holding the output data |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 121 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 122 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 124 | int aes_crypt_cfb128( aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 125 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 126 | size_t length, |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame^] | 127 | size_t *iv_off, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 129 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 130 | unsigned char *output ); |
| 131 | |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 132 | /* |
| 133 | * \brief AES-CTR buffer encryption/decryption |
| 134 | * |
| 135 | * Warning: You have to keep the maximum use of your counter in mind! |
| 136 | * |
| 137 | * \param length The length of the data |
| 138 | * \param nc_off The offset in the current stream_block (for resuming |
| 139 | * within current cipher stream). The offset pointer to |
| 140 | * should be 0 at the start of a stream. |
| 141 | * \param nonce_counter The 128-bit nonce and counter. |
| 142 | * \param stream_block The saved stream-block for resuming. Is overwritten |
| 143 | * by the function. |
| 144 | * \param input The input data stream |
| 145 | * \param output The output data stream |
| 146 | * |
| 147 | * \return 0 if successful |
| 148 | */ |
| 149 | int aes_crypt_ctr( aes_context *ctx, |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame^] | 150 | size_t length, |
| 151 | size_t *nc_off, |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 152 | unsigned char nonce_counter[16], |
| 153 | unsigned char stream_block[16], |
| 154 | const unsigned char *input, |
| 155 | unsigned char *output ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 156 | /** |
| 157 | * \brief Checkup routine |
| 158 | * |
| 159 | * \return 0 if successful, or 1 if the test failed |
| 160 | */ |
| 161 | int aes_self_test( int verbose ); |
| 162 | |
| 163 | #ifdef __cplusplus |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | #endif /* aes.h */ |