progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / symm / blkc.h
CommitLineData
d03ab969 1/* -*-c-*-
2 *
d03ab969 3 * Common definitions for block ciphers
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
d03ab969 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.
45c0fd36 16 *
d03ab969 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.
45c0fd36 21 *
d03ab969 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
b3f05084 28#ifndef CATACOMB_BLKC_H
29#define CATACOMB_BLKC_H
d03ab969 30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
35/*----- Header files ------------------------------------------------------*/
36
c5885da8 37#include <assert.h>
38
d03ab969 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
b3f05084 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)
d03ab969 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
6dc589ef
MW
72#define BLKC_ID(x) (x)
73
d03ab969 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
6dc589ef
MW
100#define BLKC_BSTEP(PRE, w) BLKC_BADD(PRE, w, 1)
101#define BLKC_LSTEP(PRE, w) BLKC_LADD(PRE, w, 1)
8ff32af3
MW
102#define BLKC_STEP(PRE, w) BLKC_ADD(PRE, w, 1)
103
6dc589ef
MW
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)
8ff32af3
MW
110#define BLKC_ADD(PRE, w, n) \
111 BLKC_GLUE(BLKC_ADD_X_, BLKC_ENDIAN(PRE)) \
6dc589ef 112 (PRE, BLKC_ID, w, n)
c5885da8 113
5c3f75ec 114#define BLKC_ZERO(PRE, w) \
115 BLKC_GLUE(BLKC_ZERO_, BLKC_TYPE(PRE)) \
116 (PRE, w, BLKC_BITS(PRE))
117
6dc589ef
MW
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)
c5885da8 124#define BLKC_SET(PRE, w, x) \
125 BLKC_GLUE(BLKC_SET_X_, BLKC_ENDIAN(PRE)) \
6dc589ef
MW
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)
c5885da8 130
131#define BLKC_SHOW(PRE, tag, w) do { \
132 fputs(tag ": ", stdout); \
6dc589ef
MW
133 BLKC_SKEL_X(PRE, const BLKC_W(w);, \
134 { printf("%08x ", BLKC_BWORD(PRE, *_w)); _w++; }); \
c5885da8 135 fputc('\n', stdout); \
136} while (0)
137
8ff32af3
MW
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
6dc589ef
MW
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; \
8ff32af3
MW
148 } while (0)
149#else
6dc589ef
MW
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); \
8ff32af3
MW
153 } while (0)
154#endif
155
d03ab969 156/* --- General implementation skeleton --- */
157
158#define BLKC_SKEL(PRE, decl, guts) do { \
159 decl \
160 guts \
161} while (0)
162
0fee61eb
MW
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)
d03ab969 166
167/* --- Implementation for unusual block sizes --- */
168
169#define BLKC_SKEL_X(PRE, decl, guts) \
c5885da8 170 BLKC_SKEL(PRE, unsigned _i; decl, \
0fee61eb 171 for (_i = 0; _i < PRE##_BLKSZ/4; _i++) { \
d03ab969 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
5c3f75ec 199#define BLKC_ZERO_X(PRE, w, n) \
200 BLKC_SKEL_X(PRE, BLKC_W(w);, *_w++ = 0;)
201
6dc589ef
MW
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 { \
8ff32af3 208 unsigned _i = PRE##_BLKSZ/4; BLKC_W(w); uint32 _n = (n); \
6dc589ef 209 while (_i-- && _n) BLKC_ADDC32(op, _w[_i], _n, _w[_i], _n); \
c5885da8 210} while (0)
211
6dc589ef 212#define BLKC_ADD_X_L(PRE, op, w, n) do { \
8ff32af3
MW
213 unsigned _i = 0; BLKC_W(w); uint32 _n = (n); \
214 while (_i < PRE##_BLKSZ/4 && _n) \
6dc589ef 215 { BLKC_ADDC32(op, _w[_i], _n, _w[_i], _n); _i++; } \
c5885da8 216} while (0)
217
6dc589ef
MW
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; \
0fee61eb 225 for (_i = 0; _i < PRE##_BLKSZ/4; _i++) { \
6dc589ef 226 *--_w = U32(op(_x)); \
e91d142c 227 _x = ((_x & ~(unsigned long)MASK32) >> 16) >> 16; \
c5885da8 228 } \
229} while (0)
230
6dc589ef
MW
231#define BLKC_SET_X_L(PRE, op, w, x) do { \
232 unsigned _i; BLKC_W(w); unsigned long _x = x; \
0fee61eb 233 for (_i = 0; _i < PRE##_BLKSZ/4; _i++) { \
6dc589ef 234 *_w++ = U32(op(_x)); \
e91d142c 235 _x = ((_x & ~(unsigned long)MASK32) >> 16) >> 16; \
c5885da8 236 } \
237} while (0)
238
6dc589ef
MW
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
d03ab969 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
c5885da8 249#define BLKC_SKEL_96(PRE, decl, op, guts) \
250 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2);)
251
d03ab969 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
70f31709 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
d03ab969 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]
5c3f75ec 271#define BLKC_ZERO_GUTS(op, i) _w[i] = 0
d03ab969 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
5c3f75ec 294#define BLKC_ZERO_N(PRE, w, n) \
295 BLKC_GLUE(BLKC_SKEL_, n) \
296 (PRE, BLKC_W(w); , op, BLKC_ZERO_GUTS)
297
d03ab969 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
1655e4a0
MW
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
d03ab969 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
f94b972d 401#include <string.h>
45c0fd36 402
141c1284 403#include <mLib/macros.h>
d03ab969 404#include <mLib/quis.h>
405#include <mLib/testrig.h>
406
c76bf871
MW
407#ifndef BLKC_TESTHOOK
408# define BLKC_TESTHOOK do ; while (0)
409#endif
410
aaae9cab
MW
411#define BLKC_VERIFY(PRE, pre) BLKC_VERIFYX(PRE, pre, #pre)
412
413#define BLKC_VERIFYX(PRE, pre, name) \
d03ab969 414 \
dcdc42e7 415static int pre##_verify(dstr *v) \
d03ab969 416{ \
b3f05084 417 pre##_ctx k; \
0fee61eb
MW
418 uint32 p[PRE##_BLKSZ/4]; \
419 uint32 c[PRE##_BLKSZ/4]; \
420 uint32 d[PRE##_BLKSZ/4]; \
d03ab969 421 dstr b = DSTR_INIT; \
422 int ok = 1; \
423 \
424 /* --- Initialize the key buffer --- */ \
425 \
b3f05084 426 dstr_ensure(&b, PRE##_BLKSZ); \
427 b.len = PRE##_BLKSZ; \
428 pre##_init(&k, v[0].buf, v[0].len); \
d03ab969 429 BLKC_LOAD(PRE, p, v[1].buf); \
430 BLKC_LOAD(PRE, c, v[2].buf); \
431 \
432 /* --- Test encryption --- */ \
433 \
434 BLKC_MOVE(PRE, d, p); \
b3f05084 435 pre##_eblk(&k, d, d); \
d03ab969 436 BLKC_STORE(PRE, b.buf, d); \
141c1284 437 if (MEMCMP(b.buf, !=, v[2].buf, PRE##_BLKSZ)) { \
d03ab969 438 ok = 0; \
439 printf("\nfail encryption:" \
45c0fd36 440 "\n\tkey = "); \
d03ab969 441 type_hex.dump(&v[0], stdout); \
442 printf("\n\tplaintext = "); type_hex.dump(&v[1], stdout); \
443 printf("\n\texpected = "); type_hex.dump(&v[2], stdout); \
444 printf("\n\tcalculated = "); type_hex.dump(&b, stdout); \
445 putchar('\n'); \
446 } \
447 \
448 /* --- Test decryption --- */ \
449 \
450 BLKC_MOVE(PRE, d, c); \
b3f05084 451 pre##_dblk(&k, d, d); \
d03ab969 452 BLKC_STORE(PRE, b.buf, d); \
141c1284 453 if (MEMCMP(b.buf, !=, v[1].buf, PRE##_BLKSZ)) { \
d03ab969 454 ok = 0; \
455 printf("\nfail decryption:" \
45c0fd36 456 "\n\tkey = "); \
d03ab969 457 type_hex.dump(&v[0], stdout); \
458 printf("\n\tciphertext = "); type_hex.dump(&v[2], stdout); \
459 printf("\n\texpected = "); type_hex.dump(&v[1], stdout); \
460 printf("\n\tcalculated = "); type_hex.dump(&b, stdout); \
461 putchar('\n'); \
462 } \
463 \
464 /* --- Return --- */ \
465 \
466 return (ok); \
dcdc42e7 467}
468
4328f746
MW
469#define BLKC_TESTDEFS(PRE, pre) BLKC_TESTDEFSX(PRE, pre, #pre)
470
471#define BLKC_TESTDEFSX(PRE, pre, name) \
472 { name, pre##_verify, { &type_hex, &type_hex, &type_hex, 0 } },
473
aaae9cab 474#define BLKC_TESTX(PRE, pre, name, fname) \
dcdc42e7 475 \
aaae9cab 476BLKC_VERIFYX(PRE, pre, name) \
d03ab969 477 \
4e66da02 478static const test_chunk defs[] = { \
4328f746 479 BLKC_TESTDEFSX(PRE, pre, name) \
d03ab969 480 { 0, 0, { 0 } } \
481}; \
482 \
483int main(int argc, char *argv[]) \
484{ \
c76bf871 485 BLKC_TESTHOOK; \
aaae9cab 486 test_run(argc, argv, defs, SRCDIR"/t/" fname); \
d03ab969 487 return (0); \
488}
489
490#else
aaae9cab 491# define BLKC_TESTX(PRE, pre, name, fname)
d03ab969 492#endif
493
aaae9cab
MW
494#define BLKC_TEST(PRE, pre) BLKC_TESTX(PRE, pre, #pre, #pre)
495
d03ab969 496/*----- That's all, folks -------------------------------------------------*/
497
498#ifdef __cplusplus
499 }
500#endif
501
502#endif