chiark / gitweb /
Makefiles: Refer to $(BASE_DIR) rather than ../base/
[chiark-tcl.git] / crypto / algtables.c
1 /*
2  * crypto - Tcl bindings for parts of the `nettle' crypto library
3  * Copyright 2006-2012 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, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "chiark_tcl_crypto.h"
21
22 #include <nettle/md5.h>
23 #include <nettle/sha.h>
24 #include <nettle/serpent.h>
25 #include <nettle/twofish.h>
26 #include <nettle/aes.h>
27 #include <nettle/blowfish.h>
28
29 #define NETTLE_BLOCKCIPHERS                     \
30   DO(serpent,  SERPENT)                         \
31   DO(twofish,  TWOFISH)                         \
32 /* DO(aes,      AES) */                         \
33   DO(blowfish, BLOWFISH)                        \
34   /*  ALIAS(rijndael, aes, AES)*/
35
36 #define ALIAS(alias,name,NAME)
37 #define DO(name,NAME)                                                         \
38   static void alg_##name##_makekey(void *sch, const void *key, int keylen) {  \
39     name##_set_key(sch, keylen, key);                                         \
40   }                                                                           \
41   static void alg_##name##_encr(const void *sch, const void *in, void *out) { \
42     name##_encrypt((void*)sch, NAME##_BLOCK_SIZE, out, in);                   \
43   }                                                                           \
44   static void alg_##name##_decr(const void *sch, const void *in, void *out) { \
45     name##_decrypt((void*)sch, NAME##_BLOCK_SIZE, out, in);                   \
46   }
47   NETTLE_BLOCKCIPHERS
48 #undef DO
49 #undef ALIAS
50
51 const BlockCipherAlgInfo cht_blockcipheralginfo_entries[]= {
52 #define ALIAS(alias,name,NAME)                                  \
53   { #alias, NAME##_BLOCK_SIZE, sizeof(struct name##_ctx),       \
54        NAME##_MIN_KEY_SIZE, NAME##_MAX_KEY_SIZE,                \
55     { alg_##name##_makekey, alg_##name##_encr },                \
56     { alg_##name##_makekey, alg_##name##_decr }                 \
57   },
58 #define DO(name,NAME) ALIAS(name,name,NAME)
59   NETTLE_BLOCKCIPHERS
60 #undef DO
61 #undef ALIAS
62   { 0 }
63 };
64
65 const BlockCipherPropInfo cht_blockcipherpropinfo_entries[]= {
66   { "blocklen",  offsetof(BlockCipherAlgInfo, blocksize) },
67   { "minkeylen", offsetof(BlockCipherAlgInfo, key_min)   },
68   { "maxkeylen", offsetof(BlockCipherAlgInfo, key_max)   },
69   { 0 }
70 };
71
72 #define NETTLE_DIGESTS                          \
73   DO(sha1,   SHA1)                              \
74   DO(sha256, SHA256)                            \
75   DO(md5,    MD5)
76
77 #define DO(name,NAME)                                                         \
78   static void alg_##name##_init(void *state) {                                \
79     name##_init(state);                                                       \
80   }                                                                           \
81   static void alg_##name##_update(void *state, const void *data, int len) {   \
82     name##_update(state, len, data);                                          \
83   }                                                                           \
84   static void alg_##name##_final(void *state, void *digest) {                 \
85     name##_digest(state,NAME##_DIGEST_SIZE,digest);                           \
86   }                                                                           \
87   static void alg_##name##_oneshot(void *digest, const void *data, int len) { \
88     struct name##_ctx ctx;                                                    \
89     name##_init(&ctx);                                                        \
90     name##_update(&ctx, len, data);                                           \
91     name##_digest(&ctx,NAME##_DIGEST_SIZE,digest);                            \
92   }
93   NETTLE_DIGESTS
94 #undef DO
95
96 const HashAlgPropInfo cht_hashalgpropinfo_entries[]= {
97   { "hashlen",  offsetof(HashAlgInfo, hashsize)  },
98   { "blocklen", offsetof(HashAlgInfo, blocksize) },
99   { 0 }
100 };
101
102 const HashAlgInfo cht_hashalginfo_entries[]= {
103 #define DO(name,NAME)                                                       \
104   { #name, NAME##_DIGEST_SIZE, NAME##_DATA_SIZE, sizeof(struct name##_ctx), \
105     alg_##name##_init, alg_##name##_update, alg_##name##_final,             \
106     alg_##name##_oneshot },
107   NETTLE_DIGESTS
108 #undef DO
109   { 0 }
110 };