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 | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org> |
| 5 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 6 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 7 | * Joined copyright on original XySSL code with: Christophe Devine |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 22 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 23 | #ifndef POLARSSL_AES_H |
| 24 | #define POLARSSL_AES_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
| 26 | #define AES_ENCRYPT 1 |
| 27 | #define AES_DECRYPT 0 |
| 28 | |
Paul Bakker | 3391b12 | 2009-07-28 20:11:54 +0000 | [diff] [blame] | 29 | #define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0800 |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame^] | 30 | #define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0810 |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 31 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | /** |
| 33 | * \brief AES context structure |
| 34 | */ |
| 35 | typedef struct |
| 36 | { |
| 37 | int nr; /*!< number of rounds */ |
| 38 | unsigned long *rk; /*!< AES round keys */ |
| 39 | unsigned long buf[68]; /*!< unaligned data */ |
| 40 | } |
| 41 | aes_context; |
| 42 | |
| 43 | #ifdef __cplusplus |
| 44 | extern "C" { |
| 45 | #endif |
| 46 | |
| 47 | /** |
| 48 | * \brief AES key schedule (encryption) |
| 49 | * |
| 50 | * \param ctx AES context to be initialized |
| 51 | * \param key encryption key |
| 52 | * \param keysize must be 128, 192 or 256 |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 53 | * |
| 54 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 55 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 56 | int aes_setkey_enc( aes_context *ctx, const unsigned char *key, int keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * \brief AES key schedule (decryption) |
| 60 | * |
| 61 | * \param ctx AES context to be initialized |
| 62 | * \param key decryption key |
| 63 | * \param keysize must be 128, 192 or 256 |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 64 | * |
| 65 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 67 | int aes_setkey_dec( aes_context *ctx, const unsigned char *key, int keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * \brief AES-ECB block encryption/decryption |
| 71 | * |
| 72 | * \param ctx AES context |
| 73 | * \param mode AES_ENCRYPT or AES_DECRYPT |
| 74 | * \param input 16-byte input block |
| 75 | * \param output 16-byte output block |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame^] | 76 | * |
| 77 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 78 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame^] | 79 | int aes_crypt_ecb( aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 80 | int mode, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 81 | const unsigned char input[16], |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 82 | unsigned char output[16] ); |
| 83 | |
| 84 | /** |
| 85 | * \brief AES-CBC buffer encryption/decryption |
Paul Bakker | 4c067eb | 2009-05-17 10:25:19 +0000 | [diff] [blame] | 86 | * Length should be a multiple of the block |
| 87 | * size (16 bytes) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 88 | * |
| 89 | * \param ctx AES context |
| 90 | * \param mode AES_ENCRYPT or AES_DECRYPT |
| 91 | * \param length length of the input data |
| 92 | * \param iv initialization vector (updated after use) |
| 93 | * \param input buffer holding the input data |
| 94 | * \param output buffer holding the output data |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame^] | 95 | * |
| 96 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 97 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame^] | 98 | int aes_crypt_cbc( aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 99 | int mode, |
| 100 | int length, |
| 101 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 102 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 | unsigned char *output ); |
| 104 | |
| 105 | /** |
Paul Bakker | 4c067eb | 2009-05-17 10:25:19 +0000 | [diff] [blame] | 106 | * \brief AES-CFB128 buffer encryption/decryption. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | * |
| 108 | * \param ctx AES context |
| 109 | * \param mode AES_ENCRYPT or AES_DECRYPT |
| 110 | * \param length length of the input data |
| 111 | * \param iv_off offset in IV (updated after use) |
| 112 | * \param iv initialization vector (updated after use) |
| 113 | * \param input buffer holding the input data |
| 114 | * \param output buffer holding the output data |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame^] | 115 | * |
| 116 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame^] | 118 | int aes_crypt_cfb128( aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 119 | int mode, |
| 120 | int length, |
| 121 | int *iv_off, |
| 122 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 123 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 124 | unsigned char *output ); |
| 125 | |
| 126 | /** |
| 127 | * \brief Checkup routine |
| 128 | * |
| 129 | * \return 0 if successful, or 1 if the test failed |
| 130 | */ |
| 131 | int aes_self_test( int verbose ); |
| 132 | |
| 133 | #ifdef __cplusplus |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | #endif /* aes.h */ |