chiark / gitweb /
Compiles but does not link. Before undo silly pad indirection.
authorian <ian>
Sun, 1 Sep 2002 22:02:11 +0000 (22:02 +0000)
committerian <ian>
Sun, 1 Sep 2002 22:02:11 +0000 (22:02 +0000)
base/chiark-tcl.h
base/tables-examples.tct
base/troglodyte-Makefile
crypto/bcmode.c [new file with mode: 0644]
crypto/crypto.c [new file with mode: 0644]
hbytes/hbytes.c
hbytes/hbytes.h

index ee69928237e7202581091154da3bbe9e41d3cfb4..75ce6c816bf0f86f95fb3ded4d91c2f88cfb7702 100644 (file)
@@ -128,18 +128,50 @@ int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
 
 /* from crypto.c */
 
+void memxor(Byte *dest, const Byte *src, int l);
+
+/* from hash.c */
+
 typedef struct {
   int blocksize, hashsize;
 } HashAlgInfo;
 
+/* from blockciph.c */
+
+typedef struct {
+  void (*make_schedule)(void *schedule, const Byte *key, int keylen);
+  void (*crypt)(const void *schedule, const void *in, void *out);
+     /* in and out may be the same, but if they aren't they may not overlap */
+     /* in and out for crypt will have been through block_byteswap */
+} BlockCipherDirectionInfo;
+
 typedef struct {
-  int blocksize;
+  const char *name;
+  int blocksize, schedule_size, key_min, key_max;
+  void (*byteswap)(Byte *block);
+  BlockCipherDirectionInfo encrypt, decrypt;
 } BlockCipherAlgInfo;
 
+extern const BlockCipherAlgInfo blockcipheralginfos[];
+
+/* from bcmode.c */
+
 typedef struct {
-  int dummy;
+  const char *name;
+  int iv_blocks, buf_blocks;
+  const char *(*encrypt)(Byte *data, int blocks,
+                        const Byte *iv, Byte *buf,
+                        const BlockCipherAlgInfo *alg, int encr,
+                        int blocksize, const void *sch);
+  const char *(*decrypt)(Byte *data, int blocks,
+                        const Byte *iv, Byte *buf,
+                        const BlockCipherAlgInfo *alg, int encr,
+                        int blocksize, const void *sch);
+    /* in each case, *iv is provided, but may be modified */
 } BlockCipherModeInfo;
 
+extern const BlockCipherModeInfo blockciphermodeinfos[];
+
 /* useful macros */
 
 #define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)
index ecb5d7cf3ed30413f7b0a6e8048d6e59802c6948..13a3e5d68a05ab9629c4b254c538eb3e3fab6e51 100644 (file)
@@ -42,9 +42,9 @@ Table hbytes HBytes_SubCommand
                v       hbv
                length  int
                =>      hb
-#      pkcs5
-#              meth    enum(PadMethod, "hbytes pad subcommand")
-#              obj     ...
+       pkcs5
+               meth    enum(PadMethod, "hbytes pad subcommand")
+               obj     ...
 #      blockcipher
 #              encrypt charfrom("de","encrypt/decrypt")
 #              v       hbv
@@ -63,8 +63,19 @@ Table hbytes HBytes_SubCommand
 #              maclen  int
 #              =>      hb
 
-#Table padmethod PadMethod
-#      pa      1, 0
+Table padmethod PadMethod
+       pa      1
+               v       hbv
+               alg     enum(BlockCipherAlgInfo, "pad alg")
+       ua      0
+               v       hbv
+               alg     enum(BlockCipherAlgInfo, "pad alg")
+       pn      1
+               v       hbv
+               block   int
+       un      0
+               v       hbv
+               block   int
 
-#EntryExtra PadMethod
-#      int pad, algname;
+EntryExtra PadMethod
+       int pad;
index 784273a2b86d7fe3431eacd7fc4a1e16e9d1bce1..8e94090f46b336968351bccb558d9fccf6ae263e 100644 (file)
@@ -3,6 +3,10 @@ OBJS=          tables.o \
                enum.o \
                chop.o \
                hook.o \
+               bcmode.o \
+               blockciph.o \
+               serpent.o \
+               crypto.o \
                parse.o
 
 HDRS=          hbytes.h \
