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) \
73 /* --- @pre_cfbgetiv@ --- * \
75 * Arguments: @const pre_cfbctx *ctx@ = pointer to CFB context block \
76 * @void *iv@ = pointer to output data block \
80 * Use: Reads the currently set IV. Reading and setting an IV \
81 * is not transparent to the cipher. It will add a `step' \
82 * which must be matched by a similar operation during \
86 void pre##_cfbgetiv(const pre##_cfbctx *ctx, void *iv) \
89 unsigned off = ctx->off; \
90 unsigned rest = PRE##_BLKSZ - off; \
91 memcpy(p, ctx->iv + off, rest); \
92 memcpy(p + rest, ctx->iv, off); \
95 /* --- @pre_cfbsetiv@ --- * \
97 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
98 * @cnost void *iv@ = pointer to IV to set \
102 * Use: Sets the IV to use for subsequent encryption. \
105 void pre##_cfbsetiv(pre##_cfbctx *ctx, const void *iv) \
107 memcpy(ctx->iv, iv, PRE##_BLKSZ); \
108 ctx->off = PRE##_BLKSZ; \
111 /* --- @pre_cfbbdry@ --- * \
113 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
117 * Use: Inserts a boundary during encryption. Successful \
118 * decryption must place a similar boundary. \
121 void pre##_cfbbdry(pre##_cfbctx *ctx) \
123 uint32 niv[PRE##_BLKSZ / 4]; \
124 BLKC_LOAD(PRE, niv, ctx->iv); \
125 pre##_eblk(&ctx->ctx, niv, niv); \
126 BLKC_STORE(PRE, ctx->iv, niv); \
127 ctx->off = PRE##_BLKSZ; \
131 /* --- @pre_cfbsetkey@ --- * \
133 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
134 * @const pre_ctx *k@ = pointer to cipher context \
138 * Use: Sets the CFB context to use a different cipher key. \
141 void pre##_cfbsetkey(pre##_cfbctx *ctx, const pre##_ctx *k) \
144 ctx->off = PRE##_BLKSZ; \
147 /* --- @pre_cfbinit@ --- * \
149 * Arguments: @pre_cfbctx *ctx@ = pointer to cipher context \
150 * @const void *key@ = pointer to the key buffer \
151 * @size_t sz@ = size of the key \
152 * @const void *iv@ = pointer to initialization vector \
156 * Use: Initializes a CFB context ready for use. You should \
157 * ensure that the IV chosen is unique: reusing an IV will \
158 * compromise the security of at least the first block \
159 * encrypted. This is equivalent to calls to @pre_init@, \
160 * @pre_cfbsetkey@ and @pre_cfbsetiv@. \
163 void pre##_cfbinit(pre##_cfbctx *ctx, \
164 const void *key, size_t sz, \
167 static const octet zero[PRE##_BLKSZ] = { 0 }; \
168 pre##_init(&ctx->ctx, key, sz); \
169 pre##_cfbsetiv(ctx, iv ? iv : zero); \
172 /* --- @pre_cfbencrypt@ --- * \
174 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
175 * @const void *src@ = pointer to source data \
176 * @void *dest@ = pointer to destination data \
177 * @size_t sz@ = size of block to be encrypted \
181 * Use: Encrypts a block with a block cipher in CFB mode. The \
182 * input block may be arbitrary in size. CFB mode is not \
183 * sensitive to block boundaries. \
186 void pre##_cfbencrypt(pre##_cfbctx *ctx, \
187 const void *src, void *dest, \
190 const octet *s = src; \
192 unsigned off = ctx->off; \
194 /* --- Empty blocks are trivial --- */ \
199 /* --- If I can deal with the block from my buffer, do that --- */ \
201 if (sz < PRE##_BLKSZ - off) \
204 /* --- Finish off what's left in my buffer --- */ \
206 while (off < PRE##_BLKSZ) { \
207 register octet x = *s++; \
209 if (d) *d++ = ctx->iv[off]; \
214 /* --- Main encryption loop --- */ \
217 uint32 iv[PRE##_BLKSZ / 4]; \
218 BLKC_LOAD(PRE, iv, ctx->iv); \
221 pre##_eblk(&ctx->ctx, iv, iv); \
222 if (sz < PRE##_BLKSZ) \
225 BLKC_XLOAD(PRE, iv, s); \
229 BLKC_STORE(PRE, d, iv); \
235 BLKC_STORE(PRE, ctx->iv, iv); \
238 /* --- Tidying up the tail end --- */ \
243 register octet x = *s++; \
245 if (d) *d++ = ctx->iv[off]; \
257 /* --- @pre_cfbdecrypt@ --- * \
259 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
260 * @const void *src@ = pointer to source data \
261 * @void *dest@ = pointer to destination data \
262 * @size_t sz@ = size of block to be encrypted \
266 * Use: Decrypts a block with a block cipher in CFB mode. The \
267 * input block may be arbitrary in size. CFB mode is not \
268 * sensitive to block boundaries. \
271 void pre##_cfbdecrypt(pre##_cfbctx *ctx, \
272 const void *src, void *dest, \
275 const octet *s = src; \
277 unsigned off = ctx->off; \
279 /* --- Empty blocks are trivial --- */ \
284 /* --- If I can deal with the block from my buffer, do that --- */ \
286 if (sz < PRE##_BLKSZ - off) \
289 /* --- Finish off what's left in my buffer --- */ \
291 while (off < PRE##_BLKSZ) { \
292 register octet x = *s++; \
293 *d++ = ctx->iv[off] ^ x; \
294 ctx->iv[off++] = x; \
298 /* --- Main encryption loop --- */ \
301 uint32 iv[PRE##_BLKSZ / 4]; \
302 BLKC_LOAD(PRE, iv, ctx->iv); \
305 uint32 x[PRE##_BLKSZ / 4]; \
306 pre##_eblk(&ctx->ctx, iv, iv); \
307 if (sz < PRE##_BLKSZ) \
309 BLKC_LOAD(PRE, x, s); \
310 BLKC_XSTORE(PRE, d, iv, x); \
311 BLKC_MOVE(PRE, iv, x); \
317 BLKC_STORE(PRE, ctx->iv, iv); \
320 /* --- Tidying up the tail end --- */ \
325 register octet x = *s++; \
326 *d++ = ctx->iv[off] ^ x; \
327 ctx->iv[off++] = x; \
338 /* --- Generic cipher interface --- */ \
340 static const gcipher_ops gops; \
342 typedef struct gctx { \
347 static gcipher *ginit(const void *k, size_t sz) \
349 gctx *g = S_CREATE(gctx); \
351 pre##_cfbinit(&g->k, k, sz, 0); \
355 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
357 gctx *g = (gctx *)c; \
358 pre##_cfbencrypt(&g->k, s, t, sz); \
361 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
363 gctx *g = (gctx *)c; \
364 pre##_cfbdecrypt(&g->k, s, t, sz); \
367 static void gdestroy(gcipher *c) \
369 gctx *g = (gctx *)c; \
374 static void gsetiv(gcipher *c, const void *iv) \
376 gctx *g = (gctx *)c; \
377 pre##_cfbsetiv(&g->k, iv); \
380 static void gbdry(gcipher *c) \
382 gctx *g = (gctx *)c; \
383 pre##_cfbbdry(&g->k); \
386 static const gcipher_ops gops = { \
388 gencrypt, gdecrypt, gdestroy, gsetiv, gbdry \
391 const gccipher pre##_cfb = { \
392 #pre "-cfb", pre##_keysz, PRE##_BLKSZ, \
398 /*----- Test rig ----------------------------------------------------------*/
404 #include "daftstory.h"
406 /* --- @CFB_TEST@ --- *
408 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
410 * Use: Standard test rig for CFB functions.
413 #define CFB_TEST(PRE, pre) \
415 /* --- Initial plaintext for the test --- */ \
417 static const octet text[] = TEXT; \
419 /* --- Key and IV to use --- */ \
421 static const octet key[] = KEY; \
422 static const octet iv[] = IV; \
424 /* --- Buffers for encryption and decryption output --- */ \
426 static octet ct[sizeof(text)]; \
427 static octet pt[sizeof(text)]; \
429 static void hexdump(const octet *p, size_t sz, size_t off) \
431 const octet *q = p + sz; \
432 for (sz = 0; p < q; p++, sz++) { \
433 printf("%02x", *p); \
434 if ((off + sz + 1) % PRE##_BLKSZ == 0) \
441 size_t sz = 0, rest; \
447 size_t keysz = PRE##_KEYSZ ? \
448 PRE##_KEYSZ : strlen((const char *)key); \
450 fputs(#pre "-cfb: ", stdout); \
452 pre##_init(&k, key, keysz); \
453 pre##_cfbsetkey(&ctx, &k); \
455 while (sz <= sizeof(text)) { \
456 rest = sizeof(text) - sz; \
457 memcpy(ct, text, sizeof(text)); \
458 pre##_cfbsetiv(&ctx, iv); \
459 pre##_cfbencrypt(&ctx, ct, ct, sz); \
460 pre##_cfbencrypt(&ctx, ct + sz, ct + sz, rest); \
461 memcpy(pt, ct, sizeof(text)); \
462 pre##_cfbsetiv(&ctx, iv); \
463 pre##_cfbdecrypt(&ctx, pt, pt, rest); \
464 pre##_cfbdecrypt(&ctx, pt + rest, pt + rest, sz); \
465 if (memcmp(pt, text, sizeof(text)) == 0) { \
467 if (sizeof(text) < 40 || done % 8 == 0) \
468 fputc('.', stdout); \
469 if (done % 480 == 0) \
470 fputs("\n\t", stdout); \
473 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
475 printf("\tplaintext = "); hexdump(text, sz, 0); \
476 printf(", "); hexdump(text + sz, rest, sz); \
477 fputc('\n', stdout); \
478 printf("\tciphertext = "); hexdump(ct, sz, 0); \
479 printf(", "); hexdump(ct + sz, rest, sz); \
480 fputc('\n', stdout); \
481 printf("\trecovered text = "); hexdump(pt, sz, 0); \
482 printf(", "); hexdump(pt + sz, rest, sz); \
483 fputc('\n', stdout); \
484 fputc('\n', stdout); \
492 fputs(status ? " failed\n" : " ok\n", stdout); \
497 # define CFB_TEST(PRE, pre)
500 /*----- That's all, folks -------------------------------------------------*/