Format with .clang-format, included herein for reference
[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 #include <u.h>
43 #include <libc.h>
44
45 #include "rijndael.h"
46
47 #include "rijndael.tbl"
48
49 /* 3. Basic macros for speeding up generic operations                           */
50
51 /* Circular rotate of 32 bit values                                                                     */
52
53 #define rotr(x, n) (((x) >> ((int)(n))) | ((x) << (32 - (int)(n))))
54 #define rotl(x, n) (((x) << ((int)(n))) | ((x) >> (32 - (int)(n))))
55
56 /* Invert byte order in a 32 bit variable                                                       */
57
58 #define bswap(x) ((rotl((x), 8) & 0x00ff00ff) | (rotr((x), 8) & 0xff00ff00))
59
60 /* Extract byte from a 32 bit quantity (little endian notation)         */
61
62 #define byte(x, n) ((uint8_t)((x) >> (8 * (n))))
63
64 #define io_swap(x) (x)
65
66 #define ff_mult(a, b)                                                          \
67         ((a) && (b) ? pow_tab[(log_tab[a] + log_tab[b]) % 255] : 0)
68
69 #define f_rn(bo, bi, n, k)                                                     \
70         (bo)[n] = ft_tab[0][byte((bi)[n], 0)] ^                                    \
71                           ft_tab[1][byte((bi)[((n) + 1) & 3], 1)] ^                        \
72                           ft_tab[2][byte((bi)[((n) + 2) & 3], 2)] ^                        \
73                           ft_tab[3][byte((bi)[((n) + 3) & 3], 3)] ^ *((k) + (n))
74
75 #define i_rn(bo, bi, n, k)                                                     \
76         (bo)[n] = it_tab[0][byte((bi)[n], 0)] ^                                    \
77                           it_tab[1][byte((bi)[((n) + 3) & 3], 1)] ^                        \
78                           it_tab[2][byte((bi)[((n) + 2) & 3], 2)] ^                        \
79                           it_tab[3][byte((bi)[((n) + 1) & 3], 3)] ^ *((k) + (n))
80
81 #define ls_box(x)                                                              \
82         (fl_tab[0][byte(x, 0)] ^ fl_tab[1][byte(x, 1)] ^ fl_tab[2][byte(x, 2)] ^   \
83          fl_tab[3][byte(x, 3)])
84
85 #define f_rl(bo, bi, n, k)                                                     \
86         (bo)[n] = fl_tab[0][byte((bi)[n], 0)] ^                                    \
87                           fl_tab[1][byte((bi)[((n) + 1) & 3], 1)] ^                        \
88                           fl_tab[2][byte((bi)[((n) + 2) & 3], 2)] ^                        \
89                           fl_tab[3][byte((bi)[((n) + 3) & 3], 3)] ^ *((k) + (n))
90
91 #define i_rl(bo, bi, n, k)                                                     \
92         (bo)[n] = il_tab[0][byte((bi)[n], 0)] ^                                    \
93                           il_tab[1][byte((bi)[((n) + 3) & 3], 1)] ^                        \
94                           il_tab[2][byte((bi)[((n) + 2) & 3], 2)] ^                        \
95                           il_tab[3][byte((bi)[((n) + 1) & 3], 3)] ^ *((k) + (n))
96
97 #define star_x(x) (((x)&0x7f7f7f7f) << 1) ^ ((((x)&0x80808080) >> 7) * 0x1b)
98
99 #define imix_col(y, x)                                                         \
100         do {                                                                       \
101                 u = star_x(x);                                                         \
102                 v = star_x(u);                                                         \
103                 w = star_x(v);                                                         \
104                 t = w ^ (x);                                                           \
105                 (y) = u ^ v ^ w;                                                       \
106                 (y) ^= rotr(u ^ t, 8) ^ rotr(v ^ t, 16) ^ rotr(t, 24);                 \
107         } while (0)
108
109 /* initialise the key schedule from the user supplied key       */
110
111 #define loop4(i)                                                               \
112         do {                                                                       \
113                 t = ls_box(rotr(t, 8)) ^ rco_tab[i];                                   \
114                 t ^= e_key[4 * i];                                                     \
115                 e_key[4 * i + 4] = t;                                                  \
116                 t ^= e_key[4 * i + 1];                                                 \
117                 e_key[4 * i + 5] = t;                                                  \
118                 t ^= e_key[4 * i + 2];                                                 \
119                 e_key[4 * i + 6] = t;                                                  \
120                 t ^= e_key[4 * i + 3];                                                 \
121                 e_key[4 * i + 7] = t;                                                  \
122         } while (0)
123
124 #define loop6(i)                                                               \
125         do {                                                                       \
126                 t = ls_box(rotr(t, 8)) ^ rco_tab[i];                                   \
127                 t ^= e_key[6 * (i)];                                                   \
128                 e_key[6 * (i) + 6] = t;                                                \
129                 t ^= e_key[6 * (i) + 1];                                               \
130                 e_key[6 * (i) + 7] = t;                                                \
131                 t ^= e_key[6 * (i) + 2];                                               \
132                 e_key[6 * (i) + 8] = t;                                                \
133                 t ^= e_key[6 * (i) + 3];                                               \
134                 e_key[6 * (i) + 9] = t;                                                \
135                 t ^= e_key[6 * (i) + 4];                                               \
136                 e_key[6 * (i) + 10] = t;                                               \
137                 t ^= e_key[6 * (i) + 5];                                               \
138                 e_key[6 * (i) + 11] = t;                                               \
139         } while (0)
140
141 #define loop8(i)                                                               \
142         do {                                                                       \
143                 t = ls_box(rotr(t, 8)) ^ rco_tab[i];                                   \
144                 t ^= e_key[8 * (i)];                                                   \
145                 e_key[8 * (i) + 8] = t;                                                \
146                 t ^= e_key[8 * (i) + 1];                                               \
147                 e_key[8 * (i) + 9] = t;                                                \
148                 t ^= e_key[8 * (i) + 2];                                               \
149                 e_key[8 * (i) + 10] = t;                                               \
150                 t ^= e_key[8 * (i) + 3];                                               \
151                 e_key[8 * (i) + 11] = t;                                               \
152                 t = e_key[8 * (i) + 4] ^ ls_box(t);                                    \
153                 e_key[8 * (i) + 12] = t;                                               \
154                 t ^= e_key[8 * (i) + 5];                                               \
155                 e_key[8 * (i) + 13] = t;                                               \
156                 t ^= e_key[8 * (i) + 6];                                               \
157                 e_key[8 * (i) + 14] = t;                                               \
158                 t ^= e_key[8 * (i) + 7];                                               \
159                 e_key[8 * (i) + 15] = t;                                               \
160         } while (0)
161
162 rijndaelCtx *rijndael_set_key(rijndaelCtx *ctx, const uint32_t *in_key,
163                                                           const uint32_t key_len, int encrypt)
164 {
165         uint32_t i, t, u, v, w;
166         uint32_t *e_key = ctx->e_key;
167         uint32_t *d_key = ctx->d_key;
168
169         ctx->decrypt = !encrypt;
170
171         ctx->k_len = (key_len + 31) / 32;
172
173         e_key[0] = io_swap(in_key[0]);
174         e_key[1] = io_swap(in_key[1]);
175         e_key[2] = io_swap(in_key[2]);
176         e_key[3] = io_swap(in_key[3]);
177
178         switch (ctx->k_len) {
179         case 4:
180                 t = e_key[3];
181                 for (i = 0; i < 10; ++i)
182                         loop4(i);
183                 break;
184
185         case 6:
186                 e_key[4] = io_swap(in_key[4]);
187                 t = e_key[5] = io_swap(in_key[5]);
188                 for (i = 0; i < 8; ++i)
189                         loop6(i);
190                 break;
191
192         case 8:
193                 e_key[4] = io_swap(in_key[4]);
194                 e_key[5] = io_swap(in_key[5]);
195                 e_key[6] = io_swap(in_key[6]);
196                 t = e_key[7] = io_swap(in_key[7]);
197                 for (i = 0; i < 7; ++i)
198                         loop8(i);
199                 break;
200         }
201
202         if (!encrypt) {
203                 d_key[0] = e_key[0];
204                 d_key[1] = e_key[1];
205                 d_key[2] = e_key[2];
206                 d_key[3] = e_key[3];
207
208                 for (i = 4; i < 4 * ctx->k_len + 24; ++i)
209                         imix_col(d_key[i], e_key[i]);
210         }
211
212         return ctx;
213 }
214
215 /* encrypt a block of text      */
216
217 #define f_nround(bo, bi, k)                                                    \
218         do {                                                                       \
219                 f_rn(bo, bi, 0, k);                                                    \
220                 f_rn(bo, bi, 1, k);                                                    \
221                 f_rn(bo, bi, 2, k);                                                    \
222                 f_rn(bo, bi, 3, k);                                                    \
223                 k += 4;                                                                \
224         } while (0)
225
226 #define f_lround(bo, bi, k)                                                    \
227         do {                                                                       \
228                 f_rl(bo, bi, 0, k);                                                    \
229                 f_rl(bo, bi, 1, k);                                                    \
230                 f_rl(bo, bi, 2, k);                                                    \
231                 f_rl(bo, bi, 3, k);                                                    \
232         } while (0)
233
234 void rijndael_encrypt(rijndaelCtx *ctx, const uint32_t *in_blk,
235                                           uint32_t *out_blk)
236 {
237         uint32_t k_len = ctx->k_len;
238         uint32_t *e_key = ctx->e_key;
239         uint32_t b0[4], b1[4], *kp;
240
241         b0[0] = io_swap(in_blk[0]) ^ e_key[0];
242         b0[1] = io_swap(in_blk[1]) ^ e_key[1];
243         b0[2] = io_swap(in_blk[2]) ^ e_key[2];
244         b0[3] = io_swap(in_blk[3]) ^ e_key[3];
245
246         kp = e_key + 4;
247
248         if (k_len > 6) {
249                 f_nround(b1, b0, kp);
250                 f_nround(b0, b1, kp);
251         }
252
253         if (k_len > 4) {
254                 f_nround(b1, b0, kp);
255                 f_nround(b0, b1, kp);
256         }
257
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_nround(b0, b1, kp);
266         f_nround(b1, b0, kp);
267         f_lround(b0, b1, kp);
268
269         out_blk[0] = io_swap(b0[0]);
270         out_blk[1] = io_swap(b0[1]);
271         out_blk[2] = io_swap(b0[2]);
272         out_blk[3] = io_swap(b0[3]);
273 }
274
275 /* decrypt a block of text      */
276
277 #define i_nround(bo, bi, k)                                                    \
278         do {                                                                       \
279                 i_rn(bo, bi, 0, k);                                                    \
280                 i_rn(bo, bi, 1, k);                                                    \
281                 i_rn(bo, bi, 2, k);                                                    \
282                 i_rn(bo, bi, 3, k);                                                    \
283                 k -= 4;                                                                \
284         } while (0)
285
286 #define i_lround(bo, bi, k)                                                    \
287         do {                                                                       \
288                 i_rl(bo, bi, 0, k);                                                    \
289                 i_rl(bo, bi, 1, k);                                                    \
290                 i_rl(bo, bi, 2, k);                                                    \
291                 i_rl(bo, bi, 3, k);                                                    \
292         } while (0)
293
294 void rijndael_decrypt(rijndaelCtx *ctx, const uint32_t *in_blk,
295                                           uint32_t *out_blk)
296 {
297         uint32_t b0[4], b1[4], *kp;
298         uint32_t k_len = ctx->k_len;
299         uint32_t *e_key = ctx->e_key;
300         uint32_t *d_key = ctx->d_key;
301
302         b0[0] = io_swap(in_blk[0]) ^ e_key[4 * k_len + 24];
303         b0[1] = io_swap(in_blk[1]) ^ e_key[4 * k_len + 25];
304         b0[2] = io_swap(in_blk[2]) ^ e_key[4 * k_len + 26];
305         b0[3] = io_swap(in_blk[3]) ^ e_key[4 * k_len + 27];
306
307         kp = d_key + 4 * (k_len + 5);
308
309         if (k_len > 6) {
310                 i_nround(b1, b0, kp);
311                 i_nround(b0, b1, kp);
312         }
313
314         if (k_len > 4) {
315                 i_nround(b1, b0, kp);
316                 i_nround(b0, b1, kp);
317         }
318
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_nround(b0, b1, kp);
327         i_nround(b1, b0, kp);
328         i_lround(b0, b1, kp);
329
330         out_blk[0] = io_swap(b0[0]);
331         out_blk[1] = io_swap(b0[1]);
332         out_blk[2] = io_swap(b0[2]);
333         out_blk[3] = io_swap(b0[3]);
334 }
335
336 /*
337  * conventional interface
338  *
339  * ATM it hopes all data is 4-byte aligned - which
340  * should be true for PX.  -marko
341  */
342
343 void aes_set_key(rijndaelCtx *ctx, const uint8_t *key, unsigned keybits,
344                                  int enc)
345 {
346         uint32_t *k;
347
348         k = (uint32_t *)key;
349         rijndael_set_key(ctx, k, keybits, enc);
350 }
351
352 void aes_ecb_encrypt(rijndaelCtx *ctx, uint8_t *data, unsigned len)
353 {
354         unsigned bs = 16;
355         uint32_t *d;
356
357         while (len >= bs) {
358                 d = (uint32_t *)data;
359                 rijndael_encrypt(ctx, d, d);
360
361                 len -= bs;
362                 data += bs;
363         }
364 }
365
366 void aes_ecb_decrypt(rijndaelCtx *ctx, uint8_t *data, unsigned len)
367 {
368         unsigned bs = 16;
369         uint32_t *d;
370
371         while (len >= bs) {
372                 d = (uint32_t *)data;
373                 rijndael_decrypt(ctx, d, d);
374
375                 len -= bs;
376                 data += bs;
377         }
378 }
379
380 void aes_cbc_encrypt(rijndaelCtx *ctx, uint8_t *iva, uint8_t *data,
381                                          unsigned len)
382 {
383         uint32_t *iv = (uint32_t *)iva;
384         uint32_t *d = (uint32_t *)data;
385         unsigned bs = 16;
386
387         while (len >= bs) {
388                 d[0] ^= iv[0];
389                 d[1] ^= iv[1];
390                 d[2] ^= iv[2];
391                 d[3] ^= iv[3];
392
393                 rijndael_encrypt(ctx, d, d);
394
395                 iv = d;
396                 d += bs / 4;
397                 len -= bs;
398         }
399 }
400
401 void aes_cbc_decrypt(rijndaelCtx *ctx, uint8_t *iva, uint8_t *data,
402                                          unsigned len)
403 {
404         uint32_t *d = (uint32_t *)data;
405         unsigned bs = 16;
406         uint32_t buf[4], iv[4];
407
408         memcpy(iv, iva, bs);
409         while (len >= bs) {
410                 buf[0] = d[0];
411                 buf[1] = d[1];
412                 buf[2] = d[2];
413                 buf[3] = d[3];
414
415                 rijndael_decrypt(ctx, buf, d);
416
417                 d[0] ^= iv[0];
418                 d[1] ^= iv[1];
419                 d[2] ^= iv[2];
420                 d[3] ^= iv[3];
421
422                 iv[0] = buf[0];
423                 iv[1] = buf[1];
424                 iv[2] = buf[2];
425                 iv[3] = buf[3];
426                 d += 4;
427                 len -= bs;
428         }
429 }