5a7d3ecd7deadab6244595e28058e899562f8beb
[u/mdw/catacomb] / blkc.h
1 /* -*-c-*-
2 *
3 * $Id: blkc.h,v 1.6 2004/04/02 01:03:49 mdw Exp $
4 *
5 * Common definitions for block ciphers
6 *
7 * (c) 1999 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: blkc.h,v $
33 * Revision 1.6 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
35 *
36 * Revision 1.5 2001/05/07 17:28:42 mdw
37 * Support block ciphers with larger blocks.
38 *
39 * Revision 1.4 2001/04/29 17:39:15 mdw
40 * Removed `-sched' tests. Reorganized so that we can theoretically have
41 * multiple tests in the same file. (This isn't so useful in production,
42 * but it's handy when doing test builds.)
43 *
44 * Revision 1.3 2000/06/17 10:47:06 mdw
45 * Slight support for 96-bit ciphers. Support for counter-mode ciphers.
46 *
47 * Revision 1.2 1999/12/10 23:29:48 mdw
48 * Change header file guard names.
49 *
50 * Revision 1.1 1999/09/03 08:41:11 mdw
51 * Initial import.
52 *
53 */
54
55 #ifndef CATACOMB_BLKC_H
56 #define CATACOMB_BLKC_H
57
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61
62 /*----- Header files ------------------------------------------------------*/
63
64 #include <assert.h>
65
66 #include <mLib/bits.h>
67
68 /*----- Theory of operation -----------------------------------------------*
69 *
70 * A block cipher has associated with it a triple, called PRE_CLASS, of the
71 * form `(TYPE, ENDIAN, BITS)', where TYPE is either `N' (representing an
72 * implemented bit size) or `X' (representing an unimplemented bit size,
73 * causing loops to be compiled rather than unrolled code), ENDIAN is `B'
74 * (big) or `L' (little), and BITS is the block size of the cipher in bits.
75 */
76
77 /*----- Data movement macros ----------------------------------------------*/
78
79 /*
80 * `The C preprocessor. You will never find a more wretched hive of bogus
81 * hackery. We must be cautious.'
82 */
83
84 /* --- General dispatch macros --- */
85
86 #define BLKC_DOGLUE(x, y) x ## y
87 #define BLKC_GLUE(x, y) BLKC_DOGLUE(x, y)
88 #define BLKC_APPLY(f, x) f x
89 #define BLKC_FIRST(x, y, z) x
90 #define BLKC_SECOND(x, y, z) y
91 #define BLKC_THIRD(x, y, z) z
92 #define BLKC_TYPE(PRE) BLKC_APPLY(BLKC_FIRST, PRE##_CLASS)
93 #define BLKC_ENDIAN(PRE) BLKC_APPLY(BLKC_SECOND, PRE##_CLASS)
94 #define BLKC_BITS(PRE) BLKC_APPLY(BLKC_THIRD, PRE##_CLASS)
95
96 #define BLKC_STORE_E(PRE) BLKC_GLUE(STORE32_, BLKC_ENDIAN(PRE))
97 #define BLKC_LOAD_E(PRE) BLKC_GLUE(LOAD32_, BLKC_ENDIAN(PRE))
98
99 /* --- Interface macros --- */
100
101 #define BLKC_STORE(PRE, b, w) \
102 BLKC_GLUE(BLKC_STORE_, BLKC_TYPE(PRE)) \
103 (PRE, b, w, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
104
105 #define BLKC_XSTORE(PRE, b, w, wx) \
106 BLKC_GLUE(BLKC_XSTORE_, BLKC_TYPE(PRE)) \
107 (PRE, b, w, wx, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
108
109 #define BLKC_LOAD(PRE, w, b) \
110 BLKC_GLUE(BLKC_LOAD_, BLKC_TYPE(PRE)) \
111 (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
112
113 #define BLKC_XLOAD(PRE, w, b) \
114 BLKC_GLUE(BLKC_XLOAD_, BLKC_TYPE(PRE)) \
115 (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
116
117 #define BLKC_MOVE(PRE, w, wx) \
118 BLKC_GLUE(BLKC_MOVE_, BLKC_TYPE(PRE)) \
119 (PRE, w, wx, BLKC_BITS(PRE))
120
121 #define BLKC_XMOVE(PRE, w, wx) \
122 BLKC_GLUE(BLKC_XMOVE_, BLKC_TYPE(PRE)) \
123 (PRE, w, wx, BLKC_BITS(PRE))
124
125 #define BLKC_STEP(PRE, w) \
126 BLKC_GLUE(BLKC_STEP_X_, BLKC_ENDIAN(PRE)) \
127 (PRE, w)
128
129 #define BLKC_SET(PRE, w, x) \
130 BLKC_GLUE(BLKC_SET_X_, BLKC_ENDIAN(PRE)) \
131 (PRE, w, x)
132
133 #define BLKC_SHOW(PRE, tag, w) do { \
134 fputs(tag ": ", stdout); \
135 BLKC_SKEL_X(PRE, BLKC_W(w);, printf("%08x ", *_w++);); \
136 fputc('\n', stdout); \
137 } while (0)
138
139 /* --- General implementation skeleton --- */
140
141 #define BLKC_SKEL(PRE, decl, guts) do { \
142 decl \
143 guts \
144 } while (0)
145
146 #define BLKC_P(p) register octet *_p = (octet *)(p)
147 #define BLKC_W(w) register uint32 *_w = (w)
148 #define BLKC_WX(wx) register uint32 *_wx = (wx)
149
150 /* --- Implementation for unusual block sizes --- */
151
152 #define BLKC_SKEL_X(PRE, decl, guts) \
153 BLKC_SKEL(PRE, unsigned _i; decl, \
154 for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) { \
155 guts \
156 })
157
158 #define BLKC_STORE_X(PRE, b, w, op, n) \
159 BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w);, \
160 op(_p, *_w); _p += 4; _w++; )
161
162 #define BLKC_XSTORE_X(PRE, b, w, wx, op, n) \
163 BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);, \
164 op(_p, *_w ^ *_wx); _p += 4; _w++; _wx++; )
165
166 #define BLKC_LOAD_X(PRE, w, b, op, n) \
167 BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);, \
168 *_w = op(_p); _p += 4; _w++; )
169
170 #define BLKC_XLOAD_X(PRE, w, b, op, n) \
171 BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);, \
172 *_w ^= op(_p); _p += 4; _w++; )
173
174 #define BLKC_MOVE_X(PRE, w, wx, n) \
175 BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);, \
176 *_w = *_wx; _w++; _wx++; ) \
177
178 #define BLKC_XMOVE_X(PRE, w, wx, n) \
179 BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);, \
180 *_w ^= *_wx; _w++; _wx++; ) \
181
182 #define BLKC_STEP_X_B(PRE, w) do { \
183 unsigned _i = PRE##_BLKSZ / 4; BLKC_W(w); uint32 _x = 0; \
184 while (_i && !_x) { _i--; _w[_i] = _x = U32(_w[_i] + 1); } \
185 } while (0)
186
187 #define BLKC_STEP_X_L(PRE, w) do { \
188 unsigned _i = 0; BLKC_W(w); uint32 _x = 0; \
189 while (_i < PRE##_BLKSZ / 4 && !_x) \
190 { _w[_i] = _x = U32(_w[_i] + 1); _i++; } \
191 } while (0)
192
193 #define BLKC_SET_X_B(PRE, w, x) do { \
194 unsigned _i; BLKC_W(w); unsigned long _x = x; \
195 for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) { \
196 *_w++ = U32(_x); \
197 _x = ((_x & ~MASK32) >> 16) >> 16; \
198 } \
199 } while (0)
200
201 #define BLKC_SET_X_L(PRE, w, x) do { \
202 unsigned _i; BLKC_W(w); unsigned long _x = x; _w += PRE##_BLKSZ / 4; \
203 for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) { \
204 *--_w = U32(_x); \
205 _x = ((_x & ~MASK32) >> 16) >> 16; \
206 } \
207 } while (0)
208
209 /* --- Implementation for known block sizes --- */
210
211 #define BLKC_SKEL_64(PRE, decl, op, guts) \
212 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1);)
213
214 #define BLKC_SKEL_96(PRE, decl, op, guts) \
215 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2);)
216
217 #define BLKC_SKEL_128(PRE, decl, op, guts) \
218 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3);)
219
220 #define BLKC_SKEL_192(PRE, decl, op, guts) \
221 BLKC_SKEL(PRE, decl, \
222 guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3); \
223 guts(op, 4); guts(op, 5);)
224
225 #define BLKC_SKEL_256(PRE, decl, op, guts) \
226 BLKC_SKEL(PRE, decl, \
227 guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3); \
228 guts(op, 4); guts(op, 5); guts(op, 6); guts(op, 7);)
229
230 #define BLKC_STORE_GUTS(op, i) op(_p + 4 * i, _w[i])
231 #define BLKC_XSTORE_GUTS(op, i) op(_p + 4 * i, _w[i] ^ _wx[i])
232 #define BLKC_LOAD_GUTS(op, i) _w[i] = op(_p + 4 * i)
233 #define BLKC_XLOAD_GUTS(op, i) _w[i] ^= op(_p + 4 * i)
234 #define BLKC_MOVE_GUTS(op, i) _w[i] = _wx[i]
235 #define BLKC_XMOVE_GUTS(op, i) _w[i] ^= _wx[i]
236
237 #define BLKC_STORE_N(PRE, b, w, op, n) \
238 BLKC_GLUE(BLKC_SKEL_, n) \
239 (PRE, BLKC_P(b); const BLKC_W(w);, op, BLKC_STORE_GUTS)
240
241 #define BLKC_XSTORE_N(PRE, b, w, wx, op, n) \
242 BLKC_GLUE(BLKC_SKEL_, n) \
243 (PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);, \
244 op, BLKC_XSTORE_GUTS)
245
246 #define BLKC_LOAD_N(PRE, w, b, op, n) \
247 BLKC_GLUE(BLKC_SKEL_, n) \
248 (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_LOAD_GUTS)
249
250 #define BLKC_XLOAD_N(PRE, w, b, op, n) \
251 BLKC_GLUE(BLKC_SKEL_, n) \
252 (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_XLOAD_GUTS)
253
254 #define BLKC_MOVE_N(PRE, w, wx, n) \
255 BLKC_GLUE(BLKC_SKEL_, n) \
256 (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_MOVE_GUTS)
257
258 #define BLKC_XMOVE_N(PRE, w, wx, n) \
259 BLKC_GLUE(BLKC_SKEL_, n) \
260 (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_XMOVE_GUTS)
261
262 /*----- Test rig for block ciphers ----------------------------------------*/
263
264 /* --- @BLKC_TEST@ --- *
265 *
266 * Arguments: @PRE@, @pre@ = prefixes for cipher-specific definitions
267 *
268 * Use: Standard test rig for block ciphers.
269 */
270
271 #ifdef TEST_RIG
272
273 #include <mLib/quis.h>
274 #include <mLib/testrig.h>
275
276 #define BLKC_VERIFY(PRE, pre) \
277 \
278 static int pre##_verify(dstr *v) \
279 { \
280 pre##_ctx k; \
281 uint32 p[PRE##_BLKSZ / 4]; \
282 uint32 c[PRE##_BLKSZ / 4]; \
283 uint32 d[PRE##_BLKSZ / 4]; \
284 dstr b = DSTR_INIT; \
285 int ok = 1; \
286 \
287 /* --- Initialize the key buffer --- */ \
288 \
289 dstr_ensure(&b, PRE##_BLKSZ); \
290 b.len = PRE##_BLKSZ; \
291 pre##_init(&k, v[0].buf, v[0].len); \
292 BLKC_LOAD(PRE, p, v[1].buf); \
293 BLKC_LOAD(PRE, c, v[2].buf); \
294 \
295 /* --- Test encryption --- */ \
296 \
297 BLKC_MOVE(PRE, d, p); \
298 pre##_eblk(&k, d, d); \
299 BLKC_STORE(PRE, b.buf, d); \
300 if (memcmp(b.buf, v[2].buf, PRE##_BLKSZ)) { \
301 ok = 0; \
302 printf("\nfail encryption:" \
303 "\n\tkey = "); \
304 type_hex.dump(&v[0], stdout); \
305 printf("\n\tplaintext = "); type_hex.dump(&v[1], stdout); \
306 printf("\n\texpected = "); type_hex.dump(&v[2], stdout); \
307 printf("\n\tcalculated = "); type_hex.dump(&b, stdout); \
308 putchar('\n'); \
309 } \
310 \
311 /* --- Test decryption --- */ \
312 \
313 BLKC_MOVE(PRE, d, c); \
314 pre##_dblk(&k, d, d); \
315 BLKC_STORE(PRE, b.buf, d); \
316 if (memcmp(b.buf, v[1].buf, PRE##_BLKSZ)) { \
317 ok = 0; \
318 printf("\nfail decryption:" \
319 "\n\tkey = "); \
320 type_hex.dump(&v[0], stdout); \
321 printf("\n\tciphertext = "); type_hex.dump(&v[2], stdout); \
322 printf("\n\texpected = "); type_hex.dump(&v[1], stdout); \
323 printf("\n\tcalculated = "); type_hex.dump(&b, stdout); \
324 putchar('\n'); \
325 } \
326 \
327 /* --- Return --- */ \
328 \
329 return (ok); \
330 }
331
332 #define BLKC_TEST(PRE, pre) \
333 \
334 BLKC_VERIFY(PRE, pre) \
335 \
336 static const test_chunk defs[] = { \
337 { #pre, pre##_verify, { &type_hex, &type_hex, &type_hex, 0 } }, \
338 { 0, 0, { 0 } } \
339 }; \
340 \
341 int main(int argc, char *argv[]) \
342 { \
343 test_run(argc, argv, defs, SRCDIR"/tests/" #pre); \
344 return (0); \
345 }
346
347 #else
348 # define BLKC_VERIFY(PRE, pre)
349 # define BLKC_TEST(PRE, pre)
350 #endif
351
352 /*----- That's all, folks -------------------------------------------------*/
353
354 #ifdef __cplusplus
355 }
356 #endif
357
358 #endif