libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_stream / aes256estream / hongjun / aes256.h
blob4e8eb40fb1283f5e2f1cb7df3434b472825ab313
1 /* aes256.h */
2 /* Hongjun Wu, January 2007*/
5 #include "ecrypt-sync.h"
6 #include "aes-table.h"
8 #include <stdio.h>
10 #define first_round(ctx,x0,y0) { \
11 u32 z0,t0,tem0; \
12 z0 = (x0) ^ ctx->round_key[0][0]; \
13 t0 = (u8) z0; \
14 tem0 = T0[t0]; \
15 (y0) = tem0 ^ ctx->first_round_output_x0; \
18 #define second_round(ctx,x0,y0,y1,y2,y3) { \
19 u32 t0,t7,t10,t13; \
20 u32 tem0,tem7,tem10,tem13; \
21 t0 = (u8)(x0); \
22 tem0 = T0[t0]; \
23 (y0) = tem0 ^ ctx->second_round_output[0]; \
24 t7 = (u8)((x0)>>24); \
25 tem7 = T3[t7]; \
26 (y1) = tem7 ^ ctx->second_round_output[1]; \
27 t10 = (u8)((x0)>>16); \
28 tem10 = T2[t10]; \
29 (y2) = tem10 ^ ctx->second_round_output[2]; \
30 t13 = (u8)((x0)>>8); \
31 tem13 = T1[t13];\
32 (y3) = tem13 ^ ctx->second_round_output[3]; \
35 #define round(ctx,x0,x1,x2,x3,y0,y1,y2,y3,r) { \
36 u32 t0,t1,t2,t3; \
37 u32 t4,t5,t6,t7; \
38 u32 t8,t9,t10,t11; \
39 u32 t12,t13,t14,t15;\
40 u32 tem0,tem1,tem2,tem3; \
41 u32 tem4,tem5,tem6,tem7; \
42 u32 tem8,tem9,tem10,tem11; \
43 u32 tem12,tem13,tem14,tem15;\
45 t0 = (u8)(x0); \
46 tem0 = T0[t0]; \
47 t1 = (u8)((x1)>>8); \
48 tem1 = tem0 ^ T1[t1]; \
49 t2 = (u8)((x2)>>16); \
50 tem2 = tem1 ^ T2[t2]; \
51 t3 = (u8)((x3)>>24); \
52 tem3 = tem2 ^ T3[t3]; \
53 (y0) = tem3 ^ ctx->round_key[r][0]; \
55 t4 = (u8)(x1); \
56 tem4 = T0[t4]; \
57 t5 = (u8)((x2)>>8); \
58 tem5 = tem4 ^ T1[t5]; \
59 t6 = (u8)((x3)>>16); \
60 tem6 = tem5 ^ T2[t6]; \
61 t7 = (u8)((x0)>>24); \
62 tem7 = tem6 ^ T3[t7]; \
63 (y1) = tem7 ^ ctx->round_key[r][1]; \
65 t8 = (u8)(x2); \
66 tem8 = T0[t8]; \
67 t9 = (u8)((x3)>>8); \
68 tem9 = tem8 ^ T1[t9]; \
69 t10 = (u8)((x0)>>16); \
70 tem10 = tem9 ^ T2[t10]; \
71 t11 = (u8)((x1)>>24); \
72 tem11 = tem10 ^ T3[t11];\
73 (y2) = tem11 ^ ctx->round_key[r][2]; \
75 t12 = (u8)(x3); \
76 tem12 = T0[t12]; \
77 t13 = (u8)((x0)>>8); \
78 tem13 = tem12 ^ T1[t13];\
79 t14 = (u8)((x1)>>16); \
80 tem14 = tem13 ^ T2[t14];\
81 t15 = (u8)((x2)>>24); \
82 tem15 = tem14 ^ T3[t15];\
83 (y3) = tem15 ^ ctx->round_key[r][3]; \
86 /* 22.14 cycles/byte*/
87 #define last_round(ctx,x0,x1,x2,x3,output,r) { \
88 u32 t0,t1,t2,t3; \
89 u32 t4,t5,t6,t7; \
90 u32 t8,t9,t10,t11; \
91 u32 t12,t13,t14,t15;\
93 t0 = (u8)(x0); \
94 output[0] = Sbox[t0]; \
95 t7 = (u8)((x0)>>24); \
96 output[7] = Sbox[t7]; \
97 t10 = (u8)((x0)>>16); \
98 output[10] = Sbox[t10]; \
99 t13 = (u8)((x0)>>8); \
100 output[13] = Sbox[t13]; \
102 t1 = (u8)((x1)>>8); \
103 output[1] = Sbox[t1]; \
104 t4 = (u8)(x1); \
105 output[4] = Sbox[t4]; \
106 t11 = (u8)((x1)>>24); \
107 output[11] = Sbox[t11]; \
108 t14 = (u8)((x1)>>16); \
109 output[14] = Sbox[t14]; \
111 t2 = (u8)((x2)>>16); \
112 output[2] = Sbox[t2]; \
113 t5 = (u8)((x2)>>8); \
114 output[5] = Sbox[t5]; \
115 t8 = (u8)(x2); \
116 output[8] = Sbox[t8]; \
117 t15 = (u8)((x2)>>24); \
118 output[15] = Sbox[t15]; \
120 t3 = (u8)((x3)>>24); \
121 output[3] = Sbox[t3]; \
122 t6 = (u8)((x3)>>16); \
123 output[6] = Sbox[t6]; \
124 t9 = (u8)((x3)>>8); \
125 output[9] = Sbox[t9]; \
126 t12 = (u8)(x3); \
127 output[12] = Sbox[t12]; \
131 ((u32*)output)[0] ^= ctx->round_key[r][0]; \
132 ((u32*)output)[1] ^= ctx->round_key[r][1]; \
133 ((u32*)output)[2] ^= ctx->round_key[r][2]; \
134 ((u32*)output)[3] ^= ctx->round_key[r][3]; \
138 #define aes256_enc_block(x,output,ctx) {\
139 u32 y0;\
140 u32 z0,z1,z2,z3;\
141 u32 a0,a1,a2,a3;\
142 u32 b0,b1,b2,b3;\
143 u32 c0,c1,c2,c3;\
144 u32 d0,d1,d2,d3;\
145 u32 e0,e1,e2,e3;\
146 u32 f0,f1,f2,f3;\
147 u32 g0,g1,g2,g3;\
148 u32 h0,h1,h2,h3;\
149 u32 i0,i1,i2,i3;\
150 u32 j0,j1,j2,j3;\
151 u32 k0,k1,k2,k3;\
152 first_round(ctx,x[0],y0);\
153 second_round(ctx,y0,z0,z1,z2,z3);\
154 round(ctx,z0,z1,z2,z3,a0,a1,a2,a3,3);\
155 round(ctx,a0,a1,a2,a3,b0,b1,b2,b3,4);\
156 round(ctx,b0,b1,b2,b3,c0,c1,c2,c3,5);\
157 round(ctx,c0,c1,c2,c3,d0,d1,d2,d3,6);\
158 round(ctx,d0,d1,d2,d3,e0,e1,e2,e3,7);\
159 round(ctx,e0,e1,e2,e3,f0,f1,f2,f3,8);\
160 round(ctx,f0,f1,f2,f3,g0,g1,g2,g3,9);\
161 round(ctx,g0,g1,g2,g3,h0,h1,h2,h3,10);\
162 round(ctx,h0,h1,h2,h3,i0,i1,i2,i3,11);\
163 round(ctx,i0,i1,i2,i3,j0,j1,j2,j3,12);\
164 round(ctx,j0,j1,j2,j3,k0,k1,k2,k3,13);\
165 last_round(ctx,k0,k1,k2,k3,(output),14);\
168 /*compute the intermediate values for the first two rounds*/
169 void partial_precompute_tworounds(ECRYPT_ctx* ctx)
171 u32 x0,x1,x2,x3,y0,y1,y2,y3;
173 x0 = ctx->counter[0] ^ ctx->round_key[0][0];
174 x1 = ctx->counter[1] ^ ctx->round_key[0][1];
175 x2 = ctx->counter[2] ^ ctx->round_key[0][2];
176 x3 = ctx->counter[3] ^ ctx->round_key[0][3];
177 x0 &= 0xffffff00;
178 round(ctx,x0,x1,x2,x3,y0,y1,y2,y3,1);
179 ctx->first_round_output_x0 = y0 ^ T0[0];
180 y0 = 0;
181 round(ctx,y0,y1,y2,y3,x0,x1,x2,x3,2);
182 ctx->second_round_output[0] = x0 ^ T0[0];
183 ctx->second_round_output[1] = x1 ^ T3[0];
184 ctx->second_round_output[2] = x2 ^ T2[0];
185 ctx->second_round_output[3] = x3 ^ T1[0];