xref: /linux/crypto/rng.c (revision aec2f682d47c54ef434b2d440992626d80b1ebdc)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Cryptographic API.
4  *
5  * RNG operations.
6  *
7  * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
8  * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
9  */
10 
11 #include <crypto/internal/rng.h>
12 #include <linux/atomic.h>
13 #include <linux/cryptouser.h>
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/random.h>
19 #include <linux/seq_file.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <net/netlink.h>
23 
24 #include "internal.h"
25 
26 static DEFINE_MUTEX(crypto_default_rng_lock);
27 static struct crypto_rng *crypto_default_rng;
28 static int crypto_default_rng_refcnt;
29 
crypto_rng_reset(struct crypto_rng * tfm,const u8 * seed,unsigned int slen)30 int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed, unsigned int slen)
31 {
32 	u8 *buf = NULL;
33 	int err;
34 
35 	if (!seed && slen) {
36 		buf = kmalloc(slen, GFP_KERNEL);
37 		if (!buf)
38 			return -ENOMEM;
39 
40 		err = get_random_bytes_wait(buf, slen);
41 		if (err)
42 			goto out;
43 		seed = buf;
44 	}
45 
46 	err = crypto_rng_alg(tfm)->seed(tfm, seed, slen);
47 out:
48 	kfree_sensitive(buf);
49 	return err;
50 }
51 EXPORT_SYMBOL_GPL(crypto_rng_reset);
52 
crypto_rng_init_tfm(struct crypto_tfm * tfm)53 static int crypto_rng_init_tfm(struct crypto_tfm *tfm)
54 {
55 	return 0;
56 }
57 
seedsize(struct crypto_alg * alg)58 static unsigned int seedsize(struct crypto_alg *alg)
59 {
60 	struct rng_alg *ralg = container_of(alg, struct rng_alg, base);
61 
62 	return ralg->seedsize;
63 }
64 
crypto_rng_report(struct sk_buff * skb,struct crypto_alg * alg)65 static int __maybe_unused crypto_rng_report(
66 	struct sk_buff *skb, struct crypto_alg *alg)
67 {
68 	struct crypto_report_rng rrng;
69 
70 	memset(&rrng, 0, sizeof(rrng));
71 
72 	strscpy(rrng.type, "rng", sizeof(rrng.type));
73 
74 	rrng.seedsize = seedsize(alg);
75 
76 	return nla_put(skb, CRYPTOCFGA_REPORT_RNG, sizeof(rrng), &rrng);
77 }
78 
crypto_rng_show(struct seq_file * m,struct crypto_alg * alg)79 static void __maybe_unused crypto_rng_show(struct seq_file *m,
80 					   struct crypto_alg *alg)
81 {
82 	seq_printf(m, "type         : rng\n");
83 	seq_printf(m, "seedsize     : %u\n", seedsize(alg));
84 }
85 
86 static const struct crypto_type crypto_rng_type = {
87 	.extsize = crypto_alg_extsize,
88 	.init_tfm = crypto_rng_init_tfm,
89 #ifdef CONFIG_PROC_FS
90 	.show = crypto_rng_show,
91 #endif
92 #if IS_ENABLED(CONFIG_CRYPTO_USER)
93 	.report = crypto_rng_report,
94 #endif
95 	.maskclear = ~CRYPTO_ALG_TYPE_MASK,
96 	.maskset = CRYPTO_ALG_TYPE_MASK,
97 	.type = CRYPTO_ALG_TYPE_RNG,
98 	.tfmsize = offsetof(struct crypto_rng, base),
99 	.algsize = offsetof(struct rng_alg, base),
100 };
101 
crypto_alloc_rng(const char * alg_name,u32 type,u32 mask)102 struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask)
103 {
104 	return crypto_alloc_tfm(alg_name, &crypto_rng_type, type, mask);
105 }
106 EXPORT_SYMBOL_GPL(crypto_alloc_rng);
107 
crypto_get_default_rng(void)108 static int crypto_get_default_rng(void)
109 {
110 	struct crypto_rng *rng;
111 	int err;
112 
113 	mutex_lock(&crypto_default_rng_lock);
114 	if (!crypto_default_rng) {
115 		rng = crypto_alloc_rng("stdrng", 0, 0);
116 		err = PTR_ERR(rng);
117 		if (IS_ERR(rng))
118 			goto unlock;
119 
120 		err = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng));
121 		if (err) {
122 			crypto_free_rng(rng);
123 			goto unlock;
124 		}
125 
126 		crypto_default_rng = rng;
127 	}
128 
129 	crypto_default_rng_refcnt++;
130 	err = 0;
131 
132 unlock:
133 	mutex_unlock(&crypto_default_rng_lock);
134 
135 	return err;
136 }
137 
crypto_put_default_rng(void)138 static void crypto_put_default_rng(void)
139 {
140 	mutex_lock(&crypto_default_rng_lock);
141 	crypto_default_rng_refcnt--;
142 	mutex_unlock(&crypto_default_rng_lock);
143 }
144 
__crypto_stdrng_get_bytes(void * buf,unsigned int len)145 int __crypto_stdrng_get_bytes(void *buf, unsigned int len)
146 {
147 	int err;
148 
149 	err = crypto_get_default_rng();
150 	if (err)
151 		return err;
152 
153 	err = crypto_rng_get_bytes(crypto_default_rng, buf, len);
154 	crypto_put_default_rng();
155 	return err;
156 }
157 EXPORT_SYMBOL_GPL(__crypto_stdrng_get_bytes);
158 
159 #if defined(CONFIG_CRYPTO_RNG) || defined(CONFIG_CRYPTO_RNG_MODULE)
crypto_del_default_rng(void)160 int crypto_del_default_rng(void)
161 {
162 	int err = -EBUSY;
163 
164 	mutex_lock(&crypto_default_rng_lock);
165 	if (crypto_default_rng_refcnt)
166 		goto out;
167 
168 	crypto_free_rng(crypto_default_rng);
169 	crypto_default_rng = NULL;
170 
171 	err = 0;
172 
173 out:
174 	mutex_unlock(&crypto_default_rng_lock);
175 
176 	return err;
177 }
178 EXPORT_SYMBOL_GPL(crypto_del_default_rng);
179 #endif
180 
rng_default_set_ent(struct crypto_rng * tfm,const u8 * data,unsigned int len)181 static void rng_default_set_ent(struct crypto_rng *tfm, const u8 *data,
182 				unsigned int len)
183 {
184 }
185 
crypto_register_rng(struct rng_alg * alg)186 int crypto_register_rng(struct rng_alg *alg)
187 {
188 	struct crypto_alg *base = &alg->base;
189 
190 	if (alg->seedsize > PAGE_SIZE / 8)
191 		return -EINVAL;
192 
193 	base->cra_type = &crypto_rng_type;
194 	base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
195 	base->cra_flags |= CRYPTO_ALG_TYPE_RNG;
196 
197 	if (!alg->set_ent)
198 		alg->set_ent = rng_default_set_ent;
199 
200 	return crypto_register_alg(base);
201 }
202 EXPORT_SYMBOL_GPL(crypto_register_rng);
203 
crypto_unregister_rng(struct rng_alg * alg)204 void crypto_unregister_rng(struct rng_alg *alg)
205 {
206 	crypto_unregister_alg(&alg->base);
207 }
208 EXPORT_SYMBOL_GPL(crypto_unregister_rng);
209 
crypto_register_rngs(struct rng_alg * algs,int count)210 int crypto_register_rngs(struct rng_alg *algs, int count)
211 {
212 	int i, ret;
213 
214 	for (i = 0; i < count; i++) {
215 		ret = crypto_register_rng(algs + i);
216 		if (ret) {
217 			crypto_unregister_rngs(algs, i);
218 			return ret;
219 		}
220 	}
221 
222 	return 0;
223 }
224 EXPORT_SYMBOL_GPL(crypto_register_rngs);
225 
crypto_unregister_rngs(struct rng_alg * algs,int count)226 void crypto_unregister_rngs(struct rng_alg *algs, int count)
227 {
228 	int i;
229 
230 	for (i = count - 1; i >= 0; --i)
231 		crypto_unregister_rng(algs + i);
232 }
233 EXPORT_SYMBOL_GPL(crypto_unregister_rngs);
234 
235 MODULE_LICENSE("GPL");
236 MODULE_DESCRIPTION("Random Number Generator");
237