Skip to content

Commit 78564f0

Browse files
fix XMSS heap hint use
1 parent e5d27b6 commit 78564f0

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
@@ -129,9 +129,10 @@ static void wc_xmss_digest_free(XmssState* state)
129129
* @return Other negative when digest algorithm initialization failed.
130130
*/
131131
static WC_INLINE int wc_xmss_state_init(XmssState* state,
132-
const XmssParams* params)
132+
const XmssParams* params, void* heap)
133133
{
134134
state->params = params;
135+
state->heap = heap;
135136
state->ret = 0;
136137
return wc_xmss_digest_init(state);
137138
}
@@ -686,7 +687,7 @@ static int wc_xmsskey_alloc_sk(XmssKey* key)
686687
}
687688
if (ret == 0) {
688689
/* Allocate a buffer to hold secret key. */
689-
key->sk = (unsigned char *)XMALLOC(key->sk_len, NULL,
690+
key->sk = (unsigned char *)XMALLOC(key->sk_len, key->heap,
690691
DYNAMIC_TYPE_TMP_BUFFER);
691692
if (key->sk == NULL) {
692693
WOLFSSL_MSG("error: malloc XMSS key->sk failed");
@@ -738,12 +739,12 @@ static WC_INLINE int wc_xmsskey_signupdate(XmssKey* key, byte* sig,
738739
if (ret == 0) {
739740
WC_DECLARE_VAR(state, XmssState, 1, 0);
740741

741-
WC_ALLOC_VAR_EX(state, XmssState, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
742-
ret=MEMORY_E);
742+
WC_ALLOC_VAR_EX(state, XmssState, 1, key->heap,
743+
DYNAMIC_TYPE_TMP_BUFFER, ret=MEMORY_E);
743744
if (WC_VAR_OK(state))
744745
{
745746
/* Initialize state for use in signing. */
746-
ret = wc_xmss_state_init(state, key->params);
747+
ret = wc_xmss_state_init(state, key->params, key->heap);
747748
if (ret == 0) {
748749
/* Read was good. Now sign and update the secret key in memory.
749750
*/
@@ -771,7 +772,7 @@ static WC_INLINE int wc_xmsskey_signupdate(XmssKey* key, byte* sig,
771772
/* Free state after use. */
772773
wc_xmss_state_free(state);
773774
}
774-
WC_FREE_VAR_EX(state, NULL, DYNAMIC_TYPE_TMP_BUFFER);
775+
WC_FREE_VAR_EX(state, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
775776
}
776777
}
777778

@@ -819,7 +820,6 @@ int wc_XmssKey_Init(XmssKey* key, void* heap, int devId)
819820
{
820821
int ret = 0;
821822

822-
(void) heap;
823823
(void) devId;
824824

825825
/* Validate parameters. */
@@ -830,6 +830,7 @@ int wc_XmssKey_Init(XmssKey* key, void* heap, int devId)
830830
if (ret == 0) {
831831
/* Zeroize key and set state to initialized. */
832832
ForceZero(key, sizeof(XmssKey));
833+
key->heap = heap;
833834
key->state = WC_XMSS_STATE_INITED;
834835
}
835836

@@ -911,7 +912,7 @@ void wc_XmssKey_Free(XmssKey* key)
911912
if (key->sk != NULL) {
912913
/* Zeroize private key. */
913914
ForceZero(key->sk, key->sk_len);
914-
XFREE(key->sk, NULL, DYNAMIC_TYPE_TMP_BUFFER);
915+
XFREE(key->sk, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
915916
key->sk = NULL;
916917
key->sk_len = 0;
917918
}
@@ -1083,7 +1084,7 @@ int wc_XmssKey_MakeKey(XmssKey* key, WC_RNG* rng)
10831084
}
10841085
#ifdef WOLFSSL_SMALL_STACK
10851086
if (ret == 0) {
1086-
seed = (unsigned char*)XMALLOC(3 * key->params->n, NULL,
1087+
seed = (unsigned char*)XMALLOC(3 * key->params->n, key->heap,
10871088
DYNAMIC_TYPE_TMP_BUFFER);
10881089
if (seed == NULL) {
10891090
ret = MEMORY_E;
@@ -1099,12 +1100,12 @@ int wc_XmssKey_MakeKey(XmssKey* key, WC_RNG* rng)
10991100
if (ret == 0) {
11001101
WC_DECLARE_VAR(state, XmssState, 1, 0);
11011102

1102-
WC_ALLOC_VAR_EX(state, XmssState, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
1103-
ret=MEMORY_E);
1103+
WC_ALLOC_VAR_EX(state, XmssState, 1, key->heap,
1104+
DYNAMIC_TYPE_TMP_BUFFER, ret=MEMORY_E);
11041105
if (WC_VAR_OK(state))
11051106
{
11061107
/* Initialize state for use in key generation. */
1107-
ret = wc_xmss_state_init(state, key->params);
1108+
ret = wc_xmss_state_init(state, key->params, key->heap);
11081109
if (ret == 0) {
11091110
/* Finally make the private/public key pair. Immediately write
11101111
* it to NV storage and then clear from memory. */
@@ -1125,7 +1126,7 @@ int wc_XmssKey_MakeKey(XmssKey* key, WC_RNG* rng)
11251126
/* Free state after use. */
11261127
wc_xmss_state_free(state);
11271128
}
1128-
WC_FREE_VAR_EX(state, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1129+
WC_FREE_VAR_EX(state, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
11291130
}
11301131
}
11311132

@@ -1146,7 +1147,7 @@ int wc_XmssKey_MakeKey(XmssKey* key, WC_RNG* rng)
11461147
key->state = WC_XMSS_STATE_OK;
11471148
}
11481149

1149-
WC_FREE_VAR_EX(seed, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1150+
WC_FREE_VAR_EX(seed, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
11501151
return ret;
11511152
}
11521153

@@ -1623,19 +1624,19 @@ int wc_XmssKey_Verify(XmssKey* key, const byte* sig, word32 sigLen,
16231624
if (ret == 0) {
16241625
WC_DECLARE_VAR(state, XmssState, 1, 0);
16251626

1626-
WC_ALLOC_VAR_EX(state, XmssState, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
1627-
ret=MEMORY_E);
1627+
WC_ALLOC_VAR_EX(state, XmssState, 1, key->heap,
1628+
DYNAMIC_TYPE_TMP_BUFFER, ret=MEMORY_E);
16281629
if (WC_VAR_OK(state))
16291630
{
16301631
/* Initialize state for use in verification. */
1631-
ret = wc_xmss_state_init(state, key->params);
1632+
ret = wc_xmss_state_init(state, key->params, key->heap);
16321633
if (ret == 0) {
16331634
/* Verify using either XMSS^MT function as it works for both. */
16341635
ret = wc_xmssmt_verify(state, m, mLen, sig, key->pk);
16351636
/* Free state after use. */
16361637
wc_xmss_state_free(state);
16371638
}
1638-
WC_FREE_VAR_EX(state, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1639+
WC_FREE_VAR_EX(state, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
16391640
}
16401641
}
16411642

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
@@ -358,12 +358,15 @@ typedef struct XmssKey {
358358
/* Context arg passed to callbacks. */
359359
void* context;
360360
#endif /* ifndef WOLFSSL_XMSS_VERIFY_ONLY */
361+
/* Dynamic memory hint. */
362+
void* heap;
361363
/* State of key. */
362364
enum wc_XmssState state;
363365
} XmssKey;
364366

365367
typedef struct XmssState {
366368
const XmssParams* params;
369+
void* heap;
367370

368371
/* Digest is assumed to be at the end. */
369372
union {

0 commit comments

Comments
 (0)