10 void memxor(Byte *dest, const Byte *src, int l) {
11 while (l--) *dest++ ^= *src++;
14 const PadMethod padmethods[]= {
22 int do_hbytes_pkcs5(ClientData cd, Tcl_Interp *ip,
23 const PadMethod *meth, HBytes_Var v, Tcl_Obj *block,
25 int rc, blocksize, padlen, old_len, i;
29 if (meth->use_algname) {
30 const BlockCipherAlgInfo *alg;
31 alg= enum_lookup_cached(ip,block,blockcipheralginfos,"cipher alg for pad");
32 if (!alg) return TCL_ERROR;
33 blocksize= alg->blocksize;
35 rc= Tcl_GetIntFromObj(ip, block, &blocksize); if (rc) return rc;
36 if (blocksize < 1 || blocksize > 255)
37 return staticerr(ip, "block size out of pkcs#5 range 1..255", 0);
41 padlen= blocksize - (hbytes_len(v.hb) % blocksize);
42 padding= hbytes_append(v.hb, padlen);
43 memset(padding, padlen, padlen);
45 old_len= hbytes_len(v.hb); if (old_len % blocksize) goto bad;
46 unpad= hbytes_unappend(v.hb, 1); if (!unpad) goto bad;
48 if (padlen < 1 || padlen > blocksize) goto bad;
49 unpad= hbytes_unappend(v.hb, padlen-1); if (!unpad) goto bad;
50 for (i=0; i<padlen-1; i++, unpad++) if (*unpad != padlen) goto bad;
61 int do_hbytes_hash(ClientData cd, Tcl_Interp *ip, const HashAlgInfo *alg,
62 HBytes_Value message, HBytes_Value *result) {
65 dest= hbytes_arrayspace(result,alg->hashsize);
66 alg->oneshot(dest, hbytes_data(&message), hbytes_len(&message));
70 #define OBJ_CIPHKEY(o) ((CiphKeyValue*)(o)->internalRep.otherValuePtr)
73 int valuelen, bufferslen;
74 Byte *value, *buffers;
76 void *alpha, *beta; /* key schedules etc.; each may be 0 */
79 static void freealg(CiphKeyValue *key) {
84 static void key_t_free(Tcl_Obj *obj) {
85 CiphKeyValue *key= OBJ_CIPHKEY(obj);
91 static void noalg(CiphKeyValue *key) {
93 key->alpha= key->beta= 0;
96 static void key_t_dup(Tcl_Obj *src_obj, Tcl_Obj *dup_obj) {
97 CiphKeyValue *src= OBJ_CIPHKEY(src_obj);
98 CiphKeyValue *dup= TALLOC(sizeof(*dup));
99 dup->valuelen= src->valuelen;
100 dup->value= src->valuelen ? TALLOC(src->valuelen) : 0;
101 dup->buffers= 0; dup->bufferslen= 0;
102 memcpy(dup->value, src->value, src->valuelen);
104 dup_obj->internalRep.otherValuePtr= dup;
105 dup_obj->typePtr= &blockcipherkey_type;
108 static void key_t_ustr(Tcl_Obj *o) {
109 obj_updatestr_array(o, OBJ_CIPHKEY(o)->value, OBJ_CIPHKEY(o)->valuelen);
112 static int key_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
116 rc= Tcl_ConvertToType(ip,o,&hbytes_type); if (rc) return rc;
117 val= TALLOC(sizeof(*val));
118 val->valuelen= l= hbytes_len(OBJ_HBYTES(o));
119 val->value= TALLOC(l);
122 memcpy(val->value, hbytes_data(OBJ_HBYTES(o)), l);
126 o->internalRep.otherValuePtr= val;
127 o->typePtr= &blockcipherkey_type;
132 Tcl_ObjType blockcipherkey_type = {
134 key_t_free, key_t_dup, key_t_ustr, key_t_sfa
137 static CiphKeyValue *get_key(Tcl_Interp *ip, Tcl_Obj *key_obj,
138 const void *alg, int want_bufferslen) {
142 rc= Tcl_ConvertToType(ip,key_obj,&blockcipherkey_type); if (rc) return 0;
143 key= OBJ_CIPHKEY(key_obj);
145 if (key->alg != alg) {
151 if (key->bufferslen < want_bufferslen) {
153 key->buffers= TALLOC(want_bufferslen);
154 key->bufferslen= want_bufferslen;
159 int do_hbytes_blockcipher(ClientData cd, Tcl_Interp *ip,
160 const BlockCipherOp *op,
161 int objc, Tcl_Obj *const *objv) {
162 return op->func((void*)op,ip,objc,objv);
165 static int blockcipher_prep(Tcl_Interp *ip, Tcl_Obj *key_obj,
166 const HBytes_Value *iv, int decrypt,
167 const BlockCipherAlgInfo *alg,
168 const BlockCipherModeInfo *mode, int data_len,
169 const CiphKeyValue **key_r, const void **sched_r,
170 const Byte **iv_r, int *iv_lenbytes_r,
171 Byte **buffers_r, int *nblocks_r) {
172 void *sched, **schedp;
173 int want_bufferslen, want_iv;
177 if (data_len % alg->blocksize)
178 return staticerr(ip, "block cipher input not whole number of blocks",
179 "HBYTES BLOCKCIPHER LENGTH");
181 want_bufferslen= alg->blocksize * (mode->buf_blocks + mode->iv_blocks);
182 key= get_key(ip, key_obj, alg, want_bufferslen); if (!key) return TCL_ERROR;
184 schedp= (alg->decrypt.make_schedule==alg->encrypt.make_schedule
185 || !decrypt) ? &key->alpha : &key->beta;
188 if (key->valuelen < alg->key_min)
189 return staticerr(ip, "key too short", "HBYTES BLOCKCIPHER PARAMS");
190 if (key->valuelen > alg->key_max)
191 return staticerr(ip, "key too long", "HBYTES BLOCKCIPHER PARAMS");
193 sched= TALLOC(alg->schedule_size);
194 (decrypt ? &alg->decrypt : &alg->encrypt)->make_schedule
195 (sched, key->value, key->valuelen);
199 want_iv= alg->blocksize * mode->iv_blocks;
201 if (!hbytes_issentinel(iv))
202 return staticerr(ip,"iv supplied but mode does not take one", 0);
203 } else if (hbytes_issentinel(iv)) {
204 if (decrypt) return staticerr(ip,"must supply iv when decrypting", 0);
205 rc= get_urandom(ip, key->buffers, want_iv);
208 int iv_supplied= hbytes_len(iv);
209 if (iv_supplied > want_iv)
210 return staticerr(ip, "iv too large for algorithm and mode",
211 "HBYTES BLOCKCIPHER PARAMS");
212 memcpy(key->buffers, hbytes_data(iv), iv_supplied);
213 memset(key->buffers + iv_supplied, 0, want_iv - iv_supplied);
220 *iv_lenbytes_r= want_iv;
222 *buffers_r= key->buffers + want_iv;
223 *nblocks_r= data_len / alg->blocksize;
228 int do_blockcipherop_d(ClientData cd, Tcl_Interp *ip,
229 HBytes_Var v, const BlockCipherAlgInfo *alg,
230 Tcl_Obj *key_obj, const BlockCipherModeInfo *mode,
231 HBytes_Value iv, HBytes_Value *result) {
232 return do_blockcipherop_e(cd,ip,v,alg,key_obj,mode,iv,result);
235 int do_blockcipherop_e(ClientData cd, Tcl_Interp *ip,
236 HBytes_Var v, const BlockCipherAlgInfo *alg,
237 Tcl_Obj *key_obj, const BlockCipherModeInfo *mode,
238 HBytes_Value iv, HBytes_Value *result) {
239 const BlockCipherOp *op= (const void*)cd;
240 int encrypt= op->encrypt;
242 const CiphKeyValue *key;
250 return staticerr(ip, "mode does not support encrypt/decrypt", 0);
252 rc= blockcipher_prep(ip,key_obj,&iv,!encrypt,
253 alg,mode, hbytes_len(v.hb),
260 (encrypt ? mode->encrypt : mode->decrypt)
261 (hbytes_data(v.hb), nblocks, ivbuf, buffers, alg, encrypt, sched);
264 return staticerr(ip, failure, "HBYTES BLOCKCIPHER CRYPTFAIL CRYPT");
266 hbytes_array(result, ivbuf, iv_lenbytes);
271 int do_blockcipherop_mac(ClientData cd, Tcl_Interp *ip,
272 HBytes_Value msg, const BlockCipherAlgInfo *alg,
273 Tcl_Obj *key_obj, const BlockCipherModeInfo *mode,
274 HBytes_Value iv, HBytes_Value *result) {
275 const CiphKeyValue *key;
280 int nblocks, iv_lenbytes;
284 return staticerr(ip, "mode does not support mac generation", 0);
286 rc= blockcipher_prep(ip,key_obj,&iv,0,
287 alg,mode, hbytes_len(&msg),
293 failure= mode->mac(hbytes_data(&msg), nblocks, ivbuf, buffers, alg, sched);
295 return staticerr(ip,failure, "HBYTES BLOCKCIPHER CRYPTFAIL MAC");
297 hbytes_array(result, buffers, alg->blocksize * mode->mac_blocks);
302 int do_hbytes_hmac(ClientData cd, Tcl_Interp *ip, const HashAlgInfo *alg,
303 HBytes_Value message, Tcl_Obj *key_obj,
304 Tcl_Obj *maclen_obj, HBytes_Value *result) {
305 /* key->alpha = state after H(K XOR ipad <unfinished>
306 * key->beta = state after H(K XOR opad <unfinished>
307 * key->buffers = room for one block, or one state
314 rc= Tcl_GetIntFromObj(ip, maclen_obj, &ml); if (rc) return rc;
315 if (ml<0 || ml>alg->hashsize)
316 return staticerr(ip, "requested hmac output size out of range",
317 "HBYTES HMAC PARAMS");
322 key= get_key(ip, key_obj, alg,
323 alg->blocksize > alg->statesize
324 ? alg->blocksize : alg->statesize);
329 if (key->valuelen > alg->blocksize)
330 return staticerr(ip, "key to hmac longer than hash block size",
331 "HBYTES HMAC PARAMS");
333 memcpy(key->buffers, key->value, key->valuelen);
334 memset(key->buffers + key->valuelen, 0, alg->blocksize - key->valuelen);
335 for (i=0; i<alg->blocksize; i++) key->buffers[i] ^= 0x36;
337 key->alpha= TALLOC(alg->statesize);
338 alg->init(key->alpha);
339 alg->update(key->alpha, key->buffers, alg->blocksize);
341 key->beta= TALLOC(alg->statesize);
342 alg->init(key->beta);
343 for (i=0; i<alg->blocksize; i++) key->buffers[i] ^= (0x5c ^ 0x36);
344 alg->update(key->beta, key->buffers, alg->blocksize);
348 dest= hbytes_arrayspace(result, alg->hashsize);
350 memcpy(key->buffers, key->alpha, alg->statesize);
351 alg->update(key->buffers, hbytes_data(&message), hbytes_len(&message));
352 alg->final(key->buffers, dest);
354 memcpy(key->buffers, key->beta, alg->statesize);
355 alg->update(key->buffers, dest, alg->hashsize);
356 alg->final(key->buffers, dest);
358 hbytes_unappend(result, alg->hashsize - ml);
363 int do_blockcipherop_prop(ClientData cd, Tcl_Interp *ip,
364 const BlockCipherPropInfo *prop,
365 const BlockCipherAlgInfo *alg, int *result) {
366 *result= *(const int*)((const char*)alg + prop->int_offset);
370 int do_hbytes_hash_prop(ClientData cd, Tcl_Interp *ip,
371 const HashAlgPropInfo *prop,
372 const HashAlgInfo *alg, int *result) {
373 *result= *(const int*)((const char*)alg + prop->int_offset);