Allow explicit group parameters for DH groups.
[u/mdw/catacomb] / rijndael.c
1 /* -*-c-*-
2 *
3 * $Id: rijndael.c,v 1.4 2001/05/07 17:31:53 mdw Exp $
4 *
5 * The Rijndael block cipher
6 *
7 * (c) 2000 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: rijndael.c,v $
33 * Revision 1.4 2001/05/07 17:31:53 mdw
34 * Separate out key scheduling.
35 *
36 * Revision 1.3 2001/05/07 15:44:46 mdw
37 * Fix unusual numbers of rounds. Simplify implementation.
38 *
39 * Revision 1.2 2000/12/06 20:32:59 mdw
40 * Fix round count for weird key sizes.
41 *
42 * Revision 1.1 2000/06/17 11:56:07 mdw
43 * New cipher.
44 *
45 */
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include <assert.h>
50 #include <stdio.h>
51
52 #include <mLib/bits.h>
53
54 #include "blkc.h"
55 #include "gcipher.h"
56 #include "rijndael.h"
57 #include "rijndael-base.h"
58
59 /*----- Main code ---------------------------------------------------------*/
60
61 /* --- @rijndael_init@ --- *
62 *
63 * Arguments: @rijndael_ctx *k@ = pointer to context to initialize
64 * @const void *buf@ = pointer to buffer of key material
65 * @size_t sz@ = size of the key material
66 *
67 * Returns: ---
68 *
69 * Use: Initializes a Rijndael context with a particular key. This
70 * implementation of Rijndael doesn't impose any particular
71 * limits on the key size except that it must be multiple of 4
72 * bytes long. 256 bits seems sensible, though.
73 */
74
75 void rijndael_init(rijndael_ctx *k, const void *buf, size_t sz)
76 {
77 rijndael_setup(k, RIJNDAEL_BLKSZ / 4, buf, sz);
78 }
79
80 /* --- @rijndael_eblk@, @rijndael_dblk@ --- *
81 *
82 * Arguments: @const rijndael_ctx *k@ = pointer to Rijndael context
83 * @const uint32 s[4]@ = pointer to source block
84 * @uint32 d[4]@ = pointer to destination block
85 *
86 * Returns: ---
87 *
88 * Use: Low-level block encryption and decryption.
89 */
90
91 #define DO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
92 aa = what(t, a, b, c, d) ^ *w++; \
93 bb = what(t, b, c, d, a) ^ *w++; \
94 cc = what(t, c, d, a, b) ^ *w++; \
95 dd = what(t, d, a, b, c) ^ *w++; \
96 } while (0)
97
98 #define UNDO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
99 aa = what(t, a, d, c, b) ^ *w++; \
100 bb = what(t, b, a, d, c) ^ *w++; \
101 cc = what(t, c, b, a, d) ^ *w++; \
102 dd = what(t, d, c, b, a) ^ *w++; \
103 } while (0)
104
105 void rijndael_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
106 {
107 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
108 uint32 aa, bb, cc, dd;
109 uint32 *w = k->w;
110
111 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
112 aa = a; bb = b; cc = c; dd = d;
113
114 switch (k->nr) {
115 case 14:
116 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
117 case 13:
118 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
119 case 12:
120 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
121 case 11:
122 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
123 case 10:
124 default:
125 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
126 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
127 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
128 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
129 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
130 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
131 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
132 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
133 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
134 }
135 DO(SUB, S, a, b, c, d, aa, bb, cc, dd, w);
136
137 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
138 }
139
140 void rijndael_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
141 {
142 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
143 uint32 aa, bb, cc, dd;
144 uint32 *w = k->wi;
145
146 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
147 aa = a; bb = b; cc = c; dd = d;
148
149 switch (k->nr) {
150 case 14:
151 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
152 case 13:
153 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
154 case 12:
155 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
156 case 11:
157 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
158 case 10:
159 default:
160 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
161 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
162 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
163 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
164 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
165 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
166 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
167 UNDO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
168 UNDO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
169 }
170 UNDO(SUB, SI, a, b, c, d, aa, bb, cc, dd, w);
171
172 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
173 }
174
175 BLKC_TEST(RIJNDAEL, rijndael)
176
177 /*----- That's all, folks -------------------------------------------------*/