AMD/Xilinx Versal TRNG support
v3 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.
V2 => V3
1) Patch #1 Use DEFINE_PROP to define a property with custom action on set
2) Patch #1 Remove duplicated version-check on writing to CTRL4 register
3) Patch #2 Defer adding TRNG to machine's device-tree to a future patch
4) Patch #3 Code style fix: group all local vars definitions together
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.c | 16 + hw/misc/Kconfig | 3 + hw/misc/meson.build | 3 + hw/misc/xlnx-versal-trng.c | 732 ++++++++++++++++++++++++++++ include/hw/arm/xlnx-versal.h | 5 + include/hw/misc/xlnx-versal-trng.h | 58 +++ tests/qtest/meson.build | 2 +- tests/qtest/xlnx-versal-trng-test.c | 490 +++++++++++++++++++ 9 files changed, 1309 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 v3 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 | 732 +++++++++++++++++++++++++++++ include/hw/misc/xlnx-versal-trng.h | 58 +++ 4 files changed, 796 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 +732 -0
732 lines not on this page · raw message
include/hw/misc/xlnx-versal-trng.h new file +58 -0
58 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v3 1/3] hw/misc: Introduce AMD/Xilix Versal TRNG device
Quoted 47 lines · Tong Ho
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
0x0100to 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 irqSigned-off-by Tong Ho <[email protected]>---
hw/misc/xlnx-versal-trng.c new file +7
-0+enum { +U384_U32 =384 /32 , + }; + +/* +* Maximum seed length is len(personalized string) + len(ext seed). +*
hw/misc/xlnx-versal-trng.c line 204
Quoted 10 lines
hw/misc/xlnx-versal-trng.c new file +10
-0+* Use little-endian to ensure guest sequence being indepedent of +* host endian. +*/ +struct { +guint32 ps[U384_U32 ]; +union { +uint64_t int_seed[2 ]; +guint32 ext_seed[U384_U32 ]; + }; + } gs;
This struct-and-union seems unnecessarily complicated. It also means you will be seeding the RNG with different values on little and big endian, because the two halves of the uint64_t values will be the opposite way around when g_rand_set_seed_array() views them as a set of guint32s. Probably better to use a simple array of guint32.
Quoted 37 lines
hw/misc/xlnx-versal-trng.c new file +37
-0+/* +* A disabled personalized string is the same as +* a string with all zeros. +* +* The device's hardware spec defines 3 modes (all selectable +* by guest at will and at anytime): +* 1) External seeding +* This is a PRNG mode, in which the produced sequence shall +* be reproducible if reseeded by the same 384-bit seed, as +* supplied by guest software. +* 2) Test seeding +* This is a PRNG mode, in which the produced sequence shall +* be reproducible if reseeded by a 128-bit test seed, as +* supplied by guest software. +* 3) Truly-random seeding +* This is the TRNG mode, in which the produced sequence is +* periodically reseeded by a crypto-strength entropy source. +* +* To assist debugging of certain classes of software defects, +* this QEMU model implements a 4th mode, +* 4) Forced PRNG +* When in this mode, a reproducible sequence is generated +* if software has selected the TRNG mode (mode 2). +* +* This emulation-only mode can only be selected by setting +* the uint64 property 'forced-prng' to a non-zero value. +* Guest software cannot select this mode. +*/ +memset (&gs,0 ,sizeof (gs)); + gs.ext_seed[ARRAY_SIZE (gs.ext_seed) -1 ] =cpu_to_be32 (1 ); + +if (!pers_disabled) { +trng_le384 (gs.ps, &s->regs[R_PER_STRNG_0 ]); + } + +if (ext_seed) { +trng_le384 (gs.ext_seed, &s->regs[R_EXT_SEED_0 ]);
hw/misc/xlnx-versal-trng.c line 252
Quoted 10 lines
hw/misc/xlnx-versal-trng.c new file +10
-0+ }else if (trng_test_enabled (s)) { + gs.int_seed[0 ] =cpu_to_le64 (s->tst_seed[0 ]); + gs.int_seed[1 ] =cpu_to_le64 (s->tst_seed[1 ]); + }else if (s->forced_prng_seed) { + s->forced_prng_count++; + gs.int_seed[0 ] =cpu_to_le64 (s->forced_prng_count); + gs.int_seed[1 ] =cpu_to_le64 (s->forced_prng_seed); + }else { + gs.int_seed[0 ] =cpu_to_le64 (qemu_clock_get_ns (QEMU_CLOCK_HOST )); + gs.int_seed[1 ] =cpu_to_le64 (getpid ());
This is not "a crypto strength entropy source". It's also going to cause problems for record-and-replay, because the value will not be the same on replay as it was on record.
If you want 128 (or 384) bits of random data, call qemu_guest_getrandom_nofail().
Quoted 26 lines
hw/misc/xlnx-versal-trng.c new file +25
-0+ } + +g_rand_set_seed_array (s->prng, gs.ps, +sizeof (gs) /sizeof (guint32)); + + s->rand_count =0 ; + s->rand_reseed =1ULL <<48 ; +} + +static void trng_regen (XlnxVersalTRng *s) +{ +if (s->rand_reseed ==0 ) { +TRNG_GUEST_ERROR (s,"Too many generations without a reseed" ); +trng_reseed (s); + } + s->rand_reseed--; + +/* +* In real hardware, each regen creates 256 bits, but QCNT +* reports a max of 4. +*/ +ARRAY_FIELD_DP32 (s->regs,STATUS ,QCNT ,4 ); + s->rand_count =256 /32 ; +} +
hw/misc/xlnx-versal-trng.c line 287
Quoted 10 lines
hw/misc/xlnx-versal-trng.c new file +10
-0+static uint32_t trng_rdout (XlnxVersalTRng *s) +{ +assert (s->rand_count); + + s->rand_count--; +if (s->rand_count <4 ) { +ARRAY_FIELD_DP32 (s->regs,STATUS ,QCNT , s->rand_count); + } + +/* Reject all 0's and all 1's */
This is weird. Why do we need to do this ?
hw/misc/xlnx-versal-trng.c line 297
Quoted 10 lines
hw/misc/xlnx-versal-trng.c new file +10
-0+while (true) { +/* g_rand_int_range() returns gint32, not guint32 */ +guint32 nr =g_rand_int (s->prng); + +if (nr && (nr != ~0 )) { +return nr; + } + } +} +
thanks
-- PMM
PATCH v3 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.c | 16 ++++++++++++++++ include/hw/arm/xlnx-versal.h | 5 +++++ 3 files changed, 22 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.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
Re: [PATCH v3 2/3] hw/arm: xlnx-versal-virt: Add AMD/Xilinx TRNG device
Quoted 9 lines · Tong Ho
Connect 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.
Signed-off-by Tong Ho <[email protected]>
Quoted 90 lines
--- hw/arm/Kconfig | 1 + hw/arm/xlnx-versal.c | 16 ++++++++++++++++ include/hw/arm/xlnx-versal.h | 5 +++++ 3 files changed, 22 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 boolhw/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 v3 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 v3 3/3] tests/qtest: Introduce tests for AMD/Xilinx Versal TRNG device
Quoted 1 line · Tong Ho
Signed-off-by Tong Ho <[email protected]>
Quoted 518 lines
--- 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 +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@@ -0,0 +1,490 @@ +/* +* QTests for the Xilinx Versal True Random Number Generator device +* +* Copyright (c) 2023 Advanced Micro Devices, Inc. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "libqtest-single.h" + +/* Base Address */ +#define TRNG_BASEADDR (0xf1230000) + +/* TRNG_INT_CTRL */ +#define R_TRNG_INT_CTRL (0x0000) +#define TRNG_INT_CTRL_CERTF_RST_MASK (1 << 5) +#define TRNG_INT_CTRL_DTF_RST_MASK (1 << 4) +#define TRNG_INT_CTRL_DONE_RST_MASK (1 << 3) +#define TRNG_INT_CTRL_CERTF_EN_MASK (1 << 2) +#define TRNG_INT_CTRL_DTF_EN_MASK (1 << 1) +#define TRNG_INT_CTRL_DONE_EN_MASK (1) + +/* TRNG_STATUS */ +#define R_TRNG_STATUS (0x0004) +#define TRNG_STATUS_QCNT_SHIFT (9) +#define TRNG_STATUS_QCNT_MASK (7 << TRNG_STATUS_QCNT_SHIFT) +#define TRNG_STATUS_CERTF_MASK (1 << 3) +#define TRNG_STATUS_DTF_MASK (1 << 1) +#define TRNG_STATUS_DONE_MASK (1) + +/* TRNG_CTRL */ +#define R_TRNG_CTRL (0x0008) +#define TRNG_CTRL_PERSODISABLE_MASK (1 << 10) +#define TRNG_CTRL_SINGLEGENMODE_MASK (1 << 9) +#define TRNG_CTRL_PRNGMODE_MASK (1 << 7) +#define TRNG_CTRL_TSTMODE_MASK (1 << 6) +#define TRNG_CTRL_PRNGSTART_MASK (1 << 5) +#define TRNG_CTRL_PRNGXS_MASK (1 << 3) +#define TRNG_CTRL_TRSSEN_MASK (1 << 2) +#define TRNG_CTRL_QERTUEN_MASK (1 << 1) +#define TRNG_CTRL_PRNGSRST_MASK (1) + +/* TRNG_EXT_SEED_0 ... _11 */ +#define R_TRNG_EXT_SEED_0 (0x0040) +#define R_TRNG_EXT_SEED_11 (R_TRNG_EXT_SEED_0 + 4 * 11) + +/* TRNG_PER_STRNG_0 ... 11 */ +#define R_TRNG_PER_STRNG_0 (0x0080) +#define R_TRNG_PER_STRNG_11 (R_TRNG_PER_STRNG_0 + 4 * 11) + +/* TRNG_CORE_OUTPUT */ +#define R_TRNG_CORE_OUTPUT (0x00c0) + +/* TRNG_RESET */ +#define R_TRNG_RESET (0x00d0) +#define TRNG_RESET_VAL_MASK (1) + +/* TRNG_OSC_EN */ +#define R_TRNG_OSC_EN (0x00d4) +#define TRNG_OSC_EN_VAL_MASK (1) + +/* TRNG_TRNG_ISR, _IMR, _IER, _IDR */ +#define R_TRNG_ISR (0x00e0) +#define R_TRNG_IMR (0x00e4) +#define R_TRNG_IER (0x00e8) +#define R_TRNG_IDR (0x00ec) +#define TRNG_IRQ_SLVERR_MASK (1 << 1) +#define TRNG_IRQ_CORE_INT_MASK (1) + +#define FAILED (FMT , ...) g_error("%s(): " FMT, __func__, ## __VA_ARGS__) + +static const uint32_t prng_seed[12 ] = { +0x01234567 ,0x12345678 ,0x23456789 ,0x3456789a ,0x456789ab ,0x56789abc , +0x76543210 ,0x87654321 ,0x98765432 ,0xa9876543 ,0xba987654 ,0xfedcba98 , +}; + +static const uint32_t pers_str[12 ] = { +0x76543210 ,0x87654321 ,0x98765432 ,0xa9876543 ,0xba987654 ,0xfedcba98 , +0x01234567 ,0x12345678 ,0x23456789 ,0x3456789a ,0x456789ab ,0x56789abc , +}; + +static void trng_test_start (void ) +{ +qtest_start ("-machine xlnx-versal-virt" ); +} + +static void trng_test_stop (void ) +{ +qtest_end (); +} + +static void trng_test_set_uint_prop (const char *name,uint64_t value) +{ +const char *path ="/machine/xlnx-versal/trng" ; +QDict *response; + + response =qmp ("{ 'execute': 'qom-set'," +" 'arguments': {" +" 'path': %s," +" 'property': %s," +" 'value': %llu" +"} }" , path, + name, (unsigned long long )value); +g_assert (qdict_haskey (response,"return" )); +qobject_unref (response); +} + +static void trng_write (unsigned ra,uint32_t val) +{ +writel (TRNG_BASEADDR + ra, val); +} + +static uint32_t trng_read (unsigned ra) +{ +return readl (TRNG_BASEADDR + ra); +} + +static void trng_bit_set (unsigned ra,uint32_t bits) +{ +trng_write (ra, (trng_read (ra) | bits)); +} + +static void trng_bit_clr (unsigned ra,uint32_t bits) +{ +trng_write (ra, (trng_read (ra) & ~bits)); +} + +static void trng_ctrl_set (uint32_t bits) +{ +trng_bit_set (R_TRNG_CTRL , bits); +} + +static void trng_ctrl_clr (uint32_t bits) +{ +trng_bit_clr (R_TRNG_CTRL , bits); +} + +static uint32_t trng_status (void ) +{ +return trng_read (R_TRNG_STATUS ); +} + +static unsigned trng_qcnt (void ) +{ +uint32_t sta =trng_status (); + +return (sta &TRNG_STATUS_QCNT_MASK ) >>TRNG_STATUS_QCNT_SHIFT ; +} + +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) +{ +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))) { +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 ; + +const size_t cnt =512 /32 ; +uint32_t rng[cnt], prng[cnt]; +size_t n; + +trng_reset (); +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
Re: [PATCH v3 3/3] tests/qtest: Introduce tests for AMD/Xilinx Versal TRNG device
In reply to Tong HoQuoted 32 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/xlnx-versal-trng-test.c new file +21
-0+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);
tests/qtest/xlnx-versal-trng-test.c line 332
Quoted 10 lines
tests/qtest/xlnx-versal-trng-test.c new file +10
-0+ + 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" ); + }
Why can't the TRNG output ever match the PRNG output ? It's random numbers, so you might get (un)lucky. This sort of check results in flaky CI, because very very occasionally the RNG will give you output that results in a test failure.
Quoted 6 lines
tests/qtest/xlnx-versal-trng-test.c new file +6
-0+ +/* PRNG #2: should matches run #1 */ +trng_reseed (prng_seed); +trng_generate (true); + + n =trng_collect (rng, cnt);
tests/qtest/xlnx-versal-trng-test.c line 348
Quoted 10 lines
tests/qtest/xlnx-versal-trng-test.c new file +10
-0+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" ); + } +} +
thanks
-- PMM