Files
wolfssl/tests/api/test_random.h
T
Daniele Lacamera 260c3f2dbe tests: MC/DC decision coverage for random.c (Hash_DRBG + seed layer)
BEFORE 15/65 (23.08%) -> AFTER 55/65 (84.62%) MC/DC on wolfcrypt/src/random.c,
measured across 5 native user_settings.h variants (default, WOLFSSL_SMALL_STACK,
WOLFSSL_SMALL_STACK_CACHE, WC_RNG_SEED_CB, CUSTOM_RAND_GENERATE_BLOCK) plus a
new tests/unit-mcdc/test_random_whitebox.c white-box supplement, in the
ISO 26262 per-module MC/DC campaign.

tests/api/test_random.c / test_random.h:
- Add test_wc_RNG_HealthTest_SHA256_Ext / test_wc_RNG_HealthTest_SHA512_Ext,
  exercising the previously-untested ACVP-oriented extended health-test entry
  points (wc_RNG_HealthTest_SHA256_ex, wc_RNG_HealthTest_SHA512_ex/_ex2):
  nonce/personalization-string/additional-input/reseed-entropy presence and
  absence, in both standard and prediction-resistance modes, including
  "valid pointer + zero size" calls needed to isolate each leaf's size
  operand independently of its pointer operand for MC/DC.
- Add test_wc_RNG_SeedCb (WC_RNG_SEED_CB custom seed callback: success,
  failing callback, and no-callback-installed paths).
- Add test_wc_RNG_CustomRandBlock (CUSTOM_RAND_GENERATE_BLOCK bypass path).
- Add test_wc_RNG_DrbgDisable (wc_Sha256Drbg_Disable/Enable/IsDisabled and
  the wc_Sha512Drbg_* equivalents: drbgType selection and the "can't disable
  both" BAD_STATE_E guard).
- Extend existing HealthTest bad-parameter coverage (reseed-without-seedB on
  wc_RNG_HealthTest_ex / wc_RNG_HealthTest_SHA512 / wc_RNG_HealthTest_SHA512_ex,
  and the untested wc_RNG_HealthTest_SHA512_ex2 3-operand bad-parameter guard).
- Guard test_wc_GenerateSeed against CUSTOM_RAND_GENERATE_BLOCK, whose
  wc_GenerateSeed() ladder intentionally has no implementation in that
  configuration (would otherwise be a link error, not a test failure).

tests/unit-mcdc/test_random_whitebox.c (new):
- White-box #include of random.c closing two structurally-unreachable-via-API
  leaves: Hash_gen()/Hash512_gen()'s "out != NULL && outSz != 0" false side,
  and array_add()'s "dLen > 0 && sLen > 0 && dLen >= sLen" false sides.

Residuals (10 of 65, documented in db/modules.json / baselines.json in the
paired testing-repo change): 4 WOLFSSL_SMALL_STACK allocation-failure
branches (no fault injection); 4 Hash_DRBG_Init/Hash512_DRBG_Init chained
Hash_df(...)==DRBG_SUCCESS conditions (transform-failure, SHA-256/512 never
fail on valid input); 2 Hash_gen()/Hash512_gen() "outSz != 0" leaves that are
structurally unsatisfiable given the caller's own outSz normalization and
loop-bound arithmetic (not merely hard to reach).

Bugs/interactions found while bringing up the CUSTOM_RAND_GENERATE_BLOCK
build variant (reported, not source-fixed): (1) random.c's PollAndReSeed()
is guarded only by "#ifdef HAVE_HASHDRBG" (not also
"!defined(CUSTOM_RAND_GENERATE_BLOCK)" like _InitRng()) and its non-callback
path unconditionally calls wc_GenerateSeed(), whose implementation ladder has
an intentionally empty CUSTOM_RAND_GENERATE_BLOCK arm -- an undefined-symbol
link error if a user_settings.h ever forces HAVE_HASHDRBG on together with
CUSTOM_RAND_GENERATE_BLOCK (this campaign's config now avoids the
combination instead of forcing it). (2) on this host, glibc's vDSO-
accelerated getrandom() does not validate the output buffer before writing
to it: wc_GenerateSeed(non-NULL os, NULL output, sz) segfaults inside
getrandom_vdso() instead of returning an error, so
TEST_WC_GENERATE_SEED_PARAMS is deliberately left undefined in the campaign
config for this test-only bad-parameter block (documented in
configs/random/user_settings.base.h).
2026-07-13 11:19:23 +02:00

69 lines
2.9 KiB
C

/* test_random.h
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef WOLFCRYPT_TEST_RANDOM_H
#define WOLFCRYPT_TEST_RANDOM_H
#include <tests/api/api_decl.h>
int test_wc_InitRng(void);
int test_wc_RNG_GenerateBlock_Reseed(void);
int test_wc_RNG_ReseedBoundary(void);
int test_wc_RNG_GenerateBlock(void);
int test_wc_RNG_GenerateByte(void);
int test_wc_InitRngNonce(void);
int test_wc_InitRngNonce_ex(void);
int test_wc_GenerateSeed(void);
int test_wc_rng_new(void);
int test_wc_RNG_DRBG_Reseed(void);
int test_wc_RNG_TestSeed(void);
int test_wc_RNG_HealthTest(void);
int test_wc_RNG_HealthTest_SHA512(void);
int test_wc_RNG_HealthTest_SHA256_Ext(void);
int test_wc_RNG_HealthTest_SHA512_Ext(void);
int test_wc_RNG_SeedCb(void);
int test_wc_RNG_CustomRandBlock(void);
int test_wc_RNG_DrbgDisable(void);
int test_wc_Entropy_Get(void);
#define TEST_RANDOM_DECLS \
TEST_DECL_GROUP("random", test_wc_InitRng), \
TEST_DECL_GROUP("random", test_wc_RNG_GenerateBlock_Reseed), \
TEST_DECL_GROUP("random", test_wc_RNG_ReseedBoundary), \
TEST_DECL_GROUP("random", test_wc_RNG_GenerateBlock), \
TEST_DECL_GROUP("random", test_wc_RNG_GenerateByte), \
TEST_DECL_GROUP("random", test_wc_InitRngNonce), \
TEST_DECL_GROUP("random", test_wc_InitRngNonce_ex), \
TEST_DECL_GROUP("random", test_wc_GenerateSeed), \
TEST_DECL_GROUP("random", test_wc_rng_new), \
TEST_DECL_GROUP("random", test_wc_RNG_DRBG_Reseed), \
TEST_DECL_GROUP("random", test_wc_RNG_TestSeed), \
TEST_DECL_GROUP("random", test_wc_RNG_HealthTest), \
TEST_DECL_GROUP("random", test_wc_RNG_HealthTest_SHA512), \
TEST_DECL_GROUP("random", test_wc_RNG_HealthTest_SHA256_Ext), \
TEST_DECL_GROUP("random", test_wc_RNG_HealthTest_SHA512_Ext), \
TEST_DECL_GROUP("random", test_wc_RNG_SeedCb), \
TEST_DECL_GROUP("random", test_wc_RNG_CustomRandBlock), \
TEST_DECL_GROUP("random", test_wc_RNG_DrbgDisable), \
TEST_DECL_GROUP("random", test_wc_Entropy_Get)
#endif /* WOLFCRYPT_TEST_RANDOM_H */