Skip to content

Commit 3a2803c

Browse files
committed
wolfhsm: add support for Ed25519 signature algorithm
1 parent a9f88c1 commit 3a2803c

File tree

9 files changed

+1676
-1
lines changed

9 files changed

+1676
-1
lines changed

src/wh_client_crypto.c

Lines changed: 652 additions & 0 deletions
Large diffs are not rendered by default.

src/wh_client_cryptocb.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,50 @@ int wh_Client_CryptoCb(int devId, wc_CryptoInfo* info, void* inCtx)
357357
*out_len = len;
358358
}
359359
} break;
360+
361+
#ifdef HAVE_ED25519
362+
case WC_PK_TYPE_ED25519_KEYGEN: {
363+
ed25519_key* key = info->pk.ed25519kg.key;
364+
/* Only default Ed25519 supported */
365+
ret = wh_Client_Ed25519MakeExportKey(ctx, key);
366+
} break;
367+
368+
case WC_PK_TYPE_ED25519_SIGN: {
369+
ed25519_key* key = info->pk.ed25519sign.key;
370+
const byte* in = info->pk.ed25519sign.in;
371+
word32 inLen = info->pk.ed25519sign.inLen;
372+
byte* out = info->pk.ed25519sign.out;
373+
word32* outLen = info->pk.ed25519sign.outLen;
374+
byte type = info->pk.ed25519sign.type;
375+
376+
/* only plain ed25519 supported for now */
377+
if (type != (byte)Ed25519) {
378+
ret = BAD_FUNC_ARG;
379+
break;
380+
}
381+
382+
ret = wh_Client_Ed25519Sign(ctx, key, in, inLen, out, outLen);
383+
} break;
384+
385+
case WC_PK_TYPE_ED25519_VERIFY: {
386+
ed25519_key* key = info->pk.ed25519verify.key;
387+
const byte* sig = info->pk.ed25519verify.sig;
388+
word32 sigLen = info->pk.ed25519verify.sigLen;
389+
const byte* msg = info->pk.ed25519verify.msg;
390+
word32 msgLen = info->pk.ed25519verify.msgLen;
391+
int* res = info->pk.ed25519verify.res;
392+
byte type = info->pk.ed25519verify.type;
393+
394+
/* only plain ed25519 supported for now */
395+
if (type != (byte)Ed25519) {
396+
ret = CRYPTOCB_UNAVAILABLE;
397+
break;
398+
}
399+
400+
ret = wh_Client_Ed25519Verify(ctx, key, sig, sigLen, msg, msgLen,
401+
res);
402+
} break;
403+
#endif /* HAVE_ED25519 */
360404
#endif /* HAVE_CURVE25519 */
361405

362406
#if defined(HAVE_DILITHIUM) || defined(HAVE_FALCON)

src/wh_crypto.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "wolfssl/wolfcrypt/rsa.h"
4040
#include "wolfssl/wolfcrypt/curve25519.h"
4141
#include "wolfssl/wolfcrypt/ecc.h"
42+
#include "wolfssl/wolfcrypt/ed25519.h"
4243
#include "wolfssl/wolfcrypt/dilithium.h"
4344

4445
#include "wolfhsm/wh_error.h"
@@ -247,6 +248,55 @@ int wh_Crypto_Curve25519DeserializeKey(const uint8_t* derBuffer,
247248
}
248249
#endif /* HAVE_CURVE25519 */
249250

251+
#ifdef HAVE_ED25519
252+
int wh_Crypto_Ed25519SerializeKeyDer(ed25519_key* key, uint16_t max_size,
253+
uint8_t* buffer, uint16_t* out_size)
254+
{
255+
int ret = 0;
256+
257+
if ((key == NULL) || (buffer == NULL)) {
258+
return WH_ERROR_BADARGS;
259+
}
260+
261+
if (key->privKeySet) {
262+
ret = wc_Ed25519KeyToDer(key, buffer, max_size);
263+
}
264+
else if (key->pubKeySet) {
265+
ret = wc_Ed25519PublicKeyToDer(key, buffer, max_size, 1);
266+
}
267+
else {
268+
ret = WH_ERROR_BADARGS;
269+
}
270+
271+
if (ret > 0) {
272+
if (out_size != NULL) {
273+
*out_size = (uint16_t)ret;
274+
}
275+
ret = WH_ERROR_OK;
276+
}
277+
return ret;
278+
}
279+
280+
int wh_Crypto_Ed25519DeserializeKeyDer(const uint8_t* buffer, uint16_t size,
281+
ed25519_key* key)
282+
{
283+
word32 idx = 0;
284+
int ret;
285+
286+
if ((buffer == NULL) || (key == NULL) || (size == 0)) {
287+
return WH_ERROR_BADARGS;
288+
}
289+
290+
/* Try private key first; fall back to public key */
291+
ret = wc_Ed25519PrivateKeyDecode(buffer, &idx, key, size);
292+
if (ret != 0) {
293+
idx = 0;
294+
ret = wc_Ed25519PublicKeyDecode(buffer, &idx, key, size);
295+
}
296+
return ret;
297+
}
298+
#endif /* HAVE_ED25519 */
299+
250300
#ifdef HAVE_DILITHIUM
251301
int wh_Crypto_MlDsaSerializeKeyDer(MlDsaKey* key, uint16_t max_size,
252302
uint8_t* buffer, uint16_t* out_size)

