Skip to contentSkip to conversation

hw/misc: Add Exynos4210 Pseudo Random Number Generator

PATCH Superseded Krzysztof Kozlowski 2 participants 8 messages Mar 18, 2017 – Apr 11, 2017 Arm Samsung Exynos

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Krzysztof Kozlowski

author maintainer
#0 lore raw

PATCH v2 1/1 hw/misc: Add Exynos4210 Pseudo Random Number Generator

Add emulation for Exynos4210 Pseudo Random Number Generator which could work on fixed seeds or with seeds provided by True Random Number Generator block inside the SoC.

Implement only the fixed seeds part of it in polling mode (no interrupts). Simple testing:

    # echo "exynos" > /sys/class/misc/hw_random/rng_current
    # dd if=/dev/hwrng of=/dev/null bs=1 count=16

---

Changes since v1:
1. Use GRand-like functions to fix build on MingW32 (this adds also finalize).
2. Add DPRINTF macro.
3. Use HWADDR_PRIx and family for printing values.

---
 hw/arm/exynos4210.c      |   4 +
 hw/misc/Makefile.objs    |   2 +-
 hw/misc/exynos4210_rng.c | 303 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 308 insertions(+), 1 deletion(-)
 create mode 100644 hw/misc/exynos4210_rng.c
hw/arm/exynos4210.c +4 -0
index 1d2b50cc4eea..0ec4250f0c05 100644
@@ -78,6 +78,9 @@
 /* Clock controller SFR base address */
 #define EXYNOS4210_CLK_BASE_ADDR            0x10030000
 
+/* PRNG/HASH SFR base address */
+#define EXYNOS4210_RNG_BASE_ADDR            0x10830400
+
 /* Display controllers (FIMD) */
 #define EXYNOS4210_FIMD0_BASE_ADDR          0x11C00000
 
