progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / symm / rijndael256.c
1 /* -*-c-*-
2 *
3 * The Rijndael block cipher, 256-bit version
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <assert.h>
31 #include <stdio.h>
32
33 #include <mLib/bits.h>
34
35 #if defined(TEST_RIG) && defined(ENABLE_ASM_DEBUG)
36 # include "regdump.h"
37 # define BLKC_TESTHOOK do { regdump_init(); } while (0)
38 #endif
39
40 #include "blkc.h"
41 #include "gcipher.h"
42 #include "rijndael.h"
43 #include "rijndael256.h"
44 #include "rijndael-base.h"
45
46 /*----- Main code ---------------------------------------------------------*/
47
48 /* --- @rijndael256_init@ --- *
49 *
50 * Arguments: @rijndael_ctx *k@ = pointer to context to initialize
51 * @const void *buf@ = pointer to buffer of key material
52 * @size_t sz@ = size of the key material
53 *
54 * Returns: ---
55 *
56 * Use: Initializes a Rijndael context with a particular key. This
57 * implementation of Rijndael doesn't impose any particular
58 * limits on the key size except that it must be multiple of 4
59 * bytes long. 256 bits seems sensible, though.
60 */
61
62 void rijndael256_init(rijndael_ctx *k, const void *buf, size_t sz)
63 {
64 rijndael_setup(k, RIJNDAEL256_BLKSZ / 4, buf, sz);
65 }
66
67 /* --- @rijndael256_eblk@, @rijndael256_dblk@ --- *
68 *
69 * Arguments: @const rijndael_ctx *k@ = pointer to Rijndael context
70 * @const uint32 s[4]@ = pointer to source block
71 * @uint32 d[4]@ = pointer to destination block
72 *
73 * Returns: ---
74 *
75 * Use: Low-level block encryption and decryption.
76 */
77
78 #define DO(what, t, \
79 aa, bb, cc, dd, ee, ff, gg, hh, \
80 a, b, c, d, e, f, g, h, w) do { \
81 aa = what(t, a, b, d, e) ^ *w++; \
82 bb = what(t, b, c, e, f) ^ *w++; \
83 cc = what(t, c, d, f, g) ^ *w++; \
84 dd = what(t, d, e, g, h) ^ *w++; \
85 ee = what(t, e, f, h, a) ^ *w++; \
86 ff = what(t, f, g, a, b) ^ *w++; \
87 gg = what(t, g, h, b, c) ^ *w++; \
88 hh = what(t, h, a, c, d) ^ *w++; \
89 } while (0)
90
91 #define UNDO(what, t, \
92 aa, bb, cc, dd, ee, ff, gg, hh, \
93 a, b, c, d, e, f, g, h, w) do { \
94 aa = what(t, a, h, f, e) ^ *w++; \
95 bb = what(t, b, a, g, f) ^ *w++; \
96 cc = what(t, c, b, h, g) ^ *w++; \
97 dd = what(t, d, c, a, h) ^ *w++; \
98 ee = what(t, e, d, b, a) ^ *w++; \
99 ff = what(t, f, e, c, b) ^ *w++; \
100 gg = what(t, g, f, d, c) ^ *w++; \
101 hh = what(t, h, g, e, d) ^ *w++; \
102 } while (0)
103
104 void rijndael256_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
105 {
106 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
107 uint32 e = s[4], f = s[5], g = s[6], h = s[7];
108 uint32 aa, bb, cc, dd, ee, ff, gg, hh;
109 const uint32 *w = k->w;
110
111 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
112 e ^= *w++; f ^= *w++; g ^= *w++; h ^= *w++;
113
114 DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
115 DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
116 DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
117 DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
118 DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
119 DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
120 DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
121 DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
122 DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
123 DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
124 DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
125 DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
126 DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
127 DO(SUB, S, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
128
129 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
130 dst[4] = e; dst[5] = f; dst[6] = g; dst[7] = h;
131 }
132
133 void rijndael256_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
134 {
135 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
136 uint32 e = s[4], f = s[5], g = s[6], h = s[7];
137 uint32 aa, bb, cc, dd, ee, ff, gg, hh;
138 const uint32 *w = k->wi;
139
140 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
141 e ^= *w++; f ^= *w++; g ^= *w++; h ^= *w++;
142
143 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
144 UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
145 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
146 UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
147 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
148 UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
149 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
150 UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
151 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
152 UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
153 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
154 UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
155 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
156 UNDO(SUB, SI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
157
158 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
159 dst[4] = e; dst[5] = f; dst[6] = g; dst[7] = h;
160 }
161
162 BLKC_TEST(RIJNDAEL256, rijndael256)
163
164 /*----- That's all, folks -------------------------------------------------*/