cryptnox-sdk-esp32 1.0.0
ESP32 SDK for Cryptnox Hardware Wallet
Loading...
Searching...
No Matches
esp32_random.cpp
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: LGPL-3.0-or-later
3 * Copyright (c) 2026 Cryptnox SA
4 */
5
6#include "CW_Utils.h"
7#include "esp_random.h"
8#include "esp_wifi.h"
9#ifdef CONFIG_BT_ENABLED
10# include "esp_bt.h"
11#endif
12#include "esp_log.h"
13
14static const char* const TAG = "CW_Utils";
15
16bool CW_Utils::fill_secure_random(uint8_t *dest, size_t len) {
17 bool result = false;
18
19 if ((dest != nullptr) && (len != 0U)) {
20 wifi_mode_t mode = WIFI_MODE_NULL;
21 bool wifi_on = ((esp_wifi_get_mode(&mode) == ESP_OK) && (mode != WIFI_MODE_NULL));
22
23#ifdef CONFIG_BT_ENABLED
24 bool bt_on = (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED);
25#else
26 bool bt_on = false;
27#endif
28
29 /* ESP32-S3 TRNG operates from thermal noise and ring-oscillator jitter
30 * even without a radio, but a live WiFi/BT subsystem feeds additional
31 * hardware entropy. Always fill the buffer; warn when reduced-entropy
32 * mode is active so callers are aware. */
33 if ((!wifi_on) && (!bt_on)) {
34 ESP_LOGW(TAG, "RNG: no radio active — reduced entropy (TRNG only)");
35 }
36
37 esp_fill_random(dest, len);
38 result = true;
39 }
40
41 return result;
42}
static const char *const TAG