Get the basic random number generator functions to compile
[akaros.git] / kern / lib / random / rijndael.c
1 /*      $OpenBSD: rijndael.c,v 1.6 2000/12/09 18:51:34 markus Exp $ */
2
3 /* contrib/pgcrypto/rijndael.c */
4
5 /* This is an independent implementation of the encryption algorithm:   */
6 /*                                                                                                                                              */
7 /*                 RIJNDAEL by Joan Daemen and Vincent Rijmen                                   */
8 /*                                                                                                                                              */
9 /* which is a candidate algorithm in the Advanced Encryption Standard   */
10 /* programme of the US National Institute of Standards and Technology.  */
11 /*                                                                                                                                              */
12 /* Copyright in this implementation is held by Dr B R Gladman but I             */
13 /* hereby give permission for its free direct or derivative use subject */
14 /* to acknowledgment of its origin and compliance with any conditions   */
15 /* that the originators of the algorithm place on its exploitation.             */
16 /*                                                                                                                                              */
17 /* Dr Brian Gladman (gladman@seven77.demon.co.uk) 14th January 1999             */
18
19 /* Timing data for Rijndael (rijndael.c)
20
21 Algorithm: rijndael (rijndael.c)
22
23 128 bit key:
24 Key Setup:        305/1389 cycles (encrypt/decrypt)
25 Encrypt:           374 cycles =    68.4 mbits/sec
26 Decrypt:           352 cycles =    72.7 mbits/sec
27 Mean:              363 cycles =    70.5 mbits/sec
28
29 192 bit key:
30 Key Setup:        277/1595 cycles (encrypt/decrypt)
31 Encrypt:           439 cycles =    58.3 mbits/sec
32 Decrypt:           425 cycles =    60.2 mbits/sec
33 Mean:              432 cycles =    59.3 mbits/sec
34
35 256 bit key:
36 Key Setup:        374/1960 cycles (encrypt/decrypt)
37 Encrypt:           502 cycles =    51.0 mbits/sec
38 Decrypt:           498 cycles =    51.4 mbits/sec
39 Mean:              500 cycles =    51.2 mbits/sec
40
41 */
42
43 #include <random/rijndael.h>
44
45 #include "rijndael.tbl"
46
47 /* 3. Basic macros for speeding up generic operations                           */
48
49 /* Circular rotate of 32 bit values                                                                     */
50
51 #define rotr(x, n) (((x) >> ((int)(n))) | ((x) << (32 - (int)(n))))
52 #define rotl(x, n) (((x) << ((int)(n))) | ((x) >> (32 - (int)(n))))
53
54 /* Invert byte order in a 32 bit variable                                                       */
55
56 #define bswap(x) ((rotl((x), 8) & 0x00ff00ff) | (rotr((x), 8) & 0xff00ff00))
57
58 /* Extract byte from a 32 bit quantity (little endian notation)         */
59
60 #define byte(x, n) ((uint8_t)((x) >> (8 * (n))))
61
62 #define io_swap(x) (x)
63
64 #define ff_mult(a, b)                                                          \
65         ((a) && (b) ? pow_tab[(log_tab[a] + log_tab[b]) % 255] : 0)
66
67 #define f_rn(bo, bi, n, k)                                                     \
68         (bo)[n] = ft_tab[0][byte((bi)[n], 0)] ^                                    \
69                           ft_tab[1][byte((bi)[((n) + 1) & 3], 1)] ^                        \
70                           ft_tab[2][byte((bi)[((n) + 2) & 3], 2)] ^                        \
71                           ft_tab[3][byte((bi)[((n) + 3) & 3], 3)] ^ *((k) + (n))
72
73 #define i_rn(bo, bi, n, k)                                                     \
74         (bo)[n] = it_tab[0][byte((bi)[n], 0)] ^                                    \
75                           it_tab[1][byte((bi)[((n) + 3) & 3], 1)] ^                        \
76                           it_tab[2][byte((bi)[((n) + 2) & 3], 2)] ^                        \
77                           it_tab[3][byte((bi)[((n) + 1) & 3], 3)] ^ *((k) + (n))
78
79 #define ls_box(x)                                                              \
80         (fl_tab[0][byte(x, 0)] ^ fl_tab[1][byte(x, 1)] ^ fl_tab[2][byte(x, 2)] ^   \
81          fl_tab[3][byte(x, 3)])
82
83 #define f_rl(bo, bi, n, k)                                                     \
84         (bo)[n] = fl_tab[0][byte((bi)[n], 0)] ^                                    \
85                           fl_tab[1][byte((bi)[((n) + 1) & 3], 1)] ^                        \
86                           fl_tab[2][byte((bi)[((n) + 2) & 3], 2)] ^                        \
87                           fl_tab[3][byte((bi)[((n) + 3) & 3], 3)] ^ *((k) + (n))
88
89 #define i_rl(bo, bi, n, k)                                                     \
90         (bo)[n] = il_tab[0][byte((bi)[n], 0)] ^                                    \
91                           il_tab[1][byte((bi)[((n) + 3) & 3], 1)] ^                        \
92                           il_tab[2][byte((bi)[((n) + 2) & 3], 2)] ^                        \
93                           il_tab[3][byte((bi)[((n) + 1) & 3], 3)] ^ *((k) + (n))
94
95 #define star_x(x) (((x)&0x7f7f7f7f) << 1) ^ ((((x)&0x80808080) >> 7) * 0x1b)
96
97 #define imix_col(y, x)                                                         \
98         do {                                                                       \
99                 u = star_x(x);                                                         \
100                 v = star_x(u);                                                         \
101                 w = star_x(v);                                                         \
102                 t = w ^ (x);                                                           \
103                 (y) = u ^ v ^ w;                                                       \
104                 (y) ^= rotr(u ^ t, 8) ^ rotr(v ^ t, 16) ^ rotr(t, 24);                 \
105         } while (0)
106
107 /* initialise the key schedule from the user supplied key       */
108
109 #define loop4(i)                                                               \
110         do {                                                                       \
111                 t = ls_box(rotr(t, 8)) ^ rco_tab[i];                                   \
112                 t ^= e_key[4 * i];                                                     \
113                 e_key[4 * i + 4] = t;                                                  \
114                 t ^= e_key[4 * i + 1];                                                 \
115                 e_key[4 * i + 5] = t;                                                  \
116                 t ^= e_key[4 * i + 2];                                                 \
117                 e_key[4 * i + 6] = t;                                                  \
118                 t ^= e_key[4 * i + 3];                                                 \
119                 e_key[4 * i + 7] = t;                                                  \
120         } while (0)
121
122 #define loop6(i)                                                               \
123         do {                                                                       \
124                 t = ls_box(rotr(t, 8)) ^ rco_tab[i];                                   \
125                 t ^= e_key[6 * (i)];                                                   \
126                 e_key[6 * (i) + 6] = t;                                                \
127                 t ^= e_key[6 * (i) + 1];                                               \
128                 e_key[6 * (i) + 7] = t;                                                \
129                 t ^= e_key[6 * (i) + 2];                                               \
130                 e_key[6 * (i) + 8] = t;                                                \
131                 t ^= e_key[6 * (i) + 3];                                               \
132                 e_key[6 * (i) + 9] = t;                                                \
133                 t ^= e_key[6 * (i) + 4];                                               \
134                 e_key[6 * (i) + 10] = t;                                               \
135                 t ^= e_key[6 * (i) + 5];                                               \
136                 e_key[6 * (i) + 11] = t;                                               \
137         } while (0)
138
139 #define loop8(i)                                                               \
140         do {                                                                       \
141                 t = ls_box(rotr(t, 8)) ^ rco_tab[i];                                   \
142                 t ^= e_key[8 * (i)];                                                   \
143                 e_key[8 * (i) + 8] = t;                                                \
144                 t ^= e_key[8 * (i) + 1];                                               \
145                 e_key[8 * (i) + 9] = t;                                                \
146                 t ^= e_key[8 * (i) + 2];                                               \
147                 e_key[8 * (i) + 10] = t;                                               \
148                 t ^= e_key[8 * (i) + 3];                                               \
149                 e_key[8 * (i) + 11] = t;                                               \
150                 t = e_key[8 * (i) + 4] ^ ls_box(t);                                    \
151                 e_key[8 * (i) + 12] = t;                                               \
152                 t ^= e_key[8 * (i) + 5];                                               \
153                 e_key[8 * (i) + 13] = t;                                               \
154                 t ^= e_key[8 * (i) + 6];                                               \
155                 e_key[8 * (i) + 14] = t;                                               \
156                 t ^= e_key[8 * (i) + 7];                                               \
157                 e_key[8 * (i) + 15] = t;                                               \
158         } while (0)
159
160 rijndaelCtx *rijndael_set_key(rijndaelCtx *ctx, const uint32_t *in_key,
161                                                           const uint32_t key_len, int encrypt)
162 {
163         uint32_t i, t, u, v, w;
164         uint32_t *e_key = ctx->e_key;
165         uint32_t *d_key = ctx->d_key;
166
167         ctx->decrypt = !encrypt;
168
169         ctx->k_len = (key_len + 31) / 32;
170
171         e_key[0] = io_swap(in_key[0]);
172         e_key[1] = io_swap(in_key[1]);
173         e_key[2] = io_swap(in_key[2]);
174         e_key[3] = io_swap(in_key[3]);
175
176         switch (ctx->k_len) {
177         case 4:
178                 t = e_key[3];
179                 for (i = 0; i < 10; ++i)
180                         loop4(i);
181                 break;
182
183         case 6:
184                 e_key[4] = io_swap(in_key[4]);
185                 t = e_key[5] = io_swap(in_key[5]);
186                 for (i = 0; i < 8; ++i)
187                         loop6(i);
188                 break;
189
190         case 8:
191                 e_key[4] = io_swap(in_key[4]);
192                 e_key[5] = io_swap(in_key[5]);
193                 e_key[6] = io_swap(in_key[6]);
194                 t = e_key[7] = io_swap(in_key[7]);
195                 for (i = 0; i < 7; ++i)
196                         loop8(i);
197                 break;
198         }
199
200         if (!encrypt) {
201                 d_key[0] = e_key[0];
202                 d_key[1] = e_key[1];
203                 d_key[2] = e_key[2];
204                 d_key[3] = e_key[3];
205
206                 for (i = 4; i < 4 * ctx->k_len + 24; ++i)
207                         imix_col(d_key[i], e_key[i]);
208         }
209
210         return ctx;
211 }
212
213 /* encrypt a block of text      */
214
215 #define f_nround(bo, bi, k)                                                    \
216         do {                                                                       \
217                 f_rn(bo, bi, 0, k);                                                    \
218                 f_rn(bo, bi, 1, k);                                                    \
219                 f_rn(bo, bi, 2, k);                                                    \
220                 f_rn(bo, bi, 3, k);                                                    \
221                 k += 4;                                                                \
222         } while (0)
223
224 #define f_lround(bo, bi, k)                                                    \
225         do {                                                                       \
226                 f_rl(bo, bi, 0, k);                                                    \
227                 f_rl(bo, bi, 1, k);                                                    \
228                 f_rl(bo, bi, 2, k);                                                    \
229                 f_rl(bo, bi, 3, k);                                                    \
230         } while (0)
231
232 void rijndael_encrypt(rijndaelCtx *ctx, const uint32_t *in_blk,
233                                           uint32_t *out_blk)
234 {
235         uint32_t k_len = ctx->k_len;
236         uint32_t *e_key = ctx->e_key;
237         uint32_t b0[4], b1[4], *kp;
238
239         b0[0] = io_swap(in_blk[0]) ^ e_key[0];
240         b0[1] = io_swap(in_blk[1]) ^ e_key[1];
241         b0[2] = io_swap(in_blk[2]) ^ e_key[2];
242         b0[3] = io_swap(in_blk[3]) ^ e_key[3];
243
244         kp = e_key + 4;
245
246         if (k_len > 6) {
247                 f_nround(b1, b0, kp);
248                 f_nround(b0, b1, kp);
249         }
250
251         if (k_len > 4) {
252                 f_nround(b1, b0, kp);
253                 f_nround(b0, b1, kp);
254         }
255
256         f_nround(b1, b0, kp);
257         f_nround(b0, b1, kp);
258         f_nround(b1, b0, kp);
259         f_nround(b0, b1, kp);
260         f_nround(b1, b0, kp);
261         f_nround(b0, b1, kp);
262         f_nround(b1, b0, kp);
263         f_nround(b0, b1, kp);
264         f_nround(b1, b0, kp);
265         f_lround(b0, b1, kp);
266
267         out_blk[0] = io_swap(b0[0]);
268         out_blk[1] = io_swap(b0[1]);
269         out_blk[2] = io_swap(b0[2]);
270         out_blk[3] = io_swap(b0[3]);
271 }
272
273 /* decrypt a block of text      */
274
275 #define i_nround(bo, bi, k)                                                    \
276         do {                                                                       \
277                 i_rn(bo, bi, 0, k);                                                    \
278                 i_rn(bo, bi, 1, k);                                                    \
279                 i_rn(bo, bi, 2, k);                                                    \
280                 i_rn(bo, bi, 3, k);                                                    \
281                 k -= 4;                                                                \
282         } while (0)
283
284 #define i_lround(bo, bi, k)                                                    \
285         do {                                                                       \
286                 i_rl(bo, bi, 0, k);                                                    \
287                 i_rl(bo, bi, 1, k);                                                    \
288                 i_rl(bo, bi, 2, k);                                                    \
289                 i_rl(bo, bi, 3, k);                                                    \
290         } while (0)
291
292 void rijndael_decrypt(rijndaelCtx *ctx, const uint32_t *in_blk,
293                                           uint32_t *out_blk)
294 {
295         uint32_t b0[4], b1[4], *kp;
296         uint32_t k_len = ctx->k_len;
297         uint32_t *e_key = ctx->e_key;
298         uint32_t *d_key = ctx->d_key;
299
300         b0[0] = io_swap(in_blk[0]) ^ e_key[4 * k_len + 24];
301         b0[1] = io_swap(in_blk[1]) ^ e_key[4 * k_len + 25];
302         b0[2] = io_swap(in_blk[2]) ^ e_key[4 * k_len + 26];
303         b0[3] = io_swap(in_blk[3]) ^ e_key[4 * k_len + 27];
304
305         kp = d_key + 4 * (k_len + 5);
306
307         if (k_len > 6) {
308                 i_nround(b1, b0, kp);
309                 i_nround(b0, b1, kp);
310         }
311
312         if (k_len > 4) {
313                 i_nround(b1, b0, kp);
314                 i_nround(b0, b1, kp);
315         }
316
317         i_nround(b1, b0, kp);
318         i_nround(b0, b1, kp);
319         i_nround(b1, b0, kp);
320         i_nround(b0, b1, kp);
321         i_nround(b1, b0, kp);
322         i_nround(b0, b1, kp);
323         i_nround(b1, b0, kp);
324         i_nround(b0, b1, kp);
325         i_nround(b1, b0, kp);
326         i_lround(b0, b1, kp);
327
328         out_blk[0] = io_swap(b0[0]);
329         out_blk[1] = io_swap(b0[1]);
330         out_blk[2] = io_swap(b0[2]);
331         out_blk[3] = io_swap(b0[3]);
332 }
333
334 /*
335  * conventional interface
336  *
337  * ATM it hopes all data is 4-byte aligned - which
338  * should be true for PX.  -marko
339  */
340
341 void aes_set_key(rijndaelCtx *ctx, const uint8_t *key, unsigned keybits,
342                                  int enc)
343 {
344         uint32_t *k;
345
346         k = (uint32_t *)key;
347         rijndael_set_key(ctx, k, keybits, enc);
348 }
349
350 void aes_ecb_encrypt(rijndaelCtx *ctx, uint8_t *data, unsigned len)
351 {
352         unsigned bs = 16;
353         uint32_t *d;
354
355         while (len >= bs) {
356                 d = (uint32_t *)data;
357                 rijndael_encrypt(ctx, d, d);
358
359                 len -= bs;
360                 data += bs;
361         }
362 }
363
364 void aes_ecb_decrypt(rijndaelCtx *ctx, uint8_t *data, unsigned len)
365 {
366         unsigned bs = 16;
367         uint32_t *d;
368
369         while (len >= bs) {
370                 d = (uint32_t *)data;
371                 rijndael_decrypt(ctx, d, d);
372
373                 len -= bs;
374                 data += bs;
375         }
376 }
377
378 void aes_cbc_encrypt(rijndaelCtx *ctx, uint8_t *iva, uint8_t *data,
379                                          unsigned len)
380 {
381         uint32_t *iv = (uint32_t *)iva;
382         uint32_t *d = (uint32_t *)data;
383         unsigned bs = 16;
384
385         while (len >= bs) {
386                 d[0] ^= iv[0];
387                 d[1] ^= iv[1];
388                 d[2] ^= iv[2];
389                 d[3] ^= iv[3];
390
391                 rijndael_encrypt(ctx, d, d);
392
393                 iv = d;
394                 d += bs / 4;
395                 len -= bs;
396         }
397 }
398
399 void aes_cbc_decrypt(rijndaelCtx *ctx, uint8_t *iva, uint8_t *data,
400                                          unsigned len)
401 {
402         uint32_t *d = (uint32_t *)data;
403         unsigned bs = 16;
404         uint32_t buf[4], iv[4];
405
406         memcpy(iv, iva, bs);
407         while (len >= bs) {
408                 buf[0] = d[0];
409                 buf[1] = d[1];
410                 buf[2] = d[2];
411                 buf[3] = d[3];
412
413                 rijndael_decrypt(ctx, buf, d);
414
415                 d[0] ^= iv[0];
416                 d[1] ^= iv[1];
417                 d[2] ^= iv[2];
418                 d[3] ^= iv[3];
419
420                 iv[0] = buf[0];
421                 iv[1] = buf[1];
422                 iv[2] = buf[2];
423                 iv[3] = buf[3];
424                 d += 4;
425                 len -= bs;
426         }
427 }