Skip to content

Commit b8ae5c7

Browse files
montjoieherbertx
authored andcommitted
crypto: sun4i-ss - support the Security System PRNG
The Security System has a PRNG, this patch adds support for it via crypto_rng. Signed-off-by: Corentin Labbe <clabbe.montjoie@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 3822c33 commit b8ae5c7

5 files changed

Lines changed: 106 additions & 0 deletions

File tree

‎drivers/crypto/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,14 @@ config CRYPTO_DEV_SUN4I_SS
616616
To compile this driver as a module, choose M here: the module
617617
will be called sun4i-ss.
618618

619+
config CRYPTO_DEV_SUN4I_SS_PRNG
620+
bool "Support for Allwinner Security System PRNG"
621+
depends on CRYPTO_DEV_SUN4I_SS
622+
select CRYPTO_RNG
623+
help
624+
Select this option if you want to provide kernel-side support for
625+
the Pseudo-Random Number Generator found in the Security System.
626+
619627
config CRYPTO_DEV_ROCKCHIP
620628
tristate "Rockchip's Cryptographic Engine driver"
621629
depends on OF && ARCH_ROCKCHIP

‎drivers/crypto/sunxi-ss/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
obj-$(CONFIG_CRYPTO_DEV_SUN4I_SS) += sun4i-ss.o
22
sun4i-ss-y += sun4i-ss-core.o sun4i-ss-hash.o sun4i-ss-cipher.o
3+
sun4i-ss-$(CONFIG_CRYPTO_DEV_SUN4I_SS_PRNG) += sun4i-ss-prng.o

‎drivers/crypto/sunxi-ss/sun4i-ss-core.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,23 @@ static struct sun4i_ss_alg_template ss_algs[] = {
213213
}
214214
}
215215
},
216+
#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_PRNG
217+
{
218+
.type = CRYPTO_ALG_TYPE_RNG,
219+
.alg.rng = {
220+
.base = {
221+
.cra_name = "stdrng",
222+
.cra_driver_name = "sun4i_ss_rng",
223+
.cra_priority = 300,
224+
.cra_ctxsize = 0,
225+
.cra_module = THIS_MODULE,
226+
},
227+
.generate = sun4i_ss_prng_generate,
228+
.seed = sun4i_ss_prng_seed,
229+
.seedsize = SS_SEED_LEN / BITS_PER_BYTE,
230+
}
231+
},
232+
#endif
216233
};
217234

218235
static int sun4i_ss_probe(struct platform_device *pdev)
@@ -355,6 +372,13 @@ static int sun4i_ss_probe(struct platform_device *pdev)
355372
goto error_alg;
356373
}
357374
break;
375+
case CRYPTO_ALG_TYPE_RNG:
376+
err = crypto_register_rng(&ss_algs[i].alg.rng);
377+
if (err) {
378+
dev_err(ss->dev, "Fail to register %s\n",
379+
ss_algs[i].alg.rng.base.cra_name);
380+
}
381+
break;
358382
}
359383
}
360384
platform_set_drvdata(pdev, ss);
@@ -369,6 +393,9 @@ static int sun4i_ss_probe(struct platform_device *pdev)
369393
case CRYPTO_ALG_TYPE_AHASH:
370394
crypto_unregister_ahash(&ss_algs[i].alg.hash);
371395
break;
396+
case CRYPTO_ALG_TYPE_RNG:
397+
crypto_unregister_rng(&ss_algs[i].alg.rng);
398+
break;
372399
}
373400
}
374401
if (ss->reset)
@@ -393,6 +420,9 @@ static int sun4i_ss_remove(struct platform_device *pdev)
393420
case CRYPTO_ALG_TYPE_AHASH:
394421
crypto_unregister_ahash(&ss_algs[i].alg.hash);
395422
break;
423+
case CRYPTO_ALG_TYPE_RNG:
424+
crypto_unregister_rng(&ss_algs[i].alg.rng);
425+
break;
396426
}
397427
}
398428

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "sun4i-ss.h"
2+
3+
int 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+
15+
int 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+
31+
spin_lock(&ss->slock);
32+
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);
54+
spin_unlock(&ss->slock);
55+
return dlen;
56+
}

‎drivers/crypto/sunxi-ss/sun4i-ss.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <crypto/aes.h>
3333
#include <crypto/des.h>
3434
#include <crypto/internal/rng.h>
35+
#include <crypto/rng.h>
3536

3637
#define SS_CTL 0x00
3738
#define SS_KEY0 0x04
@@ -127,6 +128,9 @@
127128
#define SS_RXFIFO_EMP_INT_ENABLE (1 << 2)
128129
#define SS_TXFIFO_AVA_INT_ENABLE (1 << 0)
129130

131+
#define SS_SEED_LEN 192
132+
#define SS_DATA_LEN 160
133+
130134
struct sun4i_ss_ctx {
131135
void __iomem *base;
132136
int irq;
@@ -136,6 +140,9 @@ struct sun4i_ss_ctx {
136140
struct device *dev;
137141
struct resource *res;
138142
spinlock_t slock; /* control the use of the device */
143+
#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_PRNG
144+
u32 seed[SS_SEED_LEN / BITS_PER_LONG];
145+
#endif
139146
};
140147

141148
struct sun4i_ss_alg_template {
@@ -144,6 +151,7 @@ struct sun4i_ss_alg_template {
144151
union {
145152
struct skcipher_alg crypto;
146153
struct ahash_alg hash;
154+
struct rng_alg rng;
147155
} alg;
148156
struct sun4i_ss_ctx *ss;
149157
};
@@ -201,3 +209,6 @@ int sun4i_ss_des_setkey(struct crypto_skcipher *tfm, const u8 *key,
201209
unsigned int keylen);
202210
int sun4i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
203211
unsigned int keylen);
212+
int sun4i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
213+
unsigned int slen, u8 *dst, unsigned int dlen);
214+
int sun4i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);

0 commit comments

Comments
 (0)