18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Cryptographic API.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * RNG operations.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
88c2ecf20Sopenharmony_ci * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/atomic.h>
128c2ecf20Sopenharmony_ci#include <crypto/internal/rng.h>
138c2ecf20Sopenharmony_ci#include <linux/err.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/mutex.h>
168c2ecf20Sopenharmony_ci#include <linux/random.h>
178c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
188c2ecf20Sopenharmony_ci#include <linux/slab.h>
198c2ecf20Sopenharmony_ci#include <linux/string.h>
208c2ecf20Sopenharmony_ci#include <linux/cryptouser.h>
218c2ecf20Sopenharmony_ci#include <linux/compiler.h>
228c2ecf20Sopenharmony_ci#include <net/netlink.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "internal.h"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(crypto_default_rng_lock);
278c2ecf20Sopenharmony_cistruct crypto_rng *crypto_default_rng;
288c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_default_rng);
298c2ecf20Sopenharmony_cistatic int crypto_default_rng_refcnt;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ciint crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed, unsigned int slen)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	struct crypto_alg *alg = tfm->base.__crt_alg;
348c2ecf20Sopenharmony_ci	u8 *buf = NULL;
358c2ecf20Sopenharmony_ci	int err;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	if (!seed && slen) {
388c2ecf20Sopenharmony_ci		buf = kmalloc(slen, GFP_KERNEL);
398c2ecf20Sopenharmony_ci		if (!buf)
408c2ecf20Sopenharmony_ci			return -ENOMEM;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci		err = get_random_bytes_wait(buf, slen);
438c2ecf20Sopenharmony_ci		if (err)
448c2ecf20Sopenharmony_ci			goto out;
458c2ecf20Sopenharmony_ci		seed = buf;
468c2ecf20Sopenharmony_ci	}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	crypto_stats_get(alg);
498c2ecf20Sopenharmony_ci	err = crypto_rng_alg(tfm)->seed(tfm, seed, slen);
508c2ecf20Sopenharmony_ci	crypto_stats_rng_seed(alg, err);
518c2ecf20Sopenharmony_ciout:
528c2ecf20Sopenharmony_ci	kfree_sensitive(buf);
538c2ecf20Sopenharmony_ci	return err;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_rng_reset);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic int crypto_rng_init_tfm(struct crypto_tfm *tfm)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	return 0;
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic unsigned int seedsize(struct crypto_alg *alg)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	struct rng_alg *ralg = container_of(alg, struct rng_alg, base);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	return ralg->seedsize;
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#ifdef CONFIG_NET
708c2ecf20Sopenharmony_cistatic int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct crypto_report_rng rrng;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	memset(&rrng, 0, sizeof(rrng));
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	strscpy(rrng.type, "rng", sizeof(rrng.type));
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	rrng.seedsize = seedsize(alg);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	return nla_put(skb, CRYPTOCFGA_REPORT_RNG, sizeof(rrng), &rrng);
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci#else
838c2ecf20Sopenharmony_cistatic int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	return -ENOSYS;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci#endif
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
908c2ecf20Sopenharmony_ci	__maybe_unused;
918c2ecf20Sopenharmony_cistatic void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	seq_printf(m, "type         : rng\n");
948c2ecf20Sopenharmony_ci	seq_printf(m, "seedsize     : %u\n", seedsize(alg));
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic const struct crypto_type crypto_rng_type = {
988c2ecf20Sopenharmony_ci	.extsize = crypto_alg_extsize,
998c2ecf20Sopenharmony_ci	.init_tfm = crypto_rng_init_tfm,
1008c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
1018c2ecf20Sopenharmony_ci	.show = crypto_rng_show,
1028c2ecf20Sopenharmony_ci#endif
1038c2ecf20Sopenharmony_ci	.report = crypto_rng_report,
1048c2ecf20Sopenharmony_ci	.maskclear = ~CRYPTO_ALG_TYPE_MASK,
1058c2ecf20Sopenharmony_ci	.maskset = CRYPTO_ALG_TYPE_MASK,
1068c2ecf20Sopenharmony_ci	.type = CRYPTO_ALG_TYPE_RNG,
1078c2ecf20Sopenharmony_ci	.tfmsize = offsetof(struct crypto_rng, base),
1088c2ecf20Sopenharmony_ci};
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistruct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	return crypto_alloc_tfm(alg_name, &crypto_rng_type, type, mask);
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_alloc_rng);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ciint crypto_get_default_rng(void)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct crypto_rng *rng;
1198c2ecf20Sopenharmony_ci	int err;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	mutex_lock(&crypto_default_rng_lock);
1228c2ecf20Sopenharmony_ci	if (!crypto_default_rng) {
1238c2ecf20Sopenharmony_ci		rng = crypto_alloc_rng("stdrng", 0, 0);
1248c2ecf20Sopenharmony_ci		err = PTR_ERR(rng);
1258c2ecf20Sopenharmony_ci		if (IS_ERR(rng))
1268c2ecf20Sopenharmony_ci			goto unlock;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci		err = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng));
1298c2ecf20Sopenharmony_ci		if (err) {
1308c2ecf20Sopenharmony_ci			crypto_free_rng(rng);
1318c2ecf20Sopenharmony_ci			goto unlock;
1328c2ecf20Sopenharmony_ci		}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		crypto_default_rng = rng;
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	crypto_default_rng_refcnt++;
1388c2ecf20Sopenharmony_ci	err = 0;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ciunlock:
1418c2ecf20Sopenharmony_ci	mutex_unlock(&crypto_default_rng_lock);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	return err;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_get_default_rng);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_civoid crypto_put_default_rng(void)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	mutex_lock(&crypto_default_rng_lock);
1508c2ecf20Sopenharmony_ci	crypto_default_rng_refcnt--;
1518c2ecf20Sopenharmony_ci	mutex_unlock(&crypto_default_rng_lock);
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_put_default_rng);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci#if defined(CONFIG_CRYPTO_RNG) || defined(CONFIG_CRYPTO_RNG_MODULE)
1568c2ecf20Sopenharmony_ciint crypto_del_default_rng(void)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	int err = -EBUSY;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	mutex_lock(&crypto_default_rng_lock);
1618c2ecf20Sopenharmony_ci	if (crypto_default_rng_refcnt)
1628c2ecf20Sopenharmony_ci		goto out;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	crypto_free_rng(crypto_default_rng);
1658c2ecf20Sopenharmony_ci	crypto_default_rng = NULL;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	err = 0;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ciout:
1708c2ecf20Sopenharmony_ci	mutex_unlock(&crypto_default_rng_lock);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	return err;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_del_default_rng);
1758c2ecf20Sopenharmony_ci#endif
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ciint crypto_register_rng(struct rng_alg *alg)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	struct crypto_alg *base = &alg->base;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	if (alg->seedsize > PAGE_SIZE / 8)
1828c2ecf20Sopenharmony_ci		return -EINVAL;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	base->cra_type = &crypto_rng_type;
1858c2ecf20Sopenharmony_ci	base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
1868c2ecf20Sopenharmony_ci	base->cra_flags |= CRYPTO_ALG_TYPE_RNG;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	return crypto_register_alg(base);
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_register_rng);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_civoid crypto_unregister_rng(struct rng_alg *alg)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	crypto_unregister_alg(&alg->base);
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_unregister_rng);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ciint crypto_register_rngs(struct rng_alg *algs, int count)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	int i, ret;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
2038c2ecf20Sopenharmony_ci		ret = crypto_register_rng(algs + i);
2048c2ecf20Sopenharmony_ci		if (ret)
2058c2ecf20Sopenharmony_ci			goto err;
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	return 0;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cierr:
2118c2ecf20Sopenharmony_ci	for (--i; i >= 0; --i)
2128c2ecf20Sopenharmony_ci		crypto_unregister_rng(algs + i);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	return ret;
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_register_rngs);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_civoid crypto_unregister_rngs(struct rng_alg *algs, int count)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	int i;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	for (i = count - 1; i >= 0; --i)
2238c2ecf20Sopenharmony_ci		crypto_unregister_rng(algs + i);
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_unregister_rngs);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2288c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Random Number Generator");
229