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