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 #ifndef CATACOMB_RSVR_H
66 /*----- Macros ------------------------------------------------------------*/
68 /* --- @CFB_DEF@ --- *
70 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
72 * Use: Creates an implementation for CFB mode.
75 #define CFB_DEF(PRE, pre) CFB_DEFX(PRE, pre, #pre, #pre)
77 #define CFB_DEFX(PRE, pre, name, fname) \
79 /* --- @pre_cfbgetiv@ --- * \
81 * Arguments: @const pre_cfbctx *ctx@ = pointer to CFB context block \
82 * @void *iv@ = pointer to output data block \
86 * Use: Reads the currently set IV. Reading and setting an IV \
87 * is not transparent to the cipher. It will add a `step' \
88 * which must be matched by a similar operation during \
92 void pre##_cfbgetiv(const pre##_cfbctx *ctx, void *iv) \
95 unsigned off = ctx->off; \
96 unsigned rest = PRE##_BLKSZ - off; \
98 memcpy(p, ctx->b + off, rest); \
99 memcpy(p + rest, ctx->b, off); \
102 /* --- @pre_cfbsetiv@ --- * \
104 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
105 * @cnost void *iv@ = pointer to IV to set \
109 * Use: Sets the IV to use for subsequent encryption. \
112 void pre##_cfbsetiv(pre##_cfbctx *ctx, const void *iv) \
113 { memcpy(ctx->b, iv, PRE##_BLKSZ); ctx->off = 0; } \
115 /* --- @pre_cfbbdry@ --- * \
117 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
121 * Use: Inserts a boundary during encryption. Successful \
122 * decryption must place a similar boundary. \
125 void pre##_cfbbdry(pre##_cfbctx *ctx) \
127 uint32 t[PRE##_BLKSZ/4]; \
129 BLKC_LOAD(PRE, t, ctx->b); \
130 pre##_eblk(&ctx->ctx, t, t); \
131 BLKC_STORE(PRE, ctx->b, t); \
136 /* --- @pre_cfbsetkey@ --- * \
138 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
139 * @const pre_ctx *k@ = pointer to cipher context \
143 * Use: Sets the CFB context to use a different cipher key. \
146 void pre##_cfbsetkey(pre##_cfbctx *ctx, const pre##_ctx *k) \
147 { ctx->ctx = *k; ctx->off = 0; } \
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 }; \
171 pre##_init(&ctx->ctx, key, sz); \
172 pre##_cfbsetiv(ctx, iv ? iv : zero); \
175 /* --- @pre_cfbencrypt@ --- * \
177 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
178 * @const void *src@ = pointer to source data \
179 * @void *dest@ = pointer to destination data \
180 * @size_t sz@ = size of block to be encrypted \
184 * Use: Encrypts a block with a block cipher in CFB mode. The \
185 * input block may be arbitrary in size. CFB mode is not \
186 * sensitive to block boundaries. \
189 static const rsvr_policy pre##_cfbpolicy = { 0, PRE##_BLKSZ, PRE##_BLKSZ }; \
191 void pre##_cfbencrypt(pre##_cfbctx *ctx, \
192 const void *src, void *dest, \
196 const octet *s = src; \
197 octet *d = dest, *p; \
198 uint32 t[PRE##_BLKSZ/4]; \
201 /* Construct a plan and prepare to follow through. */ \
202 rsvr_mkplan(&plan, &pre##_cfbpolicy, ctx->off, sz); \
203 BLKC_LOAD(PRE, t, ctx->b); \
205 /* Initial portion, fulfilled from the buffer. If the chunk is small \
206 * enough, then this will be the only portion. If the buffer is \
207 * currently empty, then we must prepare it. \
211 pre##_eblk(&ctx->ctx, t, t); \
212 BLKC_STORE(PRE, ctx->b, t); \
214 p = ctx->b + ctx->off; ctx->off += plan.head; \
215 if (s) while (plan.head--) { y = *s++ ^ *p; *p++ = y; if (d) *d++ = y; } \
216 else if (d) { memcpy(d, p, plan.head); d += plan.head; } \
217 BLKC_LOAD(PRE, t, ctx->b); \
220 /* If the buffer is all used, then reset it ready for next time. */ \
221 ctx->off -= plan.from_rsvr; \
223 /* Handle multiple whole blocks. */ \
225 if (!s) while (plan.from_input) { \
226 pre##_eblk(&ctx->ctx, t, t); \
227 plan.from_input -= PRE##_BLKSZ; \
228 } else while (plan.from_input) { \
229 pre##_eblk(&ctx->ctx, t, t); \
230 BLKC_XLOAD(PRE, t, s); s += PRE##_BLKSZ; \
231 plan.from_input -= PRE##_BLKSZ; \
234 if (!s) while (plan.from_input) { \
235 pre##_eblk(&ctx->ctx, t, t); \
236 BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ; \
237 plan.from_input -= PRE##_BLKSZ; \
238 } else while (plan.from_input) { \
239 pre##_eblk(&ctx->ctx, t, t); \
240 BLKC_XLOAD(PRE, t, s); s += PRE##_BLKSZ; \
241 BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ; \
242 plan.from_input -= PRE##_BLKSZ; \
246 /* Final portion. Note that the buffer must be empty if there is a \
247 * tail, since otherwise the input data would have been part of the \
248 * head portion instad. */ \
250 BLKC_STORE(PRE, ctx->b, t); \
252 pre##_eblk(&ctx->ctx, t, t); \
253 BLKC_STORE(PRE, ctx->b, t); \
254 p = ctx->b; ctx->off += plan.tail; \
255 if (s) while (plan.tail--) { y = *s++ ^ *p; *p++ = y; if (d) *d++ = y; } \
256 else if (d) { memcpy(d, p, plan.tail); d += plan.tail; } \
260 /* --- @pre_cfbdecrypt@ --- * \
262 * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \
263 * @const void *src@ = pointer to source data \
264 * @void *dest@ = pointer to destination data \
265 * @size_t sz@ = size of block to be encrypted \
269 * Use: Decrypts a block with a block cipher in CFB mode. The \
270 * input block may be arbitrary in size. CFB mode is not \
271 * sensitive to block boundaries. \
274 void pre##_cfbdecrypt(pre##_cfbctx *ctx, \
275 const void *src, void *dest, \
279 const octet *s = src; \
280 octet *d = dest, *p; \
281 uint32 t[PRE##_BLKSZ/4], u[PRE##_BLKSZ/4]; \
284 /* Construct a plan and prepare to follow through. */ \
285 rsvr_mkplan(&plan, &pre##_cfbpolicy, ctx->off, sz); \
286 BLKC_LOAD(PRE, t, ctx->b); \
288 /* Initial portion, fulfilled from the buffer. If the chunk is small \
289 * enough, then this will be the only portion. If the buffer is \
290 * currently empty, then we must prepare it. \
294 pre##_eblk(&ctx->ctx, t, t); \
295 BLKC_STORE(PRE, ctx->b, t); \
297 p = ctx->b + ctx->off; \
298 ctx->off += plan.head; \
299 while (plan.head--) { y = *s++; *d++ = y ^ *p; *p++ = y; } \
300 BLKC_LOAD(PRE, t, ctx->b); \
303 /* If the buffer is all used, then reset it ready for next time. */ \
304 ctx->off -= plan.from_rsvr; \
306 /* Handle multiple whole blocks. */ \
307 while (plan.from_input) { \
308 pre##_eblk(&ctx->ctx, t, t); \
309 BLKC_LOAD(PRE, u, s); s += PRE##_BLKSZ; \
310 BLKC_XSTORE(PRE, d, t, u); d += PRE##_BLKSZ; \
311 BLKC_MOVE(PRE, t, u); \
312 plan.from_input -= PRE##_BLKSZ; \
315 /* Final portion. Note that the buffer must be empty if there is a \
316 * tail, since otherwise the input data would have been part of the \
317 * head portion instad. */ \
319 BLKC_STORE(PRE, ctx->b, t); \
321 pre##_eblk(&ctx->ctx, t, t); \
322 BLKC_STORE(PRE, ctx->b, t); \
324 ctx->off += plan.tail; \
325 while (plan.tail--) { y = *s++; *d++ = y ^ *p; *p++ = y; } \
329 /* --- Generic cipher interface --- */ \
331 static const gcipher_ops gops; \
333 typedef struct gctx { \
338 static gcipher *ginit(const void *k, size_t sz) \
340 gctx *g = S_CREATE(gctx); \
342 pre##_cfbinit(&g->k, k, sz, 0); \
346 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
347 { gctx *g = (gctx *)c; pre##_cfbencrypt(&g->k, s, t, sz); } \
349 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
350 { gctx *g = (gctx *)c; pre##_cfbdecrypt(&g->k, s, t, sz); } \
352 static void gdestroy(gcipher *c) \
353 { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); } \
355 static void gsetiv(gcipher *c, const void *iv) \
356 { gctx *g = (gctx *)c; pre##_cfbsetiv(&g->k, iv); } \
358 static void gbdry(gcipher *c) \
359 { gctx *g = (gctx *)c; pre##_cfbbdry(&g->k); } \
361 static const gcipher_ops gops = { \
363 gencrypt, gdecrypt, gdestroy, gsetiv, gbdry \
366 const gccipher pre##_cfb = { \
367 name "-cfb", pre##_keysz, PRE##_BLKSZ, \
371 CFB_TESTX(PRE, pre, name, fname)
373 /*----- Test rig ----------------------------------------------------------*/
375 #define CFB_TEST(PRE, pre) CFB_TESTX(PRE, pre, #pre, #pre)
379 #include "modes-test.h"
381 /* --- @CFB_TEST@ --- *
383 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
385 * Use: Standard test rig for CFB functions.
388 #define CFB_TESTX(PRE, pre, name, fname) \
390 static pre##_ctx key; \
391 static pre##_cfbctx ctx; \
393 static void pre##_cfb_test_setup(const octet *k, size_t ksz) \
394 { pre##_init(&key, k, ksz); pre##_cfbsetkey(&ctx, &key); } \
396 static void pre##_cfb_test_reset(const octet *iv) \
397 { pre##_cfbsetiv(&ctx, iv); } \
399 static void pre##_cfb_test_enc(const octet *s, octet *d, size_t sz) \
400 { pre##_cfbencrypt(&ctx, s, d, sz); } \
402 static void pre##_cfb_test_dec(const octet *s, octet *d, size_t sz) \
403 { pre##_cfbdecrypt(&ctx, s, d, sz); } \
405 int main(int argc, char *argv[]) \
407 return test_encmode(fname "-cfb", PRE##_KEYSZ, PRE##_BLKSZ, 1, 0, \
408 pre##_cfb_test_setup, pre##_cfb_test_reset, \
409 pre##_cfb_test_enc, pre##_cfb_test_dec, \
414 # define CFB_TESTX(PRE, pre, name, fname)
417 /*----- That's all, folks -------------------------------------------------*/