source: svn/trunk/newcon3bcm2_21bu/dta/src/bootloader/aes/aes.h @ 2

Last change on this file since 2 was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
  • Property svn:executable set to *
File size: 2.2 KB
Line 
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
29extern unsigned char g_aes_logt[256], g_aes_ilogt[256];
30extern unsigned char g_aes_sbox[256], g_aes_isbox[256];
31
32typedef struct {
33        unsigned char state[4][4];
34        int kcol;
35        unsigned long rounds;
36        unsigned long keysched[0];
37} aes_ctx_t;
38
39void init_aes();
40aes_ctx_t *aes_alloc_ctx(unsigned char *key, unsigned long keyLen);
41inline unsigned long aes_subword(unsigned long w);
42inline unsigned long aes_rotword(unsigned long w);
43void aes_keyexpansion(aes_ctx_t *ctx);
44
45inline unsigned char aes_mul_manual(unsigned char a, unsigned char b); // use aes_mul instead
46
47void aes_subbytes(aes_ctx_t *ctx);
48void aes_shiftrows(aes_ctx_t *ctx);
49void aes_mixcolumns(aes_ctx_t *ctx);
50void aes_addroundkey(aes_ctx_t *ctx, int round);
51void aes_encrypt(aes_ctx_t *ctx, unsigned char input[16], unsigned char output[16]);
52
53void aes_invsubbytes(aes_ctx_t *ctx);
54void aes_invshiftrows(aes_ctx_t *ctx);
55void aes_invmixcolumns(aes_ctx_t *ctx);
56void aes_decrypt(aes_ctx_t *ctx, unsigned char input[16], unsigned char output[16]);
57
58void aes_free_ctx(aes_ctx_t *ctx);
59
60#endif //__aes_h
Note: See TracBrowser for help on using the repository browser.