enable CAAM's HWRNG as default
v2 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
Feedback is welcome!
Thanks,
Andrey Smirnov
[discussion] https://patchwork.kernel.org/patch/9850669/
[v1] lore.kernel.org/lkml/[email protected]
Andrey Smirnov (6):
crypto: caam - RNG4 TRNG errata
crypto: caam - enable prediction resistance in HRWNG
crypto: caam - allocate RNG instantiation descriptor with GFP_DMA
crypto: caam - move RNG presense 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 | 29 ++- drivers/crypto/caam/desc.h | 2 + drivers/crypto/caam/drng.c | 175 +++++++++++++++++ drivers/crypto/caam/intern.h | 32 ++- drivers/crypto/caam/jr.c | 3 +- drivers/crypto/caam/regs.h | 14 +- drivers/crypto/caam/trng.c | 85 ++++++++ 10 files changed, 334 insertions(+), 382 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 v2 1/6 crypto: caam - RNG4 TRNG errata
The TRNG as used in RNG4, used in CAAM has a documentation issue. The effect is that it is possible that the entropy used to instantiate the DRBG may be old entropy, rather than newly generated entropy. There is proper programming guidance, but it is not in the documentation.
--- drivers/crypto/caam/ctrl.c | 11 ++++++++--- drivers/crypto/caam/regs.h | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-)
drivers/crypto/caam/ctrl.c +8 -3
@@ -338,8 +338,12 @@ static void kick_trng(struct platform_device *pdev, int ent_delay) ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl; r4tst = &ctrl->r4tst[0 ]; -/* put RNG4 into program mode */ -clrsetbits_32 (&r4tst->rtmctl,0 ,RTMCTL_PRGM ); +/* +* Setting both RTMCTL:PRGM and RTMCTL:TRNG_ACC causes TRNG to +* properly invalidate the entropy in the entropy register and +* force re-generation. +*/ +clrsetbits_32 (&r4tst->rtmctl,0 ,RTMCTL_PRGM |RTMCTL_ACC );/* * Performance-wise, it does not make sense to
@@ -369,7 +373,8 @@ static void kick_trng(struct platform_device *pdev, int ent_delay)* select raw sampling in both entropy shifter * and statistical checker; ; put RNG4 into run mode */ -clrsetbits_32 (&r4tst->rtmctl,RTMCTL_PRGM ,RTMCTL_SAMP_MODE_RAW_ES_SC ); +clrsetbits_32 (&r4tst->rtmctl,RTMCTL_PRGM |RTMCTL_ACC , +RTMCTL_SAMP_MODE_RAW_ES_SC ); }static int caam_get_era_from_hw (struct caam_ctrl __iomem *ctrl)
drivers/crypto/caam/regs.h +2 -1
@@ -487,7 +487,8 @@ 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_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 */
Signature · 1 line
2.21.0
PATCH v2 2/6 crypto: caam - enable prediction resistance in HRWNG
In reply to Andrey SmirnovInstantiate CAAM RNG with prediction resistance enabled to improve its quality.
--- drivers/crypto/caam/caamrng.c | 3 ++- drivers/crypto/caam/ctrl.c | 8 +++++--- drivers/crypto/caam/desc.h | 2 ++ drivers/crypto/caam/regs.h | 4 +++- 4 files changed, 12 insertions(+), 5 deletions(-)
drivers/crypto/caam/caamrng.c +2 -1
@@ -202,7 +202,8 @@ static inline int rng_create_sh_desc(struct caam_rng_ctx *ctx)init_sh_desc (desc,HDR_SHARE_SERIAL );/* Generate random bytes */ -append_operation (desc,OP_ALG_ALGSEL_RNG |OP_TYPE_CLASS1_ALG ); +append_operation (desc,OP_ALG_ALGSEL_RNG |OP_TYPE_CLASS1_ALG | +OP_ALG_PR_ON );/* Store bytes */ append_seq_fifo_store (desc,RN_BUF_SIZE ,FIFOST_TYPE_RNGSTORE );
drivers/crypto/caam/ctrl.c +5 -3
@@ -36,7 +36,8 @@ static void build_instantiation_desc(u32 *desc, int handle, int do_sk)init_job_desc (desc,0 ); op_flags =OP_TYPE_CLASS1_ALG |OP_ALG_ALGSEL_RNG | - (handle <<OP_ALG_AAI_SHIFT ) |OP_ALG_AS_INIT ; + (handle <<OP_ALG_AAI_SHIFT ) |OP_ALG_AS_INIT | +OP_ALG_PR_ON ;/* INIT RNG in non-test mode */ append_operation (desc, op_flags);
@@ -275,11 +276,12 @@ static int instantiate_rng(struct device *ctrldev, int state_handle_mask,return -ENOMEM ;for (sh_idx =0 ; sh_idx <RNG4_MAX_HANDLES ; sh_idx++) { +const u32 rdsta_mask = (RDSTA_PR0 |RDSTA_IF0 ) << sh_idx;/* * If the corresponding bit is set, this state handle * was initialized by somebody else, so it's left alone. */ -if ((1 << sh_idx) & state_handle_mask) +if (rdsta_mask & state_handle_mask)continue ;/* Create the descriptor for instantiating RNG State Handle */
@@ -302,7 +304,7 @@ static int instantiate_rng(struct device *ctrldev, int state_handle_mask, rdsta_val =rd_reg32 (&ctrl->r4tst[0 ].rdsta) &RDSTA_IFMASK ;if ((status && status !=JRSTA_SSRC_JUMP_HALT_CC ) || - !(rdsta_val & (1 << sh_idx))) { + (rdsta_val & rdsta_mask) != rdsta_mask) { ret = -EAGAIN ;break ; }
drivers/crypto/caam/desc.h +2 -0
@@ -1254,6 +1254,8 @@#define OP_ALG_ICV_OFF (0 << OP_ALG_ICV_SHIFT)#define OP_ALG_ICV_ON (1 << OP_ALG_ICV_SHIFT) +#define OP_ALG_PR_ON BIT(1) +#define OP_ALG_DIR_SHIFT 0#define OP_ALG_DIR_MASK 1#define OP_ALG_DECRYPT 0
drivers/crypto/caam/regs.h +3 -1
@@ -524,9 +524,11 @@ struct rng4tst {u32 rsvd1[40 ];#define RDSTA_SKVT 0x80000000#define RDSTA_SKVN 0x40000000 +#define RDSTA_PR0 BIT(4) +#define RDSTA_PR1 BIT(5)#define RDSTA_IF0 0x00000001#define RDSTA_IF1 0x00000002 -#define RDSTA_IFMASK (RDSTA_IF1 | RDSTA_IF0) +#define RDSTA_IFMASK (RDSTA_PR1 | RDSTA_PR0 | RDSTA_IF1 | RDSTA_IF0)u32 rdsta;u32 rsvd2[15 ]; };
Signature · 1 line
2.21.0
PATCH v2 3/6 crypto: caam - allocate RNG instantiation descriptor with GFP_DMA
In reply to Andrey SmirnovBe 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
@@ -194,7 +194,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 ;
@@ -271,7 +271,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 v2 4/6 crypto: caam - move RNG presense check into a shared function
In reply to Andrey SmirnovMove the code to check if RNG block is instantitated 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
@@ -314,19 +314,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 v2 6/6 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 | 175 +++++++++++++++++++++++++++++++++++ drivers/crypto/caam/intern.h | 13 +++ drivers/crypto/caam/jr.c | 1 + 5 files changed, 198 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 +175 -0
175 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
PATCH v2 5/6 crypto: caam - replace DRNG with TRNG for use with hw_random
In reply to Andrey SmirnovIn order to give CAAM-generated random data highest quarlity raiting (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 | 351 ---------------------------------- drivers/crypto/caam/ctrl.c | 6 + drivers/crypto/caam/intern.h | 9 +- drivers/crypto/caam/jr.c | 2 - drivers/crypto/caam/regs.h | 7 +- drivers/crypto/caam/trng.c | 85 ++++++++ 8 files changed, 107 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 -351
351 lines not on this page · raw message
drivers/crypto/caam/ctrl.c +6 -0
@@ -907,6 +907,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 +5 -2
@@ -488,6 +488,7 @@ struct rngtst {/* RNG4 TRNG test registers */ struct rng4tst {#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
@@ -521,7 +522,9 @@ 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_PR0 BIT(4)
@@ -530,7 +533,7 @@ struct rng4tst {#define RDSTA_IF1 0x00000002#define RDSTA_IFMASK (RDSTA_PR1 | RDSTA_PR0 | RDSTA_IF1 | RDSTA_IF0)u32 rdsta; -u32 rsvd2[15 ]; +u32 rsvd3[15 ]; };/*
drivers/crypto/caam/trng.c new file +85 -0
85 lines not on this page · raw message
Signature · 1 line
2.21.0
Re: [PATCH v2 5/6] crypto: caam - replace DRNG with TRNG for use with hw_random
Quoted 14 lines · Andrey Smirnov
In order to give CAAM-generated random data highest quarlity raiting (999), replace current code that uses DRNG with code that fetches data straight out of TRNG used to seed aforementioned DRNG.
Signed-off-by Andrey Smirnov <[email protected]>Cc Chris Healy <[email protected]>Cc Lucas Stach <[email protected]>Cc Horia Geantă <[email protected]>Cc Herbert Xu <[email protected]>Cc Iuliana Prodan <[email protected]>---
[...]
drivers/crypto/caam/trng.c line 5
Quoted 11 lines
drivers/crypto/caam/trng.c new file +5
-0@@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* hw_random interface for TRNG generator in CAAM RNG block +* +* Copyright 2019 Zoidac Inflight Innovations
^ Zodiac
Quoted 22 lines
drivers/crypto/caam/trng.c new file +22
-0+* +*/ + +#include <linux/hw_random.h> + +#include "compat.h" +#include "regs.h" +#include "intern.h" + +struct caam_trng_ctx { +struct rng4tst __iomem *r4tst; +struct hwrng rng; +}; + +static bool caam_trng_busy (struct caam_trng_ctx *ctx) +{ +return !(rd_reg32 (&ctx->r4tst->rtmctl) &RTMCTL_ENT_VAL ); +} + +static int caam_trng_read (struct hwrng *rng,void *data,size_t max,bool wait) +{ +struct caam_trng_ctx *ctx = (void *)rng->priv;
drivers/crypto/caam/trng.c line 37
Quoted 10 lines
drivers/crypto/caam/trng.c new file +10
-0+u32 rtent[ARRAY_SIZE (ctx->r4tst->rtent)]; +size_t residue = max; + +clrsetbits_32 (&ctx->r4tst->rtmctl,0 ,RTMCTL_ACC ); + + do { + const size_t chunk = min(residue, sizeof(rtent)); + unsigned int i; + + while (caam_trng_busy(ctx)) {
The CAAM needs quite a bit of time to gather the 384bits of raw entropy, in my testing it was almost 60ms. A busy loop (even with a cpu_relax) for such an extended amount of time is probably not appropriate, better sleep for some time here.
Also in the !wait case we are almost guaranteed to leave this function without any entropy gathered. Maybe we should just bail out on !wait without even trying to enable the TRNG access?
Regards,
Lucas
Quoted 48 lines
drivers/crypto/caam/trng.c new file +48
-0+if (wait) +cpu_relax (); +else + goto out; + } + +for (i =0 ; i <DIV_ROUND_UP (chunk,sizeof (u32)); i++) + rtent[i] =rd_reg32 (&ctx->r4tst->rtent[i]); + +memcpy (data, rtent, chunk); + + residue -= chunk; + data += chunk; + }while (residue); + +out: +clrsetbits_32 (&ctx->r4tst->rtmctl,RTMCTL_ACC ,0 ); +return max - residue; +} + +int caam_trng_register (struct device *ctrldev) +{ +struct caam_drv_private *priv =dev_get_drvdata (ctrldev); + +if (caam_has_rng (priv)) { +struct caam_trng_ctx *ctx; +int err; + + ctx =devm_kzalloc (ctrldev,sizeof (*ctx),GFP_KERNEL ); +if (!ctx) +return -ENOMEM ; + + ctx->r4tst = &priv->ctrl->r4tst[0 ]; + + ctx->rng.name ="trng-caam" ; + ctx->rng.read = caam_trng_read; + ctx->rng.priv = (unsigned long )ctx; + ctx->rng.quality =999 ; + +dev_info (ctrldev,"registering %s\n" , ctx->rng.name); + + err =devm_hwrng_register (ctrldev, &ctx->rng); +if (err) +return err; + } + +return 0 ; +}
Re: [PATCH v2 5/6] crypto: caam - replace DRNG with TRNG for use with hw_random
Quoted 17 lines · Lucas Stach
Quoted 14 lines · Andrey Smirnov
In order to give CAAM-generated random data highest quarlity raiting (999), replace current code that uses DRNG with code that fetches data straight out of TRNG used to seed aforementioned DRNG.
Signed-off-by Andrey Smirnov <[email protected]>Cc Chris Healy <[email protected]>Cc Lucas Stach <[email protected]>Cc Horia Geantă <[email protected]>Cc Herbert Xu <[email protected]>Cc Iuliana Prodan <[email protected]>---
[...]
Quoted 13 lines
drivers/crypto/caam/trng.c line 5
Quoted 11 lines
drivers/crypto/caam/trng.c new file +5
-0@@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* hw_random interface for TRNG generator in CAAM RNG block +* +* Copyright 2019 Zoidac Inflight Innovations ^ Zodiac
Ugh, thanks for catching this, will fix in v3
Quoted 29 lines
Quoted 29 lines
drivers/crypto/caam/trng.c new file +29
-0+* +*/ + +#include <linux/hw_random.h> + +#include "compat.h" +#include "regs.h" +#include "intern.h" + +struct caam_trng_ctx { +struct rng4tst __iomem *r4tst; +struct hwrng rng; +}; + +static bool caam_trng_busy (struct caam_trng_ctx *ctx) +{ +return !(rd_reg32 (&ctx->r4tst->rtmctl) &RTMCTL_ENT_VAL ); +} + +static int caam_trng_read (struct hwrng *rng,void *data,size_t max,bool wait) +{ +struct caam_trng_ctx *ctx = (void *)rng->priv; +u32 rtent[ARRAY_SIZE (ctx->r4tst->rtent)]; +size_t residue = max; + +clrsetbits_32 (&ctx->r4tst->rtmctl,0 ,RTMCTL_ACC ); + + do { + const size_t chunk = min(residue, sizeof(rtent));
Quoted 9 lines
drivers/crypto/caam/trng.c line 37
Quoted 3 lines
drivers/crypto/caam/trng.c new file +3
-0+unsigned int i; + +while (caam_trng_busy (ctx)) {The CAAM needs quite a bit of time to gather the 384bits of raw entropy, in my testing it was almost 60ms. A busy loop (even with a
cpu_relax) for such an extended amount of time is probably not appropriate, better sleep for some time here.
Good point, will fix in v3.
Quoted 4 lines
Also in the !wait case we are almost guaranteed to leave this function without any entropy gathered. Maybe we should just bail out on !wait without even trying to enable the TRNG access?
Yeah, I think you're right. Will change in v3.
Thanks,
Andrey Smirnov