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