Commit | Line | Data |
---|---|---|
79ba130c | 1 | /* -*-c-*- |
79ba130c | 2 | * |
3 | * Definitions electronic code book mode | |
4 | * | |
5 | * (c) 1999 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
79ba130c | 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 | * |
79ba130c | 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 | * |
79ba130c | 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 | ||
79ba130c | 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 | ||
99461409 | 43 | #ifndef CATACOMB_ARENA_H |
44 | # include "arena.h" | |
45 | #endif | |
46 | ||
79ba130c | 47 | #ifndef CATACOMB_BLKC_H |
48 | # include "blkc.h" | |
49 | #endif | |
50 | ||
51 | #ifndef CATACOMB_GCIPHER_H | |
52 | # include "gcipher.h" | |
53 | #endif | |
54 | ||
99461409 | 55 | #ifndef CATACOMB_PARANOIA_H |
56 | # include "paranoia.h" | |
57 | #endif | |
58 | ||
79ba130c | 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]; \ | |
5c3f75ec | 149 | if (!s) \ |
150 | BLKC_ZERO(PRE, x); \ | |
151 | else { \ | |
152 | BLKC_LOAD(PRE, x, s); \ | |
153 | s += PRE##_BLKSZ; \ | |
154 | } \ | |
79ba130c | 155 | pre##_eblk(&ctx->ctx, x, x); \ |
5c3f75ec | 156 | if (d) { \ |
157 | BLKC_STORE(PRE, d, x); \ | |
158 | d += PRE##_BLKSZ; \ | |
159 | } \ | |
79ba130c | 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 | \ | |
5c3f75ec | 184 | if (!s) \ |
185 | BLKC_ZERO(PRE, x); \ | |
186 | else { \ | |
187 | BLKC_LOAD(PRE, x, s); \ | |
188 | s += PRE##_BLKSZ; \ | |
189 | } \ | |
79ba130c | 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 | \ | |
5c3f75ec | 200 | if (d) d += PRE##_BLKSZ; \ |
79ba130c | 201 | for (i = 0; i < sz; i++) { \ |
202 | register octet y = b[i]; \ | |
203 | b[i] = s[i]; \ | |
5c3f75ec | 204 | if (d) d[i] = y; \ |
79ba130c | 205 | } \ |
206 | BLKC_LOAD(PRE, x, b); \ | |
207 | pre##_eblk(&ctx->ctx, x, x); \ | |
5c3f75ec | 208 | if (d) BLKC_STORE(PRE, d - PRE##_BLKSZ, x); \ |
79ba130c | 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 | { \ | |
99461409 | 326 | gctx *g = S_CREATE(gctx); \ |
79ba130c | 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; \ | |
99461409 | 347 | BURN(*g); \ |
348 | S_DESTROY(g); \ | |
79ba130c | 349 | } \ |
350 | \ | |
351 | static const gcipher_ops gops = { \ | |
99461409 | 352 | &pre##_ecb, \ |
353 | gencrypt, gdecrypt, gdestroy, 0, 0 \ | |
79ba130c | 354 | }; \ |
355 | \ | |
356 | const gccipher pre##_ecb = { \ | |
99461409 | 357 | #pre "-ecb", pre##_keysz, PRE##_BLKSZ, \ |
79ba130c | 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 | \ | |
12331b2d | 394 | static void hexdump(const octet *p, size_t sz, size_t off) \ |
79ba130c | 395 | { \ |
396 | const octet *q = p + sz; \ | |
397 | for (sz = 0; p < q; p++, sz++) { \ | |
398 | printf("%02x", *p); \ | |
12331b2d | 399 | if ((off + sz + 1) % PRE##_BLKSZ == 0) \ |
79ba130c | 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) \ | |
45c0fd36 | 432 | fputc('.', stdout); \ |
79ba130c | 433 | if (done % 480 == 0) \ |
45c0fd36 | 434 | fputs("\n\t", stdout); \ |
79ba130c | 435 | fflush(stdout); \ |
436 | } else { \ | |
437 | printf("\nError (sz = %lu)\n", (unsigned long)sz); \ | |
438 | status = 1; \ | |
12331b2d MW |
439 | printf("\tplaintext = "); hexdump(text, sz, 0); \ |
440 | printf(", "); hexdump(text + sz, rest, sz); \ | |
45c0fd36 | 441 | fputc('\n', stdout); \ |
12331b2d MW |
442 | printf("\tciphertext = "); hexdump(ct, sz, 0); \ |
443 | printf(", "); hexdump(ct + sz, rest, sz); \ | |
45c0fd36 | 444 | fputc('\n', stdout); \ |
12331b2d MW |
445 | printf("\trecovered text = "); hexdump(pt, sz, 0); \ |
446 | printf(", "); hexdump(pt + sz, rest, sz); \ | |
45c0fd36 | 447 | fputc('\n', stdout); \ |
79ba130c | 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 |