1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file crypt.h Public crypto interface. */
6 * Asymmetric pubkey cryptosystem (apc).
8 * This is just RSA, but this fact is a hidden implementation detail.
11 /** The size of the challenge sent to the client. */
12 #define APC_CHALLENGE_SIZE 64
14 /** Opaque structure for public and private keys. */
15 struct asymmetric_key;
18 * Encrypt a buffer using asymmetric keys.
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.
25 * \return The size of the encrypted data on success, negative on errors.
27 int apc_pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
28 unsigned len, unsigned char *outbuf);
31 * Decrypt a buffer using a private key.
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.
38 * The \a outbuf must be large enough to hold at least 512 bytes.
40 * \return The size of the recovered plaintext on success, negative on errors.
42 int apc_priv_decrypt(const char *key_file, unsigned char *outbuf,
43 unsigned char *inbuf, int inlen);
46 * Read an asymmetric key from a file.
48 * \param key_file The file containing the key.
49 * \param result The key structure is returned here.
51 * \return The size of the key on success, negative on errors.
53 int apc_get_pubkey(const char *key_file, struct asymmetric_key **result);
56 * Deallocate a public key.
58 * \param key Pointer to the key structure to free.
60 * This should be called for keys obtained by \ref apc_get_pubkey() if the key is no
63 void apc_free_pubkey(struct asymmetric_key *key);
67 * Fill a buffer with random content.
69 * \param buf The buffer to fill.
70 * \param num The size of \a buf in bytes.
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().
77 void get_random_bytes_or_die(unsigned char *buf, int num);
80 * Initialize the crypto backend.
82 * This function initializes the crypto library and seeds the pseudo random
83 * number generator used by random() with a random seed obtained from the
84 * crypto implementation. On errors, an error message is logged and the
85 * function calls exit().
87 * \sa \ref get_random_bytes_or_die(), srandom(3), random(3), \ref
90 void crypt_init(void);
92 /** Allocate all resources of the crypto backend. */
93 void crypt_shutdown(void);
95 /** Opaque structure for stream ciphers. */
98 /** Number of bytes of the session key for stream ciphers. */
99 #define SESSION_KEY_LEN 32
102 * Used for client-server communication encryption.
104 * The traffic between (the forked child of) para_server and the remote client
105 * process is crypted by a symmetric session key. This structure contains the
106 * keys for the stream cipher and the file descriptor for which these keys
109 struct stream_cipher_context {
110 /** The socket file descriptor. */
112 /** Key used for receiving data. */
113 struct stream_cipher *recv;
114 /** Key used for sending data. */
115 struct stream_cipher *send;
119 * Allocate and initialize an aes_ctr128 stream cipher structure.
121 * \param data The key.
122 * \param len The size of the key.
124 * \return A new stream cipher structure.
126 struct stream_cipher *sc_new(const unsigned char *data, int len);
129 * Encrypt or decrypt a buffer using a stream cipher.
131 * \param sc Crypto key.
132 * \param src The source buffer and length.
133 * \param dst The destination buffer and length, filled out by the function.
135 * It is up to the implementation to decide whether the crypt operation is
136 * performed in place. The caller can tell by looking if the buffers given by
137 * \a src and \a dst coincide after the call. If (and only if) the crypt
138 * operation was not performed in place, the function allocated a new buffer
139 * for the result, so dst->iov_base is different from src->iov_base. In this
140 * case, the destination buffer must be freed by the caller when it is no
143 void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst);
146 * Wrapper for \ref sc_crypt() that can be used as a sideband transformation.
148 * \param src Passed verbatim to \ref sc_crypt().
149 * \param dst Passed verbatim to \ref sc_crypt().
150 * \param trafo_context Must point to an initialized stream cipher.
152 _static_inline_ void sc_trafo(struct iovec *src, struct iovec *dst,
155 sc_crypt(trafo_context, src, dst);
159 * Deallocate a stream cipher structure.
161 * \param sc A stream cipher previously obtained by \ref sc_new().
163 void sc_free(struct stream_cipher *sc);
165 /** Size of the hash value in bytes. */
169 * Compute the hash of the given input data.
171 * \param data Pointer to the data to compute the hash value from.
172 * \param len The length of \a data in bytes.
173 * \param hash Result pointer.
175 * \a hash must point to an area at least \p HASH_SIZE bytes large.
177 * \sa sha(3), openssl(1).
179 void hash_function(const char *data, unsigned long len, unsigned char *hash);
182 * Convert a hash value to ascii format.
184 * \param hash the hash value.
185 * \param asc Result pointer.
187 * \a asc must point to an area of at least 2 * \p HASH_SIZE + 1 bytes which
188 * will be filled by the function with the ascii representation of the hash
189 * value given by \a hash, and a terminating \p NULL byte.
191 void hash_to_asc(const unsigned char *hash, char *asc);
194 * Compare two hashes.
196 * \param h1 Pointer to the first hash value.
197 * \param h2 Pointer to the second hash value.
199 * \return 1, -1, or zero, depending on whether \a h1 is greater than,
200 * less than or equal to h2, respectively.
202 int hash_compare(const unsigned char *h1, const unsigned char *h2);