UNO R4 TRNG and AES

I'll share a sample code using the TRNG from SCE5.

#ifdef __cplusplus
extern "C" {
#endif
#include <hw_sce_private.h>
#include <hw_sce_trng_private.h>
#ifdef __cplusplus
}
#endif

uint32_t rnd[4] = { 0 };
fsp_err_t err = FSP_ERR_ASSERTION;

void setup() {
  Serial.begin(115200);
  while (!Serial)
    ;

  Serial.println("PowerOn");
  HW_SCE_PowerOn();

  Serial.println("MCU Specific Init.");
  err = HW_SCE_McuSpecificInit();
  if (err != FSP_SUCCESS) {
    Serial.println("MSU Specific Init failed!");
    delay(5000);
  } else {
    Serial.println("MCU Specific Init done.");
    Serial.println("SCE5 setup complete.");
  }
}

void loop() {
  err = HW_SCE_RNG_Read(rnd);
  if (err != FSP_SUCCESS) {
    Serial.print("failed HW_SCE_RNG_Read: ");
    Serial.println(err, HEX);
  } else {
    char s[33] = {0};
    sprintf(s, "%08X%08X%08X%08X", rnd[0], rnd[1], rnd[2], rnd[3]);
    Serial.println(s);
  }
  delay(1000);
}

Before compiling, it's important to note that the r_sce_if.h header file included in the Arduino IDE's Board Manager contains C++ reserved words. Therefore, you'll need to manually modify the header file as follows (since this header file is almost always necessary when using SCE5, it can cause compilation errors everywhere :frowning: )

/* RSA 1024bit key index pair structure */
typedef struct sce_rsa1024_key_pair_index
{
    sce_rsa1024_private_key_index_t    priv_key; // Changed 'private' to 'priv_key'
    sce_rsa1024_public_key_index_t     pub_key;  // Changed 'public' to 'pub_key'
} sce_rsa1024_key_pair_index_t;

There are other parts in this file that use private and public , so please make similar changes.

Additionally, r_sce_if.h is located at the following paths (I apologize, as I am a MacOS user, I am not sure about Windows):

for MINIMA:

/Users/{username}/Library/Arduino15/packages/arduino/hardware/renesas_uno/1.1.0/variants/MINIMA/includes/ra/fsp/src/r_sce/crypto_procedures/src/sce5/plainkey/public/inc/r_sce_if.h

for WiFi version:

/Users/{username}/Library/Arduino15/packages/arduino/hardware/renesas_uno/1.1.0/variants/UNOWIFIR4/includes/ra/fsp/src/r_sce/crypto_procedures/src/sce5/plainkey/public/inc/r_sce_if.h

This is my article summarizing the above: Exploring the Hardware Random Number Generator of Arduino R4 MINIMA | blog.jetzou.com

1 Like