From: ian Date: Sun, 1 Sep 2002 22:02:11 +0000 (+0000) Subject: Compiles but does not link. Before undo silly pad indirection. X-Git-Tag: debian/1.1.1~171 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-tcl.git;a=commitdiff_plain;h=9b7d11070d3e9dc1eb61cbccd5155f47a27047c3 Compiles but does not link. Before undo silly pad indirection. --- diff --git a/base/chiark-tcl.h b/base/chiark-tcl.h index ee69928..75ce6c8 100644 --- a/base/chiark-tcl.h +++ b/base/chiark-tcl.h @@ -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) diff --git a/base/tables-examples.tct b/base/tables-examples.tct index ecb5d7c..13a3e5d 100644 --- a/base/tables-examples.tct +++ b/base/tables-examples.tct @@ -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; diff --git a/base/troglodyte-Makefile b/base/troglodyte-Makefile index 784273a..8e94090 100644 --- a/base/troglodyte-Makefile +++ b/base/troglodyte-Makefile @@ -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 index 0000000..7d5a5d4 --- /dev/null +++ b/crypto/bcmode.c @@ -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 index 0000000..1866a65 --- /dev/null +++ b/crypto/crypto.c @@ -0,0 +1,17 @@ +/* + */ + +#include + +#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); +} diff --git a/hbytes/hbytes.c b/hbytes/hbytes.c index 4bbf8f6..5f06d71 100644 --- a/hbytes/hbytes.c +++ b/hbytes/hbytes.c @@ -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) diff --git a/hbytes/hbytes.h b/hbytes/hbytes.h index ee69928..75ce6c8 100644 --- a/hbytes/hbytes.h +++ b/hbytes/hbytes.h @@ -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)