3 * The Rijndael block cipher
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
35 #include <mLib/bits.h>
41 #include "rijndael-base.h"
43 /*----- Main code ---------------------------------------------------------*/
45 /* --- @rijndael_init@ --- *
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
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.
59 void rijndael_init(rijndael_ctx *k, const void *buf, size_t sz)
61 rijndael_setup(k, RIJNDAEL_BLKSZ / 4, buf, sz);
64 /* --- @rijndael_eblk@, @rijndael_dblk@ --- *
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
72 * Use: Low-level block encryption and decryption.
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)
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)
83 #if CPUFAM_X86 || CPUFAM_AMD64
84 extern rijndael_eblk__functype rijndael_eblk_x86ish_aesni;
85 extern rijndael_dblk__functype rijndael_dblk_x86ish_aesni;
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;
92 static rijndael_eblk__functype *pick_eblk(void)
94 #if CPUFAM_X86 || CPUFAM_AMD64
95 DISPATCH_PICK_COND(rijndael_eblk, rijndael_eblk_x86ish_aesni,
96 cpu_feature_p(CPUFEAT_X86_AESNI));
98 #if CPUFAM_ARMEL && HAVE_AS_ARMV8_CRYPTO
99 DISPATCH_PICK_COND(rijndael_eblk, rijndael_eblk_arm_crypto,
100 cpu_feature_p(CPUFEAT_ARM_AES));
102 DISPATCH_PICK_FALLBACK(rijndael_eblk, simple_eblk);
105 static rijndael_dblk__functype *pick_dblk(void)
107 #if CPUFAM_X86 || CPUFAM_AMD64
108 DISPATCH_PICK_COND(rijndael_dblk, rijndael_dblk_x86ish_aesni,
109 cpu_feature_p(CPUFEAT_X86_AESNI));
111 #if CPUFAM_ARMEL && HAVE_AS_ARMV8_CRYPTO
112 DISPATCH_PICK_COND(rijndael_dblk, rijndael_dblk_arm_crypto,
113 cpu_feature_p(CPUFEAT_ARM_AES));
115 DISPATCH_PICK_FALLBACK(rijndael_dblk, simple_dblk);
118 #define DO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
119 aa = what(t, a, b, c, d) ^ *w++; \
120 bb = what(t, b, c, d, a) ^ *w++; \
121 cc = what(t, c, d, a, b) ^ *w++; \
122 dd = what(t, d, a, b, c) ^ *w++; \
125 #define UNDO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
126 aa = what(t, a, d, c, b) ^ *w++; \
127 bb = what(t, b, a, d, c) ^ *w++; \
128 cc = what(t, c, b, a, d) ^ *w++; \
129 dd = what(t, d, c, b, a) ^ *w++; \
132 static void simple_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
134 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
135 uint32 aa, bb, cc, dd;
136 const uint32 *w = k->w;
138 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
139 aa = a; bb = b; cc = c; dd = d;
143 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
145 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
147 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
149 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
152 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
153 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
154 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
155 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
156 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
157 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
158 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
159 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
160 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
162 DO(SUB, S, a, b, c, d, aa, bb, cc, dd, w);
164 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
167 static void simple_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
169 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
170 uint32 aa, bb, cc, dd;
171 const uint32 *w = k->wi;
173 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
174 aa = a; bb = b; cc = c; dd = d;
178 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
180 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
182 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
184 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
187 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
188 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
189 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
190 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
191 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
192 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
193 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
194 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
195 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
197 UNDO(SUB, SI, a, b, c, d, aa, bb, cc, dd, w);
199 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
202 BLKC_TEST(RIJNDAEL, rijndael)
204 /*----- That's all, folks -------------------------------------------------*/