xref: /linux/drivers/char/hw_random/core.c (revision 8b6aaf65d3b001ec9b5dcba0992b3b68cbf6057f)
1 /*
2         Added support for the AMD Geode LX RNG
3 	(c) Copyright 2004-2005 Advanced Micro Devices, Inc.
4 
5 	derived from
6 
7  	Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
8 	(c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
9 
10  	derived from
11 
12         Hardware driver for the AMD 768 Random Number Generator (RNG)
13         (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
14 
15  	derived from
16 
17 	Hardware driver for Intel i810 Random Number Generator (RNG)
18 	Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
19 	Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
20 
21 	Added generic RNG API
22 	Copyright 2006 Michael Buesch <m@bues.ch>
23 	Copyright 2005 (c) MontaVista Software, Inc.
24 
25 	Please read Documentation/hw_random.txt for details on use.
26 
27 	----------------------------------------------------------
28 	This software may be used and distributed according to the terms
29         of the GNU General Public License, incorporated herein by reference.
30 
31  */
32 
33 
34 #include <linux/device.h>
35 #include <linux/hw_random.h>
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/fs.h>
39 #include <linux/sched.h>
40 #include <linux/miscdevice.h>
41 #include <linux/kthread.h>
42 #include <linux/delay.h>
43 #include <linux/slab.h>
44 #include <linux/random.h>
45 #include <asm/uaccess.h>
46 
47 
48 #define RNG_MODULE_NAME		"hw_random"
49 #define PFX			RNG_MODULE_NAME ": "
50 #define RNG_MISCDEV_MINOR	183 /* official */
51 
52 
53 static struct hwrng *current_rng;
54 static struct task_struct *hwrng_fill;
55 static LIST_HEAD(rng_list);
56 static DEFINE_MUTEX(rng_mutex);
57 static int data_avail;
58 static u8 *rng_buffer, *rng_fillbuf;
59 static unsigned short current_quality;
60 static unsigned short default_quality; /* = 0; default to "off" */
61 
62 module_param(current_quality, ushort, 0644);
63 MODULE_PARM_DESC(current_quality,
64 		 "current hwrng entropy estimation per mill");
65 module_param(default_quality, ushort, 0644);
66 MODULE_PARM_DESC(default_quality,
67 		 "default entropy content of hwrng per mill");
68 
69 static void start_khwrngd(void);
70 
71 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
72 			       int wait);
73 
74 static size_t rng_buffer_size(void)
75 {
76 	return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES;
77 }
78 
79 static void add_early_randomness(struct hwrng *rng)
80 {
81 	unsigned char bytes[16];
82 	int bytes_read;
83 
84 	/*
85 	 * Currently only virtio-rng cannot return data during device
86 	 * probe, and that's handled in virtio-rng.c itself.  If there
87 	 * are more such devices, this call to rng_get_data can be
88 	 * made conditional here instead of doing it per-device.
89 	 */
90 	bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1);
91 	if (bytes_read > 0)
92 		add_device_randomness(bytes, bytes_read);
93 }
94 
95 static inline int hwrng_init(struct hwrng *rng)
96 {
97 	if (rng->init) {
98 		int ret;
99 
100 		ret =  rng->init(rng);
101 		if (ret)
102 			return ret;
103 	}
104 	add_early_randomness(rng);
105 
106 	current_quality = rng->quality ? : default_quality;
107 	current_quality &= 1023;
108 
109 	if (current_quality == 0 && hwrng_fill)
110 		kthread_stop(hwrng_fill);
111 	if (current_quality > 0 && !hwrng_fill)
112 		start_khwrngd();
113 
114 	return 0;
115 }
116 
117 static inline void hwrng_cleanup(struct hwrng *rng)
118 {
119 	if (rng && rng->cleanup)
120 		rng->cleanup(rng);
121 }
122 
123 static int rng_dev_open(struct inode *inode, struct file *filp)
124 {
125 	/* enforce read-only access to this chrdev */
126 	if ((filp->f_mode & FMODE_READ) == 0)
127 		return -EINVAL;
128 	if (filp->f_mode & FMODE_WRITE)
129 		return -EINVAL;
130 	return 0;
131 }
132 
133 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
134 			int wait) {
135 	int present;
136 
137 	if (rng->read)
138 		return rng->read(rng, (void *)buffer, size, wait);
139 
140 	if (rng->data_present)
141 		present = rng->data_present(rng, wait);
142 	else
143 		present = 1;
144 
145 	if (present)
146 		return rng->data_read(rng, (u32 *)buffer);
147 
148 	return 0;
149 }
150 
151 static ssize_t rng_dev_read(struct file *filp, char __user *buf,
152 			    size_t size, loff_t *offp)
153 {
154 	ssize_t ret = 0;
155 	int err = 0;
156 	int bytes_read, len;
157 
158 	while (size) {
159 		if (mutex_lock_interruptible(&rng_mutex)) {
160 			err = -ERESTARTSYS;
161 			goto out;
162 		}
163 
164 		if (!current_rng) {
165 			err = -ENODEV;
166 			goto out_unlock;
167 		}
168 
169 		if (!data_avail) {
170 			bytes_read = rng_get_data(current_rng, rng_buffer,
171 				rng_buffer_size(),
172 				!(filp->f_flags & O_NONBLOCK));
173 			if (bytes_read < 0) {
174 				err = bytes_read;
175 				goto out_unlock;
176 			}
177 			data_avail = bytes_read;
178 		}
179 
180 		if (!data_avail) {
181 			if (filp->f_flags & O_NONBLOCK) {
182 				err = -EAGAIN;
183 				goto out_unlock;
184 			}
185 		} else {
186 			len = data_avail;
187 			if (len > size)
188 				len = size;
189 
190 			data_avail -= len;
191 
192 			if (copy_to_user(buf + ret, rng_buffer + data_avail,
193 								len)) {
194 				err = -EFAULT;
195 				goto out_unlock;
196 			}
197 
198 			size -= len;
199 			ret += len;
200 		}
201 
202 		mutex_unlock(&rng_mutex);
203 
204 		if (need_resched())
205 			schedule_timeout_interruptible(1);
206 
207 		if (signal_pending(current)) {
208 			err = -ERESTARTSYS;
209 			goto out;
210 		}
211 	}
212 out:
213 	return ret ? : err;
214 out_unlock:
215 	mutex_unlock(&rng_mutex);
216 	goto out;
217 }
218 
219 
220 static const struct file_operations rng_chrdev_ops = {
221 	.owner		= THIS_MODULE,
222 	.open		= rng_dev_open,
223 	.read		= rng_dev_read,
224 	.llseek		= noop_llseek,
225 };
226 
227 static struct miscdevice rng_miscdev = {
228 	.minor		= RNG_MISCDEV_MINOR,
229 	.name		= RNG_MODULE_NAME,
230 	.nodename	= "hwrng",
231 	.fops		= &rng_chrdev_ops,
232 };
233 
234 
235 static ssize_t hwrng_attr_current_store(struct device *dev,
236 					struct device_attribute *attr,
237 					const char *buf, size_t len)
238 {
239 	int err;
240 	struct hwrng *rng;
241 
242 	err = mutex_lock_interruptible(&rng_mutex);
243 	if (err)
244 		return -ERESTARTSYS;
245 	err = -ENODEV;
246 	list_for_each_entry(rng, &rng_list, list) {
247 		if (strcmp(rng->name, buf) == 0) {
248 			if (rng == current_rng) {
249 				err = 0;
250 				break;
251 			}
252 			err = hwrng_init(rng);
253 			if (err)
254 				break;
255 			hwrng_cleanup(current_rng);
256 			current_rng = rng;
257 			err = 0;
258 			break;
259 		}
260 	}
261 	mutex_unlock(&rng_mutex);
262 
263 	return err ? : len;
264 }
265 
266 static ssize_t hwrng_attr_current_show(struct device *dev,
267 				       struct device_attribute *attr,
268 				       char *buf)
269 {
270 	int err;
271 	ssize_t ret;
272 	const char *name = "none";
273 
274 	err = mutex_lock_interruptible(&rng_mutex);
275 	if (err)
276 		return -ERESTARTSYS;
277 	if (current_rng)
278 		name = current_rng->name;
279 	ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
280 	mutex_unlock(&rng_mutex);
281 
282 	return ret;
283 }
284 
285 static ssize_t hwrng_attr_available_show(struct device *dev,
286 					 struct device_attribute *attr,
287 					 char *buf)
288 {
289 	int err;
290 	ssize_t ret = 0;
291 	struct hwrng *rng;
292 
293 	err = mutex_lock_interruptible(&rng_mutex);
294 	if (err)
295 		return -ERESTARTSYS;
296 	buf[0] = '\0';
297 	list_for_each_entry(rng, &rng_list, list) {
298 		strncat(buf, rng->name, PAGE_SIZE - ret - 1);
299 		ret += strlen(rng->name);
300 		strncat(buf, " ", PAGE_SIZE - ret - 1);
301 		ret++;
302 	}
303 	strncat(buf, "\n", PAGE_SIZE - ret - 1);
304 	ret++;
305 	mutex_unlock(&rng_mutex);
306 
307 	return ret;
308 }
309 
310 static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
311 		   hwrng_attr_current_show,
312 		   hwrng_attr_current_store);
313 static DEVICE_ATTR(rng_available, S_IRUGO,
314 		   hwrng_attr_available_show,
315 		   NULL);
316 
317 
318 static void unregister_miscdev(void)
319 {
320 	device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
321 	device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
322 	misc_deregister(&rng_miscdev);
323 }
324 
325 static int register_miscdev(void)
326 {
327 	int err;
328 
329 	err = misc_register(&rng_miscdev);
330 	if (err)
331 		goto out;
332 	err = device_create_file(rng_miscdev.this_device,
333 				 &dev_attr_rng_current);
334 	if (err)
335 		goto err_misc_dereg;
336 	err = device_create_file(rng_miscdev.this_device,
337 				 &dev_attr_rng_available);
338 	if (err)
339 		goto err_remove_current;
340 out:
341 	return err;
342 
343 err_remove_current:
344 	device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
345 err_misc_dereg:
346 	misc_deregister(&rng_miscdev);
347 	goto out;
348 }
349 
350 static int hwrng_fillfn(void *unused)
351 {
352 	long rc;
353 
354 	while (!kthread_should_stop()) {
355 		if (!current_rng)
356 			break;
357 		rc = rng_get_data(current_rng, rng_fillbuf,
358 				  rng_buffer_size(), 1);
359 		if (rc <= 0) {
360 			pr_warn("hwrng: no data available\n");
361 			msleep_interruptible(10000);
362 			continue;
363 		}
364 		add_hwgenerator_randomness((void *)rng_fillbuf, rc,
365 					   rc * current_quality * 8 >> 10);
366 	}
367 	hwrng_fill = NULL;
368 	return 0;
369 }
370 
371 static void start_khwrngd(void)
372 {
373 	hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");
374 	if (hwrng_fill == ERR_PTR(-ENOMEM)) {
375 		pr_err("hwrng_fill thread creation failed");
376 		hwrng_fill = NULL;
377 	}
378 }
379 
380 int hwrng_register(struct hwrng *rng)
381 {
382 	int err = -EINVAL;
383 	struct hwrng *old_rng, *tmp;
384 
385 	if (rng->name == NULL ||
386 	    (rng->data_read == NULL && rng->read == NULL))
387 		goto out;
388 
389 	mutex_lock(&rng_mutex);
390 
391 	/* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
392 	err = -ENOMEM;
393 	if (!rng_buffer) {
394 		rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
395 		if (!rng_buffer)
396 			goto out_unlock;
397 	}
398 	if (!rng_fillbuf) {
399 		rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL);
400 		if (!rng_fillbuf) {
401 			kfree(rng_buffer);
402 			goto out_unlock;
403 		}
404 	}
405 
406 	/* Must not register two RNGs with the same name. */
407 	err = -EEXIST;
408 	list_for_each_entry(tmp, &rng_list, list) {
409 		if (strcmp(tmp->name, rng->name) == 0)
410 			goto out_unlock;
411 	}
412 
413 	old_rng = current_rng;
414 	if (!old_rng) {
415 		err = hwrng_init(rng);
416 		if (err)
417 			goto out_unlock;
418 		current_rng = rng;
419 	}
420 	err = 0;
421 	if (!old_rng) {
422 		err = register_miscdev();
423 		if (err) {
424 			hwrng_cleanup(rng);
425 			current_rng = NULL;
426 			goto out_unlock;
427 		}
428 	}
429 	INIT_LIST_HEAD(&rng->list);
430 	list_add_tail(&rng->list, &rng_list);
431 
432 	if (old_rng && !rng->init) {
433 		/*
434 		 * Use a new device's input to add some randomness to
435 		 * the system.  If this rng device isn't going to be
436 		 * used right away, its init function hasn't been
437 		 * called yet; so only use the randomness from devices
438 		 * that don't need an init callback.
439 		 */
440 		add_early_randomness(rng);
441 	}
442 
443 out_unlock:
444 	mutex_unlock(&rng_mutex);
445 out:
446 	return err;
447 }
448 EXPORT_SYMBOL_GPL(hwrng_register);
449 
450 void hwrng_unregister(struct hwrng *rng)
451 {
452 	int err;
453 
454 	mutex_lock(&rng_mutex);
455 
456 	list_del(&rng->list);
457 	if (current_rng == rng) {
458 		hwrng_cleanup(rng);
459 		if (list_empty(&rng_list)) {
460 			current_rng = NULL;
461 		} else {
462 			current_rng = list_entry(rng_list.prev, struct hwrng, list);
463 			err = hwrng_init(current_rng);
464 			if (err)
465 				current_rng = NULL;
466 		}
467 	}
468 	if (list_empty(&rng_list)) {
469 		unregister_miscdev();
470 		if (hwrng_fill)
471 			kthread_stop(hwrng_fill);
472 	}
473 
474 	mutex_unlock(&rng_mutex);
475 }
476 EXPORT_SYMBOL_GPL(hwrng_unregister);
477 
478 static void __exit hwrng_exit(void)
479 {
480 	mutex_lock(&rng_mutex);
481 	BUG_ON(current_rng);
482 	kfree(rng_buffer);
483 	kfree(rng_fillbuf);
484 	mutex_unlock(&rng_mutex);
485 }
486 
487 module_exit(hwrng_exit);
488 
489 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
490 MODULE_LICENSE("GPL");
491