1d983f319fd8720a4c08168cef66e2a63a4731b2
3 * $Id: cfb-def.h,v 1.4 2004/04/02 01:03:49 mdw Exp $
5 * Definitions for ciphertext feedback mode
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
36 * Revision 1.3 2001/06/17 00:10:51 mdw
39 * Revision 1.2 2000/06/17 10:50:39 mdw
40 * Use secure arena for memory allocation. Rearrange setiv slightly.
42 * Revision 1.1 1999/12/10 23:16:39 mdw
43 * Split mode macros into interface and implementation.
47 #ifndef CATACOMB_CFB_DEF_H
48 #define CATACOMB_CFB_DEF_H
54 /*----- Header files ------------------------------------------------------*/
58 #include <mLib/bits.h>
61 #ifndef CATACOMB_ARENA_H
65 #ifndef CATACOMB_BLKC_H
69 #ifndef CATACOMB_GCIPHER_H
73 #ifndef CATACOMB_PARANOIA_H
74 # include "paranoia.h"
77 #ifndef CATACOMB_PARANOIA_H
78 # include "paranoia.h"
81 /*----- Macros ------------------------------------------------------------*/
83 /* --- @CFB_DEF@ --- *
85 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
87 * Use: Creates an implementation for CFB mode.
90 #define CFB_DEF(PRE, pre) \
92 /* --- @pre_cfbgetiv@ --- * \
94 * Arguments: @const pre_cfbctx *ctx@ = pointer to CFB context block \
95 * @void *iv@ = pointer to output data block \
99 * Use: Reads the currently set IV. Reading and setting an IV \
100 * is not transparent to the cipher. It will add a `step' \
101 * which must be matched by a similar operation during \
105 void pre##_cfbgetiv(const pre##_cfbctx *ctx, void *iv) \
108 unsigned off = ctx->off; \
109 unsigned rest = PRE##_BLKSZ - off; \
110 memcpy(p, ctx->iv + off, rest); \
111 memcpy(p + rest, ctx->iv, off); \
114 /* --- @pre_cfbsetiv@ --- * \
116 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
117 * @cnost void *iv@ = pointer to IV to set \
121 * Use: Sets the IV to use for subsequent encryption. \
124 void pre##_cfbsetiv(pre##_cfbctx *ctx, const void *iv) \
126 memcpy(ctx->iv, iv, PRE##_BLKSZ); \
127 ctx->off = PRE##_BLKSZ; \
130 /* --- @pre_cfbbdry@ --- * \
132 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
136 * Use: Inserts a boundary during encryption. Successful \
137 * decryption must place a similar boundary. \
140 void pre##_cfbbdry(pre##_cfbctx *ctx) \
142 uint32 niv[PRE##_BLKSZ / 4]; \
143 BLKC_LOAD(PRE, niv, ctx->iv); \
144 pre##_eblk(&ctx->ctx, niv, niv); \
145 BLKC_STORE(PRE, ctx->iv, niv); \
146 ctx->off = PRE##_BLKSZ; \
150 /* --- @pre_cfbsetkey@ --- * \
152 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
153 * @const pre_ctx *k@ = pointer to cipher context \
157 * Use: Sets the CFB context to use a different cipher key. \
160 void pre##_cfbsetkey(pre##_cfbctx *ctx, const pre##_ctx *k) \
163 ctx->off = PRE##_BLKSZ; \
166 /* --- @pre_cfbinit@ --- * \
168 * Arguments: @pre_cfbctx *ctx@ = pointer to cipher context \
169 * @const void *key@ = pointer to the key buffer \
170 * @size_t sz@ = size of the key \
171 * @const void *iv@ = pointer to initialization vector \
175 * Use: Initializes a CFB context ready for use. You should \
176 * ensure that the IV chosen is unique: reusing an IV will \
177 * compromise the security of at least the first block \
178 * encrypted. This is equivalent to calls to @pre_init@, \
179 * @pre_cfbsetkey@ and @pre_cfbsetiv@. \
182 void pre##_cfbinit(pre##_cfbctx *ctx, \
183 const void *key, size_t sz, \
186 static const octet zero[PRE##_BLKSZ] = { 0 }; \
187 pre##_init(&ctx->ctx, key, sz); \
188 pre##_cfbsetiv(ctx, iv ? iv : zero); \
191 /* --- @pre_cfbencrypt@ --- * \
193 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
194 * @const void *src@ = pointer to source data \
195 * @void *dest@ = pointer to destination data \
196 * @size_t sz@ = size of block to be encrypted \
200 * Use: Encrypts a block with a block cipher in CFB mode. The \
201 * input block may be arbitrary in size. CFB mode is not \
202 * sensitive to block boundaries. \
205 void pre##_cfbencrypt(pre##_cfbctx *ctx, \
206 const void *src, void *dest, \
209 const octet *s = src; \
211 unsigned off = ctx->off; \
213 /* --- Empty blocks are trivial --- */ \
218 /* --- If I can deal with the block from my buffer, do that --- */ \
220 if (sz < PRE##_BLKSZ - off) \
223 /* --- Finish off what's left in my buffer --- */ \
225 while (off < PRE##_BLKSZ) { \
226 register octet x = *s++; \
227 *d++ = ctx->iv[off++] ^= x; \
231 /* --- Main encryption loop --- */ \
234 uint32 iv[PRE##_BLKSZ / 4]; \
235 BLKC_LOAD(PRE, iv, ctx->iv); \
238 pre##_eblk(&ctx->ctx, iv, iv); \
239 if (sz < PRE##_BLKSZ) \
241 BLKC_XLOAD(PRE, iv, s); \
242 BLKC_STORE(PRE, d, iv); \
248 BLKC_STORE(PRE, ctx->iv, iv); \
251 /* --- Tidying up the tail end --- */ \
256 register octet x = *s++; \
257 *d++ = ctx->iv[off++] ^= x; \
268 /* --- @pre_cfbdecrypt@ --- * \
270 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
271 * @const void *src@ = pointer to source data \
272 * @void *dest@ = pointer to destination data \
273 * @size_t sz@ = size of block to be encrypted \
277 * Use: Decrypts a block with a block cipher in CFB mode. The \
278 * input block may be arbitrary in size. CFB mode is not \
279 * sensitive to block boundaries. \
282 void pre##_cfbdecrypt(pre##_cfbctx *ctx, \
283 const void *src, void *dest, \
286 const octet *s = src; \
288 unsigned off = ctx->off; \
290 /* --- Empty blocks are trivial --- */ \
295 /* --- If I can deal with the block from my buffer, do that --- */ \
297 if (sz < PRE##_BLKSZ - off) \
300 /* --- Finish off what's left in my buffer --- */ \
302 while (off < PRE##_BLKSZ) { \
303 register octet x = *s++; \
304 *d++ = ctx->iv[off] ^ x; \
305 ctx->iv[off++] = x; \
309 /* --- Main encryption loop --- */ \
312 uint32 iv[PRE##_BLKSZ / 4]; \
313 BLKC_LOAD(PRE, iv, ctx->iv); \
316 uint32 x[PRE##_BLKSZ / 4]; \
317 pre##_eblk(&ctx->ctx, iv, iv); \
318 if (sz < PRE##_BLKSZ) \
320 BLKC_LOAD(PRE, x, s); \
321 BLKC_XSTORE(PRE, d, iv, x); \
322 BLKC_MOVE(PRE, iv, x); \
328 BLKC_STORE(PRE, ctx->iv, iv); \
331 /* --- Tidying up the tail end --- */ \
336 register octet x = *s++; \
337 *d++ = ctx->iv[off] ^ x; \
338 ctx->iv[off++] = x; \
349 /* --- Generic cipher interface --- */ \
351 static const gcipher_ops gops; \
353 typedef struct gctx { \
358 static gcipher *ginit(const void *k, size_t sz) \
360 gctx *g = S_CREATE(gctx); \
362 pre##_cfbinit(&g->k, k, sz, 0); \
366 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
368 gctx *g = (gctx *)c; \
369 pre##_cfbencrypt(&g->k, s, t, sz); \
372 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
374 gctx *g = (gctx *)c; \
375 pre##_cfbdecrypt(&g->k, s, t, sz); \
378 static void gdestroy(gcipher *c) \
380 gctx *g = (gctx *)c; \
385 static void gsetiv(gcipher *c, const void *iv) \
387 gctx *g = (gctx *)c; \
388 pre##_cfbsetiv(&g->k, iv); \
391 static void gbdry(gcipher *c) \
393 gctx *g = (gctx *)c; \
394 pre##_cfbbdry(&g->k); \
397 static const gcipher_ops gops = { \
399 gencrypt, gdecrypt, gdestroy, gsetiv, gbdry \
402 const gccipher pre##_cfb = { \
403 #pre "-cfb", pre##_keysz, PRE##_BLKSZ, \
409 /*----- Test rig ----------------------------------------------------------*/
415 #include "daftstory.h"
417 /* --- @CFB_TEST@ --- *
419 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
421 * Use: Standard test rig for CFB functions.
424 #define CFB_TEST(PRE, pre) \
426 /* --- Initial plaintext for the test --- */ \
428 static const octet text[] = TEXT; \
430 /* --- Key and IV to use --- */ \
432 static const octet key[] = KEY; \
433 static const octet iv[] = IV; \
435 /* --- Buffers for encryption and decryption output --- */ \
437 static octet ct[sizeof(text)]; \
438 static octet pt[sizeof(text)]; \
440 static void hexdump(const octet *p, size_t sz) \
442 const octet *q = p + sz; \
443 for (sz = 0; p < q; p++, sz++) { \
444 printf("%02x", *p); \
445 if ((sz + 1) % PRE##_BLKSZ == 0) \
452 size_t sz = 0, rest; \
458 size_t keysz = PRE##_KEYSZ ? \
459 PRE##_KEYSZ : strlen((const char *)key); \
461 fputs(#pre "-cfb: ", stdout); \
463 pre##_init(&k, key, keysz); \
464 pre##_cfbsetkey(&ctx, &k); \
466 while (sz <= sizeof(text)) { \
467 rest = sizeof(text) - sz; \
468 memcpy(ct, text, sizeof(text)); \
469 pre##_cfbsetiv(&ctx, iv); \
470 pre##_cfbencrypt(&ctx, ct, ct, sz); \
471 pre##_cfbencrypt(&ctx, ct + sz, ct + sz, rest); \
472 memcpy(pt, ct, sizeof(text)); \
473 pre##_cfbsetiv(&ctx, iv); \
474 pre##_cfbdecrypt(&ctx, pt, pt, rest); \
475 pre##_cfbdecrypt(&ctx, pt + rest, pt + rest, sz); \
476 if (memcmp(pt, text, sizeof(text)) == 0) { \
478 if (sizeof(text) < 40 || done % 8 == 0) \
479 fputc('.', stdout); \
480 if (done % 480 == 0) \
481 fputs("\n\t", stdout); \
484 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
486 printf("\tplaintext = "); hexdump(text, sz); \
487 printf(", "); hexdump(text + sz, rest); \
488 fputc('\n', stdout); \
489 printf("\tciphertext = "); hexdump(ct, sz); \
490 printf(", "); hexdump(ct + sz, rest); \
491 fputc('\n', stdout); \
492 printf("\trecovered text = "); hexdump(pt, sz); \
493 printf(", "); hexdump(pt + sz, rest); \
494 fputc('\n', stdout); \
495 fputc('\n', stdout); \
503 fputs(status ? " failed\n" : " ok\n", stdout); \
508 # define CFB_TEST(PRE, pre)
511 /*----- That's all, folks -------------------------------------------------*/