|
| 1 | +/* main.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2026 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSL. |
| 6 | + * |
| 7 | + * wolfSSL is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfSSL is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | + |
| 22 | +/* |
| 23 | + * Brings up the wolfCrypt DRBG in a wolfCrypt-only image that never calls |
| 24 | + * wolfSSL_Init() or wolfcrypt_test(). Both of those register the DRBG seed |
| 25 | + * callback themselves (src/ssl.c, wolfcrypt/test/test.c), so the wolfssl_test |
| 26 | + * sample passes even when the port's own registration is broken; here nothing |
| 27 | + * else can have registered it. Under FIPS v5 the module refuses to seed until |
| 28 | + * the callback is set, so wc_InitRng() fails with DRBG_NO_SEED_CB unless the |
| 29 | + * APPLICATION-level hook in zephyr_init.c ran. Without a bundle, which is how |
| 30 | + * CI runs it, the same test covers the tree's Zephyr wc_GenerateSeed(); see |
| 31 | + * fips-v5.conf for the hand run with a bundle. |
| 32 | + */ |
| 33 | + |
| 34 | +#include <zephyr/ztest.h> |
| 35 | + |
| 36 | +#include <wolfssl/wolfcrypt/settings.h> |
| 37 | +#include <wolfssl/wolfcrypt/wc_port.h> |
| 38 | +#include <wolfssl/wolfcrypt/types.h> |
| 39 | +#include <wolfssl/wolfcrypt/random.h> |
| 40 | + |
| 41 | +ZTEST(wolfssl_rng, test_init_rng_at_boot) |
| 42 | +{ |
| 43 | + WC_RNG rng; |
| 44 | + byte block[32]; |
| 45 | + byte zero[32]; |
| 46 | + int ret; |
| 47 | + |
| 48 | + zassert_equal(wolfCrypt_Init(), 0, "wolfCrypt_Init"); |
| 49 | + |
| 50 | + /* Nothing in this image calls wc_SetSeed_Cb(), so under FIPS v5 this is |
| 51 | + * where a missing boot-time registration shows. */ |
| 52 | + ret = wc_InitRng(&rng); |
| 53 | + zassert_equal(ret, 0, "wc_InitRng: %d", ret); |
| 54 | + |
| 55 | + XMEMSET(block, 0, sizeof(block)); |
| 56 | + XMEMSET(zero, 0, sizeof(zero)); |
| 57 | + ret = wc_RNG_GenerateBlock(&rng, block, sizeof(block)); |
| 58 | + zassert_equal(ret, 0, "wc_RNG_GenerateBlock: %d", ret); |
| 59 | + zassert_true(XMEMCMP(block, zero, sizeof(block)) != 0, |
| 60 | + "generated block is all zero"); |
| 61 | + |
| 62 | + zassert_equal(wc_FreeRng(&rng), 0, "wc_FreeRng"); |
| 63 | + zassert_equal(wolfCrypt_Cleanup(), 0, "wolfCrypt_Cleanup"); |
| 64 | +} |
| 65 | + |
| 66 | +ZTEST_SUITE(wolfssl_rng, NULL, NULL, NULL, NULL, NULL); |
0 commit comments