AMD/Xilinx Versal TRNG support
v2 cover AMD/Xilinx Versal TRNG support
This series adds support for the True Random Number Generator (TRNG) in the AMD/Xilinx Versal family of devices.
The series starts by introducing a non-cryptographic grade model of the TRNG controller in the Versal family of devices, followed by instantiating the model in Xilinx Versal machine.
The series ends with a q-test for sanity check of the TRNG model in the Xilinx Versal machine.
V1 => V2
1) Change patch #1 only
2) Use g_rand_*() PRNG from glib to replace V1's custom PRNG.
3) Implement ResettableClass for device-reset.
4) Add device-mode description to commit-message.
Best regards,
Tong Ho
Tong Ho (3):
hw/misc: Introduce AMD/Xilix Versal TRNG device hw/arm: xlnx-versal-virt: Add AMD/Xilinx TRNG device tests/qtest: Introduce tests for AMD/Xilinx Versal TRNG device
hw/arm/Kconfig | 1 + hw/arm/xlnx-versal-virt.c | 20 + hw/arm/xlnx-versal.c | 16 + hw/misc/Kconfig | 3 + hw/misc/meson.build | 3 + hw/misc/xlnx-versal-trng.c | 734 ++++++++++++++++++++++++++++ include/hw/arm/xlnx-versal.h | 5 + include/hw/misc/xlnx-versal-trng.h | 57 +++ tests/qtest/meson.build | 2 +- tests/qtest/xlnx-versal-trng-test.c | 490 +++++++++++++++++++ 10 files changed, 1330 insertions(+), 1 deletion(-) create mode 100644 hw/misc/xlnx-versal-trng.c create mode 100644 include/hw/misc/xlnx-versal-trng.h create mode 100644 tests/qtest/xlnx-versal-trng-test.c
Signature · 1 line
2.25.1
PATCH v2 1/3 hw/misc: Introduce AMD/Xilix Versal TRNG device
This adds a non-cryptographic grade implementation of the model for the True Random Number Generator (TRNG) component in AMD/Xilinx Versal device family.
This implements all 3 modes defined by the actual hardware specs, all of which selectable by guest software at will at anytime:
1) PRNG mode, in which the generated sequence is required to be reproducible after reseeded by the same 384-bit value as supplied by guest software.
2) Test mode, in which the generated sequence is required to be reproducible ater reseeded by the same 128-bit test seed supplied by guest software.
3) TRNG mode, in which non-reproducible sequence is generated based on periodic reseed by a suitable entropy source.
This model is only intended for non-real world testing of guest software, where cryptographically strong PRNG and TRNG is not needed.
This model supports versions 1 & 2 of the device, with default to be version 2; the 'hw-version' uint32 property can be set to 0x0100 to override the default.
Other implemented properties:
- 'forced-prng', uint64
When set to non-zero, mode 3's entropy source is implemented as a deterministic sequence based on the given value and other deterministic parameters.
This option allows the emulation to test guest software using mode 3 and to reproduce data-dependent defects.
- 'fips-fault-events', uint32, bit-mask
bit 3: Triggers the SP800-90B entropy health test fault irq bit 1: Triggers the FIPS 140-2 continuous test fault irq
--- hw/misc/Kconfig | 3 + hw/misc/meson.build | 3 + hw/misc/xlnx-versal-trng.c | 734 +++++++++++++++++++++++++++++ include/hw/misc/xlnx-versal-trng.h | 57 +++ 4 files changed, 797 insertions(+) create mode 100644 hw/misc/xlnx-versal-trng.c create mode 100644 include/hw/misc/xlnx-versal-trng.h
hw/misc/Kconfig +3 -0
@@ -186,4 +186,7 @@ config AXP2XX_PMU bool depends on I2C +config XLNX_VERSAL_TRNG + bool + source macio/Kconfig
hw/misc/meson.build +3 -0
@@ -102,6 +102,9 @@ system_ss.add(when: 'CONFIG_XLNX_VERSAL', if_true: files( 'xlnx-cfi-if.c', 'xlnx-versal-cframe-reg.c', )) +system_ss.add(when: 'CONFIG_XLNX_VERSAL_TRNG', if_true: files( + 'xlnx-versal-trng.c', +)) system_ss.add(when: 'CONFIG_STM32F2XX_SYSCFG', if_true: files('stm32f2xx_syscfg.c')) system_ss.add(when: 'CONFIG_STM32F4XX_SYSCFG', if_true: files('stm32f4xx_syscfg.c')) system_ss.add(when: 'CONFIG_STM32F4XX_EXTI', if_true: files('stm32f4xx_exti.c'))
hw/misc/xlnx-versal-trng.c new file +734 -0
734 lines not on this page · raw message
include/hw/misc/xlnx-versal-trng.h new file +57 -0
57 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v2 2/3 hw/arm: xlnx-versal-virt: Add AMD/Xilinx TRNG device
In reply to Tong HoConnect the support for Versal True Random Number Generator (TRNG) device.
Warning: unlike the TRNG component in a real device from the Versal device familiy, the connected TRNG model is not of cryptographic grade and is not intended for use cases when cryptograpically strong TRNG is needed.
--- hw/arm/Kconfig | 1 + hw/arm/xlnx-versal-virt.c | 20 ++++++++++++++++++++ hw/arm/xlnx-versal.c | 16 ++++++++++++++++ include/hw/arm/xlnx-versal.h | 5 +++++ 4 files changed, 42 insertions(+)
hw/arm/Kconfig +1 -0
@@ -482,6 +482,7 @@ config XLNX_VERSAL select XLNX_BBRAM select XLNX_EFUSE_VERSAL select XLNX_USB_SUBSYS + select XLNX_VERSAL_TRNG config NPCM7XX bool
hw/arm/xlnx-versal-virt.c +20 -0
@@ -391,6 +391,25 @@ static void fdt_add_rtc_node(VersalVirt *s)g_free (name); } +static void fdt_add_trng_node (VersalVirt *s) +{ +const char compat[] =TYPE_XLNX_VERSAL_TRNG ; +const char interrupt_names[] ="trng" ; + g_autofree char *name = g_strdup_printf("/trng@%x", MM_PMC_TRNG); + +qemu_fdt_add_subnode (s->fdt, name); + +qemu_fdt_setprop_cells (s->fdt, name,"interrupts" , +GIC_FDT_IRQ_TYPE_SPI ,VERSAL_TRNG_IRQ , +GIC_FDT_IRQ_FLAGS_LEVEL_HI ); +qemu_fdt_setprop (s->fdt, name,"interrupt-names" , + interrupt_names,sizeof (interrupt_names)); +qemu_fdt_setprop_sized_cells (s->fdt, name,"reg" , +2 ,MM_PMC_TRNG , +2 ,MM_PMC_TRNG_SIZE ); +qemu_fdt_setprop (s->fdt, name,"compatible" , compat,sizeof (compat)); +} +static void fdt_add_bbram_node (VersalVirt *s) {const char compat[] =TYPE_XLNX_BBRAM ;
@@ -690,6 +709,7 @@ static void versal_virt_init(MachineState *machine)fdt_add_usb_xhci_nodes (s);fdt_add_sd_nodes (s);fdt_add_rtc_node (s); +fdt_add_trng_node (s);fdt_add_bbram_node (s);fdt_add_efuse_ctrl_node (s);fdt_add_efuse_cache_node (s);
hw/arm/xlnx-versal.c +16 -0
@@ -373,6 +373,21 @@ static void versal_create_rtc(Versal *s, qemu_irq *pic) qdev_get_gpio_in(DEVICE(&s->pmc.apb_irq_orgate), 0)); } +static void versal_create_trng (Versal *s,qemu_irq *pic) +{ +SysBusDevice *sbd; +MemoryRegion *mr; + +object_initialize_child (OBJECT (s),"trng" , &s->pmc.trng, +TYPE_XLNX_VERSAL_TRNG ); + sbd =SYS_BUS_DEVICE (&s->pmc.trng); +sysbus_realize (sbd, &error_fatal); + + mr =sysbus_mmio_get_region (sbd,0 ); +memory_region_add_subregion (&s->mr_ps,MM_PMC_TRNG , mr); +sysbus_connect_irq (sbd,0 , pic[VERSAL_TRNG_IRQ ]); +} +static void versal_create_xrams (Versal *s,qemu_irq *pic) {int nr_xrams =ARRAY_SIZE (s->lpd.xram.ctrl);
@@ -909,6 +924,7 @@ static void versal_realize(DeviceState *dev, Error **errp)versal_create_sds (s, pic);versal_create_pmc_apb_irq_orgate (s, pic);versal_create_rtc (s, pic); +versal_create_trng (s, pic);versal_create_xrams (s, pic);versal_create_bbram (s, pic);versal_create_efuse (s, pic);
include/hw/arm/xlnx-versal.h +5 -0
@@ -31,6 +31,7 @@#include "hw/dma/xlnx_csu_dma.h" #include "hw/misc/xlnx-versal-crl.h" #include "hw/misc/xlnx-versal-pmc-iou-slcr.h" +#include "hw/misc/xlnx-versal-trng.h" #include "hw/net/xlnx-versal-canfd.h" #include "hw/misc/xlnx-versal-cfu.h" #include "hw/misc/xlnx-versal-cframe-reg.h"
@@ -116,6 +117,7 @@ struct Versal { } iou;XlnxZynqMPRTC rtc; +XlnxVersalTRng trng;XlnxBBRam bbram;XlnxEFuse efuse;XlnxVersalEFuseCtrl efuse_ctrl;
@@ -160,6 +162,7 @@ struct Versal {#define VERSAL_OSPI_IRQ 124#define VERSAL_SD0_IRQ_0 126#define VERSAL_EFUSE_IRQ 139 +#define VERSAL_TRNG_IRQ 141#define VERSAL_RTC_ALARM_IRQ 142#define VERSAL_RTC_SECONDS_IRQ 143
@@ -329,4 +332,6 @@ struct Versal {#define MM_PMC_CRP_SIZE 0x10000#define MM_PMC_RTC 0xf12a0000#define MM_PMC_RTC_SIZE 0x10000 +#define MM_PMC_TRNG 0xf1230000 +#define MM_PMC_TRNG_SIZE 0x10000#endif
Signature · 1 line
2.25.1
PATCH v2 3/3 tests/qtest: Introduce tests for AMD/Xilinx Versal TRNG device
In reply to Tong Ho--- tests/qtest/meson.build | 2 +- tests/qtest/xlnx-versal-trng-test.c | 490 ++++++++++++++++++++++++++++ 2 files changed, 491 insertions(+), 1 deletion(-) create mode 100644 tests/qtest/xlnx-versal-trng-test.c
tests/qtest/meson.build +1 -1
@@ -216,7 +216,7 @@ qtests_aarch64 = \ (config_all.has_key('CONFIG_TCG') and config_all_devices.has_key('CONFIG_TPM_TIS_SYSBUS') ? \ ['tpm-tis-device-test', 'tpm-tis-device-swtpm-test'] : []) + \ (config_all_devices.has_key('CONFIG_XLNX_ZYNQMP_ARM') ? ['xlnx-can-test', 'fuzz-xlnx-dp-test'] : []) + \ - (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test'] : []) + \ + (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test', 'xlnx-versal-trng-test'] : []) + \ (config_all_devices.has_key('CONFIG_RASPI') ? ['bcm2835-dma-test'] : []) + \ (config_all.has_key('CONFIG_TCG') and \ config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test'] : []) + \
tests/qtest/xlnx-versal-trng-test.c new file +490 -0
490 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v2 3/3] tests/qtest: Introduce tests for AMD/Xilinx Versal TRNG device
Hi Tong,
Quoted 12 lines · Tong Ho
Signed-off-by Tong Ho <[email protected]>--- tests/qtest/meson.build | 2 +- tests/qtest/xlnx-versal-trng-test.c | 490 ++++++++++++++++++++++++++++ 2 files changed, 491 insertions(+), 1 deletion(-) create mode 100644 tests/qtest/xlnx-versal-trng-test.ctests/qtest/meson.build +0
-0
tests/qtest/meson.build qtests_aarch64 = \ · line 222
Quoted 8 lines
tests/qtest/meson.build +1
-1@@ -216,7 +216,7 @@ qtests_aarch64 = \ (config_all.has_key('CONFIG_TCG') and config_all_devices.has_key('CONFIG_TPM_TIS_SYSBUS') ? \ ['tpm-tis-device-test', 'tpm-tis-device-swtpm-test'] : []) + \ (config_all_devices.has_key('CONFIG_XLNX_ZYNQMP_ARM') ? ['xlnx-can-test', 'fuzz-xlnx-dp-test'] : []) + \ - (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test'] : []) + \ + (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test', 'xlnx-versal-trng-test'] : []) + \ (config_all_devices.has_key('CONFIG_RASPI') ? ['bcm2835-dma-test'] : []) + \ (config_all.has_key('CONFIG_TCG') and \ config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test'] : []) + \
.
.
.
tests/qtest/meson.build
Quoted 13 lines
tests/qtest/meson.build +13
-0+ +static const char *trng_info(void) +{ + uint32_t sta = trng_status(); + uint32_t ctl = trng_read(R_TRNG_CTRL); + + static char info[64]; + + snprintf(info, sizeof(info), "; status=0x%x, ctrl=0x%x", sta, ctl); + return info; +} + +static void trng_wait(uint32_t wait_mask, bool on, const char *act)
A suggestion is to remove the 'bool on' arg (it is only called with 'true' below).
Quoted 251 lines
tests/qtest/meson.build +251
-0+{ + time_t tmo = time(NULL) + 2; /* at most 2 seconds */ + uint32_t event_mask = 0; + uint32_t clear_mask = 0; + + /* + * Only selected bits are events in R_TRNG_STATUS, and + * clear them needs to go through R_INT_CTRL. + */ + if (wait_mask & TRNG_STATUS_CERTF_MASK) { + event_mask |= TRNG_STATUS_CERTF_MASK; + clear_mask |= TRNG_INT_CTRL_CERTF_RST_MASK; + } + if (wait_mask & TRNG_STATUS_DTF_MASK) { + event_mask |= TRNG_STATUS_DTF_MASK; + clear_mask |= TRNG_INT_CTRL_DTF_RST_MASK; + } + if (wait_mask & TRNG_STATUS_DONE_MASK) { + event_mask |= TRNG_STATUS_DONE_MASK; + clear_mask |= TRNG_INT_CTRL_DONE_RST_MASK; + } + + for (;;) { + bool sta = !!(trng_status() & event_mask); + + if ((on ^ sta) == 0) { + break; + } + + if (time(NULL) >= tmo) { + FAILED("%s: Timed out waiting for event 0x%x to be %d%s", + act, event_mask, (int)on, trng_info()); + } + + g_usleep(10000); + } + + /* Remove event */ + trng_bit_set(R_TRNG_INT_CTRL, clear_mask); + + if (!!(trng_read(R_TRNG_STATUS) & event_mask)) { + FAILED("%s: Event 0x%0x stuck at 1 after clear: %s", + act, event_mask, trng_info()); + } +} + +static void trng_wait_done(const char *act) +{ + trng_wait(TRNG_STATUS_DONE_MASK, true, act); +} + +static void trng_wait_dtf(void) +{ + trng_wait(TRNG_STATUS_DTF_MASK, true, "DTF injection"); +} + +static void trng_wait_certf(void) +{ + trng_wait(TRNG_STATUS_CERTF_MASK, true, "CERTF injection"); +} + +static void trng_reset(void) +{ + trng_write(R_TRNG_RESET, TRNG_RESET_VAL_MASK); + trng_write(R_TRNG_RESET, 0); +} + +static void trng_load(unsigned r0, const uint32_t *b384) +{ + static const uint32_t zero[12] = { 0 }; + unsigned k; + + if (!b384) { + b384 = zero; + } + + for (k = 0; k < 12; k++) { + trng_write(r0 + 4 * k, b384[k]); + } +} + +static void trng_reseed(const uint32_t *seed) +{ + const char *act; + uint32_t ctl; + + ctl = TRNG_CTRL_PRNGSTART_MASK | + TRNG_CTRL_PRNGXS_MASK | + TRNG_CTRL_TRSSEN_MASK; + + trng_ctrl_clr(ctl | TRNG_CTRL_PRNGMODE_MASK); + + if (seed) { + trng_load(R_TRNG_EXT_SEED_0, seed); + act = "Reseed PRNG"; + ctl &= ~TRNG_CTRL_TRSSEN_MASK; + } else { + trng_write(R_TRNG_OSC_EN, TRNG_OSC_EN_VAL_MASK); + act = "Reseed TRNG"; + ctl &= ~TRNG_CTRL_PRNGXS_MASK; + } + + trng_ctrl_set(ctl); + trng_wait_done(act); + trng_ctrl_clr(TRNG_CTRL_PRNGSTART_MASK); +} + +static void trng_generate(bool auto_enb) +{ + uint32_t ctl; + + ctl = TRNG_CTRL_PRNGSTART_MASK | TRNG_CTRL_SINGLEGENMODE_MASK; + trng_ctrl_clr(ctl); + + if (auto_enb) { + ctl &= ~TRNG_CTRL_SINGLEGENMODE_MASK; + } + + trng_ctrl_set(ctl | TRNG_CTRL_PRNGMODE_MASK); + + trng_wait_done("Generate"); + g_assert(trng_qcnt() != 7); +} + +static size_t trng_collect(uint32_t *rnd, size_t cnt) +{ + size_t i; + + for (i = 0; i < cnt; i++) { + if (trng_qcnt() == 0) { + return i; + } + + rnd[i] = trng_read(R_TRNG_CORE_OUTPUT); + } + + return i; +} + +static void trng_test_autogen(void) +{ + const size_t cnt = 512 / 32; + uint32_t rng[cnt], prng[cnt]; + size_t n; + + trng_reset(); + + /* PRNG run #1 */ + trng_reseed(prng_seed); + trng_generate(true); + + n = trng_collect(prng, cnt); + if (n != cnt) { + FAILED("PRNG_1 Auto-gen test failed: expected = %u, got = %u", + (unsigned)cnt, (unsigned)n); + } + + /* TRNG, should not match PRNG */ + trng_reseed(NULL); + trng_generate(true); + + n = trng_collect(rng, cnt); + if (n != cnt) { + FAILED("TRNG Auto-gen test failed: expected = %u, got = %u", + (unsigned)cnt, (unsigned)n); + } + + if (!memcmp(rng, prng, sizeof(rng))) { + FAILED("TRNG test failed: matching PRNG"); + } + + /* PRNG #2: should matches run #1 */ + trng_reseed(prng_seed); + trng_generate(true); + + n = trng_collect(rng, cnt); + if (n != cnt) { + FAILED("PRNG_2 Auto-gen test failed: expected = %u, got = %u", + (unsigned)cnt, (unsigned)n); + } + + if (memcmp(rng, prng, sizeof(rng))) { + FAILED("PRNG_2 Auto-gen test failed: does not match PRNG_1"); + } +} + +static void trng_test_oneshot(void) +{ + const size_t cnt = 512 / 32; + uint32_t rng[cnt]; + size_t n; + + trng_reset(); + + /* PRNG run #1 */ + trng_reseed(prng_seed); + trng_generate(false); + + n = trng_collect(rng, cnt); + if (n == cnt) { + FAILED("PRNG_1 One-shot gen test failed"); + } + + /* TRNG, should not match PRNG */ + trng_reseed(NULL); + trng_generate(false); + + n = trng_collect(rng, cnt); + if (n == cnt) { + FAILED("TRNG One-shot test failed"); + } +} + +static void trng_test_per_str(void) +{ + const size_t cnt = 512 / 32; + uint32_t rng[cnt], prng[cnt]; + size_t n; + + trng_reset(); + + /* #1: disabled */ + trng_ctrl_set(TRNG_CTRL_PERSODISABLE_MASK); + trng_reseed(prng_seed); + trng_ctrl_clr(TRNG_CTRL_PERSODISABLE_MASK); + + trng_generate(true); + n = trng_collect(prng, cnt); + g_assert_cmpuint(n, ==, cnt); + + /* #2: zero string should match personalization disabled */ + trng_load(R_TRNG_PER_STRNG_0, NULL); + trng_reseed(prng_seed); + + trng_generate(true); + n = trng_collect(rng, cnt); + g_assert_cmpuint(n, ==, cnt); + + if (memcmp(rng, prng, sizeof(rng))) { + FAILED("Failed: PER_DISABLE != PER_STRNG_ALL_ZERO"); + } + + /* #3: non-zero string should not match personalization disabled */ + trng_load(R_TRNG_PER_STRNG_0, pers_str); + trng_reseed(prng_seed); + + trng_generate(true); + n = trng_collect(rng, cnt); + g_assert_cmpuint(n, ==, cnt); + + if (!memcmp(rng, prng, sizeof(rng))) {
tests/qtest/meson.build
Quoted 10 lines
tests/qtest/meson.build +10
-0+ FAILED("Failed: PER_DISABLE == PER_STRNG_NON_ZERO"); + } +} + +static void trng_test_forced_prng(void) +{ + const char *prop = "forced-prng"; + const uint64_t seed = 0xdeadbeefbad1bad0ULL; + + trng_reset();
We need to move above line below the variable initialization below.
tests/qtest/meson.build
Quoted 3 lines
tests/qtest/meson.build +3
-0+ const size_t cnt = 512 / 32; + uint32_t rng[cnt], prng[cnt]; + size_t n;
Looks good to me otherwise!
Best regards,
Francisco Iglesias
Quoted 68 lines
tests/qtest/meson.build +64
-0+ + trng_test_set_uint_prop(prop, seed); + + /* TRNG run #1 */ + trng_reset(); + trng_reseed(NULL); + trng_generate(true); + + n = trng_collect(prng, cnt); + g_assert_cmpuint(n, ==, cnt); + + /* TRNG run #2 should match run #1 */ + trng_reset(); + trng_reseed(NULL); + trng_generate(true); + + n = trng_collect(rng, cnt); + g_assert_cmpuint(n, ==, cnt); + + if (memcmp(rng, prng, sizeof(rng))) { + FAILED("Forced-prng test failed: results do not match"); + } +} + +static void trng_test_fault_events(void) +{ + const char *prop = "fips-fault-events"; + + trng_reset(); + + /* Fault events only when TRSS is enabled */ + trng_write(R_TRNG_OSC_EN, TRNG_OSC_EN_VAL_MASK); + trng_ctrl_set(TRNG_CTRL_TRSSEN_MASK); + + trng_test_set_uint_prop(prop, TRNG_STATUS_CERTF_MASK); + trng_wait_certf(); + + trng_test_set_uint_prop(prop, TRNG_STATUS_DTF_MASK); + trng_wait_dtf(); + + trng_reset(); +} + +int main(int argc, char **argv) +{ + int rc; + + g_test_init(&argc, &argv, NULL); + + #define TRNG_TEST_ADD(n) \ + qtest_add_func("/hw/misc/xlnx-versal-trng/" #n, trng_test_ ## n); + TRNG_TEST_ADD(autogen); + TRNG_TEST_ADD(oneshot); + TRNG_TEST_ADD(per_str); + TRNG_TEST_ADD(forced_prng); + TRNG_TEST_ADD(fault_events); + #undef TRNG_TEST_ADD + + trng_test_start(); + rc = g_test_run(); + trng_test_stop(); + + return rc; +}Signature · 1 line
2.25.1