3 * Definitions for cipher block chaining mode
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 #ifndef CATACOMB_CBC_DEF_H
29 #define CATACOMB_CBC_DEF_H
35 /*----- Header files ------------------------------------------------------*/
39 #include <mLib/bits.h>
42 #ifndef CATACOMB_ARENA_H
46 #ifndef CATACOMB_BLKC_H
50 #ifndef CATACOMB_GCIPHER_H
54 #ifndef CATACOMB_PARANOIA_H
55 # include "paranoia.h"
58 /*----- Macros ------------------------------------------------------------*/
60 /* --- @CBC_DEF@ --- *
62 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
64 * Use: Creates an implementation for CBC stealing mode.
67 #define CBC_DEF(PRE, pre) \
69 /* --- @pre_cbcgetiv@ --- * \
71 * Arguments: @const pre_cbcctx *ctx@ = pointer to CBC context block \
72 * @void *iv@ = pointer to output data block \
76 * Use: Reads the currently set IV. Reading and setting an IV \
77 * is transparent to the CBC encryption or decryption \
81 void pre##_cbcgetiv(const pre##_cbcctx *ctx, void *iv) \
83 BLKC_STORE(PRE, iv, ctx->iv); \
86 /* --- @pre_cbcsetiv@ --- * \
88 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
89 * @cnost void *iv@ = pointer to IV to set \
93 * Use: Sets the IV to use for subsequent encryption. \
96 void pre##_cbcsetiv(pre##_cbcctx *ctx, const void *iv) \
98 BLKC_LOAD(PRE, ctx->iv, iv); \
101 /* --- @pre_cbcsetkey@ --- * \
103 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
104 * @const pre_ctx *k@ = pointer to cipher context \
108 * Use: Sets the CBC context to use a different cipher key. \
111 void pre##_cbcsetkey(pre##_cbcctx *ctx, const pre##_ctx *k) \
116 /* --- @pre_cbcinit@ --- * \
118 * Arguments: @pre_cbcctx *ctx@ = pointer to cipher context \
119 * @const void *key@ = pointer to the key buffer \
120 * @size_t sz@ = size of the key \
121 * @const void *iv@ = pointer to initialization vector \
125 * Use: Initializes a CBC context ready for use. The @iv@ \
126 * argument may be passed as a null pointer to set a zero \
127 * IV. Apart from that, this call is equivalent to calls \
128 * to @pre_init@, @pre_cbcsetkey@ and @pre_cbcsetiv@. \
131 void pre##_cbcinit(pre##_cbcctx *ctx, \
132 const void *key, size_t sz, \
135 static const octet zero[PRE##_BLKSZ] = { 0 }; \
136 pre##_init(&ctx->ctx, key, sz); \
137 BLKC_LOAD(PRE, ctx->iv, iv ? iv : zero); \
140 /* --- @pre_cbcencrypt@ --- * \
142 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
143 * @const void *src@ = pointer to source data \
144 * @void *dest@ = pointer to destination data \
145 * @size_t sz@ = size of block to be encrypted \
149 * Use: Encrypts a block with a block cipher in CBC mode, with \
150 * ciphertext stealing and other clever tricks. \
151 * Essentially, data can be encrypted in arbitrary sized \
152 * chunks, although decryption must use the same chunks. \
155 void pre##_cbcencrypt(pre##_cbcctx *ctx, \
156 const void *src, void *dest, \
159 const octet *s = src; \
162 /* --- Empty blocks are trivial --- */ \
167 /* --- Extra magical case for a short block --- * \
169 * Encrypt the IV, then exclusive-or the plaintext with the octets \
170 * of the encrypted IV, shifting ciphertext octets in instead. This \
171 * basically switches over to CFB. \
174 if (sz < PRE##_BLKSZ) { \
175 octet b[PRE##_BLKSZ]; \
178 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
179 BLKC_STORE(PRE, b, ctx->iv); \
181 for (i = 0; i < sz; i++) \
182 d[i] = b[i] ^ (s ? s[i] : 0); \
184 memmove(b, b + sz, PRE##_BLKSZ - sz); \
185 memcpy(b + PRE##_BLKSZ - sz, d, sz); \
186 BLKC_LOAD(PRE, ctx->iv, b); \
190 /* --- Do the main chunk of encryption --- * \
192 * This will do the whole lot if it's a whole number of blocks. For \
193 * each block, XOR it with the previous ciphertext in @iv@, encrypt, \
194 * and keep a copy of the ciphertext for the next block. \
197 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
199 BLKC_XLOAD(PRE, ctx->iv, s); \
202 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
204 BLKC_STORE(PRE, d, ctx->iv); \
210 /* --- Do the tail-end block and bit-left-over --- * \
212 * This isn't very efficient. That shouldn't matter much. \
216 octet b[PRE##_BLKSZ]; \
219 /* --- Let @sz@ be the size of the partial block --- */ \
223 /* --- First stage --- * \
225 * XOR the complete block with the current IV, and encrypt it. The \
226 * first part of the result is the partial ciphertext block. Don't \
227 * write that out yet, because I've not read the partial plaintext \
231 if (s) BLKC_XLOAD(PRE, ctx->iv, s); \
232 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
233 BLKC_STORE(PRE, b, ctx->iv); \
235 /* --- Second stage --- * \
237 * Now XOR in the partial plaintext block, writing out the \
238 * ciphertext as I go. Then encrypt, and write the complete \
239 * ciphertext block. \
242 if (s) s += PRE##_BLKSZ; \
243 if (d) d += PRE##_BLKSZ; \
244 for (i = 0; i < sz; i++) { \
245 register octet x = b[i]; \
246 if (s) b[i] ^= s[i]; \
249 BLKC_LOAD(PRE, ctx->iv, b); \
250 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
251 if (d) BLKC_STORE(PRE, d - PRE##_BLKSZ, ctx->iv); \
259 /* --- @pre_cbcdecrypt@ --- * \
261 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
262 * @const void *src@ = pointer to source data \
263 * @void *dest@ = pointer to destination data \
264 * @size_t sz@ = size of block to be encrypted \
268 * Use: Decrypts a block with a block cipher in CBC mode, with \
269 * ciphertext stealing and other clever tricks. \
270 * Essentially, data can be encrypted in arbitrary sized \
271 * chunks, although decryption must use the same chunks. \
274 void pre##_cbcdecrypt(pre##_cbcctx *ctx, \
275 const void *src, void *dest, \
278 const octet *s = src; \
281 /* --- Empty blocks are trivial --- */ \
286 /* --- Extra magical case for a short block --- * \
288 * Encrypt the IV, then exclusive-or the ciphertext with the octets \
289 * of the encrypted IV, shifting ciphertext octets in instead. This \
290 * basically switches over to CFB. \
293 if (sz < PRE##_BLKSZ) { \
294 octet b[PRE##_BLKSZ], c[PRE##_BLKSZ]; \
297 pre##_eblk(&ctx->ctx, ctx->iv, ctx->iv); \
298 BLKC_STORE(PRE, b, ctx->iv); \
299 for (i = 0; i < sz; i++) { \
300 register octet x = s[i]; \
304 memmove(b, b + sz, PRE##_BLKSZ - sz); \
305 memcpy(b + PRE##_BLKSZ - sz, c, sz); \
306 BLKC_LOAD(PRE, ctx->iv, b); \
310 /* --- Do the main chunk of decryption --- * \
312 * This will do the whole lot if it's a whole number of blocks. For \
313 * each block, decrypt, XOR it with the previous ciphertext in @iv@, \
314 * and keep a copy of the ciphertext for the next block. \
317 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
318 uint32 b[PRE##_BLKSZ / 4], niv[PRE##_BLKSZ / 4]; \
319 BLKC_LOAD(PRE, niv, s); \
320 pre##_dblk(&ctx->ctx, niv, b); \
321 BLKC_XSTORE(PRE, d, b, ctx->iv); \
322 BLKC_MOVE(PRE, ctx->iv, niv); \
328 /* --- Do the tail-end block and bit-left-over --- * \
330 * This isn't very efficient. That shouldn't matter much. \
334 octet b[PRE##_BLKSZ]; \
335 uint32 bk[PRE##_BLKSZ / 4], niv[PRE##_BLKSZ / 4]; \
338 /* --- Let @sz@ be the size of the partial block --- */ \
342 /* --- First stage --- * \
344 * Take the complete ciphertext block, and decrypt it. This block \
345 * is carried over for the next encryption operation. \
348 BLKC_LOAD(PRE, niv, s); \
349 pre##_dblk(&ctx->ctx, niv, bk); \
351 /* --- Second stage --- * \
353 * XORing the first few bytes of this with the partial ciphertext \
354 * block recovers the partial plaintext block. At the same time, \
355 * write the partial ciphertext block's contents in ready for stage \
359 BLKC_STORE(PRE, b, bk); \
362 for (i = 0; i < sz; i++) { \
363 register octet x = s[i]; \
368 /* --- Third stage --- * \
370 * Decrypt the block we've got left, and XOR with the initial IV to \
371 * recover the complete plaintext block. \
374 BLKC_LOAD(PRE, bk, b); \
375 pre##_dblk(&ctx->ctx, bk, bk); \
376 BLKC_XSTORE(PRE, d - PRE##_BLKSZ, bk, ctx->iv); \
377 BLKC_MOVE(PRE, ctx->iv, niv); \
385 /* --- Generic cipher interface --- */ \
387 static const gcipher_ops gops; \
389 typedef struct gctx { \
394 static gcipher *ginit(const void *k, size_t sz) \
396 gctx *g = S_CREATE(gctx); \
398 pre##_cbcinit(&g->k, k, sz, 0); \
402 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
404 gctx *g = (gctx *)c; \
405 pre##_cbcencrypt(&g->k, s, t, sz); \
408 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
410 gctx *g = (gctx *)c; \
411 pre##_cbcdecrypt(&g->k, s, t, sz); \
414 static void gdestroy(gcipher *c) \
416 gctx *g = (gctx *)c; \
421 static void gsetiv(gcipher *c, const void *iv) \
423 gctx *g = (gctx *)c; \
424 pre##_cbcsetiv(&g->k, iv); \
427 static const gcipher_ops gops = { \
429 gencrypt, gdecrypt, gdestroy, gsetiv, 0 \
432 const gccipher pre##_cbc = { \
433 #pre "-cbc", pre##_keysz, PRE##_BLKSZ, \
439 /*----- Test rig ----------------------------------------------------------*/
445 #include "daftstory.h"
447 /* --- @CBC_TEST@ --- *
449 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
451 * Use: Standard test rig for CBC functions.
454 #define CBC_TEST(PRE, pre) \
456 /* --- Initial plaintext for the test --- */ \
458 static const octet text[] = TEXT; \
460 /* --- Key and IV to use --- */ \
462 static const octet key[] = KEY; \
463 static const octet iv[] = IV; \
465 /* --- Buffers for encryption and decryption output --- */ \
467 static octet ct[sizeof(text)]; \
468 static octet pt[sizeof(text)]; \
470 static void hexdump(const octet *p, size_t sz, size_t off) \
472 const octet *q = p + sz; \
473 for (sz = 0; p < q; p++, sz++) { \
474 printf("%02x", *p); \
475 if ((off + sz + 1) % PRE##_BLKSZ == 0) \
482 size_t sz = 0, rest; \
488 size_t keysz = PRE##_KEYSZ ? \
489 PRE##_KEYSZ : strlen((const char *)key); \
491 fputs(#pre "-cbc: ", stdout); \
493 pre##_init(&k, key, keysz); \
494 pre##_cbcsetkey(&ctx, &k); \
496 while (sz <= sizeof(text)) { \
497 rest = sizeof(text) - sz; \
498 memcpy(ct, text, sizeof(text)); \
499 pre##_cbcsetiv(&ctx, iv); \
500 pre##_cbcencrypt(&ctx, ct, ct, sz); \
501 pre##_cbcencrypt(&ctx, ct + sz, ct + sz, rest); \
502 memcpy(pt, ct, sizeof(text)); \
503 pre##_cbcsetiv(&ctx, iv); \
504 pre##_cbcdecrypt(&ctx, pt, pt, sz); \
505 pre##_cbcdecrypt(&ctx, pt + sz, pt + sz, rest); \
506 if (memcmp(pt, text, sizeof(text)) == 0) { \
508 if (sizeof(text) < 40 || done % 8 == 0) \
509 fputc('.', stdout); \
510 if (done % 480 == 0) \
511 fputs("\n\t", stdout); \
514 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
516 printf("\tplaintext = "); hexdump(text, sz, 0); \
517 printf(", "); hexdump(text + sz, rest, sz); \
518 fputc('\n', stdout); \
519 printf("\tciphertext = "); hexdump(ct, sz, 0); \
520 printf(", "); hexdump(ct + sz, rest, sz); \
521 fputc('\n', stdout); \
522 printf("\trecovered text = "); hexdump(pt, sz, 0); \
523 printf(", "); hexdump(pt + sz, rest, sz); \
524 fputc('\n', stdout); \
525 fputc('\n', stdout); \
533 fputs(status ? " failed\n" : " ok\n", stdout); \
538 # define CBC_TEST(PRE, pre)
541 /*----- That's all, folks -------------------------------------------------*/