math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / symm / ecb-def.h
1 /* -*-c-*-
2 *
3 * Definitions electronic code book mode
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_ECB_DEF_H
29 #define CATACOMB_ECB_DEF_H
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <assert.h>
38 #include <string.h>
39
40 #include <mLib/bits.h>
41 #include <mLib/sub.h>
42
43 #ifndef CATACOMB_ARENA_H
44 # include "arena.h"
45 #endif
46
47 #ifndef CATACOMB_BLKC_H
48 # include "blkc.h"
49 #endif
50
51 #ifndef CATACOMB_GCIPHER_H
52 # include "gcipher.h"
53 #endif
54
55 #ifndef CATACOMB_PARANOIA_H
56 # include "paranoia.h"
57 #endif
58
59 /*----- Macros ------------------------------------------------------------*/
60
61 /* --- @ECB_DEF@ --- *
62 *
63 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
64 *
65 * Use: Creates an implementation for ECB stealing mode.
66 */
67
68 #define ECB_DEF(PRE, pre) \
69 \
70 /* --- @pre_ecbsetkey@ --- * \
71 * \
72 * Arguments: @pre_ecbctx *ctx@ = pointer to ECB context block \
73 * @const pre_ctx *k@ = pointer to cipher context \
74 * \
75 * Returns: --- \
76 * \
77 * Use: Sets the ECB context to use a different cipher key. \
78 */ \
79 \
80 void pre##_ecbsetkey(pre##_ecbctx *ctx, const pre##_ctx *k) \
81 { \
82 ctx->ctx = *k; \
83 } \
84 \
85 /* --- @pre_ecbinit@ --- * \
86 * \
87 * Arguments: @pre_ecbctx *ctx@ = pointer to cipher context \
88 * @const void *key@ = pointer to the key buffer \
89 * @size_t sz@ = size of the key \
90 * @const void *iv@ = pointer to initialization vector \
91 * \
92 * Returns: --- \
93 * \
94 * Use: Initializes an ECB context ready for use. This is \
95 * equivalent to calls to @pre_init@ and @pre_setkey@. \
96 */ \
97 \
98 void pre##_ecbinit(pre##_ecbctx *ctx, \
99 const void *key, size_t sz, \
100 const void *iv) \
101 { \
102 pre##_init(&ctx->ctx, key, sz); \
103 } \
104 \
105 /* --- @pre_ecbencrypt@ --- * \
106 * \
107 * Arguments: @pre_ecbctx *ctx@ = pointer to ECB context block \
108 * @const void *src@ = pointer to source data \
109 * @void *dest@ = pointer to destination data \
110 * @size_t sz@ = size of block to be encrypted \
111 * \
112 * Returns: --- \
113 * \
114 * Use: Encrypts a block with a block cipher in ECB mode, with \
115 * ciphertext stealing and other clever tricks. \
116 * Essentially, data can be encrypted in arbitrary sized \
117 * chunks, although decryption must use the same chunks. \
118 */ \
119 \
120 void pre##_ecbencrypt(pre##_ecbctx *ctx, \
121 const void *src, void *dest, \
122 size_t sz) \
123 { \
124 const octet *s = src; \
125 octet *d = dest; \
126 \
127 /* --- Empty blocks are trivial --- */ \
128 \
129 if (!sz) \
130 return; \
131 \
132 /* --- Short blocks aren't allowed in ECB --- * \
133 * \
134 * There's absolutely nothing secure I can do with them. \
135 */ \
136 \
137 assert(((void)"ECB must have at least one whole block to work with", \
138 sz >= PRE##_BLKSZ)); \
139 \
140 /* --- Do the main chunk of encryption --- * \
141 * \
142 * This will do the whole lot if it's a whole number of blocks. Just \
143 * give each block to the cipher in turn. This is trivial. \
144 * Hopefully... \
145 */ \
146 \
147 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
148 uint32 x[PRE##_BLKSZ / 4]; \
149 if (!s) \
150 BLKC_ZERO(PRE, x); \
151 else { \
152 BLKC_LOAD(PRE, x, s); \
153 s += PRE##_BLKSZ; \
154 } \
155 pre##_eblk(&ctx->ctx, x, x); \
156 if (d) { \
157 BLKC_STORE(PRE, d, x); \
158 d += PRE##_BLKSZ; \
159 } \
160 sz -= PRE##_BLKSZ; \
161 } \
162 \
163 /* --- Do the tail-end block and bit-left-over --- * \
164 * \
165 * This isn't very efficient. That shouldn't matter much. \
166 */ \
167 \
168 if (sz) { \
169 uint32 x[PRE##_BLKSZ / 4]; \
170 octet b[PRE##_BLKSZ]; \
171 unsigned i; \
172 \
173 /* --- Let @sz@ be the size of the partial block --- */ \
174 \
175 sz -= PRE##_BLKSZ; \
176 \
177 /* --- First stage --- * \
178 * \
179 * Read in the current block, and encrypt it. The first part of \
180 * the result is the partial ciphertext block. Don't write that \
181 * out yet, because I've not read the partial plaintext block. \
182 */ \
183 \
184 if (!s) \
185 BLKC_ZERO(PRE, x); \
186 else { \
187 BLKC_LOAD(PRE, x, s); \
188 s += PRE##_BLKSZ; \
189 } \
190 pre##_eblk(&ctx->ctx, x, x); \
191 BLKC_STORE(PRE, b, x); \
192 \
193 /* --- Second stage --- * \
194 * \
195 * Now move in the partial plaintext block, writing out the \
196 * ciphertext as I go. Then encrypt, and write the complete \
197 * ciphertext block. \
198 */ \
199 \
200 if (d) d += PRE##_BLKSZ; \
201 for (i = 0; i < sz; i++) { \
202 register octet y = b[i]; \
203 b[i] = s[i]; \
204 if (d) d[i] = y; \
205 } \
206 BLKC_LOAD(PRE, x, b); \
207 pre##_eblk(&ctx->ctx, x, x); \
208 if (d) BLKC_STORE(PRE, d - PRE##_BLKSZ, x); \
209 } \
210 \
211 /* --- Done --- */ \
212 \
213 return; \
214 } \
215 \
216 /* --- @pre_ecbdecrypt@ --- * \
217 * \
218 * Arguments: @pre_ecbctx *ctx@ = pointer to ECB context block \
219 * @const void *src@ = pointer to source data \
220 * @void *dest@ = pointer to destination data \
221 * @size_t sz@ = size of block to be encrypted \
222 * \
223 * Returns: --- \
224 * \
225 * Use: Decrypts a block with a block cipher in ECB mode, with \
226 * ciphertext stealing and other clever tricks. \
227 * Essentially, data can be encrypted in arbitrary sized \
228 * chunks, although decryption must use the same chunks. \
229 */ \
230 \
231 void pre##_ecbdecrypt(pre##_ecbctx *ctx, \
232 const void *src, void *dest, \
233 size_t sz) \
234 { \
235 const octet *s = src; \
236 octet *d = dest; \
237 \
238 /* --- Empty blocks are trivial --- */ \
239 \
240 if (!sz) \
241 return; \
242 \
243 /* --- Short blocks aren't allowed in ECB --- * \
244 * \
245 * There's absolutely nothing secure I can do with them. \
246 */ \
247 \
248 assert(((void)"ECB must have at least one whole block to work with", \
249 sz >= PRE##_BLKSZ)); \
250 \
251 /* --- Do the main chunk of decryption --- * \
252 * \
253 * This will do the whole lot if it's a whole number of blocks. \
254 * Each block is just handed to the block cipher in turn. \
255 */ \
256 \
257 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
258 uint32 x[PRE##_BLKSZ / 4]; \
259 BLKC_LOAD(PRE, x, s); \
260 pre##_dblk(&ctx->ctx, x, x); \
261 BLKC_STORE(PRE, d, x); \
262 s += PRE##_BLKSZ; \
263 d += PRE##_BLKSZ; \
264 sz -= PRE##_BLKSZ; \
265 } \
266 \
267 /* --- Do the tail-end block and bit-left-over --- * \
268 * \
269 * This isn't very efficient. That shouldn't matter much. \
270 */ \
271 \
272 if (sz) { \
273 uint32 x[PRE##_BLKSZ / 4]; \
274 octet b[PRE##_BLKSZ]; \
275 unsigned i; \
276 \
277 /* --- Let @sz@ be the size of the partial block --- */ \
278 \
279 sz -= PRE##_BLKSZ; \
280 \
281 /* --- First stage --- * \
282 * \
283 * Take the complete ciphertext block, and decrypt it. This block \
284 * is carried over for the next encryption operation. \
285 */ \
286 \
287 BLKC_LOAD(PRE, x, s); \
288 pre##_dblk(&ctx->ctx, x, x); \
289 BLKC_STORE(PRE, b, x); \
290 \
291 /* --- Second stage --- * \
292 * \
293 * The first few bytes are the partial plaintext block. Write that \
294 * and replace with the partial ciphertext block. Then decrypt \
295 * what's left as the complete plaintext. \
296 */ \
297 \
298 s += PRE##_BLKSZ; \
299 d += PRE##_BLKSZ; \
300 for (i = 0; i < sz; i++) { \
301 register octet y = s[i]; \
302 d[i] = b[i]; \
303 b[i] = y; \
304 } \
305 BLKC_LOAD(PRE, x, b); \
306 pre##_dblk(&ctx->ctx, x, x); \
307 BLKC_STORE(PRE, d - PRE##_BLKSZ, x); \
308 } \
309 \
310 /* --- Done --- */ \
311 \
312 return; \
313 } \
314 \
315 /* --- Generic cipher interface --- */ \
316 \
317 static const gcipher_ops gops; \
318 \
319 typedef struct gctx { \
320 gcipher c; \
321 pre##_ecbctx k; \
322 } gctx; \
323 \
324 static gcipher *ginit(const void *k, size_t sz) \
325 { \
326 gctx *g = S_CREATE(gctx); \
327 g->c.ops = &gops; \
328 pre##_ecbinit(&g->k, k, sz, 0); \
329 return (&g->c); \
330 } \
331 \
332 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
333 { \
334 gctx *g = (gctx *)c; \
335 pre##_ecbencrypt(&g->k, s, t, sz); \
336 } \
337 \
338 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
339 { \
340 gctx *g = (gctx *)c; \
341 pre##_ecbdecrypt(&g->k, s, t, sz); \
342 } \
343 \
344 static void gdestroy(gcipher *c) \
345 { \
346 gctx *g = (gctx *)c; \
347 BURN(*g); \
348 S_DESTROY(g); \
349 } \
350 \
351 static const gcipher_ops gops = { \
352 &pre##_ecb, \
353 gencrypt, gdecrypt, gdestroy, 0, 0 \
354 }; \
355 \
356 const gccipher pre##_ecb = { \
357 #pre "-ecb", pre##_keysz, PRE##_BLKSZ, \
358 ginit \
359 }; \
360 \
361 ECB_TEST(PRE, pre)
362
363 /*----- Test rig ----------------------------------------------------------*/
364
365 #ifdef TEST_RIG
366
367 #include <stdio.h>
368
369 #include "daftstory.h"
370
371 /* --- @ECB_TEST@ --- *
372 *
373 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
374 *
375 * Use: Standard test rig for ECB functions.
376 */
377
378 #define ECB_TEST(PRE, pre) \
379 \
380 /* --- Initial plaintext for the test --- */ \
381 \
382 static const octet text[] = TEXT; \
383 \
384 /* --- Key and IV to use --- */ \
385 \
386 static const octet key[] = KEY; \
387 static const octet iv[] = IV; \
388 \
389 /* --- Buffers for encryption and decryption output --- */ \
390 \
391 static octet ct[sizeof(text)]; \
392 static octet pt[sizeof(text)]; \
393 \
394 static void hexdump(const octet *p, size_t sz) \
395 { \
396 const octet *q = p + sz; \
397 for (sz = 0; p < q; p++, sz++) { \
398 printf("%02x", *p); \
399 if ((sz + 1) % PRE##_BLKSZ == 0) \
400 putchar(':'); \
401 } \
402 } \
403 \
404 int main(void) \
405 { \
406 size_t sz = 0, rest; \
407 pre##_ecbctx ctx; \
408 int status = 0; \
409 int done = 0; \
410 \
411 size_t keysz = PRE##_KEYSZ ? \
412 PRE##_KEYSZ : strlen((const char *)key); \
413 \
414 fputs(#pre "-ecb: ", stdout); \
415 \
416 pre##_ecbinit(&ctx, key, keysz, iv); \
417 \
418 while (sz <= sizeof(text)) { \
419 rest = sizeof(text) - sz; \
420 if ((sz != 0 && sz < PRE##_BLKSZ) || \
421 (rest != 0 && rest < PRE##_BLKSZ)) \
422 goto next; \
423 memcpy(ct, text, sizeof(text)); \
424 pre##_ecbencrypt(&ctx, ct, ct, sz); \
425 pre##_ecbencrypt(&ctx, ct + sz, ct + sz, rest); \
426 memcpy(pt, ct, sizeof(text)); \
427 pre##_ecbdecrypt(&ctx, pt, pt, sz); \
428 pre##_ecbdecrypt(&ctx, pt + sz, pt + sz, rest); \
429 if (memcmp(pt, text, sizeof(text)) == 0) { \
430 done++; \
431 if (sizeof(text) < 40 || done % 8 == 0) \
432 fputc('.', stdout); \
433 if (done % 480 == 0) \
434 fputs("\n\t", stdout); \
435 fflush(stdout); \
436 } else { \
437 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
438 status = 1; \
439 printf("\tplaintext = "); hexdump(text, sz); \
440 printf(", "); hexdump(text + sz, rest); \
441 fputc('\n', stdout); \
442 printf("\tciphertext = "); hexdump(ct, sz); \
443 printf(", "); hexdump(ct + sz, rest); \
444 fputc('\n', stdout); \
445 printf("\trecovered text = "); hexdump(pt, sz); \
446 printf(", "); hexdump(pt + sz, rest); \
447 fputc('\n', stdout); \
448 fputc('\n', stdout); \
449 } \
450 next: \
451 if (sz < 63) \
452 sz++; \
453 else \
454 sz += 9; \
455 } \
456 \
457 fputs(status ? " failed\n" : " ok\n", stdout); \
458 return (status); \
459 }
460
461 #else
462 # define ECB_TEST(PRE, pre)
463 #endif
464
465 /*----- That's all, folks -------------------------------------------------*/
466
467 #ifdef __cplusplus
468 }
469 #endif
470
471 #endif