Skip to content

Commit e9292e3

Browse files
committed
add _new/_delete API for ML-KEM/ML-DSA
1 parent a1e2ba2 commit e9292e3

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

wolfcrypt/src/dilithium.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8488,6 +8488,52 @@ int wc_dilithium_verify_ctx_hash(const byte* sig, word32 sigLen,
84888488
}
84898489
#endif /* WOLFSSL_DILITHIUM_NO_VERIFY */
84908490

8491+
#ifndef WC_NO_CONSTRUCTORS
8492+
/**
8493+
* Create a new dilithium key object.
8494+
*
8495+
* heap [in] Dynamic memory hint.
8496+
* devId [in] Device Id.
8497+
* returns MEMORY_E when dynamic memory allocation fails
8498+
*/
8499+
8500+
dilithium_key* wc_dilithium_new(void* heap, int devId)
8501+
{
8502+
int ret;
8503+
dilithium_key* key = (dilithium_key*)XMALLOC(sizeof(dilithium_key), heap,
8504+
DYNAMIC_TYPE_DILITHIUM);
8505+
if (key != NULL) {
8506+
ret = wc_dilithium_init_ex(key, heap, devId);
8507+
if (ret != 0) {
8508+
XFREE(key, heap, DYNAMIC_TYPE_DILITHIUM);
8509+
key = NULL;
8510+
}
8511+
}
8512+
8513+
return key;
8514+
}
8515+
8516+
/**
8517+
* Delete and free a dilithium key object.
8518+
*
8519+
* key [in] dilithium key object to delete.
8520+
* key_p [in, out] Pointer to key pointer to set to NULL.
8521+
* returns BAD_FUNC_ARG when key is NULL
8522+
*/
8523+
8524+
int wc_dilithium_delete(dilithium_key* key, dilithium_key** key_p)
8525+
{
8526+
if (key == NULL)
8527+
return BAD_FUNC_ARG;
8528+
wc_dilithium_free(key);
8529+
XFREE(key, key->heap, DYNAMIC_TYPE_DILITHIUM);
8530+
if (key_p != NULL)
8531+
*key_p = NULL;
8532+
8533+
return 0;
8534+
}
8535+
#endif /* !WC_NO_CONSTRUCTORS */
8536+
84918537
/* Initialize the dilithium private/public key.
84928538
*
84938539
* key [in] Dilithium key.

wolfcrypt/src/wc_mlkem.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,60 @@ volatile sword16 mlkem_opt_blocker = 0;
126126

127127
/******************************************************************************/
128128

