b5a4bf1b2d10b573c456264182969638580828e5
[openwrt/staging/linusw.git] /
1 From 387a26c56933fbfd0fa211ad5347d48d08695ea0 Mon Sep 17 00:00:00 2001
2 From: "Victoria Milhoan (b42089)" <vicki.milhoan@freescale.com>
3 Date: Fri, 17 Oct 2014 16:30:56 -0700
4 Subject: [PATCH] MLK-9769-8 crypto: caam - add a test for the RNG
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Freescale's CAAM includes a Random Number Generator. This change adds
10 a kernel configuration option to test the RNG's capabilities via the
11 hw_random framework.
12
13 Signed-off-by: Victoria Milhoan <vicki.milhoan@freescale.com>
14 Signed-off-by: Dan Douglass <dan.douglass@freescale.com>
15 Signed-off-by: Vipul Kumar <vipul_kumar@mentor.com>
16 (cherry picked from commit 05fba1bb857d5329fdcf79e736643fce0944a86d)
17
18 -fixed compilation warning:
19 drivers/crypto/caam/caamrng.c:271:2: warning: format '%d' expects argument of type 'int', but argument 2 has type 'size_t' [-Wformat=]
20
21 -changed commit headline
22
23 Signed-off-by: Horia Geantă <horia.geanta@nxp.com>
24 ---
25 drivers/crypto/caam/Kconfig | 8 ++++++++
26 drivers/crypto/caam/caamrng.c | 47 +++++++++++++++++++++++++++++++++++++++++++
27 2 files changed, 55 insertions(+)
28
29 --- a/drivers/crypto/caam/Kconfig
30 +++ b/drivers/crypto/caam/Kconfig
31 @@ -147,6 +147,14 @@ config CRYPTO_DEV_FSL_CAAM_RNG_API
32 Selecting this will register the SEC4 hardware rng to
33 the hw_random API for suppying the kernel entropy pool.
34
35 +config CRYPTO_DEV_FSL_CAAM_RNG_TEST
36 + bool "Test caam rng"
37 + depends on CRYPTO_DEV_FSL_CAAM_RNG_API
38 + help
39 + Selecting this will enable a self-test to run for the
40 + caam RNG. This test is several minutes long and executes
41 + just before the RNG is registered with the hw_random API.
42 +
43 endif # CRYPTO_DEV_FSL_CAAM_JR
44
45 endif # CRYPTO_DEV_FSL_CAAM
46 --- a/drivers/crypto/caam/caamrng.c
47 +++ b/drivers/crypto/caam/caamrng.c
48 @@ -258,6 +258,49 @@ static void caam_cleanup(struct hwrng *r
49 rng_unmap_ctx(rng_ctx);
50 }
51
52 +#ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_TEST
53 +static inline void test_len(struct hwrng *rng, size_t len, bool wait)
54 +{
55 + u8 *buf;
56 + int real_len;
57 +
58 + buf = kzalloc(sizeof(u8) * len, GFP_KERNEL);
59 + real_len = rng->read(rng, buf, len, wait);
60 + if (real_len == 0 && wait)
61 + pr_err("WAITING FAILED\n");
62 + pr_info("wanted %zu bytes, got %d\n", len, real_len);
63 + print_hex_dump(KERN_INFO, "random bytes@: ", DUMP_PREFIX_ADDRESS,
64 + 16, 4, buf, real_len, 1);
65 + kfree(buf);
66 +}
67 +
68 +static inline void test_mode_once(struct hwrng *rng, bool wait)
69 +{
70 +#define TEST_CHUNK (RN_BUF_SIZE / 4)
71 +
72 + test_len(rng, TEST_CHUNK, wait);
73 + test_len(rng, RN_BUF_SIZE * 2, wait);
74 + test_len(rng, RN_BUF_SIZE * 2 - TEST_CHUNK, wait);
75 +}
76 +
77 +static inline void test_mode(struct hwrng *rng, bool wait)
78 +{
79 +#define TEST_PASS 1
80 + int i;
81 +
82 + for (i = 0; i < TEST_PASS; i++)
83 + test_mode_once(rng, wait);
84 +}
85 +
86 +static void self_test(struct hwrng *rng)
87 +{
88 + pr_info("testing without waiting\n");
89 + test_mode(rng, false);
90 + pr_info("testing with waiting\n");
91 + test_mode(rng, true);
92 +}
93 +#endif
94 +
95 static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id)
96 {
97 struct buf_data *bd = &ctx->bufs[buf_id];
98 @@ -342,6 +385,10 @@ int caam_rng_init(struct device *ctrldev
99 if (err)
100 goto free_rng_ctx;
101
102 +#ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_TEST
103 + self_test(&caam_rng);
104 +#endif
105 +
106 dev_info(dev, "registering rng-caam\n");
107
108 err = hwrng_register(&caam_rng);