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 */
13 #include <openssl/rand.h>
14 #include <openssl/err.h>
17 * Fill a buffer with random content.
19 * \param buf The buffer to fill.
20 * \param num The size of \a buf in bytes.
22 * This function puts \a num cryptographically strong pseudo-random bytes into
23 * buf. If libssl can not guarantee an unpredictable byte sequence (for example
24 * because the PRNG has not been seeded with enough randomness) the function
25 * logs an error message and calls exit().
27 void get_random_bytes_or_die(unsigned char *buf, int num)
31 /* RAND_bytes() returns 1 on success, 0 otherwise. */
32 if (RAND_bytes(buf, num) == 1)
34 err = ERR_get_error();
35 PARA_EMERG_LOG("%s\n", ERR_reason_error_string(err));
40 * Seed pseudo random number generators.
42 * This function reads 64 bytes from /dev/urandom and adds them to the SSL
43 * PRNG. It also seeds the PRNG used by random() with a random seed obtained
44 * from SSL. If /dev/random could not be read, an error message is logged and
45 * the function calls exit().
47 * \sa RAND_load_file(3), \ref get_random_bytes_or_die(), srandom(3),
48 * random(3), \ref para_random().
50 void init_random_seed_or_die(void)
52 int seed, ret = RAND_load_file("/dev/urandom", 64);
55 PARA_EMERG_LOG("could not seed PRNG (ret = %d)\n", ret);
58 get_random_bytes_or_die((unsigned char *)&seed, sizeof(seed));
62 static EVP_PKEY *load_key(const char *file, int private)
65 EVP_PKEY *pkey = NULL;
67 key = BIO_new(BIO_s_file());
70 if (BIO_read_filename(key, file) > 0) {
71 if (private == LOAD_PRIVATE_KEY)
72 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, NULL);
74 pkey = PEM_read_bio_PUBKEY(key, NULL, NULL, NULL);
81 * read an RSA key from a file
83 * \param key_file the file containing the key
84 * \param rsa RSA structure is returned here
85 * \param private if non-zero, read the private key, otherwise the public key
87 * \return The size of the RSA key on success, negative on errors.
89 * \sa openssl(1), rsa(1).
91 int get_rsa_key(char *key_file, RSA **rsa, int private)
93 EVP_PKEY *key = load_key(key_file, private);
96 return (private == LOAD_PRIVATE_KEY)? -E_PRIVATE_KEY
98 *rsa = EVP_PKEY_get1_RSA(key);
102 return RSA_size(*rsa);
106 * free an RSA structure
108 * \param rsa pointer to the RSA struct to free
110 * This must be called for any key obtained by get_rsa_key().
112 void rsa_free(RSA *rsa)
119 * decrypt a buffer using an RSA key
121 * \param key_file full path of the rsa key
122 * \param outbuf the output buffer
123 * \param inbuf the encrypted input buffer
124 * \param rsa_inlen the length of \a inbuf
126 * The \a outbuf must be large enough to hold at least \a rsa_inlen bytes.
128 * \return The size of the recovered plaintext on success, negative on errors.
130 * \sa RSA_private_decrypt(3)
132 int para_decrypt_buffer(char *key_file, unsigned char *outbuf, unsigned char *inbuf,
136 int ret, inlen = rsa_inlen;
140 ret = get_rsa_key(key_file, &rsa, LOAD_PRIVATE_KEY);
143 ret = RSA_private_decrypt(inlen, inbuf, outbuf, rsa, RSA_PKCS1_PADDING);
145 return (ret > 0)? ret : -E_DECRYPT;
149 * decrypt the challenge number sent by para_server
151 * \param key_file full path of the rsa key
152 * \param challenge_nr result is stored here
153 * \param inbuf the input buffer
154 * \param rsa_inlen the length of \a inbuf
156 * \return positive on success, negative on errors
158 * \sa para_decrypt_buffer()
160 int para_decrypt_challenge(char *key_file, long unsigned *challenge_nr,
161 unsigned char *inbuf, unsigned rsa_inlen)
163 unsigned char *rsa_out = OPENSSL_malloc(rsa_inlen + 1);
164 int ret = para_decrypt_buffer(key_file, rsa_out, inbuf, rsa_inlen);
168 ret = sscanf((char *)rsa_out, "%lu", challenge_nr) == 1?
171 OPENSSL_free(rsa_out);
176 * encrypt a buffer using an RSA key
178 * \param rsa: public rsa key
179 * \param inbuf the input buffer
180 * \param len the length of \a inbuf
181 * \param outbuf the output buffer
183 * \return The size of the encrypted data on success, negative on errors
185 * \sa RSA_public_encrypt(3)
187 int para_encrypt_buffer(RSA *rsa, unsigned char *inbuf,
188 unsigned len, unsigned char *outbuf)
190 int ret, flen = len; /* RSA_public_encrypt expects a signed int */
194 ret = RSA_public_encrypt(flen, inbuf, outbuf, rsa, RSA_PKCS1_PADDING);
195 return ret < 0? -E_ENCRYPT : ret;
199 * encrypt the given challenge number
201 * \param rsa: public rsa key
202 * \param challenge_nr the number to be encrypted
203 * \param outbuf the output buffer
205 * \a outbuf must be at least 64 bytes long
207 * \return The size of the encrypted data on success, negative on errors
209 * \sa para_encrypt_buffer()
212 int para_encrypt_challenge(RSA* rsa, long unsigned challenge_nr,
213 unsigned char *outbuf)
215 unsigned char *inbuf = (unsigned char*) make_message("%lu", challenge_nr);
216 int ret = para_encrypt_buffer(rsa, inbuf, strlen((char *)inbuf), outbuf);