cryptodev.c 27 KB

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