Skip to content

Commit cbe8214

Browse files
committed
Add RSA key generation example
1 parent e7d48be commit cbe8214

4 files changed

Lines changed: 269 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ pk/ed25519_gen/gen_key_files
220220
pk/ed25519_gen/sign_and_verify
221221
pk/ecdh_generate_secret/ecdh_gen_secret
222222
pk/rsa-kg/rsa-kg-sv
223+
pk/rsa-kg/rsa-kg
223224
pk/dh-pg/dh-pg-ka
224225
pk/test_cert_and_private_keypair/test-cert-privkey-pair
225226
pk/srp/srp_gen

pk/rsa-kg/Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ CC=gcc
22
CFLAGS=-Wall
33
LIBS= -lwolfssl -lm
44

5-
all: rsa-kg-sv
5+
all: rsa-kg-sv rsa-kg
66

77
rsa-kg-sv.o: rsa-kg-sv.c rsa-key.h
88
$(CC) -c -o $@ $< $(CFLAGS)
99

1010
rsa-kg-sv: rsa-kg-sv.o
11-
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
11+
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
12+
13+
rsa-kg.o: rsa-kg.c
14+
$(CC) -c -o $@ $< $(CFLAGS)
15+
16+
rsa-kg: rsa-kg.o
17+
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
1218

1319
.PHONY: clean
1420

1521
clean:
16-
rm -f *.o rsa-kg-sv
22+
rm -f *.o rsa-kg-sv rsa-kg

pk/rsa-kg/README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ How to use rsa-kg-sv.c
1313
NOTE: on error, the key and digest will be displayed so that they can be
1414
copied into rsa-key.h replacing the existing values.
1515

16-
4) Running 'make clean' will delete the executable and object files.
16+
3) Running 'make clean' will delete the executable and object files.
1717

1818

19+
How to use rsa-kg.c
20+
21+
1) a. Compile wolfSSL with ./configure --enable-keygen, run
22+
'make', and then install by typing 'sudo make install'.
23+
b. In the pk/rsa-kg directory run the Makefile by typing 'make'.
24+
2) run the executable, for help run with -help. Basic command is as follows:
25+
26+
./rsa-kg <options>
27+
28+
Key sizes supported, in bits: 1024-4096.
29+
30+
Specify the name of the output files with -priv and -pub.
31+
32+
3) Running 'make clean' will delete the executable and object files.
33+

