| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Block cipher counter mode (or long cycle mode) |
| 4 | * |
| 5 | * (c) 2000 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Catacomb. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | #ifndef CATACOMB_COUNTER_DEF_H |
| 29 | #define CATACOMB_COUNTER_DEF_H |
| 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | /*----- Header files ------------------------------------------------------*/ |
| 36 | |
| 37 | #include <stdarg.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include <mLib/bits.h> |
| 41 | #include <mLib/sub.h> |
| 42 | |
| 43 | #ifndef CATACOMB_ARENA_H |
| 44 | # include "arena.h" |
| 45 | #endif |
| 46 | |
| 47 | #ifndef CATACOMB_BLKC_H |
| 48 | # include "blkc.h" |
| 49 | #endif |
| 50 | |
| 51 | #ifndef CATACOMB_GCIPHER_H |
| 52 | # include "gcipher.h" |
| 53 | #endif |
| 54 | |
| 55 | #ifndef CATACOMB_PARANOIA_H |
| 56 | # include "paranoia.h" |
| 57 | #endif |
| 58 | |
| 59 | /*----- Macros ------------------------------------------------------------*/ |
| 60 | |
| 61 | /* --- @COUNTER_DEF@ --- * |
| 62 | * |
| 63 | * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher |
| 64 | * |
| 65 | * Use: Creates definitions for counter mode. |
| 66 | */ |
| 67 | |
| 68 | #define COUNTER_DEF(PRE, pre) COUNTER_DEFX(PRE, pre, #pre, #pre) |
| 69 | |
| 70 | #define COUNTER_DEFX(PRE, pre, name, fname) \ |
| 71 | \ |
| 72 | /* --- @pre_countergetiv@ --- * \ |
| 73 | * \ |
| 74 | * Arguments: @const pre_counterctx *ctx@ = pointer to counter \ |
| 75 | * context \ |
| 76 | * @void *iv@ = pointer to output data block \ |
| 77 | * \ |
| 78 | * Returns: --- \ |
| 79 | * \ |
| 80 | * Use: Reads the currently set IV. Reading and setting an IV \ |
| 81 | * is not transparent to the cipher. It will add a `step' \ |
| 82 | * which must be matched by a similar operation during \ |
| 83 | * decryption. \ |
| 84 | */ \ |
| 85 | \ |
| 86 | void pre##_countergetiv(const pre##_counterctx *ctx, void *iv) \ |
| 87 | { \ |
| 88 | BLKC_STORE(PRE, iv, ctx->n); \ |
| 89 | } \ |
| 90 | \ |
| 91 | /* --- @pre_countersetiv@ --- * \ |
| 92 | * \ |
| 93 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
| 94 | * @cnost void *iv@ = pointer to IV to set \ |
| 95 | * \ |
| 96 | * Returns: --- \ |
| 97 | * \ |
| 98 | * Use: Sets the IV to use for subsequent encryption. \ |
| 99 | */ \ |
| 100 | \ |
| 101 | void pre##_countersetiv(pre##_counterctx *ctx, const void *iv) \ |
| 102 | { \ |
| 103 | BLKC_LOAD(PRE, ctx->n, iv); \ |
| 104 | ctx->off = PRE##_BLKSZ; \ |
| 105 | } \ |
| 106 | \ |
| 107 | /* --- @pre_counterbdry@ --- * \ |
| 108 | * \ |
| 109 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
| 110 | * \ |
| 111 | * Returns: --- \ |
| 112 | * \ |
| 113 | * Use: Inserts a boundary during encryption. Successful \ |
| 114 | * decryption must place a similar boundary. \ |
| 115 | */ \ |
| 116 | \ |
| 117 | void pre##_counterbdry(pre##_counterctx *ctx) \ |
| 118 | { \ |
| 119 | BLKC_STEP(PRE, ctx->n); \ |
| 120 | ctx->off = PRE##_BLKSZ; \ |
| 121 | } \ |
| 122 | \ |
| 123 | /* --- @pre_countersetkey@ --- * \ |
| 124 | * \ |
| 125 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
| 126 | * @const pre_ctx *k@ = pointer to cipher context \ |
| 127 | * \ |
| 128 | * Returns: --- \ |
| 129 | * \ |
| 130 | * Use: Sets the counter context to use a different cipher key. \ |
| 131 | */ \ |
| 132 | \ |
| 133 | void pre##_countersetkey(pre##_counterctx *ctx, const pre##_ctx *k) \ |
| 134 | { \ |
| 135 | ctx->ctx = *k; \ |
| 136 | } \ |
| 137 | \ |
| 138 | /* --- @pre_counterinit@ --- * \ |
| 139 | * \ |
| 140 | * Arguments: @pre_counterctx *ctx@ = pointer to cipher context \ |
| 141 | * @const void *key@ = pointer to the key buffer \ |
| 142 | * @size_t sz@ = size of the key \ |
| 143 | * @const void *iv@ = pointer to initialization vector \ |
| 144 | * \ |
| 145 | * Returns: --- \ |
| 146 | * \ |
| 147 | * Use: Initializes a counter context ready for use. You \ |
| 148 | * should ensure that the IV chosen is unique: reusing an \ |
| 149 | * IV will compromise the security of the entire \ |
| 150 | * plaintext. This is equivalent to calls to @pre_init@, \ |
| 151 | * @pre_countersetkey@ and @pre_countersetiv@. \ |
| 152 | */ \ |
| 153 | \ |
| 154 | void pre##_counterinit(pre##_counterctx *ctx, \ |
| 155 | const void *key, size_t sz, \ |
| 156 | const void *iv) \ |
| 157 | { \ |
| 158 | static const octet zero[PRE##_BLKSZ] = { 0 }; \ |
| 159 | pre##_init(&ctx->ctx, key, sz); \ |
| 160 | pre##_countersetiv(ctx, iv ? iv : zero); \ |
| 161 | } \ |
| 162 | \ |
| 163 | /* --- @pre_counterencrypt@ --- * \ |
| 164 | * \ |
| 165 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
| 166 | * @const void *src@ = pointer to source data \ |
| 167 | * @void *dest@ = pointer to destination data \ |
| 168 | * @size_t sz@ = size of block to be encrypted \ |
| 169 | * \ |
| 170 | * Returns: --- \ |
| 171 | * \ |
| 172 | * Use: Encrypts or decrypts a block with a block cipher in \ |
| 173 | * counter mode: encryption and decryption are the same in \ |
| 174 | * counter. The destination may be null to just churn the \ |
| 175 | * feedback round for a bit. The source may be null to \ |
| 176 | * use the cipher as a random data generator. \ |
| 177 | */ \ |
| 178 | \ |
| 179 | void pre##_counterencrypt(pre##_counterctx *ctx, \ |
| 180 | const void *src, void *dest, \ |
| 181 | size_t sz) \ |
| 182 | { \ |
| 183 | const octet *s = src; \ |
| 184 | octet *d = dest; \ |
| 185 | unsigned off = ctx->off; \ |
| 186 | \ |
| 187 | /* --- Empty blocks are trivial --- */ \ |
| 188 | \ |
| 189 | if (!sz) \ |
| 190 | return; \ |
| 191 | \ |
| 192 | /* --- If I can deal with the block from my buffer, do that --- */ \ |
| 193 | \ |
| 194 | if (sz < PRE##_BLKSZ - off) \ |
| 195 | goto small; \ |
| 196 | \ |
| 197 | /* --- Finish off what's left in my buffer --- */ \ |
| 198 | \ |
| 199 | if (!d) \ |
| 200 | sz -= PRE##_BLKSZ - off; \ |
| 201 | else { \ |
| 202 | while (off < PRE##_BLKSZ) { \ |
| 203 | register octet x = s ? *s++ : 0; \ |
| 204 | *d++ = ctx->buf[off++] ^ x; \ |
| 205 | sz--; \ |
| 206 | } \ |
| 207 | } \ |
| 208 | \ |
| 209 | /* --- Main encryption loop --- */ \ |
| 210 | \ |
| 211 | { \ |
| 212 | uint32 n[PRE##_BLKSZ / 4]; \ |
| 213 | \ |
| 214 | for (;;) { \ |
| 215 | pre##_eblk(&ctx->ctx, ctx->n, n); \ |
| 216 | BLKC_STEP(PRE, ctx->n); \ |
| 217 | if (sz < PRE##_BLKSZ) \ |
| 218 | break; \ |
| 219 | if (d) { \ |
| 220 | if (!s) \ |
| 221 | BLKC_STORE(PRE, d, n); \ |
| 222 | else { \ |
| 223 | uint32 x[PRE##_BLKSZ / 4]; \ |
| 224 | BLKC_LOAD(PRE, x, s); \ |
| 225 | BLKC_XSTORE(PRE, d, n, x); \ |
| 226 | s += PRE##_BLKSZ; \ |
| 227 | } \ |
| 228 | d += PRE##_BLKSZ; \ |
| 229 | } \ |
| 230 | sz -= PRE##_BLKSZ; \ |
| 231 | } \ |
| 232 | \ |
| 233 | BLKC_STORE(PRE, ctx->buf, n); \ |
| 234 | off = 0; \ |
| 235 | } \ |
| 236 | \ |
| 237 | /* --- Tidying up the tail end --- */ \ |
| 238 | \ |
| 239 | if (sz) { \ |
| 240 | small: \ |
| 241 | if (!d) \ |
| 242 | off += sz; \ |
| 243 | else do { \ |
| 244 | register octet x = s ? *s++ : 0; \ |
| 245 | *d++ = ctx->buf[off++] ^ x; \ |
| 246 | sz--; \ |
| 247 | } while (sz); \ |
| 248 | } \ |
| 249 | \ |
| 250 | /* --- Done --- */ \ |
| 251 | \ |
| 252 | ctx->off = off; \ |
| 253 | return; \ |
| 254 | } \ |
| 255 | \ |
| 256 | /* --- Generic cipher interface --- */ \ |
| 257 | \ |
| 258 | static const gcipher_ops gops; \ |
| 259 | \ |
| 260 | typedef struct gctx { \ |
| 261 | gcipher c; \ |
| 262 | pre##_counterctx k; \ |
| 263 | } gctx; \ |
| 264 | \ |
| 265 | static gcipher *ginit(const void *k, size_t sz) \ |
| 266 | { \ |
| 267 | gctx *g = S_CREATE(gctx); \ |
| 268 | g->c.ops = &gops; \ |
| 269 | pre##_counterinit(&g->k, k, sz, 0); \ |
| 270 | return (&g->c); \ |
| 271 | } \ |
| 272 | \ |
| 273 | static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \ |
| 274 | { \ |
| 275 | gctx *g = (gctx *)c; \ |
| 276 | pre##_counterencrypt(&g->k, s, t, sz); \ |
| 277 | } \ |
| 278 | \ |
| 279 | static void gdestroy(gcipher *c) \ |
| 280 | { \ |
| 281 | gctx *g = (gctx *)c; \ |
| 282 | BURN(*g); \ |
| 283 | S_DESTROY(g); \ |
| 284 | } \ |
| 285 | \ |
| 286 | static void gsetiv(gcipher *c, const void *iv) \ |
| 287 | { \ |
| 288 | gctx *g = (gctx *)c; \ |
| 289 | pre##_countersetiv(&g->k, iv); \ |
| 290 | } \ |
| 291 | \ |
| 292 | static void gbdry(gcipher *c) \ |
| 293 | { \ |
| 294 | gctx *g = (gctx *)c; \ |
| 295 | pre##_counterbdry(&g->k); \ |
| 296 | } \ |
| 297 | \ |
| 298 | static const gcipher_ops gops = { \ |
| 299 | &pre##_counter, \ |
| 300 | gencrypt, gencrypt, gdestroy, gsetiv, gbdry \ |
| 301 | }; \ |
| 302 | \ |
| 303 | const gccipher pre##_counter = { \ |
| 304 | name "-counter", pre##_keysz, PRE##_BLKSZ, \ |
| 305 | ginit \ |
| 306 | }; \ |
| 307 | \ |
| 308 | /* --- Generic random number generator interface --- */ \ |
| 309 | \ |
| 310 | typedef struct grctx { \ |
| 311 | grand r; \ |
| 312 | pre##_counterctx k; \ |
| 313 | } grctx; \ |
| 314 | \ |
| 315 | static void grdestroy(grand *r) \ |
| 316 | { \ |
| 317 | grctx *g = (grctx *)r; \ |
| 318 | BURN(*g); \ |
| 319 | S_DESTROY(g); \ |
| 320 | } \ |
| 321 | \ |
| 322 | static int grmisc(grand *r, unsigned op, ...) \ |
| 323 | { \ |
| 324 | grctx *g = (grctx *)r; \ |
| 325 | va_list ap; \ |
| 326 | int rc = 0; \ |
| 327 | octet buf[PRE##_BLKSZ]; \ |
| 328 | va_start(ap, op); \ |
| 329 | \ |
| 330 | switch (op) { \ |
| 331 | case GRAND_CHECK: \ |
| 332 | switch (va_arg(ap, unsigned)) { \ |
| 333 | case GRAND_CHECK: \ |
| 334 | case GRAND_SEEDINT: \ |
| 335 | case GRAND_SEEDUINT32: \ |
| 336 | case GRAND_SEEDBLOCK: \ |
| 337 | case GRAND_SEEDRAND: \ |
| 338 | rc = 1; \ |
| 339 | break; \ |
| 340 | default: \ |
| 341 | rc = 0; \ |
| 342 | break; \ |
| 343 | } \ |
| 344 | break; \ |
| 345 | case GRAND_SEEDINT: \ |
| 346 | BLKC_SET(PRE, g->k.n, va_arg(ap, unsigned)); \ |
| 347 | g->k.off = PRE##_BLKSZ; \ |
| 348 | break; \ |
| 349 | case GRAND_SEEDUINT32: \ |
| 350 | BLKC_SET(PRE, g->k.n, va_arg(ap, uint32)); \ |
| 351 | g->k.off = PRE##_BLKSZ; \ |
| 352 | break; \ |
| 353 | case GRAND_SEEDBLOCK: { \ |
| 354 | const void *p = va_arg(ap, const void *); \ |
| 355 | size_t sz = va_arg(ap, size_t); \ |
| 356 | if (sz < sizeof(buf)) { \ |
| 357 | memset(buf, 0, sizeof(buf)); \ |
| 358 | memcpy(buf, p, sz); \ |
| 359 | p = buf; \ |
| 360 | } \ |
| 361 | pre##_countersetiv(&g->k, p); \ |
| 362 | } break; \ |
| 363 | case GRAND_SEEDRAND: { \ |
| 364 | grand *rr = va_arg(ap, grand *); \ |
| 365 | rr->ops->fill(rr, buf, sizeof(buf)); \ |
| 366 | pre##_countersetiv(&g->k, buf); \ |
| 367 | } break; \ |
| 368 | default: \ |
| 369 | GRAND_BADOP; \ |
| 370 | break; \ |
| 371 | } \ |
| 372 | \ |
| 373 | va_end(ap); \ |
| 374 | return (rc); \ |
| 375 | } \ |
| 376 | \ |
| 377 | static octet grbyte(grand *r) \ |
| 378 | { \ |
| 379 | grctx *g = (grctx *)r; \ |
| 380 | octet o; \ |
| 381 | pre##_counterencrypt(&g->k, 0, &o, 1); \ |
| 382 | return (o); \ |
| 383 | } \ |
| 384 | \ |
| 385 | static uint32 grword(grand *r) \ |
| 386 | { \ |
| 387 | grctx *g = (grctx *)r; \ |
| 388 | octet b[4]; \ |
| 389 | pre##_counterencrypt(&g->k, 0, b, sizeof(b)); \ |
| 390 | return (LOAD32(b)); \ |
| 391 | } \ |
| 392 | \ |
| 393 | static void grfill(grand *r, void *p, size_t sz) \ |
| 394 | { \ |
| 395 | grctx *g = (grctx *)r; \ |
| 396 | pre##_counterencrypt(&g->k, 0, p, sz); \ |
| 397 | } \ |
| 398 | \ |
| 399 | static const grand_ops grops = { \ |
| 400 | name "-counter", \ |
| 401 | GRAND_CRYPTO, 0, \ |
| 402 | grmisc, grdestroy, \ |
| 403 | grword, grbyte, grword, grand_defaultrange, grfill \ |
| 404 | }; \ |
| 405 | \ |
| 406 | /* --- @pre_counterrand@ --- * \ |
| 407 | * \ |
| 408 | * Arguments: @const void *k@ = pointer to key material \ |
| 409 | * @size_t sz@ = size of key material \ |
| 410 | * \ |
| 411 | * Returns: Pointer to generic random number generator interface. \ |
| 412 | * \ |
| 413 | * Use: Creates a random number interface wrapper around an \ |
| 414 | * counter-mode block cipher. \ |
| 415 | */ \ |
| 416 | \ |
| 417 | grand *pre##_counterrand(const void *k, size_t sz) \ |
| 418 | { \ |
| 419 | grctx *g = S_CREATE(grctx); \ |
| 420 | g->r.ops = &grops; \ |
| 421 | pre##_counterinit(&g->k, k, sz, 0); \ |
| 422 | return (&g->r); \ |
| 423 | } \ |
| 424 | \ |
| 425 | COUNTER_TESTX(PRE, pre, name, fname) |
| 426 | |
| 427 | /*----- Test rig ----------------------------------------------------------*/ |
| 428 | |
| 429 | #define COUNTER_TEST(PRE, pre) COUNTER_TESTX(PRE, pre, #pre, #pre) |
| 430 | |
| 431 | #ifdef TEST_RIG |
| 432 | |
| 433 | #include "modes-test.h" |
| 434 | |
| 435 | /* --- @COUNTER_TEST@ --- * |
| 436 | * |
| 437 | * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions |
| 438 | * |
| 439 | * Use: Standard test rig for counter functions. |
| 440 | */ |
| 441 | |
| 442 | #define COUNTER_TESTX(PRE, pre, name, fname) \ |
| 443 | \ |
| 444 | static pre##_ctx key; \ |
| 445 | static pre##_counterctx ctx; \ |
| 446 | \ |
| 447 | static void pre##_counter_test_setup(const octet *k, size_t ksz) \ |
| 448 | { pre##_init(&key, k, ksz); pre##_countersetkey(&ctx, &key); } \ |
| 449 | \ |
| 450 | static void pre##_counter_test_reset(const octet *iv) \ |
| 451 | { pre##_countersetiv(&ctx, iv); } \ |
| 452 | \ |
| 453 | static void pre##_counter_test_enc(const octet *s, octet *d, size_t sz) \ |
| 454 | { pre##_counterencrypt(&ctx, s, d, sz); } \ |
| 455 | \ |
| 456 | int main(int argc, char *argv[]) \ |
| 457 | { \ |
| 458 | return test_encmode(fname "-counter", PRE##_KEYSZ, PRE##_BLKSZ, 1, 0, \ |
| 459 | pre##_counter_test_setup, pre##_counter_test_reset, \ |
| 460 | pre##_counter_test_enc, pre##_counter_test_enc, \ |
| 461 | argc, argv); \ |
| 462 | } |
| 463 | |
| 464 | #else |
| 465 | # define COUNTER_TESTX(PRE, pre, name, fname) |
| 466 | #endif |
| 467 | |
| 468 | /*----- That's all, folks -------------------------------------------------*/ |
| 469 | |
| 470 | #ifdef __cplusplus |
| 471 | } |
| 472 | #endif |
| 473 | |
| 474 | #endif |