blob: 5e0425a246579f490f52b14eb00d5c2a5f5b0c8a [file] [log] [blame]
Neil Horman17f0f4a2008-08-14 22:15:52 +10001/*
2 * Cryptographic API.
3 *
4 * RNG operations.
5 *
6 * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
7 *
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)
11 * any later version.
12 *
13 */
14
Arun Sharma600634972011-07-26 16:09:06 -070015#include <linux/atomic.h>
Neil Horman17f0f4a2008-08-14 22:15:52 +100016#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Neil Horman17f0f4a2008-08-14 22:15:52 +100023#include <linux/string.h>
Steffen Klassert792608e2011-09-27 07:47:11 +020024#include <linux/cryptouser.h>
25#include <net/netlink.h>
Neil Horman17f0f4a2008-08-14 22:15:52 +100026
Herbert Xud0e83052015-04-20 13:39:03 +080027#include "internal.h"
28
Neil Horman17f0f4a2008-08-14 22:15:52 +100029static DEFINE_MUTEX(crypto_default_rng_lock);
30struct crypto_rng *crypto_default_rng;
31EXPORT_SYMBOL_GPL(crypto_default_rng);
32static int crypto_default_rng_refcnt;
33
Herbert Xud0e83052015-04-20 13:39:03 +080034static inline struct crypto_rng *__crypto_rng_cast(struct crypto_tfm *tfm)
35{
36 return container_of(tfm, struct crypto_rng, base);
37}
38
Herbert Xuacec27f2015-04-21 10:46:38 +080039static inline struct old_rng_alg *crypto_old_rng_alg(struct crypto_rng *tfm)
40{
41 return &crypto_rng_tfm(tfm)->__crt_alg->cra_rng;
42}
43
Herbert Xuff030b02015-04-20 13:39:04 +080044static int generate(struct crypto_rng *tfm, const u8 *src, unsigned int slen,
45 u8 *dst, unsigned int dlen)
46{
Herbert Xuacec27f2015-04-21 10:46:38 +080047 return crypto_old_rng_alg(tfm)->rng_make_random(tfm, dst, dlen);
Herbert Xuff030b02015-04-20 13:39:04 +080048}
49
Herbert Xu3c5d8fa2015-04-21 10:46:37 +080050static int rngapi_reset(struct crypto_rng *tfm, const u8 *seed,
51 unsigned int slen)
52{
53 u8 *buf = NULL;
54 u8 *src = (u8 *)seed;
55 int err;
56
57 if (slen) {
58 buf = kmalloc(slen, GFP_KERNEL);
59 if (!buf)
60 return -ENOMEM;
61
62 memcpy(buf, seed, slen);
63 src = buf;
64 }
65
Herbert Xuacec27f2015-04-21 10:46:38 +080066 err = crypto_old_rng_alg(tfm)->rng_reset(tfm, src, slen);
Herbert Xu3c5d8fa2015-04-21 10:46:37 +080067
68 kzfree(buf);
69 return err;
70}
71
72int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed, unsigned int slen)
Neil Horman17f0f4a2008-08-14 22:15:52 +100073{
74 u8 *buf = NULL;
75 int err;
76
77 if (!seed && slen) {
78 buf = kmalloc(slen, GFP_KERNEL);
79 if (!buf)
80 return -ENOMEM;
81
82 get_random_bytes(buf, slen);
83 seed = buf;
84 }
85
Herbert Xu3c5d8fa2015-04-21 10:46:37 +080086 err = tfm->seed(tfm, seed, slen);
Neil Horman17f0f4a2008-08-14 22:15:52 +100087
88 kfree(buf);
89 return err;
90}
Herbert Xu3c5d8fa2015-04-21 10:46:37 +080091EXPORT_SYMBOL_GPL(crypto_rng_reset);
Neil Horman17f0f4a2008-08-14 22:15:52 +100092
Herbert Xud0e83052015-04-20 13:39:03 +080093static int crypto_rng_init_tfm(struct crypto_tfm *tfm)
Neil Horman17f0f4a2008-08-14 22:15:52 +100094{
Herbert Xud0e83052015-04-20 13:39:03 +080095 struct crypto_rng *rng = __crypto_rng_cast(tfm);
Herbert Xuacec27f2015-04-21 10:46:38 +080096 struct rng_alg *alg = crypto_rng_alg(rng);
97 struct old_rng_alg *oalg = crypto_old_rng_alg(rng);
Neil Horman17f0f4a2008-08-14 22:15:52 +100098
Herbert Xuacec27f2015-04-21 10:46:38 +080099 if (oalg->rng_make_random) {
100 rng->generate = generate;
101 rng->seed = rngapi_reset;
102 rng->seedsize = oalg->seedsize;
103 return 0;
104 }
105
106 rng->generate = alg->generate;
107 rng->seed = alg->seed;
108 rng->seedsize = alg->seedsize;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000109
110 return 0;
111}
112
Herbert Xuacec27f2015-04-21 10:46:38 +0800113static unsigned int seedsize(struct crypto_alg *alg)
114{
115 struct rng_alg *ralg = container_of(alg, struct rng_alg, base);
116
117 return alg->cra_rng.rng_make_random ?
118 alg->cra_rng.seedsize : ralg->seedsize;
119}
120
Herbert Xu3acc8472011-11-03 23:46:07 +1100121#ifdef CONFIG_NET
Steffen Klassert792608e2011-09-27 07:47:11 +0200122static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
123{
124 struct crypto_report_rng rrng;
125
Mathias Krause9a5467bf2013-02-05 18:19:13 +0100126 strncpy(rrng.type, "rng", sizeof(rrng.type));
Steffen Klassert792608e2011-09-27 07:47:11 +0200127
Herbert Xuacec27f2015-04-21 10:46:38 +0800128 rrng.seedsize = seedsize(alg);
Steffen Klassert792608e2011-09-27 07:47:11 +0200129
David S. Miller6662df32012-04-01 20:19:05 -0400130 if (nla_put(skb, CRYPTOCFGA_REPORT_RNG,
131 sizeof(struct crypto_report_rng), &rrng))
132 goto nla_put_failure;
Steffen Klassert792608e2011-09-27 07:47:11 +0200133 return 0;
134
135nla_put_failure:
136 return -EMSGSIZE;
137}
Herbert Xu3acc8472011-11-03 23:46:07 +1100138#else
139static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
140{
141 return -ENOSYS;
142}
143#endif
Steffen Klassert792608e2011-09-27 07:47:11 +0200144
Neil Horman17f0f4a2008-08-14 22:15:52 +1000145static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
146 __attribute__ ((unused));
147static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
148{
149 seq_printf(m, "type : rng\n");
Herbert Xuacec27f2015-04-21 10:46:38 +0800150 seq_printf(m, "seedsize : %u\n", seedsize(alg));
Neil Horman17f0f4a2008-08-14 22:15:52 +1000151}
152
Neil Horman17f0f4a2008-08-14 22:15:52 +1000153const struct crypto_type crypto_rng_type = {
Herbert Xud0e83052015-04-20 13:39:03 +0800154 .extsize = crypto_alg_extsize,
155 .init_tfm = crypto_rng_init_tfm,
Neil Horman17f0f4a2008-08-14 22:15:52 +1000156#ifdef CONFIG_PROC_FS
157 .show = crypto_rng_show,
158#endif
Steffen Klassert792608e2011-09-27 07:47:11 +0200159 .report = crypto_rng_report,
Herbert Xud0e83052015-04-20 13:39:03 +0800160 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
161 .maskset = CRYPTO_ALG_TYPE_MASK,
162 .type = CRYPTO_ALG_TYPE_RNG,
163 .tfmsize = offsetof(struct crypto_rng, base),
Neil Horman17f0f4a2008-08-14 22:15:52 +1000164};
165EXPORT_SYMBOL_GPL(crypto_rng_type);
166
Herbert Xud0e83052015-04-20 13:39:03 +0800167struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask)
168{
169 return crypto_alloc_tfm(alg_name, &crypto_rng_type, type, mask);
170}
171EXPORT_SYMBOL_GPL(crypto_alloc_rng);
172
Neil Horman17f0f4a2008-08-14 22:15:52 +1000173int crypto_get_default_rng(void)
174{
175 struct crypto_rng *rng;
176 int err;
177
178 mutex_lock(&crypto_default_rng_lock);
179 if (!crypto_default_rng) {
180 rng = crypto_alloc_rng("stdrng", 0, 0);
181 err = PTR_ERR(rng);
182 if (IS_ERR(rng))
183 goto unlock;
184
185 err = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng));
186 if (err) {
187 crypto_free_rng(rng);
188 goto unlock;
189 }
190
191 crypto_default_rng = rng;
192 }
193
194 crypto_default_rng_refcnt++;
195 err = 0;
196
197unlock:
198 mutex_unlock(&crypto_default_rng_lock);
199
200 return err;
201}
202EXPORT_SYMBOL_GPL(crypto_get_default_rng);
203
204void crypto_put_default_rng(void)
205{
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;
210 }
211 mutex_unlock(&crypto_default_rng_lock);
212}
213EXPORT_SYMBOL_GPL(crypto_put_default_rng);
214
Herbert Xuacec27f2015-04-21 10:46:38 +0800215int crypto_register_rng(struct rng_alg *alg)
216{
217 struct crypto_alg *base = &alg->base;
218
219 if (alg->seedsize > PAGE_SIZE / 8)
220 return -EINVAL;
221
222 base->cra_type = &crypto_rng_type;
223 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
224 base->cra_flags |= CRYPTO_ALG_TYPE_RNG;
225
226 return crypto_register_alg(base);
227}
228EXPORT_SYMBOL_GPL(crypto_register_rng);
229
230void crypto_unregister_rng(struct rng_alg *alg)
231{
232 crypto_unregister_alg(&alg->base);
233}
234EXPORT_SYMBOL_GPL(crypto_unregister_rng);
235
Neil Horman17f0f4a2008-08-14 22:15:52 +1000236MODULE_LICENSE("GPL");
Christian Kujaua8ccc392009-08-13 11:53:56 +1000237MODULE_DESCRIPTION("Random Number Generator");