2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file crypt.c Openssl-based 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>
15 #include <openssl/pem.h>
16 #include <openssl/sha.h>
17 #include <openssl/bn.h>
18 #include <openssl/aes.h>
25 #include "crypt_backend.h"
28 struct asymmetric_key {
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 * Read 64 bytes from /dev/urandom and adds them to the SSL PRNG. Seed the PRNG
46 * used by random() with a random seed obtained from SSL. If /dev/random is not
47 * readable the function calls exit().
49 * \sa RAND_load_file(3), \ref get_random_bytes_or_die(), srandom(3),
50 * random(3), \ref para_random().
52 void init_random_seed_or_die(void)
54 int seed, ret = RAND_load_file("/dev/urandom", 64);
57 PARA_EMERG_LOG("could not seed PRNG (ret = %d)\n", ret);
60 get_random_bytes_or_die((unsigned char *)&seed, sizeof(seed));
64 static EVP_PKEY *load_key(const char *file, int private)
67 EVP_PKEY *pkey = NULL;
68 int ret = check_key_file(file, private);
71 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
74 key = BIO_new(BIO_s_file());
77 if (BIO_read_filename(key, file) > 0) {
78 if (private == LOAD_PRIVATE_KEY)
79 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, NULL);
81 pkey = PEM_read_bio_PUBKEY(key, NULL, NULL, NULL);
87 static int get_openssl_key(const char *key_file, RSA **rsa, int private)
89 EVP_PKEY *key = load_key(key_file, private);
92 return (private == LOAD_PRIVATE_KEY)? -E_PRIVATE_KEY
94 *rsa = EVP_PKEY_get1_RSA(key);
98 return RSA_size(*rsa);
102 * The public key loading functions below were inspired by corresponding code
103 * of openssh-5.2p1, Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo,
104 * Finland. However, not much of the original code remains.
107 static int read_bignum(const unsigned char *buf, size_t len, BIGNUM **result)
109 const unsigned char *p = buf, *end = buf + len;
117 bnsize = read_ssh_u32(p);
118 PARA_DEBUG_LOG("bnsize: %u\n", bnsize);
122 if (p + bnsize > end)
126 bn = BN_bin2bn(p, bnsize, NULL);
133 static int read_rsa_bignums(const unsigned char *blob, int blen, RSA **result)
137 const unsigned char *p = blob, *end = blob + blen;
142 ret = read_bignum(p, end - p, &rsa->e);
146 ret = read_bignum(p, end - p, &rsa->n);
156 int get_asymmetric_key(const char *key_file, int private,
157 struct asymmetric_key **result)
159 struct asymmetric_key *key = NULL;
161 unsigned char *blob = NULL;
162 size_t map_size, encoded_size, decoded_size;
166 key = para_malloc(sizeof(*key));
168 ret = get_openssl_key(key_file, &key->rsa, LOAD_PRIVATE_KEY);
171 ret = mmap_full_file(key_file, O_RDONLY, &map, &map_size, NULL);
174 ret = is_ssh_rsa_key(map, map_size);
176 ret = para_munmap(map, map_size);
180 ret = get_openssl_key(key_file, &key->rsa, LOAD_PUBLIC_KEY);
184 encoded_size = map_size - ret;
185 PARA_INFO_LOG("decoding public rsa-ssh key %s\n", key_file);
186 ret = uudecode(cp, encoded_size, (char **)&blob, &decoded_size);
189 ret = check_ssh_key_header(blob, decoded_size);
192 ret = read_rsa_bignums(blob + ret, decoded_size - ret, &key->rsa);
195 ret = RSA_size(key->rsa);
197 ret2 = para_munmap(map, map_size);
198 if (ret >= 0 && ret2 < 0)
204 PARA_ERROR_LOG("key %s: %s\n", key_file, para_strerror(-ret));
211 void free_asymmetric_key(struct asymmetric_key *key)
219 int priv_decrypt(const char *key_file, unsigned char *outbuf,
220 unsigned char *inbuf, int inlen)
222 struct asymmetric_key *priv;
227 ret = get_asymmetric_key(key_file, LOAD_PRIVATE_KEY, &priv);
231 * RSA is vulnerable to timing attacks. Generate a random blinding
232 * factor to protect against this kind of attack.
235 if (RSA_blinding_on(priv->rsa, NULL) == 0)
237 ret = RSA_private_decrypt(inlen, inbuf, outbuf, priv->rsa,
238 RSA_PKCS1_OAEP_PADDING);
239 RSA_blinding_off(priv->rsa);
243 free_asymmetric_key(priv);
247 int pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
248 unsigned len, unsigned char *outbuf)
250 int ret, flen = len; /* RSA_public_encrypt expects a signed int */
254 ret = RSA_public_encrypt(flen, inbuf, outbuf, pub->rsa,
255 RSA_PKCS1_OAEP_PADDING);
256 return ret < 0? -E_ENCRYPT : ret;
259 struct aes_ctr_128_context {
261 unsigned char ivec[AES_CRT128_BLOCK_SIZE];
262 unsigned char ecount[AES_CRT128_BLOCK_SIZE];
266 struct stream_cipher {
270 struct aes_ctr_128_context aes;
274 struct stream_cipher *sc_new(const unsigned char *data, int len,
278 struct stream_cipher *sc = para_malloc(sizeof(*sc));
279 struct aes_ctr_128_context *aes;
281 sc->use_aes = use_aes;
283 RC4_set_key(&sc->context.rc4_key, len, data);
286 assert(len >= 2 * AES_CRT128_BLOCK_SIZE);
287 aes = &sc->context.aes;
288 ret = AES_set_encrypt_key(data, AES_CRT128_BLOCK_SIZE * 8 /* bits */,
291 memcpy(aes->ivec, data + AES_CRT128_BLOCK_SIZE, AES_CRT128_BLOCK_SIZE);
296 void sc_free(struct stream_cipher *sc)
302 * The RC4() implementation of openssl apparently reads and writes data in
303 * blocks of 8 bytes. So we have to make sure our buffer sizes are a multiple
308 static void rc4_crypt(RC4_KEY *key, struct iovec *src, struct iovec *dst)
310 size_t len = src->iov_len, l1, l2;
313 assert(len < ((typeof(src->iov_len))-1) / 2);
314 l1 = ROUND_DOWN(len, RC4_ALIGN);
315 l2 = ROUND_UP(len, RC4_ALIGN);
317 *dst = (typeof(*dst)) {
318 /* Add one for the terminating zero byte. */
319 .iov_base = para_malloc(l2 + 1),
322 RC4(key, l1, src->iov_base, dst->iov_base);
324 unsigned char remainder[RC4_ALIGN] = "";
325 memcpy(remainder, src->iov_base + l1, len - l1);
326 RC4(key, len - l1, remainder, dst->iov_base + l1);
328 ((char *)dst->iov_base)[len] = '\0';
331 static void aes_ctr128_crypt(struct aes_ctr_128_context *aes, struct iovec *src,
334 size_t len = src->iov_len;
336 *dst = (typeof(*dst)) {
337 /* Add one for the terminating zero byte. */
338 .iov_base = para_malloc(len + 1),
341 AES_ctr128_encrypt(src->iov_base, dst->iov_base, len,
342 &aes->key, aes->ivec, aes->ecount, &aes->num);
343 ((char *)dst->iov_base)[len] = '\0';
346 void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
349 return aes_ctr128_crypt(&sc->context.aes, src, dst);
350 return rc4_crypt(&sc->context.rc4_key, src, dst);
353 void hash_function(const char *data, unsigned long len, unsigned char *hash)
357 SHA1_Update(&c, data, len);
358 SHA1_Final(hash, &c);