summaryrefslogtreecommitdiff
path: root/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-prng.c
blob: 729aafdbea84eada50c3964995c02352a4718ad4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "sun4i-ss.h"

int sun4i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed,
		       unsigned int slen)
{
	struct sun4i_ss_alg_template *algt;
	struct rng_alg *alg = crypto_rng_alg(tfm);

	algt = container_of(alg, struct sun4i_ss_alg_template, alg.rng);
	memcpy(algt->ss->seed, seed, slen);

	return 0;
}

int sun4i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
			   unsigned int slen, u8 *dst, unsigned int dlen)
{
	struct sun4i_ss_alg_template *algt;
	struct rng_alg *alg = crypto_rng_alg(tfm);
	int i, err;
	u32 v;
	u32 *data = (u32 *)dst;
	const u32 mode = SS_OP_PRNG | SS_PRNG_CONTINUE | SS_ENABLED;
	size_t len;
	struct sun4i_ss_ctx *ss;
	unsigned int todo = (dlen / 4) * 4;

	algt = container_of(alg, struct sun4i_ss_alg_template, alg.rng);
	ss = algt->ss;

	err = pm_runtime_get_sync(ss->dev);
	if (err < 0)
		return err;

	spin_lock_bh(&ss->slock);

	writel(mode, ss->base + SS_CTL);

	while (todo > 0) {
		/* write the seed */
		for (i = 0; i < SS_SEED_LEN / BITS_PER_LONG; i++)
			writel(ss->seed[i], ss->base + SS_KEY0 + i * 4);

		/* Read the random data */
		len = min_t(size_t, SS_DATA_LEN / BITS_PER_BYTE, todo);
		readsl(ss->base + SS_TXFIFO, data, len / 4);
		data += len / 4;
		todo -= len;

		/* Update the seed */
		for (i = 0; i < SS_SEED_LEN / BITS_PER_LONG; i++) {
			v = readl(ss->base + SS_KEY0 + i * 4);
			ss->seed[i] = v;
		}
	}

	writel(0, ss->base + SS_CTL);
	spin_unlock_bh(&ss->slock);

	pm_runtime_put(ss->dev);

	return 0;
}