From 288c17549e4d409b60147b6eb8444e72c0eea4e2 Mon Sep 17 00:00:00 2001 From: ian Date: Sat, 7 Jan 2006 20:40:49 +0000 Subject: [PATCH 1/1] hbytes and crypto compile now --- Makefile | 3 +- adns/Makefile | 1 - adns/chiark_tcl_adns.h | 2 +- base/extension.make | 6 +- crypto/.cvsignore | 2 + crypto/Makefile | 9 +++ crypto/algtables.c | 18 ++--- crypto/bcmode.c | 4 +- crypto/chiark_tcl_crypto.h | 10 +++ crypto/crypto.c | 143 ++++++++++++++++++------------------- crypto/crypto.h | 19 +++-- crypto/crypto.tct | 39 +++++----- crypto/hook.c | 25 ++++++- hbytes/Makefile | 1 + hbytes/hbytes-base.tct | 10 +++ hbytes/hbytes.h | 2 + hbytes/hbytes.tct | 10 --- hbytes/hook.c | 1 - 18 files changed, 181 insertions(+), 124 deletions(-) create mode 100644 crypto/.cvsignore create mode 100644 crypto/Makefile create mode 100644 crypto/chiark_tcl_crypto.h create mode 100644 hbytes/hbytes-base.tct diff --git a/Makefile b/Makefile index 1ccf437..d0462d6 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ -SUBDIRS= base adns hbytes crypto dgram tuntap +SUBDIRS= base adns hbytes crypto +# dgram tuntap default: all diff --git a/adns/Makefile b/adns/Makefile index 31e3df9..869d890 100644 --- a/adns/Makefile +++ b/adns/Makefile @@ -1,6 +1,5 @@ BASE_DIR = ../base EXTBASE = adns -TABLE = adnscmds CFILES = adns LDLIBS += -ladns diff --git a/adns/chiark_tcl_adns.h b/adns/chiark_tcl_adns.h index 0881253..af8de0e 100644 --- a/adns/chiark_tcl_adns.h +++ b/adns/chiark_tcl_adns.h @@ -13,6 +13,6 @@ typedef struct { const IdDataSpec adnstcl_queries, adnstcl_resolvers; -#include "adnscmds.h" +#include "adns+tcmdif.h" #endif /*ADNSTCL_H*/ diff --git a/base/extension.make b/base/extension.make index d9ae479..d12723b 100644 --- a/base/extension.make +++ b/base/extension.make @@ -12,12 +12,12 @@ LDLIBS += $(BASE_DIR)/chiark-tcl.so include $(BASE_DIR)/common.make include $(BASE_DIR)/shlib.make -TCMDIFARGS ?= -p$(EXTENSION) -o$@ $(BASE_TCT) $< +TCMDIFARGS ?= -p$(EXTENSION) -o$@ $(BASE_TCT) $(OTHER_TCTS) $< -%+tcmdif.c: %.tct $(BASE_TCT) $(TCMDIFGEN) +%+tcmdif.c: %.tct $(BASE_TCT) $(OTHER_TCTS) $(TCMDIFGEN) $(TCMDIFGEN) -wc $(TCMDIFARGS) -%+tcmdif.h: %.tct $(BASE_TCT) $(TCMDIFGEN) +%+tcmdif.h: %.tct $(BASE_TCT) $(OTHER_TCTS) $(TCMDIFGEN) $(TCMDIFGEN) -wh $(TCMDIFARGS) include $(BASE_DIR)/final.make diff --git a/crypto/.cvsignore b/crypto/.cvsignore new file mode 100644 index 0000000..fc2ec4f --- /dev/null +++ b/crypto/.cvsignore @@ -0,0 +1,2 @@ +*+tcmdif.[ch] +*.d diff --git a/crypto/Makefile b/crypto/Makefile new file mode 100644 index 0000000..12fccd7 --- /dev/null +++ b/crypto/Makefile @@ -0,0 +1,9 @@ +BASE_DIR = ../base +EXTBASE = crypto +CFILES = algtables bcmode crypto hook +OTHER_TCTS = ../hbytes/hbytes-base.tct +CPPFLAGS += -I../hbytes +LDLIBS += ../hbytes/chiark_tcl_hbytes.so -lnettle + +include ../base/extension.make + diff --git a/crypto/algtables.c b/crypto/algtables.c index bb111de..ea591fb 100644 --- a/crypto/algtables.c +++ b/crypto/algtables.c @@ -1,11 +1,7 @@ /* */ -#include -#include -#include - -#include "hbytes.h" +#include "chiark_tcl_crypto.h" #include #include @@ -17,9 +13,9 @@ #define NETTLE_BLOCKCIPHERS \ DO(serpent, SERPENT) \ DO(twofish, TWOFISH) \ - DO(aes, AES) \ +/* DO(aes, AES) */ \ DO(blowfish, BLOWFISH) \ - ALIAS(rijndael, aes, AES) + /* ALIAS(rijndael, aes, AES)*/ #define ALIAS(alias,name,NAME) #define DO(name,NAME) \ @@ -36,7 +32,7 @@ #undef DO #undef ALIAS -const BlockCipherAlgInfo blockcipheralginfos[]= { +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, \ @@ -50,7 +46,7 @@ const BlockCipherAlgInfo blockcipheralginfos[]= { { 0 } }; -const BlockCipherPropInfo blockcipherpropinfos[]= { +const BlockCipherPropInfo cht_blockcipherpropinfo_entries[]= { { "blocklen", offsetof(BlockCipherAlgInfo, blocksize) }, { "minkeylen", offsetof(BlockCipherAlgInfo, key_min) }, { "maxkeylen", offsetof(BlockCipherAlgInfo, key_max) }, @@ -81,13 +77,13 @@ const BlockCipherPropInfo blockcipherpropinfos[]= { NETTLE_DIGESTS #undef DO -const HashAlgPropInfo hashalgpropinfos[]= { +const HashAlgPropInfo cht_hashalgpropinfo_entries[]= { { "hashlen", offsetof(HashAlgInfo, hashsize) }, { "blocklen", offsetof(HashAlgInfo, blocksize) }, { 0 } }; -const HashAlgInfo hashalginfos[]= { +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, \ diff --git a/crypto/bcmode.c b/crypto/bcmode.c index e413de7..24dfdb8 100644 --- a/crypto/bcmode.c +++ b/crypto/bcmode.c @@ -1,7 +1,7 @@ /* */ -#include "hbytes.h" +#include "chiark_tcl_crypto.h" static const char *mode_cbc_encrypt(Byte *data, int blocks, const Byte *iv, Byte *chain, @@ -110,7 +110,7 @@ static const char *mode_ctr(Byte *data, int blocks, return 0; } -const BlockCipherModeInfo blockciphermodeinfos[]= { +const BlockCipherModeInfo cht_blockciphermodeinfo_entries[]= { { "cbc", 1, 2, 1, mode_cbc_encrypt, mode_cbc_decrypt, mode_cbc_mac }, { "cbc-mac2", 1, 2, 1, 0, 0, mode_cbc_mac2 }, { "ecb", 0, 0, 0, mode_ecb, mode_ecb, 0 }, diff --git a/crypto/chiark_tcl_crypto.h b/crypto/chiark_tcl_crypto.h new file mode 100644 index 0000000..02f68db --- /dev/null +++ b/crypto/chiark_tcl_crypto.h @@ -0,0 +1,10 @@ + +#include +#include +#include + +#include + +#include "hbytes.h" +#include "crypto.h" +#include "crypto+tcmdif.h" diff --git a/crypto/crypto.c b/crypto/crypto.c index cf50712..e8d7f1a 100644 --- a/crypto/crypto.c +++ b/crypto/crypto.c @@ -1,12 +1,9 @@ /* */ -#include +#include "chiark_tcl_crypto.h" -#include "hbytes.h" -#include "tables.h" - -const PadOp padops[]= { +const PadOp cht_padop_entries[]= { { "un", 0, 0 }, { "ua", 0, 1 }, { "pn", 1, 0 }, @@ -19,7 +16,7 @@ typedef struct { int pad, blocksize; /* 0 or 1 */ } PadMethodClientData; -int do_hbytes_pad(ClientData cd, Tcl_Interp *ip, const PadOp *op, +int cht_do_hbcrypto_pad(ClientData cd, Tcl_Interp *ip, const PadOp *op, HBytes_Var v, Tcl_Obj *blocksz, const PadMethodInfo *meth, int methargsc, Tcl_Obj *const *methargsv) { PadMethodClientData pmcd; @@ -27,13 +24,13 @@ int do_hbytes_pad(ClientData cd, Tcl_Interp *ip, const PadOp *op, if (op->use_algname) { const BlockCipherAlgInfo *alg; - alg= enum_lookup_cached(ip,blocksz,blockcipheralginfos, + alg= enum_lookup_cached(ip,blocksz, cht_blockcipheralginfo_entries, "blockcipher alg for pad"); if (!alg) return TCL_ERROR; pmcd.blocksize= alg->blocksize; } else { rc= Tcl_GetIntFromObj(ip, blocksz, &pmcd.blocksize); if (rc) return rc; - if (pmcd.blocksize < 1) staticerr(ip, "block size must be at least 1", 0); + if (pmcd.blocksize < 1) cht_staticerr(ip, "block size must be at least 1", 0); } pmcd.hb= v.hb; @@ -42,29 +39,29 @@ int do_hbytes_pad(ClientData cd, Tcl_Interp *ip, const PadOp *op, return meth->func(&pmcd,ip,methargsc,methargsv); } -int do_padmethodinfo_rfc2406(ClientData cd, Tcl_Interp *ip, +int cht_do_padmethodinfo_rfc2406(ClientData cd, Tcl_Interp *ip, Tcl_Obj *nxthdr_arg, int *ok) { const PadMethodClientData *pmcd= (const void*)cd; int i, rc, padlen, old_len; if (pmcd->blocksize > 256) - return staticerr(ip, "block size too large for RFC2406 padding", 0); + return cht_staticerr(ip, "block size too large for RFC2406 padding", 0); if (pmcd->pad) { Byte *padding; HBytes_Value nxthdr; - rc= pat_hb(ip,nxthdr_arg,&nxthdr); + rc= cht_pat_hb(ip,nxthdr_arg,&nxthdr); if (rc) return rc; - if (hbytes_len(&nxthdr) != 1) return - staticerr(ip, "RFC2406 next header field must be exactly 1 byte", 0); - padlen= pmcd->blocksize-1 - ((hbytes_len(pmcd->hb)+1) % pmcd->blocksize); - padding= hbytes_append(pmcd->hb, padlen+2); + if (cht_hb_len(&nxthdr) != 1) return + cht_staticerr(ip, "RFC2406 next header field must be exactly 1 byte", 0); + padlen= pmcd->blocksize-1 - ((cht_hb_len(pmcd->hb)+1) % pmcd->blocksize); + padding= cht_hb_append(pmcd->hb, padlen+2); for (i=1; i<=padlen; i++) *padding++ = i; *padding++ = padlen; - *padding++ = hbytes_data(&nxthdr)[0]; + *padding++ = cht_hb_data(&nxthdr)[0]; *ok= 1; } else { @@ -73,16 +70,16 @@ int do_padmethodinfo_rfc2406(ClientData cd, Tcl_Interp *ip, Tcl_Obj *nxthdr_valobj, *ro; *ok= 0; - old_len= hbytes_len(pmcd->hb); if (old_len % pmcd->blocksize) goto quit; - trailer= hbytes_unappend(pmcd->hb, 2); if (!trailer) goto quit; + old_len= cht_hb_len(pmcd->hb); if (old_len % pmcd->blocksize) goto quit; + trailer= cht_hb_unappend(pmcd->hb, 2); if (!trailer) goto quit; padlen= trailer[0]; - hbytes_array(&nxthdr,trailer+1,1); - nxthdr_valobj= ret_hb(ip,nxthdr); + cht_hb_array(&nxthdr,trailer+1,1); + nxthdr_valobj= cht_ret_hb(ip,nxthdr); ro= Tcl_ObjSetVar2(ip,nxthdr_arg,0,nxthdr_valobj,TCL_LEAVE_ERR_MSG); if (!ro) { Tcl_DecrRefCount(nxthdr_valobj); return TCL_ERROR; } - padding= hbytes_unappend(pmcd->hb, padlen); + padding= cht_hb_unappend(pmcd->hb, padlen); for (i=1; i<=padlen; i++) if (*padding++ != i) goto quit; @@ -95,30 +92,30 @@ int do_padmethodinfo_rfc2406(ClientData cd, Tcl_Interp *ip, return TCL_OK; } -int do_padmethodinfo_pkcs5(ClientData cd, Tcl_Interp *ip, int *ok) { +int cht_do_padmethodinfo_pkcs5(ClientData cd, Tcl_Interp *ip, int *ok) { const PadMethodClientData *pmcd= (const void*)cd; int padlen, old_len, i; if (pmcd->blocksize > 255) - return staticerr(ip, "block size too large for pkcs#5", 0); + return cht_staticerr(ip, "block size too large for pkcs#5", 0); if (pmcd->pad) { Byte *padding; - padlen= pmcd->blocksize - (hbytes_len(pmcd->hb) % pmcd->blocksize); - padding= hbytes_append(pmcd->hb, padlen); + padlen= pmcd->blocksize - (cht_hb_len(pmcd->hb) % pmcd->blocksize); + padding= cht_hb_append(pmcd->hb, padlen); memset(padding, padlen, padlen); } else { const Byte *padding; - old_len= hbytes_len(pmcd->hb); if (old_len % pmcd->blocksize) goto bad; - padding= hbytes_unappend(pmcd->hb, 1); if (!padding) goto bad; + old_len= cht_hb_len(pmcd->hb); if (old_len % pmcd->blocksize) goto bad; + padding= cht_hb_unappend(pmcd->hb, 1); if (!padding) goto bad; padlen= *padding; if (padlen < 1 || padlen > pmcd->blocksize) goto bad; - padding= hbytes_unappend(pmcd->hb, padlen-1); if (!padding) goto bad; + padding= cht_hb_unappend(pmcd->hb, padlen-1); if (!padding) goto bad; for (i=0; ihashsize); - alg->oneshot(dest, hbytes_data(&message), hbytes_len(&message)); + dest= cht_hb_arrayspace(result,alg->hashsize); + alg->oneshot(dest, cht_hb_data(&message), cht_hb_len(&message)); return TCL_OK; } @@ -176,7 +173,7 @@ static void key_t_dup(Tcl_Obj *src_obj, Tcl_Obj *dup_obj) { memcpy(dup->value, src->value, src->valuelen); noalg(dup); dup_obj->internalRep.otherValuePtr= dup; - dup_obj->typePtr= &blockcipherkey_type; + dup_obj->typePtr= &cht_blockcipherkey_type; } static void key_t_ustr(Tcl_Obj *o) { @@ -187,23 +184,23 @@ static int key_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) { int rc, l; CiphKeyValue *val; - rc= Tcl_ConvertToType(ip,o,&hbytes_type); if (rc) return rc; + rc= Tcl_ConvertToType(ip,o,&cht_hbytes_type); if (rc) return rc; val= TALLOC(sizeof(*val)); - val->valuelen= l= hbytes_len(OBJ_HBYTES(o)); + val->valuelen= l= cht_hb_len(OBJ_HBYTES(o)); val->value= TALLOC(l); val->buffers= 0; val->bufferslen= 0; - memcpy(val->value, hbytes_data(OBJ_HBYTES(o)), l); + memcpy(val->value, cht_hb_data(OBJ_HBYTES(o)), l); noalg(val); - objfreeir(o); + cht_objfreeir(o); o->internalRep.otherValuePtr= val; - o->typePtr= &blockcipherkey_type; + o->typePtr= &cht_blockcipherkey_type; return TCL_OK; } -Tcl_ObjType blockcipherkey_type = { +Tcl_ObjType cht_blockcipherkey_type = { "blockcipher-key", key_t_free, key_t_dup, key_t_ustr, key_t_sfa }; @@ -213,7 +210,7 @@ static CiphKeyValue *get_key(Tcl_Interp *ip, Tcl_Obj *key_obj, CiphKeyValue *key; int rc; - rc= Tcl_ConvertToType(ip,key_obj,&blockcipherkey_type); if (rc) return 0; + rc= Tcl_ConvertToType(ip,key_obj,&cht_blockcipherkey_type); if (rc) return 0; key= OBJ_CIPHKEY(key_obj); if (key->alg != alg) { @@ -230,7 +227,7 @@ static CiphKeyValue *get_key(Tcl_Interp *ip, Tcl_Obj *key_obj, return key; } -int do_hbytes_blockcipher(ClientData cd, Tcl_Interp *ip, +int cht_do_hbcrypto_blockcipher(ClientData cd, Tcl_Interp *ip, const BlockCipherOp *op, int objc, Tcl_Obj *const *objv) { return op->func((void*)op,ip,objc,objv); @@ -249,7 +246,7 @@ static int blockcipher_prep(Tcl_Interp *ip, Tcl_Obj *key_obj, CiphKeyValue *key; if (data_len % alg->blocksize) - return staticerr(ip, "block cipher input not whole number of blocks", + return cht_staticerr(ip, "block cipher input not whole number of blocks", "HBYTES BLOCKCIPHER LENGTH"); want_bufferslen= alg->blocksize * (mode->buf_blocks + mode->iv_blocks); @@ -260,9 +257,9 @@ static int blockcipher_prep(Tcl_Interp *ip, Tcl_Obj *key_obj, sched= *schedp; if (!sched) { if (key->valuelen < alg->key_min) - return staticerr(ip, "key too short", "HBYTES BLOCKCIPHER PARAMS"); + return cht_staticerr(ip, "key too short", "HBYTES BLOCKCIPHER PARAMS"); if (key->valuelen > alg->key_max) - return staticerr(ip, "key too long", "HBYTES BLOCKCIPHER PARAMS"); + return cht_staticerr(ip, "key too long", "HBYTES BLOCKCIPHER PARAMS"); sched= TALLOC(alg->schedule_size); (decrypt ? &alg->decrypt : &alg->encrypt)->make_schedule @@ -272,18 +269,18 @@ static int blockcipher_prep(Tcl_Interp *ip, Tcl_Obj *key_obj, want_iv= alg->blocksize * mode->iv_blocks; if (!want_iv) { - if (!hbytes_issentinel(iv)) - return staticerr(ip,"iv supplied but mode does not take one", 0); - } else if (hbytes_issentinel(iv)) { - if (decrypt) return staticerr(ip,"must supply iv when decrypting", 0); - rc= get_urandom(ip, key->buffers, want_iv); + if (!cht_hb_issentinel(iv)) + return cht_staticerr(ip,"iv supplied but mode does not take one", 0); + } else if (cht_hb_issentinel(iv)) { + if (decrypt) return cht_staticerr(ip,"must supply iv when decrypting", 0); + rc= cht_get_urandom(ip, key->buffers, want_iv); if (rc) return rc; } else { - int iv_supplied= hbytes_len(iv); + int iv_supplied= cht_hb_len(iv); if (iv_supplied > want_iv) - return staticerr(ip, "iv too large for algorithm and mode", + return cht_staticerr(ip, "iv too large for algorithm and mode", "HBYTES BLOCKCIPHER PARAMS"); - memcpy(key->buffers, hbytes_data(iv), iv_supplied); + memcpy(key->buffers, cht_hb_data(iv), iv_supplied); memset(key->buffers + iv_supplied, 0, want_iv - iv_supplied); } @@ -299,14 +296,14 @@ static int blockcipher_prep(Tcl_Interp *ip, Tcl_Obj *key_obj, return TCL_OK; } -int do_blockcipherop_d(ClientData cd, Tcl_Interp *ip, +int cht_do_blockcipherop_d(ClientData cd, Tcl_Interp *ip, HBytes_Var v, const BlockCipherAlgInfo *alg, Tcl_Obj *key_obj, const BlockCipherModeInfo *mode, HBytes_Value iv, HBytes_Value *result) { - return do_blockcipherop_e(cd,ip,v,alg,key_obj,mode,iv,result); + return cht_do_blockcipherop_e(cd,ip,v,alg,key_obj,mode,iv,result); } -int do_blockcipherop_e(ClientData cd, Tcl_Interp *ip, +int cht_do_blockcipherop_e(ClientData cd, Tcl_Interp *ip, HBytes_Var v, const BlockCipherAlgInfo *alg, Tcl_Obj *key_obj, const BlockCipherModeInfo *mode, HBytes_Value iv, HBytes_Value *result) { @@ -321,10 +318,10 @@ int do_blockcipherop_e(ClientData cd, Tcl_Interp *ip, int nblocks; if (!mode->encrypt) - return staticerr(ip, "mode does not support encrypt/decrypt", 0); + return cht_staticerr(ip, "mode does not support encrypt/decrypt", 0); rc= blockcipher_prep(ip,key_obj,&iv,!encrypt, - alg,mode, hbytes_len(v.hb), + alg,mode, cht_hb_len(v.hb), &key,&sched, &ivbuf,&iv_lenbytes, &buffers,&nblocks); @@ -332,17 +329,17 @@ int do_blockcipherop_e(ClientData cd, Tcl_Interp *ip, failure= (encrypt ? mode->encrypt : mode->decrypt) - (hbytes_data(v.hb), nblocks, ivbuf, buffers, alg, encrypt, sched); + (cht_hb_data(v.hb), nblocks, ivbuf, buffers, alg, encrypt, sched); if (failure) - return staticerr(ip, failure, "HBYTES BLOCKCIPHER CRYPTFAIL CRYPT"); + return cht_staticerr(ip, failure, "HBYTES BLOCKCIPHER CRYPTFAIL CRYPT"); - hbytes_array(result, ivbuf, iv_lenbytes); + cht_hb_array(result, ivbuf, iv_lenbytes); return TCL_OK; } -int do_blockcipherop_mac(ClientData cd, Tcl_Interp *ip, +int cht_do_blockcipherop_mac(ClientData cd, Tcl_Interp *ip, HBytes_Value msg, const BlockCipherAlgInfo *alg, Tcl_Obj *key_obj, const BlockCipherModeInfo *mode, HBytes_Value iv, HBytes_Value *result) { @@ -355,25 +352,25 @@ int do_blockcipherop_mac(ClientData cd, Tcl_Interp *ip, int rc; if (!mode->mac) - return staticerr(ip, "mode does not support mac generation", 0); + return cht_staticerr(ip, "mode does not support mac generation", 0); rc= blockcipher_prep(ip,key_obj,&iv,0, - alg,mode, hbytes_len(&msg), + alg,mode, cht_hb_len(&msg), &key,&sched, &ivbuf,&iv_lenbytes, &buffers,&nblocks); if (rc) return rc; - failure= mode->mac(hbytes_data(&msg), nblocks, ivbuf, buffers, alg, sched); + failure= mode->mac(cht_hb_data(&msg), nblocks, ivbuf, buffers, alg, sched); if (failure) - return staticerr(ip,failure, "HBYTES BLOCKCIPHER CRYPTFAIL MAC"); + return cht_staticerr(ip,failure, "HBYTES BLOCKCIPHER CRYPTFAIL MAC"); - hbytes_array(result, buffers, alg->blocksize * mode->mac_blocks); + cht_hb_array(result, buffers, alg->blocksize * mode->mac_blocks); return TCL_OK; } -int do_hbytes_hmac(ClientData cd, Tcl_Interp *ip, const HashAlgInfo *alg, +int cht_do_hbcrypto_hmac(ClientData cd, Tcl_Interp *ip, const HashAlgInfo *alg, HBytes_Value message, Tcl_Obj *key_obj, Tcl_Obj *maclen_obj, HBytes_Value *result) { /* key->alpha = state after H(K XOR ipad @@ -387,7 +384,7 @@ int do_hbytes_hmac(ClientData cd, Tcl_Interp *ip, const HashAlgInfo *alg, if (maclen_obj) { rc= Tcl_GetIntFromObj(ip, maclen_obj, &ml); if (rc) return rc; if (ml<0 || ml>alg->hashsize) - return staticerr(ip, "requested hmac output size out of range", + return cht_staticerr(ip, "requested hmac output size out of range", "HBYTES HMAC PARAMS"); } else { ml= alg->hashsize; @@ -401,7 +398,7 @@ int do_hbytes_hmac(ClientData cd, Tcl_Interp *ip, const HashAlgInfo *alg, assert(!key->beta); if (key->valuelen > alg->blocksize) - return staticerr(ip, "key to hmac longer than hash block size", + return cht_staticerr(ip, "key to hmac longer than hash block size", "HBYTES HMAC PARAMS"); memcpy(key->buffers, key->value, key->valuelen); @@ -419,29 +416,29 @@ int do_hbytes_hmac(ClientData cd, Tcl_Interp *ip, const HashAlgInfo *alg, } assert(key->beta); - dest= hbytes_arrayspace(result, alg->hashsize); + dest= cht_hb_arrayspace(result, alg->hashsize); memcpy(key->buffers, key->alpha, alg->statesize); - alg->update(key->buffers, hbytes_data(&message), hbytes_len(&message)); + alg->update(key->buffers, cht_hb_data(&message), cht_hb_len(&message)); alg->final(key->buffers, dest); memcpy(key->buffers, key->beta, alg->statesize); alg->update(key->buffers, dest, alg->hashsize); alg->final(key->buffers, dest); - hbytes_unappend(result, alg->hashsize - ml); + cht_hb_unappend(result, alg->hashsize - ml); return TCL_OK; } -int do_blockcipherop_prop(ClientData cd, Tcl_Interp *ip, +int cht_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, +int cht_do_hbcrypto_hash_prop(ClientData cd, Tcl_Interp *ip, const HashAlgPropInfo *prop, const HashAlgInfo *alg, int *result) { *result= *(const int*)((const char*)alg + prop->int_offset); diff --git a/crypto/crypto.h b/crypto/crypto.h index 82efb61..3350631 100644 --- a/crypto/crypto.h +++ b/crypto/crypto.h @@ -1,3 +1,11 @@ +/* + */ + +#ifndef CRYPTO_H +#define CRYPTO_H + +#include "chiark-tcl.h" + /* from crypto.c */ void memxor(Byte *dest, const Byte *src, int l); @@ -7,7 +15,7 @@ typedef struct { int pad, use_algname; } PadOp; -extern Tcl_ObjType blockcipherkey_type; +extern Tcl_ObjType cht_blockcipherkey_type; /* from algtables.c */ @@ -25,7 +33,7 @@ typedef struct { void (*oneshot)(void *digest, const void *data, int len); } HashAlgInfo; -extern const HashAlgInfo hashalginfos[]; +extern const HashAlgInfo cht_hashalginfo_entries[]; typedef struct { void (*make_schedule)(void *schedule, const void *key, int keylen); @@ -40,7 +48,7 @@ typedef struct { BlockCipherPerDirectionInfo encrypt, decrypt; } BlockCipherAlgInfo; -extern const BlockCipherAlgInfo blockcipheralginfos[]; +extern const BlockCipherAlgInfo cht_blockcipheralginfo_entries[]; /* from bcmode.c */ @@ -73,5 +81,8 @@ typedef struct { const void *sch); } BlockCipherModeInfo; -extern const BlockCipherModeInfo blockciphermodeinfos[]; +extern const BlockCipherModeInfo cht_blockciphermodeinfo_entries[]; + +#include "crypto+tcmdif.h" +#endif /*CRYPTO_H*/ diff --git a/crypto/crypto.tct b/crypto/crypto.tct index fa9de22..cc10545 100644 --- a/crypto/crypto.tct +++ b/crypto/crypto.tct @@ -1,26 +1,33 @@ -Table hbcrypto_SubCommand + + +Table *hbcryptotoplevel TopLevel_Command + hbcrypto + subcmd enum(HBCrypto/_SubCommand, "hbcrypto subcommand") + ... obj + +Table hbcrypto HBCrypto_SubCommand pad - op enum(PadOp, "hbytes pad subcommand") + op enum(PadOp/, "hbytes pad subcommand") v hbv blocksz obj - meth enum(PadMethodInfo, "pad method") + meth enum(PadMethodInfo/, "pad method") ... methargs blockcipher - op enum(BlockCipherOp, "op") + op enum(BlockCipherOp/, "op") ... obj hash - alg enum(HashAlgInfo, "hash alg") + alg enum(HashAlgInfo/, "hash alg") message hb => hb hmac - alg enum(HashAlgInfo, "hash alg for hmac") + alg enum(HashAlgInfo/, "hash alg for hmac") message hb key obj ?maclen obj => hb hash-prop - prop enum(HashAlgPropInfo, "prop") - alg enum(HashAlgInfo, "alg") + prop enum(HashAlgPropInfo/, "prop") + alg enum(HashAlgInfo/, "alg") => int Table padmethodinfo PadMethodInfo @@ -33,28 +40,28 @@ Table padmethodinfo PadMethodInfo Table blockcipherop BlockCipherOp e 1 v hbv - alg enum(BlockCipherAlgInfo, "alg") + alg enum(BlockCipherAlgInfo/, "alg") key obj - mode enum(BlockCipherModeInfo, "mode") + mode enum(BlockCipherModeInfo/, "mode") ?iv hb => hb d 0 v hbv - alg enum(BlockCipherAlgInfo, "alg") + alg enum(BlockCipherAlgInfo/, "alg") key obj - mode enum(BlockCipherModeInfo, "mode") + mode enum(BlockCipherModeInfo/, "mode") ?iv hb => hb mac -1 msg hb - alg enum(BlockCipherAlgInfo, "alg") + alg enum(BlockCipherAlgInfo/, "alg") key obj - mode enum(BlockCipherModeInfo, "mode") + mode enum(BlockCipherModeInfo/, "mode") iv hb => hb prop -1 - prop enum(BlockCipherPropInfo, "prop") - alg enum(BlockCipherAlgInfo, "alg") + prop enum(BlockCipherPropInfo/, "prop") + alg enum(BlockCipherAlgInfo/, "alg") => int EntryExtra BlockCipherOp diff --git a/crypto/hook.c b/crypto/hook.c index 3e1f87e..002c0d4 100644 --- a/crypto/hook.c +++ b/crypto/hook.c @@ -1 +1,24 @@ - Tcl_RegisterObjType(&blockcipherkey_type); +/* + */ + +#include "chiark_tcl_crypto.h" + +int cht_do_hbcryptotoplevel_hbcrypto(ClientData cd, Tcl_Interp *ip, + const HBCrypto_SubCommand *subcmd, + int objc, Tcl_Obj *const *objv) { + return subcmd->func(0,ip,objc,objv); +} + +extern int Chiark_tcl_crypto_Init(Tcl_Interp *ip); /*called by load(3tcl)*/ +int Chiark_tcl_crypto_Init(Tcl_Interp *ip) { + static int initd; + int rc; + + rc= Chiark_tcl_hbytes_Init(ip); if (rc) return rc; + rc= cht_initextension(ip, cht_hbcryptotoplevel_entries, &initd, + &cht_blockcipherkey_type, + (Tcl_ObjType*)0); + if (rc) return rc; + + return TCL_OK; +} diff --git a/hbytes/Makefile b/hbytes/Makefile index 8551e29..06b4a3a 100644 --- a/hbytes/Makefile +++ b/hbytes/Makefile @@ -1,6 +1,7 @@ BASE_DIR = ../base EXTBASE = hbytes CFILES = chop hbytes hook parse ulongs +OTHER_TCTS = hbytes-base.tct include ../base/extension.make diff --git a/hbytes/hbytes-base.tct b/hbytes/hbytes-base.tct new file mode 100644 index 0000000..97b7ed3 --- /dev/null +++ b/hbytes/hbytes-base.tct @@ -0,0 +1,10 @@ +Type hb: HBytes_Value @ +Init hb cht_hb_sentinel(&@); + +Type hbv: HBytes_Var @ +Init hbv @.hb=0; cht_init_somethingv(&@.sth); +Fini hbv cht_fini_somethingv(ip, rc, &@.sth); + +Type addrmapv: AddrMap_Var @ +Init addrmapv @.am=0; cht_init_somethingv(&@.sth); +Fini addrmapv cht_fini_somethingv(ip, rc, &@.sth); diff --git a/hbytes/hbytes.h b/hbytes/hbytes.h index 4a2f394..b24144b 100644 --- a/hbytes/hbytes.h +++ b/hbytes/hbytes.h @@ -165,6 +165,8 @@ typedef struct { } HBytes_ComplexValue; /* pointed to from internalRep.otherValuePtr */ void memxor(Byte *dest, const Byte *src, int l); +extern int Chiark_tcl_hbytes_Init(Tcl_Interp *ip); + /* called by load(3tcl) and also by extensions which depend on this one */ /* Public interfaces: */ diff --git a/hbytes/hbytes.tct b/hbytes/hbytes.tct index 299d4a9..5611805 100644 --- a/hbytes/hbytes.tct +++ b/hbytes/hbytes.tct @@ -1,13 +1,3 @@ -Type hb: HBytes_Value @ -Init hb cht_hb_sentinel(&@); - -Type hbv: HBytes_Var @ -Init hbv @.hb=0; cht_init_somethingv(&@.sth); -Fini hbv cht_fini_somethingv(ip, rc, &@.sth); - -Type addrmapv: AddrMap_Var @ -Init addrmapv @.am=0; cht_init_somethingv(&@.sth); -Fini addrmapv cht_fini_somethingv(ip, rc, &@.sth); Table *hbytestoplevel TopLevel_Command hbytes diff --git a/hbytes/hook.c b/hbytes/hook.c index 7eeb437..985122e 100644 --- a/hbytes/hook.c +++ b/hbytes/hook.c @@ -310,7 +310,6 @@ int cht_do_hbytestoplevel_ulong(ClientData cd, Tcl_Interp *ip, return subcmd->func(0,ip,objc,objv); } -extern int Chiark_tcl_hbytes_Init(Tcl_Interp *ip); /*called by load(3tcl)*/ int Chiark_tcl_hbytes_Init(Tcl_Interp *ip) { static int initd; -- 2.30.2