chiark / gitweb /
Fix daft error in the comment for @gfshare_get@.
[catacomb] / cfb-def.h
1 /* -*-c-*-
2  *
3  * $Id: cfb-def.h,v 1.2 2000/06/17 10:50:39 mdw Exp $
4  *
5  * Definitions for ciphertext feedback mode
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Catacomb.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: cfb-def.h,v $
33  * Revision 1.2  2000/06/17 10:50:39  mdw
34  * Use secure arena for memory allocation.  Rearrange setiv slightly.
35  *
36  * Revision 1.1  1999/12/10 23:16:39  mdw
37  * Split mode macros into interface and implementation.
38  *
39  */
40
41 #ifndef CATACOMB_CFB_DEF_H
42 #define CATACOMB_CFB_DEF_H
43
44 #ifdef __cplusplus
45   extern "C" {
46 #endif
47
48 /*----- Header files ------------------------------------------------------*/
49
50 #include <string.h>
51
52 #include <mLib/bits.h>
53 #include <mLib/sub.h>
54
55 #ifndef CATACOMB_ARENA_H
56 #  include "arena.h"
57 #endif
58
59 #ifndef CATACOMB_BLKC_H
60 #  include "blkc.h"
61 #endif
62
63 #ifndef CATACOMB_GCIPHER_H
64 #  include "gcipher.h"
65 #endif
66
67 #ifndef CATACOMB_PARANOIA_H
68 #  include "paranoia.h"
69 #endif
70
71 #ifndef CATACOMB_PARANOIA_H
72 #  include "paranoia.h"
73 #endif
74
75 /*----- Macros ------------------------------------------------------------*/
76
77 /* --- @CFB_DEF@ --- *
78  *
79  * Arguments:   @PRE@, @pre@ = prefixes for the underlying block cipher
80  *
81  * Use:         Creates an implementation for CFB mode.
82  */
83
84 #define CFB_DEF(PRE, pre)                                               \
85                                                                         \
86 /* --- @pre_cfbgetiv@ --- *                                             \
87  *                                                                      \
88  * Arguments:   @const pre_cfbctx *ctx@ = pointer to CFB context block  \
89  *              @void *iv#@ = pointer to output data block              \
90  *                                                                      \
91  * Returns:     ---                                                     \
92  *                                                                      \
93  * Use:         Reads the currently set IV.  Reading and setting an IV  \
94  *              is not transparent to the cipher.  It will add a `step' \
95  *              which must be matched by a similar operation during     \
96  *              decryption.                                             \
97  */                                                                     \
98                                                                         \
99 void pre##_cfbgetiv(const pre##_cfbctx *ctx, void *iv)                  \
100 {                                                                       \
101   octet *p = iv;                                                        \
102   unsigned off = ctx->off;                                              \
103   unsigned rest = PRE##_BLKSZ - off;                                    \
104   memcpy(p, ctx->iv + off, rest);                                       \
105   memcpy(p + rest, ctx->iv, off);                                       \
106 }                                                                       \
107                                                                         \
108 /* --- @pre_cfbsetiv@ --- *                                             \
109  *                                                                      \
110  * Arguments:   @pre_cfbctx *ctx@ = pointer to CFB context block        \
111  *              @cnost void *iv@ = pointer to IV to set                 \
112  *                                                                      \
113  * Returns:     ---                                                     \
114  *                                                                      \
115  * Use:         Sets the IV to use for subsequent encryption.           \
116  */                                                                     \
117                                                                         \
118 void pre##_cfbsetiv(pre##_cfbctx *ctx, const void *iv)                  \
119 {                                                                       \
120   memcpy(ctx->iv, iv, PRE##_BLKSZ);                                     \
121   ctx->off = PRE##_BLKSZ;                                               \
122 }                                                                       \
123                                                                         \
124 /* --- @pre_cfbbdry@ --- *                                              \
125  *                                                                      \
126  * Arguments:   @pre_cfbctx *ctx@ = pointer to CFB context block        \
127  *                                                                      \
128  * Returns:     ---                                                     \
129  *                                                                      \
130  * Use:         Inserts a boundary during encryption.  Successful       \
131  *              decryption must place a similar boundary.               \
132  */                                                                     \
133                                                                         \
134 void pre##_cfbbdry(pre##_cfbctx *ctx)                                   \
135 {                                                                       \
136   uint32 niv[PRE##_BLKSZ / 4];                                          \
137   BLKC_LOAD(PRE, niv, ctx->iv);                                         \
138   pre##_eblk(&ctx->ctx, niv, niv);                                      \
139   BLKC_STORE(PRE, ctx->iv, niv);                                        \
140   ctx->off = PRE##_BLKSZ;                                               \
141   BURN(niv);                                                            \
142 }                                                                       \
143                                                                         \
144 /* --- @pre_cfbsetkey@ --- *                                            \
145  *                                                                      \
146  * Arguments:   @pre_cfbctx *ctx@ = pointer to CFB context block        \
147  *              @const pre_ctx *k@ = pointer to cipher context          \
148  *                                                                      \
149  * Returns:     ---                                                     \
150  *                                                                      \
151  * Use:         Sets the CFB context to use a different cipher key.     \
152  */                                                                     \
153                                                                         \
154 void pre##_cfbsetkey(pre##_cfbctx *ctx, const pre##_ctx *k)             \
155 {                                                                       \
156   ctx->ctx = *k;                                                        \
157   ctx->off = PRE##_BLKSZ;                                               \
158 }                                                                       \
159                                                                         \
160 /* --- @pre_cfbinit@ --- *                                              \
161  *                                                                      \
162  * Arguments:   @pre_cfbctx *ctx@ = pointer to cipher context           \
163  *              @const void *key@ = pointer to the key buffer           \
164  *              @size_t sz@ = size of the key                           \
165  *              @const void *iv@ = pointer to initialization vector     \
166  *                                                                      \
167  * Returns:     ---                                                     \
168  *                                                                      \
169  * Use:         Initializes a CFB context ready for use.  You should    \
170  *              ensure that the IV chosen is unique: reusing an IV will \
171  *              compromise the security of at least the first block     \
172  *              encrypted.  This is equivalent to calls to @pre_init@,  \
173  *              @pre_cfbsetkey@ and @pre_cfbsetiv@.                     \
174  */                                                                     \
175                                                                         \
176 void pre##_cfbinit(pre##_cfbctx *ctx,                                   \
177                      const void *key, size_t sz,                        \
178                      const void *iv)                                    \
179 {                                                                       \
180   static octet zero[PRE##_BLKSZ] = { 0 };                               \
181   pre##_init(&ctx->ctx, key, sz);                                       \
182   pre##_cfbsetiv(ctx, iv ? iv : zero);                                  \
183 }                                                                       \
184                                                                         \
185 /* --- @pre_cfbencrypt@ --- *                                           \
186  *                                                                      \
187  * Arguments:   @pre_cfbctx *ctx@ = pointer to CFB context block        \
188  *              @const void *src@ = pointer to source data              \
189  *              @void *dest@ = pointer to destination data              \
190  *              @size_t sz@ = size of block to be encrypted             \
191  *                                                                      \
192  * Returns:     ---                                                     \
193  *                                                                      \
194  * Use:         Encrypts a block with a block cipher in CFB mode.  The  \
195  *              input block may be arbitrary in size.  CFB mode is not  \
196  *              sensitive to block boundaries.                          \
197  */                                                                     \
198                                                                         \
199 void pre##_cfbencrypt(pre##_cfbctx *ctx,                                \
200                         const void *src, void *dest,                    \
201                         size_t sz)                                      \
202 {                                                                       \
203   const octet *s = src;                                                 \
204   octet *d = dest;                                                      \
205   unsigned off = ctx->off;                                              \
206                                                                         \
207   /* --- Empty blocks are trivial --- */                                \
208                                                                         \
209   if (!sz)                                                              \
210     return;                                                             \
211                                                                         \
212   /* --- If I can deal with the block from my buffer, do that --- */    \
213                                                                         \
214   if (sz < PRE##_BLKSZ - off)                                           \
215     goto small;                                                         \
216                                                                         \
217   /* --- Finish off what's left in my buffer --- */                     \
218                                                                         \
219   while (off < PRE##_BLKSZ) {                                           \
220     register octet x = *s++;                                            \
221     *d++ = ctx->iv[off++] ^= x;                                         \
222     sz--;                                                               \
223   }                                                                     \
224                                                                         \
225   /* --- Main encryption loop --- */                                    \
226                                                                         \
227   {                                                                     \
228     uint32 iv[PRE##_BLKSZ / 4];                                         \
229     BLKC_LOAD(PRE, iv, ctx->iv);                                        \
230                                                                         \
231     for (;;) {                                                          \
232       pre##_eblk(&ctx->ctx, iv, iv);                                    \
233       if (sz < PRE##_BLKSZ)                                             \
234         break;                                                          \
235       BLKC_XLOAD(PRE, iv, s);                                           \
236       BLKC_STORE(PRE, d, iv);                                           \
237       s += PRE##_BLKSZ;                                                 \
238       d += PRE##_BLKSZ;                                                 \
239       sz -= PRE##_BLKSZ;                                                \
240     }                                                                   \
241     off = 0;                                                            \
242     BLKC_STORE(PRE, ctx->iv, iv);                                       \
243   }                                                                     \
244                                                                         \
245   /* --- Tidying up the tail end --- */                                 \
246                                                                         \
247   if (sz) {                                                             \
248   small:                                                                \
249     do {                                                                \
250       register octet x = *s++;                                          \
251       *d++ = ctx->iv[off++] ^= x;                                       \
252       sz--;                                                             \
253     } while (sz);                                                       \
254   }                                                                     \
255                                                                         \
256   /* --- Done --- */                                                    \
257                                                                         \
258   ctx->off = off;                                                       \
259   return;                                                               \
260 }                                                                       \
261                                                                         \
262 /* --- @pre_cfbdecrypt@ --- *                                           \
263  *                                                                      \
264  * Arguments:   @pre_cfbctx *ctx@ = pointer to CFB context block        \
265  *              @const void *src@ = pointer to source data              \
266  *              @void *dest@ = pointer to destination data              \
267  *              @size_t sz@ = size of block to be encrypted             \
268  *                                                                      \
269  * Returns:     ---                                                     \
270  *                                                                      \
271  * Use:         Decrypts a block with a block cipher in CFB mode.  The  \
272  *              input block may be arbitrary in size.  CFB mode is not  \
273  *              sensitive to block boundaries.                          \
274  */                                                                     \
275                                                                         \
276 void pre##_cfbdecrypt(pre##_cfbctx *ctx,                                \
277                         const void *src, void *dest,                    \
278                         size_t sz)                                      \
279 {                                                                       \
280   const octet *s = src;                                                 \
281   octet *d = dest;                                                      \
282   unsigned off = ctx->off;                                              \
283                                                                         \
284   /* --- Empty blocks are trivial --- */                                \
285                                                                         \
286   if (!sz)                                                              \
287     return;                                                             \
288                                                                         \
289   /* --- If I can deal with the block from my buffer, do that --- */    \
290                                                                         \
291   if (sz < PRE##_BLKSZ - off)                                           \
292     goto small;                                                         \
293                                                                         \
294   /* --- Finish off what's left in my buffer --- */                     \
295                                                                         \
296   while (off < PRE##_BLKSZ) {                                           \
297     register octet x = *s++;                                            \
298     *d++ = ctx->iv[off] ^ x;                                            \
299     ctx->iv[off++] = x;                                                 \
300     sz--;                                                               \
301   }                                                                     \
302                                                                         \
303   /* --- Main encryption loop --- */                                    \
304                                                                         \
305   {                                                                     \
306     uint32 iv[PRE##_BLKSZ / 4];                                         \
307     BLKC_LOAD(PRE, iv, ctx->iv);                                        \
308                                                                         \
309     for (;;) {                                                          \
310       uint32 x[PRE##_BLKSZ / 4];                                        \
311       pre##_eblk(&ctx->ctx, iv, iv);                                    \
312       if (sz < PRE##_BLKSZ)                                             \
313         break;                                                          \
314       BLKC_LOAD(PRE, x, s);                                             \
315       BLKC_XSTORE(PRE, d, iv, x);                                       \
316       BLKC_MOVE(PRE, iv, x);                                            \
317       s += PRE##_BLKSZ;                                                 \
318       d += PRE##_BLKSZ;                                                 \
319       sz -= PRE##_BLKSZ;                                                \
320     }                                                                   \
321     off = 0;                                                            \
322     BLKC_STORE(PRE, ctx->iv, iv);                                       \
323   }                                                                     \
324                                                                         \
325   /* --- Tidying up the tail end --- */                                 \
326                                                                         \
327   if (sz) {                                                             \
328   small:                                                                \
329     do {                                                                \
330       register octet x = *s++;                                          \
331       *d++ = ctx->iv[off] ^ x;                                          \
332       ctx->iv[off++] = x;                                               \
333       sz--;                                                             \
334     } while (sz);                                                       \
335   }                                                                     \
336                                                                         \
337   /* --- Done --- */                                                    \
338                                                                         \
339   ctx->off = off;                                                       \
340   return;                                                               \
341 }                                                                       \
342                                                                         \
343 /* --- Generic cipher interface --- */                                  \
344                                                                         \
345 static const gcipher_ops gops;                                          \
346                                                                         \
347 typedef struct gctx {                                                   \
348   gcipher c;                                                            \
349   pre##_cfbctx k;                                                       \
350 } gctx;                                                                 \
351                                                                         \
352 static gcipher *ginit(const void *k, size_t sz)                         \
353 {                                                                       \
354   gctx *g = S_CREATE(gctx);                                             \
355   g->c.ops = &gops;                                                     \
356   pre##_cfbinit(&g->k, k, sz, 0);                                       \
357   return (&g->c);                                                       \
358 }                                                                       \
359                                                                         \
360 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz)     \
361 {                                                                       \
362   gctx *g = (gctx *)c;                                                  \
363   pre##_cfbencrypt(&g->k, s, t, sz);                                    \
364 }                                                                       \
365                                                                         \
366 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz)     \
367 {                                                                       \
368   gctx *g = (gctx *)c;                                                  \
369   pre##_cfbdecrypt(&g->k, s, t, sz);                                    \
370 }                                                                       \
371                                                                         \
372 static void gdestroy(gcipher *c)                                        \
373 {                                                                       \
374   gctx *g = (gctx *)c;                                                  \
375   BURN(*g);                                                             \
376   S_DESTROY(g);                                                         \
377 }                                                                       \
378                                                                         \
379 static void gsetiv(gcipher *c, const void *iv)                          \
380 {                                                                       \
381   gctx *g = (gctx *)c;                                                  \
382   pre##_cfbsetiv(&g->k, iv);                                            \
383 }                                                                       \
384                                                                         \
385 static void gbdry(gcipher *c)                                           \
386 {                                                                       \
387   gctx *g = (gctx *)c;                                                  \
388   pre##_cfbbdry(&g->k);                                                 \
389 }                                                                       \
390                                                                         \
391 static const gcipher_ops gops = {                                       \
392   &pre##_cfb,                                                           \
393   gencrypt, gdecrypt, gdestroy, gsetiv, gbdry                           \
394 };                                                                      \
395                                                                         \
396 const gccipher pre##_cfb = {                                            \
397   #pre "-cfb", pre##_keysz, PRE##_BLKSZ,                                \
398   ginit                                                                 \
399 };                                                                      \
400                                                                         \
401 CFB_TEST(PRE, pre)
402
403 /*----- Test rig ----------------------------------------------------------*/
404
405 #ifdef TEST_RIG
406
407 #include <stdio.h>
408
409 #include "daftstory.h"
410
411 /* --- @CFB_TEST@ --- *
412  *
413  * Arguments:   @PRE@, @pre@ = prefixes for block cipher definitions
414  *
415  * Use:         Standard test rig for CFB functions.
416  */
417
418 #define CFB_TEST(PRE, pre)                                              \
419                                                                         \
420 /* --- Initial plaintext for the test --- */                            \
421                                                                         \
422 static const octet text[] = TEXT;                                       \
423                                                                         \
424 /* --- Key and IV to use --- */                                         \
425                                                                         \
426 static const octet key[] = KEY;                                         \
427 static const octet iv[] = IV;                                           \
428                                                                         \
429 /* --- Buffers for encryption and decryption output --- */              \
430                                                                         \
431 static octet ct[sizeof(text)];                                          \
432 static octet pt[sizeof(text)];                                          \
433                                                                         \
434 static void hexdump(const octet *p, size_t sz)                          \
435 {                                                                       \
436   const octet *q = p + sz;                                              \
437   for (sz = 0; p < q; p++, sz++) {                                      \
438     printf("%02x", *p);                                                 \
439     if ((sz + 1) % PRE##_BLKSZ == 0)                                    \
440       putchar(':');                                                     \
441   }                                                                     \
442 }                                                                       \
443                                                                         \
444 int main(void)                                                          \
445 {                                                                       \
446   size_t sz = 0, rest;                                                  \
447   pre##_cfbctx ctx;                                                     \
448   int status = 0;                                                       \
449   int done = 0;                                                         \
450   pre##_ctx k;                                                          \
451                                                                         \
452   size_t keysz = PRE##_KEYSZ ?                                          \
453     PRE##_KEYSZ : strlen((const char *)key);                            \
454                                                                         \
455   fputs(#pre "-cfb: ", stdout);                                         \
456                                                                         \
457   pre##_init(&k, key, keysz);                                           \
458   pre##_cfbsetkey(&ctx, &k);                                            \
459                                                                         \
460   while (sz <= sizeof(text)) {                                          \
461     rest = sizeof(text) - sz;                                           \
462     memcpy(ct, text, sizeof(text));                                     \
463     pre##_cfbsetiv(&ctx, iv);                                           \
464     pre##_cfbencrypt(&ctx, ct, ct, sz);                                 \
465     pre##_cfbencrypt(&ctx, ct + sz, ct + sz, rest);                     \
466     memcpy(pt, ct, sizeof(text));                                       \
467     pre##_cfbsetiv(&ctx, iv);                                           \
468     pre##_cfbdecrypt(&ctx, pt, pt, rest);                               \
469     pre##_cfbdecrypt(&ctx, pt + rest, pt + rest, sz);                   \
470     if (memcmp(pt, text, sizeof(text)) == 0) {                          \
471       done++;                                                           \
472       if (sizeof(text) < 40 || done % 8 == 0)                           \
473         fputc('.', stdout);                                             \
474       if (done % 480 == 0)                                              \
475         fputs("\n\t", stdout);                                          \
476       fflush(stdout);                                                   \
477     } else {                                                            \
478       printf("\nError (sz = %lu)\n", (unsigned long)sz);                \
479       status = 1;                                                       \
480       printf("\tplaintext      = "); hexdump(text, sz);                 \
481         printf(", "); hexdump(text + sz, rest);                         \
482         fputc('\n', stdout);                                            \
483       printf("\tciphertext     = "); hexdump(ct, sz);                   \
484         printf(", "); hexdump(ct + sz, rest);                           \
485         fputc('\n', stdout);                                            \
486       printf("\trecovered text = "); hexdump(pt, sz);                   \
487         printf(", "); hexdump(pt + sz, rest);                           \
488         fputc('\n', stdout);                                            \
489       fputc('\n', stdout);                                              \
490     }                                                                   \
491     if (sz < 63)                                                        \
492       sz++;                                                             \
493     else                                                                \
494       sz += 9;                                                          \
495   }                                                                     \
496                                                                         \
497   fputs(status ? " failed\n" : " ok\n", stdout);                        \
498   return (status);                                                      \
499 }
500
501 #else
502 #  define CFB_TEST(PRE, pre)
503 #endif
504
505 /*----- That's all, folks -------------------------------------------------*/
506
507 #ifdef __cplusplus
508   }
509 #endif
510
511 #endif