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