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; \
94 memcpy(p, ctx->b + off, rest); \
95 memcpy(p + rest, ctx->b, off); \
98 /* --- @pre_cfbsetiv@ --- * \
100 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
101 * @cnost void *iv@ = pointer to IV to set \
105 * Use: Sets the IV to use for subsequent encryption. \
108 void pre##_cfbsetiv(pre##_cfbctx *ctx, const void *iv) \
109 { memcpy(ctx->b, iv, PRE##_BLKSZ); 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 t[PRE##_BLKSZ/4]; \
125 BLKC_LOAD(PRE, t, ctx->b); \
126 pre##_eblk(&ctx->ctx, t, t); \
127 BLKC_STORE(PRE, ctx->b, t); \
128 ctx->off = PRE##_BLKSZ; \
132 /* --- @pre_cfbsetkey@ --- * \
134 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
135 * @const pre_ctx *k@ = pointer to cipher context \
139 * Use: Sets the CFB context to use a different cipher key. \
142 void pre##_cfbsetkey(pre##_cfbctx *ctx, const pre##_ctx *k) \
143 { ctx->ctx = *k; ctx->off = PRE##_BLKSZ; } \
145 /* --- @pre_cfbinit@ --- * \
147 * Arguments: @pre_cfbctx *ctx@ = pointer to cipher context \
148 * @const void *key@ = pointer to the key buffer \
149 * @size_t sz@ = size of the key \
150 * @const void *iv@ = pointer to initialization vector \
154 * Use: Initializes a CFB context ready for use. You should \
155 * ensure that the IV chosen is unique: reusing an IV will \
156 * compromise the security of at least the first block \
157 * encrypted. This is equivalent to calls to @pre_init@, \
158 * @pre_cfbsetkey@ and @pre_cfbsetiv@. \
161 void pre##_cfbinit(pre##_cfbctx *ctx, \
162 const void *key, size_t sz, \
165 static const octet zero[PRE##_BLKSZ] = { 0 }; \
167 pre##_init(&ctx->ctx, key, sz); \
168 pre##_cfbsetiv(ctx, iv ? iv : zero); \
171 /* --- @pre_cfbencrypt@ --- * \
173 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
174 * @const void *src@ = pointer to source data \
175 * @void *dest@ = pointer to destination data \
176 * @size_t sz@ = size of block to be encrypted \
180 * Use: Encrypts a block with a block cipher in CFB mode. The \
181 * input block may be arbitrary in size. CFB mode is not \
182 * sensitive to block boundaries. \
185 void pre##_cfbencrypt(pre##_cfbctx *ctx, \
186 const void *src, void *dest, \
189 const octet *s = src; \
191 unsigned off = ctx->off; \
192 uint32 t[PRE##_BLKSZ/4]; \
195 /* --- Empty blocks are trivial --- */ \
199 /* --- If I can deal with the block from my buffer, do that --- */ \
201 if (sz < PRE##_BLKSZ - off) goto small; \
203 /* --- Finish off what's left in my buffer --- */ \
205 while (off < PRE##_BLKSZ) { \
208 if (d) *d++ = ctx->b[off]; \
212 /* --- Main encryption loop --- */ \
214 BLKC_LOAD(PRE, t, ctx->b); \
217 pre##_eblk(&ctx->ctx, t, t); \
218 if (sz < PRE##_BLKSZ) break; \
219 if (s) { BLKC_XLOAD(PRE, t, s); s += PRE##_BLKSZ; } \
220 if (d) { BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ; } \
224 BLKC_STORE(PRE, ctx->b, t); \
227 /* --- Tidying up the tail end --- */ \
234 if (d) *d++ = ctx->b[off]; \
245 /* --- @pre_cfbdecrypt@ --- * \
247 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
248 * @const void *src@ = pointer to source data \
249 * @void *dest@ = pointer to destination data \
250 * @size_t sz@ = size of block to be encrypted \
254 * Use: Decrypts a block with a block cipher in CFB mode. The \
255 * input block may be arbitrary in size. CFB mode is not \
256 * sensitive to block boundaries. \
259 void pre##_cfbdecrypt(pre##_cfbctx *ctx, \
260 const void *src, void *dest, \
263 const octet *s = src; \
265 unsigned off = ctx->off; \
266 uint32 t[PRE##_BLKSZ/4], u[PRE##_BLKSZ/4]; \
269 /* --- Empty blocks are trivial --- */ \
273 /* --- If I can deal with the block from my buffer, do that --- */ \
275 if (sz < PRE##_BLKSZ - off) goto small; \
277 /* --- Finish off what's left in my buffer --- */ \
279 while (off < PRE##_BLKSZ) \
280 { y = *s++; *d++ = ctx->b[off] ^ y; ctx->b[off++] = y; sz--; } \
282 /* --- Main encryption loop --- */ \
284 BLKC_LOAD(PRE, t, ctx->b); \
287 pre##_eblk(&ctx->ctx, t, t); \
288 if (sz < PRE##_BLKSZ) break; \
289 BLKC_LOAD(PRE, u, s); s += PRE##_BLKSZ; \
290 BLKC_XSTORE(PRE, d, t, u); d += PRE##_BLKSZ; \
291 BLKC_MOVE(PRE, t, u); \
295 BLKC_STORE(PRE, ctx->b, t); \
298 /* --- Tidying up the tail end --- */ \
302 do { y = *s++; *d++ = ctx->b[off] ^ y; ctx->b[off++] = y; sz--; } \
312 /* --- Generic cipher interface --- */ \
314 static const gcipher_ops gops; \
316 typedef struct gctx { \
321 static gcipher *ginit(const void *k, size_t sz) \
323 gctx *g = S_CREATE(gctx); \
325 pre##_cfbinit(&g->k, k, sz, 0); \
329 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
330 { gctx *g = (gctx *)c; pre##_cfbencrypt(&g->k, s, t, sz); } \
332 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
333 { gctx *g = (gctx *)c; pre##_cfbdecrypt(&g->k, s, t, sz); } \
335 static void gdestroy(gcipher *c) \
336 { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); } \
338 static void gsetiv(gcipher *c, const void *iv) \
339 { gctx *g = (gctx *)c; pre##_cfbsetiv(&g->k, iv); } \
341 static void gbdry(gcipher *c) \
342 { gctx *g = (gctx *)c; pre##_cfbbdry(&g->k); } \
344 static const gcipher_ops gops = { \
346 gencrypt, gdecrypt, gdestroy, gsetiv, gbdry \
349 const gccipher pre##_cfb = { \
350 name "-cfb", pre##_keysz, PRE##_BLKSZ, \
354 CFB_TESTX(PRE, pre, name, fname)
356 /*----- Test rig ----------------------------------------------------------*/
358 #define CFB_TEST(PRE, pre) CFB_TESTX(PRE, pre, #pre, #pre)
362 #include "modes-test.h"
364 /* --- @CFB_TEST@ --- *
366 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
368 * Use: Standard test rig for CFB functions.
371 #define CFB_TESTX(PRE, pre, name, fname) \
373 static pre##_ctx key; \
374 static pre##_cfbctx ctx; \
376 static void pre##_cfb_test_setup(const octet *k, size_t ksz) \
377 { pre##_init(&key, k, ksz); pre##_cfbsetkey(&ctx, &key); } \
379 static void pre##_cfb_test_reset(const octet *iv) \
380 { pre##_cfbsetiv(&ctx, iv); } \
382 static void pre##_cfb_test_enc(const octet *s, octet *d, size_t sz) \
383 { pre##_cfbencrypt(&ctx, s, d, sz); } \
385 static void pre##_cfb_test_dec(const octet *s, octet *d, size_t sz) \
386 { pre##_cfbdecrypt(&ctx, s, d, sz); } \
388 int main(int argc, char *argv[]) \
390 return test_encmode(fname "-cfb", PRE##_KEYSZ, PRE##_BLKSZ, 1, 0, \
391 pre##_cfb_test_setup, pre##_cfb_test_reset, \
392 pre##_cfb_test_enc, pre##_cfb_test_dec, \
397 # define CFB_TESTX(PRE, pre, name, fname)
400 /*----- That's all, folks -------------------------------------------------*/