Branch data Line data Source code
1 : : /*
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/fips.h>
43 : : #include <linux/time.h>
44 : : #include <linux/crypto.h>
45 : : #include <crypto/internal/rng.h>
46 : :
47 : : #include "jitterentropy.h"
48 : :
49 : : /***************************************************************************
50 : : * Helper function
51 : : ***************************************************************************/
52 : :
53 : 0 : void *jent_zalloc(unsigned int len)
54 : : {
55 : 0 : return kzalloc(len, GFP_KERNEL);
56 : : }
57 : :
58 : 0 : void jent_zfree(void *ptr)
59 : : {
60 : 0 : kzfree(ptr);
61 : 0 : }
62 : :
63 : 0 : int jent_fips_enabled(void)
64 : : {
65 : 0 : return fips_enabled;
66 : : }
67 : :
68 : 0 : void jent_panic(char *s)
69 : : {
70 : 0 : panic("%s", s);
71 : : }
72 : :
73 : 0 : void jent_memcpy(void *dest, const void *src, unsigned int n)
74 : : {
75 : 0 : memcpy(dest, src, n);
76 : 0 : }
77 : :
78 : : /*
79 : : * Obtain a high-resolution time stamp value. The time stamp is used to measure
80 : : * the execution time of a given code path and its variations. Hence, the time
81 : : * stamp must have a sufficiently high resolution.
82 : : *
83 : : * Note, if the function returns zero because a given architecture does not
84 : : * implement a high-resolution time stamp, the RNG code's runtime test
85 : : * will detect it and will not produce output.
86 : : */
87 : 33600 : void jent_get_nstime(__u64 *out)
88 : : {
89 : 33600 : __u64 tmp = 0;
90 : :
91 : 33600 : tmp = random_get_entropy();
92 : :
93 : : /*
94 : : * If random_get_entropy does not return a value, i.e. it is not
95 : : * implemented for a given architecture, use a clock source.
96 : : * hoping that there are timers we can work with.
97 : : */
98 [ - + ]: 33600 : if (tmp == 0)
99 : 0 : tmp = ktime_get_ns();
100 : :
101 : 33600 : *out = tmp;
102 : 33600 : }
103 : :
104 : : /***************************************************************************
105 : : * Kernel crypto API interface
106 : : ***************************************************************************/
107 : :
108 : : struct jitterentropy {
109 : : spinlock_t jent_lock;
110 : : struct rand_data *entropy_collector;
111 : : };
112 : :
113 : 0 : static int jent_kcapi_init(struct crypto_tfm *tfm)
114 : : {
115 : 0 : struct jitterentropy *rng = crypto_tfm_ctx(tfm);
116 : 0 : int ret = 0;
117 : :
118 : 0 : rng->entropy_collector = jent_entropy_collector_alloc(1, 0);
119 [ # # ]: 0 : if (!rng->entropy_collector)
120 : 0 : ret = -ENOMEM;
121 : :
122 : 0 : spin_lock_init(&rng->jent_lock);
123 : 0 : return ret;
124 : : }
125 : :
126 : 0 : static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
127 : : {
128 : 0 : struct jitterentropy *rng = crypto_tfm_ctx(tfm);
129 : :
130 : 0 : spin_lock(&rng->jent_lock);
131 [ # # ]: 0 : if (rng->entropy_collector)
132 : 0 : jent_entropy_collector_free(rng->entropy_collector);
133 : 0 : rng->entropy_collector = NULL;
134 : 0 : spin_unlock(&rng->jent_lock);
135 : 0 : }
136 : :
137 : 0 : static int jent_kcapi_random(struct crypto_rng *tfm,
138 : : const u8 *src, unsigned int slen,
139 : : u8 *rdata, unsigned int dlen)
140 : : {
141 : 0 : struct jitterentropy *rng = crypto_rng_ctx(tfm);
142 : 0 : int ret = 0;
143 : :
144 : 0 : spin_lock(&rng->jent_lock);
145 : 0 : ret = jent_read_entropy(rng->entropy_collector, rdata, dlen);
146 : 0 : spin_unlock(&rng->jent_lock);
147 : :
148 : 0 : return ret;
149 : : }
150 : :
151 : 0 : static int jent_kcapi_reset(struct crypto_rng *tfm,
152 : : const u8 *seed, unsigned int slen)
153 : : {
154 : 0 : return 0;
155 : : }
156 : :
157 : : static struct rng_alg jent_alg = {
158 : : .generate = jent_kcapi_random,
159 : : .seed = jent_kcapi_reset,
160 : : .seedsize = 0,
161 : : .base = {
162 : : .cra_name = "jitterentropy_rng",
163 : : .cra_driver_name = "jitterentropy_rng",
164 : : .cra_priority = 100,
165 : : .cra_ctxsize = sizeof(struct jitterentropy),
166 : : .cra_module = THIS_MODULE,
167 : : .cra_init = jent_kcapi_init,
168 : : .cra_exit = jent_kcapi_cleanup,
169 : :
170 : : }
171 : : };
172 : :
173 : 28 : static int __init jent_mod_init(void)
174 : : {
175 : 28 : int ret = 0;
176 : :
177 : 28 : ret = jent_entropy_init();
178 [ - + ]: 28 : if (ret) {
179 : 0 : pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
180 : 0 : return -EFAULT;
181 : : }
182 : 28 : return crypto_register_rng(&jent_alg);
183 : : }
184 : :
185 : 0 : static void __exit jent_mod_exit(void)
186 : : {
187 : 0 : crypto_unregister_rng(&jent_alg);
188 : 0 : }
189 : :
190 : : module_init(jent_mod_init);
191 : : module_exit(jent_mod_exit);
192 : :
193 : : MODULE_LICENSE("Dual BSD/GPL");
194 : : MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
195 : : MODULE_DESCRIPTION("Non-physical True Random Number Generator based on CPU Jitter");
196 : : MODULE_ALIAS_CRYPTO("jitterentropy_rng");
|