@@ -314,6 +317,7 @@ Exynos4210State *exynos4210_init(MemoryRegion *system_mem,
     sysbus_create_simple("exynos4210.pmu", EXYNOS4210_PMU_BASE_ADDR, NULL);
 
     sysbus_create_simple("exynos4210.clk", EXYNOS4210_CLK_BASE_ADDR, NULL);
+    sysbus_create_simple("exynos4210.rng", EXYNOS4210_RNG_BASE_ADDR, NULL);
 
     /* PWM */
     sysbus_create_varargs("exynos4210.pwm", EXYNOS4210_PWM_BASE_ADDR,
hw/misc/Makefile.objs +1 -1
index c8b489390f7e..e0699ea11155 100644
@@ -26,7 +26,7 @@ obj-$(CONFIG_IVSHMEM) += ivshmem.o
 obj-$(CONFIG_REALVIEW) += arm_sysctl.o
 obj-$(CONFIG_NSERIES) += cbus.o
 obj-$(CONFIG_ECCMEMCTL) += eccmemctl.o
-obj-$(CONFIG_EXYNOS4) += exynos4210_pmu.o exynos4210_clk.o
+obj-$(CONFIG_EXYNOS4) += exynos4210_pmu.o exynos4210_clk.o exynos4210_rng.o
 obj-$(CONFIG_IMX) += imx_ccm.o
 obj-$(CONFIG_IMX) += imx31_ccm.o
 obj-$(CONFIG_IMX) += imx25_ccm.o
hw/misc/exynos4210_rng.c new file +303 -0

303 lines not on this page · raw message

Signature · 1 line
2.9.3
#1 lore raw
Quoted 11 lines · Krzysztof Kozlowski

Add emulation for Exynos4210 Pseudo Random Number Generator which could work on fixed seeds or with seeds provided by True Random Number Generator block inside the SoC.

Implement only the fixed seeds part of it in polling mode (no interrupts). Simple testing:

    # echo "exynos" > /sys/class/misc/hw_random/rng_current
    # dd if=/dev/hwrng of=/dev/null bs=1 count=16
Quoted 7 lines

---

Changes since v1:
1. Use GRand-like functions to fix build on MingW32 (this adds also finalize).
2. Add DPRINTF macro.
3. Use HWADDR_PRIx and family for printing values.

Is there a data sheet that describes this RNG? I had a quick google but couldn't find anything in the 4210 manual you can get from Samsung.

In particular I'm not sure we want to use GRand here.

thanks
-- PMM

author maintainer
#2 lore raw
Quoted 14 lines · Peter Maydell
Quoted 13 lines · Krzysztof Kozlowski

Add emulation for Exynos4210 Pseudo Random Number Generator which could work on fixed seeds or with seeds provided by True Random Number Generator block inside the SoC.

Implement only the fixed seeds part of it in polling mode (no interrupts). Simple testing:

    # echo "exynos" > /sys/class/misc/hw_random/rng_current
    # dd if=/dev/hwrng of=/dev/null bs=1 count=16

---

Quoted 8 lines
Quoted 5 lines

Changes since v1:
1. Use GRand-like functions to fix build on MingW32 (this adds also finalize).
2. Add DPRINTF macro.
3. Use HWADDR_PRIx and family for printing values.

Is there a data sheet that describes this RNG? I had a quick google but couldn't find anything in the 4210 manual you can get from Samsung.

Official and public datasheet - I never heard about it... AFAIK, Samsung never released any datasheet... But recently I found a copy of Exynos4412 datasheet published on FriendlyArm website:
http://wiki.friendlyarm.com/wiki/index.php/NanoPC-T1
(at the bottom in "Resources").

Some blocks in Exynos4412, including the RNG, are the same as in Exynos4210. However, you should not expect too much data about the RNG in the datasheet...

Quoted 1 line

In particular I'm not sure we want to use GRand here.

Now, I am not sure neither. :) Let me describe the background:

Recently I started also improving the Linux kernel driver for RNG module which ended in writing a new driver followed by discussions from which I learnt a lot. See [1][2][3]

The datasheet is not saying too much how the RNG module works inside... so it is all a guessing. I must admit that I wrote the QEMU RNG part mostly looking at behavior of kernel driver and combining the knowledge with information obtained from datasheet. Thus I used GRand to have repeatable sequences for same seed.

During the discussions about new kernel driver, I found that this RNG module (at least on Exynos4412 board) requires seeding but it is not following the Pseudo RNG behavior - seeding with the same value produces different results. This means that GRand would not be needed here.

The new driver (not merged yet) took this into account and it behaves differently than previous one. Most notably it tries to be a better pseudo random number generator in terms of randomness.

I did not test the new kernel driver with this QEMU RNG driver because it uses different user-space API and I still did not configure full Linux box in QEMU (mounting root, having some distro in the image). I am booting the QEMU kernel to initramfs with some toolset included.

[1] https://www.spinics.net/lists/kernel/msg2474154.html
[2] https://www.spinics.net/lists/arm-kernel/msg571653.html
[3] https://www.spinics.net/lists/arm-kernel/msg571516.html

Best regards,
Krzysztof

#3 lore raw
Quoted 30 lines · Krzysztof Kozlowski
Quoted 14 lines · Peter Maydell
Quoted 13 lines · Krzysztof Kozlowski

Add emulation for Exynos4210 Pseudo Random Number Generator which could work on fixed seeds or with seeds provided by True Random Number Generator block inside the SoC.

Implement only the fixed seeds part of it in polling mode (no interrupts). Simple testing:

    # echo "exynos" > /sys/class/misc/hw_random/rng_current
    # dd if=/dev/hwrng of=/dev/null bs=1 count=16

---

Quoted 8 lines
Quoted 5 lines

Changes since v1:
1. Use GRand-like functions to fix build on MingW32 (this adds also finalize).
2. Add DPRINTF macro.
3. Use HWADDR_PRIx and family for printing values.

Is there a data sheet that describes this RNG? I had a quick google but couldn't find anything in the 4210 manual you can get from Samsung.

Official and public datasheet - I never heard about it... AFAIK, Samsung never released any datasheet... But recently I found a copy of Exynos4412 datasheet published on FriendlyArm website:
http://wiki.friendlyarm.com/wiki/index.php/NanoPC-T1
(at the bottom in "Resources").

Quoted 7 lines

Some blocks in Exynos4412, including the RNG, are the same as in Exynos4210. However, you should not expect too much data about the RNG in the datasheet...

Quoted 1 line

In particular I'm not sure we want to use GRand here.

Now, I am not sure neither. :)

