804-crypto-0014-MLK-9769-8-crypto-caam-add-a-test-for-the-RNG.patch 3.3 KB

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