]> git.tuebingen.mpg.de Git - paraslash.git/blob - crypt.c
Use openssl's RAND_load_file() and RAND_bytes() to get randomness.
[paraslash.git] / crypt.c
1 /*
2  * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file crypt.c openssl-based RSA encryption/decryption routines */
8
9 #include "para.h"
10 #include "error.h"
11 #include "string.h"
12 #include "crypt.h"
13 #include <openssl/rand.h>
14 #include <openssl/err.h>
15
16 /**
17  * Fill a buffer with random content.
18  *
19  * \param buf The buffer to fill.
20  * \param num The size of \a buf in bytes.
21  *
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().
26  */
27 void get_random_bytes_or_die(unsigned char *buf, int num)
28 {
29         unsigned long err;
30
31         /* RAND_bytes() returns 1 on success, 0 otherwise. */
32         if (RAND_bytes(buf, num) == 1)
33                 return;
34         err = ERR_get_error();
35         PARA_EMERG_LOG("%s\n", ERR_reason_error_string(err));
36         exit(EXIT_FAILURE);
37 }
38
39 /**
40  * Seed pseudo random number generators.
41  *
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().
46  *
47  * \sa RAND_load_file(3), \ref get_random_bytes_or_die(), srandom(3),
48  * random(3), \ref para_random().
49  */
50 void init_random_seed_or_die(void)
51 {
52         int seed, ret = RAND_load_file("/dev/urandom", 64);
53
54         if (ret != 64) {
55                 PARA_EMERG_LOG("could not seed PRNG (ret = %d)\n", ret);
56                 exit(EXIT_FAILURE);
57         }
58         get_random_bytes_or_die((unsigned char *)&seed, sizeof(seed));
59         srandom(seed);
60 }
61
62 static EVP_PKEY *load_key(const char *file, int private)
63 {
64         BIO *key;
65         EVP_PKEY *pkey = NULL;
66
67         key = BIO_new(BIO_s_file());
68         if (!key)
69                 return NULL;
70         if (BIO_read_filename(key, file) > 0) {
71                 if (private == LOAD_PRIVATE_KEY)
72                         pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, NULL);
73                 else
74                         pkey = PEM_read_bio_PUBKEY(key, NULL, NULL, NULL);
75         }
76         BIO_free(key);
77         return pkey;
78 }
79
80 /**
81  * read an RSA key from a file
82  *
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
86  *
87  * \return The size of the RSA key on success, negative on errors.
88  *
89  * \sa openssl(1), rsa(1).
90  */
91 int get_rsa_key(char *key_file, RSA **rsa, int private)
92 {
93         EVP_PKEY *key = load_key(key_file, private);
94
95         if (!key)
96                 return (private == LOAD_PRIVATE_KEY)? -E_PRIVATE_KEY
97                         : -E_PUBLIC_KEY;
98         *rsa = EVP_PKEY_get1_RSA(key);
99         EVP_PKEY_free(key);
100         if (!*rsa)
101                 return -E_RSA;
102         return RSA_size(*rsa);
103 }
104
105 /**
106  * free an RSA structure
107  *
108  * \param rsa pointer to the RSA struct to free
109  *
110  * This must be called for any key obtained by get_rsa_key().
111  */
112 void rsa_free(RSA *rsa)
113 {
114         if (rsa)
115                 RSA_free(rsa);
116 }
117
118 /**
119  * decrypt a buffer using an RSA key
120  *
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
125  *
126  * The \a outbuf must be large enough to hold at least \a rsa_inlen bytes.
127  *
128  * \return The size of the recovered plaintext on success, negative on errors.
129  *
130  * \sa RSA_private_decrypt(3)
131  **/
132 int para_decrypt_buffer(char *key_file, unsigned char *outbuf, unsigned char *inbuf,
133                         unsigned rsa_inlen)
134 {
135         RSA *rsa;
136         int ret, inlen = rsa_inlen;
137
138         if (inlen < 0)
139                 return -E_RSA;
140         ret = get_rsa_key(key_file, &rsa, LOAD_PRIVATE_KEY);
141         if (ret < 0)
142                 return ret;
143         ret = RSA_private_decrypt(inlen, inbuf, outbuf, rsa, RSA_PKCS1_PADDING);
144         rsa_free(rsa);
145         return (ret > 0)? ret : -E_DECRYPT;
146 }
147
148 /**
149  * decrypt the challenge number sent by para_server
150  *
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
155  *
156  * \return positive on success, negative on errors
157  *
158  * \sa para_decrypt_buffer()
159  */
160 int para_decrypt_challenge(char *key_file, long unsigned *challenge_nr,
161                 unsigned char *inbuf, unsigned rsa_inlen)
162 {
163         unsigned char *rsa_out = OPENSSL_malloc(rsa_inlen + 1);
164         int ret = para_decrypt_buffer(key_file, rsa_out, inbuf, rsa_inlen);
165
166         if (ret >= 0) {
167                 rsa_out[ret] = '\0';
168                 ret = sscanf((char *)rsa_out, "%lu", challenge_nr) == 1?
169                         1 : -E_CHALLENGE;
170         }
171         OPENSSL_free(rsa_out);
172         return ret;
173 }
174
175 /**
176  * encrypt a buffer using an RSA key
177  *
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
182  *
183  * \return The size of the encrypted data on success, negative on errors
184  *
185  * \sa RSA_public_encrypt(3)
186  */
187 int para_encrypt_buffer(RSA *rsa, unsigned char *inbuf,
188                 unsigned len, unsigned char *outbuf)
189 {
190         int ret, flen = len; /* RSA_public_encrypt expects a signed int */
191
192         if (flen < 0)
193                 return -E_ENCRYPT;
194         ret = RSA_public_encrypt(flen, inbuf, outbuf, rsa, RSA_PKCS1_PADDING);
195         return ret < 0?  -E_ENCRYPT : ret;
196 }
197
198 /**
199  * encrypt the given challenge number
200  *
201  * \param rsa: public rsa key
202  * \param challenge_nr the number to be encrypted
203  * \param outbuf the output buffer
204  *
205  * \a outbuf must be at least 64 bytes long
206  *
207  * \return The size of the encrypted data on success, negative on errors
208  *
209  * \sa para_encrypt_buffer()
210  *
211  */
212 int para_encrypt_challenge(RSA* rsa, long unsigned challenge_nr,
213         unsigned char *outbuf)
214 {
215         unsigned char *inbuf = (unsigned char*) make_message("%lu", challenge_nr);
216         int ret = para_encrypt_buffer(rsa, inbuf, strlen((char *)inbuf), outbuf);
217         free(inbuf);
218         return ret;
219 }
220