abstract_crypto.h 2.53 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
/* abstract_crypto.h
 *
 * Interface to block cryptos and hash functions */

#ifndef LSH_ABSTRACT_CRYPTO_H_INCLUDED
#define LSH_ABSTRACT_CRYPTO_H_INCLUDED

Niels Möller's avatar
Niels Möller committed
8
9
#include "lsh_types.h"

Niels Möller's avatar
Niels Möller committed
10
11
12
13
14
15
16
17
18
19
struct crypto_instance
{
  UINT32 block_size;
  /* Length must be a multiple of the block size */
  void (*crypt)(struct crypto_instance *self,
		UINT32 length, UINT8 *dst, UINT8 *src);
};

#define CRYPT(instance, length, src, dst) \
((instance)->crypt((instance), (length), (src), (dst)))
Niels Möller's avatar
Niels Möller committed
20
21
22
23

#define CRYPTO_ENCRYPT 0
#define CRYPTO_DECRYPT 1

Niels Möller's avatar
Niels Möller committed
24
25
26
27
28
struct crypto_algorithm
{
  UINT32 block_size;
  UINT32 key_size;

Niels Möller's avatar
Niels Möller committed
29
30
31
  struct crypto_instance * (*make_crypt)(struct crypto_algorithm *self,
					 int mode,
					 UINT8 *key);
Niels Möller's avatar
Niels Möller committed
32
33
};

Niels Möller's avatar
Niels Möller committed
34
35
36
37
38
39
#define MAKE_ENCRYPT(crypto, key) \
((crypto)->make_crypt((crypto), CRYPTO_ENCRYPT, (key)))

#define MAKE_DECRYPT(crypto, key) \
((crypto)->make_crypt((crypto), CRYPTO_DECRYPT, (key)))

Niels Möller's avatar
Niels Möller committed
40
41
42
43
44
45
46
struct hash_instance
{
  UINT32 hash_size;
  void (*update)(struct hash_instance *self,
		 UINT32 length, UINT8 *data);
  void (*digest)(struct hash_instance *self,
		 UINT8 *result);
Niels Möller's avatar
Niels Möller committed
47
  struct hash_instance * (*copy)(struct hash_instance *self);
Niels Möller's avatar
Niels Möller committed
48
49
};

Niels Möller's avatar
Niels Möller committed
50
#define HASH_UPDATE(instance, length, data) \
Niels Möller's avatar
Niels Möller committed
51
52
((instance)->update((instance), (length), (data)))

Niels Möller's avatar
Niels Möller committed
53
#define HASH_DIGEST(instance, result) \
Niels Möller's avatar
Niels Möller committed
54
55
((instance)->digest((instance), (result)))

Niels Möller's avatar
Niels Möller committed
56
57
#define HASH_COPY(instance) ((instance)->copy((instance)))

Niels Möller's avatar
Niels Möller committed
58
59
60
61
62
63
64
65
/* Used for both hash functions ad macs */
#define mac_instance hash_instance
#define mac_size hash_size
  
struct hash_algorithm
{
  UINT32 block_size;
  UINT32 hash_size;
Niels Möller's avatar
Niels Möller committed
66
  struct hash_instance * (*make_hash)(struct hash_algorithm *self);
Niels Möller's avatar
Niels Möller committed
67
68
};

Niels Möller's avatar
Niels Möller committed
69
70
#define MAKE_HASH(h) ((h)->make_hash((h)))

Niels Möller's avatar
Niels Möller committed
71
72
73
74
struct mac_algorithm
{
  UINT32 hash_size;
  UINT32 key_size;
Niels Möller's avatar
Niels Möller committed
75
76
77
78
79
80
  struct mac_instance * (*make_mac)(struct mac_algorithm *self,
				    UINT8 *key);
};

#define MAKE_MAC(m, key) ((m)->make_mac((m), (key)))

81
struct signer
Niels Möller's avatar
Niels Möller committed
82
{
83
84
85
86
  /* Returns a signature string, *without* the length field */
  struct lsh_string * (*sign)(struct signer *closure,
			      UINT32 length,
			      UINT8 *data);
Niels Möller's avatar
Niels Möller committed
87
88
};

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
struct verifier
{
  int (*verify)(struct verifier *closure,
		UINT32 length,
		UINT8 *data,
		UINT32 signature_length,
		UINT8 * signature_data);
};
		
struct signature_algorithm
{
  struct signer *
  (struct make_signer *)(struct signature_algorithm *closure,
			 UINT32 public_length,
			 UINT8 *public,
			 UINT32 secret_length,
			 UINT8 *secret);
  struct verifier *
  (struct make_verifier *)(struct signature_algorithm *closure,
			   UINT32 public_length,
			   UINT8 *public);
};
Niels Möller's avatar
Niels Möller committed
111
112

#endif /* LSH_ABSTRACT_CRYPTO_H_INCLUDED */