129+
#ifndef WC_NO_CONSTRUCTORS
130+
/**
131+
* Create a new ML-KEM key object.
132+
*
133+
* Allocates and initializes a ML-KEM key object.
134+
*
135+
* @param [in] type Type of key:
136+
* WC_ML_KEM_512, WC_ML_KEM_768, WC_ML_KEM_1024,
137+
* KYBER512, KYBER768, KYBER1024.
138+
* @param [in] heap Dynamic memory hint.
139+
* @param [in] devId Device Id.
140+
* @return Pointer to new MlKemKey object, or NULL on failure.
141+
*/
142+
143+
MlKemKey* wc_MlKemKey_New(int type, void* heap, int devId)
144+
{
145+
int ret;
146+
MlKemKey* key = (MlKemKey*)XMALLOC(sizeof(MlKemKey), heap,
147+
DYNAMIC_TYPE_TMP_BUFFER);
148+
if (key != NULL) {
149+
ret = wc_MlKemKey_Init(key, type, heap, devId);
150+
if (ret != 0) {
151+
XFREE(key, heap, DYNAMIC_TYPE_TMP_BUFFER);
152+
key = NULL;
153+
}
154+
}
155+
156+
return key;
157+
}
158+
159+
/**
160+
* Delete and free a ML-KEM key object.
161+
*
162+
* Frees resources associated with a ML-KEM key object and sets pointer to NULL.
163+
*
164+
* @param [in] key ML-KEM key object to delete.
165+
* @param [in, out] key_p Pointer to key pointer to set to NULL.
166+
* @return 0 on success.
167+
* @return BAD_FUNC_ARG when key is NULL.
168+
*/
169+
170+
int wc_MlKemKey_Delete(MlKemKey* key, MlKemKey** key_p)
171+
{
172+
if (key == NULL)
173+
return BAD_FUNC_ARG;
174+
wc_MlKemKey_Free(key);
175+
XFREE(key, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
176+
if (key_p != NULL)
177+
*key_p = NULL;
178+
179+
return 0;
180+
}
181+
#endif /* !WC_NO_CONSTRUCTORS */
182+
129183
/**
130184
* Initialize the Kyber key.
131185
*

wolfcrypt/test/test.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43480,6 +43480,9 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mlkem_test(void)
4348043480
#endif
4348143481
#endif
4348243482
#endif
43483+
#endif
43484+
#ifdef WOLFSSL_WC_MLKEM
43485+
MlKemKey *tmpKey = NULL;
4348343486
#endif
4348443487
int key_inited = 0;
4348543488
static const int testData[][4] = {
@@ -43628,6 +43631,15 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mlkem_test(void)
4362843631

4362943632
if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
4363043633
ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
43634+
43635+
#ifdef WOLFSSL_WC_MLKEM
43636+
tmpKey = wc_MlKemKey_New(testData[i][0], HEAP_HINT, INVALID_DEVID);
43637+
if (tmpKey == NULL)
43638+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
43639+
ret = wc_MlKemKey_Delete(tmpKey, &tmpKey);
43640+
if (ret != 0)
43641+
ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
43642+
#endif
4363143643
#endif
4363243644
}
4363343645

@@ -46865,6 +46877,7 @@ static wc_test_ret_t dilithium_param_test(int param, WC_RNG* rng)
4686546877
#ifndef WOLFSSL_DILITHIUM_NO_VERIFY
4686646878
int res = 0;
4686746879
#endif
46880+
dilithium_key* tmpKey = NULL;
4686846881
#endif
4686946882

4687046883
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
@@ -46910,6 +46923,14 @@ static wc_test_ret_t dilithium_param_test(int param, WC_RNG* rng)
4691046923
#endif
4691146924
#endif
4691246925

46926+
tmpKey = wc_dilithium_new(HEAP_HINT, INVALID_DEVID);
46927+
if (tmpKey == NULL)
46928+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
46929+
46930+
ret = wc_dilithium_delete(tmpKey, &tmpKey);
46931+
if (ret != 0)
46932+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
46933+
4691346934
out:
4691446935
wc_dilithium_free(key);
4691546936
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)

wolfssl/wolfcrypt/dilithium.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,11 @@ int wc_dilithium_verify_ctx_hash(const byte* sig, word32 sigLen,
788788
const byte* ctx, word32 ctxLen, int hashAlg, const byte* hash,
789789
word32 hashLen, int* res, dilithium_key* key);
790790

791+
WOLFSSL_API
792+
dilithium_key* wc_dilithium_new(void* heap, int devId);
793+
WOLFSSL_API
794+
int wc_dilithium_delete(dilithium_key* key, dilithium_key** key_p);
795+
791796
WOLFSSL_API
792797
int wc_dilithium_init(dilithium_key* key);
793798

wolfssl/wolfcrypt/mlkem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ typedef struct MlKemKey MlKemKey;
313313
extern "C" {
314314
#endif
315315

316+
WOLFSSL_API MlKemKey* wc_MlKemKey_New(int type, void* heap, int devId);
317+
WOLFSSL_API int wc_MlKemKey_Delete(MlKemKey* key, MlKemKey** key_p);
318+
316319
WOLFSSL_API int wc_MlKemKey_Init(MlKemKey* key, int type, void* heap,
317320
int devId);
318321
WOLFSSL_API int wc_MlKemKey_Free(MlKemKey* key);

0 commit comments

Comments
 (0)