Skip to content

Commit e0913c4

Browse files
Merge pull request #9039 from tamasan238/for-pr-1
Add _new/_delete API for ML-KEM/ML-DSA
2 parents ffbcd4f + e9292e3 commit e0913c4

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
@@ -10586,6 +10586,52 @@ int wc_dilithium_verify_ctx_hash(const byte* sig, word32 sigLen,
1058610586
}
1058710587
#endif /* WOLFSSL_DILITHIUM_NO_VERIFY */
1058810588

10589+
#ifndef WC_NO_CONSTRUCTORS
10590+
/**
10591+
* Create a new dilithium key object.
10592+
*
10593+
* heap [in] Dynamic memory hint.
10594+
* devId [in] Device Id.
10595+
* returns MEMORY_E when dynamic memory allocation fails
10596+
*/
10597+
10598+
dilithium_key* wc_dilithium_new(void* heap, int devId)
10599+
{
10600+
int ret;
10601+
dilithium_key* key = (dilithium_key*)XMALLOC(sizeof(dilithium_key), heap,
10602+
DYNAMIC_TYPE_DILITHIUM);
10603+
if (key != NULL) {
10604+
ret = wc_dilithium_init_ex(key, heap, devId);
10605+
if (ret != 0) {
10606+
XFREE(key, heap, DYNAMIC_TYPE_DILITHIUM);
10607+
key = NULL;
10608+
}
10609+
}
10610+
10611+
return key;
10612+
}
10613+
10614+
/**
10615+
* Delete and free a dilithium key object.
10616+
*
10617+
* key [in] dilithium key object to delete.
10618+
* key_p [in, out] Pointer to key pointer to set to NULL.
10619+
* returns BAD_FUNC_ARG when key is NULL
10620+
*/
10621+
10622+
int wc_dilithium_delete(dilithium_key* key, dilithium_key** key_p)
10623+
{
10624+
if (key == NULL)
10625+
return BAD_FUNC_ARG;
10626+
wc_dilithium_free(key);
10627+
XFREE(key, key->heap, DYNAMIC_TYPE_DILITHIUM);
10628+
if (key_p != NULL)
10629+
*key_p = NULL;
10630+
10631+
return 0;
10632+
}
10633+
#endif /* !WC_NO_CONSTRUCTORS */
10634+
1058910635
/* Initialize the dilithium private/public key.
1059010636
*
1059110637
* 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
@@ -43699,6 +43699,9 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mlkem_test(void)
4369943699
#endif
4370043700
#endif
4370143701
#endif
43702+
#endif
43703+
#ifdef WOLFSSL_WC_MLKEM
43704+
MlKemKey *tmpKey = NULL;
4370243705
#endif
4370343706
int key_inited = 0;
4370443707
static const int testData[][4] = {
@@ -43847,6 +43850,15 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mlkem_test(void)
4384743850

4384843851
if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
4384943852
ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
43853+
43854+
#ifdef WOLFSSL_WC_MLKEM
43855+
tmpKey = wc_MlKemKey_New(testData[i][0], HEAP_HINT, INVALID_DEVID);
43856+
if (tmpKey == NULL)
43857+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
43858+
ret = wc_MlKemKey_Delete(tmpKey, &tmpKey);
43859+
if (ret != 0)
43860+
ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
43861+
#endif
4385043862
#endif
4385143863
}
4385243864

@@ -47084,6 +47096,7 @@ static wc_test_ret_t dilithium_param_test(int param, WC_RNG* rng)
4708447096
#ifndef WOLFSSL_DILITHIUM_NO_VERIFY
4708547097
int res = 0;
4708647098
#endif
47099+
dilithium_key* tmpKey = NULL;
4708747100
#endif
4708847101

4708947102
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
@@ -47129,6 +47142,14 @@ static wc_test_ret_t dilithium_param_test(int param, WC_RNG* rng)
4712947142
#endif
4713047143
#endif
4713147144

47145+
tmpKey = wc_dilithium_new(HEAP_HINT, INVALID_DEVID);
47146+
if (tmpKey == NULL)
47147+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
47148+
47149+
ret = wc_dilithium_delete(tmpKey, &tmpKey);
47150+
if (ret != 0)
47151+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
47152+
4713247153
out:
4713347154
wc_dilithium_free(key);
4713447155
#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
@@ -794,6 +794,11 @@ int wc_dilithium_verify_ctx_hash(const byte* sig, word32 sigLen,
794794
const byte* ctx, word32 ctxLen, int hashAlg, const byte* hash,
795795
word32 hashLen, int* res, dilithium_key* key);
796796

797+
WOLFSSL_API
798+
dilithium_key* wc_dilithium_new(void* heap, int devId);
799+
WOLFSSL_API
800+
int wc_dilithium_delete(dilithium_key* key, dilithium_key** key_p);
801+
797802
WOLFSSL_API
798803
int wc_dilithium_init(dilithium_key* key);
799804

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)