Skip to content

Commit 66c3130

Browse files
fix XMSS heap hint use
1 parent 6b84b78 commit 66c3130

3 files changed

Lines changed: 36 additions & 30 deletions

File tree

wolfcrypt/src/wc_xmss.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ static void wc_xmss_digest_free(XmssState* state)
124124
* @return Other negative when digest algorithm initialization failed.
125125
*/
126126
static WC_INLINE int wc_xmss_state_init(XmssState* state,
127-
const XmssParams* params)
127+
const XmssParams* params, void* heap)
128128
{
129129
state->params = params;
130+
state->heap = heap;
130131
state->ret = 0;
131132
return wc_xmss_digest_init(state);
132133
}
@@ -681,7 +682,7 @@ static int wc_xmsskey_alloc_sk(XmssKey* key)
681682
}
682683
if (ret == 0) {
683684
/* Allocate a buffer to hold secret key. */
684-
key->sk = (unsigned char *)XMALLOC(key->sk_len, NULL,
685+
key->sk = (unsigned char *)XMALLOC(key->sk_len, key->heap,
685686
DYNAMIC_TYPE_TMP_BUFFER);
686687
if (key->sk == NULL) {
687688
WOLFSSL_MSG("error: malloc XMSS key->sk failed");
@@ -733,12 +734,12 @@ static WC_INLINE int wc_xmsskey_signupdate(XmssKey* key, byte* sig,
733734
if (ret == 0) {
734735
WC_DECLARE_VAR(state, XmssState, 1, 0);
735736

736-
WC_ALLOC_VAR_EX(state, XmssState, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
737-
ret=MEMORY_E);
737+
WC_ALLOC_VAR_EX(state, XmssState, 1, key->heap,
738+
DYNAMIC_TYPE_TMP_BUFFER, ret=MEMORY_E);
738739
if (WC_VAR_OK(state))
739740
{
740741
/* Initialize state for use in signing. */
741-
ret = wc_xmss_state_init(state, key->params);
742+
ret = wc_xmss_state_init(state, key->params, key->heap);
742743
if (ret == 0) {
743744
/* Read was good. Now sign and update the secret key in memory.
744745
*/
@@ -766,7 +767,7 @@ static WC_INLINE int wc_xmsskey_signupdate(XmssKey* key, byte* sig,
766767
/* Free state after use. */
767768
wc_xmss_state_free(state);
768769
}
769-
WC_FREE_VAR_EX(state, NULL, DYNAMIC_TYPE_TMP_BUFFER);
770+
WC_FREE_VAR_EX(state, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
770771
}
771772
}
772773

@@ -814,7 +815,6 @@ int wc_XmssKey_Init(XmssKey* key, void* heap, int devId)
814815
{
815816
int ret = 0;
816817

817-
(void) heap;
818818
(void) devId;
819819

820820
/* Validate parameters. */
@@ -825,6 +825,7 @@ int wc_XmssKey_Init(XmssKey* key, void* heap, int devId)
825825
if (ret == 0) {
826826
/* Zeroize key and set state to initialized. */
827827
ForceZero(key, sizeof(XmssKey));
828+
key->heap = heap;
828829
key->state = WC_XMSS_STATE_INITED;
829830
}
830831

@@ -906,7 +907,7 @@ void wc_XmssKey_Free(XmssKey* key)
906907
if (key->sk != NULL) {
907908
/* Zeroize private key. */
908909
ForceZero(key->sk, key->sk_len);
909-
XFREE(key->sk, NULL, DYNAMIC_TYPE_TMP_BUFFER);
910+
XFREE(key->sk, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
910911
key->sk = NULL;
911912
key->sk_len = 0;
912913
}
@@ -1078,7 +1079,7 @@ int wc_XmssKey_MakeKey(XmssKey* key, WC_RNG* rng)
10781079
}
10791080
#ifdef WOLFSSL_SMALL_STACK
10801081
if (ret == 0) {
1081-
seed = (unsigned char*)XMALLOC(3 * key->params->n, NULL,
1082+
seed = (unsigned char*)XMALLOC(3 * key->params->n, key->heap,
10821083
DYNAMIC_TYPE_TMP_BUFFER);
10831084
if (seed == NULL) {
10841085
ret = MEMORY_E;
@@ -1094,12 +1095,12 @@ int wc_XmssKey_MakeKey(XmssKey* key, WC_RNG* rng)
10941095
if (ret == 0) {
10951096
WC_DECLARE_VAR(state, XmssState, 1, 0);
10961097

1097-
WC_ALLOC_VAR_EX(state, XmssState, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
1098-
ret=MEMORY_E);
1098+
WC_ALLOC_VAR_EX(state, XmssState, 1, key->heap,
1099+
DYNAMIC_TYPE_TMP_BUFFER, ret=MEMORY_E);
10991100
if (WC_VAR_OK(state))
11001101
{
11011102
/* Initialize state for use in key generation. */
1102-
ret = wc_xmss_state_init(state, key->params);
1103+
ret = wc_xmss_state_init(state, key->params, key->heap);
11031104
if (ret == 0) {
11041105
/* Finally make the private/public key pair. Immediately write
11051106
* it to NV storage and then clear from memory. */
@@ -1120,7 +1121,7 @@ int wc_XmssKey_MakeKey(XmssKey* key, WC_RNG* rng)
11201121
/* Free state after use. */
11211122
wc_xmss_state_free(state);
11221123
}
1123-
WC_FREE_VAR_EX(state, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1124+
WC_FREE_VAR_EX(state, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
11241125
}
11251126
}
11261127

@@ -1141,7 +1142,7 @@ int wc_XmssKey_MakeKey(XmssKey* key, WC_RNG* rng)
11411142
key->state = WC_XMSS_STATE_OK;
11421143
}
11431144

1144-
WC_FREE_VAR_EX(seed, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1145+
WC_FREE_VAR_EX(seed, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
11451146
return ret;
11461147
}
11471148

@@ -1618,19 +1619,19 @@ int wc_XmssKey_Verify(XmssKey* key, const byte* sig, word32 sigLen,
16181619
if (ret == 0) {
16191620
WC_DECLARE_VAR(state, XmssState, 1, 0);
16201621

1621-
WC_ALLOC_VAR_EX(state, XmssState, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
1622-
ret=MEMORY_E);
1622+
WC_ALLOC_VAR_EX(state, XmssState, 1, key->heap,
1623+
DYNAMIC_TYPE_TMP_BUFFER, ret=MEMORY_E);
16231624
if (WC_VAR_OK(state))
16241625
{
16251626
/* Initialize state for use in verification. */
1626-
ret = wc_xmss_state_init(state, key->params);
1627+
ret = wc_xmss_state_init(state, key->params, key->heap);
16271628
if (ret == 0) {
16281629
/* Verify using either XMSS^MT function as it works for both. */
16291630
ret = wc_xmssmt_verify(state, m, mLen, sig, key->pk);
16301631
/* Free state after use. */
16311632
wc_xmss_state_free(state);
16321633
}
1633-
WC_FREE_VAR_EX(state, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1634+
WC_FREE_VAR_EX(state, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
16341635
}
16351636
}
16361637

wolfcrypt/src/wc_xmss_impl.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,14 +2634,15 @@ static void wc_xmss_bds_state_treehash_set(BdsState* bds, int i,
26342634
* @return 0 on success.
26352635
* @return MEMORY_E on dynamic memory allocation failure.
26362636
*/
2637-
static int wc_xmss_bds_state_alloc(const XmssParams* params, BdsState** bds)
2637+
static int wc_xmss_bds_state_alloc(const XmssParams* params, BdsState** bds,
2638+
void* heap)
26382639
{
26392640
const word8 cnt = 2 * params->d - 1;
26402641
int ret = 0;
26412642

26422643
if (*bds == NULL) {
26432644
/* Allocate memory for BDS states. */
2644-
*bds = (BdsState*)XMALLOC(sizeof(BdsState) * cnt, NULL,
2645+
*bds = (BdsState*)XMALLOC(sizeof(BdsState) * cnt, heap,
26452646
DYNAMIC_TYPE_TMP_BUFFER);
26462647
if (*bds == NULL) {
26472648
ret = MEMORY_E;
@@ -2657,11 +2658,12 @@ static int wc_xmss_bds_state_alloc(const XmssParams* params, BdsState** bds)
26572658
/* Dispose of allocated memory associated with BDS state.
26582659
*
26592660
* @param [in] bds BDS state.
2661+
* @param [in] heap Dynamic memory hint.
26602662
*/
2661-
static void wc_xmss_bds_state_free(BdsState* bds)
2663+
static void wc_xmss_bds_state_free(BdsState* bds, void* heap)
26622664
{
26632665
/* BDS states was allocated - must free. */
2664-
XFREE(bds, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2666+
XFREE(bds, heap, DYNAMIC_TYPE_TMP_BUFFER);
26652667
}
26662668

26672669
/* Load the BDS state from the secret/private key.
@@ -3315,7 +3317,7 @@ int wc_xmss_keygen(XmssState* state, const unsigned char* seed,
33153317

33163318
#ifdef WOLFSSL_SMALL_STACK
33173319
/* Allocate memory for tree hash instances and put in BDS state. */
3318-
ret = wc_xmss_bds_state_alloc(params, &bds);
3320+
ret = wc_xmss_bds_state_alloc(params, &bds, state->heap);
33193321
if (ret == 0)
33203322
#endif
33213323
{
@@ -3361,7 +3363,7 @@ int wc_xmss_keygen(XmssState* state, const unsigned char* seed,
33613363

33623364
#ifdef WOLFSSL_SMALL_STACK
33633365
/* Dispose of allocated data of BDS states. */
3364-
wc_xmss_bds_state_free(bds);
3366+
wc_xmss_bds_state_free(bds, state->heap);
33653367
#endif
33663368
return ret;
33673369
#else
@@ -3426,7 +3428,7 @@ int wc_xmss_sign(XmssState* state, const unsigned char* m, word32 mlen,
34263428

34273429
#ifdef WOLFSSL_SMALL_STACK
34283430
/* Allocate memory for tree hash instances and put in BDS state. */
3429-
ret = wc_xmss_bds_state_alloc(params, &bds);
3431+
ret = wc_xmss_bds_state_alloc(params, &bds, state->heap);
34303432
if (ret == 0)
34313433
#endif
34323434
{
@@ -3515,7 +3517,7 @@ int wc_xmss_sign(XmssState* state, const unsigned char* m, word32 mlen,
35153517

35163518
#ifdef WOLFSSL_SMALL_STACK
35173519
/* Dispose of allocated data of BDS states. */
3518-
wc_xmss_bds_state_free(bds);
3520+
wc_xmss_bds_state_free(bds, state->heap);
35193521
#endif
35203522
return ret;
35213523
#else
@@ -3599,7 +3601,7 @@ int wc_xmssmt_keygen(XmssState* state, const unsigned char* seed,
35993601
BdsState* bds = NULL;
36003602

36013603
/* Allocate memory for BDS states and tree hash instances. */
3602-
ret = wc_xmss_bds_state_alloc(params, &bds);
3604+
ret = wc_xmss_bds_state_alloc(params, &bds, state->heap);
36033605
if (ret == 0) {
36043606
/* Load the BDS state from secret/private key. */
36053607
ret = wc_xmss_bds_state_load(state, sk, bds, &wots_sigs);
@@ -3655,7 +3657,7 @@ int wc_xmssmt_keygen(XmssState* state, const unsigned char* seed,
36553657
}
36563658

36573659
/* Dispose of allocated data of BDS states. */
3658-
wc_xmss_bds_state_free(bds);
3660+
wc_xmss_bds_state_free(bds, state->heap);
36593661
return ret;
36603662
}
36613663

@@ -4029,7 +4031,7 @@ int wc_xmssmt_sign(XmssState* state, const unsigned char* m, word32 mlen,
40294031
BdsState* bds = NULL;
40304032

40314033
/* Allocate memory for BDS states and tree hash instances. */
4032-
ret = wc_xmss_bds_state_alloc(params, &bds);
4034+
ret = wc_xmss_bds_state_alloc(params, &bds, state->heap);
40334035
if (ret == 0) {
40344036
/* Load the BDS state from secret/private key. */
40354037
ret = wc_xmss_bds_state_load(state, sk, bds, &wots_sigs);
@@ -4069,7 +4071,7 @@ int wc_xmssmt_sign(XmssState* state, const unsigned char* m, word32 mlen,
40694071
}
40704072

40714073
/* Dispose of allocated data of BDS states. */
4072-
wc_xmss_bds_state_free(bds);
4074+
wc_xmss_bds_state_free(bds, state->heap);
40734075
return ret;
40744076
}
40754077

wolfssl/wolfcrypt/wc_xmss.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,15 @@ struct XmssKey {
226226
/* Context arg passed to callbacks. */
227227
void* context;
228228
#endif /* ifndef WOLFSSL_XMSS_VERIFY_ONLY */
229+
/* Dynamic memory hint. */
230+
void* heap;
229231
/* State of key. */
230232
enum wc_XmssState state;
231233
};
232234

233235
typedef struct XmssState {
234236
const XmssParams* params;
237+
void* heap;
235238

236239
/* Digest is assumed to be at the end. */
237240
union {

0 commit comments

Comments
 (0)