Merge branch '2.5.x'
[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
37#include "blkc.h"
226639f3 38#include "dispatch.h"
3a65506d 39#include "gcipher.h"
40#include "rijndael.h"
7cee294a 41#include "rijndael-base.h"
3a65506d 42
43/*----- Main code ---------------------------------------------------------*/
44
3a65506d 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
59void rijndael_init(rijndael_ctx *k, const void *buf, size_t sz)
60{
7cee294a 61 rijndael_setup(k, RIJNDAEL_BLKSZ / 4, buf, sz);
3a65506d 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
8a1aa284
MW
75CPU_DISPATCH(EMPTY, EMPTY, void, rijndael_eblk,
76 (const rijndael_ctx *k, const uint32 s[4], uint32 d[4]),
226639f3
MW
77 (k, s, d), pick_eblk, simple_eblk)
78
8a1aa284
MW
79CPU_DISPATCH(EMPTY, EMPTY, void, rijndael_dblk,
80 (const rijndael_ctx *k, const uint32 s[4], uint32 d[4]),
226639f3
MW
81 (k, s, d), pick_dblk, simple_dblk)
82
0f23f75f
MW
83#if CPUFAM_X86 || CPUFAM_AMD64
84extern rijndael_eblk__functype rijndael_eblk_x86ish_aesni;
85extern rijndael_dblk__functype rijndael_dblk_x86ish_aesni;
b9b279b4
MW
86extern rijndael_eblk__functype rijndael_eblk_x86ish_aesni_avx;
87extern rijndael_dblk__functype rijndael_dblk_x86ish_aesni_avx;
226639f3 88#endif
26e182fc
MW
89#if CPUFAM_ARMEL && HAVE_AS_ARMV8_CRYPTO
90extern rijndael_eblk__functype rijndael_eblk_arm_crypto;
91extern rijndael_dblk__functype rijndael_dblk_arm_crypto;
92#endif
e492db88
MW
93#if CPUFAM_ARM64
94extern rijndael_eblk__functype rijndael_eblk_arm64_crypto;
95extern rijndael_dblk__functype rijndael_dblk_arm64_crypto;
96#endif
226639f3
MW
97
98static rijndael_eblk__functype *pick_eblk(void)
99{
0f23f75f 100#if CPUFAM_X86 || CPUFAM_AMD64
b9b279b4
MW
101 DISPATCH_PICK_COND(rijndael_eblk, rijndael_eblk_x86ish_aesni_avx,
102 cpu_feature_p(CPUFEAT_X86_AVX) &&
103 cpu_feature_p(CPUFEAT_X86_AESNI));
0f23f75f 104 DISPATCH_PICK_COND(rijndael_eblk, rijndael_eblk_x86ish_aesni,
fac645f7 105 cpu_feature_p(CPUFEAT_X86_AESNI));
226639f3 106#endif
26e182fc
MW
107#if CPUFAM_ARMEL && HAVE_AS_ARMV8_CRYPTO
108 DISPATCH_PICK_COND(rijndael_eblk, rijndael_eblk_arm_crypto,
109 cpu_feature_p(CPUFEAT_ARM_AES));
110#endif
e492db88
MW
111#if CPUFAM_ARM64
112 DISPATCH_PICK_COND(rijndael_eblk, rijndael_eblk_arm64_crypto,
113 cpu_feature_p(CPUFEAT_ARM_AES));
114#endif
fac645f7 115 DISPATCH_PICK_FALLBACK(rijndael_eblk, simple_eblk);
226639f3
MW
116}
117
118static rijndael_dblk__functype *pick_dblk(void)
119{
0f23f75f 120#if CPUFAM_X86 || CPUFAM_AMD64
b9b279b4
MW
121 DISPATCH_PICK_COND(rijndael_dblk, rijndael_dblk_x86ish_aesni_avx,
122 cpu_feature_p(CPUFEAT_X86_AVX) &&
123 cpu_feature_p(CPUFEAT_X86_AESNI));
0f23f75f 124 DISPATCH_PICK_COND(rijndael_dblk, rijndael_dblk_x86ish_aesni,
fac645f7 125 cpu_feature_p(CPUFEAT_X86_AESNI));
226639f3 126#endif
26e182fc
MW
127#if CPUFAM_ARMEL && HAVE_AS_ARMV8_CRYPTO
128 DISPATCH_PICK_COND(rijndael_dblk, rijndael_dblk_arm_crypto,
129 cpu_feature_p(CPUFEAT_ARM_AES));
130#endif
e492db88
MW
131#if CPUFAM_ARM64
132 DISPATCH_PICK_COND(rijndael_dblk, rijndael_dblk_arm64_crypto,
133 cpu_feature_p(CPUFEAT_ARM_AES));
134#endif
fac645f7 135 DISPATCH_PICK_FALLBACK(rijndael_dblk, simple_dblk);
226639f3
MW
136}
137
bb3d2477 138#define DO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
139 aa = what(t, a, b, c, d) ^ *w++; \
140 bb = what(t, b, c, d, a) ^ *w++; \
141 cc = what(t, c, d, a, b) ^ *w++; \
142 dd = what(t, d, a, b, c) ^ *w++; \
3a65506d 143} while (0)
144
bb3d2477 145#define UNDO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
146 aa = what(t, a, d, c, b) ^ *w++; \
147 bb = what(t, b, a, d, c) ^ *w++; \
148 cc = what(t, c, b, a, d) ^ *w++; \
149 dd = what(t, d, c, b, a) ^ *w++; \
3a65506d 150} while (0)
151
226639f3 152static void simple_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
3a65506d 153{
154 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
155 uint32 aa, bb, cc, dd;
78ec50fa 156 const uint32 *w = k->w;
3a65506d 157
158 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
bb3d2477 159 aa = a; bb = b; cc = c; dd = d;
3a65506d 160
161 switch (k->nr) {
162 case 14:
bb3d2477 163 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
164 case 13:
165 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
3a65506d 166 case 12:
bb3d2477 167 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
168 case 11:
169 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
3a65506d 170 case 10:
171 default:
bb3d2477 172 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
173 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
174 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
175 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
176 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
177 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
178 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
179 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
180 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
3a65506d 181 }
bb3d2477 182 DO(SUB, S, a, b, c, d, aa, bb, cc, dd, w);
3a65506d 183
184 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
185}
186
226639f3 187static void simple_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
3a65506d 188{
189 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
190 uint32 aa, bb, cc, dd;
78ec50fa 191 const uint32 *w = k->wi;
3a65506d 192
193 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
bb3d2477 194 aa = a; bb = b; cc = c; dd = d;
3a65506d 195
196 switch (k->nr) {
197 case 14:
bb3d2477 198 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
199 case 13:
200 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
3a65506d 201 case 12:
bb3d2477 202 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
203 case 11:
204 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
3a65506d 205 case 10:
206 default:
bb3d2477 207 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
208 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
209 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
210 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
211 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
212 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
213 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
214 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
215 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
3a65506d 216 }
bb3d2477 217 UNDO(SUB, SI, a, b, c, d, aa, bb, cc, dd, w);
3a65506d 218
219 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
220}
221
222BLKC_TEST(RIJNDAEL, rijndael)
223
224/*----- That's all, folks -------------------------------------------------*/