sun4i-ss-prng.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "sun4i-ss.h"
  2. int sun4i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed,
  3. unsigned int slen)
  4. {
  5. struct sun4i_ss_alg_template *algt;
  6. struct rng_alg *alg = crypto_rng_alg(tfm);
  7. algt = container_of(alg, struct sun4i_ss_alg_template, alg.rng);
  8. memcpy(algt->ss->seed, seed, slen);
  9. return 0;
  10. }
  11. int sun4i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
  12. unsigned int slen, u8 *dst, unsigned int dlen)
  13. {
  14. struct sun4i_ss_alg_template *algt;
  15. struct rng_alg *alg = crypto_rng_alg(tfm);
  16. int i;
  17. u32 v;
  18. u32 *data = (u32 *)dst;
  19. const u32 mode = SS_OP_PRNG | SS_PRNG_CONTINUE | SS_ENABLED;
  20. size_t len;
  21. struct sun4i_ss_ctx *ss;
  22. unsigned int todo = (dlen / 4) * 4;
  23. algt = container_of(alg, struct sun4i_ss_alg_template, alg.rng);
  24. ss = algt->ss;
  25. spin_lock_bh(&ss->slock);
  26. writel(mode, ss->base + SS_CTL);
  27. while (todo > 0) {
  28. /* write the seed */
  29. for (i = 0; i < SS_SEED_LEN / BITS_PER_LONG; i++)
  30. writel(ss->seed[i], ss->base + SS_KEY0 + i * 4);
  31. /* Read the random data */
  32. len = min_t(size_t, SS_DATA_LEN / BITS_PER_BYTE, todo);
  33. readsl(ss->base + SS_TXFIFO, data, len / 4);
  34. data += len / 4;
  35. todo -= len;
  36. /* Update the seed */
  37. for (i = 0; i < SS_SEED_LEN / BITS_PER_LONG; i++) {
  38. v = readl(ss->base + SS_KEY0 + i * 4);
  39. ss->seed[i] = v;
  40. }
  41. }
  42. writel(0, ss->base + SS_CTL);
  43. spin_unlock_bh(&ss->slock);
  44. return 0;
  45. }