chiark / gitweb /
hmac seems to work
[chiark-tcl.git] / crypto / algtables.c
1 /*
2  */
3
4 #include <stdint.h>
5 #include <netinet/in.h>
6
7 #include "hbytes.h"
8 #include "serpent.h"
9 #include "sha1.h"
10 #include "md5.h"
11
12 static void alg_serpent_byteswap_block(Byte *b) {
13   uint32_t t, *a= (void*)b;
14
15   t=    htonl(a[0]);
16   a[0]= htonl(a[3]);
17   a[3]= t;
18
19   t=    htonl(a[1]);
20   a[1]= htonl(a[2]);
21   a[2]= t;
22 }
23
24 static void alg_serpent_makekey(void *schedule, const Byte *key, int keylen) {
25   serpent_makekey(schedule, keylen*8, key);
26 }
27
28 static void alg_serpent_encrypt(const void *sch, const void *in, void *out) {
29   serpent_encrypt(sch, in, out);
30 }
31   
32 static void alg_serpent_decrypt(const void *sch, const void *in, void *out) {
33   serpent_decrypt(sch, in, out);
34 }
35
36 const BlockCipherAlgInfo blockcipheralginfos[]= {
37   { "serpent", 16, sizeof(struct SerpentKeyInstance), 16,32,
38     alg_serpent_byteswap_block,
39     { alg_serpent_makekey, alg_serpent_encrypt },
40     { alg_serpent_makekey, alg_serpent_decrypt } },
41   { 0 }
42 };
43
44 static void alg_sha1_init(void *state) { sha1_init(state); }
45 static void alg_sha1_update(void *state, const Byte *data, int len) {
46   sha1_update(state, data, len);
47 }
48 static void alg_sha1_final(void *state, Byte *digest) {
49   sha1_final(state, digest);
50 }
51 static void alg_sha1_oneshot(Byte *digest, const Byte *data, int len) {
52   sha1(data,len,digest);
53 }
54
55 static void alg_md5_init(void *state) { MD5Init(state); }
56 static void alg_md5_update(void *state, const Byte *data, int len) {
57   MD5Update(state, data, len);
58 }
59 static void alg_md5_final(void *state, Byte *digest) {
60   MD5Final(digest, state);
61 }
62 static void alg_md5_oneshot(Byte *digest, const Byte *data, int len) {
63   struct MD5Context ctx;
64   MD5Init(&ctx);
65   MD5Update(&ctx,data,len);
66   MD5Final(digest,&ctx);
67 }
68
69 const HashAlgInfo hashalginfos[]= {
70   { "sha1", 20, 64, sizeof(struct sha1_state),
71     alg_sha1_init, alg_sha1_update, alg_sha1_final, alg_sha1_oneshot },
72   { "md5", 16, 64, sizeof(struct MD5Context),
73     alg_md5_init, alg_md5_update, alg_md5_final, alg_md5_oneshot },
74   { 0 }
75 };