hw/crypto: add Allwinner sun4i-ss crypto device
PATCH 1/1 hw/crypto: add Allwinner sun4i-ss crypto device
From: Corentin LABBE <[email protected]>
The Allwinner A10 has a cryptographic offloader device which could be easily emulated.
The emulated device is tested with Linux only as any of BSD does not support it.
--- MAINTAINERS | 8 + docs/system/arm/cubieboard.rst | 1 + docs/system/devices/allwinner-sun4i-ss.rst | 31 + hw/Kconfig | 1 + hw/arm/Kconfig | 1 + hw/arm/allwinner-a10.c | 11 + hw/crypto/Kconfig | 2 + hw/crypto/allwinner-sun4i-ss.c | 688 +++++++++++++++++++++ hw/crypto/meson.build | 3 + hw/crypto/trace-events | 3 + hw/crypto/trace.h | 1 + hw/meson.build | 1 + include/hw/arm/allwinner-a10.h | 2 + include/hw/crypto/allwinner-sun4i-ss.h | 72 +++ meson.build | 1 + 15 files changed, 826 insertions(+) create mode 100644 docs/system/devices/allwinner-sun4i-ss.rst create mode 100644 hw/crypto/Kconfig create mode 100644 hw/crypto/allwinner-sun4i-ss.c create mode 100644 hw/crypto/meson.build create mode 100644 hw/crypto/trace-events create mode 100644 hw/crypto/trace.h create mode 100644 include/hw/crypto/allwinner-sun4i-ss.h
MAINTAINERS +8 -0
@@ -583,6 +583,14 @@ F: include/hw/*/allwinner-h3* F: hw/arm/orangepi.c F: docs/system/arm/orangepi.rst +Allwinner sun4i-ss cryptographic offloader +M: Corentin LABBE <[email protected]> +L: [email protected] +S: Maintained +F: hw/crypto/allwinner-* +F: include/hw/crypto/allwinner-* +F: docs/system/arm/orangepi.rst + ARM PrimeCell and CMSDK devices M: Peter Maydell <[email protected]> L: [email protected]
docs/system/arm/cubieboard.rst +1 -0
@@ -14,3 +14,4 @@ Emulated devices: - SDHCI - USB controller - SATA controller +- crypto
docs/system/devices/allwinner-sun4i-ss.rst new file +31 -0
@@ -0,0 +1,31 @@ +Allwinner sun4i-ss +================== + +The ``sun4i-ss`` emulates the Allwinner cryptographic offloader +present on early Allwinner SoCs (A10, A10s, A13, A20, A33) +In qemu only A10 via the cubieboard machine is supported. + +The emulated hardware is capable of handling the following algorithms: +- SHA1 and MD5 hash algorithms +- AES/DES/DES3 in CBC/ECB +- PRNG + +The emulated hardware does not handle yet: +- CTS for AES +- CTR for AES/DES/DES3 +- IRQ and DMA mode +Anyway the Linux driver also does not handle them yet. + +The emulation needs a real crypto backend, for the moment only gnutls/nettle is supported. +So the device emulation needs qemu to be compiled with optionnal gnutls. + +Emulation limit +--------------- + +PRNG: +The PRNG is not really emulated as its internal is not known. +It is replaced by the g_random_int function from glib. + +SPEED: +The emulated hardware is ""faster"" than real hw as any write +already give results immediatly instead of a few delay in real HW.
hw/Kconfig +1 -0
@@ -6,6 +6,7 @@ source audio/Kconfig source block/Kconfig source char/Kconfig source core/Kconfig +source crypto/Kconfig source display/Kconfig source dma/Kconfig source gpio/Kconfig
hw/arm/Kconfig +1 -0
@@ -317,6 +317,7 @@ config ALLWINNER_A10 select AHCI select ALLWINNER_A10_PIT select ALLWINNER_A10_PIC + select ALLWINNER_CRYPTO_SUN4I_SS select ALLWINNER_EMAC select SERIAL select UNIMP
hw/arm/allwinner-a10.c +11 -0
@@ -23,6 +23,7 @@#include "hw/misc/unimp.h" #include "sysemu/sysemu.h" #include "hw/boards.h" +#include "hw/crypto/allwinner-sun4i-ss.h" #include "hw/usb/hcd-ohci.h" #define AW_A10_MMC0_BASE 0x01c0f000
@@ -32,6 +33,7 @@#define AW_A10_EMAC_BASE 0x01c0b000#define AW_A10_EHCI_BASE 0x01c14000#define AW_A10_OHCI_BASE 0x01c14400 +#define AW_A10_CRYPTO_BASE 0x01c15000#define AW_A10_SATA_BASE 0x01c18000#define AW_A10_RTC_BASE 0x01c20d00
@@ -48,6 +50,10 @@ static void aw_a10_init(Object *obj)object_initialize_child (obj,"emac" , &s->emac,TYPE_AW_EMAC ); +#if definedCONFIG_NETTLE +object_initialize_child (obj,"crypto" , &s->crypto,TYPE_AW_SUN4I_SS ); +#endif +object_initialize_child (obj,"sata" , &s->sata,TYPE_ALLWINNER_AHCI );if (machine_usb (current_machine)) {
@@ -115,6 +121,11 @@ static void aw_a10_realize(DeviceState *dev, Error **errp)sysbus_mmio_map (sysbusdev,0 ,AW_A10_EMAC_BASE );sysbus_connect_irq (sysbusdev,0 ,qdev_get_gpio_in (dev,55 )); +#if definedCONFIG_NETTLE +sysbus_realize (SYS_BUS_DEVICE (&s->crypto), &error_fatal); +sysbus_mmio_map (SYS_BUS_DEVICE (&s->crypto),0 ,AW_A10_CRYPTO_BASE ); +#endif +if (!sysbus_realize (SYS_BUS_DEVICE (&s->sata), errp)) {return ; }
hw/crypto/Kconfig new file +2 -0
@@ -0,0 +1,2 @@ +config ALLWINNER_CRYPTO_SUN4I_SS + bool
hw/crypto/allwinner-sun4i-ss.c new file +688 -0
688 lines not on this page · raw message
hw/crypto/meson.build new file +3 -0
@@ -0,0 +1,3 @@ +if nettle.found() + softmmu_ss.add(when: 'CONFIG_ALLWINNER_CRYPTO_SUN4I_SS', if_true: files('allwinner-sun4i-ss.c')) +endif
hw/crypto/trace-events new file +3 -0
@@ -0,0 +1,3 @@ +allwinner_sun4i_ss_reset(void) "HW reset" +allwinner_sun4i_ss_read(uint64_t offset, uint64_t val) "MMIO read: offset=0x%" PRIx64 " value=0x%" PRIx64 +allwinner_sun4i_ss_write(uint64_t offset, uint64_t val) "MMIO write: offset=0x%" PRIx64 " value=0x%" PRIx64
hw/crypto/trace.h new file +1 -0
@@ -0,0 +1 @@ +#include "trace/trace-hw_crypto.h"
hw/meson.build +1 -0
@@ -6,6 +6,7 @@ subdir('block') subdir('char') subdir('core') subdir('cpu') +subdir('crypto') subdir('display') subdir('dma') subdir('gpio')
include/hw/arm/allwinner-a10.h +2 -0
@@ -4,6 +4,7 @@#include "qemu/error-report.h" #include "hw/char/serial.h" #include "hw/arm/boot.h" +#include "hw/crypto/allwinner-sun4i-ss.h" #include "hw/timer/allwinner-a10-pit.h" #include "hw/intc/allwinner-a10-pic.h" #include "hw/net/allwinner_emac.h"
@@ -33,6 +34,7 @@ struct AwA10State {AwA10PITState timer;AwA10PICState intc;AwEmacState emac; +AwSun4iSSState crypto;AllwinnerAHCIState sata;AwSdHostState mmc0;AwRtcState rtc;
include/hw/crypto/allwinner-sun4i-ss.h new file +72 -0
72 lines not on this page · raw message
meson.build +1 -0
@@ -2718,6 +2718,7 @@ if have_system 'hw/block', 'hw/block/dataplane', 'hw/char', + 'hw/crypto', 'hw/display', 'hw/dma', 'hw/hppa',
Signature · 1 line
2.35.1
Quoted 9 lines · Corentin Labbe
From: Corentin LABBE <[email protected]>
The Allwinner A10 has a cryptographic offloader device which could be easily emulated.
The emulated device is tested with Linux only as any of BSD does not support it.Signed-off-by Corentin LABBE <[email protected]>
Hi; thanks for this patch, and sorry it's taken me a while to get to reviewing it.
(Daniel, I cc'd you since this device model is making use of crypto related APIs.)
Firstly, a note on patch structure. This is quite a large patch, and I think it would be useful to split it at least into two parts:
(1) add the new device model
(2) change the allwinner SoC to create that new device
Quoted 15 lines
docs/system/arm/cubieboard.rst +1
-0@@ -14,3 +14,4 @@ Emulated devices: - SDHCI - USB controller - SATA controller +- cryptodocs/system/devices/allwinner-sun4i-ss.rst new file +0
-0
docs/system/devices/allwinner-sun4i-ss.rst line 2
Quoted 2 lines
docs/system/devices/allwinner-sun4i-ss.rst new file +2
-0@@ -0,0 +1,31 @@ +Allwinner sun4i-ss +==================
If you create a new rst file in docs, you need to put it into the manual by adding it to some table of contents. Otherwise sphinx will complain when you build the documentation, and users won't be able to find it. (If you pass 'configure' the --enable-docs option that will check that you have everything installed to be able to build the docs.)
There are two options here: you can have this document, and add it to the toctree in docs/system/device-emulation.rst, and make the "crypto" bullet point in cubieboard.rst be a hyperlink to the device-emulation.rst file. Or you can compress the information down and put it into orangepi.rst.
Quoted 23 lines
docs/system/devices/allwinner-sun4i-ss.rst new file +17
-0+The ``sun4i-ss`` emulates the Allwinner cryptographic offloader +present on early Allwinner SoCs (A10, A10s, A13, A20, A33) +In qemu only A10 via the cubieboard machine is supported. + +The emulated hardware is capable of handling the following algorithms: +- SHA1 and MD5 hash algorithms +- AES/DES/DES3 in CBC/ECB +- PRNG + +The emulated hardware does not handle yet: +- CTS for AES +- CTR for AES/DES/DES3 +- IRQ and DMA mode +Anyway the Linux driver also does not handle them yet. + +The emulation needs a real crypto backend, for the moment only gnutls/nettle is supported. +So the device emulation needs qemu to be compiled with optionnal gnutls.hw/Kconfig +0
-0
hw/Kconfig source audio/Kconfig · line 12
Quoted 7 lines
hw/Kconfig +1
-0@@ -6,6 +6,7 @@ source audio/Kconfig source block/Kconfig source char/Kconfig source core/Kconfig +source crypto/Kconfig source display/Kconfig source dma/Kconfig source gpio/Kconfig
I don't think we really need a new subdirectory of hw/ for a single device. If you can find two other devices that already exist in QEMU that would also belong in hw/crypto/ then we can create it. Otherwise just put this device in hw/misc.
Quoted 33 lines
hw/arm/Kconfig +1
-0@@ -317,6 +317,7 @@ config ALLWINNER_A10 select AHCI select ALLWINNER_A10_PIT select ALLWINNER_A10_PIC + select ALLWINNER_CRYPTO_SUN4I_SS select ALLWINNER_EMAC select SERIAL select UNIMPhw/arm/allwinner-a10.c +2
-0@@ -23,6 +23,7 @@#include "hw/misc/unimp.h" #include "sysemu/sysemu.h" #include "hw/boards.h" +#include "hw/crypto/allwinner-sun4i-ss.h" #include "hw/usb/hcd-ohci.h" #define AW_A10_MMC0_BASE 0x01c0f000@@ -32,6 +33,7 @@#define AW_A10_EMAC_BASE 0x01c0b000#define AW_A10_EHCI_BASE 0x01c14000#define AW_A10_OHCI_BASE 0x01c14400 +#define AW_A10_CRYPTO_BASE 0x01c15000#define AW_A10_SATA_BASE 0x01c18000#define AW_A10_RTC_BASE 0x01c20d00
hw/arm/allwinner-a10.c static void aw_a10_init(Object *obj) · line 55
Quoted 6 lines
hw/arm/allwinner-a10.c +3
-0@@ -48,6 +50,10 @@ static void aw_a10_init(Object *obj)object_initialize_child (obj,"emac" , &s->emac,TYPE_AW_EMAC ); +#if definedCONFIG_NETTLE +object_initialize_child (obj,"crypto" , &s->crypto,TYPE_AW_SUN4I_SS ); +#endif
Don't put this kind of ifdef into device/SoC code, please. The device emulation needs to work regardless of what the specific crypto backends that got compiled into QEMU are.
hw/arm/allwinner-a10.c static void aw_a10_init(Object *obj)
Quoted 5 lines
hw/arm/allwinner-a10.c +5
-0+#include <nettle/aes.h> +#include <nettle/cbc.h> +#include <nettle/des.h> +#include <nettle/md5.h> +#include <nettle/sha1.h>
Similarly, don't directly include nettle headers. The device needs to use the backend-agnostic headers from include/crypto. To the extent that they aren't sufficient to implement this device we can look at enhancing them.
hw/arm/allwinner-a10.c
Quoted 10 lines
hw/arm/allwinner-a10.c +10
-0+static const VMStateDescription vmstate_allwinner_sun4i_ss = { + .name ="allwinner-sun4i-ss" , + .version_id =1 , + .minimum_version_id =1 , + .fields = (VMStateField []) { +VMSTATE_UINT32 (ctl, AwSun4iSSState), +VMSTATE_UINT32 (fcsr, AwSun4iSSState), +VMSTATE_UINT32_ARRAY (iv, AwSun4iSSState,5 ), +VMSTATE_UINT32_ARRAY (key, AwSun4iSSState,8 ), +VMSTATE_UINT32_ARRAY (md, AwSun4iSSState,5 ),
Shouldn't this also include rx, rxc, tx, txc ? Or do they never contain live data at the point where we're migrating?
hw/arm/allwinner-a10.c
Quoted 9 lines
hw/arm/allwinner-a10.c +7
-0+ VMSTATE_END_OF_LIST() + } +}; +/** +* Object model +* @{ +*/
We don't use these @{ ... @} markers in our doc comments, so you can delete all of those.
I haven't looked at the rest of the code in detail, but I skimmed over it and didn't see anything that looked wrong.
thanks
-- PMM
Quoted 39 lines · Peter Maydell
Quoted 27 lines · Corentin Labbe
From: Corentin LABBE <[email protected]>
The Allwinner A10 has a cryptographic offloader device which could be easily emulated.
The emulated device is tested with Linux only as any of BSD does not support it.Signed-off-by Corentin LABBE <[email protected]>docs/system/devices/allwinner-sun4i-ss.rst new file +12
-0+The ``sun4i-ss`` emulates the Allwinner cryptographic offloader +present on early Allwinner SoCs (A10, A10s, A13, A20, A33) +In qemu only A10 via the cubieboard machine is supported. + +The emulated hardware is capable of handling the following algorithms: +- SHA1 and MD5 hash algorithms +- AES/DES/DES3 in CBC/ECB +- PRNG + +The emulated hardware does not handle yet: +- CTS for AES +- CTR for AES/DES/DES3docs/system/devices/allwinner-sun4i-ss.rst static void aw_a10_init(Object *obj) · line 55
Quoted 6 lines
docs/system/devices/allwinner-sun4i-ss.rst new file +3
-0@@ -48,6 +50,10 @@ static void aw_a10_init(Object *obj) object_initialize_child(obj, "emac", &s->emac, TYPE_AW_EMAC); +#if defined CONFIG_NETTLE + object_initialize_child(obj, "crypto", &s->crypto, TYPE_AW_SUN4I_SS); +#endifDon't put this kind of ifdef into device/SoC code, please. The device emulation needs to work regardless of what the specific crypto backends that got compiled into QEMU are.
Quoted 10 lines
hw/crypto/allwinner-sun4i-ss.c line 36
Quoted 5 lines
hw/crypto/allwinner-sun4i-ss.c new file +5
-0+#include <nettle/aes.h> +#include <nettle/cbc.h> +#include <nettle/des.h> +#include <nettle/md5.h> +#include <nettle/sha1.h> Similarly, don't directly include nettle headers. The device needs to use the backend-agnostic headers from
include/crypto. To the extent that they aren't sufficient to implement this device we can look at enhancing them.
The include/crypto/{cipher,hash}.h files should provide APIs that cover these uses cases from what I see in this patch.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 14 lines · Peter Maydell
Quoted 9 lines · Corentin Labbe
From: Corentin LABBE <[email protected]>
The Allwinner A10 has a cryptographic offloader device which could be easily emulated.
The emulated device is tested with Linux only as any of BSD does not support it.Signed-off-by Corentin LABBE <[email protected]>Hi; thanks for this patch, and sorry it's taken me a while to get to reviewing it.
Quoted 7 lines
(Daniel, I cc'd you since this device model is making use of crypto related APIs.)
Firstly, a note on patch structure. This is quite a large patch, and I think it would be useful to split it at least into two parts:
(1) add the new device model
(2) change the allwinner SoC to create that new device
Hello
I will do it for next iteration
Quoted 60 lines
Quoted 16 lines
docs/system/arm/cubieboard.rst +1
-0@@ -14,3 +14,4 @@ Emulated devices: - SDHCI - USB controller - SATA controller +- cryptodocs/system/devices/allwinner-sun4i-ss.rst new file +0
-0docs/system/devices/allwinner-sun4i-ss.rst line 2
Quoted 2 lines
docs/system/devices/allwinner-sun4i-ss.rst new file +2
-0@@ -0,0 +1,31 @@ +Allwinner sun4i-ss +==================If you create a new rst file in docs, you need to put it into the manual by adding it to some table of contents. Otherwise sphinx will complain when you build the documentation, and users won't be able to find it. (If you pass 'configure' the --enable-docs option that will check that you have everything installed to be able to build the docs.)
There are two options here: you can have this document, and add it to the toctree in
docs/system/device-emulation.rst, and make the "crypto" bullet point incubieboard.rstbe a hyperlink to thedevice-emulation.rstfile. Or you can compress the information down and put it intoorangepi.rst.Quoted 28 lines
docs/system/devices/allwinner-sun4i-ss.rst new file +17
-0+The ``sun4i-ss`` emulates the Allwinner cryptographic offloader +present on early Allwinner SoCs (A10, A10s, A13, A20, A33) +In qemu only A10 via the cubieboard machine is supported. + +The emulated hardware is capable of handling the following algorithms: +- SHA1 and MD5 hash algorithms +- AES/DES/DES3 in CBC/ECB +- PRNG + +The emulated hardware does not handle yet: +- CTS for AES +- CTR for AES/DES/DES3 +- IRQ and DMA mode +Anyway the Linux driver also does not handle them yet. + +The emulation needs a real crypto backend, for the moment only gnutls/nettle is supported. +So the device emulation needs qemu to be compiled with optionnal gnutls.hw/Kconfig +1
-0@@ -6,6 +6,7 @@ source audio/Kconfig source block/Kconfig source char/Kconfig source core/Kconfig +source crypto/Kconfig source display/Kconfig
Quoted 8 lines
hw/Kconfig source audio/Kconfig · line 12
Quoted 2 lines
hw/Kconfig +0
-0source dma/Kconfig source gpio/KconfigI don't think we really need a new subdirectory of hw/ for a single device. If you can find two other devices that already exist in QEMU that would also belong in hw/crypto/ then we can create it. Otherwise just put this device in hw/misc.
I plan to add at least one other hw/crypto device (allwinner H3 sun8i-ce).
I have another one already ready (rockchip rk3288) but I delay it since there are no related SoC in qemu yet.
Quoted 45 lines
Quoted 34 lines
hw/arm/Kconfig +1
-0@@ -317,6 +317,7 @@ config ALLWINNER_A10 select AHCI select ALLWINNER_A10_PIT select ALLWINNER_A10_PIC + select ALLWINNER_CRYPTO_SUN4I_SS select ALLWINNER_EMAC select SERIAL select UNIMPhw/arm/allwinner-a10.c +2
-0@@ -23,6 +23,7 @@#include "hw/misc/unimp.h" #include "sysemu/sysemu.h" #include "hw/boards.h" +#include "hw/crypto/allwinner-sun4i-ss.h" #include "hw/usb/hcd-ohci.h" #define AW_A10_MMC0_BASE 0x01c0f000@@ -32,6 +33,7 @@#define AW_A10_EMAC_BASE 0x01c0b000#define AW_A10_EHCI_BASE 0x01c14000#define AW_A10_OHCI_BASE 0x01c14400 +#define AW_A10_CRYPTO_BASE 0x01c15000#define AW_A10_SATA_BASE 0x01c18000#define AW_A10_RTC_BASE 0x01c20d00hw/arm/allwinner-a10.c static void aw_a10_init(Object *obj) · line 55
Quoted 6 lines
hw/arm/allwinner-a10.c +3
-0@@ -48,6 +50,10 @@ static void aw_a10_init(Object *obj)object_initialize_child (obj,"emac" , &s->emac,TYPE_AW_EMAC ); +#if definedCONFIG_NETTLE +object_initialize_child (obj,"crypto" , &s->crypto,TYPE_AW_SUN4I_SS ); +#endif Don't put this kind of ifdef into device/SoC code, please. The device emulation needs to work regardless of what the specific crypto backends that got compiled into QEMU are.
Quoted 10 lines
hw/arm/allwinner-a10.c static void aw_a10_init(Object *obj)
Quoted 5 lines
hw/arm/allwinner-a10.c +5
-0+#include <nettle/aes.h> +#include <nettle/cbc.h> +#include <nettle/des.h> +#include <nettle/md5.h> +#include <nettle/sha1.h> Similarly, don't directly include nettle headers. The device needs to use the backend-agnostic headers from
include/crypto. To the extent that they aren't sufficient to implement this device we can look at enhancing them.
Problem is that current qemu crypto backends do not have necessary functions needed by this driver, I need to do basic MD5 transform with custom IV. I will check if it can be added in qemu crypto API.
Quoted 12 lines
hw/arm/allwinner-a10.c
Quoted 11 lines
hw/arm/allwinner-a10.c +10
-0+static const VMStateDescription vmstate_allwinner_sun4i_ss = { + .name ="allwinner-sun4i-ss" , + .version_id =1 , + .minimum_version_id =1 , + .fields = (VMStateField []) { +VMSTATE_UINT32 (ctl, AwSun4iSSState), +VMSTATE_UINT32 (fcsr, AwSun4iSSState), +VMSTATE_UINT32_ARRAY (iv, AwSun4iSSState,5 ), +VMSTATE_UINT32_ARRAY (key, AwSun4iSSState,8 ), +VMSTATE_UINT32_ARRAY (md, AwSun4iSSState,5 ),
Quoted 2 lines
Shouldn't this also include rx, rxc, tx, txc ? Or do they never contain live data at the point where we're migrating?
I will fix this.
Quoted 11 lines
hw/arm/allwinner-a10.c
Quoted 10 lines
hw/arm/allwinner-a10.c +7
-0+ VMSTATE_END_OF_LIST() + } +}; +/** +* Object model +* @{ +*/
Quoted 7 lines
We don't use these @{ ... @} markers in our doc comments, so you can delete all of those.
I haven't looked at the rest of the code in detail, but I skimmed over it and didn't see anything that looked wrong.
thanks
Thanks for your review.
Regards
Quoted 155 lines · LABBE Corentin
Quoted 14 lines · Peter Maydell
Quoted 9 lines · Corentin Labbe
From: Corentin LABBE <[email protected]>
The Allwinner A10 has a cryptographic offloader device which could be easily emulated.
The emulated device is tested with Linux only as any of BSD does not support it.Signed-off-by Corentin LABBE <[email protected]>Hi; thanks for this patch, and sorry it's taken me a while to get to reviewing it.
Quoted 7 lines
(Daniel, I cc'd you since this device model is making use of crypto related APIs.)
Firstly, a note on patch structure. This is quite a large patch, and I think it would be useful to split it at least into two parts:
(1) add the new device model
(2) change the allwinner SoC to create that new deviceHello
I will do it for next iteration
Quoted 60 lines
Quoted 16 lines
docs/system/arm/cubieboard.rst +1
-0@@ -14,3 +14,4 @@ Emulated devices: - SDHCI - USB controller - SATA controller +- cryptodocs/system/devices/allwinner-sun4i-ss.rst new file +0
-0docs/system/devices/allwinner-sun4i-ss.rst line 2
Quoted 2 lines
docs/system/devices/allwinner-sun4i-ss.rst new file +2
-0@@ -0,0 +1,31 @@ +Allwinner sun4i-ss +==================If you create a new rst file in docs, you need to put it into the manual by adding it to some table of contents. Otherwise sphinx will complain when you build the documentation, and users won't be able to find it. (If you pass 'configure' the --enable-docs option that will check that you have everything installed to be able to build the docs.)
There are two options here: you can have this document, and add it to the toctree in
docs/system/device-emulation.rst, and make the "crypto" bullet point incubieboard.rstbe a hyperlink to thedevice-emulation.rstfile. Or you can compress the information down and put it intoorangepi.rst.Quoted 28 lines
docs/system/devices/allwinner-sun4i-ss.rst new file +17
-0+The ``sun4i-ss`` emulates the Allwinner cryptographic offloader +present on early Allwinner SoCs (A10, A10s, A13, A20, A33) +In qemu only A10 via the cubieboard machine is supported. + +The emulated hardware is capable of handling the following algorithms: +- SHA1 and MD5 hash algorithms +- AES/DES/DES3 in CBC/ECB +- PRNG + +The emulated hardware does not handle yet: +- CTS for AES +- CTR for AES/DES/DES3 +- IRQ and DMA mode +Anyway the Linux driver also does not handle them yet. + +The emulation needs a real crypto backend, for the moment only gnutls/nettle is supported. +So the device emulation needs qemu to be compiled with optionnal gnutls.hw/Kconfig +1
-0@@ -6,6 +6,7 @@ source audio/Kconfig source block/Kconfig source char/Kconfig source core/Kconfig +source crypto/Kconfig source display/KconfigQuoted 8 lines
hw/Kconfig source audio/Kconfig · line 12
Quoted 2 lines
hw/Kconfig +0
-0source dma/Kconfig source gpio/KconfigI don't think we really need a new subdirectory of hw/ for a single device. If you can find two other devices that already exist in QEMU that would also belong in hw/crypto/ then we can create it. Otherwise just put this device in hw/misc.
I plan to add at least one other hw/crypto device (allwinner H3 sun8i-ce).
I have another one already ready (rockchip rk3288) but I delay it since there are no related SoC in qemu yet.Quoted 45 lines
Quoted 34 lines
hw/arm/Kconfig +1
-0@@ -317,6 +317,7 @@ config ALLWINNER_A10 select AHCI select ALLWINNER_A10_PIT select ALLWINNER_A10_PIC + select ALLWINNER_CRYPTO_SUN4I_SS select ALLWINNER_EMAC select SERIAL select UNIMPhw/arm/allwinner-a10.c +2
-0@@ -23,6 +23,7 @@#include "hw/misc/unimp.h" #include "sysemu/sysemu.h" #include "hw/boards.h" +#include "hw/crypto/allwinner-sun4i-ss.h" #include "hw/usb/hcd-ohci.h" #define AW_A10_MMC0_BASE 0x01c0f000@@ -32,6 +33,7 @@#define AW_A10_EMAC_BASE 0x01c0b000#define AW_A10_EHCI_BASE 0x01c14000#define AW_A10_OHCI_BASE 0x01c14400 +#define AW_A10_CRYPTO_BASE 0x01c15000#define AW_A10_SATA_BASE 0x01c18000#define AW_A10_RTC_BASE 0x01c20d00hw/arm/allwinner-a10.c static void aw_a10_init(Object *obj) · line 55
Quoted 6 lines
hw/arm/allwinner-a10.c +3
-0@@ -48,6 +50,10 @@ static void aw_a10_init(Object *obj)object_initialize_child (obj,"emac" , &s->emac,TYPE_AW_EMAC ); +#if definedCONFIG_NETTLE +object_initialize_child (obj,"crypto" , &s->crypto,TYPE_AW_SUN4I_SS ); +#endif Don't put this kind of ifdef into device/SoC code, please. The device emulation needs to work regardless of what the specific crypto backends that got compiled into QEMU are.
Quoted 10 lines
hw/arm/allwinner-a10.c static void aw_a10_init(Object *obj)
Quoted 5 lines
hw/arm/allwinner-a10.c +5
-0+#include <nettle/aes.h> +#include <nettle/cbc.h> +#include <nettle/des.h> +#include <nettle/md5.h> +#include <nettle/sha1.h> Similarly, don't directly include nettle headers. The device needs to use the backend-agnostic headers from
include/crypto. To the extent that they aren't sufficient to implement this device we can look at enhancing them.
Quoted 4 lines
Problem is that current qemu crypto backends do not have necessary functions needed by this driver, I need to do basic MD5 transform with custom IV.
I will check if it can be added in qemu crypto API.
If you don't want to/can't extend the crypto API, then at least use the crypto API for all the pieces where it is possible to do so. That way, we can clearly see where the gaps remain for this device.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 161 lines · Daniel P. Berrangé
Quoted 155 lines · LABBE Corentin
Quoted 14 lines · Peter Maydell
Quoted 9 lines · Corentin Labbe
From: Corentin LABBE <[email protected]>
The Allwinner A10 has a cryptographic offloader device which could be easily emulated.
The emulated device is tested with Linux only as any of BSD does not support it.Signed-off-by Corentin LABBE <[email protected]>Hi; thanks for this patch, and sorry it's taken me a while to get to reviewing it.
Quoted 7 lines
(Daniel, I cc'd you since this device model is making use of crypto related APIs.)
Firstly, a note on patch structure. This is quite a large patch, and I think it would be useful to split it at least into two parts:
(1) add the new device model
(2) change the allwinner SoC to create that new deviceHello
I will do it for next iteration
Quoted 60 lines
Quoted 16 lines
docs/system/arm/cubieboard.rst +1
-0@@ -14,3 +14,4 @@ Emulated devices: - SDHCI - USB controller - SATA controller +- cryptodocs/system/devices/allwinner-sun4i-ss.rst new file +0
-0docs/system/devices/allwinner-sun4i-ss.rst line 2
Quoted 2 lines
docs/system/devices/allwinner-sun4i-ss.rst new file +2
-0@@ -0,0 +1,31 @@ +Allwinner sun4i-ss +==================If you create a new rst file in docs, you need to put it into the manual by adding it to some table of contents. Otherwise sphinx will complain when you build the documentation, and users won't be able to find it. (If you pass 'configure' the --enable-docs option that will check that you have everything installed to be able to build the docs.)
There are two options here: you can have this document, and add it to the toctree in
docs/system/device-emulation.rst, and make the "crypto" bullet point incubieboard.rstbe a hyperlink to thedevice-emulation.rstfile. Or you can compress the information down and put it intoorangepi.rst.Quoted 28 lines
docs/system/devices/allwinner-sun4i-ss.rst new file +17
-0+The ``sun4i-ss`` emulates the Allwinner cryptographic offloader +present on early Allwinner SoCs (A10, A10s, A13, A20, A33) +In qemu only A10 via the cubieboard machine is supported. + +The emulated hardware is capable of handling the following algorithms: +- SHA1 and MD5 hash algorithms +- AES/DES/DES3 in CBC/ECB +- PRNG + +The emulated hardware does not handle yet: +- CTS for AES +- CTR for AES/DES/DES3 +- IRQ and DMA mode +Anyway the Linux driver also does not handle them yet. + +The emulation needs a real crypto backend, for the moment only gnutls/nettle is supported. +So the device emulation needs qemu to be compiled with optionnal gnutls.hw/Kconfig +1
-0@@ -6,6 +6,7 @@ source audio/Kconfig source block/Kconfig source char/Kconfig source core/Kconfig +source crypto/Kconfig source display/KconfigQuoted 8 lines
hw/Kconfig source audio/Kconfig · line 12
Quoted 2 lines
hw/Kconfig +0
-0source dma/Kconfig source gpio/KconfigI don't think we really need a new subdirectory of hw/ for a single device. If you can find two other devices that already exist in QEMU that would also belong in hw/crypto/ then we can create it. Otherwise just put this device in hw/misc.
I plan to add at least one other hw/crypto device (allwinner H3 sun8i-ce).
I have another one already ready (rockchip rk3288) but I delay it since there are no related SoC in qemu yet.Quoted 45 lines
Quoted 34 lines
hw/arm/Kconfig +1
-0@@ -317,6 +317,7 @@ config ALLWINNER_A10 select AHCI select ALLWINNER_A10_PIT select ALLWINNER_A10_PIC + select ALLWINNER_CRYPTO_SUN4I_SS select ALLWINNER_EMAC select SERIAL select UNIMPhw/arm/allwinner-a10.c +2
-0@@ -23,6 +23,7 @@#include "hw/misc/unimp.h" #include "sysemu/sysemu.h" #include "hw/boards.h" +#include "hw/crypto/allwinner-sun4i-ss.h" #include "hw/usb/hcd-ohci.h" #define AW_A10_MMC0_BASE 0x01c0f000@@ -32,6 +33,7 @@#define AW_A10_EMAC_BASE 0x01c0b000#define AW_A10_EHCI_BASE 0x01c14000#define AW_A10_OHCI_BASE 0x01c14400 +#define AW_A10_CRYPTO_BASE 0x01c15000#define AW_A10_SATA_BASE 0x01c18000#define AW_A10_RTC_BASE 0x01c20d00hw/arm/allwinner-a10.c static void aw_a10_init(Object *obj) · line 55
Quoted 6 lines
hw/arm/allwinner-a10.c +3
-0@@ -48,6 +50,10 @@ static void aw_a10_init(Object *obj)object_initialize_child (obj,"emac" , &s->emac,TYPE_AW_EMAC ); +#if definedCONFIG_NETTLE +object_initialize_child (obj,"crypto" , &s->crypto,TYPE_AW_SUN4I_SS ); +#endif Don't put this kind of ifdef into device/SoC code, please. The device emulation needs to work regardless of what the specific crypto backends that got compiled into QEMU are.
Quoted 10 lines
hw/arm/allwinner-a10.c static void aw_a10_init(Object *obj)
Quoted 5 lines
hw/arm/allwinner-a10.c +5
-0+#include <nettle/aes.h> +#include <nettle/cbc.h> +#include <nettle/des.h> +#include <nettle/md5.h> +#include <nettle/sha1.h> Similarly, don't directly include nettle headers. The device needs to use the backend-agnostic headers from
include/crypto. To the extent that they aren't sufficient to implement this device we can look at enhancing them.Quoted 4 lines
Problem is that current qemu crypto backends do not have necessary functions needed by this driver, I need to do basic MD5 transform with custom IV.
I will check if it can be added in qemu crypto API.
Quoted 5 lines
If you don't want to/can't extend the crypto API, then at least use the crypto API for all the pieces where it is possible to do so. That way, we can clearly see where the gaps remain for this device.
Hello
Cipher operations are converted to the qemu API without problem, qemu API permits to even made it simplier. (same code path for des/3des/aes).
But for hashs, only nettle has the needed function in its API. (I checked gcrypt, gnutls and glib).
I have added hashcompress in crypto API but not sure if it is worth it if only nettle support it.
Note that nettle export compress functions in its header only for sha1 and md5, but I will send a fix to nettle to include others shaXXX (which will be needed for crypto/sun8i-ce).
Regards
crypto/hash-nettle.c +43 -0
@@ -26,10 +26,22 @@#include <nettle/sha.h> #include <nettle/ripemd160.h> +#ifndef nettle_sha256_compress +#define nettle_sha256_compress _nettle_sha256_compress +void _nettle_sha256_compress (uint32_t *state,const uint8_t *input,const uint32_t *k); +#endif + +#ifndef nettle_sha512_compress +#define nettle_sha512_compress _nettle_sha512_compress +void _nettle_sha512_compress (uint32_t *state,const uint8_t *input,const uint32_t *k); +#endif +typedef void (*qcrypto_nettle_init )(void *ctx);typedef void (*qcrypto_nettle_write )(void *ctx,size_t len,const uint8_t *buf); +typedef void (*qcrypto_nettle_compress )(uint32_t *state, +const uint8_t *buf);typedef void (*qcrypto_nettle_result )(void *ctx,size_t len,uint8_t *buf);
@@ -47,18 +59,21 @@ union qcrypto_hash_ctx {struct qcrypto_hash_alg {qcrypto_nettle_init init;qcrypto_nettle_write write; +qcrypto_nettle_compress compress;qcrypto_nettle_result result;size_t len; } qcrypto_hash_alg_map[] = { [QCRYPTO_HASH_ALG_MD5 ] = { .init = (qcrypto_nettle_init )md5_init, .write = (qcrypto_nettle_write )md5_update, + .compress = (qcrypto_nettle_compress )nettle_md5_compress, .result = (qcrypto_nettle_result )md5_digest, .len =MD5_DIGEST_SIZE , }, [QCRYPTO_HASH_ALG_SHA1 ] = { .init = (qcrypto_nettle_init )sha1_init, .write = (qcrypto_nettle_write )sha1_update, + .compress = (qcrypto_nettle_compress )nettle_sha1_compress, .result = (qcrypto_nettle_result )sha1_digest, .len =SHA1_DIGEST_SIZE , },
@@ -70,6 +85,7 @@ struct qcrypto_hash_alg { }, [QCRYPTO_HASH_ALG_SHA256] = { .init = (qcrypto_nettle_init)sha256_init, + .compress = (qcrypto_nettle_compress)_nettle_sha256_compress, .write = (qcrypto_nettle_write)sha256_update, .result = (qcrypto_nettle_result)sha256_digest, .len = SHA256_DIGEST_SIZE,
@@ -82,6 +98,7 @@ struct qcrypto_hash_alg { }, [QCRYPTO_HASH_ALG_SHA512] = { .init = (qcrypto_nettle_init)sha512_init, + .compress = (qcrypto_nettle_compress)_nettle_sha512_compress, .write = (qcrypto_nettle_write)sha512_update, .result = (qcrypto_nettle_result)sha512_digest, .len = SHA512_DIGEST_SIZE,
@@ -156,6 +173,32 @@ qcrypto_nettle_hash_bytesv(QCryptoHashAlgorithm alg, } +static int +qcrypto_nettle_compress_bytesv (QCryptoHashAlgorithm alg, +const struct iovec *iov, +size_t niov, +uint32_t *state, +Error **errp) +{ +size_t i; + +if (!qcrypto_hash_supports (alg)) { +error_setg (errp, +"Unknown hash algorithm %d" , + alg); +return -1 ; + } + +for (i =0 ; i < niov; i++) { +uint8_t *data = iov[i].iov_base; + qcrypto_hash_alg_map[alg].compress (state, data); + } + +return 0 ; +} + +QCryptoHashDriver qcrypto_hash_lib_driver = { .hash_bytesv = qcrypto_nettle_hash_bytesv, + .compress_bytesv = qcrypto_nettle_compress_bytesv, };
crypto/hash.c +21 -0
@@ -142,3 +142,24 @@ int qcrypto_hash_base64(QCryptoHashAlgorithm alg,return qcrypto_hash_base64v (alg, &iov,1 , base64, errp); } + +int qcrypto_compress_bytesv (QCryptoHashAlgorithm alg, +const struct iovec *iov, +size_t niov, +uint32_t *state, +Error **errp) +{ +return qcrypto_hash_lib_driver.compress_bytesv (alg, iov, niov, state, errp); +} + +int qcrypto_compress_bytes (QCryptoHashAlgorithm alg, +const char *buf, +size_t len, +uint32_t *state, +Error **errp) +{ +struct iovec iov = { .iov_base = (char *)buf, + .iov_len = len }; +return qcrypto_compress_bytesv (alg, &iov,1 , state, errp); +} +
crypto/hashpriv.h +8 -0
@@ -24,6 +24,11 @@ struct QCryptoHashDriver {uint8_t **result, size_t *resultlen, Error **errp); +int qcrypto_compress_bytes (QCryptoHashAlgorithm alg, +const char *buf, +size_t len, +uint32_t *state, +Error **errp) +{ +struct iovec iov = { .iov_base = (char *)buf, + .iov_len = len };
+ return qcrypto_compress_bytesv(alg, &iov, 1, state, errp);
+}
+
crypto/hashpriv.h +5 -0
@@ -24,6 +24,11 @@ struct QCryptoHashDriver {uint8_t **result, size_t *resultlen, Error **errp); +int (*compress_bytesv)(QCryptoHashAlgorithm alg, +const struct iovec *iov, +size_t niov, +uint32_t *state, +Error **errp); };extern QCryptoHashDriver qcrypto_hash_lib_driver;
include/crypto/hash.h +11 -0
@@ -189,4 +189,15 @@ int qcrypto_hash_base64(QCryptoHashAlgorithm alg,char **base64, Error **errp); +int qcrypto_compress_bytesv (QCryptoHashAlgorithm alg, +const struct iovec *iov, +size_t niov, +uint32_t *state, +Error **errp); + +int qcrypto_compress_bytes (QCryptoHashAlgorithm alg, +const char *buf, +size_t len, +uint32_t *state, +Error **errp);#endif /* QCRYPTO_HASH_H */
Quoted 7 lines · LABBE Corentin
crypto/hash-nettle.c +0
-0@@ -26,10 +26,22 @@#include <nettle/sha.h> #include <nettle/ripemd160.h>
crypto/hash-nettle.c line 37
Quoted 10 lines
crypto/hash-nettle.c +9
-0+#ifndef nettle_sha256_compress +#define nettle_sha256_compress _nettle_sha256_compress +void _nettle_sha256_compress (uint32_t *state,const uint8_t *input,const uint32_t *k); +#endif + +#ifndef nettle_sha512_compress +#define nettle_sha512_compress _nettle_sha512_compress +void _nettle_sha512_compress (uint32_t *state,const uint8_t *input,const uint32_t *k); +#endif
The 'nettle_sha256_compress' function doesn't exist in any header file from nettle that I've looked at.
The '_nettle_sha256_compress' function exists as a symbol in the .so library, but it is clearly not intended for public usage:
$ nm -a -D /usr/lib64/libnettle.so | grep sha256_compress 0000000000027730 T _nettle_sha256_compress@@NETTLE_INTERNAL_8_4
So this #define magic is definitely not something we can do.
IOW, unless I'm missing something, I don't think we can even use nettle for your desired goal, which leaves us no impl at all.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 25 lines · Daniel P. Berrangé
Quoted 7 lines · LABBE Corentin
crypto/hash-nettle.c +0
-0@@ -26,10 +26,22 @@#include <nettle/sha.h> #include <nettle/ripemd160.h> crypto/hash-nettle.c line 37
Quoted 10 lines
crypto/hash-nettle.c +9
-0+#ifndef nettle_sha256_compress +#define nettle_sha256_compress _nettle_sha256_compress +void _nettle_sha256_compress (uint32_t *state,const uint8_t *input,const uint32_t *k); +#endif + +#ifndef nettle_sha512_compress +#define nettle_sha512_compress _nettle_sha512_compress +void _nettle_sha512_compress (uint32_t *state,const uint8_t *input,const uint32_t *k); +#endif The '
nettle_sha256_compress' function doesn't exist in any header file from nettle that I've looked at.The '
_nettle_sha256_compress' function exists as a symbol in the .so library, but it is clearly not intended for public usage:
Quoted 8 lines
$ nm -a -D /usr/lib64/libnettle.so | grep sha256_compress 0000000000027730 T _nettle_sha256_compress@@NETTLE_INTERNAL_8_4So this #define magic is definitely not something we can do.
IOW, unless I'm missing something, I don't think we can even use nettle for your desired goal, which leaves us no impl at all.
sha256 is only necessary for the upcomming sun8i-ce, so it do not block sun4i-ss. I try to contact nettle to add necessary headers.
So for sun4i-ss, what do you think about my qcrypto_nettle_compress implementation ?
Quoted 31 lines · LABBE Corentin
Quoted 30 lines · Daniel P. Berrangé
Quoted 7 lines · LABBE Corentin
crypto/hash-nettle.c +0
-0@@ -26,10 +26,22 @@#include <nettle/sha.h> #include <nettle/ripemd160.h> crypto/hash-nettle.c line 37
Quoted 10 lines
crypto/hash-nettle.c +9
-0+#ifndef nettle_sha256_compress +#define nettle_sha256_compress _nettle_sha256_compress +void _nettle_sha256_compress (uint32_t *state,const uint8_t *input,const uint32_t *k); +#endif + +#ifndef nettle_sha512_compress +#define nettle_sha512_compress _nettle_sha512_compress +void _nettle_sha512_compress (uint32_t *state,const uint8_t *input,const uint32_t *k); +#endif The '
nettle_sha256_compress' function doesn't exist in any header file from nettle that I've looked at.The '
_nettle_sha256_compress' function exists as a symbol in the .so library, but it is clearly not intended for public usage:$ nm -a -D /usr/lib64/libnettle.so | grep sha256_compress 0000000000027730 T _nettle_sha256_compress@@NETTLE_INTERNAL_8_4So this #define magic is definitely not something we can do.
Quoted 8 lines
Quoted 3 lines
IOW, unless I'm missing something, I don't think we can even use nettle for your desired goal, which leaves us no impl at all.
sha256 is only necessary for the upcomming sun8i-ce, so it do not block sun4i-ss. I try to contact nettle to add necessary headers.
So for sun4i-ss, what do you think about my
qcrypto_nettle_compressimplementation ?
It is reasonable in general.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|