From 9af88eb2e41e2b6a73643948e31262eee08c5400 Mon Sep 17 00:00:00 2001 From: ian Date: Sun, 29 Sep 2002 19:30:03 +0000 Subject: [PATCH] better core algorithm selection and new core alg suites --- base/chiark-tcl.h | 7 +++++ base/tables-examples.tct | 8 +++++ base/troglodyte-Makefile | 6 ++-- crypto/algtables.c | 63 ++++++++++++++++++++++++++++++---------- crypto/bcmode.c | 24 +++++++-------- crypto/crypto.c | 14 +++++++++ hbytes/hbytes.h | 7 +++++ 7 files changed, 98 insertions(+), 31 deletions(-) diff --git a/base/chiark-tcl.h b/base/chiark-tcl.h index 383eb53..06c98fc 100644 --- a/base/chiark-tcl.h +++ b/base/chiark-tcl.h @@ -32,9 +32,11 @@ * 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-prop PROPERTY ALG => property value * * 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 */ +typedef struct { + const char *name; + int int_offset; +} BlockCipherPropInfo, HashAlgPropInfo; + typedef struct { const char *name; int hashsize, blocksize, statesize; diff --git a/base/tables-examples.tct b/base/tables-examples.tct index 5a1859d..56fd4ab 100644 --- a/base/tables-examples.tct +++ b/base/tables-examples.tct @@ -149,6 +149,10 @@ Table hbytes HBytes_SubCommand key obj ?maclen obj => hb + hash-prop + prop enum(HashAlgPropInfo, "prop") + alg enum(HashAlgInfo, "alg") + => int Table dgram_socket DgramSocket_SubCommand create @@ -186,6 +190,10 @@ Table blockcipherop BlockCipherOp mode enum(BlockCipherModeInfo, "mode") iv hb => hb + prop -1 + prop enum(BlockCipherPropInfo, "prop") + alg enum(BlockCipherAlgInfo, "alg") + => int EntryExtra BlockCipherOp int encrypt; diff --git a/base/troglodyte-Makefile b/base/troglodyte-Makefile index 6807fe4..d84d43e 100644 --- a/base/troglodyte-Makefile +++ b/base/troglodyte-Makefile @@ -9,12 +9,10 @@ OBJS= tables.o \ bcmode.o \ misc.o \ algtables.o \ - serpent.o \ crypto.o \ parse.o HDRS= hbytes.h \ - serpent.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 $< -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) *~ ./#*# diff --git a/crypto/algtables.c b/crypto/algtables.c index 6f7a81c..8d7b0a2 100644 --- a/crypto/algtables.c +++ b/crypto/algtables.c @@ -5,28 +5,55 @@ #include #include "hbytes.h" -#include "serpent.h" #include #include +#include +#include +#include +#include -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[]= { - { "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 } }; @@ -54,6 +81,12 @@ const BlockCipherAlgInfo blockcipheralginfos[]= { 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), \ diff --git a/crypto/bcmode.c b/crypto/bcmode.c index f6a2a4b..9a5ae92 100644 --- a/crypto/bcmode.c +++ b/crypto/bcmode.c @@ -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); - alg->byteswap(chain); + if (alg->byteswap) alg->byteswap(chain); 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); - alg->byteswap(data); + if (alg->byteswap) alg->byteswap(data); 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); - alg->byteswap(chain); + if (alg->byteswap) alg->byteswap(chain); 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; - alg->byteswap(data); + if (alg->byteswap) alg->byteswap(data); 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); - alg->byteswap(buf); + if (alg->byteswap) alg->byteswap(buf); 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); @@ -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); - alg->byteswap(buf); + if (alg->byteswap) alg->byteswap(buf); 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); - alg->byteswap(buf); + if (alg->byteswap) alg->byteswap(buf); return 0; } @@ -94,9 +94,9 @@ static const char *mode_ecb(Byte *data, int blocks, 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); - alg->byteswap(data); + if (alg->byteswap) alg->byteswap(data); blocks--; data += blocksize; } return 0; diff --git a/crypto/crypto.c b/crypto/crypto.c index 1fc1f83..9b79881 100644 --- a/crypto/crypto.c +++ b/crypto/crypto.c @@ -359,3 +359,17 @@ int do_hbytes_hmac(ClientData cd, Tcl_Interp *ip, const HashAlgInfo *alg, 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; +} diff --git a/hbytes/hbytes.h b/hbytes/hbytes.h index 383eb53..06c98fc 100644 --- a/hbytes/hbytes.h +++ b/hbytes/hbytes.h @@ -32,9 +32,11 @@ * 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-prop PROPERTY ALG => property value * * 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 */ +typedef struct { + const char *name; + int int_offset; +} BlockCipherPropInfo, HashAlgPropInfo; + typedef struct { const char *name; int hashsize, blocksize, statesize; -- 2.30.2