2 rsagen.c -- RSA key generation and export
3 Copyright (C) 2008 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.
27 // Base64 encoding table
29 static const char b64e[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33 static bool pem_encode(FILE *fp, const char *header, uint8_t *buf, size_t size) {
40 fprintf(fp, "-----BEGIN %s-----\n", header);
42 for(i = 0; i < size; i += 3) {
44 word = buf[i] << 16 | buf[i + 1] << 8 | buf[i + 2];
48 word |= buf[i + 1] << 8;
51 line[j++] = b64e[(word >> 18) ];
52 line[j++] = b64e[(word >> 12) & 0x3f];
53 line[j++] = b64e[(word >> 6) & 0x3f];
54 line[j++] = b64e[(word ) & 0x3f];
76 fprintf(fp, "-----END %s-----\n", header);
82 // BER encoding functions
84 static bool ber_write_id(uint8_t **p, size_t *buflen, int id) {
108 static bool ber_write_len(uint8_t **p, size_t *buflen, size_t len) {
124 static bool ber_write_sequence(uint8_t **p, size_t *buflen, uint8_t *seqbuf, size_t seqlen) {
125 if(!ber_write_id(p, buflen, 0x10) || !ber_write_len(p, buflen, seqlen) || *buflen < seqlen)
128 memcpy(*p, seqbuf, seqlen);
135 static bool ber_write_mpi(uint8_t **p, size_t *buflen, gcry_mpi_t mpi) {
136 uint8_t tmpbuf[1024];
137 size_t tmplen = sizeof tmpbuf;
140 err = gcry_mpi_aprint(GCRYMPI_FMT_USG, &tmpbuf, &tmplen, mpi);
144 if(!ber_write_id(p, buflen, 0x02) || !ber_write_len(p, buflen, tmplen) || *buflen < tmplen)
147 memcpy(*p, tmpbuf, tmplen);
154 // Write PEM RSA keys
156 bool rsa_write_pem_public_key(rsa_t *rsa, FILE *fp) {
157 uint8_t derbuf1[8096];
158 uint8_t derbuf2[8096];
159 uint8_t *derp1 = derbuf1;
160 uint8_t *derp2 = derbuf2;
161 size_t derlen1 = sizeof derbuf1;
162 size_t derlen2 = sizeof derbuf2;
164 if(!ber_write_mpi(&derp1, &derlen1, &rsa->n)
165 || !ber_write_mpi(&derp1, &derlen1, &rsa->e)
166 || !ber_write_sequence(&derp2, &derlen2, derbuf1, derlen1)) {
167 logger(LOG_ERR, "Error while encoding RSA public key");
171 if(!pem_encode(fp, "RSA PUBLIC KEY", derbuf2, derlen2)) {
172 logger(LOG_ERR, "Unable to write RSA public key: %s", strerror(errno));
179 bool rsa_write_pem_private_key(rsa_t *rsa, FILE *fp) {
180 uint8_t derbuf1[8096];
181 uint8_t derbuf2[8096];
182 uint8_t *derp1 = derbuf1;
183 uint8_t *derp2 = derbuf2;
184 size_t derlen1 = sizeof derbuf1;
185 size_t derlen2 = sizeof derbuf2;
187 if(!ber_write_mpi(&derp1, &derlen1, &bits)
188 || ber_write_mpi(&derp1, &derlen1, &rsa->n) // modulus
189 || ber_write_mpi(&derp1, &derlen1, &rsa->e) // public exponent
190 || ber_write_mpi(&derp1, &derlen1, &rsa->d) // private exponent
191 || ber_write_mpi(&derp1, &derlen1, &p)
192 || ber_write_mpi(&derp1, &derlen1, &q)
193 || ber_write_mpi(&derp1, &derlen1, &exp1)
194 || ber_write_mpi(&derp1, &derlen1, &exp2)
195 || ber_write_mpi(&derp1, &derlen1, &coeff))
196 logger(LOG_ERR, "Error while encoding RSA private key");
200 if(!pem_encode(fp, "RSA PRIVATE KEY", derbuf2, derlen2)) {
201 logger(LOG_ERR, "Unable to write RSA private key: %s", strerror(errno));
209 bool rsa_write_pem_public_key(rsa_t *rsa, FILE *fp) {
213 bool rsa_write_pem_private_key(rsa_t *rsa, FILE *fp) {
217 bool rsa_generate(rsa_t *rsa, size_t bits, unsigned long exponent) {
218 fprintf(stderr, "Generating RSA keys with libgcrypt not implemented yet\n");