Skip to content
Closed
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
3 changes: 2 additions & 1 deletion src/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -8596,7 +8596,8 @@ static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse)
}

#ifdef WOLFSSL_HAVE_MLKEM
#if defined(WOLFSSL_MLKEM_CACHE_A) && \
#if (defined(WOLFSSL_MLKEM_CACHE_A) || \
(defined(HAVE_PKCS11) && !defined(NO_PKCS11_MLKEM))) && \
!defined(WOLFSSL_TLSX_PQC_MLKEM_STORE_PRIV_KEY)
/* Store KyberKey object rather than private key bytes in key share entry.
* Improves performance at cost of more dynamic memory being used. */
Expand Down
78 changes: 77 additions & 1 deletion wolfcrypt/src/ext_mlkem.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,97 @@ int wc_MlKemKey_Init(MlKemKey* key, int type, void* heap, int devId)

/* Keep type for parameters. */
key->type = type;
key->heap = heap;

#ifdef WOLF_CRYPTO_CB
key->devCtx = NULL;
key->devId = devId;
#endif
#ifdef WOLF_PRIVATE_KEY_ID
key->idLen = 0;
key->labelLen = 0;
#endif
}

(void)heap;
(void)devId;

return ret;
}

#ifdef WOLF_PRIVATE_KEY_ID
int wc_MlKemKey_Init_Id(MlKemKey* key, const unsigned char* id, int len,
void* heap, int devId)
{
int ret = 0;

if (key == NULL) {
ret = BAD_FUNC_ARG;
}
if (ret == 0 && (len < 0 || len > MLKEM_MAX_ID_LEN)) {
ret = BUFFER_E;
}

if (ret == 0) {
ret = wc_MlKemKey_Init(key, WC_ML_KEM_1024, heap, devId);
}
if (ret == 0 && id != NULL && len != 0) {
XMEMCPY(key->id, id, (size_t)len);
key->idLen = len;
}

return ret;
}

int wc_MlKemKey_Init_Label(MlKemKey* key, const char* label, void* heap,
int devId)
{
int ret = 0;
int labelLen = 0;

if (key == NULL || label == NULL) {
ret = BAD_FUNC_ARG;
}
if (ret == 0) {
labelLen = (int)XSTRLEN(label);
if ((labelLen == 0) || (labelLen > MLKEM_MAX_LABEL_LEN)) {
ret = BUFFER_E;
}
}
if (ret == 0) {
ret = wc_MlKemKey_Init(key, WC_ML_KEM_1024, heap, devId);
}
if (ret == 0) {
XMEMCPY(key->label, label, (size_t)labelLen);
key->labelLen = labelLen;
}

return ret;
}
#endif

