cryptodev.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
  2. /*-
  3. * Linux port done by David McCullough <david_mccullough@mcafee.com>
  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. #include <linux/version.h>
  41. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) && !defined(AUTOCONF_INCLUDED)
  42. #include <linux/config.h>
  43. #endif
  44. #include <linux/types.h>
  45. #include <linux/time.h>
  46. #include <linux/delay.h>
  47. #include <linux/list.h>
  48. #include <linux/init.h>
  49. #include <linux/sched.h>
  50. #include <linux/unistd.h>
  51. #include <linux/module.h>
  52. #include <linux/wait.h>
  53. #include <linux/slab.h>
  54. #include <linux/fs.h>
  55. #include <linux/dcache.h>
  56. #include <linux/file.h>
  57. #include <linux/mount.h>
  58. #include <linux/miscdevice.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 LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
  886. /*
  887. * on 2.6.35 private_data points to a miscdevice structure, we override
  888. * it, which is currently safe to do.
  889. */
  890. if (filp->private_data) {
  891. printk("cryptodev: Private data already exists - %p!\n", filp->private_data);
  892. return(-ENODEV);
  893. }
  894. #endif
  895. fcr = kmalloc(sizeof(*fcr), GFP_KERNEL);
  896. if (!fcr) {
  897. dprintk("%s() - malloc failed\n", __FUNCTION__);
  898. return(-ENOMEM);
  899. }
  900. memset(fcr, 0, sizeof(*fcr));
  901. INIT_LIST_HEAD(&fcr->csessions);
  902. filp->private_data = fcr;
  903. return(0);
  904. }
  905. static int
  906. cryptodev_release(struct inode *inode, struct file *filp)
  907. {
  908. struct fcrypt *fcr = filp->private_data;
  909. struct csession *cse, *tmp;
  910. dprintk("%s()\n", __FUNCTION__);
  911. if (!filp) {
  912. printk("cryptodev: No private data on release\n");
  913. return(0);
  914. }
  915. list_for_each_entry_safe(cse, tmp, &fcr->csessions, list) {
  916. list_del(&cse->list);
  917. (void)csefree(cse);
  918. }
  919. filp->private_data = NULL;
  920. kfree(fcr);
  921. return(0);
  922. }
  923. static struct file_operations cryptodev_fops = {
  924. .owner = THIS_MODULE,
  925. .open = cryptodev_open,
  926. .release = cryptodev_release,
  927. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
  928. .ioctl = cryptodev_ioctl,
  929. #endif
  930. #ifdef HAVE_UNLOCKED_IOCTL
  931. .unlocked_ioctl = cryptodev_unlocked_ioctl,
  932. #endif
  933. };
  934. static struct miscdevice cryptodev = {
  935. .minor = CRYPTODEV_MINOR,
  936. .name = "crypto",
  937. .fops = &cryptodev_fops,
  938. };
  939. static int __init
  940. cryptodev_init(void)
  941. {
  942. int rc;
  943. dprintk("%s(%p)\n", __FUNCTION__, cryptodev_init);
  944. rc = misc_register(&cryptodev);
  945. if (rc) {
  946. printk(KERN_ERR "cryptodev: registration of /dev/crypto failed\n");
  947. return(rc);
  948. }
  949. return(0);
  950. }
  951. static void __exit
  952. cryptodev_exit(void)
  953. {
  954. dprintk("%s()\n", __FUNCTION__);
  955. misc_deregister(&cryptodev);
  956. }
  957. module_init(cryptodev_init);
  958. module_exit(cryptodev_exit);
  959. MODULE_LICENSE("Dual BSD/GPL");
  960. MODULE_AUTHOR("David McCullough <david_mccullough@mcafee.com>");
  961. MODULE_DESCRIPTION("Cryptodev (user interface to OCF)");