chiark / gitweb /
debian/.gitignore: Ignore `catacomb-data' directory.
[catacomb] / symm / blkc.h
1 /* -*-c-*-
2  *
3  * Common definitions for block ciphers
4  *
5  * (c) 1999 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 #ifndef CATACOMB_BLKC_H
29 #define CATACOMB_BLKC_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <assert.h>
38
39 #include <mLib/bits.h>
40
41 /*----- Theory of operation -----------------------------------------------*
42  *
43  * A block cipher has associated with it a triple, called PRE_CLASS, of the
44  * form `(TYPE, ENDIAN, BITS)', where TYPE is either `N' (representing an
45  * implemented bit size) or `X' (representing an unimplemented bit size,
46  * causing loops to be compiled rather than unrolled code), ENDIAN is `B'
47  * (big) or `L' (little), and BITS is the block size of the cipher in bits.
48  */
49
50 /*----- Data movement macros ----------------------------------------------*/
51
52 /*
53  * `The C preprocessor.  You will never find a more wretched hive of bogus
54  * hackery.  We must be cautious.'
55  */
56
57 /* --- General dispatch macros --- */
58
59 #define BLKC_DOGLUE(x, y) x ## y
60 #define BLKC_GLUE(x, y) BLKC_DOGLUE(x, y)
61 #define BLKC_APPLY(f, x) f x
62 #define BLKC_FIRST(x, y, z) x
63 #define BLKC_SECOND(x, y, z) y
64 #define BLKC_THIRD(x, y, z) z
65 #define BLKC_TYPE(PRE) BLKC_APPLY(BLKC_FIRST, PRE##_CLASS)
66 #define BLKC_ENDIAN(PRE) BLKC_APPLY(BLKC_SECOND, PRE##_CLASS)
67 #define BLKC_BITS(PRE) BLKC_APPLY(BLKC_THIRD, PRE##_CLASS)
68
69 #define BLKC_STORE_E(PRE) BLKC_GLUE(STORE32_, BLKC_ENDIAN(PRE))
70 #define BLKC_LOAD_E(PRE) BLKC_GLUE(LOAD32_, BLKC_ENDIAN(PRE))
71
72 #define BLKC_ID(x) (x)
73
74 /* --- Interface macros --- */
75
76 #define BLKC_STORE(PRE, b, w)                                           \
77   BLKC_GLUE(BLKC_STORE_, BLKC_TYPE(PRE))                                \
78     (PRE, b, w, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
79
80 #define BLKC_XSTORE(PRE, b, w, wx)                                      \
81   BLKC_GLUE(BLKC_XSTORE_, BLKC_TYPE(PRE))                               \
82     (PRE, b, w, wx, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
83
84 #define BLKC_LOAD(PRE, w, b)                                            \
85   BLKC_GLUE(BLKC_LOAD_, BLKC_TYPE(PRE))                                 \
86     (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
87
88 #define BLKC_XLOAD(PRE, w, b)                                           \
89   BLKC_GLUE(BLKC_XLOAD_, BLKC_TYPE(PRE))                                \
90     (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
91
92 #define BLKC_MOVE(PRE, w, wx)                                           \
93   BLKC_GLUE(BLKC_MOVE_, BLKC_TYPE(PRE))                                 \
94     (PRE, w, wx, BLKC_BITS(PRE))
95
96 #define BLKC_XMOVE(PRE, w, wx)                                          \
97   BLKC_GLUE(BLKC_XMOVE_, BLKC_TYPE(PRE))                                \
98     (PRE, w, wx, BLKC_BITS(PRE))
99
100 #define BLKC_BSTEP(PRE, w) BLKC_BADD(PRE, w, 1)
101 #define BLKC_LSTEP(PRE, w) BLKC_LADD(PRE, w, 1)
102 #define BLKC_STEP(PRE, w) BLKC_ADD(PRE, w, 1)
103
104 #define BLKC_BADD(PRE, w, n)                                            \
105   BLKC_GLUE(BLKC_BADD_X_, BLKC_ENDIAN(PRE))                             \
106     (PRE, w, n)
107 #define BLKC_LADD(PRE, w, n)                                            \
108   BLKC_GLUE(BLKC_LADD_X_, BLKC_ENDIAN(PRE))                             \
109     (PRE, w, n)
110 #define BLKC_ADD(PRE, w, n)                                             \
111   BLKC_GLUE(BLKC_ADD_X_, BLKC_ENDIAN(PRE))                              \
112     (PRE, BLKC_ID, w, n)
113
114 #define BLKC_ZERO(PRE, w)                                               \
115   BLKC_GLUE(BLKC_ZERO_, BLKC_TYPE(PRE))                                 \
116     (PRE, w, BLKC_BITS(PRE))
117
118 #define BLKC_BSET(PRE, w, x)                                            \
119   BLKC_GLUE(BLKC_BSET_X_, BLKC_ENDIAN(PRE))                             \
120     (PRE, w, x)
121 #define BLKC_LSET(PRE, w, x)                                            \
122   BLKC_GLUE(BLKC_LSET_X_, BLKC_ENDIAN(PRE))                             \
123     (PRE, w, x)
124 #define BLKC_SET(PRE, w, x)                                             \
125   BLKC_GLUE(BLKC_SET_X_, BLKC_ENDIAN(PRE))                              \
126     (PRE, BLKC_ID, w, x)
127
128 #define BLKC_BWORD(PRE, x) BLKC_GLUE(BLKC_BWORD_, BLKC_ENDIAN(PRE))(x)
129 #define BLKC_LWORD(PRE, x) BLKC_GLUE(BLKC_LWORD_, BLKC_ENDIAN(PRE))(x)
130
131 #define BLKC_SHOW(PRE, tag, w) do {                                     \
132   fputs(tag ": ", stdout);                                              \
133   BLKC_SKEL_X(PRE, const BLKC_W(w);,                                    \
134               { printf("%08x ", BLKC_BWORD(PRE, *_w)); _w++; });        \
135   fputc('\n', stdout);                                                  \
136 } while (0)
137
138 /* --- Utilities --- *
139  *
140  * These seem too hard to properly generalize, or I'd have put them in
141  * <mLib/bits.h>.
142  */
143
144 #ifdef HAVE_UINT64
145 #  define BLKC_ADDC32(op, z_out, c_out, x, y) do {                      \
146      uint64 _t = (uint64)op(x) + (y);                                   \
147      (z_out) = U32(op(_t)); (c_out) = _t >> 32;                         \
148    } while (0)
149 #else
150 #  define BLKC_ADDC32(op, z_out, c_out, x, y) do {                      \
151      uint32 _x = op(x), _c = 0, _t;                                     \
152      _t = U32(_x + (y)); (z_out) = op(_t); (c_out) = (_t < _x);         \
153    } while (0)
154 #endif
155
156 /* --- General implementation skeleton --- */
157
158 #define BLKC_SKEL(PRE, decl, guts) do {                                 \
159   decl                                                                  \
160   guts                                                                  \
161 } while (0)
162
163 #define BLKC_P(p) octet *_p = (octet *)(p)
164 #define BLKC_W(w) uint32 *_w = (w)
165 #define BLKC_WX(wx) uint32 *_wx = (wx)
166
167 /* --- Implementation for unusual block sizes --- */
168
169 #define BLKC_SKEL_X(PRE, decl, guts)                                    \
170   BLKC_SKEL(PRE, unsigned _i; decl,                                     \
171             for (_i = 0; _i < PRE##_BLKSZ/4; _i++) {                    \
172               guts                                                      \
173             })
174
175 #define BLKC_STORE_X(PRE, b, w, op, n)                                  \
176   BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w);,                         \
177               op(_p, *_w); _p += 4; _w++; )
178
179 #define BLKC_XSTORE_X(PRE, b, w, wx, op, n)                             \
180   BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);,      \
181               op(_p, *_w ^ *_wx); _p += 4; _w++; _wx++; )
182
183 #define BLKC_LOAD_X(PRE, w, b, op, n)                                   \
184   BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);,                         \
185               *_w = op(_p); _p += 4; _w++; )
186
187 #define BLKC_XLOAD_X(PRE, w, b, op, n)                                  \
188   BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);,                         \
189               *_w ^= op(_p); _p += 4; _w++; )
190
191 #define BLKC_MOVE_X(PRE, w, wx, n)                                      \
192   BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);,                       \
193               *_w = *_wx; _w++; _wx++; )                                \
194
195 #define BLKC_XMOVE_X(PRE, w, wx, n)                                     \
196   BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);,                       \
197               *_w ^= *_wx; _w++; _wx++; )                               \
198
199 #define BLKC_ZERO_X(PRE, w, n)                                          \
200   BLKC_SKEL_X(PRE, BLKC_W(w);, *_w++ = 0;)
201
202 #define BLKC_BADD_X_B(PRE, w, n) BLKC_ADD_X_B(PRE, BLKC_ID, w, n)
203 #define BLKC_BADD_X_L(PRE, w, n) BLKC_ADD_X_B(PRE, ENDSWAP32, w, n)
204 #define BLKC_LADD_X_B(PRE, w, n) BLKC_ADD_X_L(PRE, ENDSWAP32, w, n)
205 #define BLKC_LADD_X_L(PRE, w, n) BLKC_ADD_X_L(PRE, BLKC_ID, w, n)
206
207 #define BLKC_ADD_X_B(PRE, op, w, n) do {                                \
208   unsigned _i = PRE##_BLKSZ/4; BLKC_W(w); uint32 _n = (n);              \
209   while (_i-- && _n) BLKC_ADDC32(op, _w[_i], _n, _w[_i], _n);           \
210 } while (0)
211
212 #define BLKC_ADD_X_L(PRE, op, w, n) do {                                \
213   unsigned _i = 0; BLKC_W(w); uint32 _n = (n);                          \
214   while (_i < PRE##_BLKSZ/4 && _n)                                      \
215     { BLKC_ADDC32(op, _w[_i], _n, _w[_i], _n); _i++; }                  \
216 } while (0)
217
218 #define BLKC_BSET_X_B(PRE, w, x) BLKC_SET_X_B(PRE, BLKC_ID, w, x)
219 #define BLKC_BSET_X_L(PRE, w, x) BLKC_SET_X_B(PRE, ENDSWAP32, w, x)
220 #define BLKC_LSET_X_B(PRE, w, x) BLKC_SET_X_L(PRE, ENDSWAP32, w, x)
221 #define BLKC_LSET_X_L(PRE, w, x) BLKC_SET_X_L(PRE, BLKC_ID, w, x)
222
223 #define BLKC_SET_X_B(PRE, op, w, x) do {                                \
224   unsigned _i; BLKC_W(w); unsigned long _x = x; _w += PRE##_BLKSZ/4;    \
225   for (_i = 0; _i < PRE##_BLKSZ/4; _i++) {                              \
226     *--_w = U32(op(_x));                                                \
227     _x = ((_x & ~(unsigned long)MASK32) >> 16) >> 16;                   \
228   }                                                                     \
229 } while (0)
230
231 #define BLKC_SET_X_L(PRE, op, w, x) do {                                \
232   unsigned _i; BLKC_W(w); unsigned long _x = x;                         \
233   for (_i = 0; _i < PRE##_BLKSZ/4; _i++) {                              \
234     *_w++ = U32(op(_x));                                                \
235     _x = ((_x & ~(unsigned long)MASK32) >> 16) >> 16;                   \
236   }                                                                     \
237 } while (0)
238
239 #define BLKC_BWORD_B(x) (x)
240 #define BLKC_BWORD_L(x) ENDSWAP32(x)
241 #define BLKC_LWORD_B(x) ENDSWAP32(x)
242 #define BLKC_LWORD_L(x) (x)
243
244 /* --- Implementation for known block sizes --- */
245
246 #define BLKC_SKEL_64(PRE, decl, op, guts)                               \
247   BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1);)
248
249 #define BLKC_SKEL_96(PRE, decl, op, guts)                               \
250   BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2);)
251
252 #define BLKC_SKEL_128(PRE, decl, op, guts)                              \
253   BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3);)
254
255 #define BLKC_SKEL_192(PRE, decl, op, guts)                              \
256   BLKC_SKEL(PRE, decl,                                                  \
257             guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3);         \
258             guts(op, 4); guts(op, 5);)
259
260 #define BLKC_SKEL_256(PRE, decl, op, guts)                              \
261   BLKC_SKEL(PRE, decl,                                                  \
262             guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3);         \
263             guts(op, 4); guts(op, 5); guts(op, 6); guts(op, 7);)
264
265 #define BLKC_STORE_GUTS(op, i) op(_p + 4 * i, _w[i])
266 #define BLKC_XSTORE_GUTS(op, i) op(_p + 4 * i, _w[i] ^ _wx[i])
267 #define BLKC_LOAD_GUTS(op, i) _w[i] = op(_p + 4 * i)
268 #define BLKC_XLOAD_GUTS(op, i) _w[i] ^= op(_p + 4 * i)
269 #define BLKC_MOVE_GUTS(op, i) _w[i] = _wx[i]
270 #define BLKC_XMOVE_GUTS(op, i) _w[i] ^= _wx[i]
271 #define BLKC_ZERO_GUTS(op, i) _w[i] = 0
272
273 #define BLKC_STORE_N(PRE, b, w, op, n)                                  \
274   BLKC_GLUE(BLKC_SKEL_, n)                                              \
275     (PRE, BLKC_P(b); const BLKC_W(w);, op, BLKC_STORE_GUTS)
276
277 #define BLKC_XSTORE_N(PRE, b, w, wx, op, n)                             \
278   BLKC_GLUE(BLKC_SKEL_, n)                                              \
279     (PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);,               \
280      op, BLKC_XSTORE_GUTS)
281
282 #define BLKC_LOAD_N(PRE, w, b, op, n)                                   \
283   BLKC_GLUE(BLKC_SKEL_, n)                                              \
284     (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_LOAD_GUTS)
285
286 #define BLKC_XLOAD_N(PRE, w, b, op, n)                                  \
287   BLKC_GLUE(BLKC_SKEL_, n)                                              \
288     (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_XLOAD_GUTS)
289
290 #define BLKC_MOVE_N(PRE, w, wx, n)                                      \
291   BLKC_GLUE(BLKC_SKEL_, n)                                              \
292     (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_MOVE_GUTS)
293
294 #define BLKC_ZERO_N(PRE, w, n)                                          \
295   BLKC_GLUE(BLKC_SKEL_, n)                                              \
296     (PRE, BLKC_W(w); , op, BLKC_ZERO_GUTS)
297
298 #define BLKC_XMOVE_N(PRE, w, wx, n)                                     \
299   BLKC_GLUE(BLKC_SKEL_, n)                                              \
300     (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_XMOVE_GUTS)
301
302 /*----- Binary field arithmetic -------------------------------------------*/
303
304 #define BLKC_POLY_IRRED64  0x001b
305 #define BLKC_POLY_IRRED96  0x0641
306 #define BLKC_POLY_IRRED128 0x0087
307 #define BLKC_POLY_IRRED192 0x0087
308 #define BLKC_POLY_IRRED256 0x0425
309
310 #define BLKC_POLY_PRIM64   0x001b
311 #define BLKC_POLY_PRIM96   0x0641
312 #define BLKC_POLY_PRIM128  0x0087
313 #define BLKC_POLY_PRIM192  0x8821
314 #define BLKC_POLY_PRIM256  0x0425
315
316 #define BLKC_POLY(PRE, f) BLKC_GLUE(BLKC_POLY_##f, BLKC_BITS(PRE))
317
318 #define BLKC_BLSHIFT(PRE, f, z, x)                                      \
319   BLKC_GLUE(BLKC_BLSHIFT_X_, BLKC_ENDIAN(PRE))                          \
320     (PRE, f, z, x)
321 #define BLKC_LLSHIFT(PRE, f, z, x)                                      \
322   BLKC_GLUE(BLKC_LLSHIFT_X_, BLKC_ENDIAN(PRE))                          \
323     (PRE, f, z, x)
324 #define BLKC_LSHIFT(PRE, f, z, x)                                       \
325   BLKC_GLUE(BLKC_LSHIFT_X_, BLKC_ENDIAN(PRE))                           \
326     (PRE, BLKC_ID, f, z, x)
327
328 #define BLKC_BRSHIFT(PRE, f, z, x)                                      \
329   BLKC_GLUE(BLKC_BRSHIFT_X_, BLKC_ENDIAN(PRE))                          \
330     (PRE, f, z, x)
331 #define BLKC_LRSHIFT(PRE, f, z, x)                                      \
332   BLKC_GLUE(BLKC_LRSHIFT_X_, BLKC_ENDIAN(PRE))                          \
333     (PRE, f, z, x)
334 #define BLKC_RSHIFT(PRE, f, z, x)                                       \
335   BLKC_GLUE(BLKC_RSHIFT_X_, BLKC_ENDIAN(PRE))                           \
336     (PRE, BLKC_ID, f, z, x)
337
338 #define BLKC_BLSHIFT_X_B(PRE, f, z, x) BLKC_LSHIFT_X_B(PRE, BLKC_ID, f, z, x)
339 #define BLKC_LLSHIFT_X_B(PRE, f, z, x) BLKC_LSHIFT_X_L(PRE, ENDSWAP32, f, z, x)
340 #define BLKC_BLSHIFT_X_L(PRE, f, z, x) BLKC_LSHIFT_X_B(PRE, ENDSWAP32, f, z, x)
341 #define BLKC_LLSHIFT_X_L(PRE, f, z, x) BLKC_LSHIFT_X_L(PRE, BLKC_ID, f, z, x)
342
343 #define BLKC_BRSHIFT_X_B(PRE, f, z, x) BLKC_RSHIFT_X_B(PRE, BLKC_ID, f, z, x)
344 #define BLKC_LRSHIFT_X_B(PRE, f, z, x) BLKC_RSHIFT_X_L(PRE, ENDSWAP32, f, z, x)
345 #define BLKC_BRSHIFT_X_L(PRE, f, z, x) BLKC_RSHIFT_X_B(PRE, ENDSWAP32, f, z, x)
346 #define BLKC_LRSHIFT_X_L(PRE, f, z, x) BLKC_RSHIFT_X_L(PRE, BLKC_ID, f, z, x)
347
348 #define BLKC_LSHIFT_X_B(PRE, op, f, z, x) do {                          \
349   uint32 *_z = (z); const uint32 *_x = (x);                             \
350   uint32 _t = op(_x[0]), _m = -(uint32)((_t >> 31)&1u),                 \
351     _c = BLKC_POLY(PRE, f)&_m;                                          \
352   unsigned _i;                                                          \
353                                                                         \
354   for (_i = PRE##_BLKSZ/4; _i-- > 0; )                                  \
355     { _t = op(_x[_i]); _z[_i] = op((_t << 1) ^ _c); _c = (_t >> 31)&1u; } \
356 } while (0)
357
358 #define BLKC_RSHIFT_X_B(PRE, op, f, z, x) do {                          \
359   uint32 *_z = (z); const uint32 *_x = (x);                             \
360   uint32 _t, _t0 = op(_x[PRE##_BLKSZ/4 - 1]), _m = -(uint32)(_t0&1u),   \
361     _c = 0x80000000&_m;                                                 \
362   unsigned _i;                                                          \
363                                                                         \
364   for (_i = 0; _i < PRE##_BLKSZ/4 - 1; _i++)                            \
365     { _t = op(_x[_i]); _z[_i] = op((_t >> 1) ^ _c); _c = (_t&1u) << 31; } \
366   _t0 ^= BLKC_POLY(PRE, f)&_m; _z[PRE##_BLKSZ/4 - 1] = op((_t0 >> 1) ^ _c); \
367 } while (0)
368
369 #define BLKC_LSHIFT_X_L(PRE, op, f, z, x) do {                          \
370   uint32 *_z = (z); const uint32 *_x = (x);                             \
371   uint32 _t = op(_x[PRE##_BLKSZ/4 - 1]), _m = -(uint32)((_t >> 31)&1u), \
372     _c = BLKC_POLY(PRE, f)&_m;                                          \
373   unsigned _i;                                                          \
374                                                                         \
375   for (_i = 0; _i < PRE##_BLKSZ/4; _i++)                                \
376     { _t = op(_x[_i]); _z[_i] = op((_t << 1) ^ _c); _c = (_t >> 31)&1u; } \
377 } while (0)
378
379 #define BLKC_RSHIFT_X_L(PRE, op, f, z, x) do {                          \
380   uint32 *_z = (z); const uint32 *_x = (x);                             \
381   uint32 _t, _t0 = op(_x[0]), _m = -(uint32)(_t0&1u),                   \
382     _c = 0x80000000&_m;                                                 \
383   unsigned _i;                                                          \
384                                                                         \
385   for (_i = PRE##_BLKSZ/4 - 1; _i-- > 1; )                              \
386     { _t = op(_x[_i]); _z[_i] = op((_t >> 1) ^ _c); _c = (_t&1u) << 31; } \
387   _t0 ^= BLKC_POLY(PRE, f)&_m; _z[0] = op((_t0 >> 1) ^ _c);             \
388 } while (0)
389
390 /*----- Test rig for block ciphers ----------------------------------------*/
391
392 /* --- @BLKC_TEST@ --- *
393  *
394  * Arguments:   @PRE@, @pre@ = prefixes for cipher-specific definitions
395  *
396  * Use:         Standard test rig for block ciphers.
397  */
398
399 #ifdef TEST_RIG
400
401 #include <string.h>
402
403 #include <mLib/quis.h>
404 #include <mLib/testrig.h>
405
406 #define BLKC_VERIFY(PRE, pre) BLKC_VERIFYX(PRE, pre, #pre)
407
408 #define BLKC_VERIFYX(PRE, pre, name)                                    \
409                                                                         \
410 static int pre##_verify(dstr *v)                                        \
411 {                                                                       \
412   pre##_ctx k;                                                          \
413   uint32 p[PRE##_BLKSZ/4];                                              \
414   uint32 c[PRE##_BLKSZ/4];                                              \
415   uint32 d[PRE##_BLKSZ/4];                                              \
416   dstr b = DSTR_INIT;                                                   \
417   int ok = 1;                                                           \
418                                                                         \
419   /* --- Initialize the key buffer --- */                               \
420                                                                         \
421   dstr_ensure(&b, PRE##_BLKSZ);                                         \
422   b.len = PRE##_BLKSZ;                                                  \
423   pre##_init(&k, v[0].buf, v[0].len);                                   \
424   BLKC_LOAD(PRE, p, v[1].buf);                                          \
425   BLKC_LOAD(PRE, c, v[2].buf);                                          \
426                                                                         \
427   /* --- Test encryption --- */                                         \
428                                                                         \
429   BLKC_MOVE(PRE, d, p);                                                 \
430   pre##_eblk(&k, d, d);                                                 \
431   BLKC_STORE(PRE, b.buf, d);                                            \
432   if (memcmp(b.buf, v[2].buf, PRE##_BLKSZ)) {                           \
433     ok = 0;                                                             \
434     printf("\nfail encryption:"                                         \
435            "\n\tkey        = ");                                        \
436     type_hex.dump(&v[0], stdout);                                       \
437     printf("\n\tplaintext  = "); type_hex.dump(&v[1], stdout);          \
438     printf("\n\texpected   = "); type_hex.dump(&v[2], stdout);          \
439     printf("\n\tcalculated = "); type_hex.dump(&b, stdout);             \
440     putchar('\n');                                                      \
441   }                                                                     \
442                                                                         \
443   /* --- Test decryption --- */                                         \
444                                                                         \
445   BLKC_MOVE(PRE, d, c);                                                 \
446   pre##_dblk(&k, d, d);                                                 \
447   BLKC_STORE(PRE, b.buf, d);                                            \
448   if (memcmp(b.buf, v[1].buf, PRE##_BLKSZ)) {                           \
449     ok = 0;                                                             \
450     printf("\nfail decryption:"                                         \
451            "\n\tkey        = ");                                        \
452     type_hex.dump(&v[0], stdout);                                       \
453     printf("\n\tciphertext = "); type_hex.dump(&v[2], stdout);          \
454     printf("\n\texpected   = "); type_hex.dump(&v[1], stdout);          \
455     printf("\n\tcalculated = "); type_hex.dump(&b, stdout);             \
456     putchar('\n');                                                      \
457   }                                                                     \
458                                                                         \
459   /* --- Return --- */                                                  \
460                                                                         \
461   return (ok);                                                          \
462 }
463
464 #define BLKC_TESTDEFS(PRE, pre) BLKC_TESTDEFSX(PRE, pre, #pre)
465
466 #define BLKC_TESTDEFSX(PRE, pre, name)                                  \
467   { name, pre##_verify, { &type_hex, &type_hex, &type_hex, 0 } },
468
469 #define BLKC_TESTX(PRE, pre, name, fname)                               \
470                                                                         \
471 BLKC_VERIFYX(PRE, pre, name)                                            \
472                                                                         \
473 static const test_chunk defs[] = {                                      \
474   BLKC_TESTDEFSX(PRE, pre, name)                                        \
475   { 0, 0, { 0 } }                                                       \
476 };                                                                      \
477                                                                         \
478 int main(int argc, char *argv[])                                        \
479 {                                                                       \
480   test_run(argc, argv, defs, SRCDIR"/t/" fname);                        \
481   return (0);                                                           \
482 }
483
484 #else
485 #  define BLKC_TESTX(PRE, pre, name, fname)
486 #endif
487
488 #define BLKC_TEST(PRE, pre) BLKC_TESTX(PRE, pre, #pre, #pre)
489
490 /*----- That's all, folks -------------------------------------------------*/
491
492 #ifdef __cplusplus
493   }
494 #endif
495
496 #endif