2 rsa.c -- RSA key handling
3 Copyright (C) 2007-2012 Guus Sliepen <guus@tinc-vpn.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 // BER decoding functions
31 static int ber_read_id(unsigned char **p, size_t *buflen) {
36 if((**p & 0x1f) == 0x1f) {
43 more = *(*p)++ & 0x80;
54 return *(*p)++ & 0x1f;
58 static size_t ber_read_len(unsigned char **p, size_t *buflen) {
65 int len = *(*p)++ & 0x7f;
73 result = (size_t)(result << 8);
86 static bool ber_read_sequence(unsigned char **p, size_t *buflen, size_t *result) {
87 int tag = ber_read_id(p, buflen);
88 size_t len = ber_read_len(p, buflen);
101 static bool ber_read_mpi(unsigned char **p, size_t *buflen, gcry_mpi_t *mpi) {
102 int tag = ber_read_id(p, buflen);
103 size_t len = ber_read_len(p, buflen);
104 gcry_error_t err = 0;
106 if(tag != 0x02 || len > *buflen) {
111 err = gcry_mpi_scan(mpi, GCRYMPI_FMT_USG, *p, len, NULL);
117 return mpi ? !err : true;
120 rsa_t *rsa_set_hex_public_key(char *n, char *e) {
121 rsa_t *rsa = xzalloc(sizeof(rsa_t));
123 gcry_error_t err = gcry_mpi_scan(&rsa->n, GCRYMPI_FMT_HEX, n, 0, NULL)
124 ? : gcry_mpi_scan(&rsa->e, GCRYMPI_FMT_HEX, e, 0, NULL);
127 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading RSA public key: %s", gcry_strerror(errno));
135 rsa_t *rsa_set_hex_private_key(char *n, char *e, char *d) {
136 rsa_t *rsa = xzalloc(sizeof(rsa_t));
138 gcry_error_t err = gcry_mpi_scan(&rsa->n, GCRYMPI_FMT_HEX, n, 0, NULL)
139 ? : gcry_mpi_scan(&rsa->e, GCRYMPI_FMT_HEX, e, 0, NULL)
140 ? : gcry_mpi_scan(&rsa->d, GCRYMPI_FMT_HEX, d, 0, NULL);
143 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading RSA public key: %s", gcry_strerror(errno));
153 rsa_t *rsa_read_pem_public_key(FILE *fp) {
154 uint8_t derbuf[8096], *derp = derbuf;
157 if(!pem_decode(fp, "RSA PUBLIC KEY", derbuf, sizeof(derbuf), &derlen)) {
158 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read RSA public key: %s", strerror(errno));
162 rsa_t *rsa = xzalloc(sizeof(rsa_t));
164 if(!ber_read_sequence(&derp, &derlen, NULL)
165 || !ber_read_mpi(&derp, &derlen, &rsa->n)
166 || !ber_read_mpi(&derp, &derlen, &rsa->e)
168 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decoding RSA public key");
176 rsa_t *rsa_read_pem_private_key(FILE *fp) {
177 uint8_t derbuf[8096], *derp = derbuf;
180 if(!pem_decode(fp, "RSA PRIVATE KEY", derbuf, sizeof(derbuf), &derlen)) {
181 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read RSA private key: %s", strerror(errno));
185 rsa_t *rsa = xzalloc(sizeof(rsa_t));
187 if(!ber_read_sequence(&derp, &derlen, NULL)
188 || !ber_read_mpi(&derp, &derlen, NULL)
189 || !ber_read_mpi(&derp, &derlen, &rsa->n)
190 || !ber_read_mpi(&derp, &derlen, &rsa->e)
191 || !ber_read_mpi(&derp, &derlen, &rsa->d)
192 || !ber_read_mpi(&derp, &derlen, NULL) // p
193 || !ber_read_mpi(&derp, &derlen, NULL) // q
194 || !ber_read_mpi(&derp, &derlen, NULL)
195 || !ber_read_mpi(&derp, &derlen, NULL)
196 || !ber_read_mpi(&derp, &derlen, NULL) // u
198 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decoding RSA private key");
206 size_t rsa_size(rsa_t *rsa) {
207 return (gcry_mpi_get_nbits(rsa->n) + 7) / 8;
210 /* Well, libgcrypt has functions to handle RSA keys, but they suck.
211 * So we just use libgcrypt's mpi functions, and do the math ourselves.
214 // TODO: get rid of this macro, properly clean up gcry_ structures after use
215 #define check(foo) { gcry_error_t err = (foo); if(err) {logger(DEBUG_ALWAYS, LOG_ERR, "gcrypt error %s/%s at %s:%d", gcry_strsource(err), gcry_strerror(err), __FILE__, __LINE__); return false; }}
217 bool rsa_public_encrypt(rsa_t *rsa, void *in, size_t len, void *out) {
219 check(gcry_mpi_scan(&inmpi, GCRYMPI_FMT_USG, in, len, NULL));
221 gcry_mpi_t outmpi = gcry_mpi_new(len * 8);
222 gcry_mpi_powm(outmpi, inmpi, rsa->e, rsa->n);
224 size_t out_bytes = (gcry_mpi_get_nbits(outmpi) + 7) / 8;
225 size_t pad = len - MIN(out_bytes, len);
231 check(gcry_mpi_print(GCRYMPI_FMT_USG, out, len, NULL, outmpi));
236 bool rsa_private_decrypt(rsa_t *rsa, void *in, size_t len, void *out) {
238 check(gcry_mpi_scan(&inmpi, GCRYMPI_FMT_USG, in, len, NULL));
240 gcry_mpi_t outmpi = gcry_mpi_new(len * 8);
241 gcry_mpi_powm(outmpi, inmpi, rsa->d, rsa->n);
243 size_t pad = len - (gcry_mpi_get_nbits(outmpi) + 7) / 8;
249 check(gcry_mpi_print(GCRYMPI_FMT_USG, out, len, NULL, outmpi));
254 void rsa_free(rsa_t *rsa) {
257 gcry_mpi_release(rsa->n);
261 gcry_mpi_release(rsa->e);
265 gcry_mpi_release(rsa->d);