Skip to content

Commit 2be0d80

Browse files
Victoria Milhoan (b42089)herbertx
authored andcommitted
crypto: caam - add a test for the RNG
CAAM includes a Random Number Generator. This change adds a kernel configuration option to test the RNG's capabilities via the hw_random framework. Signed-off-by: Victoria Milhoan <[email protected]> Signed-off-by: Dan Douglass <[email protected]> Signed-off-by: Vipul Kumar <[email protected]> Signed-off-by: Horia Geantă <[email protected]> Signed-off-by: Meenakshi Aggarwal <[email protected]> Reviewed-by: Gaurav Jain <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent ef492d0 commit 2be0d80

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

drivers/crypto/caam/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ config CRYPTO_DEV_FSL_CAAM_PRNG_API
162162
config CRYPTO_DEV_FSL_CAAM_BLOB_GEN
163163
bool
164164

165+
config CRYPTO_DEV_FSL_CAAM_RNG_TEST
166+
bool "Test caam rng"
167+
select CRYPTO_DEV_FSL_CAAM_RNG_API
168+
help
169+
Selecting this will enable a self-test to run for the
170+
caam RNG.
171+
This test is several minutes long and executes
172+
just before the RNG is registered with the hw_random API.
173+
165174
endif # CRYPTO_DEV_FSL_CAAM_JR
166175

167176
endif # CRYPTO_DEV_FSL_CAAM

drivers/crypto/caam/caamrng.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,50 @@ static void caam_cleanup(struct hwrng *rng)
172172
kfifo_free(&ctx->fifo);
173173
}
174174

175+
#ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_TEST
176+
static inline void test_len(struct hwrng *rng, size_t len, bool wait)
177+
{
178+
u8 *buf;
179+
int read_len;
180+
struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng);
181+
struct device *dev = ctx->ctrldev;
182+
183+
buf = kcalloc(CAAM_RNG_MAX_FIFO_STORE_SIZE, sizeof(u8), GFP_KERNEL);
184+
185+
while (len > 0) {
186+
read_len = rng->read(rng, buf, len, wait);
187+
188+
if (read_len < 0 || (read_len == 0 && wait)) {
189+
dev_err(dev, "RNG Read FAILED received %d bytes\n",
190+
read_len);
191+
kfree(buf);
192+
return;
193+
}
194+
195+
print_hex_dump_debug("random bytes@: ",
196+
DUMP_PREFIX_ADDRESS, 16, 4,
197+
buf, read_len, 1);
198+
199+
len = len - read_len;
200+
}
201+
202+
kfree(buf);
203+
}
204+
205+
static inline void test_mode_once(struct hwrng *rng, bool wait)
206+
{
207+
test_len(rng, 32, wait);
208+
test_len(rng, 64, wait);
209+
test_len(rng, 128, wait);
210+
}
211+
212+
static void self_test(struct hwrng *rng)
213+
{
214+
pr_info("Executing RNG SELF-TEST with wait\n");
215+
test_mode_once(rng, true);
216+
}
217+
#endif
218+
175219
static int caam_init(struct hwrng *rng)
176220
{
177221
struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng);
@@ -258,6 +302,10 @@ int caam_rng_init(struct device *ctrldev)
258302
return ret;
259303
}
260304

305+
#ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_TEST
306+
self_test(&ctx->rng);
307+
#endif
308+
261309
devres_close_group(ctrldev, caam_rng_init);
262310
return 0;
263311
}

0 commit comments

Comments
 (0)