| 1 | /* AES Implementation by X-N2O |
|---|
| 2 | * Started: 15:41:35 - 18 Nov 2009 |
|---|
| 3 | * Finished: 20:03:59 - 21 Nov 2009 |
|---|
| 4 | * Logarithm, S-Box, and RCON tables are not hardcoded |
|---|
| 5 | * Instead they are generated when the program starts |
|---|
| 6 | * All of the code below is based from the AES specification |
|---|
| 7 | * You can find it at http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf |
|---|
| 8 | * This is only a proof of concept, and should not be considered as the most efficient implementation |
|---|
| 9 | * |
|---|
| 10 | * This work is licensed under the Creative Commons Attribution 3.0 Unported License. |
|---|
| 11 | * To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons: |
|---|
| 12 | * 171 Second Street, Suite 300, San Francisco, California, 94105, USA. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | #ifndef __aes_h |
|---|
| 16 | #define __aes_h |
|---|
| 17 | |
|---|
| 18 | #define AES_RPOL 0x011b // reduction polynomial (x^8 + x^4 + x^3 + x + 1) |
|---|
| 19 | #define AES_GEN 0x03 // gf(2^8) generator (x^4 + 1) |
|---|
| 20 | #define AES_SBOX_CC 0x63 // S-Box C constant |
|---|
| 21 | |
|---|
| 22 | #define KEY_128 (128/8) |
|---|
| 23 | #define KEY_192 (192/8) |
|---|
| 24 | #define KEY_256 (256/8) |
|---|
| 25 | |
|---|
| 26 | #define aes_mul(a, b) ((a)&&(b)?g_aes_ilogt[(g_aes_logt[(a)]+g_aes_logt[(b)])%0xff]:0) |
|---|
| 27 | #define aes_inv(a) ((a)?g_aes_ilogt[0xff-g_aes_logt[(a)]]:0) |
|---|
| 28 | |
|---|
| 29 | extern unsigned char g_aes_logt[256], g_aes_ilogt[256]; |
|---|
| 30 | extern unsigned char g_aes_sbox[256], g_aes_isbox[256]; |
|---|
| 31 | |
|---|
| 32 | typedef struct { |
|---|
| 33 | unsigned char state[4][4]; |
|---|
| 34 | int kcol; |
|---|
| 35 | unsigned long rounds; |
|---|
| 36 | unsigned long keysched[0]; |
|---|
| 37 | } aes_ctx_t; |
|---|
| 38 | |
|---|
| 39 | void init_aes(); |
|---|
| 40 | aes_ctx_t *aes_alloc_ctx(unsigned char *key, unsigned long keyLen); |
|---|
| 41 | inline unsigned long aes_subword(unsigned long w); |
|---|
| 42 | inline unsigned long aes_rotword(unsigned long w); |
|---|
| 43 | void aes_keyexpansion(aes_ctx_t *ctx); |
|---|
| 44 | |
|---|
| 45 | inline unsigned char aes_mul_manual(unsigned char a, unsigned char b); // use aes_mul instead |
|---|
| 46 | |
|---|
| 47 | void aes_subbytes(aes_ctx_t *ctx); |
|---|
| 48 | void aes_shiftrows(aes_ctx_t *ctx); |
|---|
| 49 | void aes_mixcolumns(aes_ctx_t *ctx); |
|---|
| 50 | void aes_addroundkey(aes_ctx_t *ctx, int round); |
|---|
| 51 | void aes_encrypt(aes_ctx_t *ctx, unsigned char input[16], unsigned char output[16]); |
|---|
| 52 | |
|---|
| 53 | void aes_invsubbytes(aes_ctx_t *ctx); |
|---|
| 54 | void aes_invshiftrows(aes_ctx_t *ctx); |
|---|
| 55 | void aes_invmixcolumns(aes_ctx_t *ctx); |
|---|
| 56 | void aes_decrypt(aes_ctx_t *ctx, unsigned char input[16], unsigned char output[16]); |
|---|
| 57 | |
|---|
| 58 | void aes_free_ctx(aes_ctx_t *ctx); |
|---|
| 59 | |
|---|
| 60 | #endif //__aes_h |
|---|