2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
19 /** \file crypt.c openssl-based RSA encryption/decryption routines */
21 #include <openssl/pem.h>
26 /** \cond used to distinguish between loading of private/public key */
27 #define LOAD_PUBLIC_KEY 0
28 #define LOAD_PRIVATE_KEY 1
31 static EVP_PKEY
*load_key(const char *file
, int private)
34 EVP_PKEY
*pkey
= NULL
;
36 key
= BIO_new(BIO_s_file());
39 if (BIO_read_filename(key
, file
) > 0) {
40 if (private == LOAD_PRIVATE_KEY
)
41 pkey
= PEM_read_bio_PrivateKey(key
, NULL
, NULL
, NULL
);
43 pkey
= PEM_read_bio_PUBKEY(key
, NULL
, NULL
, NULL
);
50 int get_rsa_key(char *key_file
, RSA
**rsa
, int private)
52 EVP_PKEY
*key
= load_key(key_file
, private);
55 return (private == LOAD_PRIVATE_KEY
)? -E_PRIVATE_KEY
: -E_PUBLIC_KEY
;
56 *rsa
= EVP_PKEY_get1_RSA(key
);
60 return RSA_size(*rsa
);
64 * decrypt a buffer using an RSA key
66 * \param key_file full path of the rsa key
67 * \param outbuf the output buffer
68 * \param inbuf the encrypted input buffer
69 * \param rsa_inlen the length of \a inbuf
71 * The \a outbuf must be large enough to hold at least \a rsa_inlen bytes.
73 * \return The size of the recovered plaintext on success, negative on errors.
75 * \sa RSA_private_decrypt(3)
77 int para_decrypt_buffer(char *key_file
, unsigned char *outbuf
, unsigned char *inbuf
,
81 int ret
= get_rsa_key(key_file
, &rsa
, LOAD_PRIVATE_KEY
);
85 ret
= RSA_private_decrypt(rsa_inlen
, inbuf
, outbuf
, rsa
, RSA_PKCS1_PADDING
);
86 return (ret
> 0)? ret
: -E_DECRYPT
;
90 * decrypt the challenge number sent by para_server
92 * \param key_file full path of the rsa key
93 * \param challenge_nr result is stored here
94 * \param inbuf the input buffer
95 * \param rsa_inlen the length of \a inbuf
97 * \return positive on success, negative on errors
99 * \sa para_decrypt_buffer()
101 int para_decrypt_challenge(char *key_file
, long unsigned *challenge_nr
,
102 unsigned char *inbuf
, int rsa_inlen
)
104 unsigned char *rsa_out
= OPENSSL_malloc(rsa_inlen
+ 1);
105 int ret
= para_decrypt_buffer(key_file
, rsa_out
, inbuf
, rsa_inlen
);
109 ret
= sscanf((char *)rsa_out
, "%lu", challenge_nr
) == 1?
112 OPENSSL_free(rsa_out
);
117 * encrypt a buffer using an RSA key
119 * \param rsa: public rsa key
120 * \param inbuf the input buffer
121 * \param len the length of \a inbuf
122 * \param outbuf the output buffer
124 * \return The size of the encrypted data on success, negative on errors
126 * \sa RSA_public_encrypt(3)
128 int para_encrypt_buffer(RSA
*rsa
, unsigned char *inbuf
,
129 const unsigned len
, unsigned char *outbuf
)
131 int ret
= RSA_public_encrypt(len
, inbuf
, outbuf
, rsa
,
133 return ret
< 0? -E_ENCRYPT
: ret
;
137 * encrypt the given challenge number
139 * \param rsa: public rsa key
140 * \param challenge_nr the number to be encrypted
141 * \param outbuf the output buffer
143 * \a outbuf must be at least 64 bytes long
145 * \return The size of the encrypted data on success, negative on errors
147 * \sa para_encrypt_buffer()
150 int para_encrypt_challenge(RSA
* rsa
, long unsigned challenge_nr
,
151 unsigned char *outbuf
)
153 unsigned char *inbuf
= (unsigned char*) make_message("%lu", challenge_nr
);
154 int ret
= para_encrypt_buffer(rsa
, inbuf
, strlen((char *)inbuf
), outbuf
);