3 * Definitions for ciphertext feedback 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_CFB_DEF_H
29 #define CATACOMB_CFB_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 #ifndef CATACOMB_PARANOIA_H
59 # include "paranoia.h"
62 /*----- Macros ------------------------------------------------------------*/
64 /* --- @CFB_DEF@ --- *
66 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
68 * Use: Creates an implementation for CFB mode.
71 #define CFB_DEF(PRE, pre) CFB_DEFX(PRE, pre, #pre, #pre)
73 #define CFB_DEFX(PRE, pre, name, fname) \
75 /* --- @pre_cfbgetiv@ --- * \
77 * Arguments: @const pre_cfbctx *ctx@ = pointer to CFB context block \
78 * @void *iv@ = pointer to output data block \
82 * Use: Reads the currently set IV. Reading and setting an IV \
83 * is not transparent to the cipher. It will add a `step' \
84 * which must be matched by a similar operation during \
88 void pre##_cfbgetiv(const pre##_cfbctx *ctx, void *iv) \
91 unsigned off = ctx->off; \
92 unsigned rest = PRE##_BLKSZ - off; \
93 memcpy(p, ctx->iv + off, rest); \
94 memcpy(p + rest, ctx->iv, off); \
97 /* --- @pre_cfbsetiv@ --- * \
99 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
100 * @cnost void *iv@ = pointer to IV to set \
104 * Use: Sets the IV to use for subsequent encryption. \
107 void pre##_cfbsetiv(pre##_cfbctx *ctx, const void *iv) \
109 memcpy(ctx->iv, iv, PRE##_BLKSZ); \
110 ctx->off = PRE##_BLKSZ; \
113 /* --- @pre_cfbbdry@ --- * \
115 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
119 * Use: Inserts a boundary during encryption. Successful \
120 * decryption must place a similar boundary. \
123 void pre##_cfbbdry(pre##_cfbctx *ctx) \
125 uint32 niv[PRE##_BLKSZ / 4]; \
126 BLKC_LOAD(PRE, niv, ctx->iv); \
127 pre##_eblk(&ctx->ctx, niv, niv); \
128 BLKC_STORE(PRE, ctx->iv, niv); \
129 ctx->off = PRE##_BLKSZ; \
133 /* --- @pre_cfbsetkey@ --- * \
135 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
136 * @const pre_ctx *k@ = pointer to cipher context \
140 * Use: Sets the CFB context to use a different cipher key. \
143 void pre##_cfbsetkey(pre##_cfbctx *ctx, const pre##_ctx *k) \
146 ctx->off = PRE##_BLKSZ; \
149 /* --- @pre_cfbinit@ --- * \
151 * Arguments: @pre_cfbctx *ctx@ = pointer to cipher context \
152 * @const void *key@ = pointer to the key buffer \
153 * @size_t sz@ = size of the key \
154 * @const void *iv@ = pointer to initialization vector \
158 * Use: Initializes a CFB context ready for use. You should \
159 * ensure that the IV chosen is unique: reusing an IV will \
160 * compromise the security of at least the first block \
161 * encrypted. This is equivalent to calls to @pre_init@, \
162 * @pre_cfbsetkey@ and @pre_cfbsetiv@. \
165 void pre##_cfbinit(pre##_cfbctx *ctx, \
166 const void *key, size_t sz, \
169 static const octet zero[PRE##_BLKSZ] = { 0 }; \
170 pre##_init(&ctx->ctx, key, sz); \
171 pre##_cfbsetiv(ctx, iv ? iv : zero); \
174 /* --- @pre_cfbencrypt@ --- * \
176 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
177 * @const void *src@ = pointer to source data \
178 * @void *dest@ = pointer to destination data \
179 * @size_t sz@ = size of block to be encrypted \
183 * Use: Encrypts a block with a block cipher in CFB mode. The \
184 * input block may be arbitrary in size. CFB mode is not \
185 * sensitive to block boundaries. \
188 void pre##_cfbencrypt(pre##_cfbctx *ctx, \
189 const void *src, void *dest, \
192 const octet *s = src; \
194 unsigned off = ctx->off; \
196 /* --- Empty blocks are trivial --- */ \
201 /* --- If I can deal with the block from my buffer, do that --- */ \
203 if (sz < PRE##_BLKSZ - off) \
206 /* --- Finish off what's left in my buffer --- */ \
208 while (off < PRE##_BLKSZ) { \
209 register octet x = *s++; \
211 if (d) *d++ = ctx->iv[off]; \
216 /* --- Main encryption loop --- */ \
219 uint32 iv[PRE##_BLKSZ / 4]; \
220 BLKC_LOAD(PRE, iv, ctx->iv); \
223 pre##_eblk(&ctx->ctx, iv, iv); \
224 if (sz < PRE##_BLKSZ) \
227 BLKC_XLOAD(PRE, iv, s); \
231 BLKC_STORE(PRE, d, iv); \
237 BLKC_STORE(PRE, ctx->iv, iv); \
240 /* --- Tidying up the tail end --- */ \
245 register octet x = *s++; \
247 if (d) *d++ = ctx->iv[off]; \
259 /* --- @pre_cfbdecrypt@ --- * \
261 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB 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 CFB mode. The \
269 * input block may be arbitrary in size. CFB mode is not \
270 * sensitive to block boundaries. \
273 void pre##_cfbdecrypt(pre##_cfbctx *ctx, \
274 const void *src, void *dest, \
277 const octet *s = src; \
279 unsigned off = ctx->off; \
281 /* --- Empty blocks are trivial --- */ \
286 /* --- If I can deal with the block from my buffer, do that --- */ \
288 if (sz < PRE##_BLKSZ - off) \
291 /* --- Finish off what's left in my buffer --- */ \
293 while (off < PRE##_BLKSZ) { \
294 register octet x = *s++; \
295 *d++ = ctx->iv[off] ^ x; \
296 ctx->iv[off++] = x; \
300 /* --- Main encryption loop --- */ \
303 uint32 iv[PRE##_BLKSZ / 4]; \
304 BLKC_LOAD(PRE, iv, ctx->iv); \
307 uint32 x[PRE##_BLKSZ / 4]; \
308 pre##_eblk(&ctx->ctx, iv, iv); \
309 if (sz < PRE##_BLKSZ) \
311 BLKC_LOAD(PRE, x, s); \
312 BLKC_XSTORE(PRE, d, iv, x); \
313 BLKC_MOVE(PRE, iv, x); \
319 BLKC_STORE(PRE, ctx->iv, iv); \
322 /* --- Tidying up the tail end --- */ \
327 register octet x = *s++; \
328 *d++ = ctx->iv[off] ^ x; \
329 ctx->iv[off++] = x; \
340 /* --- Generic cipher interface --- */ \
342 static const gcipher_ops gops; \
344 typedef struct gctx { \
349 static gcipher *ginit(const void *k, size_t sz) \
351 gctx *g = S_CREATE(gctx); \
353 pre##_cfbinit(&g->k, k, sz, 0); \
357 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
359 gctx *g = (gctx *)c; \
360 pre##_cfbencrypt(&g->k, s, t, sz); \
363 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
365 gctx *g = (gctx *)c; \
366 pre##_cfbdecrypt(&g->k, s, t, sz); \
369 static void gdestroy(gcipher *c) \
371 gctx *g = (gctx *)c; \
376 static void gsetiv(gcipher *c, const void *iv) \
378 gctx *g = (gctx *)c; \
379 pre##_cfbsetiv(&g->k, iv); \
382 static void gbdry(gcipher *c) \
384 gctx *g = (gctx *)c; \
385 pre##_cfbbdry(&g->k); \
388 static const gcipher_ops gops = { \
390 gencrypt, gdecrypt, gdestroy, gsetiv, gbdry \
393 const gccipher pre##_cfb = { \
394 name "-cfb", pre##_keysz, PRE##_BLKSZ, \
398 CFB_TESTX(PRE, pre, name, fname)
400 /*----- Test rig ----------------------------------------------------------*/
402 #define CFB_TEST(PRE, pre) CFB_TESTX(PRE, pre, #pre, #pre)
408 #include "daftstory.h"
410 /* --- @CFB_TEST@ --- *
412 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
414 * Use: Standard test rig for CFB functions.
417 #define CFB_TESTX(PRE, pre, name, fname) \
419 /* --- Initial plaintext for the test --- */ \
421 static const octet text[] = TEXT; \
423 /* --- Key and IV to use --- */ \
425 static const octet key[] = KEY; \
426 static const octet iv[] = IV; \
428 /* --- Buffers for encryption and decryption output --- */ \
430 static octet ct[sizeof(text)]; \
431 static octet pt[sizeof(text)]; \
433 static void hexdump(const octet *p, size_t sz, size_t off) \
435 const octet *q = p + sz; \
436 for (sz = 0; p < q; p++, sz++) { \
437 printf("%02x", *p); \
438 if ((off + sz + 1) % PRE##_BLKSZ == 0) \
445 size_t sz = 0, rest; \
451 size_t keysz = PRE##_KEYSZ ? \
452 PRE##_KEYSZ : strlen((const char *)key); \
454 fputs(name "-cfb: ", stdout); \
456 pre##_init(&k, key, keysz); \
457 pre##_cfbsetkey(&ctx, &k); \
459 while (sz <= sizeof(text)) { \
460 rest = sizeof(text) - sz; \
461 memcpy(ct, text, sizeof(text)); \
462 pre##_cfbsetiv(&ctx, iv); \
463 pre##_cfbencrypt(&ctx, ct, ct, sz); \
464 pre##_cfbencrypt(&ctx, ct + sz, ct + sz, rest); \
465 memcpy(pt, ct, sizeof(text)); \
466 pre##_cfbsetiv(&ctx, iv); \
467 pre##_cfbdecrypt(&ctx, pt, pt, rest); \
468 pre##_cfbdecrypt(&ctx, pt + rest, pt + rest, sz); \
469 if (memcmp(pt, text, sizeof(text)) == 0) { \
471 if (sizeof(text) < 40 || done % 8 == 0) \
472 fputc('.', stdout); \
473 if (done % 480 == 0) \
474 fputs("\n\t", stdout); \
477 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
479 printf("\tplaintext = "); hexdump(text, sz, 0); \
480 printf(", "); hexdump(text + sz, rest, sz); \
481 fputc('\n', stdout); \
482 printf("\tciphertext = "); hexdump(ct, sz, 0); \
483 printf(", "); hexdump(ct + sz, rest, sz); \
484 fputc('\n', stdout); \
485 printf("\trecovered text = "); hexdump(pt, sz, 0); \
486 printf(", "); hexdump(pt + sz, rest, sz); \
487 fputc('\n', stdout); \
488 fputc('\n', stdout); \
496 fputs(status ? " failed\n" : " ok\n", stdout); \
501 # define CFB_TESTX(PRE, pre, name, fname)
504 /*----- That's all, folks -------------------------------------------------*/