Skip to content

Commit b174fcd

Browse files
committed
wolfcrypt/src/random.c, wolfssl/wolfcrypt/random.h, wolfssl/wolfcrypt/wc_port.h, linuxkm/lkcapi_sha_glue.c: fixes from autotesting:
* refactor to eliminate recursion in wc_RNG_GenerateBlock(); * refactor enum wc_rng_bank_flags as word32 and macros; * fix -Wconversions, -Wunused, and stray EINVAL in wc_rng_bank_init(); * make struct wc_rng_bank_inst a top-level definition for C++ compat; * fix several bugprone-macro-parentheses.
1 parent 7a4452e commit b174fcd

File tree

4 files changed

+89
-72
lines changed

4 files changed

+89
-72
lines changed

linuxkm/lkcapi_sha_glue.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ static int wc_linuxkm_drbg_init_tfm(struct crypto_tfm *tfm)
995995
{
996996
struct wc_rng_bank *ctx = (struct wc_rng_bank *)crypto_tfm_ctx(tfm);
997997
int ret;
998-
enum wc_rng_bank_flags flags = WC_RNG_BANK_FLAG_CAN_WAIT;
998+
word32 flags = WC_RNG_BANK_FLAG_CAN_WAIT;
999999

10001000
if (wc_linuxkm_drbg_init_tfm_disable_vector_registers)
10011001
flags |= WC_RNG_BANK_FLAG_NO_VECTOR_OPS;
@@ -1048,7 +1048,7 @@ static struct wc_rng_bank_inst *linuxkm_get_drbg(struct crypto_rng *tfm) {
10481048
struct wc_rng_bank *ctx = (struct wc_rng_bank *)crypto_rng_ctx(tfm);
10491049
int err;
10501050
struct wc_rng_bank_inst *ret;
1051-
enum wc_rng_bank_flags flags =
1051+
word32 flags =
10521052
WC_RNG_BANK_FLAG_CAN_FAIL_OVER_INST |
10531053
WC_RNG_BANK_FLAG_CAN_WAIT |
10541054
WC_RNG_BANK_FLAG_PREFER_AFFINITY_INST;

wolfcrypt/src/random.c

Lines changed: 66 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,8 +1274,12 @@ static int PollAndReSeed(WC_RNG* rng)
12741274
#endif
12751275

12761276
/* place a generated block in output */
1277+
#ifdef WC_RNG_BANK_SUPPORT
1278+
static int wc_local_RNG_GenerateBlock(WC_RNG* rng, byte* output, word32 sz)
1279+
#else
12771280
WOLFSSL_ABI
12781281
int wc_RNG_GenerateBlock(WC_RNG* rng, byte* output, word32 sz)
1282+
#endif
12791283
{
12801284
int ret;
12811285

@@ -1285,46 +1289,6 @@ int wc_RNG_GenerateBlock(WC_RNG* rng, byte* output, word32 sz)
12851289
if (sz == 0)
12861290
return 0;
12871291

1288-
#ifdef WC_RNG_BANK_SUPPORT
1289-
if (rng->status == WC_DRBG_BANKREF) {
1290-
struct wc_rng_bank_inst *bank_inst = NULL;
1291-
1292-
if ((rng->bankref == NULL) ||
1293-
(! (rng->bankref->flags & WC_RNG_BANK_FLAG_INITED)))
1294-
{
1295-
return BAD_FUNC_ARG;
1296-
}
1297-
1298-
ret = wc_rng_bank_checkout(rng->bankref, &bank_inst, 0, 0,
1299-
WC_RNG_BANK_FLAG_CAN_FAIL_OVER_INST |
1300-
WC_RNG_BANK_FLAG_CAN_WAIT |
1301-
WC_RNG_BANK_FLAG_PREFER_AFFINITY_INST |
1302-
WC_RNG_BANK_FLAG_AFFINITY_LOCK);
1303-
if (ret != 0)
1304-
return ret;
1305-
if (bank_inst == NULL)
1306-
return BAD_STATE_E;
1307-
if (bank_inst->rng.status != WC_DRBG_OK) {
1308-
(void)wc_rng_bank_checkin(rng->bankref, bank_inst);
1309-
return BAD_STATE_E;
1310-
}
1311-
ret = wc_RNG_GenerateBlock(&bank_inst->rng, output, sz);
1312-
{
1313-
int checkin_ret = wc_rng_bank_checkin(rng->bankref, bank_inst);
1314-
if (checkin_ret != 0) {
1315-
#ifdef WC_VERBOSE_RNG
1316-
WOLFSSL_DEBUG_PRINTF(
1317-
"ERROR: wc_RNG_GenerateBlock() wc_rng_bank_checkin() "
1318-
"failed with err %d.", checkin_ret);
1319-
#endif
1320-
if (ret == 0)
1321-
ret = checkin_ret;
1322-
}
1323-
}
1324-
return ret;
1325-
}
1326-
#endif
1327-
13281292
#ifdef WOLF_CRYPTO_CB
13291293
#ifndef WOLF_CRYPTO_CB_FIND
13301294
if (rng->devId != INVALID_DEVID)
@@ -1416,6 +1380,55 @@ int wc_RNG_GenerateBlock(WC_RNG* rng, byte* output, word32 sz)
14161380
return ret;
14171381
}
14181382

1383+
#ifdef WC_RNG_BANK_SUPPORT
1384+
WOLFSSL_ABI
1385+
int wc_RNG_GenerateBlock(WC_RNG* rng, byte* output, word32 sz)
1386+
{
1387+
if ((rng == NULL) || (output == NULL))
1388+
return BAD_FUNC_ARG;
1389+
1390+
if (rng->status == WC_DRBG_BANKREF) {
1391+
int ret;
1392+
struct wc_rng_bank_inst *bank_inst = NULL;
1393+
1394+
if ((rng->bankref == NULL) ||
1395+
(! (rng->bankref->flags & WC_RNG_BANK_FLAG_INITED)))
1396+
{
1397+
return BAD_FUNC_ARG;
1398+
}
1399+
1400+
ret = wc_rng_bank_checkout(rng->bankref, &bank_inst, 0, 0,
1401+
WC_RNG_BANK_FLAG_CAN_FAIL_OVER_INST |
1402+
WC_RNG_BANK_FLAG_CAN_WAIT |
1403+
WC_RNG_BANK_FLAG_PREFER_AFFINITY_INST |
1404+
WC_RNG_BANK_FLAG_AFFINITY_LOCK);
1405+
if (ret != 0)
1406+
return ret;
1407+
if (bank_inst == NULL)
1408+
return BAD_STATE_E;
1409+
if (bank_inst->rng.status != WC_DRBG_OK) {
1410+
(void)wc_rng_bank_checkin(rng->bankref, bank_inst);
1411+
return BAD_STATE_E;
1412+
}
1413+
ret = wc_local_RNG_GenerateBlock(&bank_inst->rng, output, sz);
1414+
{
1415+
int checkin_ret = wc_rng_bank_checkin(rng->bankref, bank_inst);
1416+
if (checkin_ret != 0) {
1417+
#ifdef WC_VERBOSE_RNG
1418+
WOLFSSL_DEBUG_PRINTF(
1419+
"ERROR: wc_RNG_GenerateBlock() wc_rng_bank_checkin() "
1420+
"failed with err %d.", checkin_ret);
1421+
#endif
1422+
if (ret == 0)
1423+
ret = checkin_ret;
1424+
}
1425+
}
1426+
return ret;
1427+
}
1428+
else
1429+
return wc_local_RNG_GenerateBlock(rng, output, sz);
1430+
}
1431+
#endif
14191432

14201433
int wc_RNG_GenerateByte(WC_RNG* rng, byte* b)
14211434
{
@@ -3833,7 +3846,7 @@ int wc_hwrng_generate_block(byte *output, word32 sz)
38333846
WOLFSSL_API int wc_rng_bank_init(
38343847
struct wc_rng_bank *ctx,
38353848
int n_rngs,
3836-
enum wc_rng_bank_flags flags,
3849+
word32 flags,
38373850
int timeout_secs,
38383851
void *heap)
38393852
{
@@ -3853,17 +3866,20 @@ WOLFSSL_API int wc_rng_bank_init(
38533866
ctx->flags = flags | WC_RNG_BANK_FLAG_INITED;
38543867
ctx->heap = heap;
38553868

3856-
ctx->rngs = (struct wc_rng_bank_inst *)XMALLOC(sizeof(*ctx->rngs) * n_rngs,
3857-
heap, DYNAMIC_TYPE_RNG);
3869+
ctx->rngs = (struct wc_rng_bank_inst *)
3870+
XMALLOC(sizeof(*ctx->rngs) * (size_t)n_rngs,
3871+
heap, DYNAMIC_TYPE_RNG);
38583872
if (! ctx->rngs)
38593873
ret = MEMORY_E;
38603874

38613875
if (ret == 0) {
3862-
XMEMSET(ctx->rngs, 0, sizeof(*ctx->rngs) * n_rngs);
3876+
XMEMSET(ctx->rngs, 0, sizeof(*ctx->rngs) * (size_t)n_rngs);
38633877
ctx->n_rngs = n_rngs;
38643878

38653879
for (i = 0; i < n_rngs; ++i) {
3880+
#ifdef WC_VERBOSE_RNG
38663881
int nretries = 0;
3882+
#endif
38673883
time_t ts1 = XTIME(0);
38683884
for (;;) {
38693885
time_t ts2;
@@ -3893,15 +3909,16 @@ WOLFSSL_API int wc_rng_bank_init(
38933909
ret = WC_TIMEOUT_E;
38943910
break;
38953911
}
3912+
#ifdef WC_VERBOSE_RNG
38963913
++nretries;
3914+
#endif
38973915
}
38983916
if (ret != 0) {
38993917
#ifdef WC_VERBOSE_RNG
39003918
WOLFSSL_DEBUG_PRINTF(
39013919
"ERROR: wc_InitRng returned %d after %d retries.\n", ret,
39023920
nretries);
39033921
#endif
3904-
ret = -EINVAL;
39053922
break;
39063923
}
39073924
}
@@ -3998,7 +4015,7 @@ WOLFSSL_API int wc_rng_bank_checkout(
39984015
struct wc_rng_bank_inst **rng,
39994016
int preferred_inst_offset,
40004017
int timeout_secs,
4001-
enum wc_rng_bank_flags flags)
4018+
word32 flags)
40024019
{
40034020
int new_lock_value, ret = 0;
40044021
time_t ts1, ts2;
@@ -4183,7 +4200,7 @@ WOLFSSL_API int wc_rng_bank_inst_reinit(
41834200
struct wc_rng_bank *bank,
41844201
struct wc_rng_bank_inst *rng_inst,
41854202
int timeout_secs,
4186-
enum wc_rng_bank_flags flags)
4203+
word32 flags)
41874204
{
41884205
int ret;
41894206
time_t ts1 = 0;
@@ -4238,7 +4255,7 @@ WOLFSSL_API int wc_rng_bank_inst_reinit(
42384255
WOLFSSL_API int wc_rng_bank_seed(struct wc_rng_bank *bank,
42394256
const byte* seed, word32 seedSz,
42404257
int timeout_secs,
4241-
enum wc_rng_bank_flags flags)
4258+
word32 flags)
42424259
{
42434260
int ret = 0;
42444261
int n;
@@ -4294,7 +4311,7 @@ WOLFSSL_API int wc_rng_bank_seed(struct wc_rng_bank *bank,
42944311

42954312
WOLFSSL_API int wc_rng_bank_reseed(struct wc_rng_bank *bank,
42964313
int timeout_secs,
4297-
enum wc_rng_bank_flags flags)
4314+
word32 flags)
42984315
{
42994316
int n;
43004317
int ret;

wolfssl/wolfcrypt/random.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -414,39 +414,39 @@ WOLFSSL_API int wc_FreeRng(WC_RNG* rng);
414414
* applications.
415415
*/
416416

417-
enum wc_rng_bank_flags {
418-
WC_RNG_BANK_FLAG_NONE = 0,
419-
WC_RNG_BANK_FLAG_INITED = (1<<0),
420-
WC_RNG_BANK_FLAG_CAN_FAIL_OVER_INST = (1<<1),
421-
WC_RNG_BANK_FLAG_CAN_WAIT = (1<<2),
422-
WC_RNG_BANK_FLAG_NO_VECTOR_OPS = (1<<3),
423-
WC_RNG_BANK_FLAG_PREFER_AFFINITY_INST = (1<<4),
424-
WC_RNG_BANK_FLAG_AFFINITY_LOCK = (1<<5)
425-
};
417+
#define WC_RNG_BANK_FLAG_NONE 0
418+
#define WC_RNG_BANK_FLAG_INITED (1<<0)
419+
#define WC_RNG_BANK_FLAG_CAN_FAIL_OVER_INST (1<<1)
420+
#define WC_RNG_BANK_FLAG_CAN_WAIT (1<<2)
421+
#define WC_RNG_BANK_FLAG_NO_VECTOR_OPS (1<<3)
422+
#define WC_RNG_BANK_FLAG_PREFER_AFFINITY_INST (1<<4)
423+
#define WC_RNG_BANK_FLAG_AFFINITY_LOCK (1<<5)
426424

427425
typedef int (*wc_affinity_lock_fn_t)(void *arg);
428426
typedef int (*wc_affinity_get_id_fn_t)(void *arg, int *id);
429427
typedef int (*wc_affinity_unlock_fn_t)(void *arg);
430428

429+
struct wc_rng_bank_inst {
430+
wolfSSL_Atomic_Int lock;
431+
WC_RNG rng;
432+
};
433+
431434
struct wc_rng_bank {
432435
wolfSSL_Ref refcount;
433436
void *heap;
434-
enum wc_rng_bank_flags flags;
437+
word32 flags;
435438
wc_affinity_lock_fn_t affinity_lock_cb;
436439
wc_affinity_get_id_fn_t affinity_get_id_cb;
437440
wc_affinity_unlock_fn_t affinity_unlock_cb;
438441
void *cb_arg; /* if mutable, caller is responsible for thread safety. */
439442
int n_rngs;
440-
struct wc_rng_bank_inst {
441-
wolfSSL_Atomic_Int lock;
442-
WC_RNG rng;
443-
} *rngs; /* typically one per CPU ID, plus a few */
443+
struct wc_rng_bank_inst *rngs; /* typically one per CPU ID, plus a few */
444444
};
445445

446446
WOLFSSL_API int wc_rng_bank_init(
447447
struct wc_rng_bank *ctx,
448448
int n_rngs,
449-
enum wc_rng_bank_flags flags,
449+
word32 flags,
450450
int timeout_secs,
451451
void *heap);
452452

@@ -464,7 +464,7 @@ WOLFSSL_API int wc_rng_bank_checkout(
464464
struct wc_rng_bank_inst **rng,
465465
int preferred_inst_offset,
466466
int timeout_secs,
467-
enum wc_rng_bank_flags flags);
467+
word32 flags);
468468

469469
WOLFSSL_API int wc_rng_bank_checkin(
470470
struct wc_rng_bank *bank,
@@ -474,22 +474,22 @@ WOLFSSL_API int wc_rng_bank_inst_reinit(
474474
struct wc_rng_bank *bank,
475475
struct wc_rng_bank_inst *rng_inst,
476476
int timeout_secs,
477-
enum wc_rng_bank_flags flags);
477+
word32 flags);
478478

479479
WOLFSSL_API int wc_rng_bank_seed(struct wc_rng_bank *bank,
480480
const byte* seed, word32 seedSz,
481481
int timeout_secs,
482-
enum wc_rng_bank_flags flags);
482+
word32 flags);
483483

484484
WOLFSSL_API int wc_rng_bank_reseed(struct wc_rng_bank *bank,
485485
int timeout_secs,
486-
enum wc_rng_bank_flags flags);
486+
word32 flags);
487487

488488
WOLFSSL_API int wc_InitRng_BankRef(struct wc_rng_bank *bank, WC_RNG *rng);
489489

490490
WOLFSSL_API int wc_rng_new_bankref(struct wc_rng_bank *bank, WC_RNG **rng);
491491

492-
#define WC_RNG_BANK_INST_TO_RNG(rng_inst) (&rng_inst->rng)
492+
#define WC_RNG_BANK_INST_TO_RNG(rng_inst) (&(rng_inst)->rng)
493493

494494
#endif /* WC_DRBG_BANK_SUPPORT */
495495

wolfssl/wolfcrypt/wc_port.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ typedef struct wolfSSL_RefWithMutex {
699699
typedef struct wolfSSL_Ref {
700700
wolfSSL_Atomic_Int count;
701701
} wolfSSL_Ref;
702-
#define wolfSSL_RefCur(ref) WOLFSSL_ATOMIC_LOAD(ref.count)
702+
#define wolfSSL_RefCur(ref) WOLFSSL_ATOMIC_LOAD((ref).count)
703703
#else
704704
typedef struct wolfSSL_RefWithMutex wolfSSL_Ref;
705705
#define wolfSSL_RefCur(ref) wolfSSL_RefWithMutexCur(ref)

0 commit comments

Comments
 (0)