chiark / gitweb /
new initialisation arrangements are sane and consistent
[chiark-tcl.git] / crypto / algtables.c
index 6f7a81cb894c32abd8923928d7a078708d2dffbd..0e15cdf67e01f1f3b444157b6f366e2b5b61730d 100644 (file)
@@ -1,32 +1,73 @@
 /*
+ * crypto - Tcl bindings for parts of the `nettle' crypto library
+ * Copyright 2006 Ian Jackson
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
  */
 
-#include <stdint.h>
-#include <netinet/in.h>
 
-#include "hbytes.h"
-#include "serpent.h"
+#include "chiark_tcl_crypto.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 cht_blockcipheralginfo_entries[]= {
+#define ALIAS(alias,name,NAME)                                 \
+  { #alias, NAME##_BLOCK_SIZE, sizeof(struct name##_ctx),      \
+       NAME##_MIN_KEY_SIZE, NAME##_MAX_KEY_SIZE,               \
+    { 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 BlockCipherAlgInfo blockcipheralginfos[]= {
-  { "serpent", 16, sizeof(SerpentKeySchedule), 16,32,
-    serpent_byteswap_block,
-    { alg_serpent_makekey, alg_serpent_encrypt },
-    { alg_serpent_makekey, alg_serpent_decrypt } },
+const BlockCipherPropInfo cht_blockcipherpropinfo_entries[]= {
+  { "blocklen",  offsetof(BlockCipherAlgInfo, blocksize) },
+  { "minkeylen", offsetof(BlockCipherAlgInfo, key_min)   },
+  { "maxkeylen", offsetof(BlockCipherAlgInfo, key_max)   },
   { 0 }
 };
 
@@ -54,7 +95,13 @@ const BlockCipherAlgInfo blockcipheralginfos[]= {
   NETTLE_DIGESTS
 #undef DO
 
-const HashAlgInfo hashalginfos[]= {
+const HashAlgPropInfo cht_hashalgpropinfo_entries[]= {
+  { "hashlen",  offsetof(HashAlgInfo, hashsize)  },
+  { "blocklen", offsetof(HashAlgInfo, blocksize) },
+  { 0 }
+};
+
+const HashAlgInfo cht_hashalginfo_entries[]= {
 #define DO(name,NAME)                                                      \
   { #name, NAME##_DIGEST_SIZE, NAME##_DATA_SIZE, sizeof(struct name##_ctx), \
     alg_##name##_init, alg_##name##_update, alg_##name##_final,                    \