1e5b75505Sopenharmony_ci/*
2e5b75505Sopenharmony_ci * AES (Rijndael) cipher
3e5b75505Sopenharmony_ci * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
4e5b75505Sopenharmony_ci *
5e5b75505Sopenharmony_ci * This software may be distributed under the terms of the BSD license.
6e5b75505Sopenharmony_ci * See README for more details.
7e5b75505Sopenharmony_ci */
8e5b75505Sopenharmony_ci
9e5b75505Sopenharmony_ci#ifndef AES_I_H
10e5b75505Sopenharmony_ci#define AES_I_H
11e5b75505Sopenharmony_ci
12e5b75505Sopenharmony_ci#include "aes.h"
13e5b75505Sopenharmony_ci
14e5b75505Sopenharmony_ci/* #define FULL_UNROLL */
15e5b75505Sopenharmony_ci#define AES_SMALL_TABLES
16e5b75505Sopenharmony_ci
17e5b75505Sopenharmony_ciextern const u32 Te0[256];
18e5b75505Sopenharmony_ciextern const u32 Te1[256];
19e5b75505Sopenharmony_ciextern const u32 Te2[256];
20e5b75505Sopenharmony_ciextern const u32 Te3[256];
21e5b75505Sopenharmony_ciextern const u32 Te4[256];
22e5b75505Sopenharmony_ciextern const u32 Td0[256];
23e5b75505Sopenharmony_ciextern const u32 Td1[256];
24e5b75505Sopenharmony_ciextern const u32 Td2[256];
25e5b75505Sopenharmony_ciextern const u32 Td3[256];
26e5b75505Sopenharmony_ciextern const u32 Td4[256];
27e5b75505Sopenharmony_ciextern const u32 rcon[10];
28e5b75505Sopenharmony_ciextern const u8 Td4s[256];
29e5b75505Sopenharmony_ciextern const u8 rcons[10];
30e5b75505Sopenharmony_ci
31e5b75505Sopenharmony_ci#ifndef AES_SMALL_TABLES
32e5b75505Sopenharmony_ci
33e5b75505Sopenharmony_ci#define RCON(i) rcon[(i)]
34e5b75505Sopenharmony_ci
35e5b75505Sopenharmony_ci#define TE0(i) Te0[((i) >> 24) & 0xff]
36e5b75505Sopenharmony_ci#define TE1(i) Te1[((i) >> 16) & 0xff]
37e5b75505Sopenharmony_ci#define TE2(i) Te2[((i) >> 8) & 0xff]
38e5b75505Sopenharmony_ci#define TE3(i) Te3[(i) & 0xff]
39e5b75505Sopenharmony_ci#define TE41(i) (Te4[((i) >> 24) & 0xff] & 0xff000000)
40e5b75505Sopenharmony_ci#define TE42(i) (Te4[((i) >> 16) & 0xff] & 0x00ff0000)
41e5b75505Sopenharmony_ci#define TE43(i) (Te4[((i) >> 8) & 0xff] & 0x0000ff00)
42e5b75505Sopenharmony_ci#define TE44(i) (Te4[(i) & 0xff] & 0x000000ff)
43e5b75505Sopenharmony_ci#define TE421(i) (Te4[((i) >> 16) & 0xff] & 0xff000000)
44e5b75505Sopenharmony_ci#define TE432(i) (Te4[((i) >> 8) & 0xff] & 0x00ff0000)
45e5b75505Sopenharmony_ci#define TE443(i) (Te4[(i) & 0xff] & 0x0000ff00)
46e5b75505Sopenharmony_ci#define TE414(i) (Te4[((i) >> 24) & 0xff] & 0x000000ff)
47e5b75505Sopenharmony_ci#define TE411(i) (Te4[((i) >> 24) & 0xff] & 0xff000000)
48e5b75505Sopenharmony_ci#define TE422(i) (Te4[((i) >> 16) & 0xff] & 0x00ff0000)
49e5b75505Sopenharmony_ci#define TE433(i) (Te4[((i) >> 8) & 0xff] & 0x0000ff00)
50e5b75505Sopenharmony_ci#define TE444(i) (Te4[(i) & 0xff] & 0x000000ff)
51e5b75505Sopenharmony_ci#define TE4(i) (Te4[(i)] & 0x000000ff)
52e5b75505Sopenharmony_ci
53e5b75505Sopenharmony_ci#define TD0(i) Td0[((i) >> 24) & 0xff]
54e5b75505Sopenharmony_ci#define TD1(i) Td1[((i) >> 16) & 0xff]
55e5b75505Sopenharmony_ci#define TD2(i) Td2[((i) >> 8) & 0xff]
56e5b75505Sopenharmony_ci#define TD3(i) Td3[(i) & 0xff]
57e5b75505Sopenharmony_ci#define TD41(i) (Td4[((i) >> 24) & 0xff] & 0xff000000)
58e5b75505Sopenharmony_ci#define TD42(i) (Td4[((i) >> 16) & 0xff] & 0x00ff0000)
59e5b75505Sopenharmony_ci#define TD43(i) (Td4[((i) >> 8) & 0xff] & 0x0000ff00)
60e5b75505Sopenharmony_ci#define TD44(i) (Td4[(i) & 0xff] & 0x000000ff)
61e5b75505Sopenharmony_ci#define TD0_(i) Td0[(i) & 0xff]
62e5b75505Sopenharmony_ci#define TD1_(i) Td1[(i) & 0xff]
63e5b75505Sopenharmony_ci#define TD2_(i) Td2[(i) & 0xff]
64e5b75505Sopenharmony_ci#define TD3_(i) Td3[(i) & 0xff]
65e5b75505Sopenharmony_ci
66e5b75505Sopenharmony_ci#else /* AES_SMALL_TABLES */
67e5b75505Sopenharmony_ci
68e5b75505Sopenharmony_ci#define RCON(i) ((u32) rcons[(i)] << 24)
69e5b75505Sopenharmony_ci
70e5b75505Sopenharmony_cistatic inline u32 rotr(u32 val, int bits)
71e5b75505Sopenharmony_ci{
72e5b75505Sopenharmony_ci	return (val >> bits) | (val << (32 - bits));
73e5b75505Sopenharmony_ci}
74e5b75505Sopenharmony_ci
75e5b75505Sopenharmony_ci#define TE0(i) Te0[((i) >> 24) & 0xff]
76e5b75505Sopenharmony_ci#define TE1(i) rotr(Te0[((i) >> 16) & 0xff], 8)
77e5b75505Sopenharmony_ci#define TE2(i) rotr(Te0[((i) >> 8) & 0xff], 16)
78e5b75505Sopenharmony_ci#define TE3(i) rotr(Te0[(i) & 0xff], 24)
79e5b75505Sopenharmony_ci#define TE41(i) ((Te0[((i) >> 24) & 0xff] << 8) & 0xff000000)
80e5b75505Sopenharmony_ci#define TE42(i) (Te0[((i) >> 16) & 0xff] & 0x00ff0000)
81e5b75505Sopenharmony_ci#define TE43(i) (Te0[((i) >> 8) & 0xff] & 0x0000ff00)
82e5b75505Sopenharmony_ci#define TE44(i) ((Te0[(i) & 0xff] >> 8) & 0x000000ff)
83e5b75505Sopenharmony_ci#define TE421(i) ((Te0[((i) >> 16) & 0xff] << 8) & 0xff000000)
84e5b75505Sopenharmony_ci#define TE432(i) (Te0[((i) >> 8) & 0xff] & 0x00ff0000)
85e5b75505Sopenharmony_ci#define TE443(i) (Te0[(i) & 0xff] & 0x0000ff00)
86e5b75505Sopenharmony_ci#define TE414(i) ((Te0[((i) >> 24) & 0xff] >> 8) & 0x000000ff)
87e5b75505Sopenharmony_ci#define TE411(i) ((Te0[((i) >> 24) & 0xff] << 8) & 0xff000000)
88e5b75505Sopenharmony_ci#define TE422(i) (Te0[((i) >> 16) & 0xff] & 0x00ff0000)
89e5b75505Sopenharmony_ci#define TE433(i) (Te0[((i) >> 8) & 0xff] & 0x0000ff00)
90e5b75505Sopenharmony_ci#define TE444(i) ((Te0[(i) & 0xff] >> 8) & 0x000000ff)
91e5b75505Sopenharmony_ci#define TE4(i) ((Te0[(i)] >> 8) & 0x000000ff)
92e5b75505Sopenharmony_ci
93e5b75505Sopenharmony_ci#define TD0(i) Td0[((i) >> 24) & 0xff]
94e5b75505Sopenharmony_ci#define TD1(i) rotr(Td0[((i) >> 16) & 0xff], 8)
95e5b75505Sopenharmony_ci#define TD2(i) rotr(Td0[((i) >> 8) & 0xff], 16)
96e5b75505Sopenharmony_ci#define TD3(i) rotr(Td0[(i) & 0xff], 24)
97e5b75505Sopenharmony_ci#define TD41(i) ((u32) Td4s[((i) >> 24) & 0xff] << 24)
98e5b75505Sopenharmony_ci#define TD42(i) ((u32) Td4s[((i) >> 16) & 0xff] << 16)
99e5b75505Sopenharmony_ci#define TD43(i) ((u32) Td4s[((i) >> 8) & 0xff] << 8)
100e5b75505Sopenharmony_ci#define TD44(i) ((u32) Td4s[(i) & 0xff])
101e5b75505Sopenharmony_ci#define TD0_(i) Td0[(i) & 0xff]
102e5b75505Sopenharmony_ci#define TD1_(i) rotr(Td0[(i) & 0xff], 8)
103e5b75505Sopenharmony_ci#define TD2_(i) rotr(Td0[(i) & 0xff], 16)
104e5b75505Sopenharmony_ci#define TD3_(i) rotr(Td0[(i) & 0xff], 24)
105e5b75505Sopenharmony_ci
106e5b75505Sopenharmony_ci#endif /* AES_SMALL_TABLES */
107e5b75505Sopenharmony_ci
108e5b75505Sopenharmony_ci#ifdef _MSC_VER
109e5b75505Sopenharmony_ci#define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00)
110e5b75505Sopenharmony_ci#define GETU32(p) SWAP(*((u32 *)(p)))
111e5b75505Sopenharmony_ci#define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); }
112e5b75505Sopenharmony_ci#else
113e5b75505Sopenharmony_ci#define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ \
114e5b75505Sopenharmony_ci((u32)(pt)[2] <<  8) ^ ((u32)(pt)[3]))
115e5b75505Sopenharmony_ci#define PUTU32(ct, st) { \
116e5b75505Sopenharmony_ci(ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); \
117e5b75505Sopenharmony_ci(ct)[2] = (u8)((st) >>  8); (ct)[3] = (u8)(st); }
118e5b75505Sopenharmony_ci#endif
119e5b75505Sopenharmony_ci
120e5b75505Sopenharmony_ci#define AES_PRIV_SIZE (4 * 4 * 15 + 4)
121e5b75505Sopenharmony_ci#define AES_PRIV_NR_POS (4 * 15)
122e5b75505Sopenharmony_ci
123e5b75505Sopenharmony_ciint rijndaelKeySetupEnc(u32 rk[], const u8 cipherKey[], int keyBits);
124e5b75505Sopenharmony_ci
125e5b75505Sopenharmony_ci#endif /* AES_I_H */
126