Skip to content

Commit acec27f

Browse files
committed
crypto: rng - Convert low-level crypto_rng to new style
This patch converts the low-level crypto_rng interface to the "new" style. This allows existing implementations to be converted over one- by-one. Once that is complete we can then remove the old rng interface. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 3c5d8fa commit acec27f

4 files changed

Lines changed: 96 additions & 11 deletions

File tree

‎crypto/rng.c‎

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ static inline struct crypto_rng *__crypto_rng_cast(struct crypto_tfm *tfm)
3636
return container_of(tfm, struct crypto_rng, base);
3737
}
3838

39+
static inline struct old_rng_alg *crypto_old_rng_alg(struct crypto_rng *tfm)
40+
{
41+
return &crypto_rng_tfm(tfm)->__crt_alg->cra_rng;
42+
}
43+
3944
static int generate(struct crypto_rng *tfm, const u8 *src, unsigned int slen,
4045
u8 *dst, unsigned int dlen)
4146
{
42-
return crypto_rng_alg(tfm)->rng_make_random(tfm, dst, dlen);
47+
return crypto_old_rng_alg(tfm)->rng_make_random(tfm, dst, dlen);
4348
}
4449

4550
static int rngapi_reset(struct crypto_rng *tfm, const u8 *seed,
@@ -58,7 +63,7 @@ static int rngapi_reset(struct crypto_rng *tfm, const u8 *seed,
5863
src = buf;
5964
}
6065

61-
err = crypto_rng_alg(tfm)->rng_reset(tfm, src, slen);
66+
err = crypto_old_rng_alg(tfm)->rng_reset(tfm, src, slen);
6267

6368
kzfree(buf);
6469
return err;
@@ -88,21 +93,39 @@ EXPORT_SYMBOL_GPL(crypto_rng_reset);
8893
static int crypto_rng_init_tfm(struct crypto_tfm *tfm)
8994
{
9095
struct crypto_rng *rng = __crypto_rng_cast(tfm);
96+
struct rng_alg *alg = crypto_rng_alg(rng);
97+
struct old_rng_alg *oalg = crypto_old_rng_alg(rng);
98+
99+
if (oalg->rng_make_random) {
100+
rng->generate = generate;
101+
rng->seed = rngapi_reset;
102+
rng->seedsize = oalg->seedsize;
103+
return 0;
104+
}
91105

92-
rng->generate = generate;
93-
rng->seed = rngapi_reset;
106+
rng->generate = alg->generate;
107+
rng->seed = alg->seed;
108+
rng->seedsize = alg->seedsize;
94109

95110
return 0;
96111
}
97112

113+
static unsigned int seedsize(struct crypto_alg *alg)
114+
{
115+
struct rng_alg *ralg = container_of(alg, struct rng_alg, base);
116+
117+
return alg->cra_rng.rng_make_random ?
118+
alg->cra_rng.seedsize : ralg->seedsize;
119+
}
120+
98121
#ifdef CONFIG_NET
99122
static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
100123
{
101124
struct crypto_report_rng rrng;
102125

103126
strncpy(rrng.type, "rng", sizeof(rrng.type));
104127

105-
rrng.seedsize = alg->cra_rng.seedsize;
128+
rrng.seedsize = seedsize(alg);
106129

107130
if (nla_put(skb, CRYPTOCFGA_REPORT_RNG,
108131
sizeof(struct crypto_report_rng), &rrng))
@@ -124,7 +147,7 @@ static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
124147
static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
125148
{
126149
seq_printf(m, "type : rng\n");
127-
seq_printf(m, "seedsize : %u\n", alg->cra_rng.seedsize);
150+
seq_printf(m, "seedsize : %u\n", seedsize(alg));
128151
}
129152

130153
const struct crypto_type crypto_rng_type = {
@@ -189,5 +212,26 @@ void crypto_put_default_rng(void)
189212
}
190213
EXPORT_SYMBOL_GPL(crypto_put_default_rng);
191214

215+
int crypto_register_rng(struct rng_alg *alg)
216+
{
217+
struct crypto_alg *base = &alg->base;
218+
219+
if (alg->seedsize > PAGE_SIZE / 8)
220+
return -EINVAL;
221+
222+
base->cra_type = &crypto_rng_type;
223+
base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
224+
base->cra_flags |= CRYPTO_ALG_TYPE_RNG;
225+
226+
return crypto_register_alg(base);
227+
}
228+
EXPORT_SYMBOL_GPL(crypto_register_rng);
229+
230+
void crypto_unregister_rng(struct rng_alg *alg)
231+
{
232+
crypto_unregister_alg(&alg->base);
233+
}
234+
EXPORT_SYMBOL_GPL(crypto_unregister_rng);
235+
192236
MODULE_LICENSE("GPL");
193237
MODULE_DESCRIPTION("Random Number Generator");

‎include/crypto/internal/rng.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
extern const struct crypto_type crypto_rng_type;
2020

21+
int crypto_register_rng(struct rng_alg *alg);
22+
void crypto_unregister_rng(struct rng_alg *alg);
23+
2124
static inline void *crypto_rng_ctx(struct crypto_rng *tfm)
2225
{
2326
return crypto_tfm_ctx(&tfm->base);

‎include/crypto/rng.h‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,48 @@
1515

1616
#include <linux/crypto.h>
1717

18+
struct crypto_rng;
19+
20+
/**
21+
* struct rng_alg - random number generator definition
22+
*
23+
* @generate: The function defined by this variable obtains a
24+
* random number. The random number generator transform
25+
* must generate the random number out of the context
26+
* provided with this call, plus any additional data
27+
* if provided to the call.
28+
* @seed: Seed or reseed the random number generator. With the
29+
* invocation of this function call, the random number
30+
* generator shall become ready fo generation. If the
31+
* random number generator requires a seed for setting
32+
* up a new state, the seed must be provided by the
33+
* consumer while invoking this function. The required
34+
* size of the seed is defined with @seedsize .
35+
* @seedsize: The seed size required for a random number generator
36+
* initialization defined with this variable. Some
37+
* random number generators does not require a seed
38+
* as the seeding is implemented internally without
39+
* the need of support by the consumer. In this case,
40+
* the seed size is set to zero.
41+
* @base: Common crypto API algorithm data structure.
42+
*/
43+
struct rng_alg {
44+
int (*generate)(struct crypto_rng *tfm,
45+
const u8 *src, unsigned int slen,
46+
u8 *dst, unsigned int dlen);
47+
int (*seed)(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
48+
49+
unsigned int seedsize;
50+
51+
struct crypto_alg base;
52+
};
53+
1854
struct crypto_rng {
1955
int (*generate)(struct crypto_rng *tfm,
2056
const u8 *src, unsigned int slen,
2157
u8 *dst, unsigned int dlen);
2258
int (*seed)(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
59+
unsigned int seedsize;
2360
struct crypto_tfm base;
2461
};
2562

@@ -72,7 +109,8 @@ static inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)
72109
*/
73110
static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
74111
{
75-
return &crypto_rng_tfm(tfm)->__crt_alg->cra_rng;
112+
return container_of(crypto_rng_tfm(tfm)->__crt_alg,
113+
struct rng_alg, base);
76114
}
77115

78116
/**
@@ -156,7 +194,7 @@ int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed,
156194
*/
157195
static inline int crypto_rng_seedsize(struct crypto_rng *tfm)
158196
{
159-
return crypto_rng_alg(tfm)->seedsize;
197+
return tfm->seedsize;
160198
}
161199

162200
#endif

‎include/linux/crypto.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ struct compress_alg {
427427
};
428428

429429
/**
430-
* struct rng_alg - random number generator definition
430+
* struct old_rng_alg - random number generator definition
431431
* @rng_make_random: The function defined by this variable obtains a random
432432
* number. The random number generator transform must generate
433433
* the random number out of the context provided with this
@@ -445,7 +445,7 @@ struct compress_alg {
445445
* seeding is implemented internally without the need of support by
446446
* the consumer. In this case, the seed size is set to zero.
447447
*/
448-
struct rng_alg {
448+
struct old_rng_alg {
449449
int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
450450
unsigned int dlen);
451451
int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
@@ -559,7 +559,7 @@ struct crypto_alg {
559559
struct blkcipher_alg blkcipher;
560560
struct cipher_alg cipher;
561561
struct compress_alg compress;
562-
struct rng_alg rng;
562+
struct old_rng_alg rng;
563563
} cra_u;
564564

565565
int (*cra_init)(struct crypto_tfm *tfm);

0 commit comments

Comments
 (0)