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