chiark / gitweb /
hbytes range
[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_b_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_l_byteswap_block(Byte *b) {
25   uint32_t t, *a= (void*)b;
26   int i;
27
28   if (htonl(0x01020304UL) == 0x04030201UL) /* little endian */
29     return;
30   
31   for (i=0; i<4; i++) {
32     t= htonl(a[i]);
33     a[i]= ((t & 0x000000ffUL) << 24 |
34            (t & 0x0000ff00UL) << 8 |
35            (t & 0x00ff0000UL) >> 8 |
36            (t & 0xff000000UL) >> 24);
37   }
38 }
39
40 static void alg_serpent_makekey(void *schedule, const Byte *key, int keylen) {
41   serpent_makekey(schedule, keylen*8, key);
42 }
43
44 static void alg_serpent_encrypt(const void *sch, const void *in, void *out) {
45   serpent_encrypt(sch, in, out);
46 }
47   
48 static void alg_serpent_decrypt(const void *sch, const void *in, void *out) {
49   serpent_decrypt(sch, in, out);
50 }
51
52 const BlockCipherAlgInfo blockcipheralginfos[]= {
53   { "serpent-l", 16, sizeof(struct SerpentKeyInstance), 16,32,
54     alg_serpent_l_byteswap_block,
55     { alg_serpent_makekey, alg_serpent_encrypt },
56     { alg_serpent_makekey, alg_serpent_decrypt } },
57   { "serpent-b", 16, sizeof(struct SerpentKeyInstance), 16,32,
58     alg_serpent_b_byteswap_block,
59     { alg_serpent_makekey, alg_serpent_encrypt },
60     { alg_serpent_makekey, alg_serpent_decrypt } },
61   { 0 }
62 };
63
64 static void alg_sha1_init(void *state) { sha1_init(state); }
65 static void alg_sha1_update(void *state, const Byte *data, int len) {
66   sha1_update(state, data, len);
67 }
68 static void alg_sha1_final(void *state, Byte *digest) {
69   sha1_final(state, digest);
70 }
71 static void alg_sha1_oneshot(Byte *digest, const Byte *data, int len) {
72   sha1(data,len,digest);
73 }
74
75 static void alg_md5_init(void *state) { MD5Init(state); }
76 static void alg_md5_update(void *state, const Byte *data, int len) {
77   MD5Update(state, data, len);
78 }
79 static void alg_md5_final(void *state, Byte *digest) {
80   MD5Final(digest, state);
81 }
82 static void alg_md5_oneshot(Byte *digest, const Byte *data, int len) {
83   struct MD5Context ctx;
84   MD5Init(&ctx);
85   MD5Update(&ctx,data,len);
86   MD5Final(digest,&ctx);
87 }
88
89 const HashAlgInfo hashalginfos[]= {
90   { "sha1", 20, 64, sizeof(struct sha1_state),
91     alg_sha1_init, alg_sha1_update, alg_sha1_final, alg_sha1_oneshot },
92   { "md5", 16, 64, sizeof(struct MD5Context),
93     alg_md5_init, alg_md5_update, alg_md5_final, alg_md5_oneshot },
94   { 0 }
95 };