2 * Copyright (C) 2005-2012 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file crypt.h Public crypto interface. */
10 /* These are used to distinguish between loading of private/public key. */
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
19 /** Opaque structure for public and private keys. */
20 struct asymmetric_key;
23 * Encrypt a buffer using asymmetric keys.
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.
30 * \return The size of the encrypted data on success, negative on errors.
32 int pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
33 unsigned len, unsigned char *outbuf);
36 * Decrypt a buffer using a private key.
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.
43 * The \a outbuf must be large enough to hold at least 512 bytes.
45 * \return The size of the recovered plaintext on success, negative on errors.
47 int priv_decrypt(const char *key_file, unsigned char *outbuf,
48 unsigned char *inbuf, int inlen);
51 * Read an asymmetric key from a file.
53 * \param key_file The file containing the key.
54 * \param private if non-zero, read the private key, otherwise the public key.
55 * \param result The key structure is returned here.
57 * \return The size of the key on success, negative on errors.
59 int get_asymmetric_key(const char *key_file, int private,
60 struct asymmetric_key **result);
63 * Deallocate an asymmetric key structure.
65 * \param key Pointer to the key structure to free.
67 * This must be called for any key obtained by get_asymmetric_key().
69 void free_asymmetric_key(struct asymmetric_key *key);
73 * Fill a buffer with random content.
75 * \param buf The buffer to fill.
76 * \param num The size of \a buf in bytes.
78 * This function puts \a num cryptographically strong pseudo-random bytes into
79 * buf. If it can not guarantee an unpredictable byte sequence (for example
80 * because the PRNG has not been seeded with enough randomness) the function
81 * logs an error message and calls exit().
83 void get_random_bytes_or_die(unsigned char *buf, int num);
86 * Seed pseudo random number generators.
88 * This function seeds the PRNG used by random() with a random seed obtained
89 * from the crypto implementation. On errors, an error message is logged and
90 * the function calls exit().
92 * \sa \ref get_random_bytes_or_die(), srandom(3), random(3), \ref
95 void init_random_seed_or_die(void);
98 /** Opaque structure for stream ciphers. */
101 /** Number of bytes of the session key for stream ciphers. */
102 #define SESSION_KEY_LEN 32
105 * Used for client-server communication encryption.
107 * The traffic between (the forked child of) para_server and the remote client
108 * process is crypted by a symmetric session key. This structure contains the
109 * keys for the stream cipher and the file descriptor for which these keys
112 struct stream_cipher_context {
113 /** The socket file descriptor. */
115 /** Key used for receiving data. */
116 struct stream_cipher *recv;
117 /** Key used for sending data. */
118 struct stream_cipher *send;
122 * Allocate and initialize a stream cipher structure.
124 * \param data The key.
125 * \param len The size of the key.
127 * \return A new stream cipher structure.
129 struct stream_cipher *sc_new(const unsigned char *data, int len);
132 * Deallocate a stream cipher structure.
134 * \param sc A stream cipher previously obtained by sc_new().
136 void sc_free(struct stream_cipher *sc);
139 * Encrypt and send a buffer.
141 * \param scc The context.
142 * \param buf The buffer to send.
143 * \param len The size of \a buf in bytes.
145 * \return The return value of the underyling call to write_all().
147 * \sa \ref write_all(), RC4(3).
149 int sc_send_bin_buffer(struct stream_cipher_context *scc, char *buf,
153 * Encrypt and send a \p NULL-terminated buffer.
155 * \param scc The context.
156 * \param buf The buffer to send.
158 * \return The return value of the underyling call to sc_send_bin_buffer().
160 int sc_send_buffer(struct stream_cipher_context *scc, char *buf);
163 * Format, encrypt and send a buffer.
165 * \param scc The context.
166 * \param fmt A format string.
168 * \return The return value of the underyling call to sc_send_buffer().
170 __printf_2_3 int sc_send_va_buffer(struct stream_cipher_context *scc,
171 const char *fmt, ...);
174 * Receive a buffer and decrypt it.
176 * \param scc The context.
177 * \param buf The buffer to write the decrypted data to.
178 * \param size The size of \a buf.
180 * \return The number of bytes received on success, negative on errors, zero if
181 * the peer has performed an orderly shutdown.
183 * \sa recv(2), RC4(3).
185 int sc_recv_bin_buffer(struct stream_cipher_context *scc, char *buf,
189 * Receive a buffer, decrypt it and write terminating NULL byte.
191 * \param scc The context.
192 * \param buf The buffer to write the decrypted data to.
193 * \param size The size of \a buf.
195 * Read at most \a size - 1 bytes from file descriptor given by \a scc, decrypt
196 * the received data and write a NULL byte at the end of the decrypted data.
198 * \return The return value of the underlying call to \ref
199 * sc_recv_bin_buffer().
201 int sc_recv_buffer(struct stream_cipher_context *scc, char *buf, size_t size);
204 /** Size of the hash value in bytes. */
209 * Compute the hash of the given input data.
211 * \param data Pointer to the data to compute the hash value from.
212 * \param len The length of \a data in bytes.
213 * \param hash Result pointer.
215 * \a hash must point to an area at least \p HASH_SIZE bytes large.
217 * \sa sha(3), openssl(1).
219 void hash_function(const char *data, unsigned long len, unsigned char *hash);
222 * Convert a hash value to ascii format.
224 * \param hash the hash value.
225 * \param asc Result pointer.
227 * \a asc must point to an area of at least 2 * \p HASH_SIZE + 1 bytes which
228 * will be filled by the function with the ascii representation of the hash
229 * value given by \a hash, and a terminating \p NULL byte.
231 void hash_to_asc(unsigned char *hash, char *asc);
234 * Compare two hashes.
236 * \param h1 Pointer to the first hash value.
237 * \param h2 Pointer to the second hash value.
239 * \return 1, -1, or zero, depending on whether \a h1 is greater than,
240 * less than or equal to h2, respectively.
242 int hash_compare(unsigned char *h1, unsigned char *h2);