chiark / gitweb /
better core algorithm selection and new core alg suites
[chiark-tcl.git] / crypto / algtables.c
1 /*
2  */
3
4 #include <stdint.h>
5 #include <netinet/in.h>
6
7 #include "hbytes.h"
8
9 #include <nettle/md5.h>
10 #include <nettle/sha.h>
11 #include <nettle/serpent.h>
12 #include <nettle/twofish.h>
13 #include <nettle/aes.h>
14 #include <nettle/blowfish.h>
15
16 #define NETTLE_BLOCKCIPHERS                     \
17   DO(serpent,  SERPENT)                         \
18   DO(twofish,  TWOFISH)                         \
19   DO(aes,      AES)                             \
20   DO(blowfish, BLOWFISH)                        \
21   ALIAS(rijndael, aes, AES)
22
23 #define ALIAS(alias,name,NAME)
24 #define DO(name,NAME)                                                         \
25   static void alg_##name##_makekey(void *sch, const void *key, int keylen) {  \
26     name##_set_key(sch, keylen, key);                                         \
27   }                                                                           \
28   static void alg_##name##_encr(const void *sch, const void *in, void *out) { \
29     ##name##_encrypt((void*)sch, NAME##_BLOCK_SIZE, out, in);                 \
30   }                                                                           \
31   static void alg_##name##_decr(const void *sch, const void *in, void *out) { \
32     ##name##_decrypt((void*)sch, NAME##_BLOCK_SIZE, out, in);                 \
33   }
34   NETTLE_BLOCKCIPHERS
35 #undef DO
36 #undef ALIAS
37
38 const BlockCipherAlgInfo blockcipheralginfos[]= {
39 #define ALIAS(alias,name,NAME)                                  \
40   { #alias, NAME##_BLOCK_SIZE, sizeof(struct name##_ctx),       \
41        NAME##_MIN_KEY_SIZE, NAME##_MAX_KEY_SIZE,                \
42     0,                                                          \
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 };