chiark / gitweb /
better core algorithm selection and new core alg suites
authorian <ian>
Sun, 29 Sep 2002 19:30:03 +0000 (19:30 +0000)
committerian <ian>
Sun, 29 Sep 2002 19:30:03 +0000 (19:30 +0000)
base/chiark-tcl.h
base/tables-examples.tct
base/troglodyte-Makefile
crypto/algtables.c
crypto/bcmode.c
crypto/crypto.c
hbytes/hbytes.h

index 383eb53582076f6e8f52ef2d5b2e94112843d368..06c98fce518d12654e61912823671b0c2ce58034 100644 (file)
  *  hbytes pkcs5 pn|un VAR BLOCKSIZE             => worked?  (always 1 for p)
  *  hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
  *  hbytes blockcipher mac MSG ALG KEY MODE IV   => final block
  *  hbytes pkcs5 pn|un VAR BLOCKSIZE             => worked?  (always 1 for p)
  *  hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
  *  hbytes blockcipher mac MSG ALG KEY MODE IV   => final block
+ *  hbytes blockcipher prop PROPERTY ALG         => property value
  *
  *  hbytes hash ALG MESSAGE                      => hash
  *  hbytes hmac ALG MESSAGE KEY [MACLENGTH]      => mac
  *
  *  hbytes hash ALG MESSAGE                      => hash
  *  hbytes hmac ALG MESSAGE KEY [MACLENGTH]      => mac
+ *  hbytes hash-prop PROPERTY ALG                => property value
  *
  *  ulong ul2int ULONG    => INT           can fail if >INT_MAX
  *  ulong int2ul INT      => ULONG         can fail if <0
  *
  *  ulong ul2int ULONG    => INT           can fail if >INT_MAX
  *  ulong int2ul INT      => ULONG         can fail if <0
@@ -241,6 +243,11 @@ extern Tcl_ObjType blockcipherkey_type;
 
 /* from algtables.c */
 
 
 /* from algtables.c */
 
