Table for driving key data extraction.
[u/mdw/catacomb] / blkc.h
CommitLineData
d03ab969 1/* -*-c-*-
2 *
b3f05084 3 * $Id: blkc.h,v 1.2 1999/12/10 23:29:48 mdw Exp $
d03ab969 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 $
b3f05084 33 * Revision 1.2 1999/12/10 23:29:48 mdw
34 * Change header file guard names.
35 *
d03ab969 36 * Revision 1.1 1999/09/03 08:41:11 mdw
37 * Initial import.
38 *
39 */
40
b3f05084 41#ifndef CATACOMB_BLKC_H
42#define CATACOMB_BLKC_H
d03ab969 43
44#ifdef __cplusplus
45 extern "C" {
46#endif
47
48/*----- Header files ------------------------------------------------------*/
49
50#include <mLib/bits.h>
51
52/*----- Theory of operation -----------------------------------------------*
53 *
54 * A block cipher has associated with it a triple, called PRE_CLASS, of the
55 * form `(TYPE, ENDIAN, BITS)', where TYPE is either `N' (representing an
56 * implemented bit size) or `X' (representing an unimplemented bit size,
57 * causing loops to be compiled rather than unrolled code), ENDIAN is `B'
58 * (big) or `L' (little), and BITS is the block size of the cipher in bits.
59 */
60
61/*----- Data movement macros ----------------------------------------------*/
62
63/*
64 * `The C preprocessor. You will never find a more wretched hive of bogus
65 * hackery. We must be cautious.'
66 */
67
68/* --- General dispatch macros --- */
69
70#define BLKC_DOGLUE(x, y) x ## y
71#define BLKC_GLUE(x, y) BLKC_DOGLUE(x, y)
72#define BLKC_APPLY(f, x) f x
73#define BLKC_FIRST(x, y, z) x
74#define BLKC_SECOND(x, y, z) y
75#define BLKC_THIRD(x, y, z) z
b3f05084 76#define BLKC_TYPE(PRE) BLKC_APPLY(BLKC_FIRST, PRE##_CLASS)
77#define BLKC_ENDIAN(PRE) BLKC_APPLY(BLKC_SECOND, PRE##_CLASS)
78#define BLKC_BITS(PRE) BLKC_APPLY(BLKC_THIRD, PRE##_CLASS)
d03ab969 79
80#define BLKC_STORE_E(PRE) BLKC_GLUE(STORE32_, BLKC_ENDIAN(PRE))
81#define BLKC_LOAD_E(PRE) BLKC_GLUE(LOAD32_, BLKC_ENDIAN(PRE))
82
83/* --- Interface macros --- */
84
85#define BLKC_STORE(PRE, b, w) \
86 BLKC_GLUE(BLKC_STORE_, BLKC_TYPE(PRE)) \
87 (PRE, b, w, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
88
89#define BLKC_XSTORE(PRE, b, w, wx) \
90 BLKC_GLUE(BLKC_XSTORE_, BLKC_TYPE(PRE)) \
91 (PRE, b, w, wx, BLKC_STORE_E(PRE), BLKC_BITS(PRE))
92
93#define BLKC_LOAD(PRE, w, b) \
94 BLKC_GLUE(BLKC_LOAD_, BLKC_TYPE(PRE)) \
95 (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
96
97#define BLKC_XLOAD(PRE, w, b) \
98 BLKC_GLUE(BLKC_XLOAD_, BLKC_TYPE(PRE)) \
99 (PRE, w, b, BLKC_LOAD_E(PRE), BLKC_BITS(PRE))
100
101#define BLKC_MOVE(PRE, w, wx) \
102 BLKC_GLUE(BLKC_MOVE_, BLKC_TYPE(PRE)) \
103 (PRE, w, wx, BLKC_BITS(PRE))
104
105#define BLKC_XMOVE(PRE, w, wx) \
106 BLKC_GLUE(BLKC_XMOVE_, BLKC_TYPE(PRE)) \
107 (PRE, w, wx, BLKC_BITS(PRE))
108
109/* --- General implementation skeleton --- */
110
111#define BLKC_SKEL(PRE, decl, guts) do { \
112 decl \
113 guts \
114} while (0)
115
116#define BLKC_P(p) register octet *_p = (octet *)(p)
117#define BLKC_W(w) register uint32 *_w = (w)
118#define BLKC_WX(wx) register uint32 *_wx = (wx);
119
120/* --- Implementation for unusual block sizes --- */
121
122#define BLKC_SKEL_X(PRE, decl, guts) \
123 BLKC_SKEL(PRE, int _i; decl, \
b3f05084 124 for (_i = 0; _i < PRE##_BLKSZ / 4; _i++) { \
d03ab969 125 guts \
126 })
127
128#define BLKC_STORE_X(PRE, b, w, op, n) \
129 BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w);, \
130 op(_p, *_w); _p += 4; _w++; )
131
132#define BLKC_XSTORE_X(PRE, b, w, wx, op, n) \
133 BLKC_SKEL_X(PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);, \
134 op(_p, *_w ^ *_wx); _p += 4; _w++; _wx++; )
135
136#define BLKC_LOAD_X(PRE, w, b, op, n) \
137 BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);, \
138 *_w = op(_p); _p += 4; _w++; )
139
140#define BLKC_XLOAD_X(PRE, w, b, op, n) \
141 BLKC_SKEL_X(PRE, const BLKC_P(b); BLKC_W(w);, \
142 *_w ^= op(_p); _p += 4; _w++; )
143
144#define BLKC_MOVE_X(PRE, w, wx, n) \
145 BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);, \
146 *_w = *_wx; _w++; _wx++; ) \
147
148#define BLKC_XMOVE_X(PRE, w, wx, n) \
149 BLKC_SKEL_X(PRE, BLKC_W(w); const BLKC_WX(wx);, \
150 *_w ^= *_wx; _w++; _wx++; ) \
151
152/* --- Implementation for known block sizes --- */
153
154#define BLKC_SKEL_64(PRE, decl, op, guts) \
155 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1);)
156
157#define BLKC_SKEL_128(PRE, decl, op, guts) \
158 BLKC_SKEL(PRE, decl, guts(op, 0); guts(op, 1); guts(op, 2); guts(op, 3);)
159
160#define BLKC_STORE_GUTS(op, i) op(_p + 4 * i, _w[i])
161#define BLKC_XSTORE_GUTS(op, i) op(_p + 4 * i, _w[i] ^ _wx[i])
162#define BLKC_LOAD_GUTS(op, i) _w[i] = op(_p + 4 * i)
163#define BLKC_XLOAD_GUTS(op, i) _w[i] ^= op(_p + 4 * i)
164#define BLKC_MOVE_GUTS(op, i) _w[i] = _wx[i]
165#define BLKC_XMOVE_GUTS(op, i) _w[i] ^= _wx[i]
166
167#define BLKC_STORE_N(PRE, b, w, op, n) \
168 BLKC_GLUE(BLKC_SKEL_, n) \
169 (PRE, BLKC_P(b); const BLKC_W(w);, op, BLKC_STORE_GUTS)
170
171#define BLKC_XSTORE_N(PRE, b, w, wx, op, n) \
172 BLKC_GLUE(BLKC_SKEL_, n) \
173 (PRE, BLKC_P(b); const BLKC_W(w); const BLKC_WX(wx);, \
174 op, BLKC_XSTORE_GUTS)
175
176#define BLKC_LOAD_N(PRE, w, b, op, n) \
177 BLKC_GLUE(BLKC_SKEL_, n) \
178 (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_LOAD_GUTS)
179
180#define BLKC_XLOAD_N(PRE, w, b, op, n) \
181 BLKC_GLUE(BLKC_SKEL_, n) \
182 (PRE, const BLKC_P(b); BLKC_W(w);, op, BLKC_XLOAD_GUTS)
183
184#define BLKC_MOVE_N(PRE, w, wx, n) \
185 BLKC_GLUE(BLKC_SKEL_, n) \
186 (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_MOVE_GUTS)
187
188#define BLKC_XMOVE_N(PRE, w, wx, n) \
189 BLKC_GLUE(BLKC_SKEL_, n) \
190 (PRE, BLKC_W(w); const BLKC_WX(wx);, op, BLKC_XMOVE_GUTS)
191
192/*----- Test rig for block ciphers ----------------------------------------*/
193
194/* --- @BLKC_TEST@ --- *
195 *
196 * Arguments: @PRE@, @pre@ = prefixes for cipher-specific definitions
197 *
198 * Use: Standard test rig for block ciphers.
199 */
200
201#ifdef TEST_RIG
202
203#include <mLib/quis.h>
204#include <mLib/testrig.h>
205
206#define BLKC_TEST(PRE, pre) \
207 \
208static int verify(dstr *v) \
209{ \
b3f05084 210 pre##_ctx k; \
211 uint32 p[PRE##_BLKSZ / 4]; \
212 uint32 c[PRE##_BLKSZ / 4]; \
213 uint32 d[PRE##_BLKSZ / 4]; \
d03ab969 214 dstr b = DSTR_INIT; \
215 int ok = 1; \
216 \
217 /* --- Initialize the key buffer --- */ \
218 \
b3f05084 219 dstr_ensure(&b, PRE##_BLKSZ); \
220 b.len = PRE##_BLKSZ; \
221 pre##_init(&k, v[0].buf, v[0].len); \
d03ab969 222 BLKC_LOAD(PRE, p, v[1].buf); \
223 BLKC_LOAD(PRE, c, v[2].buf); \
224 \
225 /* --- Test encryption --- */ \
226 \
227 BLKC_MOVE(PRE, d, p); \
b3f05084 228 pre##_eblk(&k, d, d); \
d03ab969 229 BLKC_STORE(PRE, b.buf, d); \
b3f05084 230 if (memcmp(b.buf, v[2].buf, PRE##_BLKSZ)) { \
d03ab969 231 ok = 0; \
232 printf("\nfail encryption:" \
233 "\n\tkey = "); \
234 type_hex.dump(&v[0], stdout); \
235 printf("\n\tplaintext = "); type_hex.dump(&v[1], stdout); \
236 printf("\n\texpected = "); type_hex.dump(&v[2], stdout); \
237 printf("\n\tcalculated = "); type_hex.dump(&b, stdout); \
238 putchar('\n'); \
239 } \
240 \
241 /* --- Test decryption --- */ \
242 \
243 BLKC_MOVE(PRE, d, c); \
b3f05084 244 pre##_dblk(&k, d, d); \
d03ab969 245 BLKC_STORE(PRE, b.buf, d); \
b3f05084 246 if (memcmp(b.buf, v[1].buf, PRE##_BLKSZ)) { \
d03ab969 247 ok = 0; \
248 printf("\nfail decryption:" \
249 "\n\tkey = "); \
250 type_hex.dump(&v[0], stdout); \
251 printf("\n\tciphertext = "); type_hex.dump(&v[2], stdout); \
252 printf("\n\texpected = "); type_hex.dump(&v[1], stdout); \
253 printf("\n\tcalculated = "); type_hex.dump(&b, stdout); \
254 putchar('\n'); \
255 } \
256 \
257 /* --- Return --- */ \
258 \
259 return (ok); \
260} \
261 \
262static test_chunk defs[] = { \
263 { #pre, verify, { &type_hex, &type_hex, &type_hex, 0 } }, \
264 { #pre "-sched", verify, { &type_hex, &type_hex, &type_hex, 0 } }, \
265 { 0, 0, { 0 } } \
266}; \
267 \
268int main(int argc, char *argv[]) \
269{ \
270 test_run(argc, argv, defs, SRCDIR"/tests/" #pre); \
271 return (0); \
272}
273
274#else
275# define BLKC_TEST(PRE, pre)
276#endif
277
278/*----- That's all, folks -------------------------------------------------*/
279
280#ifdef __cplusplus
281 }
282#endif
283
284#endif