The last RNG we added was hw/misc/bcm2835_rng.c, which uses qcrypto_random_bytes() to get cryptographically-random bytes from the host. On the other hand it sounds like this Exynos hardware is a PRNG without true-random input...

Quoted 4 lines

During the discussions about new kernel driver, I found that this RNG module (at least on Exynos4412 board) requires seeding but it is not following the Pseudo RNG behavior - seeding with the same value produces different results. This means that GRand would not be needed here.

That's rather odd. You're right that the datasheet is hopelessly uninformative about what's actually going on.

thanks
-- PMM

author maintainer
#4 lore raw
Quoted 35 lines · Peter Maydell
Quoted 34 lines · Krzysztof Kozlowski
Quoted 14 lines · Peter Maydell
Quoted 13 lines · Krzysztof Kozlowski

Add emulation for Exynos4210 Pseudo Random Number Generator which could work on fixed seeds or with seeds provided by True Random Number Generator block inside the SoC.

Implement only the fixed seeds part of it in polling mode (no interrupts). Simple testing:

    # echo "exynos" > /sys/class/misc/hw_random/rng_current
    # dd if=/dev/hwrng of=/dev/null bs=1 count=16

---

Quoted 8 lines
Quoted 5 lines

Changes since v1:
1. Use GRand-like functions to fix build on MingW32 (this adds also finalize).
2. Add DPRINTF macro.
3. Use HWADDR_PRIx and family for printing values.

Is there a data sheet that describes this RNG? I had a quick google but couldn't find anything in the 4210 manual you can get from Samsung.

Official and public datasheet - I never heard about it... AFAIK, Samsung never released any datasheet... But recently I found a copy of Exynos4412 datasheet published on FriendlyArm website:
http://wiki.friendlyarm.com/wiki/index.php/NanoPC-T1
(at the bottom in "Resources").

Some blocks in Exynos4412, including the RNG, are the same as in Exynos4210. However, you should not expect too much data about the RNG in the datasheet...

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

In particular I'm not sure we want to use GRand here.

Now, I am not sure neither. :)

The last RNG we added was hw/misc/bcm2835_rng.c, which uses qcrypto_random_bytes() to get cryptographically-random bytes from the host. On the other hand it sounds like this Exynos hardware is a PRNG without true-random input...

Yes, I think that is the case. The PRNG block looks the same on all Exynos SoCs. At least from datasheet perspective and registers. The difference came with Exynos5420 with introducing another block - True RNG - which could be chained to PRNG as seed. However the PRNG stays the same (according to datasheet).

Unfortunately I could not verify too much of this because on Exynos5420 apparently the PRNG block is locked by SecureMonitor.

At some point I will probably get back to Exynos5420 (and True RNG) but till then, the choice of GRand repeatable random sequences makes some sense to me.

The remaining issue with this QEMU patch, brought to light during discussions on LKML, is that it does not accept multiple seeds. From what I understood, a good PRNG should be able to consume more seeds at one pass (into same registers) thus. In other words these cases should produce different results:
1. Seed(a), random
2. Seed(b), Seed(a), random

As of now, I think GRand will not allow this.

The hardware on Exynos4412 behaves like this plus it generates random numbers even for the same seed after reboot (which could mean that it was seeded by bootloader or Secure OS).

Best regards,
Krzysztof

#5 lore raw
Quoted 31 lines · Krzysztof Kozlowski
Quoted 15 lines · Peter Maydell
Quoted 14 lines · Krzysztof Kozlowski
Quoted 2 lines · Peter Maydell

Is there a data sheet that describes this RNG? I had a quick google but couldn't find anything in the 4210 manual you can get from Samsung.

Official and public datasheet - I never heard about it... AFAIK, Samsung never released any datasheet... But recently I found a copy of Exynos4412 datasheet published on FriendlyArm website:
http://wiki.friendlyarm.com/wiki/index.php/NanoPC-T1
(at the bottom in "Resources").

Some blocks in Exynos4412, including the RNG, are the same as in Exynos4210. However, you should not expect too much data about the RNG in the datasheet...

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

