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 | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 30 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | /** |
| 32 | * \brief AES context structure |
| 33 | */ |
| 34 | typedef struct |
| 35 | { |
| 36 | int nr; /*!< number of rounds */ |
| 37 | unsigned long *rk; /*!< AES round keys */ |
| 38 | unsigned long buf[68]; /*!< unaligned data */ |
| 39 | } |
| 40 | aes_context; |
| 41 | |
| 42 | #ifdef __cplusplus |
| 43 | extern "C" { |
| 44 | #endif |
| 45 | |
| 46 | /** |
| 47 | * \brief AES key schedule (encryption) |
| 48 | * |
| 49 | * \param ctx AES context to be initialized |
| 50 | * \param key encryption key |
| 51 | * \param keysize must be 128, 192 or 256 |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 52 | * |
| 53 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 54 | */ |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 55 | int aes_setkey_enc( aes_context *ctx, unsigned char *key, int keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * \brief AES key schedule (decryption) |
| 59 | * |
| 60 | * \param ctx AES context to be initialized |
| 61 | * \param key decryption key |
| 62 | * \param keysize must be 128, 192 or 256 |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 63 | * |
| 64 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 65 | */ |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 66 | int aes_setkey_dec( aes_context *ctx, unsigned char *key, int keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * \brief AES-ECB block encryption/decryption |
| 70 | * |
| 71 | * \param ctx AES context |
| 72 | * \param mode AES_ENCRYPT or AES_DECRYPT |
| 73 | * \param input 16-byte input block |
| 74 | * \param output 16-byte output block |
| 75 | */ |
| 76 | void aes_crypt_ecb( aes_context *ctx, |
| 77 | int mode, |
| 78 | unsigned char input[16], |
| 79 | unsigned char output[16] ); |
| 80 | |
| 81 | /** |
| 82 | * \brief AES-CBC buffer encryption/decryption |
Paul Bakker | 4c067eb | 2009-05-17 10:25:19 +0000 | [diff] [blame] | 83 | * Length should be a multiple of the block |
| 84 | * size (16 bytes) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | * |
| 86 | * \param ctx AES context |
| 87 | * \param mode AES_ENCRYPT or AES_DECRYPT |
| 88 | * \param length length of the input data |
| 89 | * \param iv initialization vector (updated after use) |
| 90 | * \param input buffer holding the input data |
| 91 | * \param output buffer holding the output data |
| 92 | */ |
| 93 | void aes_crypt_cbc( aes_context *ctx, |
| 94 | int mode, |
| 95 | int length, |
| 96 | unsigned char iv[16], |
| 97 | unsigned char *input, |
| 98 | unsigned char *output ); |
| 99 | |
| 100 | /** |
Paul Bakker | 4c067eb | 2009-05-17 10:25:19 +0000 | [diff] [blame] | 101 | * \brief AES-CFB128 buffer encryption/decryption. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 102 | * |
| 103 | * \param ctx AES context |
| 104 | * \param mode AES_ENCRYPT or AES_DECRYPT |
| 105 | * \param length length of the input data |
| 106 | * \param iv_off offset in IV (updated after use) |
| 107 | * \param iv initialization vector (updated after use) |
| 108 | * \param input buffer holding the input data |
| 109 | * \param output buffer holding the output data |
| 110 | */ |
| 111 | void aes_crypt_cfb128( aes_context *ctx, |
| 112 | int mode, |
| 113 | int length, |
| 114 | int *iv_off, |
| 115 | unsigned char iv[16], |
| 116 | unsigned char *input, |
| 117 | unsigned char *output ); |
| 118 | |
| 119 | /** |
| 120 | * \brief Checkup routine |
| 121 | * |
| 122 | * \return 0 if successful, or 1 if the test failed |
| 123 | */ |
| 124 | int aes_self_test( int verbose ); |
| 125 | |
| 126 | #ifdef __cplusplus |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | #endif /* aes.h */ |