From 97829771e9fa1317b2166f9d7a626212b355e5e0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Fri, 7 Sep 2001 19:38:29 +0200
Subject: [PATCH] Work-in-progress.

Rev: src/nettle/Makefile.am:1.12
Rev: src/nettle/cbc.c:1.2
Rev: src/nettle/cbc.h:1.2
---
 Makefile.am |  1 +
 cbc.c       | 19 +++++++++++++++++--
 cbc.h       |  9 ++++++++-
 3 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 984903fd..4587ce33 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -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 \
 		      cast128.c cast128.h cast128_sboxes.h \
 		      blowfish.h blowfish.c \
+		      cbc.c \
 		      des.c des.h desinfo.h desCode.h \
 		      md5.c md5.h md5-compat.c md5-compat.h \
 		      sha1.c sha1.h \
diff --git a/cbc.c b/cbc.c
index 768ba1db..d12ed79a 100644
--- a/cbc.c
+++ b/cbc.c
@@ -25,7 +25,11 @@
 
 #include "cbc.h"
 
+#include "memxor.h"
+
 #include <assert.h>
+#include <stdlib.h>
+#include <string.h>
 
 void
 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)
     {
       memxor(iv, src, block_size);
-      f(ctx, dst, src, block_size);
+      f(ctx, block_size, dst, src);
       memcpy(iv, dst, block_size);
     }
 }
@@ -69,10 +73,21 @@ cbc_decrypt(void *ctx, void (*f)(void *ctx,
     }
 
   /* Decrypt in ECB mode */
-  f(ctx, dst, src, length);
+  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);
 }
+
+#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);
+}
diff --git a/cbc.h b/cbc.h
index 4335a518..2bf72e7c 100644
--- a/cbc.h
+++ b/cbc.h
@@ -28,7 +28,7 @@
 
 #include <inttypes.h>
 
-/* Uses a void * for cipher contexts. It's hard to be type safe. */
+/* Uses a void * for cipher contexts. */
 
 void
 cbc_encrypt(void *ctx, void (*f)(void *ctx,
@@ -46,4 +46,11 @@ cbc_decrypt(void *ctx, void (*f)(void *ctx,
 	    unsigned length, uint8_t *dst,
 	    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 */
-- 
GitLab