progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / symm / rijndael.c
1 /* -*-c-*-
2 *
3 * The Rijndael block cipher
4 *
5 * (c) 2000 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 "config.h"
31
32 #include <assert.h>
33 #include <stdio.h>
34
35 #include <mLib/bits.h>
36
37 #if defined(TEST_RIG) && defined(ENABLE_ASM_DEBUG)
38 # include "regdump.h"
39 # define BLKC_TESTHOOK do { regdump_init(); } while (0)
40 #endif
41
42 #include "blkc.h"
43 #include "dispatch.h"
44 #include "gcipher.h"
45 #include "rijndael.h"
46 #include "rijndael-base.h"
47
48 /*----- Main code ---------------------------------------------------------*/
49
50 /* --- @rijndael_init@ --- *
51 *
52 * Arguments: @rijndael_ctx *k@ = pointer to context to initialize
53 * @const void *buf@ = pointer to buffer of key material
54 * @size_t sz@ = size of the key material
55 *
56 * Returns: ---
57 *
58 * Use: Initializes a Rijndael context with a particular key. This
59 * implementation of Rijndael doesn't impose any particular
60 * limits on the key size except that it must be multiple of 4
61 * bytes long. 256 bits seems sensible, though.
62 */
63
64 void rijndael_init(rijndael_ctx *k, const void *buf, size_t sz)
65 {
66 rijndael_setup(k, RIJNDAEL_BLKSZ / 4, buf, sz);
67 }
68
69 /* --- @rijndael_eblk@, @rijndael_dblk@ --- *
70 *
71 * Arguments: @const rijndael_ctx *k@ = pointer to Rijndael context
72 * @const uint32 s[4]@ = pointer to source block
73 * @uint32 d[4]@ = pointer to destination block
74 *
75 * Returns: ---
76 *
77 * Use: Low-level block encryption and decryption.
78 */
79
80 CPU_DISPATCH(EMPTY, EMPTY, void, rijndael_eblk,
81 (const rijndael_ctx *k, const uint32 s[4], uint32 d[4]),
82 (k, s, d), pick_eblk, simple_eblk)
83
84 CPU_DISPATCH(EMPTY, EMPTY, void, rijndael_dblk,
85 (const rijndael_ctx *k, const uint32 s[4], uint32 d[4]),
86 (k, s, d), pick_dblk, simple_dblk)
87
88 #if CPUFAM_X86 || CPUFAM_AMD64
89 extern rijndael_eblk__functype rijndael_eblk_x86ish_aesni;
90 extern rijndael_dblk__functype rijndael_dblk_x86ish_aesni;
91 extern rijndael_eblk__functype rijndael_eblk_x86ish_aesni_avx;
92 extern rijndael_dblk__functype rijndael_dblk_x86ish_aesni_avx;
93 #endif
94 #if CPUFAM_ARMEL && HAVE_AS_ARMV8_CRYPTO
95 extern rijndael_eblk__functype rijndael_eblk_arm_crypto;
96 extern rijndael_dblk__functype rijndael_dblk_arm_crypto;
97 #endif
98 #if CPUFAM_ARM64
99 extern rijndael_eblk__functype rijndael_eblk_arm64_crypto;
100 extern rijndael_dblk__functype rijndael_dblk_arm64_crypto;
101 #endif
102
103 static rijndael_eblk__functype *pick_eblk(void)
104 {
105 #if CPUFAM_X86 || CPUFAM_AMD64
106 DISPATCH_PICK_COND(rijndael_eblk, rijndael_eblk_x86ish_aesni_avx,
107 cpu_feature_p(CPUFEAT_X86_AVX) &&
108 cpu_feature_p(CPUFEAT_X86_AESNI));
109 DISPATCH_PICK_COND(rijndael_eblk, rijndael_eblk_x86ish_aesni,
110 cpu_feature_p(CPUFEAT_X86_AESNI));
111 #endif
112 #if CPUFAM_ARMEL && HAVE_AS_ARMV8_CRYPTO
113 DISPATCH_PICK_COND(rijndael_eblk, rijndael_eblk_arm_crypto,
114 cpu_feature_p(CPUFEAT_ARM_AES));
115 #endif
116 #if CPUFAM_ARM64
117 DISPATCH_PICK_COND(rijndael_eblk, rijndael_eblk_arm64_crypto,
118 cpu_feature_p(CPUFEAT_ARM_AES));
119 #endif
120 DISPATCH_PICK_FALLBACK(rijndael_eblk, simple_eblk);
121 }
122
123 static rijndael_dblk__functype *pick_dblk(void)
124 {
125 #if CPUFAM_X86 || CPUFAM_AMD64
126 DISPATCH_PICK_COND(rijndael_dblk, rijndael_dblk_x86ish_aesni_avx,
127 cpu_feature_p(CPUFEAT_X86_AVX) &&
128 cpu_feature_p(CPUFEAT_X86_AESNI));
129 DISPATCH_PICK_COND(rijndael_dblk, rijndael_dblk_x86ish_aesni,
130 cpu_feature_p(CPUFEAT_X86_AESNI));
131 #endif
132 #if CPUFAM_ARMEL && HAVE_AS_ARMV8_CRYPTO
133 DISPATCH_PICK_COND(rijndael_dblk, rijndael_dblk_arm_crypto,
134 cpu_feature_p(CPUFEAT_ARM_AES));
135 #endif
136 #if CPUFAM_ARM64
137 DISPATCH_PICK_COND(rijndael_dblk, rijndael_dblk_arm64_crypto,
138 cpu_feature_p(CPUFEAT_ARM_AES));
139 #endif
140 DISPATCH_PICK_FALLBACK(rijndael_dblk, simple_dblk);
141 }
142
143 #define DO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
144 aa = what(t, a, b, c, d) ^ *w++; \
145 bb = what(t, b, c, d, a) ^ *w++; \
146 cc = what(t, c, d, a, b) ^ *w++; \
147 dd = what(t, d, a, b, c) ^ *w++; \
148 } while (0)
149
150 #define UNDO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
151 aa = what(t, a, d, c, b) ^ *w++; \
152 bb = what(t, b, a, d, c) ^ *w++; \
153 cc = what(t, c, b, a, d) ^ *w++; \
154 dd = what(t, d, c, b, a) ^ *w++; \
155 } while (0)
156
157 static void simple_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
158 {
159 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
160 uint32 aa, bb, cc, dd;
161 const uint32 *w = k->w;
162
163 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
164 aa = a; bb = b; cc = c; dd = d;
165
166 switch (k->nr) {
167 case 14:
168 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
169 case 13:
170 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
171 case 12:
172 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
173 case 11:
174 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
175 case 10:
176 default:
177 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
178 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
179 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
180 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
181 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
182 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
183 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
184 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
185 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
186 }
187 DO(SUB, S, a, b, c, d, aa, bb, cc, dd, w);
188
189 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
190 }
191
192 static void simple_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
193 {
194 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
195 uint32 aa, bb, cc, dd;
196 const uint32 *w = k->wi;
197
198 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
199 aa = a; bb = b; cc = c; dd = d;
200
201 switch (k->nr) {
202 case 14:
203 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
204 case 13:
205 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
206 case 12:
207 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
208 case 11:
209 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
210 case 10:
211 default:
212 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
213 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
214 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
215 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
216 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
217 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
218 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
219 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
220 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
221 }
222 UNDO(SUB, SI, a, b, c, d, aa, bb, cc, dd, w);
223
224 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
225 }
226
227 BLKC_TEST(RIJNDAEL, rijndael)
228
229 /*----- That's all, folks -------------------------------------------------*/