pk/rsa-kg/rsa-kg.c

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
/* rsa-kg.c
2+
*
3+
* Copyright (C) 2006-2020 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
/*
23+
* An implementation of RSA key generation using wolfSSL
24+
* Usage:
25+
./rsa-kg -priv <private key filename> -pub <public key filename>
26+
*/
27+
28+
#include <stdio.h>
29+
#include <string.h>
30+
31+
#include <wolfssl/options.h>
32+
#include <wolfssl/wolfcrypt/settings.h>
33+
#include <wolfssl/ssl.h>
34+
#include <wolfssl/wolfcrypt/rsa.h>
35+
#include <wolfssl/wolfcrypt/error-crypt.h>
36+
37+
#define MAX_DER_SIZE 2500
38+
#define MIN_RSA_KEY_SIZE 1024
39+
#define MAX_RSA_KEY_SIZE 4096
40+
#define DEF_RSA_KEY_SIZE 2048
41+
42+
static const char* kRsaPubKey = "./rsa-public.der";
43+
static const char* kRsaPrivKey = "./rsa-private.der";
44+
45+
/* Shows usage information */
46+
void usage()
47+
{
48+
fprintf(stderr, "rsa_kg <options>:\n");
49+
fprintf(stderr, " -bits <num> Size in bits of RSA keys generated\n");
50+
fprintf(stderr, " Range: 1024-4096\n");
51+
fprintf(stderr, " -priv <filename> Private key filename\n");
52+
fprintf(stderr, " -pub <filename> Public key filename\n");
53+
fprintf(stderr, "\n");
54+
}
55+
56+
int main(int argc, char** argv)
57+
{
58+
/* These examples require RSA and Key Gen */
59+
#if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
60+
RsaKey* pRsaKey = NULL;
61+
WC_RNG rng;
62+
int ret = 0;
63+
int bits = DEF_RSA_KEY_SIZE;
64+
int sz;
65+
unsigned char derBuf[MAX_DER_SIZE];
66+
FILE* f;
67+
const char* pubKey = kRsaPubKey;
68+
const char* privKey = kRsaPrivKey;
69+
70+
argc--;
71+
argv++;
72+
while (argc > 0) {
73+
/* Number of bits in RSA key to generate */
74+
if (XSTRNCMP(*argv, "-bits", 6) == 0) {
75+
++argv;
76+
if (--argc == 0) {
77+
fprintf(stderr, "Missing bits value\n");
78+
usage();
79+
return 1;
80+
}
81+
bits = atoi(*argv);
82+
}
83+
else if (XSTRNCMP(*argv, "-priv", 6) == 0) {
84+
++argv;
85+
if (--argc == 0) {
86+
fprintf(stderr, "Missing private key filename\n");
87+
usage();
88+
return 1;
89+
}
90+
privKey = *argv;
91+
}
92+
else if (XSTRNCMP(*argv, "-pub", 6) == 0) {
93+
++argv;
94+
if (--argc == 0) {
95+
fprintf(stderr, "Missing public key filename\n");
96+
usage();
97+
return 1;
98+
}
99+
pubKey = *argv;
100+
}
101+
else if (XSTRNCMP(*argv, "-help", 6) == 0) {
102+
usage();
103+
return 0;
104+
}
105+
else {
106+
fprintf(stderr, "Unrecognized option: %s\n", *argv);
107+
usage();
108+
return 1;
109+
}
110+
111+
argc--;
112+
argv++;
113+
}
114+
115+
/* Check bit count if generating keys */
116+
if ((bits < MIN_RSA_KEY_SIZE || bits > MAX_RSA_KEY_SIZE)) {
117+
fprintf(stderr, "Bits out of range (%d-%d): %d\n", MIN_RSA_KEY_SIZE,
118+
MAX_RSA_KEY_SIZE, bits);
119+
usage();
120+
return 1;
121+
}
122+
#ifdef WOLFSSL_SP_MATH
123+
if (0) {
124+
}
125+
#ifndef WOLFSSL_SP_NO_2048
126+
else if (bits == 2048) {
127+
}
128+
#endif
129+
#ifndef WOLFSSL_SP_NO_3072
130+
else if (bits == 3072) {
131+
}
132+
#endif
133+
#ifdef WOLFSSL_SP_4096
134+
else if (bits == 4096) {
135+
}
136+
#endif
137+
else {
138+
fprintf(stderr, "Bit size not supported with SP_MATH: %d\n", bits);
139+
fprintf(stderr, " wolfSSL compiled to support, in bits:");
140+
#ifndef WOLFSSL_SP_NO_2048
141+
fprintf(stderr, " 2048");
142+
#endif
143+
#ifndef WOLFSSL_SP_NO_3072
144+
fprintf(stderr, " 3072");
145+
#endif
146+
#ifdef WOLFSSL_SP_4096
147+
fprintf(stderr, " 4096");
148+
#endif
149+
fprintf(stderr, "\n");
150+
return 1;
151+
}
152+
#endif
153+
154+
wolfSSL_Debugging_ON();
155+
156+
wolfSSL_Init();
157+
158+
/* Allocate space for RSA key object. */
159+
pRsaKey = malloc(sizeof(RsaKey));
160+
if (!pRsaKey) {
161+
printf("RSA_generate_key failed with error\n");
162+
return MEMORY_E;
163+
}
164+
165+
/* Create a random number generator for key generation. */
166+
ret = wc_InitRng(&rng);
167+
if (ret != 0) {
168+
printf("Init RNG failed %d\n", ret);
169+
free(pRsaKey);
170+
return ret;
171+
}
172+
173+
/* Initialize RSA key object. */
174+
ret = wc_InitRsaKey(pRsaKey, NULL);
175+
if (ret != 0) {
176+
printf("Init RSA key failed %d\n", ret);
177+
wc_FreeRng(&rng);
178+
free(pRsaKey);
179+
return ret;
180+
}
181+
182+
printf("Generating RSA key\n");
183+
184+
/* Generate an RSA key pair. */
185+
if (wc_MakeRsaKey(pRsaKey, bits, WC_RSA_EXPONENT, &rng) != 0) {
186+
printf("failed to create rsa key\n");
187+
}
188+
else {
189+
/* Open public key file. */
190+
f = fopen(pubKey, "wb");
191+
printf("writing public key to %s\n", pubKey);
192+
if (f == NULL) {
193+
printf("unable to write out public key\n");
194+
}
195+
else {
196+
/* Encode public key to DER. */
197+
sz = wc_RsaKeyToPublicDer(pRsaKey, derBuf, sizeof(derBuf));
198+
if (sz <= 0) {
199+
printf("error with rsa to public der %d\n", sz);
200+
}
201+
else {
202+
/* Write DER encoded public key to file. */
203+
fwrite(derBuf, 1, sz, f);
204+
}
205+
fclose(f);
206+
}
207+
208+
/* Open private key file. */
209+
f = fopen(privKey, "wb");
210+
printf("writing public key to %s\n", privKey);
211+
if (f == NULL) {
212+
printf("unable to write out public key\n");
213+
}
214+
else {
215+
/* Encode private key to DER. */
216+
sz = wc_RsaKeyToDer(pRsaKey, derBuf, sizeof(derBuf));
217+
if (sz <= 0) {
218+
printf("error with rsa to public der %d\n", sz);
219+
}
220+
else {
221+
/* Write DER encoded private key to file. */
222+
fwrite(derBuf, 1, sz, f);
223+
}
224+
fclose(f);
225+
}
226+
}
227+
228+
/* Dispose of allocated memory. */
229+
wc_FreeRsaKey(pRsaKey);
230+
wc_FreeRng(&rng);
231+
free(pRsaKey);
232+
wolfSSL_Cleanup();
233+
234+
return 0;
235+
#else
236+
(void)kRsaPubKey;
237+
(void)kRsaPrivKey;
238+
239+
printf("wolfSSL missing build features.\n");
240+
printf("Please build using `./configure --enable-keygen`\n");
241+
return 1;
242+
#endif
243+
}

0 commit comments

Comments
 (0)