Initialize the register dumping machinery while testing assembler code.
[catacomb] / symm / rijndael.c
CommitLineData
3a65506d 1/* -*-c-*-
2 *
3a65506d 3 * The Rijndael block cipher
4 *
5 * (c) 2000 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
3a65506d 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.
45c0fd36 16 *
3a65506d 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.
45c0fd36 21 *
3a65506d 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
3a65506d 28/*----- Header files ------------------------------------------------------*/
29
226639f3
MW
30#include "config.h"
31
3a65506d 32#include <assert.h>
33#include <stdio.h>
34
35#include <mLib/bits.h>
36
416b8869
MW
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
3a65506d 42#include "blkc.h"
226639f3 43#include "dispatch.h"
3a65506d 44#include "gcipher.h"
45#include "rijndael.h"
7cee294a 46#include "rijndael-base.h"
3a65506d 47
48/*----- Main code ---------------------------------------------------------*/
49
3a65506d 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
64void rijndael_init(rijndael_ctx *k, const void *buf, size_t sz)
65{
7cee294a 66 rijndael_setup(k, RIJNDAEL_BLKSZ / 4, buf, sz);
3a65506d 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
8a1aa284
MW
80CPU_DISPATCH(EMPTY, EMPTY, void, rijndael_eblk,
81 (const rijndael_ctx *k, const uint32 s[4], uint32 d[4]),
226639f3
MW
82 (k, s, d), pick_eblk, simple_eblk)
83
8a1aa284
MW
84CPU_DISPATCH(EMPTY, EMPTY, void, rijndael_dblk,
85 (const rijndael_ctx *k, const uint32 s[4], uint32 d[4]),
226639f3
MW
86 (k, s, d), pick_dblk, simple_dblk)
87
0f23f75f
MW
88#if CPUFAM_X86 || CPUFAM_AMD64
89extern rijndael_eblk__functype rijndael_eblk_x86ish_aesni;
90extern rijndael_dblk__functype rijndael_dblk_x86ish_aesni;
b9b279b4
MW
91extern rijndael_eblk__functype rijndael_eblk_x86ish_aesni_avx;
92extern rijndael_dblk__functype rijndael_dblk_x86ish_aesni_avx;
226639f3 93#endif
26e182fc
MW
94#if CPUFAM_ARMEL && HAVE_AS_ARMV8_CRYPTO
95extern rijndael_eblk__functype rijndael_eblk_arm_crypto;
96extern rijndael_dblk__functype rijndael_dblk_arm_crypto;
97#endif
e492db88
MW
98#if CPUFAM_ARM64
99extern rijndael_eblk__functype rijndael_eblk_arm64_crypto;
100extern rijndael_dblk__functype rijndael_dblk_arm64_crypto;
101#endif
226639f3
MW
102
103static rijndael_eblk__functype *pick_eblk(void)
104{
0f23f75f 105#if CPUFAM_X86 || CPUFAM_AMD64
b9b279b4
MW
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));
0f23f75f 109 DISPATCH_PICK_COND(rijndael_eblk, rijndael_eblk_x86ish_aesni,
fac645f7 110 cpu_feature_p(CPUFEAT_X86_AESNI));
226639f3 111#endif
26e182fc
MW
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
e492db88
MW
116#if CPUFAM_ARM64
117 DISPATCH_PICK_COND(rijndael_eblk, rijndael_eblk_arm64_crypto,
118 cpu_feature_p(CPUFEAT_ARM_AES));
119#endif
fac645f7 120 DISPATCH_PICK_FALLBACK(rijndael_eblk, simple_eblk);
226639f3
MW
121}
122
123static rijndael_dblk__functype *pick_dblk(void)
124{
0f23f75f 125#if CPUFAM_X86 || CPUFAM_AMD64
b9b279b4
MW
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));
0f23f75f 129 DISPATCH_PICK_COND(rijndael_dblk, rijndael_dblk_x86ish_aesni,
fac645f7 130 cpu_feature_p(CPUFEAT_X86_AESNI));
226639f3 131#endif
26e182fc
MW
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
e492db88
MW
136#if CPUFAM_ARM64
137 DISPATCH_PICK_COND(rijndael_dblk, rijndael_dblk_arm64_crypto,
138 cpu_feature_p(CPUFEAT_ARM_AES));
139#endif
fac645f7 140 DISPATCH_PICK_FALLBACK(rijndael_dblk, simple_dblk);
226639f3
MW
141}
142
bb3d2477 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++; \
3a65506d 148} while (0)
149
bb3d2477 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++; \
3a65506d 155} while (0)
156
226639f3 157static void simple_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
3a65506d 158{
159 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
160 uint32 aa, bb, cc, dd;
78ec50fa 161 const uint32 *w = k->w;
3a65506d 162
163 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
bb3d2477 164 aa = a; bb = b; cc = c; dd = d;
3a65506d 165
166 switch (k->nr) {
167 case 14:
bb3d2477 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);
3a65506d 171 case 12:
bb3d2477 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);
3a65506d 175 case 10:
176 default:
bb3d2477 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);
3a65506d 186 }
bb3d2477 187 DO(SUB, S, a, b, c, d, aa, bb, cc, dd, w);
3a65506d 188
189 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
190}
191
226639f3 192static void simple_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
3a65506d 193{
194 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
195 uint32 aa, bb, cc, dd;
78ec50fa 196 const uint32 *w = k->wi;
3a65506d 197
198 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
bb3d2477 199 aa = a; bb = b; cc = c; dd = d;
3a65506d 200
201 switch (k->nr) {
202 case 14:
bb3d2477 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);
3a65506d 206 case 12:
bb3d2477 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);
3a65506d 210 case 10:
211 default:
bb3d2477 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);
3a65506d 221 }
bb3d2477 222 UNDO(SUB, SI, a, b, c, d, aa, bb, cc, dd, w);
3a65506d 223
224 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
225}
226
227BLKC_TEST(RIJNDAEL, rijndael)
228
229/*----- That's all, folks -------------------------------------------------*/