| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Definitions for ciphertext feedback mode |
| 4 | * |
| 5 | * (c) 1999 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_CFB_DEF_H |
| 29 | #define CATACOMB_CFB_DEF_H |
| 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | /*----- Header files ------------------------------------------------------*/ |
| 36 | |
| 37 | #include <string.h> |
| 38 | |
| 39 | #include <mLib/bits.h> |
| 40 | #include <mLib/sub.h> |
| 41 | |
| 42 | #ifndef CATACOMB_ARENA_H |
| 43 | # include "arena.h" |
| 44 | #endif |
| 45 | |
| 46 | #ifndef CATACOMB_BLKC_H |
| 47 | # include "blkc.h" |
| 48 | #endif |
| 49 | |
| 50 | #ifndef CATACOMB_GCIPHER_H |
| 51 | # include "gcipher.h" |
| 52 | #endif |
| 53 | |
| 54 | #ifndef CATACOMB_PARANOIA_H |
| 55 | # include "paranoia.h" |
| 56 | #endif |
| 57 | |
| 58 | #ifndef CATACOMB_PARANOIA_H |
| 59 | # include "paranoia.h" |
| 60 | #endif |
| 61 | |
| 62 | /*----- Macros ------------------------------------------------------------*/ |
| 63 | |
| 64 | /* --- @CFB_DEF@ --- * |
| 65 | * |
| 66 | * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher |
| 67 | * |
| 68 | * Use: Creates an implementation for CFB mode. |
| 69 | */ |
| 70 | |
| 71 | #define CFB_DEF(PRE, pre) CFB_DEFX(PRE, pre, #pre, #pre) |
| 72 | |
| 73 | #define CFB_DEFX(PRE, pre, name, fname) \ |
| 74 | \ |
| 75 | /* --- @pre_cfbgetiv@ --- * \ |
| 76 | * \ |
| 77 | * Arguments: @const pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 78 | * @void *iv@ = pointer to output data block \ |
| 79 | * \ |
| 80 | * Returns: --- \ |
| 81 | * \ |
| 82 | * Use: Reads the currently set IV. Reading and setting an IV \ |
| 83 | * is not transparent to the cipher. It will add a `step' \ |
| 84 | * which must be matched by a similar operation during \ |
| 85 | * decryption. \ |
| 86 | */ \ |
| 87 | \ |
| 88 | void pre##_cfbgetiv(const pre##_cfbctx *ctx, void *iv) \ |
| 89 | { \ |
| 90 | octet *p = iv; \ |
| 91 | unsigned off = ctx->off; \ |
| 92 | unsigned rest = PRE##_BLKSZ - off; \ |
| 93 | \ |
| 94 | memcpy(p, ctx->b + off, rest); \ |
| 95 | memcpy(p + rest, ctx->b, off); \ |
| 96 | } \ |
| 97 | \ |
| 98 | /* --- @pre_cfbsetiv@ --- * \ |
| 99 | * \ |
| 100 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 101 | * @cnost void *iv@ = pointer to IV to set \ |
| 102 | * \ |
| 103 | * Returns: --- \ |
| 104 | * \ |
| 105 | * Use: Sets the IV to use for subsequent encryption. \ |
| 106 | */ \ |
| 107 | \ |
| 108 | void pre##_cfbsetiv(pre##_cfbctx *ctx, const void *iv) \ |
| 109 | { memcpy(ctx->b, iv, PRE##_BLKSZ); ctx->off = PRE##_BLKSZ; } \ |
| 110 | \ |
| 111 | /* --- @pre_cfbbdry@ --- * \ |
| 112 | * \ |
| 113 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 114 | * \ |
| 115 | * Returns: --- \ |
| 116 | * \ |
| 117 | * Use: Inserts a boundary during encryption. Successful \ |
| 118 | * decryption must place a similar boundary. \ |
| 119 | */ \ |
| 120 | \ |
| 121 | void pre##_cfbbdry(pre##_cfbctx *ctx) \ |
| 122 | { \ |
| 123 | uint32 t[PRE##_BLKSZ/4]; \ |
| 124 | \ |
| 125 | BLKC_LOAD(PRE, t, ctx->b); \ |
| 126 | pre##_eblk(&ctx->ctx, t, t); \ |
| 127 | BLKC_STORE(PRE, ctx->b, t); \ |
| 128 | ctx->off = PRE##_BLKSZ; \ |
| 129 | BURN(t); \ |
| 130 | } \ |
| 131 | \ |
| 132 | /* --- @pre_cfbsetkey@ --- * \ |
| 133 | * \ |
| 134 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 135 | * @const pre_ctx *k@ = pointer to cipher context \ |
| 136 | * \ |
| 137 | * Returns: --- \ |
| 138 | * \ |
| 139 | * Use: Sets the CFB context to use a different cipher key. \ |
| 140 | */ \ |
| 141 | \ |
| 142 | void pre##_cfbsetkey(pre##_cfbctx *ctx, const pre##_ctx *k) \ |
| 143 | { ctx->ctx = *k; ctx->off = PRE##_BLKSZ; } \ |
| 144 | \ |
| 145 | /* --- @pre_cfbinit@ --- * \ |
| 146 | * \ |
| 147 | * Arguments: @pre_cfbctx *ctx@ = pointer to cipher context \ |
| 148 | * @const void *key@ = pointer to the key buffer \ |
| 149 | * @size_t sz@ = size of the key \ |
| 150 | * @const void *iv@ = pointer to initialization vector \ |
| 151 | * \ |
| 152 | * Returns: --- \ |
| 153 | * \ |
| 154 | * Use: Initializes a CFB context ready for use. You should \ |
| 155 | * ensure that the IV chosen is unique: reusing an IV will \ |
| 156 | * compromise the security of at least the first block \ |
| 157 | * encrypted. This is equivalent to calls to @pre_init@, \ |
| 158 | * @pre_cfbsetkey@ and @pre_cfbsetiv@. \ |
| 159 | */ \ |
| 160 | \ |
| 161 | void pre##_cfbinit(pre##_cfbctx *ctx, \ |
| 162 | const void *key, size_t sz, \ |
| 163 | const void *iv) \ |
| 164 | { \ |
| 165 | static const octet zero[PRE##_BLKSZ] = { 0 }; \ |
| 166 | \ |
| 167 | pre##_init(&ctx->ctx, key, sz); \ |
| 168 | pre##_cfbsetiv(ctx, iv ? iv : zero); \ |
| 169 | } \ |
| 170 | \ |
| 171 | /* --- @pre_cfbencrypt@ --- * \ |
| 172 | * \ |
| 173 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 174 | * @const void *src@ = pointer to source data \ |
| 175 | * @void *dest@ = pointer to destination data \ |
| 176 | * @size_t sz@ = size of block to be encrypted \ |
| 177 | * \ |
| 178 | * Returns: --- \ |
| 179 | * \ |
| 180 | * Use: Encrypts a block with a block cipher in CFB mode. The \ |
| 181 | * input block may be arbitrary in size. CFB mode is not \ |
| 182 | * sensitive to block boundaries. \ |
| 183 | */ \ |
| 184 | \ |
| 185 | void pre##_cfbencrypt(pre##_cfbctx *ctx, \ |
| 186 | const void *src, void *dest, \ |
| 187 | size_t sz) \ |
| 188 | { \ |
| 189 | const octet *s = src; \ |
| 190 | octet *d = dest; \ |
| 191 | unsigned off = ctx->off; \ |
| 192 | uint32 t[PRE##_BLKSZ/4]; \ |
| 193 | octet y; \ |
| 194 | \ |
| 195 | /* --- Empty blocks are trivial --- */ \ |
| 196 | \ |
| 197 | if (!sz) return; \ |
| 198 | \ |
| 199 | /* --- If I can deal with the block from my buffer, do that --- */ \ |
| 200 | \ |
| 201 | if (sz < PRE##_BLKSZ - off) goto small; \ |
| 202 | \ |
| 203 | /* --- Finish off what's left in my buffer --- */ \ |
| 204 | \ |
| 205 | while (off < PRE##_BLKSZ) { \ |
| 206 | y = s ? *s++ : 0; \ |
| 207 | ctx->b[off] ^= y; \ |
| 208 | if (d) *d++ = ctx->b[off]; \ |
| 209 | off++; sz--; \ |
| 210 | } \ |
| 211 | \ |
| 212 | /* --- Main encryption loop --- */ \ |
| 213 | \ |
| 214 | BLKC_LOAD(PRE, t, ctx->b); \ |
| 215 | \ |
| 216 | for (;;) { \ |
| 217 | pre##_eblk(&ctx->ctx, t, t); \ |
| 218 | if (sz < PRE##_BLKSZ) break; \ |
| 219 | if (s) { BLKC_XLOAD(PRE, t, s); s += PRE##_BLKSZ; } \ |
| 220 | if (d) { BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ; } \ |
| 221 | sz -= PRE##_BLKSZ; \ |
| 222 | } \ |
| 223 | \ |
| 224 | BLKC_STORE(PRE, ctx->b, t); \ |
| 225 | off = 0; \ |
| 226 | \ |
| 227 | /* --- Tidying up the tail end --- */ \ |
| 228 | \ |
| 229 | if (sz) { \ |
| 230 | small: \ |
| 231 | do { \ |
| 232 | y = s ? *s++ : 0; \ |
| 233 | ctx->b[off] ^= y; \ |
| 234 | if (d) *d++ = ctx->b[off]; \ |
| 235 | off++; sz--; \ |
| 236 | } while (sz); \ |
| 237 | } \ |
| 238 | \ |
| 239 | /* --- Done --- */ \ |
| 240 | \ |
| 241 | ctx->off = off; \ |
| 242 | return; \ |
| 243 | } \ |
| 244 | \ |
| 245 | /* --- @pre_cfbdecrypt@ --- * \ |
| 246 | * \ |
| 247 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 248 | * @const void *src@ = pointer to source data \ |
| 249 | * @void *dest@ = pointer to destination data \ |
| 250 | * @size_t sz@ = size of block to be encrypted \ |
| 251 | * \ |
| 252 | * Returns: --- \ |
| 253 | * \ |
| 254 | * Use: Decrypts a block with a block cipher in CFB mode. The \ |
| 255 | * input block may be arbitrary in size. CFB mode is not \ |
| 256 | * sensitive to block boundaries. \ |
| 257 | */ \ |
| 258 | \ |
| 259 | void pre##_cfbdecrypt(pre##_cfbctx *ctx, \ |
| 260 | const void *src, void *dest, \ |
| 261 | size_t sz) \ |
| 262 | { \ |
| 263 | const octet *s = src; \ |
| 264 | octet *d = dest; \ |
| 265 | unsigned off = ctx->off; \ |
| 266 | uint32 t[PRE##_BLKSZ/4], u[PRE##_BLKSZ/4]; \ |
| 267 | octet y; \ |
| 268 | \ |
| 269 | /* --- Empty blocks are trivial --- */ \ |
| 270 | \ |
| 271 | if (!sz) return; \ |
| 272 | \ |
| 273 | /* --- If I can deal with the block from my buffer, do that --- */ \ |
| 274 | \ |
| 275 | if (sz < PRE##_BLKSZ - off) goto small; \ |
| 276 | \ |
| 277 | /* --- Finish off what's left in my buffer --- */ \ |
| 278 | \ |
| 279 | while (off < PRE##_BLKSZ) \ |
| 280 | { y = *s++; *d++ = ctx->b[off] ^ y; ctx->b[off++] = y; sz--; } \ |
| 281 | \ |
| 282 | /* --- Main encryption loop --- */ \ |
| 283 | \ |
| 284 | BLKC_LOAD(PRE, t, ctx->b); \ |
| 285 | \ |
| 286 | for (;;) { \ |
| 287 | pre##_eblk(&ctx->ctx, t, t); \ |
| 288 | if (sz < PRE##_BLKSZ) break; \ |
| 289 | BLKC_LOAD(PRE, u, s); s += PRE##_BLKSZ; \ |
| 290 | BLKC_XSTORE(PRE, d, t, u); d += PRE##_BLKSZ; \ |
| 291 | BLKC_MOVE(PRE, t, u); \ |
| 292 | sz -= PRE##_BLKSZ; \ |
| 293 | } \ |
| 294 | \ |
| 295 | BLKC_STORE(PRE, ctx->b, t); \ |
| 296 | off = 0; \ |
| 297 | \ |
| 298 | /* --- Tidying up the tail end --- */ \ |
| 299 | \ |
| 300 | if (sz) { \ |
| 301 | small: \ |
| 302 | do { y = *s++; *d++ = ctx->b[off] ^ y; ctx->b[off++] = y; sz--; } \ |
| 303 | while (sz); \ |
| 304 | } \ |
| 305 | \ |
| 306 | /* --- Done --- */ \ |
| 307 | \ |
| 308 | ctx->off = off; \ |
| 309 | return; \ |
| 310 | } \ |
| 311 | \ |
| 312 | /* --- Generic cipher interface --- */ \ |
| 313 | \ |
| 314 | static const gcipher_ops gops; \ |
| 315 | \ |
| 316 | typedef struct gctx { \ |
| 317 | gcipher c; \ |
| 318 | pre##_cfbctx k; \ |
| 319 | } gctx; \ |
| 320 | \ |
| 321 | static gcipher *ginit(const void *k, size_t sz) \ |
| 322 | { \ |
| 323 | gctx *g = S_CREATE(gctx); \ |
| 324 | g->c.ops = &gops; \ |
| 325 | pre##_cfbinit(&g->k, k, sz, 0); \ |
| 326 | return (&g->c); \ |
| 327 | } \ |
| 328 | \ |
| 329 | static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \ |
| 330 | { gctx *g = (gctx *)c; pre##_cfbencrypt(&g->k, s, t, sz); } \ |
| 331 | \ |
| 332 | static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \ |
| 333 | { gctx *g = (gctx *)c; pre##_cfbdecrypt(&g->k, s, t, sz); } \ |
| 334 | \ |
| 335 | static void gdestroy(gcipher *c) \ |
| 336 | { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); } \ |
| 337 | \ |
| 338 | static void gsetiv(gcipher *c, const void *iv) \ |
| 339 | { gctx *g = (gctx *)c; pre##_cfbsetiv(&g->k, iv); } \ |
| 340 | \ |
| 341 | static void gbdry(gcipher *c) \ |
| 342 | { gctx *g = (gctx *)c; pre##_cfbbdry(&g->k); } \ |
| 343 | \ |
| 344 | static const gcipher_ops gops = { \ |
| 345 | &pre##_cfb, \ |
| 346 | gencrypt, gdecrypt, gdestroy, gsetiv, gbdry \ |
| 347 | }; \ |
| 348 | \ |
| 349 | const gccipher pre##_cfb = { \ |
| 350 | name "-cfb", pre##_keysz, PRE##_BLKSZ, \ |
| 351 | ginit \ |
| 352 | }; \ |
| 353 | \ |
| 354 | CFB_TESTX(PRE, pre, name, fname) |
| 355 | |
| 356 | /*----- Test rig ----------------------------------------------------------*/ |
| 357 | |
| 358 | #define CFB_TEST(PRE, pre) CFB_TESTX(PRE, pre, #pre, #pre) |
| 359 | |
| 360 | #ifdef TEST_RIG |
| 361 | |
| 362 | #include "modes-test.h" |
| 363 | |
| 364 | /* --- @CFB_TEST@ --- * |
| 365 | * |
| 366 | * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions |
| 367 | * |
| 368 | * Use: Standard test rig for CFB functions. |
| 369 | */ |
| 370 | |
| 371 | #define CFB_TESTX(PRE, pre, name, fname) \ |
| 372 | \ |
| 373 | static pre##_ctx key; \ |
| 374 | static pre##_cfbctx ctx; \ |
| 375 | \ |
| 376 | static void pre##_cfb_test_setup(const octet *k, size_t ksz) \ |
| 377 | { pre##_init(&key, k, ksz); pre##_cfbsetkey(&ctx, &key); } \ |
| 378 | \ |
| 379 | static void pre##_cfb_test_reset(const octet *iv) \ |
| 380 | { pre##_cfbsetiv(&ctx, iv); } \ |
| 381 | \ |
| 382 | static void pre##_cfb_test_enc(const octet *s, octet *d, size_t sz) \ |
| 383 | { pre##_cfbencrypt(&ctx, s, d, sz); } \ |
| 384 | \ |
| 385 | static void pre##_cfb_test_dec(const octet *s, octet *d, size_t sz) \ |
| 386 | { pre##_cfbdecrypt(&ctx, s, d, sz); } \ |
| 387 | \ |
| 388 | int main(int argc, char *argv[]) \ |
| 389 | { \ |
| 390 | return test_encmode(fname "-cfb", PRE##_KEYSZ, PRE##_BLKSZ, 1, 0, \ |
| 391 | pre##_cfb_test_setup, pre##_cfb_test_reset, \ |
| 392 | pre##_cfb_test_enc, pre##_cfb_test_dec, \ |
| 393 | argc, argv); \ |
| 394 | } |
| 395 | |
| 396 | #else |
| 397 | # define CFB_TESTX(PRE, pre, name, fname) |
| 398 | #endif |
| 399 | |
| 400 | /*----- That's all, folks -------------------------------------------------*/ |
| 401 | |
| 402 | #ifdef __cplusplus |
| 403 | } |
| 404 | #endif |
| 405 | |
| 406 | #endif |