base/dispatch.c: Add documentation for some internal functions.
[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
226639f3
MW
75CPU_DISPATCH(EMPTY, EMPTY, void, rijndael_eblk, (const rijndael_ctx *k,
76 const uint32 s[4],
77 uint32 d[4]),
78 (k, s, d), pick_eblk, simple_eblk)
79
80CPU_DISPATCH(EMPTY, EMPTY, void, rijndael_dblk, (const rijndael_ctx *k,
81 const uint32 s[4],
82 uint32 d[4]),
83 (k, s, d), pick_dblk, simple_dblk)
84
85#ifdef CPUFAM_X86
86extern rijndael_eblk__functype rijndael_eblk_x86_aesni;
87extern rijndael_dblk__functype rijndael_dblk_x86_aesni;
88#endif
89
90static rijndael_eblk__functype *pick_eblk(void)
91{
92#ifdef CPUFAM_X86
93 if (cpu_feature_p(CPUFEAT_X86_AESNI)) return rijndael_eblk_x86_aesni;
94#endif
95 return simple_eblk;
96}
97
98static rijndael_dblk__functype *pick_dblk(void)
99{
100#ifdef CPUFAM_X86
101 if (cpu_feature_p(CPUFEAT_X86_AESNI)) return rijndael_dblk_x86_aesni;
102#endif
103 return simple_dblk;
104}
105
bb3d2477 106#define DO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
107 aa = what(t, a, b, c, d) ^ *w++; \
108 bb = what(t, b, c, d, a) ^ *w++; \
109 cc = what(t, c, d, a, b) ^ *w++; \
110 dd = what(t, d, a, b, c) ^ *w++; \
3a65506d 111} while (0)
112
bb3d2477 113#define UNDO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
114 aa = what(t, a, d, c, b) ^ *w++; \
115 bb = what(t, b, a, d, c) ^ *w++; \
116 cc = what(t, c, b, a, d) ^ *w++; \
117 dd = what(t, d, c, b, a) ^ *w++; \
3a65506d 118} while (0)
119
226639f3 120static void simple_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
3a65506d 121{
122 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
123 uint32 aa, bb, cc, dd;
78ec50fa 124 const uint32 *w = k->w;
3a65506d 125
126 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
bb3d2477 127 aa = a; bb = b; cc = c; dd = d;
3a65506d 128
129 switch (k->nr) {
130 case 14:
bb3d2477 131 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
132 case 13:
133 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
3a65506d 134 case 12:
bb3d2477 135 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
136 case 11:
137 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
3a65506d 138 case 10:
139 default:
bb3d2477 140 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
141 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
142 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
143 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
144 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);
146 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
147 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
148 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
3a65506d 149 }
bb3d2477 150 DO(SUB, S, a, b, c, d, aa, bb, cc, dd, w);
3a65506d 151
152 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
153}
154
226639f3 155static void simple_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
3a65506d 156{
157 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
158 uint32 aa, bb, cc, dd;
78ec50fa 159 const uint32 *w = k->wi;
3a65506d 160
161 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
bb3d2477 162 aa = a; bb = b; cc = c; dd = d;
3a65506d 163
164 switch (k->nr) {
165 case 14:
bb3d2477 166 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
167 case 13:
168 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
3a65506d 169 case 12:
bb3d2477 170 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
171 case 11:
172 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
3a65506d 173 case 10:
174 default:
bb3d2477 175 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
176 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
177 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
178 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
179 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);
181 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
182 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
183 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
3a65506d 184 }
bb3d2477 185 UNDO(SUB, SI, a, b, c, d, aa, bb, cc, dd, w);
3a65506d 186
187 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
188}
189
190BLKC_TEST(RIJNDAEL, rijndael)
191
192/*----- That's all, folks -------------------------------------------------*/