blob: 63d636424161dccfbb5cdd12fd34b5db8e1915d1 [file] [log] [blame]
Corentin LABBEb8ae5c72017-07-03 20:48:48 +02001#include "sun4i-ss.h"
2
3int sun4i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed,
4 unsigned int slen)
5{
6 struct sun4i_ss_alg_template *algt;
7 struct rng_alg *alg = crypto_rng_alg(tfm);
8
9 algt = container_of(alg, struct sun4i_ss_alg_template, alg.rng);
10 memcpy(algt->ss->seed, seed, slen);
11
12 return 0;
13}
14
15int sun4i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
16 unsigned int slen, u8 *dst, unsigned int dlen)
17{
18 struct sun4i_ss_alg_template *algt;
19 struct rng_alg *alg = crypto_rng_alg(tfm);
20 int i;
21 u32 v;
22 u32 *data = (u32 *)dst;
23 const u32 mode = SS_OP_PRNG | SS_PRNG_CONTINUE | SS_ENABLED;
24 size_t len;
25 struct sun4i_ss_ctx *ss;
26 unsigned int todo = (dlen / 4) * 4;
27
28 algt = container_of(alg, struct sun4i_ss_alg_template, alg.rng);
29 ss = algt->ss;
30
Artem Savkov2e7d1d62018-02-06 22:20:22 +010031 spin_lock_bh(&ss->slock);
Corentin LABBEb8ae5c72017-07-03 20:48:48 +020032
33 writel(mode, ss->base + SS_CTL);
34
35 while (todo > 0) {
36 /* write the seed */
37 for (i = 0; i < SS_SEED_LEN / BITS_PER_LONG; i++)
38 writel(ss->seed[i], ss->base + SS_KEY0 + i * 4);
39
40 /* Read the random data */
41 len = min_t(size_t, SS_DATA_LEN / BITS_PER_BYTE, todo);
42 readsl(ss->base + SS_TXFIFO, data, len / 4);
43 data += len / 4;
44 todo -= len;
45
46 /* Update the seed */
47 for (i = 0; i < SS_SEED_LEN / BITS_PER_LONG; i++) {
48 v = readl(ss->base + SS_KEY0 + i * 4);
49 ss->seed[i] = v;
50 }
51 }
52
53 writel(0, ss->base + SS_CTL);
Artem Savkov2e7d1d62018-02-06 22:20:22 +010054 spin_unlock_bh(&ss->slock);
Artem Savkovdd78c832018-02-06 22:20:21 +010055 return 0;
Corentin LABBEb8ae5c72017-07-03 20:48:48 +020056}