chiark / gitweb /
Add support for seeding from a generic pseudorandom source.
[catacomb] / ofb-def.h
1 /* -*-c-*-
2  *
3  * $Id: ofb-def.h,v 1.2 1999/12/13 15:34:01 mdw Exp $
4  *
5  * Definitions for output 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: ofb-def.h,v $
33  * Revision 1.2  1999/12/13 15:34:01  mdw
34  * Add support for seeding from a generic pseudorandom source.
35  *
36  * Revision 1.1  1999/12/10 23:16:40  mdw
37  * Split mode macros into interface and implementation.
38  *
39  */
40
41 #ifndef CATACOMB_OFB_DEF_H
42 #define CATACOMB_OFB_DEF_H
43
44 #ifdef __cplusplus
45   extern "C" {
46 #endif
47
48 /*----- Header files ------------------------------------------------------*/
49
50 #include <stdarg.h>
51 #include <string.h>
52
53 #include <mLib/bits.h>
54 #include <mLib/sub.h>
55
56 #ifndef CATACOMB_BLKC_H
57 #  include "blkc.h"
58 #endif
59
60 #ifndef CATACOMB_GCIPHER_H
61 #  include "gcipher.h"
62 #endif
63
64 #ifndef CATACOMB_PARANOIA_H
65 #  include "paranoia.h"
66 #endif
67
68 /*----- Macros ------------------------------------------------------------*/
69
70 /* --- @OFB_DEF@ --- *
71  *
72  * Arguments:   @PRE@, @pre@ = prefixes for the underlying block cipher
73  *
74  * Use:         Creates definitions for output feedback mode.
75  */
76
77 #define OFB_DEF(PRE, pre)                                               \
78                                                                         \
79 /* --- @pre_ofbgetiv@ --- *                                             \
80  *                                                                      \
81  * Arguments:   @const pre_ofbctx *ctx@ = pointer to OFB context block  \
82  *              @void *iv#@ = pointer to output data block              \
83  *                                                                      \
84  * Returns:     ---                                                     \
85  *                                                                      \
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     \
89  *              decryption.                                             \
90  */                                                                     \
91                                                                         \
92 void pre##_ofbgetiv(const pre##_ofbctx *ctx, void *iv)                  \
93 {                                                                       \
94   octet *p = iv;                                                        \
95   int off = ctx->off;                                                   \
96   int rest = PRE##_BLKSZ - off;                                         \
97   memcpy(p, ctx->iv + off, rest);                                       \
98   memcpy(p + rest, ctx->iv, off);                                       \
99 }                                                                       \
100                                                                         \
101 /* --- @pre_ofbsetiv@ --- *                                             \
102  *                                                                      \
103  * Arguments:   @pre_ofbctx *ctx@ = pointer to OFB context block        \
104  *              @cnost void *iv@ = pointer to IV to set                 \
105  *                                                                      \
106  * Returns:     ---                                                     \
107  *                                                                      \
108  * Use:         Sets the IV to use for subsequent encryption.           \
109  */                                                                     \
110                                                                         \
111 void pre##_ofbsetiv(pre##_ofbctx *ctx, const void *iv)                  \
112 {                                                                       \
113   uint32 niv[PRE##_BLKSZ / 4];                                          \
114   BLKC_LOAD(PRE, niv, iv);                                              \
115   pre##_eblk(&ctx->ctx, niv, niv);                                      \
116   BLKC_STORE(PRE, ctx->iv, niv);                                        \
117   ctx->off = 0;                                                         \
118 }                                                                       \
119                                                                         \
120 /* --- @pre_ofbbdry@ --- *                                              \
121  *                                                                      \
122  * Arguments:   @pre_ofbctx *ctx@ = pointer to OFB context block        \
123  *                                                                      \
124  * Returns:     ---                                                     \
125  *                                                                      \
126  * Use:         Inserts a boundary during encryption.  Successful       \
127  *              decryption must place a similar boundary.               \
128  */                                                                     \
129                                                                         \
130 void pre##_ofbbdry(pre##_ofbctx *ctx)                                   \
131 {                                                                       \
132   octet iv[PRE##_BLKSZ];                                                \
133   pre##_ofbgetiv(ctx, iv);                                              \
134   pre##_ofbsetiv(ctx, iv);                                              \
135   BURN(iv);                                                             \
136 }                                                                       \
137                                                                         \
138 /* --- @pre_ofbsetkey@ --- *                                            \
139  *                                                                      \
140  * Arguments:   @pre_ofbctx *ctx@ = pointer to OFB context block        \
141  *              @const pre_ctx *k@ = pointer to cipher context          \
142  *                                                                      \
143  * Returns:     ---                                                     \
144  *                                                                      \
145  * Use:         Sets the OFB context to use a different cipher key.     \
146  */                                                                     \
147                                                                         \
148 void pre##_ofbsetkey(pre##_ofbctx *ctx, const pre##_ctx *k)             \
149 {                                                                       \
150   ctx->ctx = *k;                                                        \
151 }                                                                       \
152                                                                         \
153 /* --- @pre_ofbinit@ --- *                                              \
154  *                                                                      \
155  * Arguments:   @pre_ofbctx *ctx@ = pointer to cipher context           \
156  *              @const void *key@ = pointer to the key buffer           \
157  *              @size_t sz@ = size of the key                           \
158  *              @const void *iv@ = pointer to initialization vector     \
159  *                                                                      \
160  * Returns:     ---                                                     \
161  *                                                                      \
162  * Use:         Initializes a OFB context ready for use.  You should    \
163  *              ensure that the IV chosen is unique: reusing an IV will \
164  *              compromise the security of the entire plaintext.  This  \
165  *              is equivalent to calls to @pre_init@, @pre_ofbsetkey@   \
166  *              and @pre_ofbsetiv@.                                     \
167  */                                                                     \
168                                                                         \
169 void pre##_ofbinit(pre##_ofbctx *ctx,                                   \
170                      const void *key, size_t sz,                        \
171                      const void *iv)                                    \
172 {                                                                       \
173   static octet zero[PRE##_BLKSZ] = { 0 };                               \
174   pre##_init(&ctx->ctx, key, sz);                                       \
175   pre##_ofbsetiv(ctx, iv ? iv : zero);                                  \
176 }                                                                       \
177                                                                         \
178 /* --- @pre_ofbencrypt@ --- *                                           \
179  *                                                                      \
180  * Arguments:   @pre_ofbctx *ctx@ = pointer to OFB context block        \
181  *              @const void *src@ = pointer to source data              \
182  *              @void *dest@ = pointer to destination data              \
183  *              @size_t sz@ = size of block to be encrypted             \
184  *                                                                      \
185  * Returns:     ---                                                     \
186  *                                                                      \
187  * Use:         Encrypts or decrypts a block with a block cipher in OFB \
188  *              mode: encryption and decryption are the same in OFB.    \
189  *              The destination may be null to just churn the feedback  \
190  *              round for a bit.  The source may be null to use the     \
191  *              cipher as a random data generator.                      \
192  */                                                                     \
193                                                                         \
194 void pre##_ofbencrypt(pre##_ofbctx *ctx,                                \
195                         const void *src, void *dest,                    \
196                         size_t sz)                                      \
197 {                                                                       \
198   const octet *s = src;                                                 \
199   octet *d = dest;                                                      \
200   int off = ctx->off;                                                   \
201                                                                         \
202   /* --- Empty blocks are trivial --- */                                \
203                                                                         \
204   if (!sz)                                                              \
205     return;                                                             \
206                                                                         \
207   /* --- If I can deal with the block from my buffer, do that --- */    \
208                                                                         \
209   if (sz < PRE##_BLKSZ - off)                                           \
210     goto small;                                                         \
211                                                                         \
212   /* --- Finish off what's left in my buffer --- */                     \
213                                                                         \
214   if (!d)                                                               \
215     sz -= off;                                                          \
216   else {                                                                \
217     while (off < PRE##_BLKSZ) {                                         \
218       register octet x = s ? *s++ : 0;                                  \
219       *d++ = ctx->iv[off++] ^ x;                                        \
220       sz--;                                                             \
221     }                                                                   \
222   }                                                                     \
223                                                                         \
224   /* --- Main encryption loop --- */                                    \
225                                                                         \
226   {                                                                     \
227     uint32 iv[PRE##_BLKSZ / 4];                                         \
228     BLKC_LOAD(PRE, iv, ctx->iv);                                        \
229                                                                         \
230     for (;;) {                                                          \
231       pre##_eblk(&ctx->ctx, iv, iv);                                    \
232       if (sz < PRE##_BLKSZ)                                             \
233         break;                                                          \
234       if (d) {                                                          \
235         if (!s)                                                         \
236           BLKC_STORE(PRE, d, iv);                                       \
237         else {                                                          \
238           uint32 x[PRE##_BLKSZ / 4];                                    \
239           BLKC_LOAD(PRE, x, s);                                         \
240           BLKC_XSTORE(PRE, d, iv, x);                                   \
241           s += PRE##_BLKSZ;                                             \
242         }                                                               \
243         d += PRE##_BLKSZ;                                               \
244       }                                                                 \
245       sz -= PRE##_BLKSZ;                                                \
246     }                                                                   \
247                                                                         \
248     BLKC_STORE(PRE, ctx->iv, iv);                                       \
249     off = 0;                                                            \
250   }                                                                     \
251                                                                         \
252   /* --- Tidying up the tail end --- */                                 \
253                                                                         \
254   if (sz) {                                                             \
255   small:                                                                \
256     if (!d)                                                             \
257       off += sz;                                                        \
258     else do {                                                           \
259       register octet x = s ? *s++ : 0;                                  \
260       *d++ = ctx->iv[off++] ^ x;                                        \
261       sz--;                                                             \
262     } while (sz);                                                       \
263   }                                                                     \
264                                                                         \
265   /* --- Done --- */                                                    \
266                                                                         \
267   ctx->off = off;                                                       \
268   return;                                                               \
269 }                                                                       \
270                                                                         \
271 /* --- Generic cipher interface --- */                                  \
272                                                                         \
273 static const gcipher_ops gops;                                          \
274                                                                         \
275 typedef struct gctx {                                                   \
276   gcipher c;                                                            \
277   pre##_ofbctx k;                                                       \
278 } gctx;                                                                 \
279                                                                         \
280 static gcipher *ginit(const void *k, size_t sz)                         \
281 {                                                                       \
282   gctx *g = CREATE(gctx);                                               \
283   g->c.ops = &gops;                                                     \
284   pre##_ofbinit(&g->k, k, sz, 0);                                       \
285   return (&g->c);                                                       \
286 }                                                                       \
287                                                                         \
288 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz)     \
289 {                                                                       \
290   gctx *g = (gctx *)c;                                                  \
291   pre##_ofbencrypt(&g->k, s, t, sz);                                    \
292 }                                                                       \
293                                                                         \
294 static void gdestroy(gcipher *c)                                        \
295 {                                                                       \
296   gctx *g = (gctx *)c;                                                  \
297   DESTROY(g);                                                           \
298 }                                                                       \
299                                                                         \
300 static void gsetiv(gcipher *c, const void *iv)                          \
301 {                                                                       \
302   gctx *g = (gctx *)c;                                                  \
303   pre##_ofbsetiv(&g->k, iv);                                            \
304 }                                                                       \
305                                                                         \
306 static void gbdry(gcipher *c)                                           \
307 {                                                                       \
308   gctx *g = (gctx *)c;                                                  \
309   pre##_ofbbdry(&g->k);                                                 \
310 }                                                                       \
311                                                                         \
312 static const gcipher_ops gops = {                                       \
313   &pre##_ofb.b,                                                         \
314   gencrypt, gencrypt, gdestroy, gsetiv, gbdry                           \
315 };                                                                      \
316                                                                         \
317 const gccipher pre##_ofb = {                                            \
318   { #pre "-ofb", PRE##_KEYSZ, PRE##_BLKSZ },                            \
319   ginit                                                                 \
320 };                                                                      \
321                                                                         \
322 /* --- Generic random number generator interface --- */                 \
323                                                                         \
324 typedef struct grctx {                                                  \
325   grand r;                                                              \
326   pre##_ofbctx k;                                                       \
327 } grctx;                                                                \
328                                                                         \
329 static void grdestroy(grand *r)                                         \
330 {                                                                       \
331   grctx *g = (grctx *)r;                                                \
332   DESTROY(g);                                                           \
333 }                                                                       \
334                                                                         \
335 static int grmisc(grand *r, unsigned op, ...)                           \
336 {                                                                       \
337   grctx *g = (grctx *)r;                                                \
338   va_list ap;                                                           \
339   int rc = 0;                                                           \
340   octet buf[PRE##_BLKSZ];                                               \
341   va_start(ap, op);                                                     \
342                                                                         \
343   switch (op) {                                                         \
344     case GRAND_CHECK:                                                   \
345       switch (va_arg(ap, unsigned)) {                                   \
346         case GRAND_CHECK:                                               \
347         case GRAND_SEEDINT:                                             \
348         case GRAND_SEEDUINT32:                                          \
349         case GRAND_SEEDBLOCK:                                           \
350         case GRAND_SEEDRAND:                                            \
351           rc = 1;                                                       \
352           break;                                                        \
353         default:                                                        \
354           rc = 0;                                                       \
355           break;                                                        \
356       }                                                                 \
357       break;                                                            \
358     case GRAND_SEEDINT:                                                 \
359       memset(buf, 0, sizeof(buf));                                      \
360       STORE32(buf, va_arg(ap, unsigned));                               \
361       pre##_ofbsetiv(&g->k, buf);                                       \
362       break;                                                            \
363     case GRAND_SEEDUINT32:                                              \
364       memset(buf, 0, sizeof(buf));                                      \
365       STORE32(buf, va_arg(ap, uint32));                                 \
366       pre##_ofbsetiv(&g->k, buf);                                       \
367       break;                                                            \
368     case GRAND_SEEDBLOCK: {                                             \
369       const void *p = va_arg(ap, const void *);                         \
370       size_t sz = va_arg(ap, size_t);                                   \
371       if (sz < sizeof(buf)) {                                           \
372         memset(buf, 0, sizeof(buf));                                    \
373         memcpy(buf, p, sz);                                             \
374         p = buf;                                                        \
375       }                                                                 \
376       pre##_ofbsetiv(&g->k, p);                                         \
377     } break;                                                            \
378     case GRAND_SEEDRAND: {                                              \
379       grand *rr = va_arg(ap, grand *);                                  \
380       rr->ops->fill(rr, buf, sizeof(buf));                              \
381       pre##_ofbsetiv(&g->k, buf);                                       \
382     } break;                                                            \
383     default:                                                            \
384       GRAND_BADOP;                                                      \
385       break;                                                            \
386   }                                                                     \
387                                                                         \
388   va_end(ap);                                                           \
389   return (rc);                                                          \
390 }                                                                       \
391                                                                         \
392 static octet grbyte(grand *r)                                           \
393 {                                                                       \
394   grctx *g = (grctx *)r;                                                \
395   octet o;                                                              \
396   pre##_ofbencrypt(&g->k, 0, &o, 1);                                    \
397   return (o);                                                           \
398 }                                                                       \
399                                                                         \
400 static uint32 grword(grand *r)                                          \
401 {                                                                       \
402   grctx *g = (grctx *)r;                                                \
403   octet b[4];                                                           \
404   pre##_ofbencrypt(&g->k, 0, b, sizeof(b));                             \
405   return (LOAD32(b));                                                   \
406 }                                                                       \
407                                                                         \
408 static void grfill(grand *r, void *p, size_t sz)                        \
409 {                                                                       \
410   grctx *g = (grctx *)r;                                                \
411   pre##_ofbencrypt(&g->k, 0, p, sz);                                    \
412 }                                                                       \
413                                                                         \
414 static const grand_ops grops = {                                        \
415   #pre "-ofb",                                                          \
416   0,                                                                    \
417   grmisc, grdestroy,                                                    \
418   grword, grbyte, grword, grand_range, grfill                           \
419 };                                                                      \
420                                                                         \
421 /* --- @pre_ofbrand@ --- *                                              \
422  *                                                                      \
423  * Arguments:   @const void *k@ = pointer to key material               \
424  *              @size_t sz@ = size of key material                      \
425  *                                                                      \
426  * Returns:     Pointer to generic random number generator interface.   \
427  *                                                                      \
428  * Use:         Creates a random number interface wrapper around an     \
429  *              OFB-mode block cipher.                                  \
430  */                                                                     \
431                                                                         \
432 grand *pre##_ofbrand(const void *k, size_t sz)                          \
433 {                                                                       \
434   grctx *g = CREATE(grctx);                                             \
435   g->r.ops = &grops;                                                    \
436   pre##_ofbinit(&g->k, k, sz, 0);                                       \
437   return (&g->r);                                                       \
438 }                                                                       \
439                                                                         \
440 OFB_TEST(PRE, pre)
441
442 /*----- Test rig ----------------------------------------------------------*/
443
444 #ifdef TEST_RIG
445
446 #include <stdio.h>
447
448 #include "daftstory.h"
449
450 /* --- @OFB_TEST@ --- *
451  *
452  * Arguments:   @PRE@, @pre@ = prefixes for block cipher definitions
453  *
454  * Use:         Standard test rig for OFB functions.
455  */
456
457 #define OFB_TEST(PRE, pre)                                              \
458                                                                         \
459 /* --- Initial plaintext for the test --- */                            \
460                                                                         \
461 static const octet text[] = TEXT;                                       \
462                                                                         \
463 /* --- Key and IV to use --- */                                         \
464                                                                         \
465 static const octet key[] = KEY;                                         \
466 static const octet iv[] = IV;                                           \
467                                                                         \
468 /* --- Buffers for encryption and decryption output --- */              \
469                                                                         \
470 static octet ct[sizeof(text)];                                          \
471 static octet pt[sizeof(text)];                                          \
472                                                                         \
473 static void hexdump(const octet *p, size_t sz)                          \
474 {                                                                       \
475   const octet *q = p + sz;                                              \
476   for (sz = 0; p < q; p++, sz++) {                                      \
477     printf("%02x", *p);                                                 \
478     if ((sz + 1) % PRE##_BLKSZ == 0)                                    \
479       putchar(':');                                                     \
480   }                                                                     \
481 }                                                                       \
482                                                                         \
483 int main(void)                                                          \
484 {                                                                       \
485   size_t sz = 0, rest;                                                  \
486   pre##_ofbctx ctx;                                                     \
487   int status = 0;                                                       \
488   int done = 0;                                                         \
489   pre##_ctx k;                                                          \
490                                                                         \
491   size_t keysz = PRE##_KEYSZ ?                                          \
492     PRE##_KEYSZ : strlen((const char *)key);                            \
493                                                                         \
494   fputs(#pre "-ofb: ", stdout);                                         \
495                                                                         \
496   pre##_init(&k, key, keysz);                                           \
497   pre##_ofbsetkey(&ctx, &k);                                            \
498                                                                         \
499   while (sz <= sizeof(text)) {                                          \
500     rest = sizeof(text) - sz;                                           \
501     memcpy(ct, text, sizeof(text));                                     \
502     pre##_ofbsetiv(&ctx, iv);                                           \
503     pre##_ofbencrypt(&ctx, ct, ct, sz);                                 \
504     pre##_ofbencrypt(&ctx, ct + sz, ct + sz, rest);                     \
505     memcpy(pt, ct, sizeof(text));                                       \
506     pre##_ofbsetiv(&ctx, iv);                                           \
507     pre##_ofbencrypt(&ctx, pt, pt, rest);                               \
508     pre##_ofbencrypt(&ctx, pt + rest, pt + rest, sz);                   \
509     if (memcmp(pt, text, sizeof(text)) == 0) {                          \
510       done++;                                                           \
511       if (sizeof(text) < 40 || done % 8 == 0)                           \
512         fputc('.', stdout);                                             \
513       if (done % 480 == 0)                                              \
514         fputs("\n\t", stdout);                                          \
515       fflush(stdout);                                                   \
516     } else {                                                            \
517       printf("\nError (sz = %lu)\n", (unsigned long)sz);                \
518       status = 1;                                                       \
519       printf("\tplaintext      = "); hexdump(text, sz);                 \
520         printf(", "); hexdump(text + sz, rest);                         \
521         fputc('\n', stdout);                                            \
522       printf("\tciphertext     = "); hexdump(ct, sz);                   \
523         printf(", "); hexdump(ct + sz, rest);                           \
524         fputc('\n', stdout);                                            \
525       printf("\trecovered text = "); hexdump(pt, sz);                   \
526         printf(", "); hexdump(pt + sz, rest);                           \
527         fputc('\n', stdout);                                            \
528       fputc('\n', stdout);                                              \
529     }                                                                   \
530     if (sz < 63)                                                        \
531       sz++;                                                             \
532     else                                                                \
533       sz += 9;                                                          \
534   }                                                                     \
535                                                                         \
536   fputs(status ? " failed\n" : " ok\n", stdout);                        \
537   return (status);                                                      \
538 }
539
540 #else
541 #  define OFB_TEST(PRE, pre)
542 #endif
543
544 /*----- That's all, folks -------------------------------------------------*/
545
546 #ifdef __cplusplus
547   }
548 #endif
549
550 #endif