Merge branch 'refs/heads/t/wma'
[paraslash.git] / crypt.h
1 /*
2  * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file crypt.h Public crypto interface. */
8
9
10 /* These are used to distinguish between loading of private/public key. */
11
12 /** The key to load is a public key. */
13 #define LOAD_PUBLIC_KEY 0
14 /** The key to load is a private key. */
15 #define LOAD_PRIVATE_KEY 1
16 /** The size of the challenge sent to the client. */
17 #define CHALLENGE_SIZE 64
18
19 /** Opaque structure for public and private keys. */
20 struct asymmetric_key;
21
22 /**
23  * Encrypt a buffer using asymmetric keys.
24  *
25  * \param pub: The public key.
26  * \param inbuf The input buffer.
27  * \param len The length of \a inbuf.
28  * \param outbuf The output buffer.
29  *
30  * \return The size of the encrypted data on success, negative on errors.
31  */
32 int pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
33                 unsigned len, unsigned char *outbuf);
34
35 /**
36  * Decrypt a buffer using a private key.
37  *
38  * \param key_file Full path of the key.
39  * \param outbuf The output buffer.
40  * \param inbuf The encrypted input buffer.
41  * \param inlen The length of \a inbuf.
42  *
43  * The \a outbuf must be large enough to hold at least 512 bytes.
44  *
45  * \return The size of the recovered plaintext on success, negative on errors.
46  */
47 int priv_decrypt(const char *key_file, unsigned char *outbuf,
48                 unsigned char *inbuf, int inlen);
49
50 /**
51  * Read an asymmetric key from a file.
52  *
53  * \param key_file The file containing the key.
54  * \param result The key structure is returned here.
55  *
56  * \return The size of the key on success, negative on errors.
57  */
58 int get_public_key(const char *key_file, struct asymmetric_key **result);
59
60 /**
61  * Deallocate a public key.
62  *
63  * \param key Pointer to the key structure to free.
64  *
65  * This should be called for keys obtained by get_public_key() if the key is no
66  * longer needed.
67  */
68 void free_public_key(struct asymmetric_key *key);
69
70
71 /**
72  * Fill a buffer with random content.
73  *
74  * \param buf The buffer to fill.
75  * \param num The size of \a buf in bytes.
76  *
77  * This function puts \a num cryptographically strong pseudo-random bytes into
78  * buf. If it can not guarantee an unpredictable byte sequence (for example
79  * because the PRNG has not been seeded with enough randomness) the function
80  * logs an error message and calls exit().
81  */
82 void get_random_bytes_or_die(unsigned char *buf, int num);
83
84 /**
85  * Seed pseudo random number generators.
86  *
87  * This function seeds the PRNG used by random() with a random seed obtained
88  * from the crypto implementation. On errors, an error message is logged and
89  * the function calls exit().
90  *
91  * \sa \ref get_random_bytes_or_die(), srandom(3), random(3), \ref
92  * para_random().
93  */
94 void init_random_seed_or_die(void);
95
96
97 /** Opaque structure for stream ciphers. */
98 struct stream_cipher;
99
100 /** Number of bytes of the session key for stream ciphers. */
101 #define SESSION_KEY_LEN 32
102
103 /**
104  * Used for client-server communication encryption.
105  *
106  * The traffic between (the forked child of) para_server and the remote client
107  * process is crypted by a symmetric session key. This structure contains the
108  * keys for the stream cipher and the file descriptor for which these keys
109  * should be used.
110  */
111 struct stream_cipher_context {
112         /** The socket file descriptor. */
113         int fd;
114         /** Key used for receiving data. */
115         struct stream_cipher *recv;
116         /** Key used for sending data. */
117         struct stream_cipher *send;
118 };
119
120 /**
121  * Allocate and initialize an aes_ctr128 stream cipher structure.
122  *
123  * \param data The key.
124  * \param len The size of the key.
125  *
126  * \return A new stream cipher structure.
127  */
128 struct stream_cipher *sc_new(const unsigned char *data, int len);
129
130 /**
131  * Encrypt or decrypt a buffer using a stream cipher.
132  *
133  * \param sc Crypto key.
134  * \param src The source buffer and length.
135  * \param dst The destination buffer and length, filled out by the function.
136  *
137  * It is up to the implementation to decide whether the crypt operation is
138  * performed in place. The caller can tell by looking if the buffers given by
139  * \a src and \a dst coincide after the call. If (and only if) the crypt
140  * operation was not performed in place, the function allocated a new buffer
141  * for the result, so dst->iov_base is different from src->iov_base. In this
142  * case, the destination buffer must be freed by the caller when it is no
143  * longer needed.
144  */
145 void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst);
146
147 /**
148  * Wrapper for \ref sc_crypt() that can be used as a sideband transformation.
149  *
150  * \param src Passed verbatim to \ref sc_crypt().
151  * \param dst Passed verbatim to \ref sc_crypt().
152  * \param trafo_context Must point to an initialized stream cipher.
153  */
154 _static_inline_ void sc_trafo(struct iovec *src, struct iovec *dst,
155                 void *trafo_context)
156 {
157         sc_crypt(trafo_context, src, dst);
158 }
159
160 /**
161  * Deallocate a stream cipher structure.
162  *
163  * \param sc A stream cipher previously obtained by sc_new().
164  */
165 void sc_free(struct stream_cipher *sc);
166
167 /** Size of the hash value in bytes. */
168 #define HASH_SIZE 20
169
170 /**
171  * Compute the hash of the given input data.
172  *
173  * \param data Pointer to the data to compute the hash value from.
174  * \param len The length of \a data in bytes.
175  * \param hash Result pointer.
176  *
177  * \a hash must point to an area at least \p HASH_SIZE bytes large.
178  *
179  * \sa sha(3), openssl(1).
180  * */
181 void hash_function(const char *data, unsigned long len, unsigned char *hash);
182
183 /**
184  * Convert a hash value to ascii format.
185  *
186  * \param hash the hash value.
187  * \param asc Result pointer.
188  *
189  * \a asc must point to an area of at least 2 * \p HASH_SIZE + 1 bytes which
190  * will be filled by the function with the ascii representation of the hash
191  * value given by \a hash, and a terminating \p NULL byte.
192  */
193 void hash_to_asc(unsigned char *hash, char *asc);
194
195 /**
196  * Compare two hashes.
197  *
198  * \param h1 Pointer to the first hash value.
199  * \param h2 Pointer to the second hash value.
200  *
201  * \return 1, -1, or zero, depending on whether \a h1 is greater than,
202  * less than or equal to h2, respectively.
203  */
204 int hash_compare(unsigned char *h1, unsigned char *h2);