Skip to content

Commit 04165fc

Browse files
Merge pull request wolfSSL#823 from tjko/ed25519_keygen
Add ED25519 key generation support.
2 parents 447024f + d557c24 commit 04165fc

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

src/keygen.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,67 @@ int wolfSSH_MakeEcdsaKey(byte* out, word32 outSz, word32 size)
187187
}
188188

189189

190+
int wolfSSH_MakeEd25519Key(byte* out, word32 outSz, word32 size)
191+
{
192+
#ifndef WOLFSSH_NO_ED25519
193+
194+
int ret = WS_SUCCESS;
195+
WC_RNG rng;
196+
197+
WLOG(WS_LOG_DEBUG, "Entering wolfSSH_MakeEd25519Key()");
198+
199+
if (wc_InitRng(&rng) != 0) {
200+
WLOG(WS_LOG_DEBUG, "Couldn't create RNG");
201+
ret = WS_CRYPTO_FAILED;
202+
}
203+
204+
if (ret == WS_SUCCESS) {
205+
ed25519_key key;
206+
207+
if (wc_ed25519_init(&key) != 0)
208+
ret = WS_CRYPTO_FAILED;
209+
210+
if (ret == WS_SUCCESS) {
211+
ret = wc_ed25519_make_key(&rng, size/8, &key);
212+
if (ret != 0) {
213+
WLOG(WS_LOG_DEBUG, "ED25519 key generation failed");
214+
ret = WS_CRYPTO_FAILED;
215+
}
216+
else
217+
ret = WS_SUCCESS;
218+
}
219+
220+
if (ret == WS_SUCCESS) {
221+
int keySz;
222+
223+
keySz = wc_Ed25519KeyToDer(&key, out, outSz);
224+
if (keySz < 0) {
225+
WLOG(WS_LOG_DEBUG, "ED25519 key to DER failed");
226+
ret = WS_CRYPTO_FAILED;
227+
}
228+
else
229+
ret = keySz;
230+
}
231+
232+
wc_ed25519_free(&key);
233+
234+
if (wc_FreeRng(&rng) != 0) {
235+
WLOG(WS_LOG_DEBUG, "Couldn't free RNG");
236+
ret = WS_CRYPTO_FAILED;
237+
}
238+
}
239+
240+
WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_MakeEd25519Key(), ret = %d", ret);
241+
return ret;
242+
#else
243+
WOLFSSH_UNUSED(out);
244+
WOLFSSH_UNUSED(outSz);
245+
WOLFSSH_UNUSED(size);
246+
return WS_NOT_COMPILED;
247+
#endif
248+
}
249+
250+
190251
#else /* WOLFSSL_KEY_GEN */
191252
#error "wolfSSH keygen requires that keygen is enabled in wolfSSL, use --enable-keygen or #define WOLFSSL_KEY_GEN."
192253
#endif /* WOLFSSL_KEY_GEN */

tests/unit.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,23 @@ static int test_EcdsaKeyGen(void)
265265
}
266266
#endif
267267

268+
#ifndef WOLFSSH_NO_ED25519
269+
static int test_Ed25519KeyGen(void)
270+
{
271+
int result = 0;
272+
byte der[1200];
273+
int derSz;
274+
275+
derSz = wolfSSH_MakeEd25519Key(der, sizeof(der), WOLFSSH_ED25519KEY);
276+
if (derSz < 0) {
277+
printf("Ed25519KeyGen: MakeEd25519Key failed\n");
278+
result = -105;
279+
}
280+
281+
return result;
282+
}
283+
#endif
284+
268285
#endif
269286

270287

@@ -350,6 +367,11 @@ int wolfSSH_UnitTest(int argc, char** argv)
350367
printf("EcdsaKeyGen: %s\n", (unitResult == 0 ? "SUCCESS" : "FAILED"));
351368
testResult = testResult || unitResult;
352369
#endif
370+
#ifndef WOLFSSH_NO_ED25519
371+
unitResult = test_Ed25519KeyGen();
372+
printf("Ed25519KeyGen: %s\n", (unitResult == 0 ? "SUCCESS" : "FAILED"));
373+
testResult = testResult || unitResult;
374+
#endif
353375
#endif
354376

355377
wolfSSH_Cleanup();

wolfssh/keygen.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ extern "C" {
4141
#define WOLFSSH_ECDSAKEY_PRIME256 256
4242
#define WOLFSSH_ECDSAKEY_PRIME384 384
4343
#define WOLFSSH_ECDSAKEY_PRIME521 521
44+
#define WOLFSSH_ED25519KEY 256
4445

4546

4647
WOLFSSH_API int wolfSSH_MakeRsaKey(byte* out, word32 outSz,
4748
word32 size, word32 e);
4849
WOLFSSH_API int wolfSSH_MakeEcdsaKey(byte* out, word32 outSz, word32 size);
50+
WOLFSSH_API int wolfSSH_MakeEd25519Key(byte* out, word32 outSz, word32 size);
4951

5052

5153
#ifdef __cplusplus

0 commit comments

Comments
 (0)