2 * Copyright (C) 2005-2007 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 */
14 static EVP_PKEY *load_key(const char *file, int private)
17 EVP_PKEY *pkey = NULL;
19 key = BIO_new(BIO_s_file());
22 if (BIO_read_filename(key, file) > 0) {
23 if (private == LOAD_PRIVATE_KEY)
24 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, NULL);
26 pkey = PEM_read_bio_PUBKEY(key, NULL, NULL, NULL);
33 * read an RSA key from a file
35 * \param key_file the file containing the key
36 * \param rsa RSA structure is returned here
37 * \param private if non-zero, read the private key, otherwise the public key
39 * \return The size of the RSA key on success, negative on errors.
41 * \sa openssl(1), rsa(1).
43 int get_rsa_key(char *key_file, RSA **rsa, int private)
45 EVP_PKEY *key = load_key(key_file, private);
48 return (private == LOAD_PRIVATE_KEY)? -E_PRIVATE_KEY
50 *rsa = EVP_PKEY_get1_RSA(key);
54 return RSA_size(*rsa);
58 * free an RSA structure
60 * \param rsa pointer to the RSA struct to free
62 * This must be called for any key obtained by get_rsa_key().
64 void rsa_free(RSA *rsa)
71 * decrypt a buffer using an RSA key
73 * \param key_file full path of the rsa key
74 * \param outbuf the output buffer
75 * \param inbuf the encrypted input buffer
76 * \param rsa_inlen the length of \a inbuf
78 * The \a outbuf must be large enough to hold at least \a rsa_inlen bytes.
80 * \return The size of the recovered plaintext on success, negative on errors.
82 * \sa RSA_private_decrypt(3)
84 int para_decrypt_buffer(char *key_file, unsigned char *outbuf, unsigned char *inbuf,
88 int ret, inlen = rsa_inlen;
92 ret = get_rsa_key(key_file, &rsa, LOAD_PRIVATE_KEY);
95 ret = RSA_private_decrypt(inlen, inbuf, outbuf, rsa, RSA_PKCS1_PADDING);
97 return (ret > 0)? ret : -E_DECRYPT;
101 * decrypt the challenge number sent by para_server
103 * \param key_file full path of the rsa key
104 * \param challenge_nr result is stored here
105 * \param inbuf the input buffer
106 * \param rsa_inlen the length of \a inbuf
108 * \return positive on success, negative on errors
110 * \sa para_decrypt_buffer()
112 int para_decrypt_challenge(char *key_file, long unsigned *challenge_nr,
113 unsigned char *inbuf, unsigned rsa_inlen)
115 unsigned char *rsa_out = OPENSSL_malloc(rsa_inlen + 1);
116 int ret = para_decrypt_buffer(key_file, rsa_out, inbuf, rsa_inlen);
120 ret = sscanf((char *)rsa_out, "%lu", challenge_nr) == 1?
123 OPENSSL_free(rsa_out);
128 * encrypt a buffer using an RSA key
130 * \param rsa: public rsa key
131 * \param inbuf the input buffer
132 * \param len the length of \a inbuf
133 * \param outbuf the output buffer
135 * \return The size of the encrypted data on success, negative on errors
137 * \sa RSA_public_encrypt(3)
139 int para_encrypt_buffer(RSA *rsa, unsigned char *inbuf,
140 unsigned len, unsigned char *outbuf)
142 int ret, flen = len; /* RSA_public_encrypt expects a signed int */
146 ret = RSA_public_encrypt(flen, inbuf, outbuf, rsa, RSA_PKCS1_PADDING);
147 return ret < 0? -E_ENCRYPT : ret;
151 * encrypt the given challenge number
153 * \param rsa: public rsa key
154 * \param challenge_nr the number to be encrypted
155 * \param outbuf the output buffer
157 * \a outbuf must be at least 64 bytes long
159 * \return The size of the encrypted data on success, negative on errors
161 * \sa para_encrypt_buffer()
164 int para_encrypt_challenge(RSA* rsa, long unsigned challenge_nr,
165 unsigned char *outbuf)
167 unsigned char *inbuf = (unsigned char*) make_message("%lu", challenge_nr);
168 int ret = para_encrypt_buffer(rsa, inbuf, strlen((char *)inbuf), outbuf);