2 rsa.c -- RSA key handling
3 Copyright (C) 2007-2022 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.
20 #include "../system.h"
28 #include "../logger.h"
30 #include "../xalloc.h"
32 // BER decoding functions
34 static int ber_read_id(unsigned char **p, size_t *buflen) {
39 if((**p & 0x1f) == 0x1f) {
46 more = *(*p)++ & 0x80;
57 return *(*p)++ & 0x1f;
61 static size_t ber_read_len(unsigned char **p, size_t *buflen) {
68 size_t len = *(*p)++ & 0x7f;
76 result = (size_t)(result << 8);
88 static bool ber_skip_sequence(unsigned char **p, size_t *buflen) {
89 int tag = ber_read_id(p, buflen);
91 return tag == TAG_SEQUENCE &&
92 ber_read_len(p, buflen) > 0;
95 static bool ber_read_mpi(unsigned char **p, size_t *buflen, gcry_mpi_t *mpi) {
96 int tag = ber_read_id(p, buflen);
97 size_t len = ber_read_len(p, buflen);
100 if(tag != 0x02 || len > *buflen) {
105 err = gcry_mpi_scan(mpi, GCRYMPI_FMT_USG, *p, len, NULL);
111 return mpi ? !err : true;
114 rsa_t *rsa_new(void) {
115 return xzalloc(sizeof(rsa_t));
118 rsa_t *rsa_set_hex_public_key(const char *n, const char *e) {
119 rsa_t *rsa = rsa_new();
121 gcry_error_t err = gcry_mpi_scan(&rsa->n, GCRYMPI_FMT_HEX, n, 0, NULL);
124 err = gcry_mpi_scan(&rsa->e, GCRYMPI_FMT_HEX, e, 0, NULL);
128 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading RSA public key: %s", gcry_strerror(errno));
136 rsa_t *rsa_set_hex_private_key(const char *n, const char *e, const char *d) {
137 rsa_t *rsa = rsa_new();
139 gcry_error_t err = gcry_mpi_scan(&rsa->n, GCRYMPI_FMT_HEX, n, 0, NULL);
142 err = gcry_mpi_scan(&rsa->e, GCRYMPI_FMT_HEX, e, 0, NULL);
146 err = gcry_mpi_scan(&rsa->d, GCRYMPI_FMT_HEX, d, 0, NULL);
150 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading RSA public key: %s", gcry_strerror(errno));
160 rsa_t *rsa_read_pem_public_key(FILE *fp) {
161 uint8_t derbuf[8096], *derp = derbuf;
164 if(!pem_decode(fp, "RSA PUBLIC KEY", derbuf, sizeof(derbuf), &derlen)) {
165 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read RSA public key: %s", strerror(errno));
169 rsa_t *rsa = rsa_new();
171 if(!ber_skip_sequence(&derp, &derlen)
172 || !ber_read_mpi(&derp, &derlen, &rsa->n)
173 || !ber_read_mpi(&derp, &derlen, &rsa->e)
175 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decoding RSA public key");
183 rsa_t *rsa_read_pem_private_key(FILE *fp) {
184 uint8_t derbuf[8096], *derp = derbuf;
187 if(!pem_decode(fp, "RSA PRIVATE KEY", derbuf, sizeof(derbuf), &derlen)) {
188 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read RSA private key: %s", strerror(errno));
192 rsa_t *rsa = rsa_new();
194 if(!ber_skip_sequence(&derp, &derlen)
195 || !ber_read_mpi(&derp, &derlen, NULL)
196 || !ber_read_mpi(&derp, &derlen, &rsa->n)
197 || !ber_read_mpi(&derp, &derlen, &rsa->e)
198 || !ber_read_mpi(&derp, &derlen, &rsa->d)
199 || !ber_read_mpi(&derp, &derlen, NULL) // p
200 || !ber_read_mpi(&derp, &derlen, NULL) // q
201 || !ber_read_mpi(&derp, &derlen, NULL)
202 || !ber_read_mpi(&derp, &derlen, NULL)
203 || !ber_read_mpi(&derp, &derlen, NULL) // u
205 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decoding RSA private key");
210 memzero(derbuf, sizeof(derbuf));
214 size_t rsa_size(const rsa_t *rsa) {
215 return (gcry_mpi_get_nbits(rsa->n) + 7) / 8;
218 static bool check(gcry_error_t err) {
220 logger(DEBUG_ALWAYS, LOG_ERR, "gcrypt error %s/%s", gcry_strsource(err), gcry_strerror(err));
226 /* Well, libgcrypt has functions to handle RSA keys, but they suck.
227 * So we just use libgcrypt's mpi functions, and do the math ourselves.
230 static bool rsa_powm(const gcry_mpi_t ed, const gcry_mpi_t n, const void *in, size_t len, void *out) {
231 gcry_mpi_t inmpi = NULL;
233 if(!check(gcry_mpi_scan(&inmpi, GCRYMPI_FMT_USG, in, len, NULL))) {
237 gcry_mpi_t outmpi = gcry_mpi_snew(len * 8);
238 gcry_mpi_powm(outmpi, inmpi, ed, n);
240 size_t out_bytes = (gcry_mpi_get_nbits(outmpi) + 7) / 8;
241 size_t pad = len - MIN(out_bytes, len);
242 unsigned char *pout = out;
248 bool ok = check(gcry_mpi_print(GCRYMPI_FMT_USG, pout, len, NULL, outmpi));
250 gcry_mpi_release(outmpi);
251 gcry_mpi_release(inmpi);
256 bool rsa_public_encrypt(rsa_t *rsa, const void *in, size_t len, void *out) {
257 return rsa_powm(rsa->e, rsa->n, in, len, out);
260 bool rsa_private_decrypt(rsa_t *rsa, const void *in, size_t len, void *out) {
261 return rsa_powm(rsa->d, rsa->n, in, len, out);
264 void rsa_free(rsa_t *rsa) {
267 gcry_mpi_release(rsa->n);
271 gcry_mpi_release(rsa->e);
275 gcry_mpi_release(rsa->d);