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