3 * $Id: ecb-def.h,v 1.4 2004/04/17 09:58:37 mdw Exp $
5 * Definitions electronic code book mode
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 #ifndef CATACOMB_ECB_DEF_H
31 #define CATACOMB_ECB_DEF_H
37 /*----- Header files ------------------------------------------------------*/
42 #include <mLib/bits.h>
45 #ifndef CATACOMB_ARENA_H
49 #ifndef CATACOMB_BLKC_H
53 #ifndef CATACOMB_GCIPHER_H
57 #ifndef CATACOMB_PARANOIA_H
58 # include "paranoia.h"
61 /*----- Macros ------------------------------------------------------------*/
63 /* --- @ECB_DEF@ --- *
65 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
67 * Use: Creates an implementation for ECB stealing mode.
70 #define ECB_DEF(PRE, pre) \
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 #pre "-ecb", pre##_keysz, PRE##_BLKSZ, \
365 /*----- Test rig ----------------------------------------------------------*/
371 #include "daftstory.h"
373 /* --- @ECB_TEST@ --- *
375 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
377 * Use: Standard test rig for ECB functions.
380 #define ECB_TEST(PRE, pre) \
382 /* --- Initial plaintext for the test --- */ \
384 static const octet text[] = TEXT; \
386 /* --- Key and IV to use --- */ \
388 static const octet key[] = KEY; \
389 static const octet iv[] = IV; \
391 /* --- Buffers for encryption and decryption output --- */ \
393 static octet ct[sizeof(text)]; \
394 static octet pt[sizeof(text)]; \
396 static void hexdump(const octet *p, size_t sz) \
398 const octet *q = p + sz; \
399 for (sz = 0; p < q; p++, sz++) { \
400 printf("%02x", *p); \
401 if ((sz + 1) % PRE##_BLKSZ == 0) \
408 size_t sz = 0, rest; \
413 size_t keysz = PRE##_KEYSZ ? \
414 PRE##_KEYSZ : strlen((const char *)key); \
416 fputs(#pre "-ecb: ", stdout); \
418 pre##_ecbinit(&ctx, key, keysz, iv); \
420 while (sz <= sizeof(text)) { \
421 rest = sizeof(text) - sz; \
422 if ((sz != 0 && sz < PRE##_BLKSZ) || \
423 (rest != 0 && rest < PRE##_BLKSZ)) \
425 memcpy(ct, text, sizeof(text)); \
426 pre##_ecbencrypt(&ctx, ct, ct, sz); \
427 pre##_ecbencrypt(&ctx, ct + sz, ct + sz, rest); \
428 memcpy(pt, ct, sizeof(text)); \
429 pre##_ecbdecrypt(&ctx, pt, pt, sz); \
430 pre##_ecbdecrypt(&ctx, pt + sz, pt + sz, rest); \
431 if (memcmp(pt, text, sizeof(text)) == 0) { \
433 if (sizeof(text) < 40 || done % 8 == 0) \
434 fputc('.', stdout); \
435 if (done % 480 == 0) \
436 fputs("\n\t", stdout); \
439 printf("\nError (sz = %lu)\n", (unsigned long)sz); \
441 printf("\tplaintext = "); hexdump(text, sz); \
442 printf(", "); hexdump(text + sz, rest); \
443 fputc('\n', stdout); \
444 printf("\tciphertext = "); hexdump(ct, sz); \
445 printf(", "); hexdump(ct + sz, rest); \
446 fputc('\n', stdout); \
447 printf("\trecovered text = "); hexdump(pt, sz); \
448 printf(", "); hexdump(pt + sz, rest); \
449 fputc('\n', stdout); \
450 fputc('\n', stdout); \
459 fputs(status ? " failed\n" : " ok\n", stdout); \
464 # define ECB_TEST(PRE, pre)
467 /*----- That's all, folks -------------------------------------------------*/