crypto: rng - Introduce crypto_rng_generate
[deliverable/linux.git] / crypto / rng.c
1 /*
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
15 #include <linux/atomic.h>
16 #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>
22 #include <linux/slab.h>
23 #include <linux/string.h>
24 #include <linux/cryptouser.h>
25 #include <net/netlink.h>
26
27 #include "internal.h"
28
29 static DEFINE_MUTEX(crypto_default_rng_lock);
30 struct crypto_rng *crypto_default_rng;
31 EXPORT_SYMBOL_GPL(crypto_default_rng);
32 static int crypto_default_rng_refcnt;
33
34 static inline struct crypto_rng *__crypto_rng_cast(struct crypto_tfm *tfm)
35 {
36 return container_of(tfm, struct crypto_rng, base);
37 }
38
39 static int generate(struct crypto_rng *tfm, const u8 *src, unsigned int slen,
40 u8 *dst, unsigned int dlen)
41 {
42 return crypto_rng_alg(tfm)->rng_make_random(tfm, dst, dlen);
43 }
44
45 static int rngapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
46 {
47 u8 *buf = NULL;
48 int err;
49
50 if (!seed && slen) {
51 buf = kmalloc(slen, GFP_KERNEL);
52 if (!buf)
53 return -ENOMEM;
54
55 get_random_bytes(buf, slen);
56 seed = buf;
57 }
58
59 err = crypto_rng_alg(tfm)->rng_reset(tfm, seed, slen);
60
61 kfree(buf);
62 return err;
63 }
64
65 static int crypto_rng_init_tfm(struct crypto_tfm *tfm)
66 {
67 struct crypto_rng *rng = __crypto_rng_cast(tfm);
68
69 rng->generate = generate;
70 rng->seed = rngapi_reset;
71
72 return 0;
73 }
74
75 #ifdef CONFIG_NET
76 static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
77 {
78 struct crypto_report_rng rrng;
79
80 strncpy(rrng.type, "rng", sizeof(rrng.type));
81
82 rrng.seedsize = alg->cra_rng.seedsize;
83
84 if (nla_put(skb, CRYPTOCFGA_REPORT_RNG,
85 sizeof(struct crypto_report_rng), &rrng))
86 goto nla_put_failure;
87 return 0;
88
89 nla_put_failure:
90 return -EMSGSIZE;
91 }
92 #else
93 static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
94 {
95 return -ENOSYS;
96 }
97 #endif
98
99 static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
100 __attribute__ ((unused));
101 static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
102 {
103 seq_printf(m, "type : rng\n");
104 seq_printf(m, "seedsize : %u\n", alg->cra_rng.seedsize);
105 }
106
107 const struct crypto_type crypto_rng_type = {
108 .extsize = crypto_alg_extsize,
109 .init_tfm = crypto_rng_init_tfm,
110 #ifdef CONFIG_PROC_FS
111 .show = crypto_rng_show,
112 #endif
113 .report = crypto_rng_report,
114 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
115 .maskset = CRYPTO_ALG_TYPE_MASK,
116 .type = CRYPTO_ALG_TYPE_RNG,
117 .tfmsize = offsetof(struct crypto_rng, base),
118 };
119 EXPORT_SYMBOL_GPL(crypto_rng_type);
120
121 struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask)
122 {
123 return crypto_alloc_tfm(alg_name, &crypto_rng_type, type, mask);
124 }
125 EXPORT_SYMBOL_GPL(crypto_alloc_rng);
126
127 int crypto_get_default_rng(void)
128 {
129 struct crypto_rng *rng;
130 int err;
131
132 mutex_lock(&crypto_default_rng_lock);
133 if (!crypto_default_rng) {
134 rng = crypto_alloc_rng("stdrng", 0, 0);
135 err = PTR_ERR(rng);
136 if (IS_ERR(rng))
137 goto unlock;
138
139 err = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng));
140 if (err) {
141 crypto_free_rng(rng);
142 goto unlock;
143 }
144
145 crypto_default_rng = rng;
146 }
147
148 crypto_default_rng_refcnt++;
149 err = 0;
150
151 unlock:
152 mutex_unlock(&crypto_default_rng_lock);
153
154 return err;
155 }
156 EXPORT_SYMBOL_GPL(crypto_get_default_rng);
157
158 void crypto_put_default_rng(void)
159 {
160 mutex_lock(&crypto_default_rng_lock);
161 if (!--crypto_default_rng_refcnt) {
162 crypto_free_rng(crypto_default_rng);
163 crypto_default_rng = NULL;
164 }
165 mutex_unlock(&crypto_default_rng_lock);
166 }
167 EXPORT_SYMBOL_GPL(crypto_put_default_rng);
168
169 MODULE_LICENSE("GPL");
170 MODULE_DESCRIPTION("Random Number Generator");
This page took 0.036198 seconds and 6 git commands to generate.