Skip to content

Commit

Permalink
ed25519 raw support genkey, sign, and verify
Browse files Browse the repository at this point in the history
  • Loading branch information
aidan garske committed Aug 9, 2024
1 parent 32ba538 commit 625022d
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 154 deletions.
218 changes: 117 additions & 101 deletions src/genkey/clu_genkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ int wolfCLU_genKey_ED25519(WC_RNG* rng, char* fOutNm, int directive, int format)
int flagOutputPub = 0; /* set if outputting both priv/pub */
char privAppend[6] = ".priv\0"; /* last part of the priv file name */
char pubAppend[6] = ".pub\0\0"; /* last part of the pub file name*/
#if 0
byte privKeyBuf[ED25519_KEY_SIZE*2]; /* will hold public & private parts */
byte pubKeyBuf[ED25519_KEY_SIZE]; /* holds just the public key part */
word32 privKeySz; /* size of private key */
word32 pubKeySz; /* size of public key */
#endif
ed25519_key edKeyOut; /* the ed25519 key structure */
char* finalOutFNm = NULL; /* file name + append */
XFILE file = NULL; /* file stream */
Expand Down Expand Up @@ -80,22 +78,20 @@ int wolfCLU_genKey_ED25519(WC_RNG* rng, char* fOutNm, int directive, int format)
}
}

#if 0
/* get key size */
privKeySz = wc_ed25519_priv_size(&edKeyOut);
if (privKeySz <= 0)
return WC_KEY_SIZE_E;
if (format == RAW_FORM && ret == 0) {
/* get key size */
privKeySz = wc_ed25519_priv_size(&edKeyOut);
if (privKeySz <= 0)
ret = WC_KEY_SIZE_E;

pubKeySz = wc_ed25519_pub_size(&edKeyOut);
if (pubKeySz <= 0)
return WC_KEY_SIZE_E;
pubKeySz = wc_ed25519_pub_size(&edKeyOut);
if (pubKeySz <= 0)
ret = WC_KEY_SIZE_E;

/* export keys to buffers */
ret = wc_ed25519_export_key(&edKeyOut, privKeyBuf, &privKeySz, pubKeyBuf,
&pubKeySz);
if (ret != 0)
return ret;
#endif
/* export keys to buffers */
ret = wc_ed25519_export_key(&edKeyOut, privKeyBuf, &privKeySz,
pubKeyBuf, &pubKeySz);
}

/* set up the file name output buffer */
if (ret == 0) {
Expand Down Expand Up @@ -130,60 +126,70 @@ int wolfCLU_genKey_ED25519(WC_RNG* rng, char* fOutNm, int directive, int format)
}
}

