X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-tcl.git;a=blobdiff_plain;f=crypto%2Fcrypto.c;h=cf50712979a9beb73d6e2f1477070c164d9a0657;hp=457dc9e37f242178a55b13acea3732199b7d8546;hb=82f88c53ddb84e42c770c23feb9bb0ee18341188;hpb=5d466de467f28ae6f7125bef086d141a7734a4ce diff --git a/crypto/crypto.c b/crypto/crypto.c index 457dc9e..cf50712 100644 --- a/crypto/crypto.c +++ b/crypto/crypto.c @@ -5,13 +5,8 @@ #include "hbytes.h" #include "tables.h" -#include "serpent.h" -void memxor(Byte *dest, const Byte *src, int l) { - while (l--) *dest++ ^= *src++; -} - -const PadMethod padmethods[]= { +const PadOp padops[]= { { "un", 0, 0 }, { "ua", 0, 1 }, { "pn", 1, 0 }, @@ -19,35 +14,114 @@ const PadMethod padmethods[]= { { 0 } }; -int do_hbytes_pkcs5(ClientData cd, Tcl_Interp *ip, - const PadMethod *meth, HBytes_Var v, Tcl_Obj *block, - int *ok) { - int rc, blocksize, padlen, old_len, i; - Byte *padding; - const Byte *unpad; +typedef struct { + HBytes_Value *hb; + int pad, blocksize; /* 0 or 1 */ +} PadMethodClientData; + +int do_hbytes_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; + int rc; - if (meth->use_algname) { + if (op->use_algname) { const BlockCipherAlgInfo *alg; - alg= enum_lookup_cached(ip,block,blockcipheralginfos,"cipher alg for pad"); + alg= enum_lookup_cached(ip,blocksz,blockcipheralginfos, + "blockcipher alg for pad"); if (!alg) return TCL_ERROR; - blocksize= alg->blocksize; + pmcd.blocksize= alg->blocksize; } else { - rc= Tcl_GetIntFromObj(ip, block, &blocksize); if (rc) return rc; - if (blocksize < 1 || blocksize > 255) - return staticerr(ip, "block size out of pkcs#5 range 1..255"); + 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); + } + + pmcd.hb= v.hb; + pmcd.pad= op->pad; + + return meth->func(&pmcd,ip,methargsc,methargsv); +} + +int 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); + + if (pmcd->pad) { + Byte *padding; + HBytes_Value nxthdr; + + rc= 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); + for (i=1; i<=padlen; i++) + *padding++ = i; + *padding++ = padlen; + *padding++ = hbytes_data(&nxthdr)[0]; + *ok= 1; + + } else { + const Byte *padding, *trailer; + HBytes_Value nxthdr; + 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; + + padlen= trailer[0]; + hbytes_array(&nxthdr,trailer+1,1); + nxthdr_valobj= 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); + for (i=1; i<=padlen; i++) + if (*padding++ != i) goto quit; + + *ok= 1; + + quit:; + } - if (meth->pad) { - padlen= blocksize - (hbytes_len(v.hb) % blocksize); - padding= hbytes_append(v.hb, padlen); + return TCL_OK; +} + +int 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); + + if (pmcd->pad) { + + Byte *padding; + + padlen= pmcd->blocksize - (hbytes_len(pmcd->hb) % pmcd->blocksize); + padding= hbytes_append(pmcd->hb, padlen); memset(padding, padlen, padlen); + } else { - old_len= hbytes_len(v.hb); if (old_len % blocksize) goto bad; - unpad= hbytes_unappend(v.hb, 1); if (!unpad) goto bad; - padlen= *unpad; - if (padlen < 1 || padlen > blocksize) goto bad; - unpad= hbytes_unappend(v.hb, padlen-1); if (!unpad) goto bad; - for (i=0; ihb); if (old_len % pmcd->blocksize) goto bad; + padding= hbytes_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; + + for (i=0; iblocksize) - return staticerr(ip, "block cipher input not whole number of blocks"); + return staticerr(ip, "block cipher input not whole number of blocks", + "HBYTES BLOCKCIPHER LENGTH"); want_bufferslen= alg->blocksize * (mode->buf_blocks + mode->iv_blocks); key= get_key(ip, key_obj, alg, want_bufferslen); if (!key) return TCL_ERROR; @@ -184,8 +259,10 @@ static int blockcipher_prep(Tcl_Interp *ip, Tcl_Obj *key_obj, || !decrypt) ? &key->alpha : &key->beta; sched= *schedp; if (!sched) { - if (key->valuelen < alg->key_min) return staticerr(ip, "key too short"); - if (key->valuelen > alg->key_max) return staticerr(ip, "key too long"); + if (key->valuelen < alg->key_min) + return staticerr(ip, "key too short", "HBYTES BLOCKCIPHER PARAMS"); + if (key->valuelen > alg->key_max) + return staticerr(ip, "key too long", "HBYTES BLOCKCIPHER PARAMS"); sched= TALLOC(alg->schedule_size); (decrypt ? &alg->decrypt : &alg->encrypt)->make_schedule @@ -196,15 +273,16 @@ 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"); + 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"); + if (decrypt) return staticerr(ip,"must supply iv when decrypting", 0); rc= get_urandom(ip, key->buffers, want_iv); if (rc) return rc; } else { int iv_supplied= hbytes_len(iv); if (iv_supplied > want_iv) - return staticerr(ip, "iv too large for algorithm and mode"); + return staticerr(ip, "iv too large for algorithm and mode", + "HBYTES BLOCKCIPHER PARAMS"); memcpy(key->buffers, hbytes_data(iv), iv_supplied); memset(key->buffers + iv_supplied, 0, want_iv - iv_supplied); } @@ -243,7 +321,7 @@ int do_blockcipherop_e(ClientData cd, Tcl_Interp *ip, int nblocks; if (!mode->encrypt) - return staticerr(ip, "mode does not support encrypt/decrypt"); + return staticerr(ip, "mode does not support encrypt/decrypt", 0); rc= blockcipher_prep(ip,key_obj,&iv,!encrypt, alg,mode, hbytes_len(v.hb), @@ -257,7 +335,7 @@ int do_blockcipherop_e(ClientData cd, Tcl_Interp *ip, (hbytes_data(v.hb), nblocks, ivbuf, buffers, alg, encrypt, sched); if (failure) - return staticerr(ip, failure); + return staticerr(ip, failure, "HBYTES BLOCKCIPHER CRYPTFAIL CRYPT"); hbytes_array(result, ivbuf, iv_lenbytes); @@ -277,7 +355,7 @@ int do_blockcipherop_mac(ClientData cd, Tcl_Interp *ip, int rc; if (!mode->mac) - return staticerr(ip, "mode does not support mac generation"); + return staticerr(ip, "mode does not support mac generation", 0); rc= blockcipher_prep(ip,key_obj,&iv,0, alg,mode, hbytes_len(&msg), @@ -288,19 +366,13 @@ int do_blockcipherop_mac(ClientData cd, Tcl_Interp *ip, failure= mode->mac(hbytes_data(&msg), nblocks, ivbuf, buffers, alg, sched); if (failure) - return staticerr(ip,failure); + return staticerr(ip,failure, "HBYTES BLOCKCIPHER CRYPTFAIL MAC"); hbytes_array(result, buffers, alg->blocksize * mode->mac_blocks); return TCL_OK; } -static void dbuf(const char *m, const Byte *a, int l) { - fprintf(stderr,"dbuf %s l=%d ",m,l); - while (l-->0) fprintf(stderr,"%02x",*a++); - putc('\n',stderr); -} - int do_hbytes_hmac(ClientData cd, Tcl_Interp *ip, const HashAlgInfo *alg, HBytes_Value message, Tcl_Obj *key_obj, Tcl_Obj *maclen_obj, HBytes_Value *result) { @@ -315,7 +387,8 @@ 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 staticerr(ip, "requested hmac output size out of range", + "HBYTES HMAC PARAMS"); } else { ml= alg->hashsize; } @@ -328,23 +401,21 @@ 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 staticerr(ip, "key to hmac longer than hash block size", + "HBYTES HMAC PARAMS"); -dbuf("start key",key->value,key->valuelen); memcpy(key->buffers, key->value, key->valuelen); memset(key->buffers + key->valuelen, 0, alg->blocksize - key->valuelen); for (i=0; iblocksize; i++) key->buffers[i] ^= 0x36; key->alpha= TALLOC(alg->statesize); alg->init(key->alpha); -dbuf("inner key",key->buffers,alg->blocksize); alg->update(key->alpha, key->buffers, alg->blocksize); key->beta= TALLOC(alg->statesize); alg->init(key->beta); for (i=0; iblocksize; i++) key->buffers[i] ^= (0x5c ^ 0x36); alg->update(key->beta, key->buffers, alg->blocksize); -dbuf("inner key",key->buffers,alg->blocksize); } assert(key->beta); @@ -353,14 +424,26 @@ dbuf("inner key",key->buffers,alg->blocksize); memcpy(key->buffers, key->alpha, alg->statesize); alg->update(key->buffers, hbytes_data(&message), hbytes_len(&message)); alg->final(key->buffers, dest); -dbuf("inner hash",dest,alg->hashsize); memcpy(key->buffers, key->beta, alg->statesize); alg->update(key->buffers, dest, alg->hashsize); alg->final(key->buffers, dest); -dbuf("outer hash",dest,alg->hashsize); hbytes_unappend(result, alg->hashsize - ml); 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; +}