/**
* Free the Kyber key object.
*
* @param [in, out] key Kyber key object to dispose of.
*/
int wc_MlKemKey_Free(MlKemKey* key)
{
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
int ret = 0;
#endif

if (key != NULL) {
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
if (key->devId != INVALID_DEVID) {
ret = wc_CryptoCb_Free(key->devId, WC_ALGO_TYPE_PK,
WC_PK_TYPE_PQC_KEM_KEYGEN, WC_PQC_KEM_TYPE_KYBER, (void*)key);
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
return ret;
}
/* fall-through to software cleanup */
}
(void)ret;
#endif
/* Ensure all private data is zeroed. */
ForceZero(key, sizeof(*key));
}
Expand Down Expand Up @@ -391,6 +461,10 @@ int wc_MlKemKey_MakeKey(MlKemKey* key, WC_RNG* rng)
OQS_SUCCESS) {
ret = BAD_FUNC_ARG;
}
else {
key->pubKeySet = 1;
key->prvKeySet = 1;
}
}
wolfSSL_liboqsRngMutexUnlock();
OQS_KEM_free(kem);
Expand Down Expand Up @@ -639,6 +713,7 @@ int wc_MlKemKey_DecodePrivateKey(MlKemKey* key, const unsigned char* in,

if (ret == 0) {
XMEMCPY(key->priv, in, privLen);
key->prvKeySet = 1;
}

return ret;
Expand Down Expand Up @@ -679,6 +754,7 @@ int wc_MlKemKey_DecodePublicKey(MlKemKey* key, const unsigned char* in,

if (ret == 0) {
XMEMCPY(key->pub, in, pubLen);
key->pubKeySet = 1;
}

return ret;
Expand Down
78 changes: 77 additions & 1 deletion wolfcrypt/src/wc_mlkem.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
#include <wolfssl/wolfcrypt/wc_mlkem.h>
#include <wolfssl/wolfcrypt/hash.h>
#include <wolfssl/wolfcrypt/memory.h>
#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
#endif

#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
Expand Down Expand Up @@ -298,9 +301,13 @@ int wc_MlKemKey_Init(MlKemKey* key, int type, void* heap, int devId)
/* Cache heap pointer. */
key->heap = heap;
#ifdef WOLF_CRYPTO_CB
/* Cache device id - not used in this algorithm yet. */
key->devCtx = NULL;
key->devId = devId;
#endif
#ifdef WOLF_PRIVATE_KEY_ID
key->idLen = 0;
key->labelLen = 0;
#endif
key->flags = 0;

/* Zero out all data. */
Expand All @@ -322,6 +329,60 @@ int wc_MlKemKey_Init(MlKemKey* key, int type, void* heap, int devId)
return ret;
}

#ifdef WOLF_PRIVATE_KEY_ID
int wc_MlKemKey_Init_Id(MlKemKey* key, const unsigned char* id, int len,
void* heap, int devId)
{
int ret = 0;

if (key == NULL) {
ret = BAD_FUNC_ARG;
}
if (ret == 0 && (len < 0 || len > MLKEM_MAX_ID_LEN)) {
ret = BUFFER_E;
}

if (ret == 0) {
/* Use max level so PKCS#11 lookup has a key object to operate on. */
ret = wc_MlKemKey_Init(key, WC_ML_KEM_1024, heap, devId);
}
if (ret == 0 && id != NULL && len != 0) {
XMEMCPY(key->id, id, (size_t)len);
key->idLen = len;
}

return ret;
}

int wc_MlKemKey_Init_Label(MlKemKey* key, const char* label, void* heap,
int devId)
{
int ret = 0;
int labelLen = 0;

if (key == NULL || label == NULL) {
ret = BAD_FUNC_ARG;
}
if (ret == 0) {
labelLen = (int)XSTRLEN(label);
if ((labelLen == 0) || (labelLen > MLKEM_MAX_LABEL_LEN)) {
ret = BUFFER_E;
}
}

if (ret == 0) {
/* Use max level so PKCS#11 lookup has a key object to operate on. */
ret = wc_MlKemKey_Init(key, WC_ML_KEM_1024, heap, devId);
}
if (ret == 0) {
XMEMCPY(key->label, label, (size_t)labelLen);
key->labelLen = labelLen;
}

return ret;
}
#endif

/**
* Free the Kyber key object.
*
Expand All @@ -330,7 +391,22 @@ int wc_MlKemKey_Init(MlKemKey* key, int type, void* heap, int devId)
*/
int wc_MlKemKey_Free(MlKemKey* key)
{
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
int ret = 0;
#endif

if (key != NULL) {
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
if (key->devId != INVALID_DEVID) {
ret = wc_CryptoCb_Free(key->devId, WC_ALGO_TYPE_PK,
WC_PK_TYPE_PQC_KEM_KEYGEN, WC_PQC_KEM_TYPE_KYBER, (void*)key);
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
return ret;
}
/* fall-through to software cleanup */
}
(void)ret;
#endif
/* Dispose of PRF object. */
mlkem_prf_free(&key->prf);
/* Dispose of hash object. */
Expand Down
Loading
Loading