src/wh_message_crypto.c

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,89 @@ int wh_MessageCrypto_TranslateCurve25519Response(
538538
return 0;
539539
}
540540

541+
/* Ed25519 Key Generation Request translation */
542+
int wh_MessageCrypto_TranslateEd25519KeyGenRequest(
543+
uint16_t magic, const whMessageCrypto_Ed25519KeyGenRequest* src,
544+
whMessageCrypto_Ed25519KeyGenRequest* dest)
545+
{
546+
if ((src == NULL) || (dest == NULL)) {
547+
return WH_ERROR_BADARGS;
548+
}
549+
WH_T32(magic, dest, src, flags);
550+
WH_T32(magic, dest, src, keyId);
551+
WH_T32(magic, dest, src, access);
552+
if (src != dest) {
553+
memcpy(dest->label, src->label, sizeof(src->label));
554+
}
555+
return 0;
556+
}
557+
558+
/* Ed25519 Key Generation Response translation */
559+
int wh_MessageCrypto_TranslateEd25519KeyGenResponse(
560+
uint16_t magic, const whMessageCrypto_Ed25519KeyGenResponse* src,
561+
whMessageCrypto_Ed25519KeyGenResponse* dest)
562+
{
563+
if ((src == NULL) || (dest == NULL)) {
564+
return WH_ERROR_BADARGS;
565+
}
566+
WH_T32(magic, dest, src, keyId);
567+
WH_T32(magic, dest, src, outSz);
568+
return 0;
569+
}
570+
571+
/* Ed25519 Sign Request translation */
572+
int wh_MessageCrypto_TranslateEd25519SignRequest(
573+
uint16_t magic, const whMessageCrypto_Ed25519SignRequest* src,
574+
whMessageCrypto_Ed25519SignRequest* dest)
575+
{
576+
if ((src == NULL) || (dest == NULL)) {
577+
return WH_ERROR_BADARGS;
578+
}
579+
WH_T32(magic, dest, src, options);
580+
WH_T32(magic, dest, src, keyId);
581+
WH_T32(magic, dest, src, msgSz);
582+
return 0;
583+
}
584+
585+
/* Ed25519 Sign Response translation */
586+
int wh_MessageCrypto_TranslateEd25519SignResponse(
587+
uint16_t magic, const whMessageCrypto_Ed25519SignResponse* src,
588+
whMessageCrypto_Ed25519SignResponse* dest)
589+
{
590+
if ((src == NULL) || (dest == NULL)) {
591+
return WH_ERROR_BADARGS;
592+
}
593+
WH_T32(magic, dest, src, sigSz);
594+
return 0;
595+
}
596+
597+
/* Ed25519 Verify Request translation */
598+
int wh_MessageCrypto_TranslateEd25519VerifyRequest(
599+
uint16_t magic, const whMessageCrypto_Ed25519VerifyRequest* src,
600+
whMessageCrypto_Ed25519VerifyRequest* dest)
601+
{
602+
if ((src == NULL) || (dest == NULL)) {
603+
return WH_ERROR_BADARGS;
604+
}
605+
WH_T32(magic, dest, src, options);
606+
WH_T32(magic, dest, src, keyId);
607+
WH_T32(magic, dest, src, sigSz);
608+
WH_T32(magic, dest, src, msgSz);
609+
return 0;
610+
}
611+
612+
/* Ed25519 Verify Response translation */
613+
int wh_MessageCrypto_TranslateEd25519VerifyResponse(
614+
uint16_t magic, const whMessageCrypto_Ed25519VerifyResponse* src,
615+
whMessageCrypto_Ed25519VerifyResponse* dest)
616+
{
617+
if ((src == NULL) || (dest == NULL)) {
618+
return WH_ERROR_BADARGS;
619+
}
620+
WH_T32(magic, dest, src, res);
621+
return 0;
622+
}
623+
541624
/* SHA256 Request translation */
542625
int wh_MessageCrypto_TranslateSha256Request(
543626
uint16_t magic, const whMessageCrypto_Sha256Request* src,
@@ -1002,6 +1085,112 @@ int wh_MessageCrypto_TranslateMlDsaVerifyDmaResponse(
10021085
return 0;
10031086
}
10041087

1088+
/* Ed25519 DMA Sign Request translation */
1089+
int wh_MessageCrypto_TranslateEd25519SignDmaRequest(
1090+
uint16_t magic, const whMessageCrypto_Ed25519SignDmaRequest* src,
1091+
whMessageCrypto_Ed25519SignDmaRequest* dest)
1092+
{
1093+
int ret;
1094+
1095+
if ((src == NULL) || (dest == NULL)) {
1096+
return WH_ERROR_BADARGS;
1097+
}
1098+
1099+
ret = wh_MessageCrypto_TranslateDmaBuffer(magic, &src->msg, &dest->msg);
1100+
if (ret != 0) {
1101+
return ret;
1102+
}
1103+
1104+
ret = wh_MessageCrypto_TranslateDmaBuffer(magic, &src->sig, &dest->sig);
1105+
if (ret != 0) {
1106+
return ret;
1107+
}
1108+
1109+
ret = wh_MessageCrypto_TranslateDmaBuffer(magic, &src->pub, &dest->pub);
1110+
if (ret != 0) {
1111+
return ret;
1112+
}
1113+
1114+
WH_T32(magic, dest, src, options);
1115+
WH_T32(magic, dest, src, keyId);
1116+
return 0;
1117+
}
1118+
1119+
/* Ed25519 DMA Sign Response translation */
1120+
int wh_MessageCrypto_TranslateEd25519SignDmaResponse(
1121+
uint16_t magic, const whMessageCrypto_Ed25519SignDmaResponse* src,
1122+
whMessageCrypto_Ed25519SignDmaResponse* dest)
1123+
{
1124+
int ret;
1125+
1126+
if ((src == NULL) || (dest == NULL)) {
1127+
return WH_ERROR_BADARGS;
1128+
}
1129+
1130+
ret = wh_MessageCrypto_TranslateDmaAddrStatus(magic, &src->dmaAddrStatus,
1131+
&dest->dmaAddrStatus);
1132+
if (ret != 0) {
1133+
return ret;
1134+
}
1135+
1136+
WH_T32(magic, dest, src, sigSz);
1137+
WH_T32(magic, dest, src, pubSz);
1138+
return 0;
1139+
}
1140+
1141+
/* Ed25519 DMA Verify Request translation */
1142+
int wh_MessageCrypto_TranslateEd25519VerifyDmaRequest(
1143+
uint16_t magic, const whMessageCrypto_Ed25519VerifyDmaRequest* src,
1144+
whMessageCrypto_Ed25519VerifyDmaRequest* dest)
1145+
{
1146+
int ret;
1147+
1148+
if ((src == NULL) || (dest == NULL)) {
1149+
return WH_ERROR_BADARGS;
1150+
}
1151+
1152+
ret = wh_MessageCrypto_TranslateDmaBuffer(magic, &src->sig, &dest->sig);
1153+
if (ret != 0) {
1154+
return ret;
1155+
}
1156+
1157+
ret = wh_MessageCrypto_TranslateDmaBuffer(magic, &src->msg, &dest->msg);
1158+
if (ret != 0) {
1159+
return ret;
1160+
}
1161+
1162+
ret = wh_MessageCrypto_TranslateDmaBuffer(magic, &src->pub, &dest->pub);
1163+
if (ret != 0) {
1164+
return ret;
1165+
}
1166+
1167+
WH_T32(magic, dest, src, options);
1168+
WH_T32(magic, dest, src, keyId);
1169+
return 0;
1170+
}
1171+
1172+
/* Ed25519 DMA Verify Response translation */
1173+
int wh_MessageCrypto_TranslateEd25519VerifyDmaResponse(
1174+
uint16_t magic, const whMessageCrypto_Ed25519VerifyDmaResponse* src,
1175+
whMessageCrypto_Ed25519VerifyDmaResponse* dest)
1176+
{
1177+
int ret;
1178+
1179+
if ((src == NULL) || (dest == NULL)) {
1180+
return WH_ERROR_BADARGS;
1181+
}
1182+
1183+
ret = wh_MessageCrypto_TranslateDmaAddrStatus(magic, &src->dmaAddrStatus,
1184+
&dest->dmaAddrStatus);
1185+
if (ret != 0) {
1186+
return ret;
1187+
}
1188+
1189+
WH_T32(magic, dest, src, verifyResult);
1190+
WH_T32(magic, dest, src, pubSz);
1191+
return 0;
1192+
}
1193+
10051194
/* AES DMA Request translation */
10061195
int wh_MessageCrypto_TranslateAesDmaRequest(
10071196
uint16_t magic, const whMessageCrypto_AesDmaRequest* src,

0 commit comments

Comments
 (0)