Skip to content
Snippets Groups Projects
Commit 97829771 authored by Niels Möller's avatar Niels Möller
Browse files

Work-in-progress.

Rev: src/nettle/Makefile.am:1.12
Rev: src/nettle/cbc.c:1.2
Rev: src/nettle/cbc.h:1.2
parent 4fc40778
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,7 @@ libnettleinclude_HEADERS = aes.h arcfour.h blowfish.h cast128.h des.h \ ...@@ -12,6 +12,7 @@ libnettleinclude_HEADERS = aes.h arcfour.h blowfish.h cast128.h des.h \
libnettle_a_SOURCES = aes.c aes.h arcfour.c arcfour.h \ libnettle_a_SOURCES = aes.c aes.h arcfour.c arcfour.h \
cast128.c cast128.h cast128_sboxes.h \ cast128.c cast128.h cast128_sboxes.h \
blowfish.h blowfish.c \ blowfish.h blowfish.c \
cbc.c \
des.c des.h desinfo.h desCode.h \ des.c des.h desinfo.h desCode.h \
md5.c md5.h md5-compat.c md5-compat.h \ md5.c md5.h md5-compat.c md5-compat.h \
sha1.c sha1.h \ sha1.c sha1.h \
......
...@@ -25,7 +25,11 @@ ...@@ -25,7 +25,11 @@
#include "cbc.h" #include "cbc.h"
#include "memxor.h"
#include <assert.h> #include <assert.h>
#include <stdlib.h>
#include <string.h>
void void
cbc_encrypt(void *ctx, void (*f)(void *ctx, cbc_encrypt(void *ctx, void (*f)(void *ctx,
...@@ -40,7 +44,7 @@ cbc_encrypt(void *ctx, void (*f)(void *ctx, ...@@ -40,7 +44,7 @@ cbc_encrypt(void *ctx, void (*f)(void *ctx,
for ( ; length; length -= block_size, src += block_size, dst += block_size) for ( ; length; length -= block_size, src += block_size, dst += block_size)
{ {
memxor(iv, src, block_size); memxor(iv, src, block_size);
f(ctx, dst, src, block_size); f(ctx, block_size, dst, src);
memcpy(iv, dst, block_size); memcpy(iv, dst, block_size);
} }
} }
...@@ -69,10 +73,21 @@ cbc_decrypt(void *ctx, void (*f)(void *ctx, ...@@ -69,10 +73,21 @@ cbc_decrypt(void *ctx, void (*f)(void *ctx,
} }
/* Decrypt in ECB mode */ /* Decrypt in ECB mode */
f(ctx, dst, src, length); f(ctx, length, dst, src);
/* XOR the cryptotext, shifted one block */ /* XOR the cryptotext, shifted one block */
memxor(dst, iv, block_size); memxor(dst, iv, block_size);
memxor(dst + block_size, src, length - block_size); memxor(dst + block_size, src, length - block_size);
memcpy(iv, src + length - block_size, block_size); memcpy(iv, src + length - block_size, block_size);
} }
#include "des.h"
static void foo(void)
{
struct des_ctx ctx;
uint8_t iv[DES_BLOCK_SIZE];
uint8_t src[DES_BLOCK_SIZE];
uint8_t dst[DES_BLOCK_SIZE];
CBC_ENCRYPT(&ctx, des_encrypt, DES_BLOCK_SIZE, iv, DES_BLOCK_SIZE, dst, src);
}
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
#include <inttypes.h> #include <inttypes.h>
/* Uses a void * for cipher contexts. It's hard to be type safe. */ /* Uses a void * for cipher contexts. */
void void
cbc_encrypt(void *ctx, void (*f)(void *ctx, cbc_encrypt(void *ctx, void (*f)(void *ctx,
...@@ -46,4 +46,11 @@ cbc_decrypt(void *ctx, void (*f)(void *ctx, ...@@ -46,4 +46,11 @@ cbc_decrypt(void *ctx, void (*f)(void *ctx,
unsigned length, uint8_t *dst, unsigned length, uint8_t *dst,
const uint8_t *src); const uint8_t *src);
/* Type safer variants */
#define CBC_ENCRYPT(ctx, f, b, iv, l, dst, src) \
(0 ? ((f)((ctx),0,NULL,NULL)) \
: cbc_encrypt((void *)(ctx), \
((*)(void *, unsigned, uint8_t *, const uint8_t *)) (f), \
(b), (iv), (l), (dst), (src)))
#endif /* NETTLE_CBC_H_INCLUDED */ #endif /* NETTLE_CBC_H_INCLUDED */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment