Skip to content

Commit 7e0a855

Browse files
authored
Merge pull request wolfSSL#9638 from douzzer/20260109-rng_bank-fixes
20260109-rng_bank-fixes
2 parents 3f8efdc + 902164c commit 7e0a855

File tree

5 files changed

+243
-89
lines changed

5 files changed

+243
-89
lines changed

.wolfssl_known_macro_extras

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,7 @@ __WATCOMC__
10741074
__WATCOM_INT64__
10751075
__XC32
10761076
__XTENSA__
1077+
__ZEPHYR__
10771078
__aarch64__
10781079
__alpha__
10791080
__arch64__
@@ -1105,7 +1106,6 @@ __thumb__
11051106
__ti__
11061107
__x86_64__
11071108
__xtensa__
1108-
__ZEPHYR__
11091109
byte
11101110
configTICK_RATE_HZ
11111111
fallthrough

wolfcrypt/src/rng_bank.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,16 @@ WOLFSSL_API int wc_rng_bank_init(
5050
ctx->flags = flags | WC_RNG_BANK_FLAG_INITED;
5151
ctx->heap = heap;
5252

53+
#ifdef WC_RNG_BANK_STATIC
54+
if (n_rngs > WC_RNG_BANK_STATIC_SIZE)
55+
return BAD_LENGTH_E;
56+
#else
5357
ctx->rngs = (struct wc_rng_bank_inst *)
5458
XMALLOC(sizeof(*ctx->rngs) * (size_t)n_rngs,
5559
heap, DYNAMIC_TYPE_RNG);
5660
if (! ctx->rngs)
5761
ret = MEMORY_E;
62+
#endif
5863

5964
if (ret == 0) {
6065
XMEMSET(ctx->rngs, 0, sizeof(*ctx->rngs) * (size_t)n_rngs);
@@ -116,6 +121,7 @@ WOLFSSL_API int wc_rng_bank_init(
116121
return ret;
117122
}
118123

124+
#ifndef WC_RNG_BANK_STATIC
119125
WOLFSSL_API int wc_rng_bank_new(
120126
struct wc_rng_bank **ctx,
121127
int n_rngs,
@@ -142,6 +148,7 @@ WOLFSSL_API int wc_rng_bank_new(
142148

143149
return ret;
144150
}
151+
#endif /* !WC_RNG_BANK_STATIC */
145152

146153
WOLFSSL_API int wc_rng_bank_set_affinity_handlers(
147154
struct wc_rng_bank *ctx,
@@ -181,7 +188,10 @@ WOLFSSL_API int wc_rng_bank_fini(struct wc_rng_bank *ctx) {
181188
if (wolfSSL_RefCur(ctx->refcount) > 1)
182189
return BUSY_E;
183190

184-
if (ctx->rngs) {
191+
#ifndef WC_RNG_BANK_STATIC
192+
if (ctx->rngs)
193+
#endif
194+
{
185195
for (i = 0; i < ctx->n_rngs; ++i) {
186196
if (ctx->rngs[i].lock != 0) {
187197
/* better to leak than to crash. */
@@ -198,8 +208,10 @@ WOLFSSL_API int wc_rng_bank_fini(struct wc_rng_bank *ctx) {
198208
wc_FreeRng(&ctx->rngs[i].rng);
199209
}
200210

211+
#ifndef WC_RNG_BANK_STATIC
201212
XFREE(ctx->rngs, ctx->heap, DYNAMIC_TYPE_RNG);
202213
ctx->rngs = NULL;
214+
#endif
203215
ctx->n_rngs = 0;
204216
}
205217

@@ -211,6 +223,7 @@ WOLFSSL_API int wc_rng_bank_fini(struct wc_rng_bank *ctx) {
211223
return 0;
212224
}
213225

226+
#ifndef WC_RNG_BANK_STATIC
214227
WOLFSSL_API int wc_rng_bank_free(struct wc_rng_bank **ctx) {
215228
int ret;
216229
void *heap;
@@ -232,6 +245,7 @@ WOLFSSL_API int wc_rng_bank_free(struct wc_rng_bank **ctx) {
232245

233246
return ret;
234247
}
248+
#endif /* !WC_RNG_BANK_STATIC */
235249

236250
/* wc_rng_bank_checkout() uses atomic operations to get exclusive ownership of a
237251
* DRBG without delay. It expects to be called in uninterruptible context,
@@ -631,7 +645,7 @@ WOLFSSL_API int wc_rng_bank_reseed(struct wc_rng_bank *bank,
631645
"ERROR: wc_crng_reseed() wc_RNG_GenerateBlock() "
632646
"for DRBG #%d returned %d.", n, ret);
633647
#endif
634-
wc_rng_bank_checkin(bank, &drbg);
648+
(void)wc_rng_bank_checkin(bank, &drbg);
635649
if (ret == WC_NO_ERR_TRACE(WC_TIMEOUT_E))
636650
return ret;
637651
ret = WC_CHECK_FOR_INTR_SIGNALS();
@@ -640,7 +654,7 @@ WOLFSSL_API int wc_rng_bank_reseed(struct wc_rng_bank *bank,
640654
WC_RELAX_LONG_LOOP();
641655
}
642656
else {
643-
wc_rng_bank_checkin(bank, &drbg);
657+
(void)wc_rng_bank_checkin(bank, &drbg);
644658
}
645659
}
646660

@@ -694,6 +708,7 @@ WOLFSSL_API int wc_BankRef_Release(WC_RNG *rng)
694708
return ret;
695709
}
696710

711+
#ifndef WC_RNG_BANK_STATIC
697712
WOLFSSL_API int wc_rng_new_bankref(struct wc_rng_bank *bank, WC_RNG **rng) {
698713
int ret;
699714

@@ -717,6 +732,7 @@ WOLFSSL_API int wc_rng_new_bankref(struct wc_rng_bank *bank, WC_RNG **rng) {
717732

718733
return ret;
719734
}
735+
#endif /* !WC_RNG_BANK_STATIC */
720736

721737
#endif /* WC_DRBG_BANKREF */
722738

0 commit comments

Comments
 (0)