3 * $Id: counter-def.h,v 1.2 2001/06/17 00:10:51 mdw Exp $
5 * Block cipher counter mode (or long cycle mode)
7 * (c) 2000 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 --------------------------------------------------*
32 * $Log: counter-def.h,v $
33 * Revision 1.2 2001/06/17 00:10:51 mdw
36 * Revision 1.1 2000/06/17 10:51:42 mdw
37 * Counter mode ciphers and pseudo-random generator.
41 #ifndef CATACOMB_COUNTER_DEF_H
42 #define CATACOMB_COUNTER_DEF_H
48 /*----- Header files ------------------------------------------------------*/
53 #include <mLib/bits.h>
56 #ifndef CATACOMB_ARENA_H
60 #ifndef CATACOMB_BLKC_H
64 #ifndef CATACOMB_GCIPHER_H
68 #ifndef CATACOMB_PARANOIA_H
69 # include "paranoia.h"
72 /*----- Macros ------------------------------------------------------------*/
74 /* --- @COUNTER_DEF@ --- *
76 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
78 * Use: Creates definitions for counter mode.
81 #define COUNTER_DEF(PRE, pre) \
83 /* --- @pre_countergetiv@ --- * \
85 * Arguments: @const pre_counterctx *ctx@ = pointer to counter \
87 * @void *iv@ = pointer to output data block \
91 * Use: Reads the currently set IV. Reading and setting an IV \
92 * is not transparent to the cipher. It will add a `step' \
93 * which must be matched by a similar operation during \
97 void pre##_countergetiv(const pre##_counterctx *ctx, void *iv) \
99 BLKC_STORE(PRE, iv, ctx->n); \
102 /* --- @pre_countersetiv@ --- * \
104 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
105 * @cnost void *iv@ = pointer to IV to set \
109 * Use: Sets the IV to use for subsequent encryption. \
112 void pre##_countersetiv(pre##_counterctx *ctx, const void *iv) \
114 BLKC_LOAD(PRE, ctx->n, iv); \
115 ctx->off = PRE##_BLKSZ; \
118 /* --- @pre_counterbdry@ --- * \
120 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
124 * Use: Inserts a boundary during encryption. Successful \
125 * decryption must place a similar boundary. \
128 void pre##_counterbdry(pre##_counterctx *ctx) \
130 BLKC_STEP(PRE, ctx->n); \
131 ctx->off = PRE##_BLKSZ; \
134 /* --- @pre_countersetkey@ --- * \
136 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
137 * @const pre_ctx *k@ = pointer to cipher context \
141 * Use: Sets the counter context to use a different cipher key. \
144 void pre##_countersetkey(pre##_counterctx *ctx, const pre##_ctx *k) \
149 /* --- @pre_counterinit@ --- * \
151 * Arguments: @pre_counterctx *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 counter context ready for use. You \
159 * should ensure that the IV chosen is unique: reusing an \
160 * IV will compromise the security of the entire \
161 * plaintext. This is equivalent to calls to @pre_init@, \
162 * @pre_countersetkey@ and @pre_countersetiv@. \
165 void pre##_counterinit(pre##_counterctx *ctx, \
166 const void *key, size_t sz, \
169 static octet zero[PRE##_BLKSZ] = { 0 }; \
170 pre##_init(&ctx->ctx, key, sz); \
171 pre##_countersetiv(ctx, iv ? iv : zero); \
174 /* --- @pre_counterencrypt@ --- * \
176 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
177 * @const void *src@ = pointer to source data \
178 * @void *dest@ = pointer to destination data \
179 * @size_t sz@ = size of block to be encrypted \
183 * Use: Encrypts or decrypts a block with a block cipher in \
184 * counter mode: encryption and decryption are the same in \
185 * counter. The destination may be null to just churn the \
186 * feedback round for a bit. The source may be null to \
187 * use the cipher as a random data generator. \
190 void pre##_counterencrypt(pre##_counterctx *ctx, \
191 const void *src, void *dest, \
194 const octet *s = src; \
196 unsigned off = ctx->off; \
198 /* --- Empty blocks are trivial --- */ \
203 /* --- If I can deal with the block from my buffer, do that --- */ \
205 if (sz < PRE##_BLKSZ - off) \
208 /* --- Finish off what's left in my buffer --- */ \
211 sz -= PRE##_BLKSZ - off; \
213 while (off < PRE##_BLKSZ) { \
214 register octet x = s ? *s++ : 0; \
215 *d++ = ctx->buf[off++] ^ x; \
220 /* --- Main encryption loop --- */ \
223 uint32 n[PRE##_BLKSZ / 4]; \
226 pre##_eblk(&ctx->ctx, ctx->n, n); \
227 BLKC_STEP(PRE, ctx->n); \
228 if (sz < PRE##_BLKSZ) \
232 BLKC_STORE(PRE, d, n); \
234 uint32 x[PRE##_BLKSZ / 4]; \
235 BLKC_LOAD(PRE, x, s); \
236 BLKC_XSTORE(PRE, d, n, x); \
244 BLKC_STORE(PRE, ctx->buf, n); \
248 /* --- Tidying up the tail end --- */ \
255 register octet x = s ? *s++ : 0; \
256 *d++ = ctx->buf[off++] ^ x; \
267 /* --- Generic cipher interface --- */ \
269 static const gcipher_ops gops; \
271 typedef struct gctx { \
273 pre##_counterctx k; \
276 static gcipher *ginit(const void *k, size_t sz) \
278 gctx *g = S_CREATE(gctx); \
280 pre##_counterinit(&g->k, k, sz, 0); \
284 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
286 gctx *g = (gctx *)c; \
287 pre##_counterencrypt(&g->k, s, t, sz); \
290 static void gdestroy(gcipher *c) \
292 gctx *g = (gctx *)c; \
297 static void gsetiv(gcipher *c, const void *iv) \
299 gctx *g = (gctx *)c; \
300 pre##_countersetiv(&g->k, iv); \
303 static void gbdry(gcipher *c) \
305 gctx *g = (gctx *)c; \
306 pre##_counterbdry(&g->k); \
309 static const gcipher_ops gops = { \
311 gencrypt, gencrypt, gdestroy, gsetiv, gbdry \
314 const gccipher pre##_counter = { \
315 #pre "-counter", pre##_keysz, PRE##_BLKSZ, \
319 /* --- Generic random number generator interface --- */ \
321 typedef struct grctx { \
323 pre##_counterctx k; \
326 static void grdestroy(grand *r) \
328 grctx *g = (grctx *)r; \
333 static int grmisc(grand *r, unsigned op, ...) \
335 grctx *g = (grctx *)r; \
338 octet buf[PRE##_BLKSZ]; \
343 switch (va_arg(ap, unsigned)) { \
345 case GRAND_SEEDINT: \
346 case GRAND_SEEDUINT32: \
347 case GRAND_SEEDBLOCK: \
348 case GRAND_SEEDRAND: \
356 case GRAND_SEEDINT: \
357 BLKC_SET(PRE, g->k.n, va_arg(ap, unsigned)); \
358 g->k.off = PRE##_BLKSZ; \
360 case GRAND_SEEDUINT32: \
361 BLKC_SET(PRE, g->k.n, va_arg(ap, uint32)); \
362 g->k.off = PRE##_BLKSZ; \
364 case GRAND_SEEDBLOCK: { \
365 const void *p = va_arg(ap, const void *); \
366 size_t sz = va_arg(ap, size_t); \
367 if (sz < sizeof(buf)) { \
368 memset(buf, 0, sizeof(buf)); \
369 memcpy(buf, p, sz); \
372 pre##_countersetiv(&g->k, p); \
374 case GRAND_SEEDRAND: { \
375 grand *rr = va_arg(ap, grand *); \
376 rr->ops->fill(rr, buf, sizeof(buf)); \
377 pre##_countersetiv(&g->k, buf); \
388 static octet grbyte(grand *r) \
390 grctx *g = (grctx *)r; \
392 pre##_counterencrypt(&g->k, 0, &o, 1); \
396 static uint32 grword(grand *r) \
398 grctx *g = (grctx *)r; \
400 pre##_counterencrypt(&g->k, 0, b, sizeof(b)); \
401 return (LOAD32(b)); \
404 static void grfill(grand *r, void *p, size_t sz) \
406 grctx *g = (grctx *)r; \
407 pre##_counterencrypt(&g->k, 0, p, sz); \
410 static const grand_ops grops = { \
414 grword, grbyte, grword, grand_range, grfill \
417 /* --- @pre_counterrand@ --- * \
419 * Arguments: @const void *k@ = pointer to key material \
420 * @size_t sz@ = size of key material \
422 * Returns: Pointer to generic random number generator interface. \
424 * Use: Creates a random number interface wrapper around an \
425 * counter-mode block cipher. \
428 grand *pre##_counterrand(const void *k, size_t sz) \
430 grctx *g = S_CREATE(grctx); \
432 pre##_counterinit(&g->k, k, sz, 0); \
436 COUNTER_TEST(PRE, pre)
438 /*----- Test rig ----------------------------------------------------------*/
444 #include "daftstory.h"
446 /* --- @COUNTER_TEST@ --- *
448 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
450 * Use: Standard test rig for counter functions.
453 #define COUNTER_TEST(PRE, pre) \
455 /* --- Initial plaintext for the test --- */ \
457 static const octet text[] = TEXT; \
459 /* --- Key and IV to use --- */ \
461 static const octet key[] = KEY; \
462 static const octet iv[] = IV; \
464 /* --- Buffers for encryption and decryption output --- */ \
466 static octet ct[sizeof(text)]; \
467 static octet pt[sizeof(text)]; \
469 static void hexdump(const octet *p, size_t sz) \
471 const octet *q = p + sz; \
472 for (sz = 0; p < q; p++, sz++) { \
473 printf("%02x", *p); \
474 if ((sz + 1) % PRE##_BLKSZ == 0) \
481 size_t sz = 0, rest; \
482 pre##_counterctx ctx; \
487 size_t keysz = PRE##_KEYSZ ? \
488 PRE##_KEYSZ : strlen((const char *)key); \
490 fputs(#pre "-counter: ", stdout); \
492 pre##_init(&k, key, keysz); \
493 pre##_countersetkey(&ctx, &k); \
495 while (sz <= sizeof(text)) { \
496 rest = sizeof(text) - sz; \
497 memcpy(ct, text, sizeof(text)); \
498 pre##_countersetiv(&ctx, iv); \
499 pre##_counterencrypt(&ctx, ct, ct, sz); \
500 pre##_counterencrypt(&ctx, ct + sz, ct + sz, rest); \
501 memcpy(pt, ct, sizeof(text)); \
502 pre##_countersetiv(&ctx, iv); \
503 pre##_counterencrypt(&ctx, pt, pt, rest); \
504 pre##_counterencrypt(&ctx, pt + rest, pt + rest, sz); \
505 if (memcmp(pt, text, sizeof(text)) == 0) { \
507 if (sizeof(text) < 40 || done % 8 == 0) \
508 fputc('.', stdout); \
509 if (done % 480 == 0) \
510 fputs("\n\t", stdout); \
513 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
515 printf("\tplaintext = "); hexdump(text, sz); \
516 printf(", "); hexdump(text + sz, rest); \
517 fputc('\n', stdout); \
518 printf("\tciphertext = "); hexdump(ct, sz); \
519 printf(", "); hexdump(ct + sz, rest); \
520 fputc('\n', stdout); \
521 printf("\trecovered text = "); hexdump(pt, sz); \
522 printf(", "); hexdump(pt + sz, rest); \
523 fputc('\n', stdout); \
524 fputc('\n', stdout); \
532 fputs(status ? " failed\n" : " ok\n", stdout); \
537 # define COUNTER_TEST(PRE, pre)
540 /*----- That's all, folks -------------------------------------------------*/