Topic: wc_InitRng(&rng) returns -199
As part of an bigger project I have problems using wolfcrypt in my sketch for my EPS32-S3.
Following is the sketch (a minimal extract from my entire project - but complete to run reproducible standalone):
#include <wolfssl.h>
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/wolfcrypt/wc_port.h>
#include <wolfssl/wolfcrypt/rsa.h>
void setup() {
int ret;
RNG rng;
Serial.begin(115200);
while (!Serial) {
delay(100);
}
Serial.println("We start ..."); Serial.flush();
delay(10000);
// initialize wolfCrypt
Serial.println("Initializing wolfCrypt ..."); Serial.flush();
ret = wolfCrypt_Init();
Serial.println("wolfCrpyt_Init:"); Serial.flush();
Serial.println(ret); Serial.flush();
if (ret != 0) {
Serial.println(String("wolfCrypt initialization failed with error " + ret)); Serial.flush();
return;
}
Serial.println("wolfCrypt initialization successful."); Serial.flush();
Serial.println("... initialized wolfCrypt."); Serial.flush();
delay(10000);
// initialize RNG
Serial.println("Initializing Random Number Generator ..."); Serial.flush();
ret = wc_FreeRng(&rng);
Serial.println("wc_FreeRng:"); Serial.flush();
Serial.println(ret); Serial.flush();
delay(3000);
ret = wc_InitRng(&rng);
Serial.println("wc_InitRng:"); Serial.flush();
Serial.println(ret); Serial.flush();
if (ret != 0) {
Serial.println(String("RNG initialization failed with error " + ret)); Serial.flush();
wolfCrypt_Cleanup();
return;
}
Serial.println("RNG initialization successful."); Serial.flush();
Serial.println("... initialized Random Number Generator."); Serial.flush();
}
void loop() {
Serial.println("Empty Sketch - Loop function");
delay(10000);
}
The output in the Serial Monitor is as follows:
<code>
22:15:07.278 -> ESP-ROM:esp32s3-20210327
22:15:07.278 -> Build:Mar 27 2021
22:15:07.278 -> rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
22:15:07.278 -> SPIWP:0xee
22:15:07.278 -> mode:DIO, clock div:1
22:15:07.278 -> load:0x3fce3808,len:0x3ac
22:15:07.278 -> load:0x403c9700,len:0x9b4
22:15:07.278 -> load:0x403cc700,len:0x28d8
22:15:07.278 -> entry 0x403c98bc
22:15:07.378 -> We start ...
22:15:17.360 -> Initializing wolfCrypt ...
22:15:17.360 -> wolfCrpyt_Init:
22:15:17.360 -> 0
22:15:17.360 -> wolfCrypt initialization successful.
22:15:17.360 -> ... initialized wolfCrypt.
22:15:27.373 -> Initializing Random Number Generator ...
22:15:27.373 -> wc_FreeRng:
22:15:27.373 -> 0
22:15:30.382 -> wc_InitRng:
22:15:30.382 -> -199
22:15:30.382 -> Crypt ...
22:15:30.382 -> Empty Sketch - Loop function
22:15:40.405 -> Empty Sketch - Loop function
22:15:50.380 -> Empty Sketch - Loop function
22:16:00.398 -> Empty Sketch - Loop function
....
</code>
What am I doing wrong? What other info is need to dig into this problem?
As alternative I removed the "wc_FreeRng(...)" call prior to the "wc_InitRng(...)" call. But no reveal.
And declaring "rng" as "WC_RNG" instead of "RNG" dir not help.
What I can see is, that the follwing "Serial.println(...);" statement only shows corrupted output, which looks to me as some memory overwrite! e.g.
<code>
22:26:36.125 -> Initializing Random Number Generator ...
22:26:36.125 -> wc_FreeRng:
22:26:36.125 -> 0
22:26:39.134 -> wc_InitRng:
22:26:39.134 -> -199
22:26:39.134 -> Crypt ...
22:26:39.134 -> Empty Sketch - Loop function
...
</code>
Actually I need the RNG for a later call to "wc_RsaSSL_Sign(..., &rng)".