00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef AES_I_H
00016 #define AES_I_H
00017
00018 #include "aes.h"
00019
00020
00021 #define AES_SMALL_TABLES
00022
00023 extern const u32 Te0[256];
00024 extern const u32 Te1[256];
00025 extern const u32 Te2[256];
00026 extern const u32 Te3[256];
00027 extern const u32 Te4[256];
00028 extern const u32 Td0[256];
00029 extern const u32 Td1[256];
00030 extern const u32 Td2[256];
00031 extern const u32 Td3[256];
00032 extern const u32 Td4[256];
00033 extern const u32 rcon[10];
00034 extern const u8 Td4s[256];
00035 extern const u8 rcons[10];
00036
00037 #ifndef AES_SMALL_TABLES
00038
00039 #define RCON(i) rcon[(i)]
00040
00041 #define TE0(i) Te0[((i) >> 24) & 0xff]
00042 #define TE1(i) Te1[((i) >> 16) & 0xff]
00043 #define TE2(i) Te2[((i) >> 8) & 0xff]
00044 #define TE3(i) Te3[(i) & 0xff]
00045 #define TE41(i) (Te4[((i) >> 24) & 0xff] & 0xff000000)
00046 #define TE42(i) (Te4[((i) >> 16) & 0xff] & 0x00ff0000)
00047 #define TE43(i) (Te4[((i) >> 8) & 0xff] & 0x0000ff00)
00048 #define TE44(i) (Te4[(i) & 0xff] & 0x000000ff)
00049 #define TE421(i) (Te4[((i) >> 16) & 0xff] & 0xff000000)
00050 #define TE432(i) (Te4[((i) >> 8) & 0xff] & 0x00ff0000)
00051 #define TE443(i) (Te4[(i) & 0xff] & 0x0000ff00)
00052 #define TE414(i) (Te4[((i) >> 24) & 0xff] & 0x000000ff)
00053 #define TE4(i) (Te4[(i)] & 0x000000ff)
00054
00055 #define TD0(i) Td0[((i) >> 24) & 0xff]
00056 #define TD1(i) Td1[((i) >> 16) & 0xff]
00057 #define TD2(i) Td2[((i) >> 8) & 0xff]
00058 #define TD3(i) Td3[(i) & 0xff]
00059 #define TD41(i) (Td4[((i) >> 24) & 0xff] & 0xff000000)
00060 #define TD42(i) (Td4[((i) >> 16) & 0xff] & 0x00ff0000)
00061 #define TD43(i) (Td4[((i) >> 8) & 0xff] & 0x0000ff00)
00062 #define TD44(i) (Td4[(i) & 0xff] & 0x000000ff)
00063 #define TD0_(i) Td0[(i) & 0xff]
00064 #define TD1_(i) Td1[(i) & 0xff]
00065 #define TD2_(i) Td2[(i) & 0xff]
00066 #define TD3_(i) Td3[(i) & 0xff]
00067
00068 #else
00069
00070 #define RCON(i) (rcons[(i)] << 24)
00071
00072 static inline u32 rotr(u32 val, int bits)
00073 {
00074 return (val >> bits) | (val << (32 - bits));
00075 }
00076
00077 #define TE0(i) Te0[((i) >> 24) & 0xff]
00078 #define TE1(i) rotr(Te0[((i) >> 16) & 0xff], 8)
00079 #define TE2(i) rotr(Te0[((i) >> 8) & 0xff], 16)
00080 #define TE3(i) rotr(Te0[(i) & 0xff], 24)
00081 #define TE41(i) ((Te0[((i) >> 24) & 0xff] << 8) & 0xff000000)
00082 #define TE42(i) (Te0[((i) >> 16) & 0xff] & 0x00ff0000)
00083 #define TE43(i) (Te0[((i) >> 8) & 0xff] & 0x0000ff00)
00084 #define TE44(i) ((Te0[(i) & 0xff] >> 8) & 0x000000ff)
00085 #define TE421(i) ((Te0[((i) >> 16) & 0xff] << 8) & 0xff000000)
00086 #define TE432(i) (Te0[((i) >> 8) & 0xff] & 0x00ff0000)
00087 #define TE443(i) (Te0[(i) & 0xff] & 0x0000ff00)
00088 #define TE414(i) ((Te0[((i) >> 24) & 0xff] >> 8) & 0x000000ff)
00089 #define TE4(i) ((Te0[(i)] >> 8) & 0x000000ff)
00090
00091 #define TD0(i) Td0[((i) >> 24) & 0xff]
00092 #define TD1(i) rotr(Td0[((i) >> 16) & 0xff], 8)
00093 #define TD2(i) rotr(Td0[((i) >> 8) & 0xff], 16)
00094 #define TD3(i) rotr(Td0[(i) & 0xff], 24)
00095 #define TD41(i) (Td4s[((i) >> 24) & 0xff] << 24)
00096 #define TD42(i) (Td4s[((i) >> 16) & 0xff] << 16)
00097 #define TD43(i) (Td4s[((i) >> 8) & 0xff] << 8)
00098 #define TD44(i) (Td4s[(i) & 0xff])
00099 #define TD0_(i) Td0[(i) & 0xff]
00100 #define TD1_(i) rotr(Td0[(i) & 0xff], 8)
00101 #define TD2_(i) rotr(Td0[(i) & 0xff], 16)
00102 #define TD3_(i) rotr(Td0[(i) & 0xff], 24)
00103
00104 #endif
00105
00106 #ifdef _MSC_VER
00107 #define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00)
00108 #define GETU32(p) SWAP(*((u32 *)(p)))
00109 #define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); }
00110 #else
00111 #define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ \
00112 ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3]))
00113 #define PUTU32(ct, st) { \
00114 (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); \
00115 (ct)[2] = (u8)((st) >> 8); (ct)[3] = (u8)(st); }
00116 #endif
00117
00118 #define AES_PRIV_SIZE (4 * 44)
00119
00120 void rijndaelKeySetupEnc(u32 rk[], const u8 cipherKey[]);
00121
00122 #endif