3 * The Rijndael block cipher, 192-bit version
5 * (c) 2001 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 ------------------------------------------------------*/
33 #include <mLib/bits.h>
35 #if defined(TEST_RIG) && defined(ENABLE_ASM_DEBUG)
37 # define BLKC_TESTHOOK do { regdump_init(); } while (0)
43 #include "rijndael192.h"
44 #include "rijndael-base.h"
46 /*----- Main code ---------------------------------------------------------*/
48 /* --- @rijndael192_init@ --- *
50 * Arguments: @rijndael_ctx *k@ = pointer to context to initialize
51 * @const void *buf@ = pointer to buffer of key material
52 * @size_t sz@ = size of the key material
56 * Use: Initializes a Rijndael context with a particular key. This
57 * implementation of Rijndael doesn't impose any particular
58 * limits on the key size except that it must be multiple of 4
59 * bytes long. 256 bits seems sensible, though.
62 void rijndael192_init(rijndael_ctx *k, const void *buf, size_t sz)
64 rijndael_setup(k, RIJNDAEL192_BLKSZ / 4, buf, sz);
67 /* --- @rijndael192_eblk@, @rijndael192_dblk@ --- *
69 * Arguments: @const rijndael_ctx *k@ = pointer to Rijndael context
70 * @const uint32 s[4]@ = pointer to source block
71 * @uint32 d[4]@ = pointer to destination block
75 * Use: Low-level block encryption and decryption.
78 #define DO(what, t, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w) do { \
79 aa = what(t, a, b, c, d) ^ *w++; \
80 bb = what(t, b, c, d, e) ^ *w++; \
81 cc = what(t, c, d, e, f) ^ *w++; \
82 dd = what(t, d, e, f, a) ^ *w++; \
83 ee = what(t, e, f, a, b) ^ *w++; \
84 ff = what(t, f, a, b, c) ^ *w++; \
87 #define UNDO(what, t, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w) do { \
88 aa = what(t, a, f, e, d) ^ *w++; \
89 bb = what(t, b, a, f, e) ^ *w++; \
90 cc = what(t, c, b, a, f) ^ *w++; \
91 dd = what(t, d, c, b, a) ^ *w++; \
92 ee = what(t, e, d, c, b) ^ *w++; \
93 ff = what(t, f, e, d, c) ^ *w++; \
96 void rijndael192_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
98 uint32 a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5];
99 uint32 aa, bb, cc, dd, ee, ff;
100 const uint32 *w = k->w;
102 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++; e ^= *w++; f ^= *w++;
103 aa = a; bb = b; cc = c; dd = d; ee = e; ff = f;
107 DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
109 DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
112 DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
113 DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
114 DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
115 DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
116 DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
117 DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
118 DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
119 DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
120 DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
121 DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
122 DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
124 DO(SUB, S, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
126 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d; dst[4] = e; dst[5] = f;
129 void rijndael192_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
131 uint32 a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5];
132 uint32 aa, bb, cc, dd, ee, ff;
133 const uint32 *w = k->wi;
135 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++; e ^= *w++; f ^= *w++;
136 aa = a; bb = b; cc = c; dd = d; ee = e; ff = f;
140 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
142 UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
145 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
146 UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
147 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
148 UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
149 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
150 UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
151 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
152 UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
153 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
154 UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
155 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
157 UNDO(SUB, SI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
159 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d; dst[4] = e; dst[5] = f;
162 BLKC_TEST(RIJNDAEL192, rijndael192)
164 /*----- That's all, folks -------------------------------------------------*/