Skip to content

Commit fc51a38

Browse files
Merge pull request #10135 from lealem47/nid_ED
Add Ed25519/Ed448 support to EVP layer
2 parents d00a137 + 5da71f4 commit fc51a38

14 files changed

Lines changed: 832 additions & 14 deletions

File tree

src/internal.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13957,9 +13957,30 @@ int CopyDecodedToX509(WOLFSSL_X509* x509, DecodedCert* dCert)
1395713957
}
1395813958

1395913959
wolfSSL_EVP_PKEY_free(x509->key.pkey);
13960-
if (!(x509->key.pkey = wolfSSL_d2i_PUBKEY(NULL,
13961-
&dCert->publicKey,
13962-
dCert->pubKeySize))) {
13960+
x509->key.pkey = NULL;
13961+
13962+
switch (dCert->keyOID) {
13963+
#ifdef HAVE_ED25519
13964+
case ED25519k:
13965+
x509->key.pkey = wolfSSL_EVP_PKEY_new_raw_public_key(
13966+
WC_EVP_PKEY_ED25519, NULL, dCert->publicKey,
13967+
dCert->pubKeySize);
13968+
break;
13969+
#endif
13970+
#ifdef HAVE_ED448
13971+
case ED448k:
13972+
x509->key.pkey = wolfSSL_EVP_PKEY_new_raw_public_key(
13973+
WC_EVP_PKEY_ED448, NULL, dCert->publicKey,
13974+
dCert->pubKeySize);
13975+
break;
13976+
#endif
13977+
default:
13978+
x509->key.pkey = wolfSSL_d2i_PUBKEY(NULL,
13979+
&dCert->publicKey, dCert->pubKeySize);
13980+
break;
13981+
}
13982+
13983+
if (x509->key.pkey == NULL) {
1396313984
ret = PUBLIC_KEY_E;
1396413985
WOLFSSL_ERROR_VERBOSE(ret);
1396513986
}

src/pk.c

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5511,6 +5511,60 @@ int wolfSSL_ED25519_verify(const unsigned char *msg, unsigned int msgSz,
55115511

55125512
#endif /* OPENSSL_EXTRA && HAVE_ED25519 */
55135513

5514+
#if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
5515+
defined(HAVE_ED25519)
5516+
/* Allocate and initialize a new ed25519_key.
5517+
*
5518+
* @param [in] heap Heap hint for memory allocation.
5519+
* @param [in] devId Device identifier for crypto callbacks.
5520+
* @return Allocated and initialized ed25519_key on success.
5521+
* @return NULL on failure.
5522+
*/
5523+
ed25519_key* wolfSSL_ED25519_new(void* heap, int devId)
5524+
{
5525+
ed25519_key* key;
5526+
5527+
WOLFSSL_ENTER("wolfSSL_ED25519_new");
5528+
5529+
#ifndef WC_NO_CONSTRUCTORS
5530+
key = wc_ed25519_new(heap, devId, NULL);
5531+
#else
5532+
key = (ed25519_key*)XMALLOC(sizeof(ed25519_key), heap,
5533+
DYNAMIC_TYPE_ED25519);
5534+
if (key == NULL) {
5535+
WOLFSSL_ERROR_MSG("wolfSSL_ED25519_new malloc failure");
5536+
}
5537+
else if (wc_ed25519_init_ex(key, heap, devId) != 0) {
5538+
WOLFSSL_ERROR_MSG("wolfSSL_ED25519_new init failure");
5539+
XFREE(key, heap, DYNAMIC_TYPE_ED25519);
5540+
key = NULL;
5541+
}
5542+
#endif
5543+
5544+
return key;
5545+
}
5546+
5547+
/* Free an ed25519_key allocated with wolfSSL_ED25519_new.
5548+
*
5549+
* @param [in] key ed25519_key to free. May be NULL.
5550+
*/
5551+
void wolfSSL_ED25519_free(ed25519_key* key)
5552+
{
5553+
if (key != NULL) {
5554+
WOLFSSL_ENTER("wolfSSL_ED25519_free");
5555+
#ifndef WC_NO_CONSTRUCTORS
5556+
wc_ed25519_delete(key, NULL);
5557+
#else
5558+
{
5559+
void* heap = key->heap;
5560+
wc_ed25519_free(key);
5561+
XFREE(key, heap, DYNAMIC_TYPE_ED25519);
5562+
}
5563+
#endif
5564+
}
5565+
}
5566+
#endif /* (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) && HAVE_ED25519 */
5567+
55145568
/*******************************************************************************
55155569
* END OF ED25519 API
55165570
******************************************************************************/
@@ -5964,6 +6018,61 @@ int wolfSSL_ED448_verify(const unsigned char *msg, unsigned int msgSz,
59646018
}
59656019
#endif /* OPENSSL_EXTRA && HAVE_ED448 */
59666020

6021+
#if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
6022+
defined(HAVE_ED448)
6023+
/* Allocate and initialize a new ed448_key.
6024+
*
6025+
* @param [in] heap Heap hint for memory allocation.
6026+
* @param [in] devId Device identifier for crypto callbacks.
6027+
* @return Allocated and initialized ed448_key on success.
6028+
* @return NULL on failure.
6029+
*/
6030+
ed448_key* wolfSSL_ED448_new(void* heap, int devId)
6031+
{
6032+
ed448_key* key;
6033+
6034+
WOLFSSL_ENTER("wolfSSL_ED448_new");
6035+
6036+
#if !defined(WC_NO_CONSTRUCTORS) && \
6037+
(!defined(HAVE_FIPS) || FIPS_VERSION_GE(7, 0))
6038+
key = wc_ed448_new(heap, devId, NULL);
6039+
#else
6040+
key = (ed448_key*)XMALLOC(sizeof(ed448_key), heap, DYNAMIC_TYPE_ED448);
6041+
if (key == NULL) {
6042+
WOLFSSL_ERROR_MSG("wolfSSL_ED448_new malloc failure");
6043+
}
6044+
else if (wc_ed448_init_ex(key, heap, devId) != 0) {
6045+
WOLFSSL_ERROR_MSG("wolfSSL_ED448_new init failure");
6046+
XFREE(key, heap, DYNAMIC_TYPE_ED448);
6047+
key = NULL;
6048+
}
6049+
#endif
6050+
6051+
return key;
6052+
}
6053+
6054+
/* Free an ed448_key allocated with wolfSSL_ED448_new.
6055+
*
6056+
* @param [in] key ed448_key to free. May be NULL.
6057+
*/
6058+
void wolfSSL_ED448_free(ed448_key* key)
6059+
{
6060+
if (key != NULL) {
6061+
WOLFSSL_ENTER("wolfSSL_ED448_free");
6062+
#if !defined(WC_NO_CONSTRUCTORS) && \
6063+
(!defined(HAVE_FIPS) || FIPS_VERSION_GE(7, 0))
6064+
wc_ed448_delete(key, NULL);
6065+
#else
6066+
{
6067+
void* heap = key->heap;
6068+
wc_ed448_free(key);
6069+
XFREE(key, heap, DYNAMIC_TYPE_ED448);
6070+
}
6071+
#endif
6072+
}
6073+
}
6074+
#endif /* (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) && HAVE_ED448 */
6075+
59676076
/*******************************************************************************
59686077
* END OF ED448 API
59696078
******************************************************************************/
@@ -6272,6 +6381,16 @@ WOLFSSL_EVP_PKEY* wolfSSL_PEM_read_bio_PrivateKey(WOLFSSL_BIO* bio,
62726381
case DHk:
62736382
type = WC_EVP_PKEY_DH;
62746383
break;
6384+
#ifdef HAVE_ED25519
6385+
case ED25519k:
6386+
type = WC_EVP_PKEY_ED25519;
6387+
break;
6388+
#endif
6389+
#ifdef HAVE_ED448
6390+
case ED448k:
6391+
type = WC_EVP_PKEY_ED448;
6392+
break;
6393+
#endif
62756394
default:
62766395
type = WOLFSSL_FATAL_ERROR;
62776396
break;
@@ -6419,6 +6538,16 @@ WOLFSSL_EVP_PKEY* wolfSSL_PEM_read_PrivateKey(XFILE fp, WOLFSSL_EVP_PKEY **key,
64196538
case DHk:
64206539
type = WC_EVP_PKEY_DH;
64216540
break;
6541+
#ifdef HAVE_ED25519
6542+
case ED25519k:
6543+
type = WC_EVP_PKEY_ED25519;
6544+
break;
6545+
#endif
6546+
#ifdef HAVE_ED448
6547+
case ED448k:
6548+
type = WC_EVP_PKEY_ED448;
6549+
break;
6550+
#endif
64226551
default:
64236552
type = WOLFSSL_FATAL_ERROR;
64246553
break;

src/ssl.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17724,6 +17724,14 @@ word32 nid2oid(int nid, int grp)
1772417724
return CTC_SHA3_512wECDSA;
1772517725
#endif
1772617726
#endif /* HAVE_ECC */
17727+
#ifdef HAVE_ED25519
17728+
case WC_NID_ED25519:
17729+
return CTC_ED25519;
17730+
#endif /* HAVE_ED25519 */
17731+
#ifdef HAVE_ED448
17732+
case WC_NID_ED448:
17733+
return CTC_ED448;
17734+
#endif /* HAVE_ED448 */
1772717735
}
1772817736
break;
1772917737

@@ -17742,6 +17750,14 @@ word32 nid2oid(int nid, int grp)
1774217750
case WC_NID_X9_62_id_ecPublicKey:
1774317751
return ECDSAk;
1774417752
#endif /* HAVE_ECC */
17753+
#ifdef HAVE_ED25519
17754+
case WC_NID_ED25519:
17755+
return ED25519k;
17756+
#endif /* HAVE_ED25519 */
17757+
#ifdef HAVE_ED448
17758+
case WC_NID_ED448:
17759+
return ED448k;
17760+
#endif /* HAVE_ED448 */
1774517761
}
1774617762
break;
1774717763

@@ -18100,6 +18116,14 @@ int oid2nid(word32 oid, int grp)
1810018116
return WC_NID_ecdsa_with_SHA3_512;
1810118117
#endif
1810218118
#endif /* HAVE_ECC */
18119+
#ifdef HAVE_ED25519
18120+
case CTC_ED25519:
18121+
return WC_NID_ED25519;
18122+
#endif /* HAVE_ED25519 */
18123+
#ifdef HAVE_ED448
18124+
case CTC_ED448:
18125+
return WC_NID_ED448;
18126+
#endif /* HAVE_ED448 */
1810318127
}
1810418128
break;
1810518129

@@ -18122,6 +18146,14 @@ int oid2nid(word32 oid, int grp)
1812218146
case ECDSAk:
1812318147
return WC_NID_X9_62_id_ecPublicKey;
1812418148
#endif /* HAVE_ECC */
18149+
#ifdef HAVE_ED25519
18150+
case ED25519k:
18151+
return WC_NID_ED25519;
18152+
#endif /* HAVE_ED25519 */
18153+
#ifdef HAVE_ED448
18154+
case ED448k:
18155+
return WC_NID_ED448;
18156+
#endif /* HAVE_ED448 */
1812518157
}
1812618158
break;
1812718159

src/ssl_load.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5306,6 +5306,18 @@ int wolfSSL_CTX_use_PrivateKey(WOLFSSL_CTX *ctx, WOLFSSL_EVP_PKEY *pkey)
53065306
WOLFSSL_MSG("populating ECC key");
53075307
ret = ECC_populate_EVP_PKEY(pkey, pkey->ecc);
53085308
break;
5309+
#endif
5310+
#ifdef HAVE_ED25519
5311+
case WC_EVP_PKEY_ED25519:
5312+
/* DER is already stored in pkey->pkey.ptr by d2i_evp_pkey. */
5313+
WOLFSSL_MSG("populating Ed25519 key");
5314+
break;
5315+
#endif
5316+
#ifdef HAVE_ED448
5317+
case WC_EVP_PKEY_ED448:
5318+
/* DER is already stored in pkey->pkey.ptr by d2i_evp_pkey. */
5319+
WOLFSSL_MSG("populating Ed448 key");
5320+
break;
53095321
#endif
53105322
default:
53115323
ret = 0;

0 commit comments

Comments
 (0)