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