add more missing documentation
[paraslash.git] / crypt.c
1 /*
2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file crypt.c openssl-based RSA encryption/decryption routines */
20
21 #include "para.h"
22 #include "error.h"
23 #include "string.h"
24 #include "crypt.h"
25
26 static EVP_PKEY *load_key(const char *file, int private)
27 {
28 BIO *key;
29 EVP_PKEY *pkey = NULL;
30
31 key = BIO_new(BIO_s_file());
32 if (!key)
33 return NULL;
34 if (BIO_read_filename(key, file) > 0) {
35 if (private == LOAD_PRIVATE_KEY)
36 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, NULL);
37 else
38 pkey = PEM_read_bio_PUBKEY(key, NULL, NULL, NULL);
39 }
40 BIO_free(key);
41 return pkey;
42 }
43
44 /**
45 * read an RSA key from a file
46 *
47 * \param key_file the file containing the key
48 * \param rsa RSA structure is returned here
49 * \param private if non-zero, read the private key, otherwise the public key
50 *
51 * \return The size of the RSA key on success, negative on errors.
52 *
53 * \sa openssl(1), rsa(1).
54 */
55 int get_rsa_key(char *key_file, RSA **rsa, int private)
56 {
57 EVP_PKEY *key = load_key(key_file, private);
58
59 if (!key)
60 return (private == LOAD_PRIVATE_KEY)? -E_PRIVATE_KEY : -E_PUBLIC_KEY;
61 *rsa = EVP_PKEY_get1_RSA(key);
62 EVP_PKEY_free(key);
63 if (!*rsa)
64 return -E_RSA;
65 return RSA_size(*rsa);
66 }
67
68 /**
69 * decrypt a buffer using an RSA key
70 *
71 * \param key_file full path of the rsa key
72 * \param outbuf the output buffer
73 * \param inbuf the encrypted input buffer
74 * \param rsa_inlen the length of \a inbuf
75 *
76 * The \a outbuf must be large enough to hold at least \a rsa_inlen bytes.
77 *
78 * \return The size of the recovered plaintext on success, negative on errors.
79 *
80 * \sa RSA_private_decrypt(3)
81 **/
82 int para_decrypt_buffer(char *key_file, unsigned char *outbuf, unsigned char *inbuf,
83 int rsa_inlen)
84 {
85 RSA *rsa;
86 int ret = get_rsa_key(key_file, &rsa, LOAD_PRIVATE_KEY);
87
88 if (ret < 0)
89 return ret;
90 ret = RSA_private_decrypt(rsa_inlen, inbuf, outbuf, rsa, RSA_PKCS1_PADDING);
91 return (ret > 0)? ret : -E_DECRYPT;
92 }
93
94 /**
95 * decrypt the challenge number sent by para_server
96 *
97 * \param key_file full path of the rsa key
98 * \param challenge_nr result is stored here
99 * \param inbuf the input buffer
100 * \param rsa_inlen the length of \a inbuf
101 *
102 * \return positive on success, negative on errors
103 *
104 * \sa para_decrypt_buffer()
105 */
106 int para_decrypt_challenge(char *key_file, long unsigned *challenge_nr,
107 unsigned char *inbuf, int rsa_inlen)
108 {
109 unsigned char *rsa_out = OPENSSL_malloc(rsa_inlen + 1);
110 int ret = para_decrypt_buffer(key_file, rsa_out, inbuf, rsa_inlen);
111
112 if (ret >= 0) {
113 rsa_out[ret] = '\0';
114 ret = sscanf((char *)rsa_out, "%lu", challenge_nr) == 1?
115 1 : -E_CHALLENGE;
116 }
117 OPENSSL_free(rsa_out);
118 return ret;
119 }
120
121 /**
122 * encrypt a buffer using an RSA key
123 *
124 * \param rsa: public rsa key
125 * \param inbuf the input buffer
126 * \param len the length of \a inbuf
127 * \param outbuf the output buffer
128 *
129 * \return The size of the encrypted data on success, negative on errors
130 *
131 * \sa RSA_public_encrypt(3)
132 */
133 int para_encrypt_buffer(RSA *rsa, unsigned char *inbuf,
134 const unsigned len, unsigned char *outbuf)
135 {
136 int ret = RSA_public_encrypt(len, inbuf, outbuf, rsa,
137 RSA_PKCS1_PADDING);
138 return ret < 0? -E_ENCRYPT : ret;
139 }
140
141 /**
142 * encrypt the given challenge number
143 *
144 * \param rsa: public rsa key
145 * \param challenge_nr the number to be encrypted
146 * \param outbuf the output buffer
147 *
148 * \a outbuf must be at least 64 bytes long
149 *
150 * \return The size of the encrypted data on success, negative on errors
151 *
152 * \sa para_encrypt_buffer()
153 *
154 */
155 int para_encrypt_challenge(RSA* rsa, long unsigned challenge_nr,
156 unsigned char *outbuf)
157 {
158 unsigned char *inbuf = (unsigned char*) make_message("%lu", challenge_nr);
159 int ret = para_encrypt_buffer(rsa, inbuf, strlen((char *)inbuf), outbuf);
160 free(inbuf);
161 return ret;
162 }
163