Commit f4b9cce1 authored by Niels Möller's avatar Niels Möller

(CBC_BUFFER_LIMIT): Reduced to 512 bytes.

(cbc_decrypt): For in-place operation, use overlapping memxor3 and
eliminate a memcpy.

Rev: nettle/cbc.c:1.3
parent 697f062f
...@@ -52,28 +52,8 @@ cbc_encrypt(void *ctx, nettle_crypt_func f, ...@@ -52,28 +52,8 @@ cbc_encrypt(void *ctx, nettle_crypt_func f,
} }
} }
/* Requires that dst != src */
static void
cbc_decrypt_internal(void *ctx, nettle_crypt_func f,
unsigned block_size, uint8_t *iv,
unsigned length, uint8_t *dst,
const uint8_t *src)
{
assert(length);
assert( !(length % block_size) );
assert(src != dst);
/* Decrypt in ECB mode */
f(ctx, length, dst, src);
/* XOR the cryptotext, shifted one block */
memxor(dst, iv, block_size);
memxor(dst + block_size, src, length - block_size);
memcpy(iv, src + length - block_size, block_size);
}
/* Don't allocate any more space than this on the stack */ /* Don't allocate any more space than this on the stack */
#define CBC_BUFFER_LIMIT 4096 #define CBC_BUFFER_LIMIT 512
void void
cbc_decrypt(void *ctx, nettle_crypt_func f, cbc_decrypt(void *ctx, nettle_crypt_func f,
...@@ -87,19 +67,28 @@ cbc_decrypt(void *ctx, nettle_crypt_func f, ...@@ -87,19 +67,28 @@ cbc_decrypt(void *ctx, nettle_crypt_func f,
return; return;
if (src != dst) if (src != dst)
cbc_decrypt_internal(ctx, f, block_size, iv, {
length, dst, src); /* Decrypt in ECB mode */
f(ctx, length, dst, src);
/* XOR the cryptotext, shifted one block */
memxor(dst, iv, block_size);
memxor(dst + block_size, src, length - block_size);
memcpy(iv, src + length - block_size, block_size);
}
else else
{ {
/* We need a copy of the ciphertext, so we can't ECB decrypt in /* For in-place CBC, we decrypt into a temporary buffer of size
* place. * at most CBC_BUFFER_LIMIT, and process that amount of data at
* * a time. */
* If length is small, we allocate a complete copy of src on the
* stack. Otherwise, we allocate a block of size at most /* NOTE: We assume that block_size <= CBC_BUFFER_LIMIT, and we
* CBC_BUFFER_LIMIT, and process that amount of data at a depend on memxor3 working from the end of the area, allowing
* time. certain overlapping operands. */
*
* NOTE: We assume that block_size <= CBC_BUFFER_LIMIT. */ TMP_DECL(buffer, uint8_t, CBC_BUFFER_LIMIT);
TMP_DECL(initial_iv, uint8_t, NETTLE_MAX_CIPHER_BLOCK_SIZE);
unsigned buffer_size; unsigned buffer_size;
...@@ -109,23 +98,29 @@ cbc_decrypt(void *ctx, nettle_crypt_func f, ...@@ -109,23 +98,29 @@ cbc_decrypt(void *ctx, nettle_crypt_func f,
buffer_size buffer_size
= CBC_BUFFER_LIMIT - (CBC_BUFFER_LIMIT % block_size); = CBC_BUFFER_LIMIT - (CBC_BUFFER_LIMIT % block_size);
{ TMP_ALLOC(buffer, buffer_size);
TMP_DECL(buffer, uint8_t, CBC_BUFFER_LIMIT); TMP_ALLOC(initial_iv, block_size);
TMP_ALLOC(buffer, buffer_size);
for ( ; length > buffer_size;
for ( ; length > buffer_size; length -= buffer_size, src += buffer_size, dst += buffer_size)
length -= buffer_size, dst += buffer_size, src += buffer_size) {
{ f(ctx, buffer_size, buffer, src);
memcpy(buffer, src, buffer_size); memcpy(initial_iv, iv, block_size);
cbc_decrypt_internal(ctx, f, block_size, iv, memcpy(iv, src + buffer_size - block_size, block_size);
buffer_size, dst, buffer); memxor3(dst + block_size, buffer + block_size, src,
} buffer_size - block_size);
/* Now, we have at most CBC_BUFFER_LIMIT octets left */ memxor3(dst, buffer, initial_iv, block_size);
memcpy(buffer, src, length); }
cbc_decrypt_internal(ctx, f, block_size, iv, f(ctx, length, buffer, src);
length, dst, buffer); memcpy(initial_iv, iv, block_size);
} /* Copies last block */
memcpy(iv, src + length - block_size, block_size);
/* Writes all but first block, reads all but last block. */
memxor3(dst + block_size, buffer + block_size, src,
length - block_size);
/* Writes first block. */
memxor3(dst, buffer, initial_iv, block_size);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment