3 * Definitions for cipher block chaining 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_CBC_DEF_H
29 #define CATACOMB_CBC_DEF_H
35 /*----- Header files ------------------------------------------------------*/
39 #include <mLib/bits.h>
42 #ifndef CATACOMB_ARENA_H
46 #ifndef CATACOMB_BLKC_H
50 #ifndef CATACOMB_GCIPHER_H
54 #ifndef CATACOMB_PARANOIA_H
55 # include "paranoia.h"
58 /*----- Macros ------------------------------------------------------------*/
60 /* --- @CBC_DEF@ --- *
62 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
64 * Use: Creates an implementation for CBC stealing mode.
67 #define CBC_DEF(PRE, pre) CBC_DEFX(PRE, pre, #pre, #pre)
69 #define CBC_DEFX(PRE, pre, name, fname) \
71 /* --- @pre_cbcgetiv@ --- * \
73 * Arguments: @const pre_cbcctx *ctx@ = pointer to CBC context block \
74 * @void *iv@ = pointer to output data block \
78 * Use: Reads the currently set IV. Reading and setting an IV \
79 * is transparent to the CBC encryption or decryption \
83 void pre##_cbcgetiv(const pre##_cbcctx *ctx, void *iv) \
84 { BLKC_STORE(PRE, iv, ctx->a); } \
86 /* --- @pre_cbcsetiv@ --- * \
88 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
89 * @cnost void *iv@ = pointer to IV to set \
93 * Use: Sets the IV to use for subsequent encryption. \
96 void pre##_cbcsetiv(pre##_cbcctx *ctx, const void *iv) \
97 { BLKC_LOAD(PRE, ctx->a, iv); } \
99 /* --- @pre_cbcsetkey@ --- * \
101 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
102 * @const pre_ctx *k@ = pointer to cipher context \
106 * Use: Sets the CBC context to use a different cipher key. \
109 void pre##_cbcsetkey(pre##_cbcctx *ctx, const pre##_ctx *k) \
112 /* --- @pre_cbcinit@ --- * \
114 * Arguments: @pre_cbcctx *ctx@ = pointer to cipher context \
115 * @const void *key@ = pointer to the key buffer \
116 * @size_t sz@ = size of the key \
117 * @const void *iv@ = pointer to initialization vector \
121 * Use: Initializes a CBC context ready for use. The @iv@ \
122 * argument may be passed as a null pointer to set a zero \
123 * IV. Apart from that, this call is equivalent to calls \
124 * to @pre_init@, @pre_cbcsetkey@ and @pre_cbcsetiv@. \
127 void pre##_cbcinit(pre##_cbcctx *ctx, \
128 const void *key, size_t sz, \
131 static const octet zero[PRE##_BLKSZ] = { 0 }; \
133 pre##_init(&ctx->ctx, key, sz); \
134 BLKC_LOAD(PRE, ctx->a, iv ? iv : zero); \
137 /* --- @pre_cbcencrypt@ --- * \
139 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
140 * @const void *src@ = pointer to source data \
141 * @void *dest@ = pointer to destination data \
142 * @size_t sz@ = size of block to be encrypted \
146 * Use: Encrypts a block with a block cipher in CBC mode, with \
147 * ciphertext stealing and other clever tricks. \
148 * Essentially, data can be encrypted in arbitrary sized \
149 * chunks, although decryption must use the same chunks. \
152 void pre##_cbcencrypt(pre##_cbcctx *ctx, \
153 const void *src, void *dest, \
156 const octet *s = src; \
158 octet b[PRE##_BLKSZ], bb[PRE##_BLKSZ]; \
162 /* --- Empty blocks are trivial --- */ \
166 /* --- Extra magical case for a short block --- * \
168 * Encrypt the IV, then exclusive-or the plaintext with the octets \
169 * of the encrypted IV, shifting ciphertext octets in instead. This \
170 * basically switches over to CFB. \
173 if (sz < PRE##_BLKSZ) { \
174 pre##_eblk(&ctx->ctx, ctx->a, ctx->a); \
175 BLKC_STORE(PRE, b, ctx->a); \
177 for (i = 0; i < sz; i++) d[i] = b[i] ^ (s ? s[i] : 0); \
178 memmove(b, b + sz, PRE##_BLKSZ - sz); \
179 memcpy(b + PRE##_BLKSZ - sz, d, sz); \
180 BLKC_LOAD(PRE, ctx->a, b); \
184 /* --- Do the main chunk of encryption --- * \
186 * This will do the whole lot if it's a whole number of blocks. For \
187 * each block, XOR it with the previous ciphertext in @iv@, encrypt, \
188 * and keep a copy of the ciphertext for the next block. \
191 while (sz >= 2*PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
192 if (s) { BLKC_XLOAD(PRE, ctx->a, s); s += PRE##_BLKSZ; } \
193 pre##_eblk(&ctx->ctx, ctx->a, ctx->a); \
194 if (d) { BLKC_STORE(PRE, d, ctx->a); d += PRE##_BLKSZ; } \
198 /* --- Do the tail-end block and bit-left-over --- * \
200 * This isn't very efficient. That shouldn't matter much. \
205 /* --- Let @sz@ be the size of the partial block --- */ \
209 /* --- First stage --- * \
211 * XOR the complete block with the current IV, and encrypt it. The \
212 * first part of the result is the partial ciphertext block. Don't \
213 * write that out yet, because I've not read the partial plaintext \
217 if (s) BLKC_XLOAD(PRE, ctx->a, s); \
218 pre##_eblk(&ctx->ctx, ctx->a, ctx->a); \
219 BLKC_STORE(PRE, b, ctx->a); \
221 /* --- Second stage --- * \
223 * Now XOR in the partial plaintext block, writing out the \
224 * ciphertext as I go. Then encrypt, and write the complete \
225 * ciphertext block. \
228 if (s) s += PRE##_BLKSZ; \
229 if (d) d += PRE##_BLKSZ; \
230 for (i = 0; i < sz; i++) { \
232 if (s) b[i] ^= s[i]; \
235 BLKC_LOAD(PRE, ctx->a, b); \
236 pre##_eblk(&ctx->ctx, ctx->a, ctx->a); \
237 if (d) BLKC_STORE(PRE, d - PRE##_BLKSZ, ctx->a); \
245 /* --- @pre_cbcdecrypt@ --- * \
247 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC 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 \
254 * Use: Decrypts a block with a block cipher in CBC mode, with \
255 * ciphertext stealing and other clever tricks. \
256 * Essentially, data can be encrypted in arbitrary sized \
257 * chunks, although decryption must use the same chunks. \
260 void pre##_cbcdecrypt(pre##_cbcctx *ctx, \
261 const void *src, void *dest, \
264 const octet *s = src; \
266 uint32 t[PRE##_BLKSZ/4], u[PRE##_BLKSZ/4]; \
267 octet b[PRE##_BLKSZ], c[PRE##_BLKSZ]; \
271 /* --- Empty blocks are trivial --- */ \
275 /* --- Extra magical case for a short block --- * \
277 * Encrypt the IV, then exclusive-or the ciphertext with the octets \
278 * of the encrypted IV, shifting ciphertext octets in instead. This \
279 * basically switches over to CFB. \
282 if (sz < PRE##_BLKSZ) { \
283 pre##_eblk(&ctx->ctx, ctx->a, ctx->a); \
284 BLKC_STORE(PRE, b, ctx->a); \
285 for (i = 0; i < sz; i++) { y = s[i]; d[i] = b[i] ^ y; c[i] = y; } \
286 memmove(b, b + sz, PRE##_BLKSZ - sz); \
287 memcpy(b + PRE##_BLKSZ - sz, c, sz); \
288 BLKC_LOAD(PRE, ctx->a, b); \
292 /* --- Do the main chunk of decryption --- * \
294 * This will do the whole lot if it's a whole number of blocks. For \
295 * each block, decrypt, XOR it with the previous ciphertext in @iv@, \
296 * and keep a copy of the ciphertext for the next block. \
299 while (sz >= 2*PRE##_BLKSZ || sz == PRE##_BLKSZ) { \
300 BLKC_LOAD(PRE, t, s); s += PRE##_BLKSZ; \
301 pre##_dblk(&ctx->ctx, t, u); \
302 BLKC_XSTORE(PRE, d, u, ctx->a); d += PRE##_BLKSZ; \
303 BLKC_MOVE(PRE, ctx->a, t); \
307 /* --- Do the tail-end block and bit-left-over --- * \
309 * This isn't very efficient. That shouldn't matter much. \
314 /* --- Let @sz@ be the size of the partial block --- */ \
318 /* --- First stage --- * \
320 * Take the complete ciphertext block, and decrypt it. This block \
321 * is carried over for the next encryption operation. \
324 BLKC_LOAD(PRE, t, s); \
325 pre##_dblk(&ctx->ctx, t, u); \
327 /* --- Second stage --- * \
329 * XORing the first few bytes of this with the partial ciphertext \
330 * block recovers the partial plaintext block. At the same time, \
331 * write the partial ciphertext block's contents in ready for stage \
335 BLKC_STORE(PRE, b, u); \
338 for (i = 0; i < sz; i++) { y = s[i]; d[i] = b[i] ^ y; b[i] = y; } \
340 /* --- Third stage --- * \
342 * Decrypt the block we've got left, and XOR with the initial IV to \
343 * recover the complete plaintext block. \
346 BLKC_LOAD(PRE, u, b); \
347 pre##_dblk(&ctx->ctx, u, u); \
348 BLKC_XSTORE(PRE, d - PRE##_BLKSZ, u, ctx->a); \
349 BLKC_MOVE(PRE, ctx->a, t); \
357 /* --- Generic cipher interface --- */ \
359 static const gcipher_ops gops; \
361 typedef struct gctx { \
366 static gcipher *ginit(const void *k, size_t sz) \
368 gctx *g = S_CREATE(gctx); \
370 pre##_cbcinit(&g->k, k, sz, 0); \
374 static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
375 { gctx *g = (gctx *)c; pre##_cbcencrypt(&g->k, s, t, sz); } \
377 static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
378 { gctx *g = (gctx *)c; pre##_cbcdecrypt(&g->k, s, t, sz); } \
380 static void gdestroy(gcipher *c) \
381 { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); } \
383 static void gsetiv(gcipher *c, const void *iv) \
384 { gctx *g = (gctx *)c; pre##_cbcsetiv(&g->k, iv); } \
386 static const gcipher_ops gops = { \
388 gencrypt, gdecrypt, gdestroy, gsetiv, 0 \
391 const gccipher pre##_cbc = { \
392 name "-cbc", pre##_keysz, PRE##_BLKSZ, \
396 CBC_TESTX(PRE, pre, name, fname)
398 /*----- Test rig ----------------------------------------------------------*/
400 #define CBC_TEST(PRE, pre) CBC_TESTX(PRE, pre, #pre, #pre)
404 #include "modes-test.h"
406 /* --- @CBC_TEST@ --- *
408 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
410 * Use: Standard test rig for CBC functions.
413 #define CBC_TESTX(PRE, pre, name, fname) \
415 static pre##_ctx key; \
416 static pre##_cbcctx ctx; \
418 static void pre##_cbc_test_setup(const octet *k, size_t ksz) \
419 { pre##_init(&key, k, ksz); pre##_cbcsetkey(&ctx, &key); } \
421 static void pre##_cbc_test_reset(const octet *iv) \
422 { pre##_cbcsetiv(&ctx, iv); } \
424 static void pre##_cbc_test_enc(const octet *s, octet *d, size_t sz) \
425 { pre##_cbcencrypt(&ctx, s, d, sz); } \
427 static void pre##_cbc_test_dec(const octet *s, octet *d, size_t sz) \
428 { pre##_cbcdecrypt(&ctx, s, d, sz); } \
430 int main(int argc, char *argv[]) \
432 return test_encmode(fname "-cbc", PRE##_KEYSZ, PRE##_BLKSZ, \
434 pre##_cbc_test_setup, pre##_cbc_test_reset, \
435 pre##_cbc_test_enc, pre##_cbc_test_dec, \
440 # define CBC_TESTX(PRE, pre, name, fname)
443 /*----- That's all, folks -------------------------------------------------*/