cryptodev.c 26 KB

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