--- /dev/null
+/*
+ */
+
+#include <stdint.h>
+#include <netinet/in.h>
+
+#include "hbytes.h"
+#include "serpent.h"
+#include "sha1.h"
+#include "md5.h"
+
+static void alg_serpent_byteswap_block(Byte *b) {
+ uint32_t t, *a= (void*)b;
+
+ t= htonl(a[0]);
+ a[0]= htonl(a[3]);
+ a[3]= t;
+
+ t= htonl(a[1]);
+ a[1]= htonl(a[2]);
+ a[2]= t;
+}
+
+static void alg_serpent_makekey(void *schedule, const Byte *key, int keylen) {
+ serpent_makekey(schedule, keylen*8, key);
+}
+
+static void alg_serpent_encrypt(const void *sch, const void *in, void *out) {
+ serpent_encrypt(sch, in, out);
+}
+
+static void alg_serpent_decrypt(const void *sch, const void *in, void *out) {
+ serpent_decrypt(sch, in, out);
+}
+
+const BlockCipherAlgInfo blockcipheralginfos[]= {
+ { "serpent", 16, sizeof(struct SerpentKeyInstance), 16,32,
+ alg_serpent_byteswap_block,
+ { alg_serpent_makekey, alg_serpent_encrypt },
+ { alg_serpent_makekey, alg_serpent_decrypt } },
+ { 0 }
+};
+
+static void alg_sha1_init(void *state) { sha1_init(state); }
+static void alg_sha1_update(void *state, const Byte *data, int len) {
+ sha1_update(state, data, len);
+}
+static void alg_sha1_final(void *state, Byte *digest) {
+ sha1_final(state, digest);
+}
+static void alg_sha1_oneshot(Byte *digest, const Byte *data, int len) {
+ sha1(data,len,digest);
+}
+
+static void alg_md5_init(void *state) { MD5Init(state); }
+static void alg_md5_update(void *state, const Byte *data, int len) {
+ MD5Update(state, data, len);
+}
+static void alg_md5_final(void *state, Byte *digest) {
+ MD5Final(digest, state);
+}
+static void alg_md5_oneshot(Byte *digest, const Byte *data, int len) {
+ struct MD5Context ctx;
+ MD5Init(&ctx);
+ MD5Update(&ctx,data,len);
+ MD5Final(digest,&ctx);
+}
+
+const HashAlgInfo hashalginfos[]= {
+ { "sha1", 20, 64, sizeof(struct sha1_state),
+ alg_sha1_init, alg_sha1_update, alg_sha1_final, alg_sha1_oneshot },
+ { "md5", 16, 64, sizeof(struct MD5Context),
+ alg_md5_init, alg_md5_update, alg_md5_final, alg_md5_oneshot },
+ { 0 }
+};
return TCL_OK;
}
+int do_hbytes_hash(ClientData cd, Tcl_Interp *ip, const HashAlgInfo *alg,
+ HBytes_Value message, HBytes_Value *result) {
+ Byte *dest;
+
+ dest= hbytes_arrayspace(result,alg->hashsize);
+ alg->oneshot(dest, hbytes_data(&message), hbytes_len(&message));
+ return TCL_OK;
+}
+
#define OBJ_CIPHKEY(o) ((CiphKeyValue*)(o)->internalRep.otherValuePtr)
typedef struct {
int valuelen, bufferslen;
Byte *value, *buffers;
- const BlockCipherAlgInfo *alg;
- void *encrypt, *decrypt; /* key schedules; each may be 0 */
+ const void *alg;
+ void *alpha, *beta; /* key schedules etc.; each may be 0 */
} CiphKeyValue;
static void freealg(CiphKeyValue *key) {
- TFREE(key->encrypt);
- TFREE(key->decrypt);
+ TFREE(key->alpha);
+ TFREE(key->beta);
}
static void key_t_free(Tcl_Obj *obj) {
static void noalg(CiphKeyValue *key) {
key->alg= 0;
- key->encrypt= key->decrypt= 0;
+ key->alpha= key->beta= 0;
}
static void key_t_dup(Tcl_Obj *src_obj, Tcl_Obj *dup_obj) {
key_t_free, key_t_dup, key_t_ustr, key_t_sfa
};
-int do_hbytes_blockciph(ClientData cd, Tcl_Interp *ip, int encrypt,
- HBytes_Var v, const BlockCipherAlgInfo *alg,
- Tcl_Obj *key_obj, const BlockCipherModeInfo *mode,
- HBytes_Value iv, HBytes_Value *result) {
- int rc, want_bufferslen, data_len, iv_want;
+CiphKeyValue *get_key(Tcl_Interp *ip, Tcl_Obj *key_obj,
+ const void *alg, int want_bufferslen) {
CiphKeyValue *key;
- const char *failure;
- void *sched, **schedp;
-
- rc= Tcl_ConvertToType(ip,key_obj,&blockcipherkey_type); if (rc) return rc;
+ int rc;
+
+ rc= Tcl_ConvertToType(ip,key_obj,&blockcipherkey_type); if (rc) return 0;
key= OBJ_CIPHKEY(key_obj);
if (key->alg != alg) {
freealg(key);
noalg(key);
-
- if (key->valuelen < alg->key_min) return staticerr(ip, "key too short");
- if (key->valuelen > alg->key_max) return staticerr(ip, "key too long");
key->alg= alg;
}
+
+ if (key->bufferslen < want_bufferslen) {
+ TFREE(key->buffers);
+ key->buffers= TALLOC(want_bufferslen);
+ key->bufferslen= want_bufferslen;
+ }
+ return key;
+}
+
+int do_hbytes_blockcipher(ClientData cd, Tcl_Interp *ip, int encrypt,
+ HBytes_Var v, const BlockCipherAlgInfo *alg,
+ Tcl_Obj *key_obj, const BlockCipherModeInfo *mode,
+ HBytes_Value iv, HBytes_Value *result) {
+ int rc, want_bufferslen, data_len, iv_want;
+ CiphKeyValue *key;
+ const char *failure;
+ void *sched, **schedp;
- schedp= (alg->decrypt.make_schedule!=alg->encrypt.make_schedule
- && !encrypt) ? &key->decrypt : &key->encrypt;
+ want_bufferslen= alg->blocksize * (mode->buf_blocks + mode->iv_blocks);
+ key= get_key(ip, key_obj, alg, want_bufferslen);
+
+ schedp= (alg->decrypt.make_schedule==alg->encrypt.make_schedule
+ || encrypt) ? &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");
+
sched= TALLOC(alg->schedule_size);
(encrypt ? &alg->encrypt : &alg->decrypt)->make_schedule
(sched, key->value, key->valuelen);
*schedp= sched;
}
-
- want_bufferslen= alg->blocksize * (mode->buf_blocks + mode->iv_blocks);
- if (key->bufferslen < want_bufferslen) {
- TFREE(key->buffers);
- key->buffers= TALLOC(want_bufferslen);
- key->bufferslen= want_bufferslen;
- }
iv_want= alg->blocksize * mode->iv_blocks;
if (hbytes_issentinel(&iv)) {
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) {
+ /* key->alpha = state after H(K XOR ipad <unfinished>
+ * key->beta = state after H(K XOR opad <unfinished>
+ * key->buffers = room for one block, or one state
+ */
+ CiphKeyValue *key;
+ Byte *dest;
+ int i, ml, rc;
+
+ 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");
+ } else {
+ ml= alg->hashsize;
+ }
+
+ key= get_key(ip, key_obj, alg,
+ alg->blocksize > alg->statesize
+ ? alg->blocksize : alg->statesize);
+
+ if (!key->alpha) {
+ assert(!key->beta);
+
+ if (key->valuelen > alg->blocksize)
+ return staticerr(ip, "key to hmac longer than hash block size");
+
+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; i<alg->blocksize; 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; i<alg->blocksize; i++) key->buffers[i] ^= (0x5c ^ 0x36);
+ alg->update(key->beta, key->buffers, alg->blocksize);
+dbuf("inner key",key->buffers,alg->blocksize);
+ }
+ assert(key->beta);
+
+ dest= hbytes_arrayspace(result, alg->hashsize);
+
+ 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;
+}