diff --git a/crypto/bcmode.c b/crypto/bcmode.c
new file mode 100644 (file)
index 0000000..7d5a5d4
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ */
+  
+#include "hbytes.h"
+
+const char *mode_cbc_encrypt(Byte *data, int blocks,
+                            const Byte *iv, Byte *chain,
+                            const BlockCipherAlgInfo *alg, int encr,
+                            int blocksize, const void *sch) {
+  memcpy(chain,iv,blocksize);
+  alg->byteswap(chain);
+  
+  while (blocks > 0) {
+    alg->byteswap(data);
+
+    memxor(data, chain, blocksize);
+    alg->encrypt.crypt(sch, data, data);
+    memcpy(chain, data, blocksize);
+
+    alg->byteswap(data);
+    blocks--; data += blocksize;
+  }
+  return 0;
+}
+
+const char *mode_cbc_decrypt(Byte *data, int blocks,
+                            const Byte *iv, Byte *chain,
+                            const BlockCipherAlgInfo *alg, int encr,
+                            int blocksize, const void *sch) {
+  int cchain= 0;
+
+  memcpy(chain,iv,blocksize);
+  alg->byteswap(chain);
+  
+  while (blocks > 0) {
+    alg->byteswap(data);
+    
+    memcpy(chain + (cchain^blocksize), data, blocksize);
+    alg->decrypt.crypt(sch, data, data);
+    memxor(data, chain + cchain, blocksize);
+    cchain ^= blocksize;
+
+    alg->byteswap(data);
+    blocks--; data += blocksize;
+  }
+  return 0;
+}
+
+const BlockCipherModeInfo blockciphermodeinfos[]= {
+  { "cbc", 1, 2, mode_cbc_encrypt, mode_cbc_decrypt },
+  { 0 }
+};
diff --git a/crypto/crypto.c b/crypto/crypto.c
new file mode 100644 (file)
index 0000000..1866a65
--- /dev/null
@@ -0,0 +1,17 @@
+/*
+ */
+
+#include <endian.h>
+
+#include "hbytes.h"
+#include "tables.h"
+#include "serpent.h"
+
+void memxor(Byte *dest, const Byte *src, int l) {
+  while (l--) *dest++ ^= *src++;
+}
+
+int do_hbytes_pkcs5(ClientData cd, Tcl_Interp *ip,
+                   const PadMethod *meth, int objc, Tcl_Obj *const *objv) {
+  return meth->func((void*)meth, ip, objc, objv);
+}
index 4bbf8f67663b54d904512f7f98c4c1921f006829..5f06d7195088cdcd6464500cb169e7ed2fe83fd2 100644 (file)
@@ -6,7 +6,6 @@
 
 #include "hbytes.h"
 #include "tables.h"
-#include "hbintern.h"
 
 #define COMPLEX(hb) ((HBytes_ComplexValue*)hb->begin_complex)
 #define SIMPLE_LEN(hb) ((Byte*)(hb)->end_0 - (Byte*)(hb)->begin_complex)
index ee69928237e7202581091154da3bbe9e41d3cfb4..75ce6c816bf0f86f95fb3ded4d91c2f88cfb7702 100644 (file)
@@ -128,18 +128,50 @@ int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
 
 /* from crypto.c */
 
+void memxor(Byte *dest, const Byte *src, int l);
+
+/* from hash.c */
+
 typedef struct {
   int blocksize, hashsize;
 } HashAlgInfo;
 
+/* from blockciph.c */
+
+typedef struct {
+  void (*make_schedule)(void *schedule, const Byte *key, int keylen);
+  void (*crypt)(const void *schedule, const void *in, void *out);
+     /* in and out may be the same, but if they aren't they may not overlap */
+     /* in and out for crypt will have been through block_byteswap */
+} BlockCipherDirectionInfo;
+
 typedef struct {
-  int blocksize;
+  const char *name;
+  int blocksize, schedule_size, key_min, key_max;
+  void (*byteswap)(Byte *block);
+  BlockCipherDirectionInfo encrypt, decrypt;
 } BlockCipherAlgInfo;
 
+extern const BlockCipherAlgInfo blockcipheralginfos[];
+
+/* from bcmode.c */
+
 typedef struct {
-  int dummy;
+  const char *name;
+  int iv_blocks, buf_blocks;
+  const char *(*encrypt)(Byte *data, int blocks,
+                        const Byte *iv, Byte *buf,
+                        const BlockCipherAlgInfo *alg, int encr,
+                        int blocksize, const void *sch);
+  const char *(*decrypt)(Byte *data, int blocks,
+                        const Byte *iv, Byte *buf,
+                        const BlockCipherAlgInfo *alg, int encr,
+                        int blocksize, const void *sch);
+    /* in each case, *iv is provided, but may be modified */
 } BlockCipherModeInfo;
 
+extern const BlockCipherModeInfo blockciphermodeinfos[];
+
 /* useful macros */
 
 #define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)