chiark / gitweb /
symm/modes.am.in: Banish the very boring per-mode tests to `modes/'.
[catacomb] / symm / rijndael.c
1 /* -*-c-*-
2  *
3  * The Rijndael block cipher
4  *
5  * (c) 2000 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 "rijndael-base.h"
39
40 /*----- Main code ---------------------------------------------------------*/
41
42 /* --- @rijndael_init@ --- *
43  *
44  * Arguments:   @rijndael_ctx *k@ = pointer to context to initialize
45  *              @const void *buf@ = pointer to buffer of key material
46  *              @size_t sz@ = size of the key material
47  *
48  * Returns:     ---
49  *
50  * Use:         Initializes a Rijndael context with a particular key.  This
51  *              implementation of Rijndael doesn't impose any particular
52  *              limits on the key size except that it must be multiple of 4
53  *              bytes long.  256 bits seems sensible, though.
54  */
55
56 void rijndael_init(rijndael_ctx *k, const void *buf, size_t sz)
57 {
58   rijndael_setup(k, RIJNDAEL_BLKSZ / 4, buf, sz);
59 }
60
61 /* --- @rijndael_eblk@, @rijndael_dblk@ --- *
62  *
63  * Arguments:   @const rijndael_ctx *k@ = pointer to Rijndael context
64  *              @const uint32 s[4]@ = pointer to source block
65  *              @uint32 d[4]@ = pointer to destination block
66  *
67  * Returns:     ---
68  *
69  * Use:         Low-level block encryption and decryption.
70  */
71
72 #define DO(what, t, aa, bb, cc, dd, a, b, c, d, w) do {                 \
73   aa = what(t, a, b, c, d) ^ *w++;                                      \
74   bb = what(t, b, c, d, a) ^ *w++;                                      \
75   cc = what(t, c, d, a, b) ^ *w++;                                      \
76   dd = what(t, d, a, b, c) ^ *w++;                                      \
77 } while (0)
78
79 #define UNDO(what, t, aa, bb, cc, dd, a, b, c, d, w) do {               \
80   aa = what(t, a, d, c, b) ^ *w++;                                      \
81   bb = what(t, b, a, d, c) ^ *w++;                                      \
82   cc = what(t, c, b, a, d) ^ *w++;                                      \
83   dd = what(t, d, c, b, a) ^ *w++;                                      \
84 } while (0)
85
86 void rijndael_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
87 {
88   uint32 a = s[0], b = s[1], c = s[2], d = s[3];
89   uint32 aa, bb, cc, dd;
90   const uint32 *w = k->w;
91
92   a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
93   aa = a; bb = b; cc = c; dd = d;
94
95   switch (k->nr) {
96     case 14:
97       DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
98     case 13:
99       DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
100     case 12:
101       DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
102     case 11:
103       DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
104     case 10:
105     default:
106       DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
107       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   }
116   DO(SUB, S, a, b, c, d, aa, bb, cc, dd, w);
117
118   dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
119 }
120
121 void rijndael_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
122 {
123   uint32 a = s[0], b = s[1], c = s[2], d = s[3];
124   uint32 aa, bb, cc, dd;
125   const uint32 *w = k->wi;
126
127   a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
128   aa = a; bb = b; cc = c; dd = d;
129
130   switch (k->nr) {
131     case 14:
132       UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
133     case 13:
134       UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
135     case 12:
136       UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
137     case 11:
138       UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
139     case 10:
140     default:
141       UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
142       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   }
151   UNDO(SUB, SI, a, b, c, d, aa, bb, cc, dd, w);
152
153   dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
154 }
155
156 BLKC_TEST(RIJNDAEL, rijndael)
157
158 /*----- That's all, folks -------------------------------------------------*/