chiark / gitweb /
utils/split-pieces (QfConvert): Split out a subclass.
[catacomb] / symm / rijndael256.c
1 /* -*-c-*-
2  *
3  * The Rijndael block cipher, 256-bit version
4  *
5  * (c) 2001 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 <assert.h>
31 #include <stdio.h>
32
33 #include <mLib/bits.h>
34
35 #include "blkc.h"
36 #include "gcipher.h"
37 #include "rijndael.h"
38 #include "rijndael256.h"
39 #include "rijndael-base.h"
40
41 /*----- Main code ---------------------------------------------------------*/
42
43 /* --- @rijndael256_init@ --- *
44  *
45  * Arguments:   @rijndael_ctx *k@ = pointer to context to initialize
46  *              @const void *buf@ = pointer to buffer of key material
47  *              @size_t sz@ = size of the key material
48  *
49  * Returns:     ---
50  *
51  * Use:         Initializes a Rijndael context with a particular key.  This
52  *              implementation of Rijndael doesn't impose any particular
53  *              limits on the key size except that it must be multiple of 4
54  *              bytes long.  256 bits seems sensible, though.
55  */
56
57 void rijndael256_init(rijndael_ctx *k, const void *buf, size_t sz)
58 {
59   rijndael_setup(k, RIJNDAEL256_BLKSZ / 4, buf, sz);
60 }
61
62 /* --- @rijndael256_eblk@, @rijndael256_dblk@ --- *
63  *
64  * Arguments:   @const rijndael_ctx *k@ = pointer to Rijndael context
65  *              @const uint32 s[4]@ = pointer to source block
66  *              @uint32 d[4]@ = pointer to destination block
67  *
68  * Returns:     ---
69  *
70  * Use:         Low-level block encryption and decryption.
71  */
72
73 #define DO(what, t,                                                     \
74            aa, bb, cc, dd, ee, ff, gg, hh,                              \
75            a, b, c, d, e, f, g, h, w) do {                              \
76   aa = what(t, a, b, d, e) ^ *w++;                                      \
77   bb = what(t, b, c, e, f) ^ *w++;                                      \
78   cc = what(t, c, d, f, g) ^ *w++;                                      \
79   dd = what(t, d, e, g, h) ^ *w++;                                      \
80   ee = what(t, e, f, h, a) ^ *w++;                                      \
81   ff = what(t, f, g, a, b) ^ *w++;                                      \
82   gg = what(t, g, h, b, c) ^ *w++;                                      \
83   hh = what(t, h, a, c, d) ^ *w++;                                      \
84 } while (0)
85
86 #define UNDO(what, t,                                                   \
87              aa, bb, cc, dd, ee, ff, gg, hh,                            \
88              a, b, c, d, e, f, g, h, w) do {                            \
89   aa = what(t, a, h, f, e) ^ *w++;                                      \
90   bb = what(t, b, a, g, f) ^ *w++;                                      \
91   cc = what(t, c, b, h, g) ^ *w++;                                      \
92   dd = what(t, d, c, a, h) ^ *w++;                                      \
93   ee = what(t, e, d, b, a) ^ *w++;                                      \
94   ff = what(t, f, e, c, b) ^ *w++;                                      \
95   gg = what(t, g, f, d, c) ^ *w++;                                      \
96   hh = what(t, h, g, e, d) ^ *w++;                                      \
97 } while (0)
98
99 void rijndael256_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
100 {
101   uint32 a = s[0], b = s[1], c = s[2], d = s[3];
102   uint32 e = s[4], f = s[5], g = s[6], h = s[7];
103   uint32 aa, bb, cc, dd, ee, ff, gg, hh;
104   const uint32 *w = k->w;
105
106   a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
107   e ^= *w++; f ^= *w++; g ^= *w++; h ^= *w++;
108
109   DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
110   DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
111   DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
112   DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
113   DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
114   DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
115   DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
116   DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
117   DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
118   DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
119   DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
120   DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
121   DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
122   DO(SUB, S, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
123
124   dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
125   dst[4] = e; dst[5] = f; dst[6] = g; dst[7] = h;
126 }
127
128 void rijndael256_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
129 {
130   uint32 a = s[0], b = s[1], c = s[2], d = s[3];
131   uint32 e = s[4], f = s[5], g = s[6], h = s[7];
132   uint32 aa, bb, cc, dd, ee, ff, gg, hh;
133   const uint32 *w = k->wi;
134
135   a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
136   e ^= *w++; f ^= *w++; g ^= *w++; h ^= *w++;
137
138   UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
139   UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
140   UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
141   UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
142   UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
143   UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
144   UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
145   UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
146   UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
147   UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
148   UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
149   UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
150   UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
151   UNDO(SUB, SI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
152
153   dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
154   dst[4] = e; dst[5] = f; dst[6] = g; dst[7] = h;
155 }
156
157 BLKC_TEST(RIJNDAEL256, rijndael256)
158
159 /*----- That's all, folks -------------------------------------------------*/