+typedef struct {
+  const char *name;
+  int int_offset;
+} BlockCipherPropInfo, HashAlgPropInfo;
+
 typedef struct {
   const char *name;
   int hashsize, blocksize, statesize;
 typedef struct {
   const char *name;
   int hashsize, blocksize, statesize;
index 5a1859dfdaa602975ffcac79853c547fc5ab8856..56fd4ab2ae4e926ac636704c9e5df75fd3b9a952 100644 (file)
@@ -149,6 +149,10 @@ Table hbytes HBytes_SubCommand
                key     obj
                ?maclen obj
                =>      hb
                key     obj
                ?maclen obj
                =>      hb
+       hash-prop
+               prop    enum(HashAlgPropInfo, "prop")
+               alg     enum(HashAlgInfo, "alg")
+               =>      int
 
 Table dgram_socket DgramSocket_SubCommand
        create
 
 Table dgram_socket DgramSocket_SubCommand
        create
@@ -186,6 +190,10 @@ Table blockcipherop BlockCipherOp
                mode    enum(BlockCipherModeInfo, "mode")
                iv      hb
                =>      hb
                mode    enum(BlockCipherModeInfo, "mode")
                iv      hb
                =>      hb
+       prop    -1
+               prop    enum(BlockCipherPropInfo, "prop")
+               alg     enum(BlockCipherAlgInfo, "alg")
+               =>      int
 
 EntryExtra BlockCipherOp
        int encrypt;
 
 EntryExtra BlockCipherOp
        int encrypt;
index 6807fe49fc79238f7165db949c4665add41f1ba5..d84d43e3d5a32f18bcb33243c0af935eb1b4fb04 100644 (file)
@@ -9,12 +9,10 @@ OBJS=         tables.o \
                bcmode.o \
                misc.o \
                algtables.o \
                bcmode.o \
                misc.o \
                algtables.o \
-               serpent.o \
                crypto.o \
                parse.o
 
 HDRS=          hbytes.h \
                crypto.o \
                parse.o
 
 HDRS=          hbytes.h \
-               serpent.h \
                $(AUTO_HDRS)
 
 AUTO_HDRS=     tables.h
                $(AUTO_HDRS)
 
 AUTO_HDRS=     tables.h
@@ -50,8 +48,8 @@ autoco%.tcl:  deco%gen.tcl decobogen.tcl general.tcl ./hbytes.so \
 %.o:           %.c $(HDRS)
                $(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $<
 
 %.o:           %.c $(HDRS)
                $(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $<
 
-serpent.o:     serpent.c serpent.h serpentsboxes.h
-               $(CC_CRYPTO) -o $@ -c $<
+#alg.o:                alg.c alg.h alg2.h
+#              $(CC_CRYPTO) -o $@ -c $<
 
 clean:
                rm -f $(OBJS) $(TARGETS) *~ ./#*#
 
 clean:
                rm -f $(OBJS) $(TARGETS) *~ ./#*#
index 6f7a81cb894c32abd8923928d7a078708d2dffbd..8d7b0a27215a412ac71fd4100167e16ce2a5991c 100644 (file)
@@ -5,28 +5,55 @@
 #include <netinet/in.h>
 
 #include "hbytes.h"
 #include <netinet/in.h>
 
 #include "hbytes.h"
-#include "serpent.h"
 
 #include <nettle/md5.h>
 #include <nettle/sha.h>
 
 #include <nettle/md5.h>
 #include <nettle/sha.h>
+#include <nettle/serpent.h>
+#include <nettle/twofish.h>
+#include <nettle/aes.h>
+#include <nettle/blowfish.h>
 
 
-static void alg_serpent_makekey(void *schedule, const void *key, int keylen) {
-  serpent_makekey(schedule, key, keylen);
-}
+#define NETTLE_BLOCKCIPHERS                    \
+  DO(serpent,  SERPENT)                                \
+  DO(twofish,  TWOFISH)                                \
+  DO(aes,      AES)                            \
+  DO(blowfish, BLOWFISH)                       \
+  ALIAS(rijndael, aes, AES)
 
 
-static void alg_serpent_encrypt(const void *sch, const void *in, void *out) {
-  serpent_encrypt(in, out, sch);
-}
-  
-static void alg_serpent_decrypt(const void *sch, const void *in, void *out) {
-  serpent_decrypt(in, out, sch);
-}
+#define ALIAS(alias,name,NAME)
+#define DO(name,NAME)                                                        \
+  static void alg_##name##_makekey(void *sch, const void *key, int keylen) {  \
+    name##_set_key(sch, keylen, key);                                        \
+  }                                                                          \
+  static void alg_##name##_encr(const void *sch, const void *in, void *out) { \
+    ##name##_encrypt((void*)sch, NAME##_BLOCK_SIZE, out, in);                \
+  }                                                                          \
+  static void alg_##name##_decr(const void *sch, const void *in, void *out) { \
+    ##name##_decrypt((void*)sch, NAME##_BLOCK_SIZE, out, in);                \
+  }
+  NETTLE_BLOCKCIPHERS
+#undef DO
+#undef ALIAS
 
 const BlockCipherAlgInfo blockcipheralginfos[]= {
 
 const BlockCipherAlgInfo blockcipheralginfos[]= {
-  { "serpent", 16, sizeof(SerpentKeySchedule), 16,32,
-    serpent_byteswap_block,
-    { alg_serpent_makekey, alg_serpent_encrypt },
-    { alg_serpent_makekey, alg_serpent_decrypt } },
+#define ALIAS(alias,name,NAME)                                 \
+  { #alias, NAME##_BLOCK_SIZE, sizeof(struct name##_ctx),      \
+       NAME##_MIN_KEY_SIZE, NAME##_MAX_KEY_SIZE,               \
+    0,                                                         \
+    { alg_##name##_makekey, alg_##name##_encr },               \
+    { alg_##name##_makekey, alg_##name##_decr }                        \
+  },
+#define DO(name,NAME) ALIAS(name,name,NAME)
+  NETTLE_BLOCKCIPHERS
+#undef DO
+#undef ALIAS
+  { 0 }
+};
+
+const BlockCipherPropInfo blockcipherpropinfos[]= {
+  { "blocklen",  offsetof(BlockCipherAlgInfo, blocksize) },
+  { "minkeylen", offsetof(BlockCipherAlgInfo, key_min)   },
+  { "maxkeylen", offsetof(BlockCipherAlgInfo, key_max)   },
   { 0 }
 };
 
   { 0 }
 };
 
@@ -54,6 +81,12 @@ const BlockCipherAlgInfo blockcipheralginfos[]= {
   NETTLE_DIGESTS
 #undef DO
 
   NETTLE_DIGESTS
 #undef DO
 
+const HashAlgPropInfo hashalgpropinfos[]= {
+  { "hashlen",  offsetof(HashAlgInfo, hashsize)  },
+  { "blocklen", offsetof(HashAlgInfo, blocksize) },
+  { 0 }
+};
+
 const HashAlgInfo hashalginfos[]= {
 #define DO(name,NAME)                                                      \
   { #name, NAME##_DIGEST_SIZE, NAME##_DATA_SIZE, sizeof(struct name##_ctx), \
 const HashAlgInfo hashalginfos[]= {
 #define DO(name,NAME)                                                      \
   { #name, NAME##_DIGEST_SIZE, NAME##_DATA_SIZE, sizeof(struct name##_ctx), \
index f6a2a4bddcedc3f41086476145a3058d3d9f4bfc..9a5ae928e1aed6dee25ddbe1bc1721379b2840f0 100644 (file)
@@ -9,16 +9,16 @@ static const char *mode_cbc_encrypt(Byte *data, int blocks,
                                    const void *sch) {
   int blocksize= alg->blocksize;
   memcpy(chain,iv,blocksize);
                                    const void *sch) {
   int blocksize= alg->blocksize;
   memcpy(chain,iv,blocksize);
-  alg->byteswap(chain);
+  if (alg->byteswap) alg->byteswap(chain);
   
   while (blocks > 0) {
   
   while (blocks > 0) {
-    alg->byteswap(data);
+    if (alg->byteswap) alg->byteswap(data);
 
     memxor(data, chain, blocksize);
     alg->encrypt.crypt(sch, data, data);
     memcpy(chain, data, blocksize);
 
 
     memxor(data, chain, blocksize);
     alg->encrypt.crypt(sch, data, data);
     memcpy(chain, data, blocksize);
 
-    alg->byteswap(data);
+    if (alg->byteswap) alg->byteswap(data);
     blocks--; data += blocksize;
   }
   return 0;
     blocks--; data += blocksize;
   }
   return 0;
@@ -32,17 +32,17 @@ static const char *mode_cbc_decrypt(Byte *data, int blocks,
   int cchain= 0;
 
   memcpy(chain,iv,blocksize);
   int cchain= 0;
 
   memcpy(chain,iv,blocksize);
-  alg->byteswap(chain);
+  if (alg->byteswap) alg->byteswap(chain);
   
   while (blocks > 0) {
   
   while (blocks > 0) {
-    alg->byteswap(data);
+    if (alg->byteswap) alg->byteswap(data);
     
     memcpy(chain + (cchain^blocksize), data, blocksize);
     alg->decrypt.crypt(sch, data, data);
     memxor(data, chain + cchain, blocksize);
     cchain ^= blocksize;
 
     
     memcpy(chain + (cchain^blocksize), data, blocksize);
     alg->decrypt.crypt(sch, data, data);
     memxor(data, chain + cchain, blocksize);
     cchain ^= blocksize;
 
-    alg->byteswap(data);
+    if (alg->byteswap) alg->byteswap(data);
     blocks--; data += blocksize;
   }
   return 0;
     blocks--; data += blocksize;
   }
   return 0;
@@ -55,11 +55,11 @@ static void cbcmac_core(const Byte *data, int blocks,
   int blocksize= alg->blocksize;
 
   memcpy(buf,iv,blocksize);
   int blocksize= alg->blocksize;
 
   memcpy(buf,iv,blocksize);
-  alg->byteswap(buf);
+  if (alg->byteswap) alg->byteswap(buf);
   
   while (blocks > 0) {
     memcpy(buf + blocksize, data, blocksize);
   
   while (blocks > 0) {
     memcpy(buf + blocksize, data, blocksize);
-    alg->byteswap(buf + blocksize);
+    if (alg->byteswap) alg->byteswap(buf + blocksize);
     memxor(buf, buf + blocksize, blocksize);
     
     alg->encrypt.crypt(sch, buf, buf);
     memxor(buf, buf + blocksize, blocksize);
     
     alg->encrypt.crypt(sch, buf, buf);
@@ -73,7 +73,7 @@ static const char *mode_cbc_mac(const Byte *data, int blocks,
                                const BlockCipherAlgInfo *alg,
                                const void *sch) {
   cbcmac_core(data,blocks,iv,buf,alg,sch);
                                const BlockCipherAlgInfo *alg,
                                const void *sch) {
   cbcmac_core(data,blocks,iv,buf,alg,sch);
-  alg->byteswap(buf);
+  if (alg->byteswap) alg->byteswap(buf);
   return 0;
 }
 
   return 0;
 }
 
@@ -83,7 +83,7 @@ static const char *mode_cbc_mac2(const Byte *data, int blocks,
                                 const void *sch) {
   cbcmac_core(data,blocks,iv,buf,alg,sch);
   alg->encrypt.crypt(sch, buf, buf);
                                 const void *sch) {
   cbcmac_core(data,blocks,iv,buf,alg,sch);
   alg->encrypt.crypt(sch, buf, buf);
-  alg->byteswap(buf);
+  if (alg->byteswap) alg->byteswap(buf);
   return 0;
 }
 
   return 0;
 }
 
@@ -94,9 +94,9 @@ static const char *mode_ecb(Byte *data, int blocks,
   int blocksize= alg->blocksize;
   
   while (blocks > 0) {
   int blocksize= alg->blocksize;
   
   while (blocks > 0) {
-    alg->byteswap(data);
+    if (alg->byteswap) alg->byteswap(data);
     (encr ? &alg->encrypt : &alg->decrypt)->crypt(sch, data, data);
     (encr ? &alg->encrypt : &alg->decrypt)->crypt(sch, data, data);
-    alg->byteswap(data);
+    if (alg->byteswap) alg->byteswap(data);
     blocks--; data += blocksize;
   }
   return 0;
     blocks--; data += blocksize;
   }
   return 0;
index 1fc1f8336bfe65cf14b5934987ac11c0f7d1ba93..9b79881941eaa6418495489830a3546df09b58a6 100644 (file)
@@ -359,3 +359,17 @@ int do_hbytes_hmac(ClientData cd, Tcl_Interp *ip, const HashAlgInfo *alg,
 
   return TCL_OK;
 }
 
   return TCL_OK;
 }
+
+int do_blockcipherop_prop(ClientData cd, Tcl_Interp *ip,
+                         const BlockCipherPropInfo *prop,
+                         const BlockCipherAlgInfo *alg, int *result) {
+  *result= *(const int*)((const char*)alg + prop->int_offset);
+  return TCL_OK;
+}
+
+int do_hbytes_hash_prop(ClientData cd, Tcl_Interp *ip,
+                       const HashAlgPropInfo *prop,
+                       const HashAlgInfo *alg, int *result) {
+  *result= *(const int*)((const char*)alg + prop->int_offset);
+  return TCL_OK;
+}
index 383eb53582076f6e8f52ef2d5b2e94112843d368..06c98fce518d12654e61912823671b0c2ce58034 100644 (file)
  *  hbytes pkcs5 pn|un VAR BLOCKSIZE             => worked?  (always 1 for p)
  *  hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
  *  hbytes blockcipher mac MSG ALG KEY MODE IV   => final block
  *  hbytes pkcs5 pn|un VAR BLOCKSIZE             => worked?  (always 1 for p)
  *  hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
  *  hbytes blockcipher mac MSG ALG KEY MODE IV   => final block
+ *  hbytes blockcipher prop PROPERTY ALG         => property value
  *
  *  hbytes hash ALG MESSAGE                      => hash
  *  hbytes hmac ALG MESSAGE KEY [MACLENGTH]      => mac
  *
  *  hbytes hash ALG MESSAGE                      => hash
  *  hbytes hmac ALG MESSAGE KEY [MACLENGTH]      => mac
+ *  hbytes hash-prop PROPERTY ALG                => property value
  *
  *  ulong ul2int ULONG    => INT           can fail if >INT_MAX
  *  ulong int2ul INT      => ULONG         can fail if <0
  *
  *  ulong ul2int ULONG    => INT           can fail if >INT_MAX
  *  ulong int2ul INT      => ULONG         can fail if <0
@@ -241,6 +243,11 @@ extern Tcl_ObjType blockcipherkey_type;
 
 /* from algtables.c */
 
 
 /* from algtables.c */
 
+typedef struct {
+  const char *name;
+  int int_offset;
+} BlockCipherPropInfo, HashAlgPropInfo;
+
 typedef struct {
   const char *name;
   int hashsize, blocksize, statesize;
 typedef struct {
   const char *name;
   int hashsize, blocksize, statesize;