/* determine size for buffer */
if (ret == 0) {
derSz = wc_Ed25519PrivateKeyToDer(&edKeyOut, NULL, 0);
if (derSz <= 0) {
ret = MEMORY_E;
/* write RAW format to the file */
if (format == RAW_FORM && ret == 0) {
if (XFWRITE(privKeyBuf, 1, privKeySz, file) != privKeySz) {
ret = OUTPUT_FILE_ERROR;
}
}

/* allocate DER buffer */
if (ret == 0) {
derBuf = (byte*)XMALLOC(derSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
if (derBuf == NULL) {
ret = MEMORY_E;
else { /* DER and PEM */
/* determine size for buffer */
if (ret == 0) {
derSz = wc_Ed25519PrivateKeyToDer(&edKeyOut, NULL, 0);
if (derSz <= 0) {
ret = MEMORY_E;
}
}
}

/* convert Key to DER */
if (ret == 0) {
derSz = wc_Ed25519PrivateKeyToDer(&edKeyOut, derBuf, derSz);
if (derSz < 0) {
ret = derSz;
/* allocate DER buffer */
if (ret == 0) {
derBuf = (byte*)XMALLOC(derSz, HEAP_HINT,
DYNAMIC_TYPE_TMP_BUFFER);
if (derBuf == NULL) {
ret = MEMORY_E;
}
}
}
if (ret != 0)
break;

/* convert DER to PEM if necessary */
if (format == PEM_FORM) {
/* convert Key to DER */
if (ret == 0) {
pemSz = wolfCLU_KeyDerToPem(derBuf, derSz, &pemBuf, PRIVATEKEY_TYPE,
DYNAMIC_TYPE_TMP_BUFFER);
if (pemSz < 0) {
ret = pemSz;
derSz = wc_Ed25519PrivateKeyToDer(&edKeyOut, derBuf, derSz);
if (derSz < 0) {
ret = derSz;
}
}
if (ret == 0) {
ret = (int)XFWRITE(pemBuf, 1, pemSz, file);
if (ret != pemSz) {
ret = OUTPUT_FILE_ERROR;
if (ret != 0)
break;

/* convert DER to PEM if necessary */
if (format == PEM_FORM) {
if (ret == 0) {
pemSz = wolfCLU_KeyDerToPem(derBuf, derSz, &pemBuf,
PRIVATEKEY_TYPE, DYNAMIC_TYPE_TMP_BUFFER);
if (pemSz < 0) {
ret = pemSz;
}
}
else {
ret = 0;
/* write PEM format to the file */
if (ret == 0) {
ret = (int)XFWRITE(pemBuf, 1, pemSz, file);
if (ret != pemSz) {
ret = OUTPUT_FILE_ERROR;
}
else {
ret = 0;
}
}
}
}
else {
/* write DER format to the file */
if (ret == 0) {
ret = (int)XFWRITE(derBuf, 1, derSz, file);
if (ret != derSz) {
ret = OUTPUT_FILE_ERROR;
}
else {
ret = 0;
else {
/* write DER format to the file */
if (ret == 0) {
ret = (int)XFWRITE(derBuf, 1, derSz, file);
if (ret != derSz) {
ret = OUTPUT_FILE_ERROR;
}
else {
ret = 0;
}
}
}
}
Expand Down Expand Up @@ -215,60 +221,70 @@ int wolfCLU_genKey_ED25519(WC_RNG* rng, char* fOutNm, int directive, int format)
}
}

/* determine size for buffer */
if (ret == 0) {
derSz = wc_Ed25519PublicKeyToDer(&edKeyOut, NULL, 0, 1);
if (derSz <= 0) {
ret = MEMORY_E;
}
}

/* allocate DER buffer */
if (ret == 0) {
derBuf = (byte*)XMALLOC(derSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
if (derBuf == NULL) {
ret = MEMORY_E;
/* write RAW format to the file */
if (format == RAW_FORM && ret == 0) {
if (XFWRITE(pubKeyBuf, 1, pubKeySz, file) != pubKeySz) {
ret = OUTPUT_FILE_ERROR;
}
}

/* convert Key to DER */
if (ret == 0) {
derSz = wc_Ed25519PublicKeyToDer(&edKeyOut, derBuf, derSz, 1);
if (derSz < 0) {
ret = derSz;
else { /* DER and PEM */
/* determine size for buffer */
if (ret == 0) {
derSz = wc_Ed25519PublicKeyToDer(&edKeyOut, NULL, 0, 1);
if (derSz <= 0) {
ret = MEMORY_E;
}
}
}

if (ret != 0)
break;

/* convert DER to PEM if necessary */
if (format == PEM_FORM) {
/* allocate DER buffer */
if (ret == 0) {
pemSz = wolfCLU_KeyDerToPem(derBuf, derSz, &pemBuf, PUBLICKEY_TYPE,
DYNAMIC_TYPE_TMP_BUFFER);
if (pemSz < 0) {
ret = pemSz;
derBuf = (byte*)XMALLOC(derSz, HEAP_HINT,
DYNAMIC_TYPE_TMP_BUFFER);
if (derBuf == NULL) {
ret = MEMORY_E;
}
}

/* convert Key to DER */
if (ret == 0) {
ret = (int)XFWRITE(pemBuf, 1, pemSz, file);
if (ret != pemSz) {
ret = OUTPUT_FILE_ERROR;
} else {
ret = 0;
derSz = wc_Ed25519PublicKeyToDer(&edKeyOut, derBuf, derSz, 1);
if (derSz < 0) {
ret = derSz;
}
}
}
else {
/* write DER format to the file */
if (ret == 0) {
ret = (int)XFWRITE(derBuf, 1, derSz, file);
if (ret != derSz) {
ret = OUTPUT_FILE_ERROR;

if (ret != 0)
break;

/* convert DER to PEM if necessary */
if (format == PEM_FORM) {
if (ret == 0) {
pemSz = wolfCLU_KeyDerToPem(derBuf, derSz, &pemBuf,
PUBLICKEY_TYPE, DYNAMIC_TYPE_TMP_BUFFER);
if (pemSz < 0) {
ret = pemSz;
}
}
else {
ret = 0;
/* write PEM format to the file */
if (ret == 0) {
ret = (int)XFWRITE(pemBuf, 1, pemSz, file);
if (ret != pemSz) {
ret = OUTPUT_FILE_ERROR;
} else {
ret = 0;
}
}
}
else {
/* write DER format to the file */
if (ret == 0) {
ret = (int)XFWRITE(derBuf, 1, derSz, file);
if (ret != derSz) {
ret = OUTPUT_FILE_ERROR;
}
else {
ret = 0;
}
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/genkey/clu_genkey_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ int wolfCLU_genKeySetup(int argc, char** argv)
format = argv[ret+1];
}
ret = wolfCLU_checkOutform(format);
if (ret == PEM_FORM || ret == DER_FORM) {
WOLFCLU_LOG(WOLFCLU_L0, "OUTPUT A %s FILE", (ret == PEM_FORM)? "PEM": "DER");
if (ret == PEM_FORM || ret == DER_FORM || ret == RAW_FORM) {
const char* formatStr = (ret == PEM_FORM) ? "PEM" :
(ret == DER_FORM) ? "DER" :
"RAW";

WOLFCLU_LOG(WOLFCLU_L0, "OUTPUT A %s FILE", formatStr);
formatArg = ret;
}
else {
Expand Down
30 changes: 21 additions & 9 deletions src/sign-verify/clu_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,18 +465,30 @@ int wolfCLU_sign_data_ed25519 (byte* data, char* out, word32 fSz, char* privKey,
}
}

/* decode the private key from the DER-encoded input */
if (ret == 0) {
ret = wc_Ed25519PrivateKeyDecode(keyBuf, &index, &key, privFileSz);
/* retrieve RAW private key and store in the ED25519 Key */
if (inForm == RAW_FORM && ret == 0) {
ret = wc_ed25519_import_private_key(keyBuf,
ED25519_KEY_SIZE,
keyBuf + ED25519_KEY_SIZE,
ED25519_KEY_SIZE, &key);
if (ret != 0 ) {
wolfCLU_LogError("Failed to import RAW private key.\nRET: %d", ret);
}
}
else {
/* decode the private key from the DER-encoded input */
if (ret == 0) {
/* Calculate the public key */
ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
ret = wc_Ed25519PrivateKeyDecode(keyBuf, &index, &key, privFileSz);
if (ret == 0) {
key.pubKeySet = 1;
/* Calculate the public key */
ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
if (ret == 0) {
key.pubKeySet = 1;
}
}
else {
wolfCLU_LogError("Failed to import private key.\nRET: %d", ret);
}
}
else {
wolfCLU_LogError("Failed to import private key.\nRET: %d", ret);
}
}

Expand Down
Loading

0 comments on commit 625022d

Please sign in to comment.