2 * crypto - Tcl bindings for parts of the `nettle' crypto library
3 * Copyright 2006-2012 Ian Jackson
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.
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.
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/>.
20 #include "chiark_tcl_crypto.h"
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>
29 #define NETTLE_BLOCKCIPHERS \
30 DO(serpent, SERPENT) \
31 DO(twofish, TWOFISH) \
33 DO(blowfish, BLOWFISH) \
34 /* ALIAS(rijndael, aes, AES)*/
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); \
41 static void alg_##name##_encr(const void *sch, const void *in, void *out) { \
42 name##_encrypt((void*)sch, NAME##_BLOCK_SIZE, out, in); \
44 static void alg_##name##_decr(const void *sch, const void *in, void *out) { \
45 name##_decrypt((void*)sch, NAME##_BLOCK_SIZE, out, in); \
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 } \
58 #define DO(name,NAME) ALIAS(name,name,NAME)
65 const BlockCipherPropInfo cht_blockcipherpropinfo_entries[]= {
66 { "blocklen", offsetof(BlockCipherAlgInfo, blocksize) },
67 { "minkeylen", offsetof(BlockCipherAlgInfo, key_min) },
68 { "maxkeylen", offsetof(BlockCipherAlgInfo, key_max) },
72 #define NETTLE_DIGESTS \
77 #define DO(name,NAME) \
78 static void alg_##name##_init(void *state) { \
81 static void alg_##name##_update(void *state, const void *data, int len) { \
82 name##_update(state, len, data); \
84 static void alg_##name##_final(void *state, void *digest) { \
85 name##_digest(state,NAME##_DIGEST_SIZE,digest); \
87 static void alg_##name##_oneshot(void *digest, const void *data, int len) { \
88 struct name##_ctx ctx; \
90 name##_update(&ctx, len, data); \
91 name##_digest(&ctx,NAME##_DIGEST_SIZE,digest); \
96 const HashAlgPropInfo cht_hashalgpropinfo_entries[]= {
97 { "hashlen", offsetof(HashAlgInfo, hashsize) },
98 { "blocklen", offsetof(HashAlgInfo, blocksize) },
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 },