cryptodev.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
  2. /*-
  3. * Linux port done by David McCullough <[email protected]>
  4. * Copyright (C) 2006-2010 David McCullough
  5. * Copyright (C) 2004-2005 Intel Corporation.
  6. * The license and original author are listed below.
  7. *
  8. * Copyright (c) 2001 Theo de Raadt
  9. * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  24. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  26. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  28. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  32. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. * Effort sponsored in part by the Defense Advanced Research Projects
  35. * Agency (DARPA) and Air Force Research Laboratory, Air Force
  36. * Materiel Command, USAF, under agreement number F30602-01-2-0537.
  37. *
  38. __FBSDID("$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.34 2007/05/09 19:37:02 gnn Exp $");
  39. */
  40. #ifndef AUTOCONF_INCLUDED
  41. #include <linux/config.h>
  42. #endif
  43. #include <linux/types.h>
  44. #include <linux/time.h>
  45. #include <linux/delay.h>
  46. #include <linux/list.h>
  47. #include <linux/init.h>
  48. #include <linux/sched.h>
  49. #include <linux/unistd.h>
  50. #include <linux/module.h>
  51. #include <linux/wait.h>
  52. #include <linux/slab.h>
  53. #include <linux/fs.h>
  54. #include <linux/dcache.h>
  55. #include <linux/file.h>
  56. #include <linux/mount.h>
  57. #include <linux/miscdevice.h>
  58. #include <linux/version.h>
  59. #include <asm/uaccess.h>
  60. #include <cryptodev.h>
  61. #include <uio.h>
  62. extern asmlinkage long sys_dup(unsigned int fildes);
  63. #define debug cryptodev_debug
  64. int cryptodev_debug = 0;
  65. module_param(cryptodev_debug, int, 0644);
  66. MODULE_PARM_DESC(cryptodev_debug, "Enable cryptodev debug");
  67. struct csession_info {
  68. u_int16_t blocksize;
  69. u_int16_t minkey, maxkey;
  70. u_int16_t keysize;
  71. /* u_int16_t hashsize; */
  72. u_int16_t authsize;
  73. u_int16_t authkey;
  74. /* u_int16_t ctxsize; */
  75. };
  76. struct csession {
  77. struct list_head list;
  78. u_int64_t sid;
  79. u_int32_t ses;
  80. wait_queue_head_t waitq;
  81. u_int32_t cipher;
  82. u_int32_t mac;
  83. caddr_t key;
  84. int keylen;
  85. u_char tmp_iv[EALG_MAX_BLOCK_LEN];
  86. caddr_t mackey;
  87. int mackeylen;
  88. struct csession_info info;
  89. struct iovec iovec;
  90. struct uio uio;
  91. int error;
  92. };
  93. struct fcrypt {
  94. struct list_head csessions;
  95. int sesn;
  96. };
  97. static struct csession *csefind(struct fcrypt *, u_int);
  98. static int csedelete(struct fcrypt *, struct csession *);
  99. static struct csession *cseadd(struct fcrypt *, struct csession *);
  100. static struct csession *csecreate(struct fcrypt *, u_int64_t,
  101. struct cryptoini *crie, struct cryptoini *cria, struct csession_info *);
  102. static int csefree(struct csession *);
  103. static int cryptodev_op(struct csession *, struct crypt_op *);
  104. static int cryptodev_key(struct crypt_kop *);
  105. static int cryptodev_find(struct crypt_find_op *);
  106. static int cryptodev_cb(void *);
  107. static int cryptodev_open(struct inode *inode, struct file *filp);
  108. /*
  109. * Check a crypto identifier to see if it requested
  110. * a valid crid and it's capabilities match.
  111. */
  112. static int
  113. checkcrid(int crid)
  114. {
  115. int hid = crid & ~(CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
  116. int typ = crid & (CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
  117. int caps = 0;
  118. /* if the user hasn't selected a driver, then just call newsession */
  119. if (hid == 0 && typ != 0)
  120. return 0;
  121. caps = crypto_getcaps(hid);
  122. /* didn't find anything with capabilities */
  123. if (caps == 0) {
  124. dprintk("%s: hid=%x typ=%x not matched\n", __FUNCTION__, hid, typ);
  125. return EINVAL;
  126. }
  127. /* the user didn't specify SW or HW, so the driver is ok */
  128. if (typ == 0)
  129. return 0;
  130. /* if the type specified didn't match */
  131. if (typ != (caps & (CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE))) {
  132. dprintk("%s: hid=%x typ=%x caps=%x not matched\n", __FUNCTION__,
  133. hid, typ, caps);
  134. return EINVAL;
  135. }
  136. return 0;
  137. }
  138. static int
  139. cryptodev_op(struct csession *cse, struct crypt_op *cop)
  140. {
  141. struct cryptop *crp = NULL;
  142. struct cryptodesc *crde = NULL, *crda = NULL;
  143. int error = 0;
  144. dprintk("%s()\n", __FUNCTION__);
  145. if (cop->len > CRYPTO_MAX_DATA_LEN) {
  146. dprintk("%s: %d > %d\n", __FUNCTION__, cop->len, CRYPTO_MAX_DATA_LEN);
  147. return (E2BIG);
  148. }
  149. if (cse->info.blocksize && (cop->len % cse->info.blocksize) != 0) {
  150. dprintk("%s: blocksize=%d len=%d\n", __FUNCTION__, cse->info.blocksize,
  151. cop->len);
  152. return (EINVAL);
  153. }
  154. cse->uio.uio_iov = &cse->iovec;
  155. cse->uio.uio_iovcnt = 1;
  156. cse->uio.uio_offset = 0;
  157. #if 0
  158. cse->uio.uio_resid = cop->len;
  159. cse->uio.uio_segflg = UIO_SYSSPACE;
  160. cse->uio.uio_rw = UIO_WRITE;
  161. cse->uio.uio_td = td;
  162. #endif
  163. cse->uio.uio_iov[0].iov_len = cop->len;
  164. if (cse->info.authsize)
  165. cse->uio.uio_iov[0].iov_len += cse->info.authsize;
  166. cse->uio.uio_iov[0].iov_base = kmalloc(cse->uio.uio_iov[0].iov_len,
  167. GFP_KERNEL);
  168. if (cse->uio.uio_iov[0].iov_base == NULL) {
  169. dprintk("%s: iov_base kmalloc(%d) failed\n", __FUNCTION__,
  170. (int)cse->uio.uio_iov[0].iov_len);
  171. return (ENOMEM);
  172. }
  173. crp = crypto_getreq((cse->info.blocksize != 0) + (cse->info.authsize != 0));
  174. if (crp == NULL) {
  175. dprintk("%s: ENOMEM\n", __FUNCTION__);
  176. error = ENOMEM;
  177. goto bail;
  178. }
  179. if (cse->info.authsize && cse->info.blocksize) {
  180. if (cop->op == COP_ENCRYPT) {
  181. crde = crp->crp_desc;
  182. crda = crde->crd_next;
  183. } else {
  184. crda = crp->crp_desc;
  185. crde = crda->crd_next;
  186. }
  187. } else if (cse->info.authsize) {
  188. crda = crp->crp_desc;
  189. } else if (cse->info.blocksize) {
  190. crde = crp->crp_desc;
  191. } else {
  192. dprintk("%s: bad request\n", __FUNCTION__);
  193. error = EINVAL;
  194. goto bail;
  195. }
  196. if ((error = copy_from_user(cse->uio.uio_iov[0].iov_base, cop->src,
  197. cop->len))) {
  198. dprintk("%s: bad copy\n", __FUNCTION__);
  199. goto bail;
  200. }
  201. if (crda) {
  202. crda->crd_skip = 0;
  203. crda->crd_len = cop->len;
  204. crda->crd_inject = cop->len;
  205. crda->crd_alg = cse->mac;
  206. crda->crd_key = cse->mackey;
  207. crda->crd_klen = cse->mackeylen * 8;
  208. }
  209. if (crde) {
  210. if (cop->op == COP_ENCRYPT)
  211. crde->crd_flags |= CRD_F_ENCRYPT;
  212. else
  213. crde->crd_flags &= ~CRD_F_ENCRYPT;
  214. crde->crd_len = cop->len;
  215. crde->crd_inject = 0;
  216. crde->crd_alg = cse->cipher;
  217. crde->crd_key = cse->key;
  218. crde->crd_klen = cse->keylen * 8;
  219. }
  220. crp->crp_ilen = cse->uio.uio_iov[0].iov_len;
  221. crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
  222. | (cop->flags & COP_F_BATCH);
  223. crp->crp_buf = (caddr_t)&cse->uio;
  224. crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
  225. crp->crp_sid = cse->sid;
  226. crp->crp_opaque = (void *)cse;
  227. if (cop->iv) {
  228. if (crde == NULL) {
  229. error = EINVAL;
  230. dprintk("%s no crde\n", __FUNCTION__);
  231. goto bail;
  232. }
  233. if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
  234. error = EINVAL;
  235. dprintk("%s arc4 with IV\n", __FUNCTION__);
  236. goto bail;
  237. }
  238. if ((error = copy_from_user(cse->tmp_iv, cop->iv,
  239. cse->info.blocksize))) {
  240. dprintk("%s bad iv copy\n", __FUNCTION__);
  241. goto bail;
  242. }
  243. memcpy(crde->crd_iv, cse->tmp_iv, cse->info.blocksize);
  244. crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
  245. crde->crd_skip = 0;
  246. } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
  247. crde->crd_skip = 0;
  248. } else if (crde) {
  249. crde->crd_flags |= CRD_F_IV_PRESENT;
  250. crde->crd_skip = cse->info.blocksize;
  251. crde->crd_len -= cse->info.blocksize;
  252. }
  253. if (cop->mac && crda == NULL) {
  254. error = EINVAL;
  255. dprintk("%s no crda\n", __FUNCTION__);
  256. goto bail;
  257. }
  258. /*
  259. * Let the dispatch run unlocked, then, interlock against the
  260. * callback before checking if the operation completed and going
  261. * to sleep. This insures drivers don't inherit our lock which
  262. * results in a lock order reversal between crypto_dispatch forced
  263. * entry and the crypto_done callback into us.
  264. */
  265. error = crypto_dispatch(crp);
  266. if (error) {
  267. dprintk("%s error in crypto_dispatch\n", __FUNCTION__);
  268. goto bail;
  269. }
  270. dprintk("%s about to WAIT\n", __FUNCTION__);
  271. /*
  272. * we really need to wait for driver to complete to maintain
  273. * state, luckily interrupts will be remembered
  274. */
  275. do {
  276. error = wait_event_interruptible(crp->crp_waitq,
  277. ((crp->crp_flags & CRYPTO_F_DONE) != 0));
  278. /*
  279. * we can't break out of this loop or we will leave behind
  280. * a huge mess, however, staying here means if your driver
  281. * is broken user applications can hang and not be killed.
  282. * The solution, fix your driver :-)
  283. */
  284. if (error) {
  285. schedule();
  286. error = 0;
  287. }
  288. } while ((crp->crp_flags & CRYPTO_F_DONE) == 0);
  289. dprintk("%s finished WAITING error=%d\n", __FUNCTION__, error);
  290. if (crp->crp_etype != 0) {
  291. error = crp->crp_etype;
  292. dprintk("%s error in crp processing\n", __FUNCTION__);
  293. goto bail;
  294. }
  295. if (cse->error) {
  296. error = cse->error;
  297. dprintk("%s error in cse processing\n", __FUNCTION__);
  298. goto bail;
  299. }
  300. if (cop->dst && (error = copy_to_user(cop->dst,
  301. cse->uio.uio_iov[0].iov_base, cop->len))) {
  302. dprintk("%s bad dst copy\n", __FUNCTION__);
  303. goto bail;
  304. }
  305. if (cop->mac &&
  306. (error=copy_to_user(cop->mac,
  307. (caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
  308. cse->info.authsize))) {
  309. dprintk("%s bad mac copy\n", __FUNCTION__);
  310. goto bail;
  311. }
  312. bail:
  313. if (crp)
  314. crypto_freereq(crp);
  315. if (cse->uio.uio_iov[0].iov_base)
  316. kfree(cse->uio.uio_iov[0].iov_base);
  317. return (error);
  318. }
  319. static int
  320. cryptodev_cb(void *op)
  321. {
  322. struct cryptop *crp = (struct cryptop *) op;
  323. struct csession *cse = (struct csession *)crp->crp_opaque;
  324. int error;
  325. dprintk("%s()\n", __FUNCTION__);
  326. error = crp->crp_etype;
  327. if (error == EAGAIN) {
  328. crp->crp_flags &= ~CRYPTO_F_DONE;
  329. #ifdef NOTYET
  330. /*
  331. * DAVIDM I am fairly sure that we should turn this into a batch
  332. * request to stop bad karma/lockup, revisit
  333. */
  334. crp->crp_flags |= CRYPTO_F_BATCH;
  335. #endif
  336. return crypto_dispatch(crp);
  337. }
  338. if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
  339. cse->error = error;
  340. wake_up_interruptible(&crp->crp_waitq);
  341. }
  342. return (0);
  343. }
  344. static int
  345. cryptodevkey_cb(void *op)
  346. {
  347. struct cryptkop *krp = (struct cryptkop *) op;
  348. dprintk("%s()\n", __FUNCTION__);
  349. wake_up_interruptible(&krp->krp_waitq);
  350. return (0);
  351. }
  352. static int
  353. cryptodev_key(struct crypt_kop *kop)
  354. {
  355. struct cryptkop *krp = NULL;
  356. int error = EINVAL;
  357. int in, out, size, i;
  358. dprintk("%s()\n", __FUNCTION__);
  359. if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
  360. dprintk("%s params too big\n", __FUNCTION__);
  361. return (EFBIG);
  362. }
  363. in = kop->crk_iparams;
  364. out = kop->crk_oparams;
  365. switch (kop->crk_op) {
  366. case CRK_MOD_EXP:
  367. if (in == 3 && out == 1)
  368. break;
  369. return (EINVAL);
  370. case CRK_MOD_EXP_CRT:
  371. if (in == 6 && out == 1)
  372. break;
  373. return (EINVAL);
  374. case CRK_DSA_SIGN:
  375. if (in == 5 && out == 2)
  376. break;
  377. return (EINVAL);
  378. case CRK_DSA_VERIFY:
  379. if (in == 7 && out == 0)
  380. break;
  381. return (EINVAL);
  382. case CRK_DH_COMPUTE_KEY:
  383. if (in == 3 && out == 1)
  384. break;
  385. return (EINVAL);
  386. default:
  387. return (EINVAL);
  388. }
  389. krp = (struct cryptkop *)kmalloc(sizeof *krp, GFP_KERNEL);
  390. if (!krp)
  391. return (ENOMEM);
  392. bzero(krp, sizeof *krp);
  393. krp->krp_op = kop->crk_op;
  394. krp->krp_status = kop->crk_status;
  395. krp->krp_iparams = kop->crk_iparams;
  396. krp->krp_oparams = kop->crk_oparams;
  397. krp->krp_crid = kop->crk_crid;
  398. krp->krp_status = 0;
  399. krp->krp_flags = CRYPTO_KF_CBIMM;
  400. krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
  401. init_waitqueue_head(&krp->krp_waitq);
  402. for (i = 0; i < CRK_MAXPARAM; i++)
  403. krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
  404. for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
  405. size = (krp->krp_param[i].crp_nbits + 7) / 8;
  406. if (size == 0)
  407. continue;
  408. krp->krp_param[i].crp_p = (caddr_t) kmalloc(size, GFP_KERNEL);
  409. if (i >= krp->krp_iparams)
  410. continue;
  411. error = copy_from_user(krp->krp_param[i].crp_p,
  412. kop->crk_param[i].crp_p, size);
  413. if (error)
  414. goto fail;
  415. }
  416. error = crypto_kdispatch(krp);
  417. if (error)
  418. goto fail;
  419. do {
  420. error = wait_event_interruptible(krp->krp_waitq,
  421. ((krp->krp_flags & CRYPTO_KF_DONE) != 0));
  422. /*
  423. * we can't break out of this loop or we will leave behind
  424. * a huge mess, however, staying here means if your driver
  425. * is broken user applications can hang and not be killed.
  426. * The solution, fix your driver :-)
  427. */
  428. if (error) {
  429. schedule();
  430. error = 0;
  431. }
  432. } while ((krp->krp_flags & CRYPTO_KF_DONE) == 0);
  433. dprintk("%s finished WAITING error=%d\n", __FUNCTION__, error);
  434. kop->crk_crid = krp->krp_crid; /* device that did the work */
  435. if (krp->krp_status != 0) {
  436. error = krp->krp_status;
  437. goto fail;
  438. }
  439. for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
  440. size = (krp->krp_param[i].crp_nbits + 7) / 8;
  441. if (size == 0)
  442. continue;
  443. error = copy_to_user(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p,
  444. size);
  445. if (error)
  446. goto fail;
  447. }
  448. fail:
  449. if (krp) {
  450. kop->crk_status = krp->krp_status;
  451. for (i = 0; i < CRK_MAXPARAM; i++) {
  452. if (krp->krp_param[i].crp_p)
  453. kfree(krp->krp_param[i].crp_p);
  454. }
  455. kfree(krp);
  456. }
  457. return (error);
  458. }
  459. static int
  460. cryptodev_find(struct crypt_find_op *find)
  461. {
  462. device_t dev;
  463. if (find->crid != -1) {
  464. dev = crypto_find_device_byhid(find->crid);
  465. if (dev == NULL)
  466. return (ENOENT);
  467. strlcpy(find->name, device_get_nameunit(dev),
  468. sizeof(find->name));
  469. } else {
  470. find->crid = crypto_find_driver(find->name);
  471. if (find->crid == -1)
  472. return (ENOENT);
  473. }
  474. return (0);
  475. }
  476. static struct csession *
  477. csefind(struct fcrypt *fcr, u_int ses)
  478. {
  479. struct csession *cse;
  480. dprintk("%s()\n", __FUNCTION__);
  481. list_for_each_entry(cse, &fcr->csessions, list)
  482. if (cse->ses == ses)
  483. return (cse);
  484. return (NULL);
  485. }
  486. static int
  487. csedelete(struct fcrypt *fcr, struct csession *cse_del)
  488. {
  489. struct csession *cse;
  490. dprintk("%s()\n", __FUNCTION__);
  491. list_for_each_entry(cse, &fcr->csessions, list) {
  492. if (cse == cse_del) {
  493. list_del(&cse->list);
  494. return (1);
  495. }
  496. }
  497. return (0);
  498. }
  499. static struct csession *
  500. cseadd(struct fcrypt *fcr, struct csession *cse)
  501. {
  502. dprintk("%s()\n", __FUNCTION__);
  503. list_add_tail(&cse->list, &fcr->csessions);
  504. cse->ses = fcr->sesn++;
  505. return (cse);
  506. }
  507. static struct csession *
  508. csecreate(struct fcrypt *fcr, u_int64_t sid, struct cryptoini *crie,
  509. struct cryptoini *cria, struct csession_info *info)
  510. {
  511. struct csession *cse;
  512. dprintk("%s()\n", __FUNCTION__);
  513. cse = (struct csession *) kmalloc(sizeof(struct csession), GFP_KERNEL);
  514. if (cse == NULL)
  515. return NULL;
  516. memset(cse, 0, sizeof(struct csession));
  517. INIT_LIST_HEAD(&cse->list);
  518. init_waitqueue_head(&cse->waitq);
  519. cse->key = crie->cri_key;
  520. cse->keylen = crie->cri_klen/8;
  521. cse->mackey = cria->cri_key;
  522. cse->mackeylen = cria->cri_klen/8;
  523. cse->sid = sid;
  524. cse->cipher = crie->cri_alg;
  525. cse->mac = cria->cri_alg;
  526. cse->info = *info;
  527. cseadd(fcr, cse);
  528. return (cse);
  529. }
  530. static int
  531. csefree(struct csession *cse)
  532. {
  533. int error;
  534. dprintk("%s()\n", __FUNCTION__);
  535. error = crypto_freesession(cse->sid);
  536. if (cse->key)
  537. kfree(cse->key);
  538. if (cse->mackey)
  539. kfree(cse->mackey);
  540. kfree(cse);
  541. return(error);
  542. }
  543. static int
  544. cryptodev_ioctl(
  545. struct inode *inode,
  546. struct file *filp,
  547. unsigned int cmd,
  548. unsigned long arg)
  549. {
  550. struct cryptoini cria, crie;
  551. struct fcrypt *fcr = filp->private_data;
  552. struct csession *cse;
  553. struct csession_info info;
  554. struct session2_op sop;
  555. struct crypt_op cop;
  556. struct crypt_kop kop;
  557. struct crypt_find_op fop;
  558. u_int64_t sid;
  559. u_int32_t ses = 0;
  560. int feat, fd, error = 0, crid;
  561. mm_segment_t fs;
  562. dprintk("%s(cmd=%x arg=%lx)\n", __FUNCTION__, cmd, arg);
  563. switch (cmd) {
  564. case CRIOGET: {
  565. dprintk("%s(CRIOGET)\n", __FUNCTION__);
  566. fs = get_fs();
  567. set_fs(get_ds());
  568. for (fd = 0; fd < files_fdtable(current->files)->max_fds; fd++)
  569. if (files_fdtable(current->files)->fd[fd] == filp)
  570. break;
  571. fd = sys_dup(fd);
  572. set_fs(fs);
  573. put_user(fd, (int *) arg);
  574. return IS_ERR_VALUE(fd) ? fd : 0;
  575. }
  576. #define CIOCGSESSSTR (cmd == CIOCGSESSION ? "CIOCGSESSION" : "CIOCGSESSION2")
  577. case CIOCGSESSION:
  578. case CIOCGSESSION2:
  579. dprintk("%s(%s)\n", __FUNCTION__, CIOCGSESSSTR);
  580. memset(&crie, 0, sizeof(crie));
  581. memset(&cria, 0, sizeof(cria));
  582. memset(&info, 0, sizeof(info));
  583. memset(&sop, 0, sizeof(sop));
  584. if (copy_from_user(&sop, (void*)arg, (cmd == CIOCGSESSION) ?
  585. sizeof(struct session_op) : sizeof(sop))) {
  586. dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
  587. error = EFAULT;
  588. goto bail;
  589. }
  590. switch (sop.cipher) {
  591. case 0:
  592. dprintk("%s(%s) - no cipher\n", __FUNCTION__, CIOCGSESSSTR);
  593. break;
  594. case CRYPTO_NULL_CBC:
  595. info.blocksize = NULL_BLOCK_LEN;
  596. info.minkey = NULL_MIN_KEY_LEN;
  597. info.maxkey = NULL_MAX_KEY_LEN;
  598. break;
  599. case CRYPTO_DES_CBC:
  600. info.blocksize = DES_BLOCK_LEN;
  601. info.minkey = DES_MIN_KEY_LEN;
  602. info.maxkey = DES_MAX_KEY_LEN;
  603. break;
  604. case CRYPTO_3DES_CBC:
  605. info.blocksize = DES3_BLOCK_LEN;
  606. info.minkey = DES3_MIN_KEY_LEN;
  607. info.maxkey = DES3_MAX_KEY_LEN;
  608. break;
  609. case CRYPTO_BLF_CBC:
  610. info.blocksize = BLOWFISH_BLOCK_LEN;
  611. info.minkey = BLOWFISH_MIN_KEY_LEN;
  612. info.maxkey = BLOWFISH_MAX_KEY_LEN;
  613. break;
  614. case CRYPTO_CAST_CBC:
  615. info.blocksize = CAST128_BLOCK_LEN;
  616. info.minkey = CAST128_MIN_KEY_LEN;
  617. info.maxkey = CAST128_MAX_KEY_LEN;
  618. break;
  619. case CRYPTO_SKIPJACK_CBC:
  620. info.blocksize = SKIPJACK_BLOCK_LEN;
  621. info.minkey = SKIPJACK_MIN_KEY_LEN;
  622. info.maxkey = SKIPJACK_MAX_KEY_LEN;
  623. break;
  624. case CRYPTO_AES_CBC:
  625. info.blocksize = AES_BLOCK_LEN;
  626. info.minkey = AES_MIN_KEY_LEN;
  627. info.maxkey = AES_MAX_KEY_LEN;
  628. break;
  629. case CRYPTO_ARC4:
  630. info.blocksize = ARC4_BLOCK_LEN;
  631. info.minkey = ARC4_MIN_KEY_LEN;
  632. info.maxkey = ARC4_MAX_KEY_LEN;
  633. break;
  634. case CRYPTO_CAMELLIA_CBC:
  635. info.blocksize = CAMELLIA_BLOCK_LEN;
  636. info.minkey = CAMELLIA_MIN_KEY_LEN;
  637. info.maxkey = CAMELLIA_MAX_KEY_LEN;
  638. break;
  639. default:
  640. dprintk("%s(%s) - bad cipher\n", __FUNCTION__, CIOCGSESSSTR);
  641. error = EINVAL;
  642. goto bail;
  643. }
  644. switch (sop.mac) {
  645. case 0:
  646. dprintk("%s(%s) - no mac\n", __FUNCTION__, CIOCGSESSSTR);
  647. break;
  648. case CRYPTO_NULL_HMAC:
  649. info.authsize = NULL_HASH_LEN;
  650. break;
  651. case CRYPTO_MD5:
  652. info.authsize = MD5_HASH_LEN;
  653. break;
  654. case CRYPTO_SHA1:
  655. info.authsize = SHA1_HASH_LEN;
  656. break;
  657. case CRYPTO_SHA2_256:
  658. info.authsize = SHA2_256_HASH_LEN;
  659. break;
  660. case CRYPTO_SHA2_384:
  661. info.authsize = SHA2_384_HASH_LEN;
  662. break;
  663. case CRYPTO_SHA2_512:
  664. info.authsize = SHA2_512_HASH_LEN;
  665. break;
  666. case CRYPTO_RIPEMD160:
  667. info.authsize = RIPEMD160_HASH_LEN;
  668. break;
  669. case CRYPTO_MD5_HMAC:
  670. info.authsize = MD5_HASH_LEN;
  671. info.authkey = 16;
  672. break;
  673. case CRYPTO_SHA1_HMAC:
  674. info.authsize = SHA1_HASH_LEN;
  675. info.authkey = 20;
  676. break;
  677. case CRYPTO_SHA2_256_HMAC:
  678. info.authsize = SHA2_256_HASH_LEN;
  679. info.authkey = 32;
  680. break;
  681. case CRYPTO_SHA2_384_HMAC:
  682. info.authsize = SHA2_384_HASH_LEN;
  683. info.authkey = 48;
  684. break;
  685. case CRYPTO_SHA2_512_HMAC:
  686. info.authsize = SHA2_512_HASH_LEN;
  687. info.authkey = 64;
  688. break;
  689. case CRYPTO_RIPEMD160_HMAC:
  690. info.authsize = RIPEMD160_HASH_LEN;
  691. info.authkey = 20;
  692. break;
  693. default:
  694. dprintk("%s(%s) - bad mac\n", __FUNCTION__, CIOCGSESSSTR);
  695. error = EINVAL;
  696. goto bail;
  697. }
  698. if (info.blocksize) {
  699. crie.cri_alg = sop.cipher;
  700. crie.cri_klen = sop.keylen * 8;
  701. if ((info.maxkey && sop.keylen > info.maxkey) ||
  702. sop.keylen < info.minkey) {
  703. dprintk("%s(%s) - bad key\n", __FUNCTION__, CIOCGSESSSTR);
  704. error = EINVAL;
  705. goto bail;
  706. }
  707. crie.cri_key = (u_int8_t *) kmalloc(crie.cri_klen/8+1, GFP_KERNEL);
  708. if (copy_from_user(crie.cri_key, sop.key,
  709. crie.cri_klen/8)) {
  710. dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
  711. error = EFAULT;
  712. goto bail;
  713. }
  714. if (info.authsize)
  715. crie.cri_next = &cria;
  716. }
  717. if (info.authsize) {
  718. cria.cri_alg = sop.mac;
  719. cria.cri_klen = sop.mackeylen * 8;
  720. if (info.authkey && sop.mackeylen != info.authkey) {
  721. dprintk("%s(%s) - mackeylen %d != %d\n", __FUNCTION__,
  722. CIOCGSESSSTR, sop.mackeylen, info.authkey);
  723. error = EINVAL;
  724. goto bail;
  725. }
  726. if (cria.cri_klen) {
  727. cria.cri_key = (u_int8_t *) kmalloc(cria.cri_klen/8,GFP_KERNEL);
  728. if (copy_from_user(cria.cri_key, sop.mackey,
  729. cria.cri_klen / 8)) {
  730. dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
  731. error = EFAULT;
  732. goto bail;
  733. }
  734. }
  735. }
  736. /* NB: CIOGSESSION2 has the crid */
  737. if (cmd == CIOCGSESSION2) {
  738. crid = sop.crid;
  739. error = checkcrid(crid);
  740. if (error) {
  741. dprintk("%s(%s) - checkcrid %x\n", __FUNCTION__,
  742. CIOCGSESSSTR, error);
  743. goto bail;
  744. }
  745. } else {
  746. /* allow either HW or SW to be used */
  747. crid = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
  748. }
  749. error = crypto_newsession(&sid, (info.blocksize ? &crie : &cria), crid);
  750. if (error) {
  751. dprintk("%s(%s) - newsession %d\n",__FUNCTION__,CIOCGSESSSTR,error);
  752. goto bail;
  753. }
  754. cse = csecreate(fcr, sid, &crie, &cria, &info);
  755. if (cse == NULL) {
  756. crypto_freesession(sid);
  757. error = EINVAL;
  758. dprintk("%s(%s) - csecreate failed\n", __FUNCTION__, CIOCGSESSSTR);
  759. goto bail;
  760. }
  761. sop.ses = cse->ses;
  762. if (cmd == CIOCGSESSION2) {
  763. /* return hardware/driver id */
  764. sop.crid = CRYPTO_SESID2HID(cse->sid);
  765. }
  766. if (copy_to_user((void*)arg, &sop, (cmd == CIOCGSESSION) ?
  767. sizeof(struct session_op) : sizeof(sop))) {
  768. dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
  769. error = EFAULT;
  770. }
  771. bail:
  772. if (error) {
  773. dprintk("%s(%s) - bail %d\n", __FUNCTION__, CIOCGSESSSTR, error);
  774. if (crie.cri_key)
  775. kfree(crie.cri_key);
  776. if (cria.cri_key)
  777. kfree(cria.cri_key);
  778. }
  779. break;
  780. case CIOCFSESSION:
  781. dprintk("%s(CIOCFSESSION)\n", __FUNCTION__);
  782. get_user(ses, (uint32_t*)arg);
  783. cse = csefind(fcr, ses);
  784. if (cse == NULL) {
  785. error = EINVAL;
  786. dprintk("%s(CIOCFSESSION) - Fail %d\n", __FUNCTION__, error);
  787. break;
  788. }
  789. csedelete(fcr, cse);
  790. error = csefree(cse);
  791. break;
  792. case CIOCCRYPT:
  793. dprintk("%s(CIOCCRYPT)\n", __FUNCTION__);
  794. if(copy_from_user(&cop, (void*)arg, sizeof(cop))) {
  795. dprintk("%s(CIOCCRYPT) - bad copy\n", __FUNCTION__);
  796. error = EFAULT;
  797. goto bail;
  798. }
  799. cse = csefind(fcr, cop.ses);
  800. if (cse == NULL) {
  801. error = EINVAL;
  802. dprintk("%s(CIOCCRYPT) - Fail %d\n", __FUNCTION__, error);
  803. break;
  804. }
  805. error = cryptodev_op(cse, &cop);
  806. if(copy_to_user((void*)arg, &cop, sizeof(cop))) {
  807. dprintk("%s(CIOCCRYPT) - bad return copy\n", __FUNCTION__);
  808. error = EFAULT;
  809. goto bail;
  810. }
  811. break;
  812. case CIOCKEY:
  813. case CIOCKEY2:
  814. dprintk("%s(CIOCKEY)\n", __FUNCTION__);
  815. if (!crypto_userasymcrypto)
  816. return (EPERM); /* XXX compat? */
  817. if(copy_from_user(&kop, (void*)arg, sizeof(kop))) {
  818. dprintk("%s(CIOCKEY) - bad copy\n", __FUNCTION__);
  819. error = EFAULT;
  820. goto bail;
  821. }
  822. if (cmd == CIOCKEY) {
  823. /* NB: crypto core enforces s/w driver use */
  824. kop.crk_crid =
  825. CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
  826. }
  827. error = cryptodev_key(&kop);
  828. if(copy_to_user((void*)arg, &kop, sizeof(kop))) {
  829. dprintk("%s(CIOCGKEY) - bad return copy\n", __FUNCTION__);
  830. error = EFAULT;
  831. goto bail;
  832. }
  833. break;
  834. case CIOCASYMFEAT:
  835. dprintk("%s(CIOCASYMFEAT)\n", __FUNCTION__);
  836. if (!crypto_userasymcrypto) {
  837. /*
  838. * NB: if user asym crypto operations are
  839. * not permitted return "no algorithms"
  840. * so well-behaved applications will just
  841. * fallback to doing them in software.
  842. */
  843. feat = 0;
  844. } else
  845. error = crypto_getfeat(&feat);
  846. if (!error) {
  847. error = copy_to_user((void*)arg, &feat, sizeof(feat));
  848. }
  849. break;
  850. case CIOCFINDDEV:
  851. if (copy_from_user(&fop, (void*)arg, sizeof(fop))) {
  852. dprintk("%s(CIOCFINDDEV) - bad copy\n", __FUNCTION__);
  853. error = EFAULT;
  854. goto bail;
  855. }
  856. error = cryptodev_find(&fop);
  857. if (copy_to_user((void*)arg, &fop, sizeof(fop))) {
  858. dprintk("%s(CIOCFINDDEV) - bad return copy\n", __FUNCTION__);
  859. error = EFAULT;
  860. goto bail;
  861. }
  862. break;
  863. default:
  864. dprintk("%s(unknown ioctl 0x%x)\n", __FUNCTION__, cmd);
  865. error = EINVAL;
  866. break;
  867. }
  868. return(-error);
  869. }
  870. #ifdef HAVE_UNLOCKED_IOCTL
  871. static long
  872. cryptodev_unlocked_ioctl(
  873. struct file *filp,
  874. unsigned int cmd,
  875. unsigned long arg)
  876. {
  877. return cryptodev_ioctl(NULL, filp, cmd, arg);
  878. }
  879. #endif
  880. static int
  881. cryptodev_open(struct inode *inode, struct file *filp)
  882. {
  883. struct fcrypt *fcr;
  884. dprintk("%s()\n", __FUNCTION__);
  885. if (filp->private_data) {
  886. printk("cryptodev: Private data already exists !\n");
  887. return(0);
  888. }
  889. fcr = kmalloc(sizeof(*fcr), GFP_KERNEL);
  890. if (!fcr) {
  891. dprintk("%s() - malloc failed\n", __FUNCTION__);
  892. return(-ENOMEM);
  893. }
  894. memset(fcr, 0, sizeof(*fcr));
  895. INIT_LIST_HEAD(&fcr->csessions);
  896. filp->private_data = fcr;
  897. return(0);
  898. }
  899. static int
  900. cryptodev_release(struct inode *inode, struct file *filp)
  901. {
  902. struct fcrypt *fcr = filp->private_data;
  903. struct csession *cse, *tmp;
  904. dprintk("%s()\n", __FUNCTION__);
  905. if (!filp) {
  906. printk("cryptodev: No private data on release\n");
  907. return(0);
  908. }
  909. list_for_each_entry_safe(cse, tmp, &fcr->csessions, list) {
  910. list_del(&cse->list);
  911. (void)csefree(cse);
  912. }
  913. filp->private_data = NULL;
  914. kfree(fcr);
  915. return(0);
  916. }
  917. static struct file_operations cryptodev_fops = {
  918. .owner = THIS_MODULE,
  919. .open = cryptodev_open,
  920. .release = cryptodev_release,
  921. .ioctl = cryptodev_ioctl,
  922. #ifdef HAVE_UNLOCKED_IOCTL
  923. .unlocked_ioctl = cryptodev_unlocked_ioctl,
  924. #endif
  925. };
  926. static struct miscdevice cryptodev = {
  927. .minor = CRYPTODEV_MINOR,
  928. .name = "crypto",
  929. .fops = &cryptodev_fops,
  930. };
  931. static int __init
  932. cryptodev_init(void)
  933. {
  934. int rc;
  935. dprintk("%s(%p)\n", __FUNCTION__, cryptodev_init);
  936. rc = misc_register(&cryptodev);
  937. if (rc) {
  938. printk(KERN_ERR "cryptodev: registration of /dev/crypto failed\n");
  939. return(rc);
  940. }
  941. return(0);
  942. }
  943. static void __exit
  944. cryptodev_exit(void)
  945. {
  946. dprintk("%s()\n", __FUNCTION__);
  947. misc_deregister(&cryptodev);
  948. }
  949. module_init(cryptodev_init);
  950. module_exit(cryptodev_exit);
  951. MODULE_LICENSE("BSD");
  952. MODULE_AUTHOR("David McCullough <[email protected]>");
  953. MODULE_DESCRIPTION("Cryptodev (user interface to OCF)");