]>
Commit | Line | Data |
---|---|---|
17f0f4a4 NH |
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 | ||
60063497 | 15 | #include <linux/atomic.h> |
17f0f4a4 NH |
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> | |
5a0e3ad6 | 22 | #include <linux/slab.h> |
17f0f4a4 | 23 | #include <linux/string.h> |
792608e9 SK |
24 | #include <linux/cryptouser.h> |
25 | #include <net/netlink.h> | |
17f0f4a4 | 26 | |
d0e83059 HX |
27 | #include "internal.h" |
28 | ||
17f0f4a4 NH |
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 | ||
d0e83059 HX |
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 | ||
ff030b09 HX |
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 | ||
17f0f4a4 NH |
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 | ||
d0e83059 | 65 | static int crypto_rng_init_tfm(struct crypto_tfm *tfm) |
17f0f4a4 | 66 | { |
d0e83059 | 67 | struct crypto_rng *rng = __crypto_rng_cast(tfm); |
17f0f4a4 | 68 | |
ff030b09 | 69 | rng->generate = generate; |
d0e83059 | 70 | rng->seed = rngapi_reset; |
17f0f4a4 NH |
71 | |
72 | return 0; | |
73 | } | |
74 | ||
3acc8473 | 75 | #ifdef CONFIG_NET |
792608e9 SK |
76 | static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg) |
77 | { | |
78 | struct crypto_report_rng rrng; | |
79 | ||
9a5467bf | 80 | strncpy(rrng.type, "rng", sizeof(rrng.type)); |
792608e9 SK |
81 | |
82 | rrng.seedsize = alg->cra_rng.seedsize; | |
83 | ||
6662df33 DM |
84 | if (nla_put(skb, CRYPTOCFGA_REPORT_RNG, |
85 | sizeof(struct crypto_report_rng), &rrng)) | |
86 | goto nla_put_failure; | |
792608e9 SK |
87 | return 0; |
88 | ||
89 | nla_put_failure: | |
90 | return -EMSGSIZE; | |
91 | } | |
3acc8473 HX |
92 | #else |
93 | static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg) | |
94 | { | |
95 | return -ENOSYS; | |
96 | } | |
97 | #endif | |
792608e9 | 98 | |
17f0f4a4 NH |
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 | ||
17f0f4a4 | 107 | const struct crypto_type crypto_rng_type = { |
d0e83059 HX |
108 | .extsize = crypto_alg_extsize, |
109 | .init_tfm = crypto_rng_init_tfm, | |
17f0f4a4 NH |
110 | #ifdef CONFIG_PROC_FS |
111 | .show = crypto_rng_show, | |
112 | #endif | |
792608e9 | 113 | .report = crypto_rng_report, |
d0e83059 HX |
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), | |
17f0f4a4 NH |
118 | }; |
119 | EXPORT_SYMBOL_GPL(crypto_rng_type); | |
120 | ||
d0e83059 HX |
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 | ||
17f0f4a4 NH |
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"); | |
a8ccc393 | 170 | MODULE_DESCRIPTION("Random Number Generator"); |