In particular I'm not sure we want to use GRand here.

Now, I am not sure neither. :)

The last RNG we added was hw/misc/bcm2835_rng.c, which uses qcrypto_random_bytes() to get cryptographically-random bytes from the host. On the other hand it sounds like this Exynos hardware is a PRNG without true-random input...

Yes, I think that is the case. The PRNG block looks the same on all Exynos SoCs. At least from datasheet perspective and registers. The difference came with Exynos5420 with introducing another block - True RNG - which could be chained to PRNG as seed. However the PRNG stays the same (according to datasheet).

Quoted 6 lines

Unfortunately I could not verify too much of this because on Exynos5420 apparently the PRNG block is locked by SecureMonitor.

At some point I will probably get back to Exynos5420 (and True RNG) but till then, the choice of GRand repeatable random sequences makes some sense to me.

I was talking to Dan on IRC and his suggestion was that we should just use qcrypto_random_bytes() for everything. On the other hand I'm not totally sure that's the right approach.

I think my core issue here is that we really don't understand what the hardware is doing or what the properties of its randomness actually are. To me that says "Linux should not trust this thing at all and shouldn't use it" which in turn makes implementing a model in QEMU less important :-)

thanks
-- PMM

author maintainer
#6 lore raw
Quoted 43 lines · Peter Maydell
Quoted 31 lines · Krzysztof Kozlowski
Quoted 15 lines · Peter Maydell
Quoted 14 lines · Krzysztof Kozlowski
Quoted 2 lines · Peter Maydell

Is there a data sheet that describes this RNG? I had a quick google but couldn't find anything in the 4210 manual you can get from Samsung.

Official and public datasheet - I never heard about it... AFAIK, Samsung never released any datasheet... But recently I found a copy of Exynos4412 datasheet published on FriendlyArm website:
http://wiki.friendlyarm.com/wiki/index.php/NanoPC-T1
(at the bottom in "Resources").

Some blocks in Exynos4412, including the RNG, are the same as in Exynos4210. However, you should not expect too much data about the RNG in the datasheet...

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

In particular I'm not sure we want to use GRand here.

Now, I am not sure neither. :)

The last RNG we added was hw/misc/bcm2835_rng.c, which uses qcrypto_random_bytes() to get cryptographically-random bytes from the host. On the other hand it sounds like this Exynos hardware is a PRNG without true-random input...

Yes, I think that is the case. The PRNG block looks the same on all Exynos SoCs. At least from datasheet perspective and registers. The difference came with Exynos5420 with introducing another block - True RNG - which could be chained to PRNG as seed. However the PRNG stays the same (according to datasheet).

Quoted 6 lines

Unfortunately I could not verify too much of this because on Exynos5420 apparently the PRNG block is locked by SecureMonitor.

At some point I will probably get back to Exynos5420 (and True RNG) but till then, the choice of GRand repeatable random sequences makes some sense to me.

I was talking to Dan on IRC and his suggestion was that we should just use qcrypto_random_bytes() for everything. On the other hand I'm not totally sure that's the right approach.

Quoted 5 lines

I think my core issue here is that we really don't understand what the hardware is doing or what the properties of its randomness actually are. To me that says "Linux should not trust this thing at all and shouldn't use it" which in turn makes implementing a model in QEMU less important :-)

That makes sense. I can only guess by experiments how the hardware really behaves. As I wrote, it seems it is not a reproducible PRNG so using qcrypto_random_bytes() should be fine.

I'll switch to that interface.

But first I need to setup proper booting image + rootfs instead of only initramfs. The sdhci node is failing (missing regulators or capabilities) so essentially there is no storage now.

How do you access any kind of disk image in case of other boards?

Best regards,
Krzysztof

#7 lore raw
Quoted 5 lines · Krzysztof Kozlowski

But first I need to setup proper booting image + rootfs instead of only initramfs. The sdhci node is failing (missing regulators or capabilities) so essentially there is no storage now.

How do you access any kind of disk image in case of other boards?

Depends on the board, some of them have SD controllers, a few have PCI controllers that can take a virtio or scsi controller.

thanks
-- PMM