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 struct crypto_rng *crypto_default_rng;
28 EXPORT_SYMBOL_GPL(crypto_default_rng);
29 static int crypto_default_rng_refcnt;
30 
31 int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed, unsigned int slen)
32 {
33 	u8 *buf = NULL;
34 	int err;
35 
36 	if (!seed && slen) {
37 		buf = kmalloc(slen, GFP_KERNEL);
38 		if (!buf)
39 			return -ENOMEM;
40 
41 		err = get_random_bytes_wait(buf, slen);
42 		if (err)
43 			goto out;
44 		seed = buf;
45 	}
46 
47 	err = crypto_rng_alg(tfm)->seed(tfm, seed, slen);
48 out:
49 	kfree_sensitive(buf);
50 	return err;
51 }
52 EXPORT_SYMBOL_GPL(crypto_rng_reset);
53 
54 static int crypto_rng_init_tfm(struct crypto_tfm *tfm)
55 {
56 	return 0;
57 }
58 
59 static unsigned int seedsize(struct crypto_alg *alg)
60 {
61 	struct rng_alg *ralg = container_of(alg, struct rng_alg, base);
62 
63 	return ralg->seedsize;
64 }
65 
66 static int __maybe_unused crypto_rng_report(
67 	struct sk_buff *skb, struct crypto_alg *alg)
68 {
69 	struct crypto_report_rng rrng;
70 
71 	memset(&rrng, 0, sizeof(rrng));
72 
73 	strscpy(rrng.type, "rng", sizeof(rrng.type));
74 
75 	rrng.seedsize = seedsize(alg);
76 
77 	return nla_put(skb, CRYPTOCFGA_REPORT_RNG, sizeof(rrng), &rrng);
78 }
79 
80 static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
81 	__maybe_unused;
82 static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
83 {
84 	seq_printf(m, "type         : rng\n");
85 	seq_printf(m, "seedsize     : %u\n", seedsize(alg));
86 }
87 
88 static const struct crypto_type crypto_rng_type = {
89 	.extsize = crypto_alg_extsize,
90 	.init_tfm = crypto_rng_init_tfm,
91 #ifdef CONFIG_PROC_FS
92 	.show = crypto_rng_show,
93 #endif
94 #if IS_ENABLED(CONFIG_CRYPTO_USER)
95 	.report = crypto_rng_report,
96 #endif
97 	.maskclear = ~CRYPTO_ALG_TYPE_MASK,
98 	.maskset = CRYPTO_ALG_TYPE_MASK,
99 	.type = CRYPTO_ALG_TYPE_RNG,
100 	.tfmsize = offsetof(struct crypto_rng, base),
101 	.algsize = offsetof(struct rng_alg, base),
102 };
103 
104 struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask)
105 {
106 	return crypto_alloc_tfm(alg_name, &crypto_rng_type, type, mask);
107 }
108 EXPORT_SYMBOL_GPL(crypto_alloc_rng);
109 
110 int crypto_get_default_rng(void)
111 {
112 	struct crypto_rng *rng;
113 	int err;
114 
115 	mutex_lock(&crypto_default_rng_lock);
116 	if (!crypto_default_rng) {
117 		rng = crypto_alloc_rng("stdrng", 0, 0);
118 		err = PTR_ERR(rng);
119 		if (IS_ERR(rng))
120 			goto unlock;
121 
122 		err = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng));
123 		if (err) {
124 			crypto_free_rng(rng);
125 			goto unlock;
126 		}
127 
128 		crypto_default_rng = rng;
129 	}
130 
131 	crypto_default_rng_refcnt++;
132 	err = 0;
133 
134 unlock:
135 	mutex_unlock(&crypto_default_rng_lock);
136 
137 	return err;
138 }
139 EXPORT_SYMBOL_GPL(crypto_get_default_rng);
140 
141 void crypto_put_default_rng(void)
142 {
143 	mutex_lock(&crypto_default_rng_lock);
144 	crypto_default_rng_refcnt--;
145 	mutex_unlock(&crypto_default_rng_lock);
146 }
147 EXPORT_SYMBOL_GPL(crypto_put_default_rng);
148 
149 #if defined(CONFIG_CRYPTO_RNG) || defined(CONFIG_CRYPTO_RNG_MODULE)
150 int crypto_del_default_rng(void)
151 {
152 	int err = -EBUSY;
153 
154 	mutex_lock(&crypto_default_rng_lock);
155 	if (crypto_default_rng_refcnt)
156 		goto out;
157 
158 	crypto_free_rng(crypto_default_rng);
159 	crypto_default_rng = NULL;
160 
161 	err = 0;
162 
163 out:
164 	mutex_unlock(&crypto_default_rng_lock);
165 
166 	return err;
167 }
168 EXPORT_SYMBOL_GPL(crypto_del_default_rng);
169 #endif
170 
171 int crypto_register_rng(struct rng_alg *alg)
172 {
173 	struct crypto_alg *base = &alg->base;
174 
175 	if (alg->seedsize > PAGE_SIZE / 8)
176 		return -EINVAL;
177 
178 	base->cra_type = &crypto_rng_type;
179 	base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
180 	base->cra_flags |= CRYPTO_ALG_TYPE_RNG;
181 
182 	return crypto_register_alg(base);
183 }
184 EXPORT_SYMBOL_GPL(crypto_register_rng);
185 
186 void crypto_unregister_rng(struct rng_alg *alg)
187 {
188 	crypto_unregister_alg(&alg->base);
189 }
190 EXPORT_SYMBOL_GPL(crypto_unregister_rng);
191 
192 int crypto_register_rngs(struct rng_alg *algs, int count)
193 {
194 	int i, ret;
195 
196 	for (i = 0; i < count; i++) {
197 		ret = crypto_register_rng(algs + i);
198 		if (ret)
199 			goto err;
200 	}
201 
202 	return 0;
203 
204 err:
205 	for (--i; i >= 0; --i)
206 		crypto_unregister_rng(algs + i);
207 
208 	return ret;
209 }
210 EXPORT_SYMBOL_GPL(crypto_register_rngs);
211 
212 void crypto_unregister_rngs(struct rng_alg *algs, int count)
213 {
214 	int i;
215 
216 	for (i = count - 1; i >= 0; --i)
217 		crypto_unregister_rng(algs + i);
218 }
219 EXPORT_SYMBOL_GPL(crypto_unregister_rngs);
220 
221 MODULE_LICENSE("GPL");
222 MODULE_DESCRIPTION("Random Number Generator");
223