Skip to content

Commit e07d591

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

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43481,6 +43481,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mlkem_test(void)
4348143481
#endif
4348243482
#endif
4348343483
#endif
43484+
MlKemKey tempKey = NULL;
4348443485
int key_inited = 0;
4348543486
static const int testData[][4] = {
4348643487
#ifndef WOLFSSL_NO_ML_KEM
@@ -43629,6 +43630,12 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mlkem_test(void)
4362943630
if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
4363043631
ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
4363143632
#endif
43633+
tmpKey = wc_MlKemKey_New(testData[i][0], HEAP_HINT, INVALID_DEVID);
43634+
if (tmpKey == NULL)
43635+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
43636+
ret = wc_MlKemKey_Delete(tmpKey, &tmpKey);
43637+
if (ret != 0)
43638+
ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
4363243639
}
4363343640

4363443641
wc_FreeRng(&rng);
@@ -46865,6 +46872,7 @@ static wc_test_ret_t dilithium_param_test(int param, WC_RNG* rng)
4686546872
#ifndef WOLFSSL_DILITHIUM_NO_VERIFY
4686646873
int res = 0;
4686746874
#endif
46875+
dilithium_key* tmpKey = NULL;
4686846876
#endif
4686946877

4687046878
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
@@ -46910,6 +46918,14 @@ static wc_test_ret_t dilithium_param_test(int param, WC_RNG* rng)
4691046918
#endif
4691146919
#endif
4691246920

46921+
tmpKey = wc_dilithium_new(HEAP_HINT, INVALID_DEVID);
46922+
if (tmpKey == NULL)
46923+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
46924+
46925+
ret = wc_dilithium_delete(tmpKey, &tmpKey);
46926+
if (ret != 0)
46927+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
46928+
4691346929
out:
4691446930
wc_dilithium_free(key);
4691546931
#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)