3 * Cipher block chaining mode.
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2001 Niels Möller
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37 #include "nettle-internal.h"
40 cbc_encrypt(void *ctx, nettle_crypt_func f,
41 unsigned block_size, uint8_t *iv,
42 unsigned length, uint8_t *dst,
45 assert(!(length % block_size));
47 for ( ; length; length -= block_size, src += block_size, dst += block_size)
49 memxor(iv, src, block_size);
50 f(ctx, block_size, dst, iv);
51 memcpy(iv, dst, block_size);
55 /* Requires that dst != src */
57 cbc_decrypt_internal(void *ctx, nettle_crypt_func f,
58 unsigned block_size, uint8_t *iv,
59 unsigned length, uint8_t *dst,
63 assert( !(length % block_size) );
66 /* Decrypt in ECB mode */
67 f(ctx, length, dst, src);
69 /* XOR the cryptotext, shifted one block */
70 memxor(dst, iv, block_size);
71 memxor(dst + block_size, src, length - block_size);
72 memcpy(iv, src + length - block_size, block_size);
75 /* Don't allocate any more space than this on the stack */
76 #define CBC_BUFFER_LIMIT 4096
79 cbc_decrypt(void *ctx, nettle_crypt_func f,
80 unsigned block_size, uint8_t *iv,
81 unsigned length, uint8_t *dst,
84 assert(!(length % block_size));
90 cbc_decrypt_internal(ctx, f, block_size, iv,
94 /* We need a copy of the ciphertext, so we can't ECB decrypt in
97 * If length is small, we allocate a complete copy of src on the
98 * stack. Otherwise, we allocate a block of size at most
99 * CBC_BUFFER_LIMIT, and process that amount of data at a
102 * NOTE: We assume that block_size <= CBC_BUFFER_LIMIT. */
104 unsigned buffer_size;
106 if (length <= CBC_BUFFER_LIMIT)
107 buffer_size = length;
110 = CBC_BUFFER_LIMIT - (CBC_BUFFER_LIMIT % block_size);
113 TMP_DECL(buffer, uint8_t, CBC_BUFFER_LIMIT);
114 TMP_ALLOC(buffer, buffer_size);
116 for ( ; length > buffer_size;
117 length -= buffer_size, dst += buffer_size, src += buffer_size)
119 memcpy(buffer, src, buffer_size);
120 cbc_decrypt_internal(ctx, f, block_size, iv,
121 buffer_size, dst, buffer);
123 /* Now, we have at most CBC_BUFFER_LIMIT octets left */
124 memcpy(buffer, src, length);
126 cbc_decrypt_internal(ctx, f, block_size, iv,
127 length, dst, buffer);
136 static void foo(void)
138 struct CBC_CTX(struct twofish_ctx, TWOFISH_BLOCK_SIZE) ctx;
139 uint8_t src[TWOFISH_BLOCK_SIZE];
140 uint8_t dst[TWOFISH_BLOCK_SIZE];
142 CBC_ENCRYPT(&ctx, twofish_encrypt, TWOFISH_BLOCK_SIZE, dst, src);
144 /* Should result in a warning */
145 CBC_ENCRYPT(&ctx, aes_encrypt, TWOFISH_BLOCK_SIZE, dst, src);
149 static void foo2(void)
151 struct twofish_ctx ctx;
152 uint8_t iv[TWOFISH_BLOCK_SIZE];
153 uint8_t src[TWOFISH_BLOCK_SIZE];
154 uint8_t dst[TWOFISH_BLOCK_SIZE];
156 CBC_ENCRYPT2(&ctx, twofish_encrypt, TWOFISH_BLOCK_SIZE, iv, TWOFISH_BLOCK_SIZE, dst, src);
157 /* Should result in a warning */
158 CBC_ENCRYPT2(&ctx, aes_encrypt, TWOFISH_BLOCK_SIZE, iv, TWOFISH_BLOCK_SIZE, dst, src);