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