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) ECB_DEFX(PRE, pre, #pre, #pre)
70 #define ECB_DEFX(PRE, pre, name, fname) \
72 /* --- @pre_ecbsetkey@ --- * \
74 * Arguments: @pre_ecbctx *ctx@ = pointer to ECB context block \
75 * @const pre_ctx *k@ = pointer to cipher context \
79 * Use: Sets the ECB context to use a different cipher key. \
82 void pre##_ecbsetkey(pre##_ecbctx *ctx, const pre##_ctx *k) \
87 /* --- @pre_ecbinit@ --- * \
89 * Arguments: @pre_ecbctx *ctx@ = pointer to cipher context \
90 * @const void *key@ = pointer to the key buffer \
91 * @size_t sz@ = size of the key \
92 * @const void *iv@ = pointer to initialization vector \
96 * Use: Initializes an ECB context ready for use. This is \
97 * equivalent to calls to @pre_init@ and @pre_setkey@. \
100 void pre##_ecbinit(pre##_ecbctx *ctx, \
101 const void *key, size_t sz, \
104 pre##_init(&ctx->ctx, key, sz); \
107 /* --- @pre_ecbencrypt@ --- * \
109 * Arguments: @pre_ecbctx *ctx@ = pointer to ECB context block \
110 * @const void *src@ = pointer to source data \
111 * @void *dest@ = pointer to destination data \
112 * @size_t sz@ = size of block to be encrypted \
116 * Use: Encrypts a block with a block cipher in ECB mode, with \
117 * ciphertext stealing and other clever tricks. \
118 * Essentially, data can be encrypted in arbitrary sized \
119 * chunks, although decryption must use the same chunks. \
122 void pre##_ecbencrypt(pre##_ecbctx *ctx, \
123 const void *src, void *dest, \
126 const octet *s = src; \
129 /* --- Empty blocks are trivial --- */ \
134 /* --- Short blocks aren't allowed in ECB --- * \
136 * There's absolutely nothing secure I can do with them. \
139 assert(((void)"ECB must have at least one whole block to work with", \
140 sz >= PRE##_BLKSZ)); \
142 /* --- Do the main chunk of encryption --- * \
144 * This will do the whole lot if it's a whole number of blocks. Just \
145 * give each block to the cipher in turn. This is trivial. \
149 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
150 uint32 x[PRE##_BLKSZ / 4]; \
154 BLKC_LOAD(PRE, x, s); \
157 pre##_eblk(&ctx->ctx, x, x); \
159 BLKC_STORE(PRE, d, x); \
165 /* --- Do the tail-end block and bit-left-over --- * \
167 * This isn't very efficient. That shouldn't matter much. \
171 uint32 x[PRE##_BLKSZ / 4]; \
172 octet b[PRE##_BLKSZ]; \
175 /* --- Let @sz@ be the size of the partial block --- */ \
179 /* --- First stage --- * \
181 * Read in the current block, and encrypt it. The first part of \
182 * the result is the partial ciphertext block. Don't write that \
183 * out yet, because I've not read the partial plaintext block. \
189 BLKC_LOAD(PRE, x, s); \
192 pre##_eblk(&ctx->ctx, x, x); \
193 BLKC_STORE(PRE, b, x); \
195 /* --- Second stage --- * \
197 * Now move in the partial plaintext block, writing out the \
198 * ciphertext as I go. Then encrypt, and write the complete \
199 * ciphertext block. \
202 if (d) d += PRE##_BLKSZ; \
203 for (i = 0; i < sz; i++) { \
204 register octet y = b[i]; \
208 BLKC_LOAD(PRE, x, b); \
209 pre##_eblk(&ctx->ctx, x, x); \
210 if (d) BLKC_STORE(PRE, d - PRE##_BLKSZ, x); \
218 /* --- @pre_ecbdecrypt@ --- * \
220 * Arguments: @pre_ecbctx *ctx@ = pointer to ECB context block \
221 * @const void *src@ = pointer to source data \
222 * @void *dest@ = pointer to destination data \
223 * @size_t sz@ = size of block to be encrypted \
227 * Use: Decrypts a block with a block cipher in ECB mode, with \
228 * ciphertext stealing and other clever tricks. \
229 * Essentially, data can be encrypted in arbitrary sized \
230 * chunks, although decryption must use the same chunks. \
233 void pre##_ecbdecrypt(pre##_ecbctx *ctx, \
234 const void *src, void *dest, \
237 const octet *s = src; \
240 /* --- Empty blocks are trivial --- */ \
245 /* --- Short blocks aren't allowed in ECB --- * \
247 * There's absolutely nothing secure I can do with them. \
250 assert(((void)"ECB must have at least one whole block to work with", \
251 sz >= PRE##_BLKSZ)); \
253 /* --- Do the main chunk of decryption --- * \
255 * This will do the whole lot if it's a whole number of blocks. \
256 * Each block is just handed to the block cipher in turn. \
259 while (sz >= 2 * PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
260 uint32 x[PRE##_BLKSZ / 4]; \
261 BLKC_LOAD(PRE, x, s); \
262 pre##_dblk(&ctx->ctx, x, x); \
263 BLKC_STORE(PRE, d, x); \
269 /* --- Do the tail-end block and bit-left-over --- * \
271 * This isn't very efficient. That shouldn't matter much. \
275 uint32 x[PRE##_BLKSZ / 4]; \
276 octet b[PRE##_BLKSZ]; \
279 /* --- Let @sz@ be the size of the partial block --- */ \
283 /* --- First stage --- * \
285 * Take the complete ciphertext block, and decrypt it. This block \
286 * is carried over for the next encryption operation. \
289 BLKC_LOAD(PRE, x, s); \
290 pre##_dblk(&ctx->ctx, x, x); \
291 BLKC_STORE(PRE, b, x); \
293 /* --- Second stage --- * \
295 * The first few bytes are the partial plaintext block. Write that \
296 * and replace with the partial ciphertext block. Then decrypt \
297 * what's left as the complete plaintext. \
302 for (i = 0; i < sz; i++) { \
303 register octet y = s[i]; \
307 BLKC_LOAD(PRE, x, b); \
308 pre##_dblk(&ctx->ctx, x, x); \
309 BLKC_STORE(PRE, d - PRE##_BLKSZ, x); \
317 /* --- Generic cipher interface --- */ \
319 static const gcipher_ops gops; \
321 typedef struct gctx { \
326 static gcipher *ginit(const void *k, size_t sz) \
328 gctx *g = S_CREATE(gctx); \
330 pre##_ecbinit(&g->k, k, sz, 0); \
334 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
336 gctx *g = (gctx *)c; \
337 pre##_ecbencrypt(&g->k, s, t, sz); \
340 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
342 gctx *g = (gctx *)c; \
343 pre##_ecbdecrypt(&g->k, s, t, sz); \
346 static void gdestroy(gcipher *c) \
348 gctx *g = (gctx *)c; \
353 static const gcipher_ops gops = { \
355 gencrypt, gdecrypt, gdestroy, 0, 0 \
358 const gccipher pre##_ecb = { \
359 name "-ecb", pre##_keysz, PRE##_BLKSZ, \
363 ECB_TESTX(PRE, pre, name, fname)
365 /*----- Test rig ----------------------------------------------------------*/
367 #define ECB_TEST(PRE, pre) ECB_TESTX(PRE, pre, #pre, #pre)
373 #include "daftstory.h"
375 /* --- @ECB_TEST@ --- *
377 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
379 * Use: Standard test rig for ECB functions.
382 #define ECB_TESTX(PRE, pre, name, fname) \
384 /* --- Initial plaintext for the test --- */ \
386 static const octet text[] = TEXT; \
388 /* --- Key and IV to use --- */ \
390 static const octet key[] = KEY; \
391 static const octet iv[] = IV; \
393 /* --- Buffers for encryption and decryption output --- */ \
395 static octet ct[sizeof(text)]; \
396 static octet pt[sizeof(text)]; \
398 static void hexdump(const octet *p, size_t sz, size_t off) \
400 const octet *q = p + sz; \
401 for (sz = 0; p < q; p++, sz++) { \
402 printf("%02x", *p); \
403 if ((off + sz + 1) % PRE##_BLKSZ == 0) \
410 size_t sz = 0, rest; \
415 size_t keysz = PRE##_KEYSZ ? \
416 PRE##_KEYSZ : strlen((const char *)key); \
418 fputs(name "-ecb: ", stdout); \
420 pre##_ecbinit(&ctx, key, keysz, iv); \
422 while (sz <= sizeof(text)) { \
423 rest = sizeof(text) - sz; \
424 if ((sz != 0 && sz < PRE##_BLKSZ) || \
425 (rest != 0 && rest < PRE##_BLKSZ)) \
427 memcpy(ct, text, sizeof(text)); \
428 pre##_ecbencrypt(&ctx, ct, ct, sz); \
429 pre##_ecbencrypt(&ctx, ct + sz, ct + sz, rest); \
430 memcpy(pt, ct, sizeof(text)); \
431 pre##_ecbdecrypt(&ctx, pt, pt, sz); \
432 pre##_ecbdecrypt(&ctx, pt + sz, pt + sz, rest); \
433 if (memcmp(pt, text, sizeof(text)) == 0) { \
435 if (sizeof(text) < 40 || done % 8 == 0) \
436 fputc('.', stdout); \
437 if (done % 480 == 0) \
438 fputs("\n\t", stdout); \
441 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
443 printf("\tplaintext = "); hexdump(text, sz, 0); \
444 printf(", "); hexdump(text + sz, rest, sz); \
445 fputc('\n', stdout); \
446 printf("\tciphertext = "); hexdump(ct, sz, 0); \
447 printf(", "); hexdump(ct + sz, rest, sz); \
448 fputc('\n', stdout); \
449 printf("\trecovered text = "); hexdump(pt, sz, 0); \
450 printf(", "); hexdump(pt + sz, rest, sz); \
451 fputc('\n', stdout); \
452 fputc('\n', stdout); \
461 fputs(status ? " failed\n" : " ok\n", stdout); \
466 # define ECB_TESTX(PRE, pre, name, fname)
469 /*----- That's all, folks -------------------------------------------------*/