3 * $Id: blkc.h,v 1.5 2001/05/07 17:28:42 mdw Exp $
5 * Common definitions for block ciphers
7 * (c) 1999 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.5 2001/05/07 17:28:42 mdw
34 * Support block ciphers with larger blocks.
36 * Revision 1.4 2001/04/29 17:39:15 mdw
37 * Removed `-sched' tests. Reorganized so that we can theoretically have
38 * multiple tests in the same file. (This isn't so useful in production,
39 * but it's handy when doing test builds.)
41 * Revision 1.3 2000/06/17 10:47:06 mdw
42 * Slight support for 96-bit ciphers. Support for counter-mode ciphers.
44 * Revision 1.2 1999/12/10 23:29:48 mdw
45 * Change header file guard names.
47 * Revision 1.1 1999/09/03 08:41:11 mdw
52 #ifndef CATACOMB_BLKC_H
53 #define CATACOMB_BLKC_H
59 /*----- Header files ------------------------------------------------------*/
63 #include <mLib/bits.h>
65 /*----- Theory of operation -----------------------------------------------*
67 * A block cipher has associated with it a triple, called PRE_CLASS, of the
68 * form `(TYPE, ENDIAN, BITS)', where TYPE is either `N' (representing an
69 * implemented bit size) or `X' (representing an unimplemented bit size,
70 * causing loops to be compiled rather than unrolled code), ENDIAN is `B'
71 * (big) or `L' (little), and BITS is the block size of the cipher in bits.
74 /*----- Data movement macros ----------------------------------------------*/
77 * `The C preprocessor. You will never find a more wretched hive of bogus
78 * hackery. We must be cautious.'
81 /* --- General dispatch macros --- */
83 #define BLKC_DOGLUE(x, y) x ## y
84 #define BLKC_GLUE(x, y) BLKC_DOGLUE(x, y)
85 #define BLKC_APPLY(f, x) f x
86 #define BLKC_FIRST(x, y, z) x
87 #define BLKC_SECOND(x, y, z) y
88 #define BLKC_THIRD(x, y, z) z
89 #define BLKC_TYPE(PRE) BLKC_APPLY(BLKC_FIRST, PRE##_CLASS)
90 #define BLKC_ENDIAN(PRE) BLKC_APPLY(BLKC_SECOND, PRE##_CLASS)
91 #define BLKC_BITS(PRE) BLKC_APPLY(BLKC_THIRD, PRE##_CLASS)
93 #define BLKC_STORE_E(PRE) BLKC_GLUE(STORE32_, BLKC_ENDIAN(PRE))
94 #define BLKC_LOAD_E(PRE) BLKC_GLUE(LOAD32_, BLKC_ENDIAN(PRE))
96 /* --- Interface macros --- */
98 #define BLKC_STORE(PRE, b, w) \
99 BLKC_GLUE(BLKC_STORE_, BLKC_TYPE(PRE)) \
100 (PRE, b, w, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
102 #define BLKC_XSTORE(PRE, b, w, wx) \
103 BLKC_GLUE(BLKC_XSTORE_, BLKC_TYPE(PRE)) \
104 (PRE, b, w, wx, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
106 #define BLKC_LOAD(PRE, w, b) \
107 BLKC_GLUE(BLKC_LOAD_, BLKC_TYPE(PRE)) \
108 (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
110 #define BLKC_XLOAD(PRE, w, b) \
111 BLKC_GLUE(BLKC_XLOAD_, BLKC_TYPE(PRE)) \
112 (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
114 #define BLKC_MOVE(PRE, w, wx) \
115 BLKC_GLUE(BLKC_MOVE_, BLKC_TYPE(PRE)) \
116 (PRE, w, wx, BLKC_BITS(PRE))
118 #define BLKC_XMOVE(PRE, w, wx) \
119 BLKC_GLUE(BLKC_XMOVE_, BLKC_TYPE(PRE)) \
120 (PRE, w, wx, BLKC_BITS(PRE))
122 #define BLKC_STEP(PRE, w) \
123 BLKC_GLUE(BLKC_STEP_X_, BLKC_ENDIAN(PRE)) \
126 #define BLKC_SET(PRE, w, x) \
127 BLKC_GLUE(BLKC_SET_X_, BLKC_ENDIAN(PRE)) \
130 #define BLKC_SHOW(PRE, tag, w) do { \
131 fputs(tag ": ", stdout); \
132 BLKC_SKEL_X(PRE, BLKC_W(w);, printf("%08x ", *_w++);); \
133 fputc('\n', stdout); \
136 /* --- General implementation skeleton --- */
138 #define BLKC_SKEL(PRE, decl, guts) do { \
143 #define BLKC_P(p) register octet *_p = (octet *)(p)
144 #define BLKC_W(w) register uint32 *_w = (w)
145 #define BLKC_WX(wx) register uint32 *_wx = (wx)
147 /* --- Implementation for unusual block sizes --- */
149 #define BLKC_SKEL_X(PRE, decl, guts) \
150 BLKC_SKEL(PRE, unsigned _i; decl, \
151 for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) { \
155 #define BLKC_STORE_X(PRE, b, w, op, n) \
156 BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w);, \
157 op(_p, *_w); _p += 4; _w++; )
159 #define BLKC_XSTORE_X(PRE, b, w, wx, op, n) \
160 BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);, \
161 op(_p, *_w ^ *_wx); _p += 4; _w++; _wx++; )
163 #define BLKC_LOAD_X(PRE, w, b, op, n) \
164 BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);, \
165 *_w = op(_p); _p += 4; _w++; )
167 #define BLKC_XLOAD_X(PRE, w, b, op, n) \
168 BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);, \
169 *_w ^= op(_p); _p += 4; _w++; )
171 #define BLKC_MOVE_X(PRE, w, wx, n) \
172 BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);, \
173 *_w = *_wx; _w++; _wx++; ) \
175 #define BLKC_XMOVE_X(PRE, w, wx, n) \
176 BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);, \
177 *_w ^= *_wx; _w++; _wx++; ) \
179 #define BLKC_STEP_X_B(PRE, w) do { \
180 unsigned _i = PRE##_BLKSZ / 4; BLKC_W(w); uint32 _x = 0; \
181 while (_i && !_x) { _i--; _w[_i] = _x = U32(_w[_i] + 1); } \
184 #define BLKC_STEP_X_L(PRE, w) do { \
185 unsigned _i = 0; BLKC_W(w); uint32 _x = 0; \
186 while (_i < PRE##_BLKSZ / 4 && !_x) \
187 { _w[_i] = _x = U32(_w[_i] + 1); _i++; } \
190 #define BLKC_SET_X_B(PRE, w, x) do { \
191 unsigned _i; BLKC_W(w); unsigned long _x = x; \
192 for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) { \
194 _x = ((_x & ~MASK32) >> 16) >> 16; \
198 #define BLKC_SET_X_L(PRE, w, x) do { \
199 unsigned _i; BLKC_W(w); unsigned long _x = x; _w += PRE##_BLKSZ / 4; \
200 for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) { \
202 _x = ((_x & ~MASK32) >> 16) >> 16; \
206 /* --- Implementation for known block sizes --- */
208 #define BLKC_SKEL_64(PRE, decl, op, guts) \
209 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1);)
211 #define BLKC_SKEL_96(PRE, decl, op, guts) \
212 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2);)
214 #define BLKC_SKEL_128(PRE, decl, op, guts) \
215 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3);)
217 #define BLKC_SKEL_192(PRE, decl, op, guts) \
218 BLKC_SKEL(PRE, decl, \
219 guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3); \
220 guts(op, 4); guts(op, 5);)
222 #define BLKC_SKEL_256(PRE, decl, op, guts) \
223 BLKC_SKEL(PRE, decl, \
224 guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3); \
225 guts(op, 4); guts(op, 5); guts(op, 6); guts(op, 7);)
227 #define BLKC_STORE_GUTS(op, i) op(_p + 4 * i, _w[i])
228 #define BLKC_XSTORE_GUTS(op, i) op(_p + 4 * i, _w[i] ^ _wx[i])
229 #define BLKC_LOAD_GUTS(op, i) _w[i] = op(_p + 4 * i)
230 #define BLKC_XLOAD_GUTS(op, i) _w[i] ^= op(_p + 4 * i)
231 #define BLKC_MOVE_GUTS(op, i) _w[i] = _wx[i]
232 #define BLKC_XMOVE_GUTS(op, i) _w[i] ^= _wx[i]
234 #define BLKC_STORE_N(PRE, b, w, op, n) \
235 BLKC_GLUE(BLKC_SKEL_, n) \
236 (PRE, BLKC_P(b); const BLKC_W(w);, op, BLKC_STORE_GUTS)
238 #define BLKC_XSTORE_N(PRE, b, w, wx, op, n) \
239 BLKC_GLUE(BLKC_SKEL_, n) \
240 (PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);, \
241 op, BLKC_XSTORE_GUTS)
243 #define BLKC_LOAD_N(PRE, w, b, op, n) \
244 BLKC_GLUE(BLKC_SKEL_, n) \
245 (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_LOAD_GUTS)
247 #define BLKC_XLOAD_N(PRE, w, b, op, n) \
248 BLKC_GLUE(BLKC_SKEL_, n) \
249 (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_XLOAD_GUTS)
251 #define BLKC_MOVE_N(PRE, w, wx, n) \
252 BLKC_GLUE(BLKC_SKEL_, n) \
253 (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_MOVE_GUTS)
255 #define BLKC_XMOVE_N(PRE, w, wx, n) \
256 BLKC_GLUE(BLKC_SKEL_, n) \
257 (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_XMOVE_GUTS)
259 /*----- Test rig for block ciphers ----------------------------------------*/
261 /* --- @BLKC_TEST@ --- *
263 * Arguments: @PRE@, @pre@ = prefixes for cipher-specific definitions
265 * Use: Standard test rig for block ciphers.
270 #include <mLib/quis.h>
271 #include <mLib/testrig.h>
273 #define BLKC_VERIFY(PRE, pre) \
275 static int pre##_verify(dstr *v) \
278 uint32 p[PRE##_BLKSZ / 4]; \
279 uint32 c[PRE##_BLKSZ / 4]; \
280 uint32 d[PRE##_BLKSZ / 4]; \
281 dstr b = DSTR_INIT; \
284 /* --- Initialize the key buffer --- */ \
286 dstr_ensure(&b, PRE##_BLKSZ); \
287 b.len = PRE##_BLKSZ; \
288 pre##_init(&k, v[0].buf, v[0].len); \
289 BLKC_LOAD(PRE, p, v[1].buf); \
290 BLKC_LOAD(PRE, c, v[2].buf); \
292 /* --- Test encryption --- */ \
294 BLKC_MOVE(PRE, d, p); \
295 pre##_eblk(&k, d, d); \
296 BLKC_STORE(PRE, b.buf, d); \
297 if (memcmp(b.buf, v[2].buf, PRE##_BLKSZ)) { \
299 printf("\nfail encryption:" \
301 type_hex.dump(&v[0], stdout); \
302 printf("\n\tplaintext = "); type_hex.dump(&v[1], stdout); \
303 printf("\n\texpected = "); type_hex.dump(&v[2], stdout); \
304 printf("\n\tcalculated = "); type_hex.dump(&b, stdout); \
308 /* --- Test decryption --- */ \
310 BLKC_MOVE(PRE, d, c); \
311 pre##_dblk(&k, d, d); \
312 BLKC_STORE(PRE, b.buf, d); \
313 if (memcmp(b.buf, v[1].buf, PRE##_BLKSZ)) { \
315 printf("\nfail decryption:" \
317 type_hex.dump(&v[0], stdout); \
318 printf("\n\tciphertext = "); type_hex.dump(&v[2], stdout); \
319 printf("\n\texpected = "); type_hex.dump(&v[1], stdout); \
320 printf("\n\tcalculated = "); type_hex.dump(&b, stdout); \
324 /* --- Return --- */ \
329 #define BLKC_TEST(PRE, pre) \
331 BLKC_VERIFY(PRE, pre) \
333 static test_chunk defs[] = { \
334 { #pre, pre##_verify, { &type_hex, &type_hex, &type_hex, 0 } }, \
338 int main(int argc, char *argv[]) \
340 test_run(argc, argv, defs, SRCDIR"/tests/" #pre); \
345 # define BLKC_VERIFY(PRE, pre)
346 # define BLKC_TEST(PRE, pre)
349 /*----- That's all, folks -------------------------------------------------*/