chaoskey.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 ALEA_VENDOR_ID 0x12d8 /* Araneus */
  51. #define ALEA_PRODUCT_ID 0x0001 /* Alea I */
  52. #define CHAOSKEY_BUF_LEN 64 /* max size of USB full speed packet */
  53. #define NAK_TIMEOUT (HZ) /* normal stall/wait timeout */
  54. #define ALEA_FIRST_TIMEOUT (HZ*3) /* first stall/wait timeout for Alea */
  55. #ifdef CONFIG_USB_DYNAMIC_MINORS
  56. #define USB_CHAOSKEY_MINOR_BASE 0
  57. #else
  58. /* IOWARRIOR_MINOR_BASE + 16, not official yet */
  59. #define USB_CHAOSKEY_MINOR_BASE 224
  60. #endif
  61. static const struct usb_device_id chaoskey_table[] = {
  62. { USB_DEVICE(CHAOSKEY_VENDOR_ID, CHAOSKEY_PRODUCT_ID) },
  63. { USB_DEVICE(ALEA_VENDOR_ID, ALEA_PRODUCT_ID) },
  64. { },
  65. };
  66. MODULE_DEVICE_TABLE(usb, chaoskey_table);
  67. static void chaos_read_callback(struct urb *urb);
  68. /* Driver-local specific stuff */
  69. struct chaoskey {
  70. struct usb_interface *interface;
  71. char in_ep;
  72. struct mutex lock;
  73. struct mutex rng_lock;
  74. int open; /* open count */
  75. bool present; /* device not disconnected */
  76. bool reading; /* ongoing IO */
  77. bool reads_started; /* track first read for Alea */
  78. int size; /* size of buf */
  79. int valid; /* bytes of buf read */
  80. int used; /* bytes of buf consumed */
  81. char *name; /* product + serial */
  82. struct hwrng hwrng; /* Embedded struct for hwrng */
  83. int hwrng_registered; /* registered with hwrng API */
  84. wait_queue_head_t wait_q; /* for timeouts */
  85. struct urb *urb; /* for performing IO */
  86. char *buf;
  87. };
  88. static void chaoskey_free(struct chaoskey *dev)
  89. {
  90. if (dev) {
  91. usb_dbg(dev->interface, "free");
  92. usb_free_urb(dev->urb);
  93. kfree(dev->name);
  94. kfree(dev->buf);
  95. kfree(dev);
  96. }
  97. }
  98. static int chaoskey_probe(struct usb_interface *interface,
  99. const struct usb_device_id *id)
  100. {
  101. struct usb_device *udev = interface_to_usbdev(interface);
  102. struct usb_host_interface *altsetting = interface->cur_altsetting;
  103. int i;
  104. int in_ep = -1;
  105. struct chaoskey *dev;
  106. int result = -ENOMEM;
  107. int size;
  108. usb_dbg(interface, "probe %s-%s", udev->product, udev->serial);
  109. /* Find the first bulk IN endpoint and its packet size */
  110. for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
  111. if (usb_endpoint_is_bulk_in(&altsetting->endpoint[i].desc)) {
  112. in_ep = usb_endpoint_num(&altsetting->endpoint[i].desc);
  113. size = usb_endpoint_maxp(&altsetting->endpoint[i].desc);
  114. break;
  115. }
  116. }
  117. /* Validate endpoint and size */
  118. if (in_ep == -1) {
  119. usb_dbg(interface, "no IN endpoint found");
  120. return -ENODEV;
  121. }
  122. if (size <= 0) {
  123. usb_dbg(interface, "invalid size (%d)", size);
  124. return -ENODEV;
  125. }
  126. if (size > CHAOSKEY_BUF_LEN) {
  127. usb_dbg(interface, "size reduced from %d to %d\n",
  128. size, CHAOSKEY_BUF_LEN);
  129. size = CHAOSKEY_BUF_LEN;
  130. }
  131. /* Looks good, allocate and initialize */
  132. dev = kzalloc(sizeof(struct chaoskey), GFP_KERNEL);
  133. if (dev == NULL)
  134. goto out;
  135. dev->buf = kmalloc(size, GFP_KERNEL);
  136. if (dev->buf == NULL)
  137. goto out;
  138. dev->urb = usb_alloc_urb(0, GFP_KERNEL);
  139. if (!dev->urb)
  140. goto out;
  141. usb_fill_bulk_urb(dev->urb,
  142. udev,
  143. usb_rcvbulkpipe(udev, in_ep),
  144. dev->buf,
  145. size,
  146. chaos_read_callback,
  147. dev);
  148. /* Construct a name using the product and serial values. Each
  149. * device needs a unique name for the hwrng code
  150. */
  151. if (udev->product && udev->serial) {
  152. dev->name = kmalloc(strlen(udev->product) + 1 +
  153. strlen(udev->serial) + 1, GFP_KERNEL);
  154. if (dev->name == NULL)
  155. goto out;
  156. strcpy(dev->name, udev->product);
  157. strcat(dev->name, "-");
  158. strcat(dev->name, udev->serial);
  159. }
  160. dev->interface = interface;
  161. dev->in_ep = in_ep;
  162. if (le16_to_cpu(udev->descriptor.idVendor) != ALEA_VENDOR_ID)
  163. dev->reads_started = 1;
  164. dev->size = size;
  165. dev->present = 1;
  166. init_waitqueue_head(&dev->wait_q);
  167. mutex_init(&dev->lock);
  168. mutex_init(&dev->rng_lock);
  169. usb_set_intfdata(interface, dev);
  170. result = usb_register_dev(interface, &chaoskey_class);
  171. if (result) {
  172. usb_err(interface, "Unable to allocate minor number.");
  173. goto out;
  174. }
  175. dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
  176. dev->hwrng.read = chaoskey_rng_read;
  177. /* Set the 'quality' metric. Quality is measured in units of
  178. * 1/1024's of a bit ("mills"). This should be set to 1024,
  179. * but there is a bug in the hwrng core which masks it with
  180. * 1023.
  181. *
  182. * The patch that has been merged to the crypto development
  183. * tree for that bug limits the value to 1024 at most, so by
  184. * setting this to 1024 + 1023, we get 1023 before the fix is
  185. * merged and 1024 afterwards. We'll patch this driver once
  186. * both bits of code are in the same tree.
  187. */
  188. dev->hwrng.quality = 1024 + 1023;
  189. dev->hwrng_registered = (hwrng_register(&dev->hwrng) == 0);
  190. if (!dev->hwrng_registered)
  191. usb_err(interface, "Unable to register with hwrng");
  192. usb_enable_autosuspend(udev);
  193. usb_dbg(interface, "chaoskey probe success, size %d", dev->size);
  194. return 0;
  195. out:
  196. usb_set_intfdata(interface, NULL);
  197. chaoskey_free(dev);
  198. return result;
  199. }
  200. static void chaoskey_disconnect(struct usb_interface *interface)
  201. {
  202. struct chaoskey *dev;
  203. usb_dbg(interface, "disconnect");
  204. dev = usb_get_intfdata(interface);
  205. if (!dev) {
  206. usb_dbg(interface, "disconnect failed - no dev");
  207. return;
  208. }
  209. if (dev->hwrng_registered)
  210. hwrng_unregister(&dev->hwrng);
  211. usb_deregister_dev(interface, &chaoskey_class);
  212. usb_set_intfdata(interface, NULL);
  213. mutex_lock(&dev->lock);
  214. dev->present = 0;
  215. usb_poison_urb(dev->urb);
  216. if (!dev->open) {
  217. mutex_unlock(&dev->lock);
  218. chaoskey_free(dev);
  219. } else
  220. mutex_unlock(&dev->lock);
  221. usb_dbg(interface, "disconnect done");
  222. }
  223. static int chaoskey_open(struct inode *inode, struct file *file)
  224. {
  225. struct chaoskey *dev;
  226. struct usb_interface *interface;
  227. /* get the interface from minor number and driver information */
  228. interface = usb_find_interface(&chaoskey_driver, iminor(inode));
  229. if (!interface)
  230. return -ENODEV;
  231. usb_dbg(interface, "open");
  232. dev = usb_get_intfdata(interface);
  233. if (!dev) {
  234. usb_dbg(interface, "open (dev)");
  235. return -ENODEV;
  236. }
  237. file->private_data = dev;
  238. mutex_lock(&dev->lock);
  239. ++dev->open;
  240. mutex_unlock(&dev->lock);
  241. usb_dbg(interface, "open success");
  242. return 0;
  243. }
  244. static int chaoskey_release(struct inode *inode, struct file *file)
  245. {
  246. struct chaoskey *dev = file->private_data;
  247. struct usb_interface *interface;
  248. if (dev == NULL)
  249. return -ENODEV;
  250. interface = dev->interface;
  251. usb_dbg(interface, "release");
  252. mutex_lock(&dev->lock);
  253. usb_dbg(interface, "open count at release is %d", dev->open);
  254. if (dev->open <= 0) {
  255. usb_dbg(interface, "invalid open count (%d)", dev->open);
  256. mutex_unlock(&dev->lock);
  257. return -ENODEV;
  258. }
  259. --dev->open;
  260. if (!dev->present) {
  261. if (dev->open == 0) {
  262. mutex_unlock(&dev->lock);
  263. chaoskey_free(dev);
  264. } else
  265. mutex_unlock(&dev->lock);
  266. } else
  267. mutex_unlock(&dev->lock);
  268. usb_dbg(interface, "release success");
  269. return 0;
  270. }
  271. static void chaos_read_callback(struct urb *urb)
  272. {
  273. struct chaoskey *dev = urb->context;
  274. int status = urb->status;
  275. usb_dbg(dev->interface, "callback status (%d)", status);
  276. if (status == 0)
  277. dev->valid = urb->actual_length;
  278. else
  279. dev->valid = 0;
  280. dev->used = 0;
  281. /* must be seen first before validity is announced */
  282. smp_wmb();
  283. dev->reading = false;
  284. wake_up(&dev->wait_q);
  285. }
  286. /* Fill the buffer. Called with dev->lock held
  287. */
  288. static int _chaoskey_fill(struct chaoskey *dev)
  289. {
  290. DEFINE_WAIT(wait);
  291. int result;
  292. bool started;
  293. usb_dbg(dev->interface, "fill");
  294. /* Return immediately if someone called before the buffer was
  295. * empty */
  296. if (dev->valid != dev->used) {
  297. usb_dbg(dev->interface, "not empty yet (valid %d used %d)",
  298. dev->valid, dev->used);
  299. return 0;
  300. }
  301. /* Bail if the device has been removed */
  302. if (!dev->present) {
  303. usb_dbg(dev->interface, "device not present");
  304. return -ENODEV;
  305. }
  306. /* Make sure the device is awake */
  307. result = usb_autopm_get_interface(dev->interface);
  308. if (result) {
  309. usb_dbg(dev->interface, "wakeup failed (result %d)", result);
  310. return result;
  311. }
  312. dev->reading = true;
  313. result = usb_submit_urb(dev->urb, GFP_KERNEL);
  314. if (result < 0) {
  315. result = usb_translate_errors(result);
  316. dev->reading = false;
  317. goto out;
  318. }
  319. /* The first read on the Alea takes a little under 2 seconds.
  320. * Reads after the first read take only a few microseconds
  321. * though. Presumably the entropy-generating circuit needs
  322. * time to ramp up. So, we wait longer on the first read.
  323. */
  324. started = dev->reads_started;
  325. dev->reads_started = true;
  326. result = wait_event_interruptible_timeout(
  327. dev->wait_q,
  328. !dev->reading,
  329. (started ? NAK_TIMEOUT : ALEA_FIRST_TIMEOUT) );
  330. if (result < 0)
  331. goto out;
  332. if (result == 0)
  333. result = -ETIMEDOUT;
  334. else
  335. result = dev->valid;
  336. out:
  337. /* Let the device go back to sleep eventually */
  338. usb_autopm_put_interface(dev->interface);
  339. usb_dbg(dev->interface, "read %d bytes", dev->valid);
  340. return result;
  341. }
  342. static ssize_t chaoskey_read(struct file *file,
  343. char __user *buffer,
  344. size_t count,
  345. loff_t *ppos)
  346. {
  347. struct chaoskey *dev;
  348. ssize_t read_count = 0;
  349. int this_time;
  350. int result = 0;
  351. unsigned long remain;
  352. dev = file->private_data;
  353. if (dev == NULL || !dev->present)
  354. return -ENODEV;
  355. usb_dbg(dev->interface, "read %zu", count);
  356. while (count > 0) {
  357. /* Grab the rng_lock briefly to ensure that the hwrng interface
  358. * gets priority over other user access
  359. */
  360. result = mutex_lock_interruptible(&dev->rng_lock);
  361. if (result)
  362. goto bail;
  363. mutex_unlock(&dev->rng_lock);
  364. result = mutex_lock_interruptible(&dev->lock);
  365. if (result)
  366. goto bail;
  367. if (dev->valid == dev->used) {
  368. result = _chaoskey_fill(dev);
  369. if (result < 0) {
  370. mutex_unlock(&dev->lock);
  371. goto bail;
  372. }
  373. }
  374. this_time = dev->valid - dev->used;
  375. if (this_time > count)
  376. this_time = count;
  377. remain = copy_to_user(buffer, dev->buf + dev->used, this_time);
  378. if (remain) {
  379. result = -EFAULT;
  380. /* Consume the bytes that were copied so we don't leak
  381. * data to user space
  382. */
  383. dev->used += this_time - remain;
  384. mutex_unlock(&dev->lock);
  385. goto bail;
  386. }
  387. count -= this_time;
  388. read_count += this_time;
  389. buffer += this_time;
  390. dev->used += this_time;
  391. mutex_unlock(&dev->lock);
  392. }
  393. bail:
  394. if (read_count) {
  395. usb_dbg(dev->interface, "read %zu bytes", read_count);
  396. return read_count;
  397. }
  398. usb_dbg(dev->interface, "empty read, result %d", result);
  399. if (result == -ETIMEDOUT)
  400. result = -EAGAIN;
  401. return result;
  402. }
  403. static int chaoskey_rng_read(struct hwrng *rng, void *data,
  404. size_t max, bool wait)
  405. {
  406. struct chaoskey *dev = container_of(rng, struct chaoskey, hwrng);
  407. int this_time;
  408. usb_dbg(dev->interface, "rng_read max %zu wait %d", max, wait);
  409. if (!dev->present) {
  410. usb_dbg(dev->interface, "device not present");
  411. return 0;
  412. }
  413. /* Hold the rng_lock until we acquire the device lock so that
  414. * this operation gets priority over other user access to the
  415. * device
  416. */
  417. mutex_lock(&dev->rng_lock);
  418. mutex_lock(&dev->lock);
  419. mutex_unlock(&dev->rng_lock);
  420. /* Try to fill the buffer if empty. It doesn't actually matter
  421. * if _chaoskey_fill works; we'll just return zero bytes as
  422. * the buffer will still be empty
  423. */
  424. if (dev->valid == dev->used)
  425. (void) _chaoskey_fill(dev);
  426. this_time = dev->valid - dev->used;
  427. if (this_time > max)
  428. this_time = max;
  429. memcpy(data, dev->buf + dev->used, this_time);
  430. dev->used += this_time;
  431. mutex_unlock(&dev->lock);
  432. usb_dbg(dev->interface, "rng_read this_time %d\n", this_time);
  433. return this_time;
  434. }
  435. #ifdef CONFIG_PM
  436. static int chaoskey_suspend(struct usb_interface *interface,
  437. pm_message_t message)
  438. {
  439. usb_dbg(interface, "suspend");
  440. return 0;
  441. }
  442. static int chaoskey_resume(struct usb_interface *interface)
  443. {
  444. usb_dbg(interface, "resume");
  445. return 0;
  446. }
  447. #else
  448. #define chaoskey_suspend NULL
  449. #define chaoskey_resume NULL
  450. #endif
  451. /* file operation pointers */
  452. static const struct file_operations chaoskey_fops = {
  453. .owner = THIS_MODULE,
  454. .read = chaoskey_read,
  455. .open = chaoskey_open,
  456. .release = chaoskey_release,
  457. .llseek = default_llseek,
  458. };
  459. /* class driver information */
  460. static struct usb_class_driver chaoskey_class = {
  461. .name = "chaoskey%d",
  462. .fops = &chaoskey_fops,
  463. .minor_base = USB_CHAOSKEY_MINOR_BASE,
  464. };
  465. /* usb specific object needed to register this driver with the usb subsystem */
  466. static struct usb_driver chaoskey_driver = {
  467. .name = DRIVER_SHORT,
  468. .probe = chaoskey_probe,
  469. .disconnect = chaoskey_disconnect,
  470. .suspend = chaoskey_suspend,
  471. .resume = chaoskey_resume,
  472. .reset_resume = chaoskey_resume,
  473. .id_table = chaoskey_table,
  474. .supports_autosuspend = 1,
  475. };
  476. module_usb_driver(chaoskey_driver);