New file.
[u/mdw/catacomb] / rijndael256.c
CommitLineData
2e8eb64a 1/* -*-c-*-
2 *
3 * $Id: rijndael256.c,v 1.1 2001/05/07 17:32:03 mdw Exp $
4 *
5 * The Rijndael block cipher, 256-bit version
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: rijndael256.c,v $
33 * Revision 1.1 2001/05/07 17:32:03 mdw
34 * New Rijndael block sizes.
35 *
36 */
37
38/*----- Header files ------------------------------------------------------*/
39
40#include <assert.h>
41#include <stdio.h>
42
43#include <mLib/bits.h>
44
45#include "blkc.h"
46#include "gcipher.h"
47#include "rijndael.h"
48#include "rijndael256.h"
49#include "rijndael-base.h"
50
51/*----- Main code ---------------------------------------------------------*/
52
53/* --- @rijndael256_init@ --- *
54 *
55 * Arguments: @rijndael_ctx *k@ = pointer to context to initialize
56 * @const void *buf@ = pointer to buffer of key material
57 * @size_t sz@ = size of the key material
58 *
59 * Returns: ---
60 *
61 * Use: Initializes a Rijndael context with a particular key. This
62 * implementation of Rijndael doesn't impose any particular
63 * limits on the key size except that it must be multiple of 4
64 * bytes long. 256 bits seems sensible, though.
65 */
66
67void rijndael256_init(rijndael_ctx *k, const void *buf, size_t sz)
68{
69 rijndael_setup(k, RIJNDAEL256_BLKSZ / 4, buf, sz);
70}
71
72/* --- @rijndael256_eblk@, @rijndael256_dblk@ --- *
73 *
74 * Arguments: @const rijndael_ctx *k@ = pointer to Rijndael context
75 * @const uint32 s[4]@ = pointer to source block
76 * @uint32 d[4]@ = pointer to destination block
77 *
78 * Returns: ---
79 *
80 * Use: Low-level block encryption and decryption.
81 */
82
83#define DO(what, t, \
84 aa, bb, cc, dd, ee, ff, gg, hh, \
85 a, b, c, d, e, f, g, h, w) do { \
86 aa = what(t, a, b, d, e) ^ *w++; \
87 bb = what(t, b, c, e, f) ^ *w++; \
88 cc = what(t, c, d, f, g) ^ *w++; \
89 dd = what(t, d, e, g, h) ^ *w++; \
90 ee = what(t, e, f, h, a) ^ *w++; \
91 ff = what(t, f, g, a, b) ^ *w++; \
92 gg = what(t, g, h, b, c) ^ *w++; \
93 hh = what(t, h, a, c, d) ^ *w++; \
94} while (0)
95
96#define UNDO(what, t, \
97 aa, bb, cc, dd, ee, ff, gg, hh, \
98 a, b, c, d, e, f, g, h, w) do { \
99 aa = what(t, a, h, f, e) ^ *w++; \
100 bb = what(t, b, a, g, f) ^ *w++; \
101 cc = what(t, c, b, h, g) ^ *w++; \
102 dd = what(t, d, c, a, h) ^ *w++; \
103 ee = what(t, e, d, b, a) ^ *w++; \
104 ff = what(t, f, e, c, b) ^ *w++; \
105 gg = what(t, g, f, d, c) ^ *w++; \
106 hh = what(t, h, g, e, d) ^ *w++; \
107} while (0)
108
109void rijndael256_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
110{
111 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
112 uint32 e = s[4], f = s[5], g = s[6], h = s[7];
113 uint32 aa, bb, cc, dd, ee, ff, gg, hh;
114 uint32 *w = k->w;
115
116 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
117 e ^= *w++; f ^= *w++; g ^= *w++; h ^= *w++;
118
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(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
123 DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
124 DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
125 DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
126 DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
127 DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
128 DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
129 DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
130 DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
131 DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
132 DO(SUB, S, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
133
134 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
135 dst[4] = e; dst[5] = f; dst[6] = g; dst[7] = h;
136}
137
138void rijndael256_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
139{
140 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
141 uint32 e = s[4], f = s[5], g = s[6], h = s[7];
142 uint32 aa, bb, cc, dd, ee, ff, gg, hh;
143 uint32 *w = k->wi;
144
145 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
146 e ^= *w++; f ^= *w++; g ^= *w++; h ^= *w++;
147
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(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
152 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
153 UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
154 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
155 UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
156 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
157 UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
158 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
159 UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
160 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
161 UNDO(SUB, SI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
162
163 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
164 dst[4] = e; dst[5] = f; dst[6] = g; dst[7] = h;
165}
166
167BLKC_TEST(RIJNDAEL256, rijndael256)
168
169/*----- That's all, folks -------------------------------------------------*/