6 * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
15 #include <linux/atomic.h>
16 #include <crypto/internal/rng.h>
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/random.h>
21 #include <linux/seq_file.h>
22 #include <linux/slab.h>
23 #include <linux/string.h>
24 #include <linux/cryptouser.h>
25 #include <net/netlink.h>
29 static DEFINE_MUTEX(crypto_default_rng_lock
);
30 struct crypto_rng
*crypto_default_rng
;
31 EXPORT_SYMBOL_GPL(crypto_default_rng
);
32 static int crypto_default_rng_refcnt
;
34 static inline struct crypto_rng
*__crypto_rng_cast(struct crypto_tfm
*tfm
)
36 return container_of(tfm
, struct crypto_rng
, base
);
39 static inline struct old_rng_alg
*crypto_old_rng_alg(struct crypto_rng
*tfm
)
41 return &crypto_rng_tfm(tfm
)->__crt_alg
->cra_rng
;
44 static int generate(struct crypto_rng
*tfm
, const u8
*src
, unsigned int slen
,
45 u8
*dst
, unsigned int dlen
)
47 return crypto_old_rng_alg(tfm
)->rng_make_random(tfm
, dst
, dlen
);
50 static int rngapi_reset(struct crypto_rng
*tfm
, const u8
*seed
,
58 buf
= kmalloc(slen
, GFP_KERNEL
);
62 memcpy(buf
, seed
, slen
);
66 err
= crypto_old_rng_alg(tfm
)->rng_reset(tfm
, src
, slen
);
72 int crypto_rng_reset(struct crypto_rng
*tfm
, const u8
*seed
, unsigned int slen
)
78 buf
= kmalloc(slen
, GFP_KERNEL
);
82 get_random_bytes(buf
, slen
);
86 err
= tfm
->seed(tfm
, seed
, slen
);
91 EXPORT_SYMBOL_GPL(crypto_rng_reset
);
93 static int crypto_rng_init_tfm(struct crypto_tfm
*tfm
)
95 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
);
99 if (oalg
->rng_make_random
) {
100 rng
->generate
= generate
;
101 rng
->seed
= rngapi_reset
;
102 rng
->seedsize
= oalg
->seedsize
;
106 rng
->generate
= alg
->generate
;
107 rng
->seed
= alg
->seed
;
108 rng
->seedsize
= alg
->seedsize
;
113 static unsigned int seedsize(struct crypto_alg
*alg
)
115 struct rng_alg
*ralg
= container_of(alg
, struct rng_alg
, base
);
117 return alg
->cra_rng
.rng_make_random
?
118 alg
->cra_rng
.seedsize
: ralg
->seedsize
;
122 static int crypto_rng_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
124 struct crypto_report_rng rrng
;
126 strncpy(rrng
.type
, "rng", sizeof(rrng
.type
));
128 rrng
.seedsize
= seedsize(alg
);
130 if (nla_put(skb
, CRYPTOCFGA_REPORT_RNG
,
131 sizeof(struct crypto_report_rng
), &rrng
))
132 goto nla_put_failure
;
139 static int crypto_rng_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
145 static void crypto_rng_show(struct seq_file
*m
, struct crypto_alg
*alg
)
146 __attribute__ ((unused
));
147 static void crypto_rng_show(struct seq_file
*m
, struct crypto_alg
*alg
)
149 seq_printf(m
, "type : rng\n");
150 seq_printf(m
, "seedsize : %u\n", seedsize(alg
));
153 const struct crypto_type crypto_rng_type
= {
154 .extsize
= crypto_alg_extsize
,
155 .init_tfm
= crypto_rng_init_tfm
,
156 #ifdef CONFIG_PROC_FS
157 .show
= crypto_rng_show
,
159 .report
= crypto_rng_report
,
160 .maskclear
= ~CRYPTO_ALG_TYPE_MASK
,
161 .maskset
= CRYPTO_ALG_TYPE_MASK
,
162 .type
= CRYPTO_ALG_TYPE_RNG
,
163 .tfmsize
= offsetof(struct crypto_rng
, base
),
165 EXPORT_SYMBOL_GPL(crypto_rng_type
);
167 struct crypto_rng
*crypto_alloc_rng(const char *alg_name
, u32 type
, u32 mask
)
169 return crypto_alloc_tfm(alg_name
, &crypto_rng_type
, type
, mask
);
171 EXPORT_SYMBOL_GPL(crypto_alloc_rng
);
173 int crypto_get_default_rng(void)
175 struct crypto_rng
*rng
;
178 mutex_lock(&crypto_default_rng_lock
);
179 if (!crypto_default_rng
) {
180 rng
= crypto_alloc_rng("stdrng", 0, 0);
185 err
= crypto_rng_reset(rng
, NULL
, crypto_rng_seedsize(rng
));
187 crypto_free_rng(rng
);
191 crypto_default_rng
= rng
;
194 crypto_default_rng_refcnt
++;
198 mutex_unlock(&crypto_default_rng_lock
);
202 EXPORT_SYMBOL_GPL(crypto_get_default_rng
);
204 void crypto_put_default_rng(void)
206 mutex_lock(&crypto_default_rng_lock
);
207 if (!--crypto_default_rng_refcnt
) {
208 crypto_free_rng(crypto_default_rng
);
209 crypto_default_rng
= NULL
;
211 mutex_unlock(&crypto_default_rng_lock
);
213 EXPORT_SYMBOL_GPL(crypto_put_default_rng
);
215 int crypto_register_rng(struct rng_alg
*alg
)
217 struct crypto_alg
*base
= &alg
->base
;
219 if (alg
->seedsize
> PAGE_SIZE
/ 8)
222 base
->cra_type
= &crypto_rng_type
;
223 base
->cra_flags
&= ~CRYPTO_ALG_TYPE_MASK
;
224 base
->cra_flags
|= CRYPTO_ALG_TYPE_RNG
;
226 return crypto_register_alg(base
);
228 EXPORT_SYMBOL_GPL(crypto_register_rng
);
230 void crypto_unregister_rng(struct rng_alg
*alg
)
232 crypto_unregister_alg(&alg
->base
);
234 EXPORT_SYMBOL_GPL(crypto_unregister_rng
);
236 MODULE_LICENSE("GPL");
237 MODULE_DESCRIPTION("Random Number Generator");