12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
217f0f4a4SNeil Horman /*
317f0f4a4SNeil Horman * Cryptographic API.
417f0f4a4SNeil Horman *
517f0f4a4SNeil Horman * RNG operations.
617f0f4a4SNeil Horman *
717f0f4a4SNeil Horman * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
894f1bb15SHerbert Xu * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
917f0f4a4SNeil Horman */
1017f0f4a4SNeil Horman
1117f0f4a4SNeil Horman #include <crypto/internal/rng.h>
129807e49bSHerbert Xu #include <linux/atomic.h>
139807e49bSHerbert Xu #include <linux/cryptouser.h>
1417f0f4a4SNeil Horman #include <linux/err.h>
159807e49bSHerbert Xu #include <linux/kernel.h>
1617f0f4a4SNeil Horman #include <linux/module.h>
1717f0f4a4SNeil Horman #include <linux/mutex.h>
1817f0f4a4SNeil Horman #include <linux/random.h>
1917f0f4a4SNeil Horman #include <linux/seq_file.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
2117f0f4a4SNeil Horman #include <linux/string.h>
22792608e9SSteffen Klassert #include <net/netlink.h>
2317f0f4a4SNeil Horman
24d0e83059SHerbert Xu #include "internal.h"
25d0e83059SHerbert Xu
2617f0f4a4SNeil Horman static DEFINE_MUTEX(crypto_default_rng_lock);
2717f0f4a4SNeil Horman struct crypto_rng *crypto_default_rng;
2817f0f4a4SNeil Horman EXPORT_SYMBOL_GPL(crypto_default_rng);
2917f0f4a4SNeil Horman static int crypto_default_rng_refcnt;
3017f0f4a4SNeil Horman
crypto_rng_reset(struct crypto_rng * tfm,const u8 * seed,unsigned int slen)313c5d8fa9SHerbert Xu int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed, unsigned int slen)
3217f0f4a4SNeil Horman {
339807e49bSHerbert Xu struct rng_alg *alg = crypto_rng_alg(tfm);
3417f0f4a4SNeil Horman u8 *buf = NULL;
3517f0f4a4SNeil Horman int err;
3617f0f4a4SNeil Horman
379807e49bSHerbert Xu if (IS_ENABLED(CONFIG_CRYPTO_STATS))
389807e49bSHerbert Xu atomic64_inc(&rng_get_stat(alg)->seed_cnt);
399807e49bSHerbert Xu
4017f0f4a4SNeil Horman if (!seed && slen) {
4117f0f4a4SNeil Horman buf = kmalloc(slen, GFP_KERNEL);
429807e49bSHerbert Xu err = -ENOMEM;
4330d0f6a9SEric Biggers if (!buf)
449807e49bSHerbert Xu goto out;
4517f0f4a4SNeil Horman
46c2176f00SJason A. Donenfeld err = get_random_bytes_wait(buf, slen);
4730d0f6a9SEric Biggers if (err)
489807e49bSHerbert Xu goto free_buf;
4917f0f4a4SNeil Horman seed = buf;
5017f0f4a4SNeil Horman }
5117f0f4a4SNeil Horman
529807e49bSHerbert Xu err = alg->seed(tfm, seed, slen);
539807e49bSHerbert Xu free_buf:
54453431a5SWaiman Long kfree_sensitive(buf);
559807e49bSHerbert Xu out:
569807e49bSHerbert Xu return crypto_rng_errstat(alg, err);
5717f0f4a4SNeil Horman }
583c5d8fa9SHerbert Xu EXPORT_SYMBOL_GPL(crypto_rng_reset);
5917f0f4a4SNeil Horman
crypto_rng_init_tfm(struct crypto_tfm * tfm)60d0e83059SHerbert Xu static int crypto_rng_init_tfm(struct crypto_tfm *tfm)
6117f0f4a4SNeil Horman {
6217f0f4a4SNeil Horman return 0;
6317f0f4a4SNeil Horman }
6417f0f4a4SNeil Horman
seedsize(struct crypto_alg * alg)65acec27ffSHerbert Xu static unsigned int seedsize(struct crypto_alg *alg)
66acec27ffSHerbert Xu {
67acec27ffSHerbert Xu struct rng_alg *ralg = container_of(alg, struct rng_alg, base);
68acec27ffSHerbert Xu
6994f1bb15SHerbert Xu return ralg->seedsize;
70acec27ffSHerbert Xu }
71acec27ffSHerbert Xu
crypto_rng_report(struct sk_buff * skb,struct crypto_alg * alg)72c0f9e01dSHerbert Xu static int __maybe_unused crypto_rng_report(
73c0f9e01dSHerbert Xu struct sk_buff *skb, struct crypto_alg *alg)
74792608e9SSteffen Klassert {
75792608e9SSteffen Klassert struct crypto_report_rng rrng;
76792608e9SSteffen Klassert
7737db69e0SEric Biggers memset(&rrng, 0, sizeof(rrng));
7837db69e0SEric Biggers
7937db69e0SEric Biggers strscpy(rrng.type, "rng", sizeof(rrng.type));
80792608e9SSteffen Klassert
81acec27ffSHerbert Xu rrng.seedsize = seedsize(alg);
82792608e9SSteffen Klassert
8337db69e0SEric Biggers return nla_put(skb, CRYPTOCFGA_REPORT_RNG, sizeof(rrng), &rrng);
84792608e9SSteffen Klassert }
85792608e9SSteffen Klassert
8617f0f4a4SNeil Horman static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
87d8c34b94SGideon Israel Dsouza __maybe_unused;
crypto_rng_show(struct seq_file * m,struct crypto_alg * alg)8817f0f4a4SNeil Horman static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
8917f0f4a4SNeil Horman {
9017f0f4a4SNeil Horman seq_printf(m, "type : rng\n");
91acec27ffSHerbert Xu seq_printf(m, "seedsize : %u\n", seedsize(alg));
9217f0f4a4SNeil Horman }
9317f0f4a4SNeil Horman
crypto_rng_report_stat(struct sk_buff * skb,struct crypto_alg * alg)949807e49bSHerbert Xu static int __maybe_unused crypto_rng_report_stat(
959807e49bSHerbert Xu struct sk_buff *skb, struct crypto_alg *alg)
969807e49bSHerbert Xu {
979807e49bSHerbert Xu struct rng_alg *rng = __crypto_rng_alg(alg);
989807e49bSHerbert Xu struct crypto_istat_rng *istat;
999807e49bSHerbert Xu struct crypto_stat_rng rrng;
1009807e49bSHerbert Xu
1019807e49bSHerbert Xu istat = rng_get_stat(rng);
1029807e49bSHerbert Xu
1039807e49bSHerbert Xu memset(&rrng, 0, sizeof(rrng));
1049807e49bSHerbert Xu
1059807e49bSHerbert Xu strscpy(rrng.type, "rng", sizeof(rrng.type));
1069807e49bSHerbert Xu
1079807e49bSHerbert Xu rrng.stat_generate_cnt = atomic64_read(&istat->generate_cnt);
1089807e49bSHerbert Xu rrng.stat_generate_tlen = atomic64_read(&istat->generate_tlen);
1099807e49bSHerbert Xu rrng.stat_seed_cnt = atomic64_read(&istat->seed_cnt);
1109807e49bSHerbert Xu rrng.stat_err_cnt = atomic64_read(&istat->err_cnt);
1119807e49bSHerbert Xu
1129807e49bSHerbert Xu return nla_put(skb, CRYPTOCFGA_STAT_RNG, sizeof(rrng), &rrng);
1139807e49bSHerbert Xu }
1149807e49bSHerbert Xu
11594f1bb15SHerbert Xu static const struct crypto_type crypto_rng_type = {
116d0e83059SHerbert Xu .extsize = crypto_alg_extsize,
117d0e83059SHerbert Xu .init_tfm = crypto_rng_init_tfm,
11817f0f4a4SNeil Horman #ifdef CONFIG_PROC_FS
11917f0f4a4SNeil Horman .show = crypto_rng_show,
12017f0f4a4SNeil Horman #endif
121*b8969a1bSOndrej Mosnacek #if IS_ENABLED(CONFIG_CRYPTO_USER)
122792608e9SSteffen Klassert .report = crypto_rng_report,
123c0f9e01dSHerbert Xu #endif
1249807e49bSHerbert Xu #ifdef CONFIG_CRYPTO_STATS
1259807e49bSHerbert Xu .report_stat = crypto_rng_report_stat,
1269807e49bSHerbert Xu #endif
127d0e83059SHerbert Xu .maskclear = ~CRYPTO_ALG_TYPE_MASK,
128d0e83059SHerbert Xu .maskset = CRYPTO_ALG_TYPE_MASK,
129d0e83059SHerbert Xu .type = CRYPTO_ALG_TYPE_RNG,
130d0e83059SHerbert Xu .tfmsize = offsetof(struct crypto_rng, base),
13117f0f4a4SNeil Horman };
13217f0f4a4SNeil Horman
crypto_alloc_rng(const char * alg_name,u32 type,u32 mask)133d0e83059SHerbert Xu struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask)
134d0e83059SHerbert Xu {
135d0e83059SHerbert Xu return crypto_alloc_tfm(alg_name, &crypto_rng_type, type, mask);
136d0e83059SHerbert Xu }
137d0e83059SHerbert Xu EXPORT_SYMBOL_GPL(crypto_alloc_rng);
138d0e83059SHerbert Xu
crypto_get_default_rng(void)13917f0f4a4SNeil Horman int crypto_get_default_rng(void)
14017f0f4a4SNeil Horman {
14117f0f4a4SNeil Horman struct crypto_rng *rng;
14217f0f4a4SNeil Horman int err;
14317f0f4a4SNeil Horman
14417f0f4a4SNeil Horman mutex_lock(&crypto_default_rng_lock);
14517f0f4a4SNeil Horman if (!crypto_default_rng) {
14617f0f4a4SNeil Horman rng = crypto_alloc_rng("stdrng", 0, 0);
14717f0f4a4SNeil Horman err = PTR_ERR(rng);
14817f0f4a4SNeil Horman if (IS_ERR(rng))
14917f0f4a4SNeil Horman goto unlock;
15017f0f4a4SNeil Horman
15117f0f4a4SNeil Horman err = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng));
15217f0f4a4SNeil Horman if (err) {
15317f0f4a4SNeil Horman crypto_free_rng(rng);
15417f0f4a4SNeil Horman goto unlock;
15517f0f4a4SNeil Horman }
15617f0f4a4SNeil Horman
15717f0f4a4SNeil Horman crypto_default_rng = rng;
15817f0f4a4SNeil Horman }
15917f0f4a4SNeil Horman
16017f0f4a4SNeil Horman crypto_default_rng_refcnt++;
16117f0f4a4SNeil Horman err = 0;
16217f0f4a4SNeil Horman
16317f0f4a4SNeil Horman unlock:
16417f0f4a4SNeil Horman mutex_unlock(&crypto_default_rng_lock);
16517f0f4a4SNeil Horman
16617f0f4a4SNeil Horman return err;
16717f0f4a4SNeil Horman }
16817f0f4a4SNeil Horman EXPORT_SYMBOL_GPL(crypto_get_default_rng);
16917f0f4a4SNeil Horman
crypto_put_default_rng(void)17017f0f4a4SNeil Horman void crypto_put_default_rng(void)
17117f0f4a4SNeil Horman {
17217f0f4a4SNeil Horman mutex_lock(&crypto_default_rng_lock);
1737cecadb7SHerbert Xu crypto_default_rng_refcnt--;
17417f0f4a4SNeil Horman mutex_unlock(&crypto_default_rng_lock);
17517f0f4a4SNeil Horman }
17617f0f4a4SNeil Horman EXPORT_SYMBOL_GPL(crypto_put_default_rng);
17717f0f4a4SNeil Horman
1787cecadb7SHerbert Xu #if defined(CONFIG_CRYPTO_RNG) || defined(CONFIG_CRYPTO_RNG_MODULE)
crypto_del_default_rng(void)1797cecadb7SHerbert Xu int crypto_del_default_rng(void)
1807cecadb7SHerbert Xu {
1817cecadb7SHerbert Xu int err = -EBUSY;
1827cecadb7SHerbert Xu
1837cecadb7SHerbert Xu mutex_lock(&crypto_default_rng_lock);
1847cecadb7SHerbert Xu if (crypto_default_rng_refcnt)
1857cecadb7SHerbert Xu goto out;
1867cecadb7SHerbert Xu
1877cecadb7SHerbert Xu crypto_free_rng(crypto_default_rng);
1887cecadb7SHerbert Xu crypto_default_rng = NULL;
1897cecadb7SHerbert Xu
1907cecadb7SHerbert Xu err = 0;
1917cecadb7SHerbert Xu
1927cecadb7SHerbert Xu out:
1937cecadb7SHerbert Xu mutex_unlock(&crypto_default_rng_lock);
1947cecadb7SHerbert Xu
1957cecadb7SHerbert Xu return err;
1967cecadb7SHerbert Xu }
1977cecadb7SHerbert Xu EXPORT_SYMBOL_GPL(crypto_del_default_rng);
1987cecadb7SHerbert Xu #endif
1997cecadb7SHerbert Xu
crypto_register_rng(struct rng_alg * alg)200acec27ffSHerbert Xu int crypto_register_rng(struct rng_alg *alg)
201acec27ffSHerbert Xu {
2029807e49bSHerbert Xu struct crypto_istat_rng *istat = rng_get_stat(alg);
203acec27ffSHerbert Xu struct crypto_alg *base = &alg->base;
204acec27ffSHerbert Xu
205acec27ffSHerbert Xu if (alg->seedsize > PAGE_SIZE / 8)
206acec27ffSHerbert Xu return -EINVAL;
207acec27ffSHerbert Xu
208acec27ffSHerbert Xu base->cra_type = &crypto_rng_type;
209acec27ffSHerbert Xu base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
210acec27ffSHerbert Xu base->cra_flags |= CRYPTO_ALG_TYPE_RNG;
211acec27ffSHerbert Xu
2129807e49bSHerbert Xu if (IS_ENABLED(CONFIG_CRYPTO_STATS))
2139807e49bSHerbert Xu memset(istat, 0, sizeof(*istat));
2149807e49bSHerbert Xu
215acec27ffSHerbert Xu return crypto_register_alg(base);
216acec27ffSHerbert Xu }
217acec27ffSHerbert Xu EXPORT_SYMBOL_GPL(crypto_register_rng);
218acec27ffSHerbert Xu
crypto_unregister_rng(struct rng_alg * alg)219acec27ffSHerbert Xu void crypto_unregister_rng(struct rng_alg *alg)
220acec27ffSHerbert Xu {
221acec27ffSHerbert Xu crypto_unregister_alg(&alg->base);
222acec27ffSHerbert Xu }
223acec27ffSHerbert Xu EXPORT_SYMBOL_GPL(crypto_unregister_rng);
224acec27ffSHerbert Xu
crypto_register_rngs(struct rng_alg * algs,int count)225881cd6c5SHerbert Xu int crypto_register_rngs(struct rng_alg *algs, int count)
226881cd6c5SHerbert Xu {
227881cd6c5SHerbert Xu int i, ret;
228881cd6c5SHerbert Xu
229881cd6c5SHerbert Xu for (i = 0; i < count; i++) {
230881cd6c5SHerbert Xu ret = crypto_register_rng(algs + i);
231881cd6c5SHerbert Xu if (ret)
232881cd6c5SHerbert Xu goto err;
233881cd6c5SHerbert Xu }
234881cd6c5SHerbert Xu
235881cd6c5SHerbert Xu return 0;
236881cd6c5SHerbert Xu
237881cd6c5SHerbert Xu err:
238881cd6c5SHerbert Xu for (--i; i >= 0; --i)
239881cd6c5SHerbert Xu crypto_unregister_rng(algs + i);
240881cd6c5SHerbert Xu
241881cd6c5SHerbert Xu return ret;
242881cd6c5SHerbert Xu }
243881cd6c5SHerbert Xu EXPORT_SYMBOL_GPL(crypto_register_rngs);
244881cd6c5SHerbert Xu
crypto_unregister_rngs(struct rng_alg * algs,int count)245881cd6c5SHerbert Xu void crypto_unregister_rngs(struct rng_alg *algs, int count)
246881cd6c5SHerbert Xu {
247881cd6c5SHerbert Xu int i;
248881cd6c5SHerbert Xu
249881cd6c5SHerbert Xu for (i = count - 1; i >= 0; --i)
250881cd6c5SHerbert Xu crypto_unregister_rng(algs + i);
251881cd6c5SHerbert Xu }
252881cd6c5SHerbert Xu EXPORT_SYMBOL_GPL(crypto_unregister_rngs);
253881cd6c5SHerbert Xu
25417f0f4a4SNeil Horman MODULE_LICENSE("GPL");
255a8ccc393SChristian Kujau MODULE_DESCRIPTION("Random Number Generator");
256