3 * Definitions electronic code book mode
5 * (c) 1999 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_ECB_DEF_H
29 #define CATACOMB_ECB_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 /* --- @ECB_DEF@ --- *
63 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
65 * Use: Creates an implementation for ECB stealing mode.
68 #define ECB_DEF(PRE, pre) \
70 /* --- @pre_ecbsetkey@ --- * \
72 * Arguments: @pre_ecbctx *ctx@ = pointer to ECB context block \
73 * @const pre_ctx *k@ = pointer to cipher context \
77 * Use: Sets the ECB context to use a different cipher key. \
80 void pre##_ecbsetkey(pre##_ecbctx *ctx, const pre##_ctx *k) \
85 /* --- @pre_ecbinit@ --- * \
87 * Arguments: @pre_ecbctx *ctx@ = pointer to cipher context \
88 * @const void *key@ = pointer to the key buffer \
89 * @size_t sz@ = size of the key \
90 * @const void *iv@ = pointer to initialization vector \
94 * Use: Initializes an ECB context ready for use. This is \
95 * equivalent to calls to @pre_init@ and @pre_setkey@. \
98 void pre##_ecbinit(pre##_ecbctx *ctx, \
99 const void *key, size_t sz, \
102 pre##_init(&ctx->ctx, key, sz); \
105 /* --- @pre_ecbencrypt@ --- * \
107 * Arguments: @pre_ecbctx *ctx@ = pointer to ECB context block \
108 * @const void *src@ = pointer to source data \
109 * @void *dest@ = pointer to destination data \
110 * @size_t sz@ = size of block to be encrypted \
114 * Use: Encrypts a block with a block cipher in ECB mode, with \
115 * ciphertext stealing and other clever tricks. \
116 * Essentially, data can be encrypted in arbitrary sized \
117 * chunks, although decryption must use the same chunks. \
120 void pre##_ecbencrypt(pre##_ecbctx *ctx, \
121 const void *src, void *dest, \
124 const octet *s = src; \
127 /* --- Empty blocks are trivial --- */ \
132 /* --- Short blocks aren't allowed in ECB --- * \
134 * There's absolutely nothing secure I can do with them. \
137 assert(((void)"ECB must have at least one whole block to work with", \
138 sz >= PRE##_BLKSZ)); \
140 /* --- Do the main chunk of encryption --- * \
142 * This will do the whole lot if it's a whole number of blocks. Just \
143 * give each block to the cipher in turn. This is trivial. \
147 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
148 uint32 x[PRE##_BLKSZ / 4]; \
152 BLKC_LOAD(PRE, x, s); \
155 pre##_eblk(&ctx->ctx, x, x); \
157 BLKC_STORE(PRE, d, x); \
163 /* --- Do the tail-end block and bit-left-over --- * \
165 * This isn't very efficient. That shouldn't matter much. \
169 uint32 x[PRE##_BLKSZ / 4]; \
170 octet b[PRE##_BLKSZ]; \
173 /* --- Let @sz@ be the size of the partial block --- */ \
177 /* --- First stage --- * \
179 * Read in the current block, and encrypt it. The first part of \
180 * the result is the partial ciphertext block. Don't write that \
181 * out yet, because I've not read the partial plaintext block. \
187 BLKC_LOAD(PRE, x, s); \
190 pre##_eblk(&ctx->ctx, x, x); \
191 BLKC_STORE(PRE, b, x); \
193 /* --- Second stage --- * \
195 * Now move in the partial plaintext block, writing out the \
196 * ciphertext as I go. Then encrypt, and write the complete \
197 * ciphertext block. \
200 if (d) d += PRE##_BLKSZ; \
201 for (i = 0; i < sz; i++) { \
202 register octet y = b[i]; \
206 BLKC_LOAD(PRE, x, b); \
207 pre##_eblk(&ctx->ctx, x, x); \
208 if (d) BLKC_STORE(PRE, d - PRE##_BLKSZ, x); \
216 /* --- @pre_ecbdecrypt@ --- * \
218 * Arguments: @pre_ecbctx *ctx@ = pointer to ECB context block \
219 * @const void *src@ = pointer to source data \
220 * @void *dest@ = pointer to destination data \
221 * @size_t sz@ = size of block to be encrypted \
225 * Use: Decrypts a block with a block cipher in ECB mode, with \
226 * ciphertext stealing and other clever tricks. \
227 * Essentially, data can be encrypted in arbitrary sized \
228 * chunks, although decryption must use the same chunks. \
231 void pre##_ecbdecrypt(pre##_ecbctx *ctx, \
232 const void *src, void *dest, \
235 const octet *s = src; \
238 /* --- Empty blocks are trivial --- */ \
243 /* --- Short blocks aren't allowed in ECB --- * \
245 * There's absolutely nothing secure I can do with them. \
248 assert(((void)"ECB must have at least one whole block to work with", \
249 sz >= PRE##_BLKSZ)); \
251 /* --- Do the main chunk of decryption --- * \
253 * This will do the whole lot if it's a whole number of blocks. \
254 * Each block is just handed to the block cipher in turn. \
257 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
258 uint32 x[PRE##_BLKSZ / 4]; \
259 BLKC_LOAD(PRE, x, s); \
260 pre##_dblk(&ctx->ctx, x, x); \
261 BLKC_STORE(PRE, d, x); \
267 /* --- Do the tail-end block and bit-left-over --- * \
269 * This isn't very efficient. That shouldn't matter much. \
273 uint32 x[PRE##_BLKSZ / 4]; \
274 octet b[PRE##_BLKSZ]; \
277 /* --- Let @sz@ be the size of the partial block --- */ \
281 /* --- First stage --- * \
283 * Take the complete ciphertext block, and decrypt it. This block \
284 * is carried over for the next encryption operation. \
287 BLKC_LOAD(PRE, x, s); \
288 pre##_dblk(&ctx->ctx, x, x); \
289 BLKC_STORE(PRE, b, x); \
291 /* --- Second stage --- * \
293 * The first few bytes are the partial plaintext block. Write that \
294 * and replace with the partial ciphertext block. Then decrypt \
295 * what's left as the complete plaintext. \
300 for (i = 0; i < sz; i++) { \
301 register octet y = s[i]; \
305 BLKC_LOAD(PRE, x, b); \
306 pre##_dblk(&ctx->ctx, x, x); \
307 BLKC_STORE(PRE, d - PRE##_BLKSZ, x); \
315 /* --- Generic cipher interface --- */ \
317 static const gcipher_ops gops; \
319 typedef struct gctx { \
324 static gcipher *ginit(const void *k, size_t sz) \
326 gctx *g = S_CREATE(gctx); \
328 pre##_ecbinit(&g->k, k, sz, 0); \
332 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
334 gctx *g = (gctx *)c; \
335 pre##_ecbencrypt(&g->k, s, t, sz); \
338 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
340 gctx *g = (gctx *)c; \
341 pre##_ecbdecrypt(&g->k, s, t, sz); \
344 static void gdestroy(gcipher *c) \
346 gctx *g = (gctx *)c; \
351 static const gcipher_ops gops = { \
353 gencrypt, gdecrypt, gdestroy, 0, 0 \
356 const gccipher pre##_ecb = { \
357 #pre "-ecb", pre##_keysz, PRE##_BLKSZ, \
363 /*----- Test rig ----------------------------------------------------------*/
369 #include "daftstory.h"
371 /* --- @ECB_TEST@ --- *
373 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
375 * Use: Standard test rig for ECB functions.
378 #define ECB_TEST(PRE, pre) \
380 /* --- Initial plaintext for the test --- */ \
382 static const octet text[] = TEXT; \
384 /* --- Key and IV to use --- */ \
386 static const octet key[] = KEY; \
387 static const octet iv[] = IV; \
389 /* --- Buffers for encryption and decryption output --- */ \
391 static octet ct[sizeof(text)]; \
392 static octet pt[sizeof(text)]; \
394 static void hexdump(const octet *p, size_t sz, size_t off) \
396 const octet *q = p + sz; \
397 for (sz = 0; p < q; p++, sz++) { \
398 printf("%02x", *p); \
399 if ((off + sz + 1) % PRE##_BLKSZ == 0) \
406 size_t sz = 0, rest; \
411 size_t keysz = PRE##_KEYSZ ? \
412 PRE##_KEYSZ : strlen((const char *)key); \
414 fputs(#pre "-ecb: ", stdout); \
416 pre##_ecbinit(&ctx, key, keysz, iv); \
418 while (sz <= sizeof(text)) { \
419 rest = sizeof(text) - sz; \
420 if ((sz != 0 && sz < PRE##_BLKSZ) || \
421 (rest != 0 && rest < PRE##_BLKSZ)) \
423 memcpy(ct, text, sizeof(text)); \
424 pre##_ecbencrypt(&ctx, ct, ct, sz); \
425 pre##_ecbencrypt(&ctx, ct + sz, ct + sz, rest); \
426 memcpy(pt, ct, sizeof(text)); \
427 pre##_ecbdecrypt(&ctx, pt, pt, sz); \
428 pre##_ecbdecrypt(&ctx, pt + sz, pt + sz, rest); \
429 if (memcmp(pt, text, sizeof(text)) == 0) { \
431 if (sizeof(text) < 40 || done % 8 == 0) \
432 fputc('.', stdout); \
433 if (done % 480 == 0) \
434 fputs("\n\t", stdout); \
437 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
439 printf("\tplaintext = "); hexdump(text, sz, 0); \
440 printf(", "); hexdump(text + sz, rest, sz); \
441 fputc('\n', stdout); \
442 printf("\tciphertext = "); hexdump(ct, sz, 0); \
443 printf(", "); hexdump(ct + sz, rest, sz); \
444 fputc('\n', stdout); \
445 printf("\trecovered text = "); hexdump(pt, sz, 0); \
446 printf(", "); hexdump(pt + sz, rest, sz); \
447 fputc('\n', stdout); \
448 fputc('\n', stdout); \
457 fputs(status ? " failed\n" : " ok\n", stdout); \
462 # define ECB_TEST(PRE, pre)
465 /*----- That's all, folks -------------------------------------------------*/