progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / symm / cbc-def.h
CommitLineData
79ba130c 1/* -*-c-*-
2 *
79ba130c 3 * Definitions for cipher block chaining mode
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
79ba130c 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.
45c0fd36 16 *
79ba130c 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.
45c0fd36 21 *
79ba130c 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
79ba130c 28#ifndef CATACOMB_CBC_DEF_H
29#define CATACOMB_CBC_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
99461409 42#ifndef CATACOMB_ARENA_H
43# include "arena.h"
44#endif
45
79ba130c 46#ifndef CATACOMB_BLKC_H
47# include "blkc.h"
48#endif
49
50#ifndef CATACOMB_GCIPHER_H
51# include "gcipher.h"
52#endif
53
99461409 54#ifndef CATACOMB_PARANOIA_H
55# include "paranoia.h"
56#endif
57
79ba130c 58/*----- Macros ------------------------------------------------------------*/
59
60/* --- @CBC_DEF@ --- *
61 *
62 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
63 *
64 * Use: Creates an implementation for CBC stealing mode.
65 */
66
aaae9cab
MW
67#define CBC_DEF(PRE, pre) CBC_DEFX(PRE, pre, #pre, #pre)
68
69#define CBC_DEFX(PRE, pre, name, fname) \
79ba130c 70 \
71/* --- @pre_cbcgetiv@ --- * \
72 * \
73 * Arguments: @const pre_cbcctx *ctx@ = pointer to CBC context block \
4efe32ba 74 * @void *iv@ = pointer to output data block \
79ba130c 75 * \
76 * Returns: --- \
77 * \
78 * Use: Reads the currently set IV. Reading and setting an IV \
79 * is transparent to the CBC encryption or decryption \
80 * process. \
81 */ \
82 \
83void pre##_cbcgetiv(const pre##_cbcctx *ctx, void *iv) \
0fee61eb 84 { BLKC_STORE(PRE, iv, ctx->a); } \
79ba130c 85 \
86/* --- @pre_cbcsetiv@ --- * \
87 * \
88 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
89 * @cnost void *iv@ = pointer to IV to set \
90 * \
91 * Returns: --- \
92 * \
93 * Use: Sets the IV to use for subsequent encryption. \
94 */ \
95 \
96void pre##_cbcsetiv(pre##_cbcctx *ctx, const void *iv) \
0fee61eb 97 { BLKC_LOAD(PRE, ctx->a, iv); } \
79ba130c 98 \
99/* --- @pre_cbcsetkey@ --- * \
100 * \
101 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
102 * @const pre_ctx *k@ = pointer to cipher context \
103 * \
104 * Returns: --- \
105 * \
106 * Use: Sets the CBC context to use a different cipher key. \
107 */ \
108 \
109void pre##_cbcsetkey(pre##_cbcctx *ctx, const pre##_ctx *k) \
0fee61eb 110 { ctx->ctx = *k; } \
79ba130c 111 \
112/* --- @pre_cbcinit@ --- * \
113 * \
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 \
118 * \
119 * Returns: --- \
120 * \
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@. \
125 */ \
126 \
127void pre##_cbcinit(pre##_cbcctx *ctx, \
81b52866
MW
128 const void *key, size_t sz, \
129 const void *iv) \
79ba130c 130{ \
4e66da02 131 static const octet zero[PRE##_BLKSZ] = { 0 }; \
0fee61eb 132 \
79ba130c 133 pre##_init(&ctx->ctx, key, sz); \
0fee61eb 134 BLKC_LOAD(PRE, ctx->a, iv ? iv : zero); \
79ba130c 135} \
136 \
137/* --- @pre_cbcencrypt@ --- * \
138 * \
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 \
143 * \
144 * Returns: --- \
145 * \
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. \
150 */ \
151 \
152void pre##_cbcencrypt(pre##_cbcctx *ctx, \
81b52866
MW
153 const void *src, void *dest, \
154 size_t sz) \
79ba130c 155{ \
156 const octet *s = src; \
157 octet *d = dest; \
460bbf88 158 octet b[PRE##_BLKSZ], bb[PRE##_BLKSZ]; \
0fee61eb
MW
159 octet y; \
160 unsigned i; \
79ba130c 161 \
162 /* --- Empty blocks are trivial --- */ \
163 \
0fee61eb 164 if (!sz) return; \
79ba130c 165 \
166 /* --- Extra magical case for a short block --- * \
167 * \
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. \
171 */ \
172 \
173 if (sz < PRE##_BLKSZ) { \
0fee61eb
MW
174 pre##_eblk(&ctx->ctx, ctx->a, ctx->a); \
175 BLKC_STORE(PRE, b, ctx->a); \
460bbf88
MW
176 if (!d) d = bb; \
177 for (i = 0; i < sz; i++) d[i] = b[i] ^ (s ? s[i] : 0); \
79ba130c 178 memmove(b, b + sz, PRE##_BLKSZ - sz); \
179 memcpy(b + PRE##_BLKSZ - sz, d, sz); \
0fee61eb 180 BLKC_LOAD(PRE, ctx->a, b); \
79ba130c 181 return; \
182 } \
183 \
184 /* --- Do the main chunk of encryption --- * \
185 * \
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. \
189 */ \
190 \
0fee61eb
MW
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; } \
79ba130c 195 sz -= PRE##_BLKSZ; \
196 } \
197 \
198 /* --- Do the tail-end block and bit-left-over --- * \
199 * \
200 * This isn't very efficient. That shouldn't matter much. \
201 */ \
202 \
203 if (sz) { \
79ba130c 204 \
205 /* --- Let @sz@ be the size of the partial block --- */ \
206 \
207 sz -= PRE##_BLKSZ; \
208 \
209 /* --- First stage --- * \
210 * \
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 \
214 * block. \
215 */ \
216 \
0fee61eb
MW
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); \
79ba130c 220 \
221 /* --- Second stage --- * \
222 * \
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. \
226 */ \
227 \
5c3f75ec 228 if (s) s += PRE##_BLKSZ; \
229 if (d) d += PRE##_BLKSZ; \
79ba130c 230 for (i = 0; i < sz; i++) { \
0fee61eb 231 y = b[i]; \
5c3f75ec 232 if (s) b[i] ^= s[i]; \
0fee61eb 233 if (d) d[i] = y; \
79ba130c 234 } \
0fee61eb
MW
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); \
79ba130c 238 } \
239 \
240 /* --- Done --- */ \
241 \
242 return; \
243} \
244 \
245/* --- @pre_cbcdecrypt@ --- * \
246 * \
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 \
251 * \
252 * Returns: --- \
253 * \
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. \
258 */ \
259 \
260void pre##_cbcdecrypt(pre##_cbcctx *ctx, \
81b52866
MW
261 const void *src, void *dest, \
262 size_t sz) \
79ba130c 263{ \
264 const octet *s = src; \
265 octet *d = dest; \
0fee61eb
MW
266 uint32 t[PRE##_BLKSZ/4], u[PRE##_BLKSZ/4]; \
267 octet b[PRE##_BLKSZ], c[PRE##_BLKSZ]; \
268 octet y; \
269 unsigned i; \
79ba130c 270 \
271 /* --- Empty blocks are trivial --- */ \
272 \
0fee61eb 273 if (!sz) return; \
79ba130c 274 \
275 /* --- Extra magical case for a short block --- * \
276 * \
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. \
280 */ \
281 \
282 if (sz < PRE##_BLKSZ) { \
0fee61eb
MW
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; } \
79ba130c 286 memmove(b, b + sz, PRE##_BLKSZ - sz); \
287 memcpy(b + PRE##_BLKSZ - sz, c, sz); \
0fee61eb 288 BLKC_LOAD(PRE, ctx->a, b); \
79ba130c 289 return; \
290 } \
291 \
292 /* --- Do the main chunk of decryption --- * \
293 * \
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. \
297 */ \
298 \
0fee61eb
MW
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); \
79ba130c 304 sz -= PRE##_BLKSZ; \
305 } \
306 \
307 /* --- Do the tail-end block and bit-left-over --- * \
308 * \
309 * This isn't very efficient. That shouldn't matter much. \
310 */ \
311 \
312 if (sz) { \
79ba130c 313 \
314 /* --- Let @sz@ be the size of the partial block --- */ \
315 \
316 sz -= PRE##_BLKSZ; \
317 \
318 /* --- First stage --- * \
319 * \
320 * Take the complete ciphertext block, and decrypt it. This block \
321 * is carried over for the next encryption operation. \
322 */ \
323 \
0fee61eb
MW
324 BLKC_LOAD(PRE, t, s); \
325 pre##_dblk(&ctx->ctx, t, u); \
79ba130c 326 \
327 /* --- Second stage --- * \
328 * \
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 \
332 * three. \
333 */ \
334 \
0fee61eb 335 BLKC_STORE(PRE, b, u); \
79ba130c 336 s += PRE##_BLKSZ; \
337 d += PRE##_BLKSZ; \
0fee61eb 338 for (i = 0; i < sz; i++) { y = s[i]; d[i] = b[i] ^ y; b[i] = y; } \
79ba130c 339 \
340 /* --- Third stage --- * \
341 * \
342 * Decrypt the block we've got left, and XOR with the initial IV to \
343 * recover the complete plaintext block. \
344 */ \
345 \
0fee61eb
MW
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); \
79ba130c 350 } \
351 \
352 /* --- Done --- */ \
353 \
354 return; \
355} \
356 \
357/* --- Generic cipher interface --- */ \
358 \
359static const gcipher_ops gops; \
360 \
361typedef struct gctx { \
362 gcipher c; \
363 pre##_cbcctx k; \
364} gctx; \
365 \
366static gcipher *ginit(const void *k, size_t sz) \
367{ \
99461409 368 gctx *g = S_CREATE(gctx); \
79ba130c 369 g->c.ops = &gops; \
370 pre##_cbcinit(&g->k, k, sz, 0); \
371 return (&g->c); \
372} \
373 \
374static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) \
0fee61eb 375 { gctx *g = (gctx *)c; pre##_cbcencrypt(&g->k, s, t, sz); } \
79ba130c 376 \
377static void gdecrypt(gcipher *c, const void *s, void *t, size_t sz) \
0fee61eb 378 { gctx *g = (gctx *)c; pre##_cbcdecrypt(&g->k, s, t, sz); } \
79ba130c 379 \
380static void gdestroy(gcipher *c) \
0fee61eb 381 { gctx *g = (gctx *)c; BURN(*g); S_DESTROY(g); } \
79ba130c 382 \
383static void gsetiv(gcipher *c, const void *iv) \
0fee61eb 384 { gctx *g = (gctx *)c; pre##_cbcsetiv(&g->k, iv); } \
79ba130c 385 \
386static const gcipher_ops gops = { \
99461409 387 &pre##_cbc, \
388 gencrypt, gdecrypt, gdestroy, gsetiv, 0 \
79ba130c 389}; \
390 \
391const gccipher pre##_cbc = { \
aaae9cab 392 name "-cbc", pre##_keysz, PRE##_BLKSZ, \
79ba130c 393 ginit \
394}; \
395 \
aaae9cab 396CBC_TESTX(PRE, pre, name, fname)
79ba130c 397
398/*----- Test rig ----------------------------------------------------------*/
399
aaae9cab
MW
400#define CBC_TEST(PRE, pre) CBC_TESTX(PRE, pre, #pre, #pre)
401
79ba130c 402#ifdef TEST_RIG
403
57f459eb 404#include "modes-test.h"
79ba130c 405
406/* --- @CBC_TEST@ --- *
407 *
408 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
409 *
410 * Use: Standard test rig for CBC functions.
411 */
412
aaae9cab 413#define CBC_TESTX(PRE, pre, name, fname) \
79ba130c 414 \
57f459eb
MW
415static pre##_ctx key; \
416static pre##_cbcctx ctx; \
79ba130c 417 \
57f459eb
MW
418static void pre##_cbc_test_setup(const octet *k, size_t ksz) \
419 { pre##_init(&key, k, ksz); pre##_cbcsetkey(&ctx, &key); } \
79ba130c 420 \
57f459eb
MW
421static void pre##_cbc_test_reset(const octet *iv) \
422 { pre##_cbcsetiv(&ctx, iv); } \
79ba130c 423 \
57f459eb
MW
424static void pre##_cbc_test_enc(const octet *s, octet *d, size_t sz) \
425 { pre##_cbcencrypt(&ctx, s, d, sz); } \
79ba130c 426 \
57f459eb
MW
427static void pre##_cbc_test_dec(const octet *s, octet *d, size_t sz) \
428 { pre##_cbcdecrypt(&ctx, s, d, sz); } \
79ba130c 429 \
57f459eb 430int main(int argc, char *argv[]) \
79ba130c 431{ \
57f459eb
MW
432 return test_encmode(fname "-cbc", PRE##_KEYSZ, PRE##_BLKSZ, \
433 1, TEMF_REFALIGN, \
434 pre##_cbc_test_setup, pre##_cbc_test_reset, \
435 pre##_cbc_test_enc, pre##_cbc_test_dec, \
436 argc, argv); \
79ba130c 437}
438
439#else
aaae9cab 440# define CBC_TESTX(PRE, pre, name, fname)
79ba130c 441#endif
442
443/*----- That's all, folks -------------------------------------------------*/
444
445#ifdef __cplusplus
446 }
447#endif
448
449#endif