UNO R4 TRNG and AES

Any tips or examples on using the UNO R4 TRNG and AES support ?

Hah. They don't seem to be documented, even in the Renesas "manuals."
The whole section describing the "secure crypto engine" is about 5 pages of feature lists and relatively useless block diagrams.

Hmm. I did find a relatively promising candidate HW_SCE_RNG_Read() in the fsp repository:

It claims to return 128bits of random number.

I can't figure out what it needs for a #include path, but the code might be a useful model. :frowning:

Ah. Apparently the Arduino distribution of fsp include files only includes SOME of the Renesad-provided include files, and that doesn't include the Crypto engine stuff (the TRNG is considered part of Crypto.)

1 Like

From fsp/ra/fsp/src/bsp/cmsis/Device/RENESAS/Include/R7FA2A1AB.h I pasted in the TRNG definitions into a sketch

// R4 TRNG

#define __IM volatile
#define R_TRNG_BASE         0x400D1000UL
#define R_TRNG         ((R_TRNG_Type *) R_TRNG_BASE)

typedef struct                         /*!< (@ 0x400D1000) R_TRNG Structure                                           */
{
  union
  {
    __IM uint8_t TRNGSDR;          /*!< (@ 0x00000000) TRNG SEED Data Register                                    */

    struct
    {
      __IM uint8_t SDATA : 8;    /*!< [7..0] When RDRDY bit is 1, these bits hold the generated SEED.
                                            When RDRDY bit is 0, these bits are read as 00h.The SEED
                                            is generated as 32-bit data. When TRNGSDR is read 4 times
                                            while RDRDY = 1, SEED reading is completed and RDRDY bit
                                            changes to 0                                                              */
    } TRNGSDR_b;
  };
  __IM uint8_t RESERVED;

  union
  {
    __IOM uint8_t TRNGSCR0;        /*!< (@ 0x00000002) TRNG SEED Command Register 0                               */

    struct
    {
      uint8_t               : 2;
      __OM uint8_t  SGSTART : 1; /*!< [2..2] SEED Generation Start                                              */
      __IOM uint8_t SGCEN   : 1; /*!< [3..3] SEED Generation Circuit Enable                                     */
      uint8_t               : 3;
      __IM uint8_t RDRDY    : 1; /*!< [7..7] When SEED geenration is completed, this bit changes to
                                            0                                                                         */
    } TRNGSCR0_b;
  };

  union
  {
    __IOM uint8_t TRNGSCR1;        /*!< (@ 0x00000003) TRNG SEED Command Register 1                               */

    struct
    {
      __IOM uint8_t INTEN : 1;   /*!< [0..0] TRNG Interrupt Enable                                              */
      uint8_t             : 7;
    } TRNGSCR1_b;
  };
} R_TRNG_Type;

uint32_t trng() {
  uint32_t rval;
  uint8_t * ptmp = (uint8_t *) &rval;
  /* Set SGCEN bit and SGSTART bit */
  R_TRNG->TRNGSCR0_b.SGCEN   = 1;
  R_TRNG->TRNGSCR0_b.SGSTART = 1;

  /* Wait for RDRDY bit to be set */
  while (0 == R_TRNG->TRNGSCR0_b.RDRDY)
  {
  }

  /* Read generated random data */
  *ptmp++ = R_TRNG->TRNGSDR;
  *ptmp++ = R_TRNG->TRNGSDR;
  *ptmp++ = R_TRNG->TRNGSDR;
  *ptmp++ = R_TRNG->TRNGSDR;
  return rval;
}

void setup() {
  Serial.begin(9600);
  while (!Serial);
}

void loop() {
  Serial.println(trng(), HEX);
  delay(500);
}

the sketch compiles, but I don't have my R4 yet, so I can't test.

i got my R4 board, but I couldn't get the TRNG sketch to work. I tried two different BASE ADDRESS.
must be other steps required to start the TRNG ?? TODO

Any new information? The old Arduino lacked a high-quality random generator.

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

I reported the issue at GitHub - renesas/fsp: Flexible Software Package (FSP) for Renesas RA MCU Family and was told it would be fixed in the release scheduled for the end of May 2024, likely v5.4.0. (see: Using SCE5 in a C++project causes a build error · Issue #353 · renesas/fsp · GitHub)

However, I'm not sure when this will be reflected in GitHub - arduino/ArduinoCore-renesas. Is this repository always tracking the renesas/fsp repository? If not, I'm wondering whether I should also raise an issue at arduino/ArduinoCore-renesas. If anyone knows what actions I should take, please let me know.