source: trunk/src/rijndael-alg-fst.c@ 226

Last change on this file since 226 was 1, checked in by katerina, 19 years ago

Initial import

File size: 8.4 KB
Line 
1/*
2 * rijndael-alg-fst.c v2.3 April '2000
3 *
4 * Optimised ANSI C code
5 *
6 * authors: v1.0: Antoon Bosselaers
7 * v2.0: Vincent Rijmen
8 * v2.3: Paulo Barreto
9 *
10 * This code is placed in the public domain.
11 */
12
13#include "config_xor.h"
14
15#include <stdio.h>
16#include <stdlib.h>
17
18#ifdef SH_ENCRYPT
19
20#include "rijndael-alg-fst.h"
21
22#include "rijndael-boxes-fst.h"
23
24int rijndaelKeySched(word8 k[MAXKC][4], word8 W[MAXROUNDS+1][4][4], int ROUNDS) {
25 /* Calculate the necessary round keys
26 * The number of calculations depends on keyBits and blockBits
27 */
28 int j, r, t, rconpointer = 0;
29 word8 tk[MAXKC][4];
30 int KC = ROUNDS - 6;
31
32 for (j = KC-1; j >= 0; j--) {
33 *((word32*)tk[j]) = *((word32*)k[j]);
34 }
35 r = 0;
36 t = 0;
37 /* copy values into round key array */
38 for (j = 0; (j < KC) && (r < ROUNDS + 1); ) {
39 for (; (j < KC) && (t < 4); j++, t++) {
40 *((word32*)W[r][t]) = *((word32*)tk[j]);
41 }
42 if (t == 4) {
43 r++;
44 t = 0;
45 }
46 }
47
48 while (r < ROUNDS + 1) { /* while not enough round key material calculated */
49 /* calculate new values */
50 tk[0][0] ^= S[tk[KC-1][1]];
51 tk[0][1] ^= S[tk[KC-1][2]];
52 tk[0][2] ^= S[tk[KC-1][3]];
53 tk[0][3] ^= S[tk[KC-1][0]];
54 tk[0][0] ^= rcon[rconpointer++];
55
56 if (KC != 8) {
57 for (j = 1; j < KC; j++) {
58 *((word32*)tk[j]) ^= *((word32*)tk[j-1]);
59 }
60 } else {
61 for (j = 1; j < KC/2; j++) {
62 *((word32*)tk[j]) ^= *((word32*)tk[j-1]);
63 }
64 tk[KC/2][0] ^= S[tk[KC/2 - 1][0]];
65 tk[KC/2][1] ^= S[tk[KC/2 - 1][1]];
66 tk[KC/2][2] ^= S[tk[KC/2 - 1][2]];
67 tk[KC/2][3] ^= S[tk[KC/2 - 1][3]];
68 for (j = KC/2 + 1; j < KC; j++) {
69 *((word32*)tk[j]) ^= *((word32*)tk[j-1]);
70 }
71 }
72 /* copy values into round key array */
73 for (j = 0; (j < KC) && (r < ROUNDS + 1); ) {
74 for (; (j < KC) && (t < 4); j++, t++) {
75 *((word32*)W[r][t]) = *((word32*)tk[j]);
76 }
77 if (t == 4) {
78 r++;
79 t = 0;
80 }
81 }
82 }
83 return 0;
84}
85
86int rijndaelKeyEncToDec(word8 W[MAXROUNDS+1][4][4], int ROUNDS) {
87 int r;
88 word8 *w;
89
90 for (r = 1; r < ROUNDS; r++) {
91 w = W[r][0];
92 *((word32*)w) =
93 *((word32*)U1[w[0]])
94 ^ *((word32*)U2[w[1]])
95 ^ *((word32*)U3[w[2]])
96 ^ *((word32*)U4[w[3]]);
97
98 w = W[r][1];
99 *((word32*)w) =
100 *((word32*)U1[w[0]])
101 ^ *((word32*)U2[w[1]])
102 ^ *((word32*)U3[w[2]])
103 ^ *((word32*)U4[w[3]]);
104
105 w = W[r][2];
106 *((word32*)w) =
107 *((word32*)U1[w[0]])
108 ^ *((word32*)U2[w[1]])
109 ^ *((word32*)U3[w[2]])
110 ^ *((word32*)U4[w[3]]);
111
112 w = W[r][3];
113 *((word32*)w) =
114 *((word32*)U1[w[0]])
115 ^ *((word32*)U2[w[1]])
116 ^ *((word32*)U3[w[2]])
117 ^ *((word32*)U4[w[3]]);
118 }
119 return 0;
120}
121
122/**
123 * Encrypt a single block.
124 */
125int rijndaelEncrypt(word8 a[16], word8 b[16], word8 rk[MAXROUNDS+1][4][4], int ROUNDS) {
126 int r;
127 word8 temp[4][4];
128
129 *((word32*)temp[0]) = *((word32*)(a )) ^ *((word32*)rk[0][0]);
130 *((word32*)temp[1]) = *((word32*)(a+ 4)) ^ *((word32*)rk[0][1]);
131 *((word32*)temp[2]) = *((word32*)(a+ 8)) ^ *((word32*)rk[0][2]);
132 *((word32*)temp[3]) = *((word32*)(a+12)) ^ *((word32*)rk[0][3]);
133 *((word32*)(b )) = *((word32*)T1[temp[0][0]])
134 ^ *((word32*)T2[temp[1][1]])
135 ^ *((word32*)T3[temp[2][2]])
136 ^ *((word32*)T4[temp[3][3]]);
137 *((word32*)(b + 4)) = *((word32*)T1[temp[1][0]])
138 ^ *((word32*)T2[temp[2][1]])
139 ^ *((word32*)T3[temp[3][2]])
140 ^ *((word32*)T4[temp[0][3]]);
141 *((word32*)(b + 8)) = *((word32*)T1[temp[2][0]])
142 ^ *((word32*)T2[temp[3][1]])
143 ^ *((word32*)T3[temp[0][2]])
144 ^ *((word32*)T4[temp[1][3]]);
145 *((word32*)(b +12)) = *((word32*)T1[temp[3][0]])
146 ^ *((word32*)T2[temp[0][1]])
147 ^ *((word32*)T3[temp[1][2]])
148 ^ *((word32*)T4[temp[2][3]]);
149 for (r = 1; r < ROUNDS-1; r++) {
150 *((word32*)temp[0]) = *((word32*)(b )) ^ *((word32*)rk[r][0]);
151 *((word32*)temp[1]) = *((word32*)(b+ 4)) ^ *((word32*)rk[r][1]);
152 *((word32*)temp[2]) = *((word32*)(b+ 8)) ^ *((word32*)rk[r][2]);
153 *((word32*)temp[3]) = *((word32*)(b+12)) ^ *((word32*)rk[r][3]);
154
155 *((word32*)(b )) = *((word32*)T1[temp[0][0]])
156 ^ *((word32*)T2[temp[1][1]])
157 ^ *((word32*)T3[temp[2][2]])
158 ^ *((word32*)T4[temp[3][3]]);
159 *((word32*)(b + 4)) = *((word32*)T1[temp[1][0]])
160 ^ *((word32*)T2[temp[2][1]])
161 ^ *((word32*)T3[temp[3][2]])
162 ^ *((word32*)T4[temp[0][3]]);
163 *((word32*)(b + 8)) = *((word32*)T1[temp[2][0]])
164 ^ *((word32*)T2[temp[3][1]])
165 ^ *((word32*)T3[temp[0][2]])
166 ^ *((word32*)T4[temp[1][3]]);
167 *((word32*)(b +12)) = *((word32*)T1[temp[3][0]])
168 ^ *((word32*)T2[temp[0][1]])
169 ^ *((word32*)T3[temp[1][2]])
170 ^ *((word32*)T4[temp[2][3]]);
171 }
172 /* last round is special */
173 *((word32*)temp[0]) = *((word32*)(b )) ^ *((word32*)rk[ROUNDS-1][0]);
174 *((word32*)temp[1]) = *((word32*)(b+ 4)) ^ *((word32*)rk[ROUNDS-1][1]);
175 *((word32*)temp[2]) = *((word32*)(b+ 8)) ^ *((word32*)rk[ROUNDS-1][2]);
176 *((word32*)temp[3]) = *((word32*)(b+12)) ^ *((word32*)rk[ROUNDS-1][3]);
177 b[ 0] = T1[temp[0][0]][1];
178 b[ 1] = T1[temp[1][1]][1];
179 b[ 2] = T1[temp[2][2]][1];
180 b[ 3] = T1[temp[3][3]][1];
181 b[ 4] = T1[temp[1][0]][1];
182 b[ 5] = T1[temp[2][1]][1];
183 b[ 6] = T1[temp[3][2]][1];
184 b[ 7] = T1[temp[0][3]][1];
185 b[ 8] = T1[temp[2][0]][1];
186 b[ 9] = T1[temp[3][1]][1];
187 b[10] = T1[temp[0][2]][1];
188 b[11] = T1[temp[1][3]][1];
189 b[12] = T1[temp[3][0]][1];
190 b[13] = T1[temp[0][1]][1];
191 b[14] = T1[temp[1][2]][1];
192 b[15] = T1[temp[2][3]][1];
193 *((word32*)(b )) ^= *((word32*)rk[ROUNDS][0]);
194 *((word32*)(b+ 4)) ^= *((word32*)rk[ROUNDS][1]);
195 *((word32*)(b+ 8)) ^= *((word32*)rk[ROUNDS][2]);
196 *((word32*)(b+12)) ^= *((word32*)rk[ROUNDS][3]);
197
198 return 0;
199}
200
201/**
202 * Decrypt a single block.
203 */
204int rijndaelDecrypt(word8 a[16], word8 b[16], word8 rk[MAXROUNDS+1][4][4], int ROUNDS) {
205 int r;
206 word8 temp[4][4];
207
208 *((word32*)temp[0]) = *((word32*)(a )) ^ *((word32*)rk[ROUNDS][0]);
209 *((word32*)temp[1]) = *((word32*)(a+ 4)) ^ *((word32*)rk[ROUNDS][1]);
210 *((word32*)temp[2]) = *((word32*)(a+ 8)) ^ *((word32*)rk[ROUNDS][2]);
211 *((word32*)temp[3]) = *((word32*)(a+12)) ^ *((word32*)rk[ROUNDS][3]);
212
213 *((word32*)(b )) = *((word32*)T5[temp[0][0]])
214 ^ *((word32*)T6[temp[3][1]])
215 ^ *((word32*)T7[temp[2][2]])
216 ^ *((word32*)T8[temp[1][3]]);
217 *((word32*)(b+ 4)) = *((word32*)T5[temp[1][0]])
218 ^ *((word32*)T6[temp[0][1]])
219 ^ *((word32*)T7[temp[3][2]])
220 ^ *((word32*)T8[temp[2][3]]);
221 *((word32*)(b+ 8)) = *((word32*)T5[temp[2][0]])
222 ^ *((word32*)T6[temp[1][1]])
223 ^ *((word32*)T7[temp[0][2]])
224 ^ *((word32*)T8[temp[3][3]]);
225 *((word32*)(b+12)) = *((word32*)T5[temp[3][0]])
226 ^ *((word32*)T6[temp[2][1]])
227 ^ *((word32*)T7[temp[1][2]])
228 ^ *((word32*)T8[temp[0][3]]);
229 for (r = ROUNDS-1; r > 1; r--) {
230 *((word32*)temp[0]) = *((word32*)(b )) ^ *((word32*)rk[r][0]);
231 *((word32*)temp[1]) = *((word32*)(b+ 4)) ^ *((word32*)rk[r][1]);
232 *((word32*)temp[2]) = *((word32*)(b+ 8)) ^ *((word32*)rk[r][2]);
233 *((word32*)temp[3]) = *((word32*)(b+12)) ^ *((word32*)rk[r][3]);
234 *((word32*)(b )) = *((word32*)T5[temp[0][0]])
235 ^ *((word32*)T6[temp[3][1]])
236 ^ *((word32*)T7[temp[2][2]])
237 ^ *((word32*)T8[temp[1][3]]);
238 *((word32*)(b+ 4)) = *((word32*)T5[temp[1][0]])
239 ^ *((word32*)T6[temp[0][1]])
240 ^ *((word32*)T7[temp[3][2]])
241 ^ *((word32*)T8[temp[2][3]]);
242 *((word32*)(b+ 8)) = *((word32*)T5[temp[2][0]])
243 ^ *((word32*)T6[temp[1][1]])
244 ^ *((word32*)T7[temp[0][2]])
245 ^ *((word32*)T8[temp[3][3]]);
246 *((word32*)(b+12)) = *((word32*)T5[temp[3][0]])
247 ^ *((word32*)T6[temp[2][1]])
248 ^ *((word32*)T7[temp[1][2]])
249 ^ *((word32*)T8[temp[0][3]]);
250 }
251 /* last round is special */
252 *((word32*)temp[0]) = *((word32*)(b )) ^ *((word32*)rk[1][0]);
253 *((word32*)temp[1]) = *((word32*)(b+ 4)) ^ *((word32*)rk[1][1]);
254 *((word32*)temp[2]) = *((word32*)(b+ 8)) ^ *((word32*)rk[1][2]);
255 *((word32*)temp[3]) = *((word32*)(b+12)) ^ *((word32*)rk[1][3]);
256 b[ 0] = S5[temp[0][0]];
257 b[ 1] = S5[temp[3][1]];
258 b[ 2] = S5[temp[2][2]];
259 b[ 3] = S5[temp[1][3]];
260 b[ 4] = S5[temp[1][0]];
261 b[ 5] = S5[temp[0][1]];
262 b[ 6] = S5[temp[3][2]];
263 b[ 7] = S5[temp[2][3]];
264 b[ 8] = S5[temp[2][0]];
265 b[ 9] = S5[temp[1][1]];
266 b[10] = S5[temp[0][2]];
267 b[11] = S5[temp[3][3]];
268 b[12] = S5[temp[3][0]];
269 b[13] = S5[temp[2][1]];
270 b[14] = S5[temp[1][2]];
271 b[15] = S5[temp[0][3]];
272 *((word32*)(b )) ^= *((word32*)rk[0][0]);
273 *((word32*)(b+ 4)) ^= *((word32*)rk[0][1]);
274 *((word32*)(b+ 8)) ^= *((word32*)rk[0][2]);
275 *((word32*)(b+12)) ^= *((word32*)rk[0][3]);
276
277 return 0;
278}
279
280#endif
Note: See TracBrowser for help on using the repository browser.