2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file crypt.c openssl-based RSA encryption/decryption routines */
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <openssl/rand.h>
13 #include <openssl/err.h>
14 #include <openssl/rc4.h>
22 * Fill a buffer with random content.
24 * \param buf The buffer to fill.
25 * \param num The size of \a buf in bytes.
27 * This function puts \a num cryptographically strong pseudo-random bytes into
28 * buf. If libssl can not guarantee an unpredictable byte sequence (for example
29 * because the PRNG has not been seeded with enough randomness) the function
30 * logs an error message and calls exit().
32 void get_random_bytes_or_die(unsigned char *buf, int num)
36 /* RAND_bytes() returns 1 on success, 0 otherwise. */
37 if (RAND_bytes(buf, num) == 1)
39 err = ERR_get_error();
40 PARA_EMERG_LOG("%s\n", ERR_reason_error_string(err));
45 * Seed pseudo random number generators.
47 * This function reads 64 bytes from /dev/urandom and adds them to the SSL
48 * PRNG. It also seeds the PRNG used by random() with a random seed obtained
49 * from SSL. If /dev/random could not be read, an error message is logged and
50 * the function calls exit().
52 * \sa RAND_load_file(3), \ref get_random_bytes_or_die(), srandom(3),
53 * random(3), \ref para_random().
55 void init_random_seed_or_die(void)
57 int seed, ret = RAND_load_file("/dev/urandom", 64);
60 PARA_EMERG_LOG("could not seed PRNG (ret = %d)\n", ret);
63 get_random_bytes_or_die((unsigned char *)&seed, sizeof(seed));
67 static EVP_PKEY *load_key(const char *file, int private)
70 EVP_PKEY *pkey = NULL;
72 key = BIO_new(BIO_s_file());
75 if (BIO_read_filename(key, file) > 0) {
76 if (private == LOAD_PRIVATE_KEY)
77 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, NULL);
79 pkey = PEM_read_bio_PUBKEY(key, NULL, NULL, NULL);
86 * read an RSA key from a file
88 * \param key_file the file containing the key
89 * \param rsa RSA structure is returned here
90 * \param private if non-zero, read the private key, otherwise the public key
92 * \return The size of the RSA key on success, negative on errors.
94 * \sa openssl(1), rsa(1).
96 int get_rsa_key(char *key_file, RSA **rsa, int private)
98 EVP_PKEY *key = load_key(key_file, private);
101 return (private == LOAD_PRIVATE_KEY)? -E_PRIVATE_KEY
103 *rsa = EVP_PKEY_get1_RSA(key);
107 return RSA_size(*rsa);
111 * free an RSA structure
113 * \param rsa pointer to the RSA struct to free
115 * This must be called for any key obtained by get_rsa_key().
117 void rsa_free(RSA *rsa)
124 * decrypt a buffer using an RSA key
126 * \param key_file full path of the rsa key
127 * \param outbuf the output buffer
128 * \param inbuf the encrypted input buffer
129 * \param rsa_inlen the length of \a inbuf
131 * The \a outbuf must be large enough to hold at least \a rsa_inlen bytes.
133 * \return The size of the recovered plaintext on success, negative on errors.
135 * \sa RSA_private_decrypt(3)
137 int para_decrypt_buffer(char *key_file, unsigned char *outbuf, unsigned char *inbuf,
141 int ret, inlen = rsa_inlen;
145 ret = get_rsa_key(key_file, &rsa, LOAD_PRIVATE_KEY);
148 ret = RSA_private_decrypt(inlen, inbuf, outbuf, rsa, RSA_PKCS1_OAEP_PADDING);
150 return (ret > 0)? ret : -E_DECRYPT;
154 * encrypt a buffer using an RSA key
156 * \param rsa: public rsa key
157 * \param inbuf the input buffer
158 * \param len the length of \a inbuf
159 * \param outbuf the output buffer
161 * \return The size of the encrypted data on success, negative on errors
163 * \sa RSA_public_encrypt(3)
165 int para_encrypt_buffer(RSA *rsa, unsigned char *inbuf,
166 unsigned len, unsigned char *outbuf)
168 int ret, flen = len; /* RSA_public_encrypt expects a signed int */
172 ret = RSA_public_encrypt(flen, inbuf, outbuf, rsa, RSA_PKCS1_OAEP_PADDING);
173 return ret < 0? -E_ENCRYPT : ret;
177 * Encrypt and send a buffer.
179 * \param rc4c The rc4 crypt context.
180 * \param buf The buffer to send.
181 * \param len The size of \a buf in bytes.
183 * \return The return value of the underyling call to write_all().
185 * \sa \ref write_all(), RC4(3).
187 int rc4_send_bin_buffer(struct rc4_context *rc4c, const char *buf, size_t len)
193 tmp = para_malloc(len);
194 RC4(&rc4c->send_key, len, (const unsigned char *)buf, tmp);
195 ret = write_all(rc4c->fd, (char *)tmp, &len);
201 * Encrypt and send a \p NULL-terminated buffer.
203 * \param rc4c The rc4 crypt context.
204 * \param buf The buffer to send.
206 * \return The return value of the underyling call to rc4_send_bin_buffer().
208 int rc4_send_buffer(struct rc4_context *rc4c, const char *buf)
210 return rc4_send_bin_buffer(rc4c, buf, strlen(buf));
214 * Format, encrypt and send a buffer.
216 * \param rc4c The rc4 crypt context.
217 * \param fmt A format string.
219 * \return The return value of the underyling call to rc4_send_buffer().
221 __printf_2_3 int rc4_send_va_buffer(struct rc4_context *rc4c, const char *fmt, ...)
226 PARA_VSPRINTF(fmt, msg);
227 ret = rc4_send_buffer(rc4c, msg);
233 * Receive a buffer and decrypt it.
235 * \param rc4c The rc4 crypt context.
236 * \param buf The buffer to write the decrypted data to.
237 * \param size The size of \a buf.
239 * \return The number of bytes received on success, negative on errors, zero if
240 * the peer has performed an orderly shutdown.
242 * \sa recv(2), RC4(3).
244 int rc4_recv_bin_buffer(struct rc4_context *rc4c, char *buf, size_t size)
246 unsigned char *tmp = para_malloc(size);
247 ssize_t ret = recv(rc4c->fd, tmp, size, 0);
250 RC4(&rc4c->recv_key, ret, tmp, (unsigned char *)buf);
252 ret = -ERRNO_TO_PARA_ERROR(errno);
258 * Receive a buffer, decrypt it and write terminating NULL byte.
260 * \param rc4c The rc4 crypt context.
261 * \param buf The buffer to write the decrypted data to.
262 * \param size The size of \a buf.
264 * Read at most \a size - 1 bytes from file descriptor given by \a rc4c,
265 * decrypt the received data and write a NULL byte at the end of the decrypted
268 * \return The return value of the underlying call to \ref
269 * rc4_recv_bin_buffer().
271 int rc4_recv_buffer(struct rc4_context *rc4c, char *buf, size_t size)
276 n = rc4_recv_bin_buffer(rc4c, buf, size - 1);