4 #include "chiark_tcl_crypto.h"
6 const PadOp cht_padop_entries[]= {
16 int pad, blocksize; /* 0 or 1 */
17 } PadMethodClientData;
19 int cht_do_hbcrypto_pad(ClientData cd, Tcl_Interp *ip, const PadOp *op,
20 HBytes_Var v, Tcl_Obj *blocksz, const PadMethodInfo *meth,
21 int methargsc, Tcl_Obj *const *methargsv) {
22 PadMethodClientData pmcd;
25 if (op->use_algname) {
26 const BlockCipherAlgInfo *alg;
27 alg= enum_lookup_cached(ip,blocksz, cht_blockcipheralginfo_entries,
28 "blockcipher alg for pad");
29 if (!alg) return TCL_ERROR;
30 pmcd.blocksize= alg->blocksize;
32 rc= Tcl_GetIntFromObj(ip, blocksz, &pmcd.blocksize); if (rc) return rc;
33 if (pmcd.blocksize < 1) cht_staticerr(ip, "block size must be at least 1", 0);
39 return meth->func(&pmcd,ip,methargsc,methargsv);
42 int cht_do_padmethodinfo_rfc2406(ClientData cd, Tcl_Interp *ip,
43 Tcl_Obj *nxthdr_arg, int *ok) {
44 const PadMethodClientData *pmcd= (const void*)cd;
45 int i, rc, padlen, old_len;
47 if (pmcd->blocksize > 256)
48 return cht_staticerr(ip, "block size too large for RFC2406 padding", 0);
54 rc= cht_pat_hb(ip,nxthdr_arg,&nxthdr);
57 if (cht_hb_len(&nxthdr) != 1) return
58 cht_staticerr(ip, "RFC2406 next header field must be exactly 1 byte", 0);
59 padlen= pmcd->blocksize-1 - ((cht_hb_len(pmcd->hb)+1) % pmcd->blocksize);
60 padding= cht_hb_append(pmcd->hb, padlen+2);
61 for (i=1; i<=padlen; i++)
64 *padding++ = cht_hb_data(&nxthdr)[0];
68 const Byte *padding, *trailer;
70 Tcl_Obj *nxthdr_valobj, *ro;
73 old_len= cht_hb_len(pmcd->hb); if (old_len % pmcd->blocksize) goto quit;
74 trailer= cht_hb_unappend(pmcd->hb, 2); if (!trailer) goto quit;
77 cht_hb_array(&nxthdr,trailer+1,1);
78 nxthdr_valobj= cht_ret_hb(ip,nxthdr);
79 ro= Tcl_ObjSetVar2(ip,nxthdr_arg,0,nxthdr_valobj,TCL_LEAVE_ERR_MSG);
80 if (!ro) { Tcl_DecrRefCount(nxthdr_valobj); return TCL_ERROR; }
82 padding= cht_hb_unappend(pmcd->hb, padlen);
83 for (i=1; i<=padlen; i++)
84 if (*padding++ != i) goto quit;
95 int cht_do_padmethodinfo_pkcs5(ClientData cd, Tcl_Interp *ip, int *ok) {
96 const PadMethodClientData *pmcd= (const void*)cd;
97 int padlen, old_len, i;
99 if (pmcd->blocksize > 255)
100 return cht_staticerr(ip, "block size too large for pkcs#5", 0);
106 padlen= pmcd->blocksize - (cht_hb_len(pmcd->hb) % pmcd->blocksize);
107 padding= cht_hb_append(pmcd->hb, padlen);
108 memset(padding, padlen, padlen);
114 old_len= cht_hb_len(pmcd->hb); if (old_len % pmcd->blocksize) goto bad;
115 padding= cht_hb_unappend(pmcd->hb, 1); if (!padding) goto bad;
117 if (padlen < 1 || padlen > pmcd->blocksize) goto bad;
118 padding= cht_hb_unappend(pmcd->hb, padlen-1); if (!padding) goto bad;
120 for (i=0; i<padlen-1; i++, padding++) if (*padding != padlen) goto bad;
132 int cht_do_hbcrypto_hash(ClientData cd, Tcl_Interp *ip, const HashAlgInfo *alg,
133 HBytes_Value message, HBytes_Value *result) {
136 dest= cht_hb_arrayspace(result,alg->hashsize);
137 alg->oneshot(dest, cht_hb_data(&message), cht_hb_len(&message));
141 #define OBJ_CIPHKEY(o) ((CiphKeyValue*)(o)->internalRep.otherValuePtr)
144 int valuelen, bufferslen;
145 Byte *value, *buffers;
147 void *alpha, *beta; /* key schedules etc.; each may be 0 */
150 static void freealg(CiphKeyValue *key) {
155 static void key_t_free(Tcl_Obj *obj) {
156 CiphKeyValue *key= OBJ_CIPHKEY(obj);
162 static void noalg(CiphKeyValue *key) {
164 key->alpha= key->beta= 0;
167 static void key_t_dup(Tcl_Obj *src_obj, Tcl_Obj *dup_obj) {
168 CiphKeyValue *src= OBJ_CIPHKEY(src_obj);
169 CiphKeyValue *dup= TALLOC(sizeof(*dup));
170 dup->valuelen= src->valuelen;
171 dup->value= src->valuelen ? TALLOC(src->valuelen) : 0;
172 dup->buffers= 0; dup->bufferslen= 0;
173 memcpy(dup->value, src->value, src->valuelen);
175 dup_obj->internalRep.otherValuePtr= dup;
176 dup_obj->typePtr= &cht_blockcipherkey_type;
179 static void key_t_ustr(Tcl_Obj *o) {
180 obj_updatestr_array(o, OBJ_CIPHKEY(o)->value, OBJ_CIPHKEY(o)->valuelen);
183 static int key_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
187 rc= Tcl_ConvertToType(ip,o,&cht_hbytes_type); if (rc) return rc;
188 val= TALLOC(sizeof(*val));
189 val->valuelen= l= cht_hb_len(OBJ_HBYTES(o));
190 val->value= TALLOC(l);
193 memcpy(val->value, cht_hb_data(OBJ_HBYTES(o)), l);
197 o->internalRep.otherValuePtr= val;
198 o->typePtr= &cht_blockcipherkey_type;
203 Tcl_ObjType cht_blockcipherkey_type = {
205 key_t_free, key_t_dup, key_t_ustr, key_t_sfa
208 static CiphKeyValue *get_key(Tcl_Interp *ip, Tcl_Obj *key_obj,
209 const void *alg, int want_bufferslen) {
213 rc= Tcl_ConvertToType(ip,key_obj,&cht_blockcipherkey_type); if (rc) return 0;
214 key= OBJ_CIPHKEY(key_obj);
216 if (key->alg != alg) {
222 if (key->bufferslen < want_bufferslen) {
224 key->buffers= TALLOC(want_bufferslen);
225 key->bufferslen= want_bufferslen;
230 int cht_do_hbcrypto_blockcipher(ClientData cd, Tcl_Interp *ip,
231 const BlockCipherOp *op,
232 int objc, Tcl_Obj *const *objv) {
233 return op->func((void*)op,ip,objc,objv);
236 static int blockcipher_prep(Tcl_Interp *ip, Tcl_Obj *key_obj,
237 const HBytes_Value *iv, int decrypt,
238 const BlockCipherAlgInfo *alg,
239 const BlockCipherModeInfo *mode, int data_len,
240 const CiphKeyValue **key_r, const void **sched_r,
241 const Byte **iv_r, int *iv_lenbytes_r,
242 Byte **buffers_r, int *nblocks_r) {
243 void *sched, **schedp;
244 int want_bufferslen, want_iv;
248 if (data_len % alg->blocksize)
249 return cht_staticerr(ip, "block cipher input not whole number of blocks",
250 "HBYTES BLOCKCIPHER LENGTH");
252 want_bufferslen= alg->blocksize * (mode->buf_blocks + mode->iv_blocks);
253 key= get_key(ip, key_obj, alg, want_bufferslen); if (!key) return TCL_ERROR;
255 schedp= (alg->decrypt.make_schedule==alg->encrypt.make_schedule
256 || !decrypt) ? &key->alpha : &key->beta;
259 if (key->valuelen < alg->key_min)
260 return cht_staticerr(ip, "key too short", "HBYTES BLOCKCIPHER PARAMS");
261 if (key->valuelen > alg->key_max)
262 return cht_staticerr(ip, "key too long", "HBYTES BLOCKCIPHER PARAMS");
264 sched= TALLOC(alg->schedule_size);
265 (decrypt ? &alg->decrypt : &alg->encrypt)->make_schedule
266 (sched, key->value, key->valuelen);
270 want_iv= alg->blocksize * mode->iv_blocks;
272 if (!cht_hb_issentinel(iv))
273 return cht_staticerr(ip,"iv supplied but mode does not take one", 0);
274 } else if (cht_hb_issentinel(iv)) {
275 if (decrypt) return cht_staticerr(ip,"must supply iv when decrypting", 0);
276 rc= cht_get_urandom(ip, key->buffers, want_iv);
279 int iv_supplied= cht_hb_len(iv);
280 if (iv_supplied > want_iv)
281 return cht_staticerr(ip, "iv too large for algorithm and mode",
282 "HBYTES BLOCKCIPHER PARAMS");
283 memcpy(key->buffers, cht_hb_data(iv), iv_supplied);
284 memset(key->buffers + iv_supplied, 0, want_iv - iv_supplied);
291 *iv_lenbytes_r= want_iv;
293 *buffers_r= key->buffers + want_iv;
294 *nblocks_r= data_len / alg->blocksize;
299 int cht_do_blockcipherop_d(ClientData cd, Tcl_Interp *ip,
300 HBytes_Var v, const BlockCipherAlgInfo *alg,
301 Tcl_Obj *key_obj, const BlockCipherModeInfo *mode,
302 HBytes_Value iv, HBytes_Value *result) {
303 return cht_do_blockcipherop_e(cd,ip,v,alg,key_obj,mode,iv,result);
306 int cht_do_blockcipherop_e(ClientData cd, Tcl_Interp *ip,
307 HBytes_Var v, const BlockCipherAlgInfo *alg,
308 Tcl_Obj *key_obj, const BlockCipherModeInfo *mode,
309 HBytes_Value iv, HBytes_Value *result) {
310 const BlockCipherOp *op= (const void*)cd;
311 int encrypt= op->encrypt;
313 const CiphKeyValue *key;
321 return cht_staticerr(ip, "mode does not support encrypt/decrypt", 0);
323 rc= blockcipher_prep(ip,key_obj,&iv,!encrypt,
324 alg,mode, cht_hb_len(v.hb),
331 (encrypt ? mode->encrypt : mode->decrypt)
332 (cht_hb_data(v.hb), nblocks, ivbuf, buffers, alg, encrypt, sched);
335 return cht_staticerr(ip, failure, "HBYTES BLOCKCIPHER CRYPTFAIL CRYPT");
337 cht_hb_array(result, ivbuf, iv_lenbytes);
342 int cht_do_blockcipherop_mac(ClientData cd, Tcl_Interp *ip,
343 HBytes_Value msg, const BlockCipherAlgInfo *alg,
344 Tcl_Obj *key_obj, const BlockCipherModeInfo *mode,
345 HBytes_Value iv, HBytes_Value *result) {
346 const CiphKeyValue *key;
351 int nblocks, iv_lenbytes;
355 return cht_staticerr(ip, "mode does not support mac generation", 0);
357 rc= blockcipher_prep(ip,key_obj,&iv,0,
358 alg,mode, cht_hb_len(&msg),
364 failure= mode->mac(cht_hb_data(&msg), nblocks, ivbuf, buffers, alg, sched);
366 return cht_staticerr(ip,failure, "HBYTES BLOCKCIPHER CRYPTFAIL MAC");
368 cht_hb_array(result, buffers, alg->blocksize * mode->mac_blocks);
373 int cht_do_hbcrypto_hmac(ClientData cd, Tcl_Interp *ip, const HashAlgInfo *alg,
374 HBytes_Value message, Tcl_Obj *key_obj,
375 Tcl_Obj *maclen_obj, HBytes_Value *result) {
376 /* key->alpha = state after H(K XOR ipad <unfinished>
377 * key->beta = state after H(K XOR opad <unfinished>
378 * key->buffers = room for one block, or one state
385 rc= Tcl_GetIntFromObj(ip, maclen_obj, &ml); if (rc) return rc;
386 if (ml<0 || ml>alg->hashsize)
387 return cht_staticerr(ip, "requested hmac output size out of range",
388 "HBYTES HMAC PARAMS");
393 key= get_key(ip, key_obj, alg,
394 alg->blocksize > alg->statesize
395 ? alg->blocksize : alg->statesize);
400 if (key->valuelen > alg->blocksize)
401 return cht_staticerr(ip, "key to hmac longer than hash block size",
402 "HBYTES HMAC PARAMS");
404 memcpy(key->buffers, key->value, key->valuelen);
405 memset(key->buffers + key->valuelen, 0, alg->blocksize - key->valuelen);
406 for (i=0; i<alg->blocksize; i++) key->buffers[i] ^= 0x36;
408 key->alpha= TALLOC(alg->statesize);
409 alg->init(key->alpha);
410 alg->update(key->alpha, key->buffers, alg->blocksize);
412 key->beta= TALLOC(alg->statesize);
413 alg->init(key->beta);
414 for (i=0; i<alg->blocksize; i++) key->buffers[i] ^= (0x5c ^ 0x36);
415 alg->update(key->beta, key->buffers, alg->blocksize);
419 dest= cht_hb_arrayspace(result, alg->hashsize);
421 memcpy(key->buffers, key->alpha, alg->statesize);
422 alg->update(key->buffers, cht_hb_data(&message), cht_hb_len(&message));
423 alg->final(key->buffers, dest);
425 memcpy(key->buffers, key->beta, alg->statesize);
426 alg->update(key->buffers, dest, alg->hashsize);
427 alg->final(key->buffers, dest);
429 cht_hb_unappend(result, alg->hashsize - ml);
434 int cht_do_blockcipherop_prop(ClientData cd, Tcl_Interp *ip,
435 const BlockCipherPropInfo *prop,
436 const BlockCipherAlgInfo *alg, int *result) {
437 *result= *(const int*)((const char*)alg + prop->int_offset);
441 int cht_do_hbcrypto_hash_prop(ClientData cd, Tcl_Interp *ip,
442 const HashAlgPropInfo *prop,
443 const HashAlgInfo *alg, int *result) {
444 *result= *(const int*)((const char*)alg + prop->int_offset);