79ba130c |
1 | /* -*-c-*- |
2 | * |
4e66da02 |
3 | * $Id: cbc-def.h,v 1.4 2004/04/02 01:03:49 mdw Exp $ |
79ba130c |
4 | * |
5 | * Definitions for cipher block chaining mode |
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: cbc-def.h,v $ |
4e66da02 |
33 | * Revision 1.4 2004/04/02 01:03:49 mdw |
34 | * Miscellaneous constification. |
35 | * |
4efe32ba |
36 | * Revision 1.3 2001/06/17 00:10:51 mdw |
37 | * Typesetting fixes |
38 | * |
99461409 |
39 | * Revision 1.2 2000/06/17 10:49:52 mdw |
40 | * Use secure arena for memory allocation. |
41 | * |
79ba130c |
42 | * Revision 1.1 1999/12/10 23:16:39 mdw |
43 | * Split mode macros into interface and implementation. |
44 | * |
45 | */ |
46 | |
47 | #ifndef CATACOMB_CBC_DEF_H |
48 | #define CATACOMB_CBC_DEF_H |
49 | |
50 | #ifdef __cplusplus |
51 | extern "C" { |
52 | #endif |
53 | |
54 | /*----- Header files ------------------------------------------------------*/ |
55 | |
56 | #include <string.h> |
57 | |
58 | #include <mLib/bits.h> |
59 | #include <mLib/sub.h> |
60 | |
99461409 |
61 | #ifndef CATACOMB_ARENA_H |
62 | # include "arena.h" |
63 | #endif |
64 | |
79ba130c |
65 | #ifndef CATACOMB_BLKC_H |
66 | # include "blkc.h" |
67 | #endif |
68 | |
69 | #ifndef CATACOMB_GCIPHER_H |
70 | # include "gcipher.h" |
71 | #endif |
72 | |
99461409 |
73 | #ifndef CATACOMB_PARANOIA_H |
74 | # include "paranoia.h" |
75 | #endif |
76 | |
79ba130c |
77 | /*----- Macros ------------------------------------------------------------*/ |
78 | |
79 | /* --- @CBC_DEF@ --- * |
80 | * |
81 | * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher |
82 | * |
83 | * Use: Creates an implementation for CBC stealing mode. |
84 | */ |
85 | |
86 | #define CBC_DEF(PRE, pre) \ |
87 | \ |
88 | /* --- @pre_cbcgetiv@ --- * \ |
89 | * \ |
90 | * Arguments: @const pre_cbcctx *ctx@ = pointer to CBC context block \ |
4efe32ba |
91 | * @void *iv@ = pointer to output data block \ |
79ba130c |
92 | * \ |
93 | * Returns: --- \ |
94 | * \ |
95 | * Use: Reads the currently set IV. Reading and setting an IV \ |
96 | * is transparent to the CBC encryption or decryption \ |
97 | * process. \ |
98 | */ \ |
99 | \ |
100 | void pre##_cbcgetiv(const pre##_cbcctx *ctx, void *iv) \ |
101 | { \ |
102 | BLKC_STORE(PRE, iv, ctx->iv); \ |
103 | } \ |
104 | \ |
105 | /* --- @pre_cbcsetiv@ --- * \ |
106 | * \ |
107 | * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \ |
108 | * @cnost void *iv@ = pointer to IV to set \ |
109 | * \ |
110 | * Returns: --- \ |
111 | * \ |
112 | * Use: Sets the IV to use for subsequent encryption. \ |
113 | */ \ |
114 | \ |
115 | void pre##_cbcsetiv(pre##_cbcctx *ctx, const void *iv) \ |
116 | { \ |
117 | BLKC_LOAD(PRE, ctx->iv, iv); \ |
118 | } \ |
119 | \ |
120 | /* --- @pre_cbcsetkey@ --- * \ |
121 | * \ |
122 | * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \ |
123 | * @const pre_ctx *k@ = pointer to cipher context \ |
124 | * \ |
125 | * Returns: --- \ |
126 | * \ |
127 | * Use: Sets the CBC context to use a different cipher key. \ |
128 | */ \ |
129 | \ |
130 | void pre##_cbcsetkey(pre##_cbcctx *ctx, const pre##_ctx *k) \ |
131 | { \ |
132 | ctx->ctx = *k; \ |
133 | } \ |
134 | \ |
135 | /* --- @pre_cbcinit@ --- * \ |
136 | * \ |
137 | * Arguments: @pre_cbcctx *ctx@ = pointer to cipher context \ |
138 | * @const void *key@ = pointer to the key buffer \ |
139 | * @size_t sz@ = size of the key \ |
140 | * @const void *iv@ = pointer to initialization vector \ |
141 | * \ |
142 | * Returns: --- \ |
143 | * \ |
144 | * Use: Initializes a CBC context ready for use. The @iv@ \ |
145 | * argument may be passed as a null pointer to set a zero \ |
146 | * IV. Apart from that, this call is equivalent to calls \ |
147 | * to @pre_init@, @pre_cbcsetkey@ and @pre_cbcsetiv@. \ |
148 | */ \ |
149 | \ |
150 | void pre##_cbcinit(pre##_cbcctx *ctx, \ |
151 | const void *key, size_t sz, \ |
152 | const void *iv) \ |
153 | { \ |
4e66da02 |
154 | static const octet zero[PRE##_BLKSZ] = { 0 }; \ |
79ba130c |
155 | pre##_init(&ctx->ctx, key, sz); \ |
156 | BLKC_LOAD(PRE, ctx->iv, iv ? iv : zero); \ |
157 | } \ |
158 | \ |
159 | /* --- @pre_cbcencrypt@ --- * \ |
160 | * \ |
161 | * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \ |
162 | * @const void *src@ = pointer to source data \ |
163 | * @void *dest@ = pointer to destination data \ |
164 | * @size_t sz@ = size of block to be encrypted \ |
165 | * \ |
166 | * Returns: --- \ |
167 | * \ |
168 | * Use: Encrypts a block with a block cipher in CBC mode, with \ |
169 | * ciphertext stealing and other clever tricks. \ |
170 | * Essentially, data can be encrypted in arbitrary sized \ |
171 | * chunks, although decryption must use the same chunks. \ |
172 | */ \ |
173 | \ |
174 | void pre##_cbcencrypt(pre##_cbcctx *ctx, \ |
175 | const void *src, void *dest, \ |
176 | size_t sz) \ |
177 | { \ |
178 | const octet *s = src; \ |
179 | octet *d = dest; \ |
180 | \ |
181 | /* --- Empty blocks are trivial --- */ \ |
182 | \ |
183 | if (!sz) \ |
184 | return; \ |
185 | \ |
186 | /* --- Extra magical case for a short block --- * \ |
187 | * \ |
188 | * Encrypt the IV, then exclusive-or the plaintext with the octets \ |
189 | * of the encrypted IV, shifting ciphertext octets in instead. This \ |
190 | * basically switches over to CFB. \ |
191 | */ \ |
192 | \ |
193 | if (sz < PRE##_BLKSZ) { \ |
194 | octet b[PRE##_BLKSZ]; \ |
195 | unsigned i; \ |
196 | \ |
197 | pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \ |
198 | BLKC_STORE(PRE, b, ctx->iv); \ |
199 | for (i = 0; i < sz; i++) \ |
200 | d[i] = b[i] ^ s[i]; \ |
201 | memmove(b, b + sz, PRE##_BLKSZ - sz); \ |
202 | memcpy(b + PRE##_BLKSZ - sz, d, sz); \ |
203 | BLKC_LOAD(PRE, ctx->iv, b); \ |
204 | return; \ |
205 | } \ |
206 | \ |
207 | /* --- Do the main chunk of encryption --- * \ |
208 | * \ |
209 | * This will do the whole lot if it's a whole number of blocks. For \ |
210 | * each block, XOR it with the previous ciphertext in @iv@, encrypt, \ |
211 | * and keep a copy of the ciphertext for the next block. \ |
212 | */ \ |
213 | \ |
214 | while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \ |
215 | BLKC_XLOAD(PRE, ctx->iv, s); \ |
216 | pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \ |
217 | BLKC_STORE(PRE, d, ctx->iv); \ |
218 | s += PRE##_BLKSZ; \ |
219 | d += PRE##_BLKSZ; \ |
220 | sz -= PRE##_BLKSZ; \ |
221 | } \ |
222 | \ |
223 | /* --- Do the tail-end block and bit-left-over --- * \ |
224 | * \ |
225 | * This isn't very efficient. That shouldn't matter much. \ |
226 | */ \ |
227 | \ |
228 | if (sz) { \ |
229 | octet b[PRE##_BLKSZ]; \ |
230 | unsigned i; \ |
231 | \ |
232 | /* --- Let @sz@ be the size of the partial block --- */ \ |
233 | \ |
234 | sz -= PRE##_BLKSZ; \ |
235 | \ |
236 | /* --- First stage --- * \ |
237 | * \ |
238 | * XOR the complete block with the current IV, and encrypt it. The \ |
239 | * first part of the result is the partial ciphertext block. Don't \ |
240 | * write that out yet, because I've not read the partial plaintext \ |
241 | * block. \ |
242 | */ \ |
243 | \ |
244 | BLKC_XLOAD(PRE, ctx->iv, s); \ |
245 | pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \ |
246 | BLKC_STORE(PRE, b, ctx->iv); \ |
247 | \ |
248 | /* --- Second stage --- * \ |
249 | * \ |
250 | * Now XOR in the partial plaintext block, writing out the \ |
251 | * ciphertext as I go. Then encrypt, and write the complete \ |
252 | * ciphertext block. \ |
253 | */ \ |
254 | \ |
255 | s += PRE##_BLKSZ; \ |
256 | d += PRE##_BLKSZ; \ |
257 | for (i = 0; i < sz; i++) { \ |
258 | register octet x = b[i]; \ |
259 | b[i] ^= s[i]; \ |
260 | d[i] = x; \ |
261 | } \ |
262 | BLKC_LOAD(PRE, ctx->iv, b); \ |
263 | pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \ |
264 | BLKC_STORE(PRE, d - PRE##_BLKSZ, ctx->iv); \ |
265 | } \ |
266 | \ |
267 | /* --- Done --- */ \ |
268 | \ |
269 | return; \ |
270 | } \ |
271 | \ |
272 | /* --- @pre_cbcdecrypt@ --- * \ |
273 | * \ |
274 | * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \ |
275 | * @const void *src@ = pointer to source data \ |
276 | * @void *dest@ = pointer to destination data \ |
277 | * @size_t sz@ = size of block to be encrypted \ |
278 | * \ |
279 | * Returns: --- \ |
280 | * \ |
281 | * Use: Decrypts a block with a block cipher in CBC mode, with \ |
282 | * ciphertext stealing and other clever tricks. \ |
283 | * Essentially, data can be encrypted in arbitrary sized \ |
284 | * chunks, although decryption must use the same chunks. \ |
285 | */ \ |
286 | \ |
287 | void pre##_cbcdecrypt(pre##_cbcctx *ctx, \ |
288 | const void *src, void *dest, \ |
289 | size_t sz) \ |
290 | { \ |
291 | const octet *s = src; \ |
292 | octet *d = dest; \ |
293 | \ |
294 | /* --- Empty blocks are trivial --- */ \ |
295 | \ |
296 | if (!sz) \ |
297 | return; \ |
298 | \ |
299 | /* --- Extra magical case for a short block --- * \ |
300 | * \ |
301 | * Encrypt the IV, then exclusive-or the ciphertext with the octets \ |
302 | * of the encrypted IV, shifting ciphertext octets in instead. This \ |
303 | * basically switches over to CFB. \ |
304 | */ \ |
305 | \ |
306 | if (sz < PRE##_BLKSZ) { \ |
307 | octet b[PRE##_BLKSZ], c[PRE##_BLKSZ]; \ |
308 | unsigned i; \ |
309 | \ |
310 | pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \ |
311 | BLKC_STORE(PRE, b, ctx->iv); \ |
312 | for (i = 0; i < sz; i++) { \ |
313 | register octet x = s[i]; \ |
314 | d[i] = b[i] ^ x; \ |
315 | c[i] = x; \ |
316 | } \ |
317 | memmove(b, b + sz, PRE##_BLKSZ - sz); \ |
318 | memcpy(b + PRE##_BLKSZ - sz, c, sz); \ |
319 | BLKC_LOAD(PRE, ctx->iv, b); \ |
320 | return; \ |
321 | } \ |
322 | \ |
323 | /* --- Do the main chunk of decryption --- * \ |
324 | * \ |
325 | * This will do the whole lot if it's a whole number of blocks. For \ |
326 | * each block, decrypt, XOR it with the previous ciphertext in @iv@, \ |
327 | * and keep a copy of the ciphertext for the next block. \ |
328 | */ \ |
329 | \ |
330 | while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \ |
331 | uint32 b[PRE##_BLKSZ / 4], niv[PRE##_BLKSZ / 4]; \ |
332 | BLKC_LOAD(PRE, niv, s); \ |
333 | pre##_dblk(&ctx->ctx, niv, b); \ |
334 | BLKC_XSTORE(PRE, d, b, ctx->iv); \ |
335 | BLKC_MOVE(PRE, ctx->iv, niv); \ |
336 | s += PRE##_BLKSZ; \ |
337 | d += PRE##_BLKSZ; \ |
338 | sz -= PRE##_BLKSZ; \ |
339 | } \ |
340 | \ |
341 | /* --- Do the tail-end block and bit-left-over --- * \ |
342 | * \ |
343 | * This isn't very efficient. That shouldn't matter much. \ |
344 | */ \ |
345 | \ |
346 | if (sz) { \ |
347 | octet b[PRE##_BLKSZ]; \ |
348 | uint32 bk[PRE##_BLKSZ / 4], niv[PRE##_BLKSZ / 4]; \ |
349 | unsigned i; \ |
350 | \ |
351 | /* --- Let @sz@ be the size of the partial block --- */ \ |
352 | \ |
353 | sz -= PRE##_BLKSZ; \ |
354 | \ |
355 | /* --- First stage --- * \ |
356 | * \ |
357 | * Take the complete ciphertext block, and decrypt it. This block \ |
358 | * is carried over for the next encryption operation. \ |
359 | */ \ |
360 | \ |
361 | BLKC_LOAD(PRE, niv, s); \ |
362 | pre##_dblk(&ctx->ctx, niv, bk); \ |
363 | \ |
364 | /* --- Second stage --- * \ |
365 | * \ |
366 | * XORing the first few bytes of this with the partial ciphertext \ |
367 | * block recovers the partial plaintext block. At the same time, \ |
368 | * write the partial ciphertext block's contents in ready for stage \ |
369 | * three. \ |
370 | */ \ |
371 | \ |
372 | BLKC_STORE(PRE, b, bk); \ |
373 | s += PRE##_BLKSZ; \ |
374 | d += PRE##_BLKSZ; \ |
375 | for (i = 0; i < sz; i++) { \ |
376 | register octet x = s[i]; \ |
377 | d[i] = b[i] ^ x; \ |
378 | b[i] = x; \ |
379 | } \ |
380 | \ |
381 | /* --- Third stage --- * \ |
382 | * \ |
383 | * Decrypt the block we've got left, and XOR with the initial IV to \ |
384 | * recover the complete plaintext block. \ |
385 | */ \ |
386 | \ |
387 | BLKC_LOAD(PRE, bk, b); \ |
388 | pre##_dblk(&ctx->ctx, bk, bk); \ |
389 | BLKC_XSTORE(PRE, d - PRE##_BLKSZ, bk, ctx->iv); \ |
390 | BLKC_MOVE(PRE, ctx->iv, niv); \ |
391 | } \ |
392 | \ |
393 | /* --- Done --- */ \ |
394 | \ |
395 | return; \ |
396 | } \ |
397 | \ |
398 | /* --- Generic cipher interface --- */ \ |
399 | \ |
400 | static const gcipher_ops gops; \ |
401 | \ |
402 | typedef struct gctx { \ |
403 | gcipher c; \ |
404 | pre##_cbcctx k; \ |
405 | } gctx; \ |
406 | \ |
407 | static gcipher *ginit(const void *k, size_t sz) \ |
408 | { \ |
99461409 |
409 | gctx *g = S_CREATE(gctx); \ |
79ba130c |
410 | g->c.ops = &gops; \ |
411 | pre##_cbcinit(&g->k, k, sz, 0); \ |
412 | return (&g->c); \ |
413 | } \ |
414 | \ |
415 | static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \ |
416 | { \ |
417 | gctx *g = (gctx *)c; \ |
418 | pre##_cbcencrypt(&g->k, s, t, sz); \ |
419 | } \ |
420 | \ |
421 | static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \ |
422 | { \ |
423 | gctx *g = (gctx *)c; \ |
424 | pre##_cbcdecrypt(&g->k, s, t, sz); \ |
425 | } \ |
426 | \ |
427 | static void gdestroy(gcipher *c) \ |
428 | { \ |
429 | gctx *g = (gctx *)c; \ |
99461409 |
430 | BURN(*g); \ |
431 | S_DESTROY(g); \ |
79ba130c |
432 | } \ |
433 | \ |
434 | static void gsetiv(gcipher *c, const void *iv) \ |
435 | { \ |
436 | gctx *g = (gctx *)c; \ |
437 | pre##_cbcsetiv(&g->k, iv); \ |
438 | } \ |
439 | \ |
440 | static const gcipher_ops gops = { \ |
99461409 |
441 | &pre##_cbc, \ |
442 | gencrypt, gdecrypt, gdestroy, gsetiv, 0 \ |
79ba130c |
443 | }; \ |
444 | \ |
445 | const gccipher pre##_cbc = { \ |
99461409 |
446 | #pre "-cbc", pre##_keysz, PRE##_BLKSZ, \ |
79ba130c |
447 | ginit \ |
448 | }; \ |
449 | \ |
450 | CBC_TEST(PRE, pre) |
451 | |
452 | /*----- Test rig ----------------------------------------------------------*/ |
453 | |
454 | #ifdef TEST_RIG |
455 | |
456 | #include <stdio.h> |
457 | |
458 | #include "daftstory.h" |
459 | |
460 | /* --- @CBC_TEST@ --- * |
461 | * |
462 | * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions |
463 | * |
464 | * Use: Standard test rig for CBC functions. |
465 | */ |
466 | |
467 | #define CBC_TEST(PRE, pre) \ |
468 | \ |
469 | /* --- Initial plaintext for the test --- */ \ |
470 | \ |
471 | static const octet text[] = TEXT; \ |
472 | \ |
473 | /* --- Key and IV to use --- */ \ |
474 | \ |
475 | static const octet key[] = KEY; \ |
476 | static const octet iv[] = IV; \ |
477 | \ |
478 | /* --- Buffers for encryption and decryption output --- */ \ |
479 | \ |
480 | static octet ct[sizeof(text)]; \ |
481 | static octet pt[sizeof(text)]; \ |
482 | \ |
483 | static void hexdump(const octet *p, size_t sz) \ |
484 | { \ |
485 | const octet *q = p + sz; \ |
486 | for (sz = 0; p < q; p++, sz++) { \ |
487 | printf("%02x", *p); \ |
488 | if ((sz + 1) % PRE##_BLKSZ == 0) \ |
489 | putchar(':'); \ |
490 | } \ |
491 | } \ |
492 | \ |
493 | int main(void) \ |
494 | { \ |
495 | size_t sz = 0, rest; \ |
496 | pre##_cbcctx ctx; \ |
497 | pre##_ctx k; \ |
498 | int status = 0; \ |
499 | int done = 0; \ |
500 | \ |
501 | size_t keysz = PRE##_KEYSZ ? \ |
502 | PRE##_KEYSZ : strlen((const char *)key); \ |
503 | \ |
504 | fputs(#pre "-cbc: ", stdout); \ |
505 | \ |
506 | pre##_init(&k, key, keysz); \ |
507 | pre##_cbcsetkey(&ctx, &k); \ |
508 | \ |
509 | while (sz <= sizeof(text)) { \ |
510 | rest = sizeof(text) - sz; \ |
511 | memcpy(ct, text, sizeof(text)); \ |
512 | pre##_cbcsetiv(&ctx, iv); \ |
513 | pre##_cbcencrypt(&ctx, ct, ct, sz); \ |
514 | pre##_cbcencrypt(&ctx, ct + sz, ct + sz, rest); \ |
515 | memcpy(pt, ct, sizeof(text)); \ |
516 | pre##_cbcsetiv(&ctx, iv); \ |
517 | pre##_cbcdecrypt(&ctx, pt, pt, sz); \ |
518 | pre##_cbcdecrypt(&ctx, pt + sz, pt + sz, rest); \ |
519 | if (memcmp(pt, text, sizeof(text)) == 0) { \ |
520 | done++; \ |
521 | if (sizeof(text) < 40 || done % 8 == 0) \ |
522 | fputc('.', stdout); \ |
523 | if (done % 480 == 0) \ |
524 | fputs("\n\t", stdout); \ |
525 | fflush(stdout); \ |
526 | } else { \ |
527 | printf("\nError (sz = %lu)\n", (unsigned long)sz); \ |
528 | status = 1; \ |
529 | printf("\tplaintext = "); hexdump(text, sz); \ |
530 | printf(", "); hexdump(text + sz, rest); \ |
531 | fputc('\n', stdout); \ |
532 | printf("\tciphertext = "); hexdump(ct, sz); \ |
533 | printf(", "); hexdump(ct + sz, rest); \ |
534 | fputc('\n', stdout); \ |
535 | printf("\trecovered text = "); hexdump(pt, sz); \ |
536 | printf(", "); hexdump(pt + sz, rest); \ |
537 | fputc('\n', stdout); \ |
538 | fputc('\n', stdout); \ |
539 | } \ |
540 | if (sz < 63) \ |
541 | sz++; \ |
542 | else \ |
543 | sz += 9; \ |
544 | } \ |
545 | \ |
546 | fputs(status ? " failed\n" : " ok\n", stdout); \ |
547 | return (status); \ |
548 | } |
549 | |
550 | #else |
551 | # define CBC_TEST(PRE, pre) |
552 | #endif |
553 | |
554 | /*----- That's all, folks -------------------------------------------------*/ |
555 | |
556 | #ifdef __cplusplus |
557 | } |
558 | #endif |
559 | |
560 | #endif |