enable CAAM's HWRNG as default
v5 cover enable CAAM's HWRNG as default
Everyone:
This series is a continuation of original [discussion]. I don't know if what's in the series is enough to use CAAMs HWRNG system wide, but I am hoping that with enough iterations and feedback it will be.
Changes since [v1]:
- Original hw_random replaced with the one using output of TRNG directly
- SEC4 DRNG IP block exposed via crypto API
- Small fix regarding use of GFP_DMA added to the series
Chagnes since [v2]:
- msleep in polling loop to avoid wasting CPU cycles
- caam_trng_read() bails out early if 'wait' is set to 'false'
- fixed typo in ZII's name
Changes since [v3]:
- DRNG's .cra_name is now "stdrng"
- collected Reviewd-by tag from Lucas
- typo fixes in commit messages of the series
Changes since [v4]:
- Dropped "crypto: caam - RNG4 TRNG errata" and "crypto: caam - enable prediction resistance in HRWNG" to limit the scope of the series. Those two patches are not yet ready and can be submitted separately later.
- Collected Tested-by from Chris
Feedback is welcome!
Thanks,
Andrey Smirnov
[discussion] https://patchwork.kernel.org/patch/9850669/
[v1] https://lore.kernel.org/lkml/[email protected]
[v2] https://lore.kernel.org/lkml/[email protected]
[v3] https://lore.kernel.org/lkml/[email protected]
[v4] https://lore.kernel.org/lkml/[email protected]
Andrey Smirnov (4):
crypto: caam - allocate RNG instantiation descriptor with GFP_DMA
crypto: caam - move RNG presence check into a shared function
crypto: caam - replace DRNG with TRNG for use with hw_random
crypto: caam - expose SEC4 DRNG via crypto RNG API
drivers/crypto/caam/Kconfig | 15 +- drivers/crypto/caam/Makefile | 3 +- drivers/crypto/caam/caamrng.c | 358 ---------------------------------- drivers/crypto/caam/ctrl.c | 10 +- drivers/crypto/caam/drng.c | 174 +++++++++++++++++ drivers/crypto/caam/intern.h | 32 ++- drivers/crypto/caam/jr.c | 3 +- drivers/crypto/caam/regs.h | 11 +- drivers/crypto/caam/trng.c | 89 +++++++++ 9 files changed, 320 insertions(+), 375 deletions(-) delete mode 100644 drivers/crypto/caam/caamrng.c create mode 100644 drivers/crypto/caam/drng.c create mode 100644 drivers/crypto/caam/trng.c
Signature · 1 line
2.21.0
PATCH v5 1/4 crypto: caam - allocate RNG instantiation descriptor with GFP_DMA
Be consistent with the rest of the codebase and use GFP_DMA when allocating memory for a CAAM JR descriptor.
--- drivers/crypto/caam/ctrl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/caam/ctrl.c +2 -2
@@ -193,7 +193,7 @@ static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)u32 *desc, status;int sh_idx, ret =0 ; - desc =kmalloc (CAAM_CMD_SZ *3 ,GFP_KERNEL ); + desc =kmalloc (CAAM_CMD_SZ *3 ,GFP_KERNEL |GFP_DMA );if (!desc)return -ENOMEM ;
@@ -270,7 +270,7 @@ static int instantiate_rng(struct device *ctrldev, int state_handle_mask,int ret =0 , sh_idx; ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl; - desc =kmalloc (CAAM_CMD_SZ *7 ,GFP_KERNEL ); + desc =kmalloc (CAAM_CMD_SZ *7 ,GFP_KERNEL |GFP_DMA );if (!desc)return -ENOMEM ;
Signature · 1 line
2.21.0
PATCH v5 2/4 crypto: caam - move RNG presence check into a shared function
In reply to Andrey SmirnovMove the code to check if RNG block is instantiated into a shared function. This will be used by commits that follow.
--- drivers/crypto/caam/caamrng.c | 10 +--------- drivers/crypto/caam/intern.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 9 deletions(-)
drivers/crypto/caam/caamrng.c +1 -9
@@ -313,19 +313,11 @@ void caam_rng_exit(void)int caam_rng_init (struct device *ctrldev) {struct device *dev; -u32 rng_inst;struct caam_drv_private *priv =dev_get_drvdata (ctrldev);int err; init_done = false; -/* Check for an instantiated RNG before registration */ -if (priv->era <10 ) - rng_inst = (rd_reg32 (&priv->ctrl->perfmon.cha_num_ls) & -CHA_ID_LS_RNG_MASK ) >>CHA_ID_LS_RNG_SHIFT ; -else - rng_inst =rd_reg32 (&priv->ctrl->vreg.rng) &CHA_VER_NUM_MASK ; - -if (!rng_inst) +if (!caam_has_rng (priv))return 0 ; dev =caam_jr_alloc ();
drivers/crypto/caam/intern.h +14 -0
@@ -104,6 +104,20 @@ struct caam_drv_private {#endif }; +static inline bool caam_has_rng (struct caam_drv_private *priv) +{ +u32 rng_inst; + +/* Check for an instantiated RNG before registration */ +if (priv->era <10 ) + rng_inst = (rd_reg32 (&priv->ctrl->perfmon.cha_num_ls) & +CHA_ID_LS_RNG_MASK ) >>CHA_ID_LS_RNG_SHIFT ; +else + rng_inst =rd_reg32 (&priv->ctrl->vreg.rng) &CHA_VER_NUM_MASK ; + +return rng_inst; +} +#ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API int caam_algapi_init (struct device *dev);
Signature · 1 line
2.21.0
PATCH v5 3/4 crypto: caam - replace DRNG with TRNG for use with hw_random
In reply to Andrey SmirnovIn order to give CAAM-generated random data highest quality rating (999), replace current code that uses DRNG with code that fetches data straight out of TRNG used to seed aforementioned DRNG.
--- drivers/crypto/caam/Kconfig | 17 +- drivers/crypto/caam/Makefile | 2 +- drivers/crypto/caam/caamrng.c | 350 ---------------------------------- drivers/crypto/caam/ctrl.c | 6 + drivers/crypto/caam/intern.h | 9 +- drivers/crypto/caam/jr.c | 2 - drivers/crypto/caam/regs.h | 11 +- drivers/crypto/caam/trng.c | 89 +++++++++ 8 files changed, 114 insertions(+), 372 deletions(-) delete mode 100644 drivers/crypto/caam/caamrng.c create mode 100644 drivers/crypto/caam/trng.c
drivers/crypto/caam/Kconfig +8 -9
@@ -31,6 +31,14 @@ config CRYPTO_DEV_FSL_CAAM_DEBUG Selecting this will enable printing of various debug information in the CAAM driver. +config CRYPTO_DEV_FSL_CAAM_RNG_API + bool "Register caam device for hwrng API" + default y + select HW_RANDOM + help + Selecting this will register the hardware TRNG to + the hw_random API for suppying the kernel entropy pool. + menuconfig CRYPTO_DEV_FSL_CAAM_JR tristate "Freescale CAAM Job Ring driver backend" default y
@@ -138,15 +146,6 @@ config CRYPTO_DEV_FSL_CAAM_PKC_API Supported cryptographic primitives: encryption, decryption, signature and verification. -config CRYPTO_DEV_FSL_CAAM_RNG_API - bool "Register caam device for hwrng API" - default y - select CRYPTO_RNG - select HW_RANDOM - help - Selecting this will register the SEC4 hardware rng to - the hw_random API for suppying the kernel entropy pool. - endif # CRYPTO_DEV_FSL_CAAM_JR endif # CRYPTO_DEV_FSL_CAAM
drivers/crypto/caam/Makefile +1 -1
@@ -15,11 +15,11 @@ obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_DESC) += caamalg_desc.o obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_AHASH_API_DESC) += caamhash_desc.o caam-y := ctrl.o +caam-$(CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API) += trng.o caam_jr-y := jr.o key_gen.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API) += caamalg.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI) += caamalg_qi.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_AHASH_API) += caamhash.o -caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API) += caamrng.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_PKC_API) += caampkc.o pkc_desc.o caam-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI) += qi.o
drivers/crypto/caam/caamrng.c deleted +0 -350
350 lines not on this page · raw message
drivers/crypto/caam/ctrl.c +6 -0
@@ -900,6 +900,12 @@ static int caam_probe(struct platform_device *pdev) &ctrlpriv->ctl_tdsk_wrap);#endif + ret =caam_trng_register (dev); +if (ret) { +dev_err (dev,"Failed to register HWRNG interface\n" ); +return ret; + } + ret =devm_of_platform_populate (dev);if (ret)dev_err (dev,"JR platform devices creation error\n" );
drivers/crypto/caam/intern.h +2 -7
@@ -174,20 +174,15 @@ static inline void caam_pkc_exit(void)#ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API -int caam_rng_init (struct device *dev); -void caam_rng_exit (void ); +int caam_trng_register (struct device *dev);#else -static inline int caam_rng_init (struct device *dev) +static inline int caam_trng_register (struct device *dev) {return 0 ; } -static inline void caam_rng_exit (void ) -{ -} -#endif /* CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API */ #ifdef CONFIG_CAAM_QI
drivers/crypto/caam/jr.c +0 -2
@@ -37,7 +37,6 @@ static void register_algs(struct device *dev)caam_algapi_init (dev);caam_algapi_hash_init (dev);caam_pkc_init (dev); -caam_rng_init (dev);caam_qi_algapi_init (dev); algs_unlock:
@@ -53,7 +52,6 @@ static void unregister_algs(void)caam_qi_algapi_exit (); -caam_rng_exit ();caam_pkc_exit ();caam_algapi_hash_exit ();caam_algapi_exit ();
drivers/crypto/caam/regs.h +8 -3
@@ -487,7 +487,10 @@ struct rngtst {/* RNG4 TRNG test registers */ struct rng4tst { -#define RTMCTL_PRGM 0x00010000/* 1 -> program mode, 0 -> run mode */ +#define RTMCTL_ACC BIT(5)/* TRNG access mode */ +#define RTMCTL_ENT_VAL BIT(10) +#define RTMCTL_PRGM BIT(16)/* 1 -> program mode, 0 -> run mode */ +#define RTMCTL_SAMP_MODE_VON_NEUMANN_ES_SC 0/* use von Neumann data in both entropy shifter and statistical checker */
@@ -520,14 +523,16 @@ struct rng4tst {u32 rtfrqmax;/* PRGM=1: freq. count max. limit register */ u32 rtfrqcnt;/* PRGM=0: freq. count register */ }; -u32 rsvd1[40 ]; +u32 rsvd1[8 ]; +u32 rtent[16 ];/* RTENT0 - RTENT15 */ +u32 rsvd2[16 ];/* RTPKRCNTn */ #define RDSTA_SKVT 0x80000000#define RDSTA_SKVN 0x40000000#define RDSTA_IF0 0x00000001#define RDSTA_IF1 0x00000002#define RDSTA_IFMASK (RDSTA_IF1 | RDSTA_IF0)u32 rdsta; -u32 rsvd2[15 ]; +u32 rsvd3[15 ]; };/*
drivers/crypto/caam/trng.c new file +89 -0
89 lines not on this page · raw message
Signature · 1 line
2.21.0
PATCH v5 4/4 crypto: caam - expose SEC4 DRNG via crypto RNG API
In reply to Andrey SmirnovExpose SEC4 DRNG IP block using crypto RNG API so it could be used both by kernel and userspace code.
--- drivers/crypto/caam/Kconfig | 8 ++ drivers/crypto/caam/Makefile | 1 + drivers/crypto/caam/drng.c | 174 +++++++++++++++++++++++++++++++++++ drivers/crypto/caam/intern.h | 13 +++ drivers/crypto/caam/jr.c | 1 + 5 files changed, 197 insertions(+) create mode 100644 drivers/crypto/caam/drng.c
drivers/crypto/caam/Kconfig +8 -0
@@ -146,6 +146,14 @@ config CRYPTO_DEV_FSL_CAAM_PKC_API Supported cryptographic primitives: encryption, decryption, signature and verification. +config CRYPTO_DEV_FSL_CAAM_DRNG_API + bool "Register caam device for hwrng API" + default y + select CRYPTO_RNG + help + Selecting this will register the SEC4 DRNG to + the crypto RNG API. + endif # CRYPTO_DEV_FSL_CAAM_JR endif # CRYPTO_DEV_FSL_CAAM
drivers/crypto/caam/Makefile +1 -0
@@ -20,6 +20,7 @@ caam_jr-y := jr.o key_gen.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API) += caamalg.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI) += caamalg_qi.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_AHASH_API) += caamhash.o +caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_DRNG_API) += drng.o caam_jr-$(CONFIG_CRYPTO_DEV_FSL_CAAM_PKC_API) += caampkc.o pkc_desc.o caam-$(CONFIG_CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI) += qi.o
drivers/crypto/caam/drng.c new file +174 -0
174 lines not on this page · raw message
drivers/crypto/caam/intern.h +13 -0
@@ -185,6 +185,19 @@ static inline int caam_trng_register(struct device *dev)#endif /* CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API */ +#ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_DRNG_API + +int caam_drng_register (struct device *dev); + +#else + +static inline int caam_drng_register (struct device *dev) +{ +return 0 ; +} + +#endif /* CONFIG_CRYPTO_DEV_FSL_CAAM_DRNG_API */ +#ifdef CONFIG_CAAM_QI int caam_qi_algapi_init (struct device *dev);
drivers/crypto/caam/jr.c +1 -0
@@ -38,6 +38,7 @@ static void register_algs(struct device *dev)caam_algapi_hash_init (dev);caam_pkc_init (dev);caam_qi_algapi_init (dev); +caam_drng_register (dev); algs_unlock:mutex_unlock (&algs_lock);
Signature · 1 line
2.21.0
Horia Geanta
maintainerQuoted 28 lines · Andrey Smirnov
Everyone:
This series is a continuation of original [discussion]. I don't know if what's in the series is enough to use CAAMs HWRNG system wide, but I am hoping that with enough iterations and feedback it will be.
Changes since [v1]:
- Original
hw_randomreplaced with the one using output of TRNG directly- SEC4 DRNG IP block exposed via crypto API
- Small fix regarding use of
GFP_DMAadded to the seriesChagnes since [v2]:
- msleep in polling loop to avoid wasting CPU cycles
-
caam_trng_read()bails out early if 'wait' is set to 'false'- fixed typo in ZII's name
Changes since [v3]:
- DRNG's .cra_name is now "stdrng"
- collected Reviewd-by tag from Lucas
Quoted 9 lines
- typo fixes in commit messages of the series
Changes since [v4]:
- Dropped "crypto: caam - RNG4 TRNG errata" and "crypto: caam - enable prediction resistance in HRWNG" to limit the scope of the series. Those two patches are not yet ready and can be submitted separately later.
I don't agree with dropping the Job Ring Interface (JRI) in favor of using TRNG registers directly - for the purpose of extracting entropy.
One of the reasons is that TRNG registers are part of page 0, which is not accessible in the Linux kernel in some cases.
It's possible to use JRI for extracting entropy following these steps:
1. Instantiate RNG state handle with Prediction Resistance (PR) support This is optional in cases when page 0 is not under kernel's control. We'll separately modify SW controlling page 0 to offer PR support.
2. For each hwrng read(), enqueue via JRI one or more job descriptors (JD) having the PR bit set in the ALGORITHM OPERATION command.
Note that according to hwrng API, it's ok to *partially* fulfill the request:
* @read: New API. drivers can fill up to max bytes of data
* into the buffer. The buffer is aligned for any type
* and max is a multiple of 4 and >= 32 bytes.
It's important to limit the output of each JD, such that the recommendation in SP800-90C (section "9.4 The Oversampling-NRBG Construction") is followed:
https://csrc.nist.gov/CSRC/media/Publications/sp/800-90c/draft/documents/sp800_90c_second_draft.pdf
For CAAM RNG4, the DRBG security strength is s = 256 bits (32 bytes), thus each JD must extract at most s/2 - 128 bits (16 bytes).
Similar to what's being done for TRNG registers-based implementation, some back-off mechanism is needed, such that DECO won't stall waiting for the TRNG.
This is important on i.MX platforms where there's a single DECO (on PPC & Layerscape platforms there are multiple DECOs).
Horia