3 * Block cipher counter mode (or long cycle mode)
5 * (c) 2000 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_COUNTER_DEF_H
29 #define CATACOMB_COUNTER_DEF_H
35 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/bits.h>
43 #ifndef CATACOMB_ARENA_H
47 #ifndef CATACOMB_BLKC_H
51 #ifndef CATACOMB_GCIPHER_H
55 #ifndef CATACOMB_PARANOIA_H
56 # include "paranoia.h"
59 /*----- Macros ------------------------------------------------------------*/
61 /* --- @COUNTER_DEF@ --- *
63 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
65 * Use: Creates definitions for counter mode.
68 #define COUNTER_DEF(PRE, pre) \
70 /* --- @pre_countergetiv@ --- * \
72 * Arguments: @const pre_counterctx *ctx@ = pointer to counter \
74 * @void *iv@ = pointer to output data block \
78 * Use: Reads the currently set IV. Reading and setting an IV \
79 * is not transparent to the cipher. It will add a `step' \
80 * which must be matched by a similar operation during \
84 void pre##_countergetiv(const pre##_counterctx *ctx, void *iv) \
86 BLKC_STORE(PRE, iv, ctx->n); \
89 /* --- @pre_countersetiv@ --- * \
91 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
92 * @cnost void *iv@ = pointer to IV to set \
96 * Use: Sets the IV to use for subsequent encryption. \
99 void pre##_countersetiv(pre##_counterctx *ctx, const void *iv) \
101 BLKC_LOAD(PRE, ctx->n, iv); \
102 ctx->off = PRE##_BLKSZ; \
105 /* --- @pre_counterbdry@ --- * \
107 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
111 * Use: Inserts a boundary during encryption. Successful \
112 * decryption must place a similar boundary. \
115 void pre##_counterbdry(pre##_counterctx *ctx) \
117 BLKC_STEP(PRE, ctx->n); \
118 ctx->off = PRE##_BLKSZ; \
121 /* --- @pre_countersetkey@ --- * \
123 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
124 * @const pre_ctx *k@ = pointer to cipher context \
128 * Use: Sets the counter context to use a different cipher key. \
131 void pre##_countersetkey(pre##_counterctx *ctx, const pre##_ctx *k) \
136 /* --- @pre_counterinit@ --- * \
138 * Arguments: @pre_counterctx *ctx@ = pointer to cipher context \
139 * @const void *key@ = pointer to the key buffer \
140 * @size_t sz@ = size of the key \
141 * @const void *iv@ = pointer to initialization vector \
145 * Use: Initializes a counter context ready for use. You \
146 * should ensure that the IV chosen is unique: reusing an \
147 * IV will compromise the security of the entire \
148 * plaintext. This is equivalent to calls to @pre_init@, \
149 * @pre_countersetkey@ and @pre_countersetiv@. \
152 void pre##_counterinit(pre##_counterctx *ctx, \
153 const void *key, size_t sz, \
156 static const octet zero[PRE##_BLKSZ] = { 0 }; \
157 pre##_init(&ctx->ctx, key, sz); \
158 pre##_countersetiv(ctx, iv ? iv : zero); \
161 /* --- @pre_counterencrypt@ --- * \
163 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
164 * @const void *src@ = pointer to source data \
165 * @void *dest@ = pointer to destination data \
166 * @size_t sz@ = size of block to be encrypted \
170 * Use: Encrypts or decrypts a block with a block cipher in \
171 * counter mode: encryption and decryption are the same in \
172 * counter. The destination may be null to just churn the \
173 * feedback round for a bit. The source may be null to \
174 * use the cipher as a random data generator. \
177 void pre##_counterencrypt(pre##_counterctx *ctx, \
178 const void *src, void *dest, \
181 const octet *s = src; \
183 unsigned off = ctx->off; \
185 /* --- Empty blocks are trivial --- */ \
190 /* --- If I can deal with the block from my buffer, do that --- */ \
192 if (sz < PRE##_BLKSZ - off) \
195 /* --- Finish off what's left in my buffer --- */ \
198 sz -= PRE##_BLKSZ - off; \
200 while (off < PRE##_BLKSZ) { \
201 register octet x = s ? *s++ : 0; \
202 *d++ = ctx->buf[off++] ^ x; \
207 /* --- Main encryption loop --- */ \
210 uint32 n[PRE##_BLKSZ / 4]; \
213 pre##_eblk(&ctx->ctx, ctx->n, n); \
214 BLKC_STEP(PRE, ctx->n); \
215 if (sz < PRE##_BLKSZ) \
219 BLKC_STORE(PRE, d, n); \
221 uint32 x[PRE##_BLKSZ / 4]; \
222 BLKC_LOAD(PRE, x, s); \
223 BLKC_XSTORE(PRE, d, n, x); \
231 BLKC_STORE(PRE, ctx->buf, n); \
235 /* --- Tidying up the tail end --- */ \
242 register octet x = s ? *s++ : 0; \
243 *d++ = ctx->buf[off++] ^ x; \
254 /* --- Generic cipher interface --- */ \
256 static const gcipher_ops gops; \
258 typedef struct gctx { \
260 pre##_counterctx k; \
263 static gcipher *ginit(const void *k, size_t sz) \
265 gctx *g = S_CREATE(gctx); \
267 pre##_counterinit(&g->k, k, sz, 0); \
271 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
273 gctx *g = (gctx *)c; \
274 pre##_counterencrypt(&g->k, s, t, sz); \
277 static void gdestroy(gcipher *c) \
279 gctx *g = (gctx *)c; \
284 static void gsetiv(gcipher *c, const void *iv) \
286 gctx *g = (gctx *)c; \
287 pre##_countersetiv(&g->k, iv); \
290 static void gbdry(gcipher *c) \
292 gctx *g = (gctx *)c; \
293 pre##_counterbdry(&g->k); \
296 static const gcipher_ops gops = { \
298 gencrypt, gencrypt, gdestroy, gsetiv, gbdry \
301 const gccipher pre##_counter = { \
302 #pre "-counter", pre##_keysz, PRE##_BLKSZ, \
306 /* --- Generic random number generator interface --- */ \
308 typedef struct grctx { \
310 pre##_counterctx k; \
313 static void grdestroy(grand *r) \
315 grctx *g = (grctx *)r; \
320 static int grmisc(grand *r, unsigned op, ...) \
322 grctx *g = (grctx *)r; \
325 octet buf[PRE##_BLKSZ]; \
330 switch (va_arg(ap, unsigned)) { \
332 case GRAND_SEEDINT: \
333 case GRAND_SEEDUINT32: \
334 case GRAND_SEEDBLOCK: \
335 case GRAND_SEEDRAND: \
343 case GRAND_SEEDINT: \
344 BLKC_SET(PRE, g->k.n, va_arg(ap, unsigned)); \
345 g->k.off = PRE##_BLKSZ; \
347 case GRAND_SEEDUINT32: \
348 BLKC_SET(PRE, g->k.n, va_arg(ap, uint32)); \
349 g->k.off = PRE##_BLKSZ; \
351 case GRAND_SEEDBLOCK: { \
352 const void *p = va_arg(ap, const void *); \
353 size_t sz = va_arg(ap, size_t); \
354 if (sz < sizeof(buf)) { \
355 memset(buf, 0, sizeof(buf)); \
356 memcpy(buf, p, sz); \
359 pre##_countersetiv(&g->k, p); \
361 case GRAND_SEEDRAND: { \
362 grand *rr = va_arg(ap, grand *); \
363 rr->ops->fill(rr, buf, sizeof(buf)); \
364 pre##_countersetiv(&g->k, buf); \
375 static octet grbyte(grand *r) \
377 grctx *g = (grctx *)r; \
379 pre##_counterencrypt(&g->k, 0, &o, 1); \
383 static uint32 grword(grand *r) \
385 grctx *g = (grctx *)r; \
387 pre##_counterencrypt(&g->k, 0, b, sizeof(b)); \
388 return (LOAD32(b)); \
391 static void grfill(grand *r, void *p, size_t sz) \
393 grctx *g = (grctx *)r; \
394 pre##_counterencrypt(&g->k, 0, p, sz); \
397 static const grand_ops grops = { \
401 grword, grbyte, grword, grand_range, grfill \
404 /* --- @pre_counterrand@ --- * \
406 * Arguments: @const void *k@ = pointer to key material \
407 * @size_t sz@ = size of key material \
409 * Returns: Pointer to generic random number generator interface. \
411 * Use: Creates a random number interface wrapper around an \
412 * counter-mode block cipher. \
415 grand *pre##_counterrand(const void *k, size_t sz) \
417 grctx *g = S_CREATE(grctx); \
419 pre##_counterinit(&g->k, k, sz, 0); \
423 COUNTER_TEST(PRE, pre)
425 /*----- Test rig ----------------------------------------------------------*/
431 #include "daftstory.h"
433 /* --- @COUNTER_TEST@ --- *
435 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
437 * Use: Standard test rig for counter functions.
440 #define COUNTER_TEST(PRE, pre) \
442 /* --- Initial plaintext for the test --- */ \
444 static const octet text[] = TEXT; \
446 /* --- Key and IV to use --- */ \
448 static const octet key[] = KEY; \
449 static const octet iv[] = IV; \
451 /* --- Buffers for encryption and decryption output --- */ \
453 static octet ct[sizeof(text)]; \
454 static octet pt[sizeof(text)]; \
456 static void hexdump(const octet *p, size_t sz) \
458 const octet *q = p + sz; \
459 for (sz = 0; p < q; p++, sz++) { \
460 printf("%02x", *p); \
461 if ((sz + 1) % PRE##_BLKSZ == 0) \
468 size_t sz = 0, rest; \
469 pre##_counterctx ctx; \
474 size_t keysz = PRE##_KEYSZ ? \
475 PRE##_KEYSZ : strlen((const char *)key); \
477 fputs(#pre "-counter: ", stdout); \
479 pre##_init(&k, key, keysz); \
480 pre##_countersetkey(&ctx, &k); \
482 while (sz <= sizeof(text)) { \
483 rest = sizeof(text) - sz; \
484 memcpy(ct, text, sizeof(text)); \
485 pre##_countersetiv(&ctx, iv); \
486 pre##_counterencrypt(&ctx, ct, ct, sz); \
487 pre##_counterencrypt(&ctx, ct + sz, ct + sz, rest); \
488 memcpy(pt, ct, sizeof(text)); \
489 pre##_countersetiv(&ctx, iv); \
490 pre##_counterencrypt(&ctx, pt, pt, rest); \
491 pre##_counterencrypt(&ctx, pt + rest, pt + rest, sz); \
492 if (memcmp(pt, text, sizeof(text)) == 0) { \
494 if (sizeof(text) < 40 || done % 8 == 0) \
495 fputc('.', stdout); \
496 if (done % 480 == 0) \
497 fputs("\n\t", stdout); \
500 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
502 printf("\tplaintext = "); hexdump(text, sz); \
503 printf(", "); hexdump(text + sz, rest); \
504 fputc('\n', stdout); \
505 printf("\tciphertext = "); hexdump(ct, sz); \
506 printf(", "); hexdump(ct + sz, rest); \
507 fputc('\n', stdout); \
508 printf("\trecovered text = "); hexdump(pt, sz); \
509 printf(", "); hexdump(pt + sz, rest); \
510 fputc('\n', stdout); \
511 fputc('\n', stdout); \
519 fputs(status ? " failed\n" : " ok\n", stdout); \
524 # define COUNTER_TEST(PRE, pre)
527 /*----- That's all, folks -------------------------------------------------*/