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., 51 Franklin Street, Fifth Floor, 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 /* Don't allocate any more space than this on the stack */
56 #define CBC_BUFFER_LIMIT 512
59 cbc_decrypt(void *ctx
, nettle_crypt_func
*f
,
60 unsigned block_size
, uint8_t *iv
,
61 unsigned length
, uint8_t *dst
,
64 assert(!(length
% block_size
));
71 /* Decrypt in ECB mode */
72 f(ctx
, length
, dst
, src
);
74 /* XOR the cryptotext, shifted one block */
75 memxor(dst
, iv
, block_size
);
76 memxor(dst
+ block_size
, src
, length
- block_size
);
77 memcpy(iv
, src
+ length
- block_size
, block_size
);
82 /* For in-place CBC, we decrypt into a temporary buffer of size
83 * at most CBC_BUFFER_LIMIT, and process that amount of data at
86 /* NOTE: We assume that block_size <= CBC_BUFFER_LIMIT, and we
87 depend on memxor3 working from the end of the area, allowing
88 certain overlapping operands. */
90 TMP_DECL(buffer
, uint8_t, CBC_BUFFER_LIMIT
);
91 TMP_DECL(initial_iv
, uint8_t, NETTLE_MAX_CIPHER_BLOCK_SIZE
);
95 if (length
<= CBC_BUFFER_LIMIT
)
99 = CBC_BUFFER_LIMIT
- (CBC_BUFFER_LIMIT
% block_size
);
101 TMP_ALLOC(buffer
, buffer_size
);
102 TMP_ALLOC(initial_iv
, block_size
);
104 for ( ; length
> buffer_size
;
105 length
-= buffer_size
, src
+= buffer_size
, dst
+= buffer_size
)
107 f(ctx
, buffer_size
, buffer
, src
);
108 memcpy(initial_iv
, iv
, block_size
);
109 memcpy(iv
, src
+ buffer_size
- block_size
, block_size
);
110 memxor3(dst
+ block_size
, buffer
+ block_size
, src
,
111 buffer_size
- block_size
);
112 memxor3(dst
, buffer
, initial_iv
, block_size
);
115 f(ctx
, length
, buffer
, src
);
116 memcpy(initial_iv
, iv
, block_size
);
117 /* Copies last block */
118 memcpy(iv
, src
+ length
- block_size
, block_size
);
119 /* Writes all but first block, reads all but last block. */
120 memxor3(dst
+ block_size
, buffer
+ block_size
, src
,
121 length
- block_size
);
122 /* Writes first block. */
123 memxor3(dst
, buffer
, initial_iv
, block_size
);
131 static void foo(void)
133 struct CBC_CTX(struct twofish_ctx
, TWOFISH_BLOCK_SIZE
) ctx
;
134 uint8_t src
[TWOFISH_BLOCK_SIZE
];
135 uint8_t dst
[TWOFISH_BLOCK_SIZE
];
137 CBC_ENCRYPT(&ctx
, twofish_encrypt
, TWOFISH_BLOCK_SIZE
, dst
, src
);
139 /* Should result in a warning */
140 CBC_ENCRYPT(&ctx
, aes_encrypt
, TWOFISH_BLOCK_SIZE
, dst
, src
);
144 static void foo2(void)
146 struct twofish_ctx ctx
;
147 uint8_t iv
[TWOFISH_BLOCK_SIZE
];
148 uint8_t src
[TWOFISH_BLOCK_SIZE
];
149 uint8_t dst
[TWOFISH_BLOCK_SIZE
];
151 CBC_ENCRYPT2(&ctx
, twofish_encrypt
, TWOFISH_BLOCK_SIZE
, iv
, TWOFISH_BLOCK_SIZE
, dst
, src
);
152 /* Should result in a warning */
153 CBC_ENCRYPT2(&ctx
, aes_encrypt
, TWOFISH_BLOCK_SIZE
, iv
, TWOFISH_BLOCK_SIZE
, dst
, src
);