2 * crypto - Tcl bindings for parts of the `nettle' crypto library
3 * Copyright 2006 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 #include "chiark_tcl_crypto.h"
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>
31 #define NETTLE_BLOCKCIPHERS \
32 DO(serpent, SERPENT) \
33 DO(twofish, TWOFISH) \
35 DO(blowfish, BLOWFISH) \
36 /* ALIAS(rijndael, aes, AES)*/
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); \
43 static void alg_##name##_encr(const void *sch, const void *in, void *out) { \
44 name##_encrypt((void*)sch, NAME##_BLOCK_SIZE, out, in); \
46 static void alg_##name##_decr(const void *sch, const void *in, void *out) { \
47 name##_decrypt((void*)sch, NAME##_BLOCK_SIZE, out, in); \
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 } \
60 #define DO(name,NAME) ALIAS(name,name,NAME)
67 const BlockCipherPropInfo cht_blockcipherpropinfo_entries[]= {
68 { "blocklen", offsetof(BlockCipherAlgInfo, blocksize) },
69 { "minkeylen", offsetof(BlockCipherAlgInfo, key_min) },
70 { "maxkeylen", offsetof(BlockCipherAlgInfo, key_max) },
74 #define NETTLE_DIGESTS \
79 #define DO(name,NAME) \
80 static void alg_##name##_init(void *state) { \
83 static void alg_##name##_update(void *state, const void *data, int len) { \
84 name##_update(state, len, data); \
86 static void alg_##name##_final(void *state, void *digest) { \
87 name##_digest(state,NAME##_DIGEST_SIZE,digest); \
89 static void alg_##name##_oneshot(void *digest, const void *data, int len) { \
90 struct name##_ctx ctx; \
92 name##_update(&ctx, len, data); \
93 name##_digest(&ctx,NAME##_DIGEST_SIZE,digest); \
98 const HashAlgPropInfo cht_hashalgpropinfo_entries[]= {
99 { "hashlen", offsetof(HashAlgInfo, hashsize) },
100 { "blocklen", offsetof(HashAlgInfo, blocksize) },
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 },