2 * crypto - Tcl bindings for parts of the `nettle' crypto library
3 * Copyright 2006-2012 Ian Jackson
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "chiark_tcl_crypto.h"
24 const HashAlgInfo *alg;
28 int cht_do_hbcrypto_hash(ClientData cd, Tcl_Interp *ip, const HashAlgInfo *alg,
29 HBytes_Value message, HBytes_Value *result) {
32 dest= cht_hb_arrayspace(result,alg->hashsize);
33 alg->oneshot(dest, cht_hb_data(&message), cht_hb_len(&message));
37 int cht_do_hbcrypto_hash_init(ClientData cd, Tcl_Interp *ip,
38 const HashAlgInfo *alg, void **state_r) {
39 HashState *state= TALLOC(sizeof(*state) + alg->statesize - 1);
47 int cht_do_hbcrypto_hash_update(ClientData cd, Tcl_Interp *ip,
48 void *state_v, HBytes_Value data) {
49 HashState *state= state_v;
50 state->alg->update(&state->d, cht_hb_data(&data), cht_hb_len(&data));
54 int cht_do_hbcrypto_hash_final(ClientData cd, Tcl_Interp *ip,
55 void *state_v, HBytes_Value *result) {
56 HashState *state= state_v;
59 digest= cht_hb_arrayspace(result,state->alg->hashsize);
60 state->alg->final(&state->d, digest);
61 return cht_do_hbcrypto_hash_discard(cd,ip,state_v);
64 int cht_do_hbcrypto_hash_discard(ClientData cd, Tcl_Interp *ip,
66 cht_tabledataid_disposing(ip,state_v,&cht_hash_states);
71 int cht_do_hbcrypto_hash_clonestate(ClientData cd, Tcl_Interp *ip,
72 void *old_v, void **new_r) {
73 HashState *old= old_v;
74 int len= sizeof(*old) + old->alg->statesize - 1;
75 void *new_v= TALLOC(len);
76 memcpy(new_v, old, len);
77 ((HashState*)new_v)->ix= -1;
83 static void destroy_idtabcb(Tcl_Interp *ip, void *state_v) {
87 const IdDataSpec cht_hash_states= {
88 "hashstate", "hashstate-table", destroy_idtabcb