chiark / gitweb /
lintian sorted out
[chiark-tcl.git] / crypto / algtables.c
1 /*
2  * crypto - Tcl bindings for parts of the `nettle' crypto library
3  * Copyright 2006 Ian Jackson
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18  * 02110-1301, USA.
19  */
20
21
22 #include "chiark_tcl_crypto.h"
23
24 #include <nettle/md5.h>
25 #include <nettle/sha.h>
26 #include <nettle/serpent.h>
27 #include <nettle/twofish.h>
28 #include <nettle/aes.h>
29 #include <nettle/blowfish.h>
30
31 #define NETTLE_BLOCKCIPHERS                     \
32   DO(serpent,  SERPENT)                         \
33   DO(twofish,  TWOFISH)                         \
34 /* DO(aes,      AES) */                         \
35   DO(blowfish, BLOWFISH)                        \
36   /*  ALIAS(rijndael, aes, AES)*/
37
38 #define ALIAS(alias,name,NAME)
39 #define DO(name,NAME)                                                         \
40   static void alg_##name##_makekey(void *sch, const void *key, int keylen) {  \
41     name##_set_key(sch, keylen, key);                                         \
42   }                                                                           \
43   static void alg_##name##_encr(const void *sch, const void *in, void *out) { \
44     name##_encrypt((void*)sch, NAME##_BLOCK_SIZE, out, in);                   \
45   }                                                                           \
46   static void alg_##name##_decr(const void *sch, const void *in, void *out) { \
47     name##_decrypt((void*)sch, NAME##_BLOCK_SIZE, out, in);                   \
48   }
49   NETTLE_BLOCKCIPHERS
50 #undef DO
51 #undef ALIAS
52
53 const BlockCipherAlgInfo cht_blockcipheralginfo_entries[]= {
54 #define ALIAS(alias,name,NAME)                                  \
55   { #alias, NAME##_BLOCK_SIZE, sizeof(struct name##_ctx),       \
56        NAME##_MIN_KEY_SIZE, NAME##_MAX_KEY_SIZE,                \
57     { alg_##name##_makekey, alg_##name##_encr },                \
58     { alg_##name##_makekey, alg_##name##_decr }                 \
59   },
60 #define DO(name,NAME) ALIAS(name,name,NAME)
61   NETTLE_BLOCKCIPHERS
62 #undef DO
63 #undef ALIAS
64   { 0 }
65 };
66
67 const BlockCipherPropInfo cht_blockcipherpropinfo_entries[]= {
68   { "blocklen",  offsetof(BlockCipherAlgInfo, blocksize) },
69   { "minkeylen", offsetof(BlockCipherAlgInfo, key_min)   },
70   { "maxkeylen", offsetof(BlockCipherAlgInfo, key_max)   },
71   { 0 }
72 };
73
74 #define NETTLE_DIGESTS                          \
75   DO(sha1,   SHA1)                              \
76   DO(sha256, SHA256)                            \
77   DO(md5,    MD5)
78
79 #define DO(name,NAME)                                                         \
80   static void alg_##name##_init(void *state) {                                \
81     name##_init(state);                                                       \
82   }                                                                           \
83   static void alg_##name##_update(void *state, const void *data, int len) {   \
84     name##_update(state, len, data);                                          \
85   }                                                                           \
86   static void alg_##name##_final(void *state, void *digest) {                 \
87     name##_digest(state,NAME##_DIGEST_SIZE,digest);                           \
88   }                                                                           \
89   static void alg_##name##_oneshot(void *digest, const void *data, int len) { \
90     struct name##_ctx ctx;                                                    \
91     name##_init(&ctx);                                                        \
92     name##_update(&ctx, len, data);                                           \
93     name##_digest(&ctx,NAME##_DIGEST_SIZE,digest);                            \
94   }
95   NETTLE_DIGESTS
96 #undef DO
97
98 const HashAlgPropInfo cht_hashalgpropinfo_entries[]= {
99   { "hashlen",  offsetof(HashAlgInfo, hashsize)  },
100   { "blocklen", offsetof(HashAlgInfo, blocksize) },
101   { 0 }
102 };
103
104 const HashAlgInfo cht_hashalginfo_entries[]= {
105 #define DO(name,NAME)                                                       \
106   { #name, NAME##_DIGEST_SIZE, NAME##_DATA_SIZE, sizeof(struct name##_ctx), \
107     alg_##name##_init, alg_##name##_update, alg_##name##_final,             \
108     alg_##name##_oneshot },
109   NETTLE_DIGESTS
110 #undef DO
111   { 0 }
112 };