blob: ceea83d13168f648e2ca22f635504992c24ba7f2 [file] [log] [blame]
Stephan Muellerdfc9fa92015-06-23 16:18:54 +02001/*
2 * Non-physical true random number generator based on timing jitter --
3 * Linux Kernel Crypto API specific code
4 *
5 * Copyright Stephan Mueller <smueller@chronox.de>, 2015
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * ALTERNATIVELY, this product may be distributed under the terms of
21 * the GNU General Public License, in which case the provisions of the GPL2 are
22 * required INSTEAD OF the above restrictions. (This clause is
23 * necessary due to a potential bad interaction between the GPL and
24 * the restrictions contained in a BSD-style copyright.)
25 *
26 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
29 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
32 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
37 * DAMAGE.
38 */
39
40#include <linux/module.h>
41#include <linux/slab.h>
42#include <linux/module.h>
43#include <linux/fips.h>
44#include <linux/time.h>
45#include <linux/crypto.h>
46#include <crypto/internal/rng.h>
47
48struct rand_data;
49int jent_read_entropy(struct rand_data *ec, unsigned char *data,
50 unsigned int len);
51int jent_entropy_init(void);
52struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
53 unsigned int flags);
54void jent_entropy_collector_free(struct rand_data *entropy_collector);
55
56/***************************************************************************
57 * Helper function
58 ***************************************************************************/
59
60__u64 jent_rol64(__u64 word, unsigned int shift)
61{
62 return rol64(word, shift);
63}
64
65void *jent_zalloc(unsigned int len)
66{
67 return kzalloc(len, GFP_KERNEL);
68}
69
70void jent_zfree(void *ptr)
71{
72 kzfree(ptr);
73}
74
75int jent_fips_enabled(void)
76{
77 return fips_enabled;
78}
79
80void jent_panic(char *s)
81{
Kees Cook0c5f0aa2015-07-24 15:41:27 -070082 panic("%s", s);
Stephan Muellerdfc9fa92015-06-23 16:18:54 +020083}
84
85void jent_memcpy(void *dest, const void *src, unsigned int n)
86{
87 memcpy(dest, src, n);
88}
89
90void jent_get_nstime(__u64 *out)
91{
92 struct timespec ts;
93 __u64 tmp = 0;
94
95 tmp = random_get_entropy();
96
97 /*
98 * If random_get_entropy does not return a value (which is possible on,
99 * for example, MIPS), invoke __getnstimeofday
100 * hoping that there are timers we can work with.
101 *
102 * The list of available timers can be obtained from
103 * /sys/devices/system/clocksource/clocksource0/available_clocksource
104 * and are registered with clocksource_register()
105 */
106 if ((0 == tmp) &&
107 (0 == __getnstimeofday(&ts))) {
108 tmp = ts.tv_sec;
109 tmp = tmp << 32;
110 tmp = tmp | ts.tv_nsec;
111 }
112
113 *out = tmp;
114}
115
116/***************************************************************************
117 * Kernel crypto API interface
118 ***************************************************************************/
119
120struct jitterentropy {
121 spinlock_t jent_lock;
122 struct rand_data *entropy_collector;
123};
124
125static int jent_kcapi_init(struct crypto_tfm *tfm)
126{
127 struct jitterentropy *rng = crypto_tfm_ctx(tfm);
128 int ret = 0;
129
130 rng->entropy_collector = jent_entropy_collector_alloc(1, 0);
131 if (!rng->entropy_collector)
132 ret = -ENOMEM;
133
134 spin_lock_init(&rng->jent_lock);
135 return ret;
136}
137
138static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
139{
140 struct jitterentropy *rng = crypto_tfm_ctx(tfm);
141
142 spin_lock(&rng->jent_lock);
143 if (rng->entropy_collector)
144 jent_entropy_collector_free(rng->entropy_collector);
145 rng->entropy_collector = NULL;
146 spin_unlock(&rng->jent_lock);
147}
148
149static int jent_kcapi_random(struct crypto_rng *tfm,
150 const u8 *src, unsigned int slen,
151 u8 *rdata, unsigned int dlen)
152{
153 struct jitterentropy *rng = crypto_rng_ctx(tfm);
154 int ret = 0;
155
156 spin_lock(&rng->jent_lock);
157 ret = jent_read_entropy(rng->entropy_collector, rdata, dlen);
158 spin_unlock(&rng->jent_lock);
159
160 return ret;
161}
162
163static int jent_kcapi_reset(struct crypto_rng *tfm,
164 const u8 *seed, unsigned int slen)
165{
166 return 0;
167}
168
169static struct rng_alg jent_alg = {
170 .generate = jent_kcapi_random,
171 .seed = jent_kcapi_reset,
172 .seedsize = 0,
173 .base = {
174 .cra_name = "jitterentropy_rng",
175 .cra_driver_name = "jitterentropy_rng",
176 .cra_priority = 100,
177 .cra_ctxsize = sizeof(struct jitterentropy),
178 .cra_module = THIS_MODULE,
179 .cra_init = jent_kcapi_init,
180 .cra_exit = jent_kcapi_cleanup,
181
182 }
183};
184
185static int __init jent_mod_init(void)
186{
187 int ret = 0;
188
189 ret = jent_entropy_init();
190 if (ret) {
191 pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
192 return -EFAULT;
193 }
194 return crypto_register_rng(&jent_alg);
195}
196
197static void __exit jent_mod_exit(void)
198{
199 crypto_unregister_rng(&jent_alg);
200}
201
202module_init(jent_mod_init);
203module_exit(jent_mod_exit);
204
205MODULE_LICENSE("Dual BSD/GPL");
206MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
207MODULE_DESCRIPTION("Non-physical True Random Number Generator based on CPU Jitter");
208MODULE_ALIAS_CRYPTO("jitterentropy_rng");