Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/keygen.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,67 @@ int wolfSSH_MakeEcdsaKey(byte* out, word32 outSz, word32 size)
}


int wolfSSH_MakeEd25519Key(byte* out, word32 outSz, word32 size)
{
#ifndef WOLFSSH_NO_ED25519

int ret = WS_SUCCESS;
WC_RNG rng;

WLOG(WS_LOG_DEBUG, "Entering wolfSSH_MakeEd25519Key()");

if (wc_InitRng(&rng) != 0) {
WLOG(WS_LOG_DEBUG, "Couldn't create RNG");
ret = WS_CRYPTO_FAILED;
}

if (ret == WS_SUCCESS) {
ed25519_key key;

if (wc_ed25519_init(&key) != 0)
ret = WS_CRYPTO_FAILED;

if (ret == WS_SUCCESS) {
ret = wc_ed25519_make_key(&rng, size/8, &key);
if (ret != 0) {
WLOG(WS_LOG_DEBUG, "ED25519 key generation failed");
ret = WS_CRYPTO_FAILED;
}
else
ret = WS_SUCCESS;
}

if (ret == WS_SUCCESS) {
int keySz;

keySz = wc_Ed25519KeyToDer(&key, out, outSz);
if (keySz < 0) {
WLOG(WS_LOG_DEBUG, "ED25519 key to DER failed");
ret = WS_CRYPTO_FAILED;
}
else
ret = keySz;
}

wc_ed25519_free(&key);

if (wc_FreeRng(&rng) != 0) {
WLOG(WS_LOG_DEBUG, "Couldn't free RNG");
ret = WS_CRYPTO_FAILED;
}
}

WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_MakeEd25519Key(), ret = %d", ret);
return ret;
#else
WOLFSSH_UNUSED(out);
WOLFSSH_UNUSED(outSz);
WOLFSSH_UNUSED(size);
return WS_NOT_COMPILED;
#endif
}


#else /* WOLFSSL_KEY_GEN */
#error "wolfSSH keygen requires that keygen is enabled in wolfSSL, use --enable-keygen or #define WOLFSSL_KEY_GEN."
#endif /* WOLFSSL_KEY_GEN */
Expand Down
22 changes: 22 additions & 0 deletions tests/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,23 @@ static int test_EcdsaKeyGen(void)
}
#endif

#ifndef WOLFSSH_NO_ED25519
static int test_Ed25519KeyGen(void)
{
int result = 0;
byte der[1200];
int derSz;

derSz = wolfSSH_MakeEd25519Key(der, sizeof(der), WOLFSSH_ED25519KEY);
if (derSz < 0) {
printf("Ed25519KeyGen: MakeEd25519Key failed\n");
result = -105;
}

return result;
}
#endif

#endif


Expand Down Expand Up @@ -350,6 +367,11 @@ int wolfSSH_UnitTest(int argc, char** argv)
printf("EcdsaKeyGen: %s\n", (unitResult == 0 ? "SUCCESS" : "FAILED"));
testResult = testResult || unitResult;
#endif
#ifndef WOLFSSH_NO_ED25519
unitResult = test_Ed25519KeyGen();
printf("Ed25519KeyGen: %s\n", (unitResult == 0 ? "SUCCESS" : "FAILED"));
testResult = testResult || unitResult;
#endif
#endif

wolfSSH_Cleanup();
Expand Down
2 changes: 2 additions & 0 deletions wolfssh/keygen.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ extern "C" {
#define WOLFSSH_ECDSAKEY_PRIME256 256
#define WOLFSSH_ECDSAKEY_PRIME384 384
#define WOLFSSH_ECDSAKEY_PRIME521 521
#define WOLFSSH_ED25519KEY 256


WOLFSSH_API int wolfSSH_MakeRsaKey(byte* out, word32 outSz,
word32 size, word32 e);
WOLFSSH_API int wolfSSH_MakeEcdsaKey(byte* out, word32 outSz, word32 size);
WOLFSSH_API int wolfSSH_MakeEd25519Key(byte* out, word32 outSz, word32 size);


#ifdef __cplusplus
Expand Down
Loading