3 * $Id: rijndael.c,v 1.5 2004/04/08 01:36:15 mdw Exp $
5 * The Rijndael block cipher
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
35 #include <mLib/bits.h>
40 #include "rijndael-base.h"
42 /*----- Main code ---------------------------------------------------------*/
44 /* --- @rijndael_init@ --- *
46 * Arguments: @rijndael_ctx *k@ = pointer to context to initialize
47 * @const void *buf@ = pointer to buffer of key material
48 * @size_t sz@ = size of the key material
52 * Use: Initializes a Rijndael context with a particular key. This
53 * implementation of Rijndael doesn't impose any particular
54 * limits on the key size except that it must be multiple of 4
55 * bytes long. 256 bits seems sensible, though.
58 void rijndael_init(rijndael_ctx *k, const void *buf, size_t sz)
60 rijndael_setup(k, RIJNDAEL_BLKSZ / 4, buf, sz);
63 /* --- @rijndael_eblk@, @rijndael_dblk@ --- *
65 * Arguments: @const rijndael_ctx *k@ = pointer to Rijndael context
66 * @const uint32 s[4]@ = pointer to source block
67 * @uint32 d[4]@ = pointer to destination block
71 * Use: Low-level block encryption and decryption.
74 #define DO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
75 aa = what(t, a, b, c, d) ^ *w++; \
76 bb = what(t, b, c, d, a) ^ *w++; \
77 cc = what(t, c, d, a, b) ^ *w++; \
78 dd = what(t, d, a, b, c) ^ *w++; \
81 #define UNDO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
82 aa = what(t, a, d, c, b) ^ *w++; \
83 bb = what(t, b, a, d, c) ^ *w++; \
84 cc = what(t, c, b, a, d) ^ *w++; \
85 dd = what(t, d, c, b, a) ^ *w++; \
88 void rijndael_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
90 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
91 uint32 aa, bb, cc, dd;
92 const uint32 *w = k->w;
94 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
95 aa = a; bb = b; cc = c; dd = d;
99 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
101 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
103 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
105 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
108 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
109 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
110 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
111 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
112 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
113 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
114 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
115 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
116 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
118 DO(SUB, S, a, b, c, d, aa, bb, cc, dd, w);
120 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
123 void rijndael_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
125 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
126 uint32 aa, bb, cc, dd;
127 const uint32 *w = k->wi;
129 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
130 aa = a; bb = b; cc = c; dd = d;
134 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
136 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
138 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
140 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
143 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
144 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
145 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
146 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
147 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
148 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
149 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
150 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
151 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
153 UNDO(SUB, SI, a, b, c, d, aa, bb, cc, dd, w);
155 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
158 BLKC_TEST(RIJNDAEL, rijndael)
160 /*----- That's all, folks -------------------------------------------------*/