chiark / gitweb /
ea591fb0ed89860e026d12f9cd3edeee88eafca8
[chiark-tcl.git] / crypto / algtables.c
1 /*
2  */
3
4 #include "chiark_tcl_crypto.h"
5
6 #include <nettle/md5.h>
7 #include <nettle/sha.h>
8 #include <nettle/serpent.h>
9 #include <nettle/twofish.h>
10 #include <nettle/aes.h>
11 #include <nettle/blowfish.h>
12
13 #define NETTLE_BLOCKCIPHERS                     \
14   DO(serpent,  SERPENT)                         \
15   DO(twofish,  TWOFISH)                         \
16 /* DO(aes,      AES) */                         \
17   DO(blowfish, BLOWFISH)                        \
18   /*  ALIAS(rijndael, aes, AES)*/
19
20 #define ALIAS(alias,name,NAME)
21 #define DO(name,NAME)                                                         \
22   static void alg_##name##_makekey(void *sch, const void *key, int keylen) {  \
23     name##_set_key(sch, keylen, key);                                         \
24   }                                                                           \
25   static void alg_##name##_encr(const void *sch, const void *in, void *out) { \
26     name##_encrypt((void*)sch, NAME##_BLOCK_SIZE, out, in);                   \
27   }                                                                           \
28   static void alg_##name##_decr(const void *sch, const void *in, void *out) { \
29     name##_decrypt((void*)sch, NAME##_BLOCK_SIZE, out, in);                   \
30   }
31   NETTLE_BLOCKCIPHERS
32 #undef DO
33 #undef ALIAS
34
35 const BlockCipherAlgInfo cht_blockcipheralginfo_entries[]= {
36 #define ALIAS(alias,name,NAME)                                  \
37   { #alias, NAME##_BLOCK_SIZE, sizeof(struct name##_ctx),       \
38        NAME##_MIN_KEY_SIZE, NAME##_MAX_KEY_SIZE,                \
39     { alg_##name##_makekey, alg_##name##_encr },                \
40     { alg_##name##_makekey, alg_##name##_decr }                 \
41   },
42 #define DO(name,NAME) ALIAS(name,name,NAME)
43   NETTLE_BLOCKCIPHERS
44 #undef DO
45 #undef ALIAS
46   { 0 }
47 };
48
49 const BlockCipherPropInfo cht_blockcipherpropinfo_entries[]= {
50   { "blocklen",  offsetof(BlockCipherAlgInfo, blocksize) },
51   { "minkeylen", offsetof(BlockCipherAlgInfo, key_min)   },
52   { "maxkeylen", offsetof(BlockCipherAlgInfo, key_max)   },
53   { 0 }
54 };
55
56 #define NETTLE_DIGESTS                          \
57   DO(sha1,   SHA1)                              \
58   DO(sha256, SHA256)                            \
59   DO(md5,    MD5)
60
61 #define DO(name,NAME)                                                         \
62   static void alg_##name##_init(void *state) {                                \
63     name##_init(state);                                                       \
64   }                                                                           \
65   static void alg_##name##_update(void *state, const void *data, int len) {   \
66     name##_update(state, len, data);                                          \
67   }                                                                           \
68   static void alg_##name##_final(void *state, void *digest) {                 \
69     name##_digest(state,NAME##_DIGEST_SIZE,digest);                           \
70   }                                                                           \
71   static void alg_##name##_oneshot(void *digest, const void *data, int len) { \
72     struct name##_ctx ctx;                                                    \
73     name##_init(&ctx);                                                        \
74     name##_update(&ctx, len, data);                                           \
75     name##_digest(&ctx,NAME##_DIGEST_SIZE,digest);                            \
76   }
77   NETTLE_DIGESTS
78 #undef DO
79
80 const HashAlgPropInfo cht_hashalgpropinfo_entries[]= {
81   { "hashlen",  offsetof(HashAlgInfo, hashsize)  },
82   { "blocklen", offsetof(HashAlgInfo, blocksize) },
83   { 0 }
84 };
85
86 const HashAlgInfo cht_hashalginfo_entries[]= {
87 #define DO(name,NAME)                                                       \
88   { #name, NAME##_DIGEST_SIZE, NAME##_DATA_SIZE, sizeof(struct name##_ctx), \
89     alg_##name##_init, alg_##name##_update, alg_##name##_final,             \
90     alg_##name##_oneshot },
91   NETTLE_DIGESTS
92 #undef DO
93   { 0 }
94 };