chaoskey.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * chaoskey - driver for ChaosKey device from Altus Metrum.
  3. *
  4. * This device provides true random numbers using a noise source based
  5. * on a reverse-biased p-n junction in avalanche breakdown. More
  6. * details can be found at http://chaoskey.org
  7. *
  8. * The driver connects to the kernel hardware RNG interface to provide
  9. * entropy for /dev/random and other kernel activities. It also offers
  10. * a separate /dev/ entry to allow for direct access to the random
  11. * bit stream.
  12. *
  13. * Copyright © 2015 Keith Packard <keithp@keithp.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; version 2 of the License.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <linux/usb.h>
  27. #include <linux/wait.h>
  28. #include <linux/hw_random.h>
  29. #include <linux/mutex.h>
  30. #include <linux/uaccess.h>
  31. static struct usb_driver chaoskey_driver;
  32. static struct usb_class_driver chaoskey_class;
  33. static int chaoskey_rng_read(struct hwrng *rng, void *data,
  34. size_t max, bool wait);
  35. #define usb_dbg(usb_if, format, arg...) \
  36. dev_dbg(&(usb_if)->dev, format, ## arg)
  37. #define usb_err(usb_if, format, arg...) \
  38. dev_err(&(usb_if)->dev, format, ## arg)
  39. /* Version Information */
  40. #define DRIVER_VERSION "v0.1"
  41. #define DRIVER_AUTHOR "Keith Packard, keithp@keithp.com"
  42. #define DRIVER_DESC "Altus Metrum ChaosKey driver"
  43. #define DRIVER_SHORT "chaoskey"
  44. MODULE_VERSION(DRIVER_VERSION);
  45. MODULE_AUTHOR(DRIVER_AUTHOR);
  46. MODULE_DESCRIPTION(DRIVER_DESC);
  47. MODULE_LICENSE("GPL");
  48. #define CHAOSKEY_VENDOR_ID 0x1d50 /* OpenMoko */
  49. #define CHAOSKEY_PRODUCT_ID 0x60c6 /* ChaosKey */
  50. #define CHAOSKEY_BUF_LEN 64 /* max size of USB full speed packet */
  51. #define NAK_TIMEOUT (HZ) /* stall/wait timeout for device */
  52. #ifdef CONFIG_USB_DYNAMIC_MINORS
  53. #define USB_CHAOSKEY_MINOR_BASE 0
  54. #else
  55. /* IOWARRIOR_MINOR_BASE + 16, not official yet */
  56. #define USB_CHAOSKEY_MINOR_BASE 224
  57. #endif
  58. static const struct usb_device_id chaoskey_table[] = {
  59. { USB_DEVICE(CHAOSKEY_VENDOR_ID, CHAOSKEY_PRODUCT_ID) },
  60. { },
  61. };
  62. MODULE_DEVICE_TABLE(usb, chaoskey_table);
  63. /* Driver-local specific stuff */
  64. struct chaoskey {
  65. struct usb_interface *interface;
  66. char in_ep;
  67. struct mutex lock;
  68. struct mutex rng_lock;
  69. int open; /* open count */
  70. int present; /* device not disconnected */
  71. int size; /* size of buf */
  72. int valid; /* bytes of buf read */
  73. int used; /* bytes of buf consumed */
  74. char *name; /* product + serial */
  75. struct hwrng hwrng; /* Embedded struct for hwrng */
  76. int hwrng_registered; /* registered with hwrng API */
  77. wait_queue_head_t wait_q; /* for timeouts */
  78. char *buf;
  79. };
  80. static void chaoskey_free(struct chaoskey *dev)
  81. {
  82. usb_dbg(dev->interface, "free");
  83. kfree(dev->name);
  84. kfree(dev->buf);
  85. kfree(dev);
  86. }
  87. static int chaoskey_probe(struct usb_interface *interface,
  88. const struct usb_device_id *id)
  89. {
  90. struct usb_device *udev = interface_to_usbdev(interface);
  91. struct usb_host_interface *altsetting = interface->cur_altsetting;
  92. int i;
  93. int in_ep = -1;
  94. struct chaoskey *dev;
  95. int result;
  96. int size;
  97. usb_dbg(interface, "probe %s-%s", udev->product, udev->serial);
  98. /* Find the first bulk IN endpoint and its packet size */
  99. for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
  100. if (usb_endpoint_is_bulk_in(&altsetting->endpoint[i].desc)) {
  101. in_ep = usb_endpoint_num(&altsetting->endpoint[i].desc);
  102. size = usb_endpoint_maxp(&altsetting->endpoint[i].desc);
  103. break;
  104. }
  105. }
  106. /* Validate endpoint and size */
  107. if (in_ep == -1) {
  108. usb_dbg(interface, "no IN endpoint found");
  109. return -ENODEV;
  110. }
  111. if (size <= 0) {
  112. usb_dbg(interface, "invalid size (%d)", size);
  113. return -ENODEV;
  114. }
  115. if (size > CHAOSKEY_BUF_LEN) {
  116. usb_dbg(interface, "size reduced from %d to %d\n",
  117. size, CHAOSKEY_BUF_LEN);
  118. size = CHAOSKEY_BUF_LEN;
  119. }
  120. /* Looks good, allocate and initialize */
  121. dev = kzalloc(sizeof(struct chaoskey), GFP_KERNEL);
  122. if (dev == NULL)
  123. return -ENOMEM;
  124. dev->buf = kmalloc(size, GFP_KERNEL);
  125. if (dev->buf == NULL) {
  126. kfree(dev);
  127. return -ENOMEM;
  128. }
  129. /* Construct a name using the product and serial values. Each
  130. * device needs a unique name for the hwrng code
  131. */
  132. if (udev->product && udev->serial) {
  133. dev->name = kmalloc(strlen(udev->product) + 1 +
  134. strlen(udev->serial) + 1, GFP_KERNEL);
  135. if (dev->name == NULL) {
  136. kfree(dev->buf);
  137. kfree(dev);
  138. return -ENOMEM;
  139. }
  140. strcpy(dev->name, udev->product);
  141. strcat(dev->name, "-");
  142. strcat(dev->name, udev->serial);
  143. }
  144. dev->interface = interface;
  145. dev->in_ep = in_ep;
  146. dev->size = size;
  147. dev->present = 1;
  148. init_waitqueue_head(&dev->wait_q);
  149. mutex_init(&dev->lock);
  150. mutex_init(&dev->rng_lock);
  151. usb_set_intfdata(interface, dev);
  152. result = usb_register_dev(interface, &chaoskey_class);
  153. if (result) {
  154. usb_err(interface, "Unable to allocate minor number.");
  155. usb_set_intfdata(interface, NULL);
  156. chaoskey_free(dev);
  157. return result;
  158. }
  159. dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
  160. dev->hwrng.read = chaoskey_rng_read;
  161. /* Set the 'quality' metric. Quality is measured in units of
  162. * 1/1024's of a bit ("mills"). This should be set to 1024,
  163. * but there is a bug in the hwrng core which masks it with
  164. * 1023.
  165. *
  166. * The patch that has been merged to the crypto development
  167. * tree for that bug limits the value to 1024 at most, so by
  168. * setting this to 1024 + 1023, we get 1023 before the fix is
  169. * merged and 1024 afterwards. We'll patch this driver once
  170. * both bits of code are in the same tree.
  171. */
  172. dev->hwrng.quality = 1024 + 1023;
  173. dev->hwrng_registered = (hwrng_register(&dev->hwrng) == 0);
  174. if (!dev->hwrng_registered)
  175. usb_err(interface, "Unable to register with hwrng");
  176. usb_enable_autosuspend(udev);
  177. usb_dbg(interface, "chaoskey probe success, size %d", dev->size);
  178. return 0;
  179. }
  180. static void chaoskey_disconnect(struct usb_interface *interface)
  181. {
  182. struct chaoskey *dev;
  183. usb_dbg(interface, "disconnect");
  184. dev = usb_get_intfdata(interface);
  185. if (!dev) {
  186. usb_dbg(interface, "disconnect failed - no dev");
  187. return;
  188. }
  189. if (dev->hwrng_registered)
  190. hwrng_unregister(&dev->hwrng);
  191. usb_deregister_dev(interface, &chaoskey_class);
  192. usb_set_intfdata(interface, NULL);
  193. mutex_lock(&dev->lock);
  194. dev->present = 0;
  195. if (!dev->open) {
  196. mutex_unlock(&dev->lock);
  197. chaoskey_free(dev);
  198. } else
  199. mutex_unlock(&dev->lock);
  200. usb_dbg(interface, "disconnect done");
  201. }
  202. static int chaoskey_open(struct inode *inode, struct file *file)
  203. {
  204. struct chaoskey *dev;
  205. struct usb_interface *interface;
  206. /* get the interface from minor number and driver information */
  207. interface = usb_find_interface(&chaoskey_driver, iminor(inode));
  208. if (!interface)
  209. return -ENODEV;
  210. usb_dbg(interface, "open");
  211. dev = usb_get_intfdata(interface);
  212. if (!dev) {
  213. usb_dbg(interface, "open (dev)");
  214. return -ENODEV;
  215. }
  216. file->private_data = dev;
  217. mutex_lock(&dev->lock);
  218. ++dev->open;
  219. mutex_unlock(&dev->lock);
  220. usb_dbg(interface, "open success");
  221. return 0;
  222. }
  223. static int chaoskey_release(struct inode *inode, struct file *file)
  224. {
  225. struct chaoskey *dev = file->private_data;
  226. struct usb_interface *interface;
  227. if (dev == NULL)
  228. return -ENODEV;
  229. interface = dev->interface;
  230. usb_dbg(interface, "release");
  231. mutex_lock(&dev->lock);
  232. usb_dbg(interface, "open count at release is %d", dev->open);
  233. if (dev->open <= 0) {
  234. usb_dbg(interface, "invalid open count (%d)", dev->open);
  235. mutex_unlock(&dev->lock);
  236. return -ENODEV;
  237. }
  238. --dev->open;
  239. if (!dev->present) {
  240. if (dev->open == 0) {
  241. mutex_unlock(&dev->lock);
  242. chaoskey_free(dev);
  243. } else
  244. mutex_unlock(&dev->lock);
  245. } else
  246. mutex_unlock(&dev->lock);
  247. usb_dbg(interface, "release success");
  248. return 0;
  249. }
  250. /* Fill the buffer. Called with dev->lock held
  251. */
  252. static int _chaoskey_fill(struct chaoskey *dev)
  253. {
  254. DEFINE_WAIT(wait);
  255. int result;
  256. int this_read;
  257. struct usb_device *udev = interface_to_usbdev(dev->interface);
  258. usb_dbg(dev->interface, "fill");
  259. /* Return immediately if someone called before the buffer was
  260. * empty */
  261. if (dev->valid != dev->used) {
  262. usb_dbg(dev->interface, "not empty yet (valid %d used %d)",
  263. dev->valid, dev->used);
  264. return 0;
  265. }
  266. /* Bail if the device has been removed */
  267. if (!dev->present) {
  268. usb_dbg(dev->interface, "device not present");
  269. return -ENODEV;
  270. }
  271. /* Make sure the device is awake */
  272. result = usb_autopm_get_interface(dev->interface);
  273. if (result) {
  274. usb_dbg(dev->interface, "wakeup failed (result %d)", result);
  275. return result;
  276. }
  277. result = usb_bulk_msg(udev,
  278. usb_rcvbulkpipe(udev, dev->in_ep),
  279. dev->buf, dev->size, &this_read,
  280. NAK_TIMEOUT);
  281. /* Let the device go back to sleep eventually */
  282. usb_autopm_put_interface(dev->interface);
  283. if (result == 0) {
  284. dev->valid = this_read;
  285. dev->used = 0;
  286. }
  287. usb_dbg(dev->interface, "bulk_msg result %d this_read %d",
  288. result, this_read);
  289. return result;
  290. }
  291. static ssize_t chaoskey_read(struct file *file,
  292. char __user *buffer,
  293. size_t count,
  294. loff_t *ppos)
  295. {
  296. struct chaoskey *dev;
  297. ssize_t read_count = 0;
  298. int this_time;
  299. int result = 0;
  300. unsigned long remain;
  301. dev = file->private_data;
  302. if (dev == NULL || !dev->present)
  303. return -ENODEV;
  304. usb_dbg(dev->interface, "read %zu", count);
  305. while (count > 0) {
  306. /* Grab the rng_lock briefly to ensure that the hwrng interface
  307. * gets priority over other user access
  308. */
  309. result = mutex_lock_interruptible(&dev->rng_lock);
  310. if (result)
  311. goto bail;
  312. mutex_unlock(&dev->rng_lock);
  313. result = mutex_lock_interruptible(&dev->lock);
  314. if (result)
  315. goto bail;
  316. if (dev->valid == dev->used) {
  317. result = _chaoskey_fill(dev);
  318. if (result) {
  319. mutex_unlock(&dev->lock);
  320. goto bail;
  321. }
  322. /* Read returned zero bytes */
  323. if (dev->used == dev->valid) {
  324. mutex_unlock(&dev->lock);
  325. goto bail;
  326. }
  327. }
  328. this_time = dev->valid - dev->used;
  329. if (this_time > count)
  330. this_time = count;
  331. remain = copy_to_user(buffer, dev->buf + dev->used, this_time);
  332. if (remain) {
  333. result = -EFAULT;
  334. /* Consume the bytes that were copied so we don't leak
  335. * data to user space
  336. */
  337. dev->used += this_time - remain;
  338. mutex_unlock(&dev->lock);
  339. goto bail;
  340. }
  341. count -= this_time;
  342. read_count += this_time;
  343. buffer += this_time;
  344. dev->used += this_time;
  345. mutex_unlock(&dev->lock);
  346. }
  347. bail:
  348. if (read_count) {
  349. usb_dbg(dev->interface, "read %zu bytes", read_count);
  350. return read_count;
  351. }
  352. usb_dbg(dev->interface, "empty read, result %d", result);
  353. return result;
  354. }
  355. static int chaoskey_rng_read(struct hwrng *rng, void *data,
  356. size_t max, bool wait)
  357. {
  358. struct chaoskey *dev = container_of(rng, struct chaoskey, hwrng);
  359. int this_time;
  360. usb_dbg(dev->interface, "rng_read max %zu wait %d", max, wait);
  361. if (!dev->present) {
  362. usb_dbg(dev->interface, "device not present");
  363. return 0;
  364. }
  365. /* Hold the rng_lock until we acquire the device lock so that
  366. * this operation gets priority over other user access to the
  367. * device
  368. */
  369. mutex_lock(&dev->rng_lock);
  370. mutex_lock(&dev->lock);
  371. mutex_unlock(&dev->rng_lock);
  372. /* Try to fill the buffer if empty. It doesn't actually matter
  373. * if _chaoskey_fill works; we'll just return zero bytes as
  374. * the buffer will still be empty
  375. */
  376. if (dev->valid == dev->used)
  377. (void) _chaoskey_fill(dev);
  378. this_time = dev->valid - dev->used;
  379. if (this_time > max)
  380. this_time = max;
  381. memcpy(data, dev->buf + dev->used, this_time);
  382. dev->used += this_time;
  383. mutex_unlock(&dev->lock);
  384. usb_dbg(dev->interface, "rng_read this_time %d\n", this_time);
  385. return this_time;
  386. }
  387. #ifdef CONFIG_PM
  388. static int chaoskey_suspend(struct usb_interface *interface,
  389. pm_message_t message)
  390. {
  391. usb_dbg(interface, "suspend");
  392. return 0;
  393. }
  394. static int chaoskey_resume(struct usb_interface *interface)
  395. {
  396. usb_dbg(interface, "resume");
  397. return 0;
  398. }
  399. #else
  400. #define chaoskey_suspend NULL
  401. #define chaoskey_resume NULL
  402. #endif
  403. /* file operation pointers */
  404. static const struct file_operations chaoskey_fops = {
  405. .owner = THIS_MODULE,
  406. .read = chaoskey_read,
  407. .open = chaoskey_open,
  408. .release = chaoskey_release,
  409. .llseek = default_llseek,
  410. };
  411. /* class driver information */
  412. static struct usb_class_driver chaoskey_class = {
  413. .name = "chaoskey%d",
  414. .fops = &chaoskey_fops,
  415. .minor_base = USB_CHAOSKEY_MINOR_BASE,
  416. };
  417. /* usb specific object needed to register this driver with the usb subsystem */
  418. static struct usb_driver chaoskey_driver = {
  419. .name = DRIVER_SHORT,
  420. .probe = chaoskey_probe,
  421. .disconnect = chaoskey_disconnect,
  422. .suspend = chaoskey_suspend,
  423. .resume = chaoskey_resume,
  424. .reset_resume = chaoskey_resume,
  425. .id_table = chaoskey_table,
  426. .supports_autosuspend = 1,
  427. };
  428. module_usb_driver(chaoskey_driver);