| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: counter-def.h,v 1.2 2001/06/17 00:10:51 mdw Exp $ |
| 4 | * |
| 5 | * Block cipher counter mode (or long cycle mode) |
| 6 | * |
| 7 | * (c) 2000 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: counter-def.h,v $ |
| 33 | * Revision 1.2 2001/06/17 00:10:51 mdw |
| 34 | * Typesetting fixes |
| 35 | * |
| 36 | * Revision 1.1 2000/06/17 10:51:42 mdw |
| 37 | * Counter mode ciphers and pseudo-random generator. |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | #ifndef CATACOMB_COUNTER_DEF_H |
| 42 | #define CATACOMB_COUNTER_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_ARENA_H |
| 57 | # include "arena.h" |
| 58 | #endif |
| 59 | |
| 60 | #ifndef CATACOMB_BLKC_H |
| 61 | # include "blkc.h" |
| 62 | #endif |
| 63 | |
| 64 | #ifndef CATACOMB_GCIPHER_H |
| 65 | # include "gcipher.h" |
| 66 | #endif |
| 67 | |
| 68 | #ifndef CATACOMB_PARANOIA_H |
| 69 | # include "paranoia.h" |
| 70 | #endif |
| 71 | |
| 72 | /*----- Macros ------------------------------------------------------------*/ |
| 73 | |
| 74 | /* --- @COUNTER_DEF@ --- * |
| 75 | * |
| 76 | * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher |
| 77 | * |
| 78 | * Use: Creates definitions for counter mode. |
| 79 | */ |
| 80 | |
| 81 | #define COUNTER_DEF(PRE, pre) \ |
| 82 | \ |
| 83 | /* --- @pre_countergetiv@ --- * \ |
| 84 | * \ |
| 85 | * Arguments: @const pre_counterctx *ctx@ = pointer to counter \ |
| 86 | * context \ |
| 87 | * @void *iv@ = pointer to output data block \ |
| 88 | * \ |
| 89 | * Returns: --- \ |
| 90 | * \ |
| 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 \ |
| 94 | * decryption. \ |
| 95 | */ \ |
| 96 | \ |
| 97 | void pre##_countergetiv(const pre##_counterctx *ctx, void *iv) \ |
| 98 | { \ |
| 99 | BLKC_STORE(PRE, iv, ctx->n); \ |
| 100 | } \ |
| 101 | \ |
| 102 | /* --- @pre_countersetiv@ --- * \ |
| 103 | * \ |
| 104 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
| 105 | * @cnost void *iv@ = pointer to IV to set \ |
| 106 | * \ |
| 107 | * Returns: --- \ |
| 108 | * \ |
| 109 | * Use: Sets the IV to use for subsequent encryption. \ |
| 110 | */ \ |
| 111 | \ |
| 112 | void pre##_countersetiv(pre##_counterctx *ctx, const void *iv) \ |
| 113 | { \ |
| 114 | BLKC_LOAD(PRE, ctx->n, iv); \ |
| 115 | ctx->off = PRE##_BLKSZ; \ |
| 116 | } \ |
| 117 | \ |
| 118 | /* --- @pre_counterbdry@ --- * \ |
| 119 | * \ |
| 120 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
| 121 | * \ |
| 122 | * Returns: --- \ |
| 123 | * \ |
| 124 | * Use: Inserts a boundary during encryption. Successful \ |
| 125 | * decryption must place a similar boundary. \ |
| 126 | */ \ |
| 127 | \ |
| 128 | void pre##_counterbdry(pre##_counterctx *ctx) \ |
| 129 | { \ |
| 130 | BLKC_STEP(PRE, ctx->n); \ |
| 131 | ctx->off = PRE##_BLKSZ; \ |
| 132 | } \ |
| 133 | \ |
| 134 | /* --- @pre_countersetkey@ --- * \ |
| 135 | * \ |
| 136 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
| 137 | * @const pre_ctx *k@ = pointer to cipher context \ |
| 138 | * \ |
| 139 | * Returns: --- \ |
| 140 | * \ |
| 141 | * Use: Sets the counter context to use a different cipher key. \ |
| 142 | */ \ |
| 143 | \ |
| 144 | void pre##_countersetkey(pre##_counterctx *ctx, const pre##_ctx *k) \ |
| 145 | { \ |
| 146 | ctx->ctx = *k; \ |
| 147 | } \ |
| 148 | \ |
| 149 | /* --- @pre_counterinit@ --- * \ |
| 150 | * \ |
| 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 \ |
| 155 | * \ |
| 156 | * Returns: --- \ |
| 157 | * \ |
| 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@. \ |
| 163 | */ \ |
| 164 | \ |
| 165 | void pre##_counterinit(pre##_counterctx *ctx, \ |
| 166 | const void *key, size_t sz, \ |
| 167 | const void *iv) \ |
| 168 | { \ |
| 169 | static octet zero[PRE##_BLKSZ] = { 0 }; \ |
| 170 | pre##_init(&ctx->ctx, key, sz); \ |
| 171 | pre##_countersetiv(ctx, iv ? iv : zero); \ |
| 172 | } \ |
| 173 | \ |
| 174 | /* --- @pre_counterencrypt@ --- * \ |
| 175 | * \ |
| 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 \ |
| 180 | * \ |
| 181 | * Returns: --- \ |
| 182 | * \ |
| 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. \ |
| 188 | */ \ |
| 189 | \ |
| 190 | void pre##_counterencrypt(pre##_counterctx *ctx, \ |
| 191 | const void *src, void *dest, \ |
| 192 | size_t sz) \ |
| 193 | { \ |
| 194 | const octet *s = src; \ |
| 195 | octet *d = dest; \ |
| 196 | unsigned off = ctx->off; \ |
| 197 | \ |
| 198 | /* --- Empty blocks are trivial --- */ \ |
| 199 | \ |
| 200 | if (!sz) \ |
| 201 | return; \ |
| 202 | \ |
| 203 | /* --- If I can deal with the block from my buffer, do that --- */ \ |
| 204 | \ |
| 205 | if (sz < PRE##_BLKSZ - off) \ |
| 206 | goto small; \ |
| 207 | \ |
| 208 | /* --- Finish off what's left in my buffer --- */ \ |
| 209 | \ |
| 210 | if (!d) \ |
| 211 | sz -= PRE##_BLKSZ - off; \ |
| 212 | else { \ |
| 213 | while (off < PRE##_BLKSZ) { \ |
| 214 | register octet x = s ? *s++ : 0; \ |
| 215 | *d++ = ctx->buf[off++] ^ x; \ |
| 216 | sz--; \ |
| 217 | } \ |
| 218 | } \ |
| 219 | \ |
| 220 | /* --- Main encryption loop --- */ \ |
| 221 | \ |
| 222 | { \ |
| 223 | uint32 n[PRE##_BLKSZ / 4]; \ |
| 224 | \ |
| 225 | for (;;) { \ |
| 226 | pre##_eblk(&ctx->ctx, ctx->n, n); \ |
| 227 | BLKC_STEP(PRE, ctx->n); \ |
| 228 | if (sz < PRE##_BLKSZ) \ |
| 229 | break; \ |
| 230 | if (d) { \ |
| 231 | if (!s) \ |
| 232 | BLKC_STORE(PRE, d, n); \ |
| 233 | else { \ |
| 234 | uint32 x[PRE##_BLKSZ / 4]; \ |
| 235 | BLKC_LOAD(PRE, x, s); \ |
| 236 | BLKC_XSTORE(PRE, d, n, x); \ |
| 237 | s += PRE##_BLKSZ; \ |
| 238 | } \ |
| 239 | d += PRE##_BLKSZ; \ |
| 240 | } \ |
| 241 | sz -= PRE##_BLKSZ; \ |
| 242 | } \ |
| 243 | \ |
| 244 | BLKC_STORE(PRE, ctx->buf, n); \ |
| 245 | off = 0; \ |
| 246 | } \ |
| 247 | \ |
| 248 | /* --- Tidying up the tail end --- */ \ |
| 249 | \ |
| 250 | if (sz) { \ |
| 251 | small: \ |
| 252 | if (!d) \ |
| 253 | off += sz; \ |
| 254 | else do { \ |
| 255 | register octet x = s ? *s++ : 0; \ |
| 256 | *d++ = ctx->buf[off++] ^ x; \ |
| 257 | sz--; \ |
| 258 | } while (sz); \ |
| 259 | } \ |
| 260 | \ |
| 261 | /* --- Done --- */ \ |
| 262 | \ |
| 263 | ctx->off = off; \ |
| 264 | return; \ |
| 265 | } \ |
| 266 | \ |
| 267 | /* --- Generic cipher interface --- */ \ |
| 268 | \ |
| 269 | static const gcipher_ops gops; \ |
| 270 | \ |
| 271 | typedef struct gctx { \ |
| 272 | gcipher c; \ |
| 273 | pre##_counterctx k; \ |
| 274 | } gctx; \ |
| 275 | \ |
| 276 | static gcipher *ginit(const void *k, size_t sz) \ |
| 277 | { \ |
| 278 | gctx *g = S_CREATE(gctx); \ |
| 279 | g->c.ops = &gops; \ |
| 280 | pre##_counterinit(&g->k, k, sz, 0); \ |
| 281 | return (&g->c); \ |
| 282 | } \ |
| 283 | \ |
| 284 | static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \ |
| 285 | { \ |
| 286 | gctx *g = (gctx *)c; \ |
| 287 | pre##_counterencrypt(&g->k, s, t, sz); \ |
| 288 | } \ |
| 289 | \ |
| 290 | static void gdestroy(gcipher *c) \ |
| 291 | { \ |
| 292 | gctx *g = (gctx *)c; \ |
| 293 | BURN(*g); \ |
| 294 | S_DESTROY(g); \ |
| 295 | } \ |
| 296 | \ |
| 297 | static void gsetiv(gcipher *c, const void *iv) \ |
| 298 | { \ |
| 299 | gctx *g = (gctx *)c; \ |
| 300 | pre##_countersetiv(&g->k, iv); \ |
| 301 | } \ |
| 302 | \ |
| 303 | static void gbdry(gcipher *c) \ |
| 304 | { \ |
| 305 | gctx *g = (gctx *)c; \ |
| 306 | pre##_counterbdry(&g->k); \ |
| 307 | } \ |
| 308 | \ |
| 309 | static const gcipher_ops gops = { \ |
| 310 | &pre##_counter, \ |
| 311 | gencrypt, gencrypt, gdestroy, gsetiv, gbdry \ |
| 312 | }; \ |
| 313 | \ |
| 314 | const gccipher pre##_counter = { \ |
| 315 | #pre "-counter", pre##_keysz, PRE##_BLKSZ, \ |
| 316 | ginit \ |
| 317 | }; \ |
| 318 | \ |
| 319 | /* --- Generic random number generator interface --- */ \ |
| 320 | \ |
| 321 | typedef struct grctx { \ |
| 322 | grand r; \ |
| 323 | pre##_counterctx k; \ |
| 324 | } grctx; \ |
| 325 | \ |
| 326 | static void grdestroy(grand *r) \ |
| 327 | { \ |
| 328 | grctx *g = (grctx *)r; \ |
| 329 | BURN(*g); \ |
| 330 | S_DESTROY(g); \ |
| 331 | } \ |
| 332 | \ |
| 333 | static int grmisc(grand *r, unsigned op, ...) \ |
| 334 | { \ |
| 335 | grctx *g = (grctx *)r; \ |
| 336 | va_list ap; \ |
| 337 | int rc = 0; \ |
| 338 | octet buf[PRE##_BLKSZ]; \ |
| 339 | va_start(ap, op); \ |
| 340 | \ |
| 341 | switch (op) { \ |
| 342 | case GRAND_CHECK: \ |
| 343 | switch (va_arg(ap, unsigned)) { \ |
| 344 | case GRAND_CHECK: \ |
| 345 | case GRAND_SEEDINT: \ |
| 346 | case GRAND_SEEDUINT32: \ |
| 347 | case GRAND_SEEDBLOCK: \ |
| 348 | case GRAND_SEEDRAND: \ |
| 349 | rc = 1; \ |
| 350 | break; \ |
| 351 | default: \ |
| 352 | rc = 0; \ |
| 353 | break; \ |
| 354 | } \ |
| 355 | break; \ |
| 356 | case GRAND_SEEDINT: \ |
| 357 | BLKC_SET(PRE, g->k.n, va_arg(ap, unsigned)); \ |
| 358 | g->k.off = PRE##_BLKSZ; \ |
| 359 | break; \ |
| 360 | case GRAND_SEEDUINT32: \ |
| 361 | BLKC_SET(PRE, g->k.n, va_arg(ap, uint32)); \ |
| 362 | g->k.off = PRE##_BLKSZ; \ |
| 363 | break; \ |
| 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); \ |
| 370 | p = buf; \ |
| 371 | } \ |
| 372 | pre##_countersetiv(&g->k, p); \ |
| 373 | } break; \ |
| 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); \ |
| 378 | } break; \ |
| 379 | default: \ |
| 380 | GRAND_BADOP; \ |
| 381 | break; \ |
| 382 | } \ |
| 383 | \ |
| 384 | va_end(ap); \ |
| 385 | return (rc); \ |
| 386 | } \ |
| 387 | \ |
| 388 | static octet grbyte(grand *r) \ |
| 389 | { \ |
| 390 | grctx *g = (grctx *)r; \ |
| 391 | octet o; \ |
| 392 | pre##_counterencrypt(&g->k, 0, &o, 1); \ |
| 393 | return (o); \ |
| 394 | } \ |
| 395 | \ |
| 396 | static uint32 grword(grand *r) \ |
| 397 | { \ |
| 398 | grctx *g = (grctx *)r; \ |
| 399 | octet b[4]; \ |
| 400 | pre##_counterencrypt(&g->k, 0, b, sizeof(b)); \ |
| 401 | return (LOAD32(b)); \ |
| 402 | } \ |
| 403 | \ |
| 404 | static void grfill(grand *r, void *p, size_t sz) \ |
| 405 | { \ |
| 406 | grctx *g = (grctx *)r; \ |
| 407 | pre##_counterencrypt(&g->k, 0, p, sz); \ |
| 408 | } \ |
| 409 | \ |
| 410 | static const grand_ops grops = { \ |
| 411 | #pre "-counter", \ |
| 412 | GRAND_CRYPTO, 0, \ |
| 413 | grmisc, grdestroy, \ |
| 414 | grword, grbyte, grword, grand_range, grfill \ |
| 415 | }; \ |
| 416 | \ |
| 417 | /* --- @pre_counterrand@ --- * \ |
| 418 | * \ |
| 419 | * Arguments: @const void *k@ = pointer to key material \ |
| 420 | * @size_t sz@ = size of key material \ |
| 421 | * \ |
| 422 | * Returns: Pointer to generic random number generator interface. \ |
| 423 | * \ |
| 424 | * Use: Creates a random number interface wrapper around an \ |
| 425 | * counter-mode block cipher. \ |
| 426 | */ \ |
| 427 | \ |
| 428 | grand *pre##_counterrand(const void *k, size_t sz) \ |
| 429 | { \ |
| 430 | grctx *g = S_CREATE(grctx); \ |
| 431 | g->r.ops = &grops; \ |
| 432 | pre##_counterinit(&g->k, k, sz, 0); \ |
| 433 | return (&g->r); \ |
| 434 | } \ |
| 435 | \ |
| 436 | COUNTER_TEST(PRE, pre) |
| 437 | |
| 438 | /*----- Test rig ----------------------------------------------------------*/ |
| 439 | |
| 440 | #ifdef TEST_RIG |
| 441 | |
| 442 | #include <stdio.h> |
| 443 | |
| 444 | #include "daftstory.h" |
| 445 | |
| 446 | /* --- @COUNTER_TEST@ --- * |
| 447 | * |
| 448 | * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions |
| 449 | * |
| 450 | * Use: Standard test rig for counter functions. |
| 451 | */ |
| 452 | |
| 453 | #define COUNTER_TEST(PRE, pre) \ |
| 454 | \ |
| 455 | /* --- Initial plaintext for the test --- */ \ |
| 456 | \ |
| 457 | static const octet text[] = TEXT; \ |
| 458 | \ |
| 459 | /* --- Key and IV to use --- */ \ |
| 460 | \ |
| 461 | static const octet key[] = KEY; \ |
| 462 | static const octet iv[] = IV; \ |
| 463 | \ |
| 464 | /* --- Buffers for encryption and decryption output --- */ \ |
| 465 | \ |
| 466 | static octet ct[sizeof(text)]; \ |
| 467 | static octet pt[sizeof(text)]; \ |
| 468 | \ |
| 469 | static void hexdump(const octet *p, size_t sz) \ |
| 470 | { \ |
| 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) \ |
| 475 | putchar(':'); \ |
| 476 | } \ |
| 477 | } \ |
| 478 | \ |
| 479 | int main(void) \ |
| 480 | { \ |
| 481 | size_t sz = 0, rest; \ |
| 482 | pre##_counterctx ctx; \ |
| 483 | int status = 0; \ |
| 484 | int done = 0; \ |
| 485 | pre##_ctx k; \ |
| 486 | \ |
| 487 | size_t keysz = PRE##_KEYSZ ? \ |
| 488 | PRE##_KEYSZ : strlen((const char *)key); \ |
| 489 | \ |
| 490 | fputs(#pre "-counter: ", stdout); \ |
| 491 | \ |
| 492 | pre##_init(&k, key, keysz); \ |
| 493 | pre##_countersetkey(&ctx, &k); \ |
| 494 | \ |
| 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) { \ |
| 506 | done++; \ |
| 507 | if (sizeof(text) < 40 || done % 8 == 0) \ |
| 508 | fputc('.', stdout); \ |
| 509 | if (done % 480 == 0) \ |
| 510 | fputs("\n\t", stdout); \ |
| 511 | fflush(stdout); \ |
| 512 | } else { \ |
| 513 | printf("\nError (sz = %lu)\n", (unsigned long)sz); \ |
| 514 | status = 1; \ |
| 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); \ |
| 525 | } \ |
| 526 | if (sz < 63) \ |
| 527 | sz++; \ |
| 528 | else \ |
| 529 | sz += 9; \ |
| 530 | } \ |
| 531 | \ |
| 532 | fputs(status ? " failed\n" : " ok\n", stdout); \ |
| 533 | return (status); \ |
| 534 | } |
| 535 | |
| 536 | #else |
| 537 | # define COUNTER_TEST(PRE, pre) |
| 538 | #endif |
| 539 | |
| 540 | /*----- That's all, folks -------------------------------------------------*/ |
| 541 | |
| 542 | #ifdef __cplusplus |
| 543 | } |
| 544 | #endif |
| 545 | |
| 546 | #endif |