cryptodev.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. /* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
  2. /*-
  3. * Copyright (c) 2001 Theo de Raadt
  4. * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
  5. * Copyright (c) 2014 The FreeBSD Foundation
  6. * All rights reserved.
  7. *
  8. * Portions of this software were developed by John-Mark Gurney
  9. * under sponsorship of the FreeBSD Foundation and
  10. * Rubicon Communications, LLC (Netgate).
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. The name of the author may not be used to endorse or promote products
  22. * derived from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  25. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  26. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  27. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  29. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  33. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * Effort sponsored in part by the Defense Advanced Research Projects
  36. * Agency (DARPA) and Air Force Research Laboratory, Air Force
  37. * Materiel Command, USAF, under agreement number F30602-01-2-0537.
  38. */
  39. #include <sys/cdefs.h>
  40. __FBSDID("$FreeBSD$");
  41. #include <sys/param.h>
  42. #include <sys/systm.h>
  43. #include <sys/malloc.h>
  44. #include <sys/mbuf.h>
  45. #include <sys/lock.h>
  46. #include <sys/mutex.h>
  47. #include <sys/proc.h>
  48. #include <sys/sysctl.h>
  49. #include <sys/errno.h>
  50. #include <sys/random.h>
  51. #include <sys/conf.h>
  52. #include <sys/kernel.h>
  53. #include <sys/module.h>
  54. #include <sys/fcntl.h>
  55. #include <sys/bus.h>
  56. #include <sys/sdt.h>
  57. #include <sys/syscallsubr.h>
  58. #include <opencrypto/cryptodev.h>
  59. #include <opencrypto/xform.h>
  60. SDT_PROVIDER_DECLARE(opencrypto);
  61. SDT_PROBE_DEFINE1(opencrypto, dev, ioctl, error, "int"/*line number*/);
  62. #ifdef COMPAT_FREEBSD12
  63. /*
  64. * Previously, most ioctls were performed against a cloned descriptor
  65. * of /dev/crypto obtained via CRIOGET. Now all ioctls are performed
  66. * against /dev/crypto directly.
  67. */
  68. #define CRIOGET _IOWR('c', 100, uint32_t)
  69. #endif
  70. /* the following are done against the cloned descriptor */
  71. #ifdef COMPAT_FREEBSD32
  72. #include <sys/mount.h>
  73. #include <compat/freebsd32/freebsd32.h>
  74. struct session_op32 {
  75. uint32_t cipher;
  76. uint32_t mac;
  77. uint32_t keylen;
  78. uint32_t key;
  79. int mackeylen;
  80. uint32_t mackey;
  81. uint32_t ses;
  82. };
  83. struct session2_op32 {
  84. uint32_t cipher;
  85. uint32_t mac;
  86. uint32_t keylen;
  87. uint32_t key;
  88. int mackeylen;
  89. uint32_t mackey;
  90. uint32_t ses;
  91. int crid;
  92. int pad[4];
  93. };
  94. struct crypt_op32 {
  95. uint32_t ses;
  96. uint16_t op;
  97. uint16_t flags;
  98. u_int len;
  99. uint32_t src, dst;
  100. uint32_t mac;
  101. uint32_t iv;
  102. };
  103. struct crypt_aead32 {
  104. uint32_t ses;
  105. uint16_t op;
  106. uint16_t flags;
  107. u_int len;
  108. u_int aadlen;
  109. u_int ivlen;
  110. uint32_t src;
  111. uint32_t dst;
  112. uint32_t aad;
  113. uint32_t tag;
  114. uint32_t iv;
  115. };
  116. struct crparam32 {
  117. uint32_t crp_p;
  118. u_int crp_nbits;
  119. };
  120. struct crypt_kop32 {
  121. u_int crk_op;
  122. u_int crk_status;
  123. u_short crk_iparams;
  124. u_short crk_oparams;
  125. u_int crk_crid;
  126. struct crparam32 crk_param[CRK_MAXPARAM];
  127. };
  128. #define CIOCGSESSION32 _IOWR('c', 101, struct session_op32)
  129. #define CIOCCRYPT32 _IOWR('c', 103, struct crypt_op32)
  130. #define CIOCKEY32 _IOWR('c', 104, struct crypt_kop32)
  131. #define CIOCGSESSION232 _IOWR('c', 106, struct session2_op32)
  132. #define CIOCKEY232 _IOWR('c', 107, struct crypt_kop32)
  133. #define CIOCCRYPTAEAD32 _IOWR('c', 109, struct crypt_aead32)
  134. static void
  135. session_op_from_32(const struct session_op32 *from, struct session2_op *to)
  136. {
  137. memset(to, 0, sizeof(*to));
  138. CP(*from, *to, cipher);
  139. CP(*from, *to, mac);
  140. CP(*from, *to, keylen);
  141. PTRIN_CP(*from, *to, key);
  142. CP(*from, *to, mackeylen);
  143. PTRIN_CP(*from, *to, mackey);
  144. CP(*from, *to, ses);
  145. to->crid = CRYPTOCAP_F_HARDWARE;
  146. }
  147. static void
  148. session2_op_from_32(const struct session2_op32 *from, struct session2_op *to)
  149. {
  150. session_op_from_32((const struct session_op32 *)from, to);
  151. CP(*from, *to, crid);
  152. }
  153. static void
  154. session_op_to_32(const struct session2_op *from, struct session_op32 *to)
  155. {
  156. CP(*from, *to, cipher);
  157. CP(*from, *to, mac);
  158. CP(*from, *to, keylen);
  159. PTROUT_CP(*from, *to, key);
  160. CP(*from, *to, mackeylen);
  161. PTROUT_CP(*from, *to, mackey);
  162. CP(*from, *to, ses);
  163. }
  164. static void
  165. session2_op_to_32(const struct session2_op *from, struct session2_op32 *to)
  166. {
  167. session_op_to_32(from, (struct session_op32 *)to);
  168. CP(*from, *to, crid);
  169. }
  170. static void
  171. crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to)
  172. {
  173. CP(*from, *to, ses);
  174. CP(*from, *to, op);
  175. CP(*from, *to, flags);
  176. CP(*from, *to, len);
  177. PTRIN_CP(*from, *to, src);
  178. PTRIN_CP(*from, *to, dst);
  179. PTRIN_CP(*from, *to, mac);
  180. PTRIN_CP(*from, *to, iv);
  181. }
  182. static void
  183. crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to)
  184. {
  185. CP(*from, *to, ses);
  186. CP(*from, *to, op);
  187. CP(*from, *to, flags);
  188. CP(*from, *to, len);
  189. PTROUT_CP(*from, *to, src);
  190. PTROUT_CP(*from, *to, dst);
  191. PTROUT_CP(*from, *to, mac);
  192. PTROUT_CP(*from, *to, iv);
  193. }
  194. static void
  195. crypt_aead_from_32(const struct crypt_aead32 *from, struct crypt_aead *to)
  196. {
  197. CP(*from, *to, ses);
  198. CP(*from, *to, op);
  199. CP(*from, *to, flags);
  200. CP(*from, *to, len);
  201. CP(*from, *to, aadlen);
  202. CP(*from, *to, ivlen);
  203. PTRIN_CP(*from, *to, src);
  204. PTRIN_CP(*from, *to, dst);
  205. PTRIN_CP(*from, *to, aad);
  206. PTRIN_CP(*from, *to, tag);
  207. PTRIN_CP(*from, *to, iv);
  208. }
  209. static void
  210. crypt_aead_to_32(const struct crypt_aead *from, struct crypt_aead32 *to)
  211. {
  212. CP(*from, *to, ses);
  213. CP(*from, *to, op);
  214. CP(*from, *to, flags);
  215. CP(*from, *to, len);
  216. CP(*from, *to, aadlen);
  217. CP(*from, *to, ivlen);
  218. PTROUT_CP(*from, *to, src);
  219. PTROUT_CP(*from, *to, dst);
  220. PTROUT_CP(*from, *to, aad);
  221. PTROUT_CP(*from, *to, tag);
  222. PTROUT_CP(*from, *to, iv);
  223. }
  224. static void
  225. crparam_from_32(const struct crparam32 *from, struct crparam *to)
  226. {
  227. PTRIN_CP(*from, *to, crp_p);
  228. CP(*from, *to, crp_nbits);
  229. }
  230. static void
  231. crparam_to_32(const struct crparam *from, struct crparam32 *to)
  232. {
  233. PTROUT_CP(*from, *to, crp_p);
  234. CP(*from, *to, crp_nbits);
  235. }
  236. static void
  237. crypt_kop_from_32(const struct crypt_kop32 *from, struct crypt_kop *to)
  238. {
  239. int i;
  240. CP(*from, *to, crk_op);
  241. CP(*from, *to, crk_status);
  242. CP(*from, *to, crk_iparams);
  243. CP(*from, *to, crk_oparams);
  244. CP(*from, *to, crk_crid);
  245. for (i = 0; i < CRK_MAXPARAM; i++)
  246. crparam_from_32(&from->crk_param[i], &to->crk_param[i]);
  247. }
  248. static void
  249. crypt_kop_to_32(const struct crypt_kop *from, struct crypt_kop32 *to)
  250. {
  251. int i;
  252. CP(*from, *to, crk_op);
  253. CP(*from, *to, crk_status);
  254. CP(*from, *to, crk_iparams);
  255. CP(*from, *to, crk_oparams);
  256. CP(*from, *to, crk_crid);
  257. for (i = 0; i < CRK_MAXPARAM; i++)
  258. crparam_to_32(&from->crk_param[i], &to->crk_param[i]);
  259. }
  260. #endif
  261. static void
  262. session2_op_from_op(const struct session_op *from, struct session2_op *to)
  263. {
  264. memset(to, 0, sizeof(*to));
  265. memcpy(to, from, sizeof(*from));
  266. to->crid = CRYPTOCAP_F_HARDWARE;
  267. }
  268. static void
  269. session2_op_to_op(const struct session2_op *from, struct session_op *to)
  270. {
  271. memcpy(to, from, sizeof(*to));
  272. }
  273. struct csession {
  274. TAILQ_ENTRY(csession) next;
  275. crypto_session_t cses;
  276. volatile u_int refs;
  277. uint32_t ses;
  278. struct mtx lock; /* for op submission */
  279. struct enc_xform *txform;
  280. int hashsize;
  281. int ivsize;
  282. int mode;
  283. void *key;
  284. void *mackey;
  285. };
  286. struct cryptop_data {
  287. struct csession *cse;
  288. char *buf;
  289. char *obuf;
  290. char *aad;
  291. bool done;
  292. };
  293. struct fcrypt {
  294. TAILQ_HEAD(csessionlist, csession) csessions;
  295. int sesn;
  296. struct mtx lock;
  297. };
  298. static bool use_outputbuffers;
  299. SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_use_output, CTLFLAG_RW,
  300. &use_outputbuffers, 0,
  301. "Use separate output buffers for /dev/crypto requests.");
  302. static bool use_separate_aad;
  303. SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_separate_aad, CTLFLAG_RW,
  304. &use_separate_aad, 0,
  305. "Use separate AAD buffer for /dev/crypto requests.");
  306. static struct timeval warninterval = { .tv_sec = 60, .tv_usec = 0 };
  307. SYSCTL_TIMEVAL_SEC(_kern, OID_AUTO, cryptodev_warn_interval, CTLFLAG_RW,
  308. &warninterval,
  309. "Delay in seconds between warnings of deprecated /dev/crypto algorithms");
  310. /*
  311. * Check a crypto identifier to see if it requested
  312. * a software device/driver. This can be done either
  313. * by device name/class or through search constraints.
  314. */
  315. static int
  316. checkforsoftware(int *cridp)
  317. {
  318. int crid;
  319. crid = *cridp;
  320. if (!crypto_devallowsoft) {
  321. if (crid & CRYPTOCAP_F_SOFTWARE) {
  322. if (crid & CRYPTOCAP_F_HARDWARE) {
  323. *cridp = CRYPTOCAP_F_HARDWARE;
  324. return 0;
  325. }
  326. return EINVAL;
  327. }
  328. if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
  329. (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
  330. return EINVAL;
  331. }
  332. return 0;
  333. }
  334. static int
  335. cse_create(struct fcrypt *fcr, struct session2_op *sop)
  336. {
  337. struct crypto_session_params csp;
  338. struct csession *cse;
  339. struct enc_xform *txform;
  340. struct auth_hash *thash;
  341. void *key = NULL;
  342. void *mackey = NULL;
  343. crypto_session_t cses;
  344. int crid, error;
  345. switch (sop->cipher) {
  346. case 0:
  347. txform = NULL;
  348. break;
  349. case CRYPTO_AES_CBC:
  350. txform = &enc_xform_rijndael128;
  351. break;
  352. case CRYPTO_AES_XTS:
  353. txform = &enc_xform_aes_xts;
  354. break;
  355. case CRYPTO_NULL_CBC:
  356. txform = &enc_xform_null;
  357. break;
  358. case CRYPTO_CAMELLIA_CBC:
  359. txform = &enc_xform_camellia;
  360. break;
  361. case CRYPTO_AES_ICM:
  362. txform = &enc_xform_aes_icm;
  363. break;
  364. case CRYPTO_AES_NIST_GCM_16:
  365. txform = &enc_xform_aes_nist_gcm;
  366. break;
  367. case CRYPTO_CHACHA20:
  368. txform = &enc_xform_chacha20;
  369. break;
  370. case CRYPTO_AES_CCM_16:
  371. txform = &enc_xform_ccm;
  372. break;
  373. default:
  374. CRYPTDEB("invalid cipher");
  375. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  376. return (EINVAL);
  377. }
  378. switch (sop->mac) {
  379. case 0:
  380. thash = NULL;
  381. break;
  382. case CRYPTO_POLY1305:
  383. thash = &auth_hash_poly1305;
  384. break;
  385. case CRYPTO_SHA1_HMAC:
  386. thash = &auth_hash_hmac_sha1;
  387. break;
  388. case CRYPTO_SHA2_224_HMAC:
  389. thash = &auth_hash_hmac_sha2_224;
  390. break;
  391. case CRYPTO_SHA2_256_HMAC:
  392. thash = &auth_hash_hmac_sha2_256;
  393. break;
  394. case CRYPTO_SHA2_384_HMAC:
  395. thash = &auth_hash_hmac_sha2_384;
  396. break;
  397. case CRYPTO_SHA2_512_HMAC:
  398. thash = &auth_hash_hmac_sha2_512;
  399. break;
  400. case CRYPTO_RIPEMD160_HMAC:
  401. thash = &auth_hash_hmac_ripemd_160;
  402. break;
  403. #ifdef COMPAT_FREEBSD12
  404. case CRYPTO_AES_128_NIST_GMAC:
  405. case CRYPTO_AES_192_NIST_GMAC:
  406. case CRYPTO_AES_256_NIST_GMAC:
  407. /* Should always be paired with GCM. */
  408. if (sop->cipher != CRYPTO_AES_NIST_GCM_16) {
  409. CRYPTDEB("GMAC without GCM");
  410. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  411. return (EINVAL);
  412. }
  413. break;
  414. #endif
  415. case CRYPTO_AES_NIST_GMAC:
  416. switch (sop->mackeylen * 8) {
  417. case 128:
  418. thash = &auth_hash_nist_gmac_aes_128;
  419. break;
  420. case 192:
  421. thash = &auth_hash_nist_gmac_aes_192;
  422. break;
  423. case 256:
  424. thash = &auth_hash_nist_gmac_aes_256;
  425. break;
  426. default:
  427. CRYPTDEB("invalid GMAC key length");
  428. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  429. return (EINVAL);
  430. }
  431. break;
  432. case CRYPTO_AES_CCM_CBC_MAC:
  433. switch (sop->mackeylen) {
  434. case 16:
  435. thash = &auth_hash_ccm_cbc_mac_128;
  436. break;
  437. case 24:
  438. thash = &auth_hash_ccm_cbc_mac_192;
  439. break;
  440. case 32:
  441. thash = &auth_hash_ccm_cbc_mac_256;
  442. break;
  443. default:
  444. CRYPTDEB("Invalid CBC MAC key size %d", sop->keylen);
  445. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  446. return (EINVAL);
  447. }
  448. break;
  449. case CRYPTO_SHA1:
  450. thash = &auth_hash_sha1;
  451. break;
  452. case CRYPTO_SHA2_224:
  453. thash = &auth_hash_sha2_224;
  454. break;
  455. case CRYPTO_SHA2_256:
  456. thash = &auth_hash_sha2_256;
  457. break;
  458. case CRYPTO_SHA2_384:
  459. thash = &auth_hash_sha2_384;
  460. break;
  461. case CRYPTO_SHA2_512:
  462. thash = &auth_hash_sha2_512;
  463. break;
  464. case CRYPTO_NULL_HMAC:
  465. thash = &auth_hash_null;
  466. break;
  467. case CRYPTO_BLAKE2B:
  468. thash = &auth_hash_blake2b;
  469. break;
  470. case CRYPTO_BLAKE2S:
  471. thash = &auth_hash_blake2s;
  472. break;
  473. default:
  474. CRYPTDEB("invalid mac");
  475. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  476. return (EINVAL);
  477. }
  478. if (txform == NULL && thash == NULL) {
  479. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  480. return (EINVAL);
  481. }
  482. memset(&csp, 0, sizeof(csp));
  483. if (use_outputbuffers)
  484. csp.csp_flags |= CSP_F_SEPARATE_OUTPUT;
  485. if (sop->cipher == CRYPTO_AES_NIST_GCM_16) {
  486. switch (sop->mac) {
  487. #ifdef COMPAT_FREEBSD12
  488. case CRYPTO_AES_128_NIST_GMAC:
  489. case CRYPTO_AES_192_NIST_GMAC:
  490. case CRYPTO_AES_256_NIST_GMAC:
  491. if (sop->keylen != sop->mackeylen) {
  492. SDT_PROBE1(opencrypto, dev, ioctl, error,
  493. __LINE__);
  494. return (EINVAL);
  495. }
  496. break;
  497. #endif
  498. case 0:
  499. break;
  500. default:
  501. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  502. return (EINVAL);
  503. }
  504. csp.csp_mode = CSP_MODE_AEAD;
  505. } else if (sop->cipher == CRYPTO_AES_CCM_16) {
  506. switch (sop->mac) {
  507. #ifdef COMPAT_FREEBSD12
  508. case CRYPTO_AES_CCM_CBC_MAC:
  509. if (sop->keylen != sop->mackeylen) {
  510. SDT_PROBE1(opencrypto, dev, ioctl, error,
  511. __LINE__);
  512. return (EINVAL);
  513. }
  514. thash = NULL;
  515. break;
  516. #endif
  517. case 0:
  518. break;
  519. default:
  520. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  521. return (EINVAL);
  522. }
  523. csp.csp_mode = CSP_MODE_AEAD;
  524. } else if (txform != NULL && thash != NULL)
  525. csp.csp_mode = CSP_MODE_ETA;
  526. else if (txform != NULL)
  527. csp.csp_mode = CSP_MODE_CIPHER;
  528. else
  529. csp.csp_mode = CSP_MODE_DIGEST;
  530. switch (csp.csp_mode) {
  531. case CSP_MODE_AEAD:
  532. case CSP_MODE_ETA:
  533. if (use_separate_aad)
  534. csp.csp_flags |= CSP_F_SEPARATE_AAD;
  535. break;
  536. }
  537. if (txform != NULL) {
  538. csp.csp_cipher_alg = txform->type;
  539. csp.csp_cipher_klen = sop->keylen;
  540. if (sop->keylen > txform->maxkey ||
  541. sop->keylen < txform->minkey) {
  542. CRYPTDEB("invalid cipher parameters");
  543. error = EINVAL;
  544. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  545. goto bail;
  546. }
  547. key = malloc(csp.csp_cipher_klen, M_XDATA, M_WAITOK);
  548. error = copyin(sop->key, key, csp.csp_cipher_klen);
  549. if (error) {
  550. CRYPTDEB("invalid key");
  551. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  552. goto bail;
  553. }
  554. csp.csp_cipher_key = key;
  555. csp.csp_ivlen = txform->ivsize;
  556. }
  557. if (thash != NULL) {
  558. csp.csp_auth_alg = thash->type;
  559. csp.csp_auth_klen = sop->mackeylen;
  560. if (sop->mackeylen > thash->keysize || sop->mackeylen < 0) {
  561. CRYPTDEB("invalid mac key length");
  562. error = EINVAL;
  563. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  564. goto bail;
  565. }
  566. if (csp.csp_auth_klen != 0) {
  567. mackey = malloc(csp.csp_auth_klen, M_XDATA, M_WAITOK);
  568. error = copyin(sop->mackey, mackey, csp.csp_auth_klen);
  569. if (error) {
  570. CRYPTDEB("invalid mac key");
  571. SDT_PROBE1(opencrypto, dev, ioctl, error,
  572. __LINE__);
  573. goto bail;
  574. }
  575. csp.csp_auth_key = mackey;
  576. }
  577. if (csp.csp_auth_alg == CRYPTO_AES_NIST_GMAC)
  578. csp.csp_ivlen = AES_GCM_IV_LEN;
  579. if (csp.csp_auth_alg == CRYPTO_AES_CCM_CBC_MAC)
  580. csp.csp_ivlen = AES_CCM_IV_LEN;
  581. }
  582. crid = sop->crid;
  583. error = checkforsoftware(&crid);
  584. if (error) {
  585. CRYPTDEB("checkforsoftware");
  586. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  587. goto bail;
  588. }
  589. error = crypto_newsession(&cses, &csp, crid);
  590. if (error) {
  591. CRYPTDEB("crypto_newsession");
  592. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  593. goto bail;
  594. }
  595. cse = malloc(sizeof(struct csession), M_XDATA, M_WAITOK | M_ZERO);
  596. mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
  597. refcount_init(&cse->refs, 1);
  598. cse->key = key;
  599. cse->mackey = mackey;
  600. cse->mode = csp.csp_mode;
  601. cse->cses = cses;
  602. cse->txform = txform;
  603. if (thash != NULL)
  604. cse->hashsize = thash->hashsize;
  605. else if (csp.csp_cipher_alg == CRYPTO_AES_NIST_GCM_16)
  606. cse->hashsize = AES_GMAC_HASH_LEN;
  607. else if (csp.csp_cipher_alg == CRYPTO_AES_CCM_16)
  608. cse->hashsize = AES_CBC_MAC_HASH_LEN;
  609. cse->ivsize = csp.csp_ivlen;
  610. mtx_lock(&fcr->lock);
  611. TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
  612. cse->ses = fcr->sesn++;
  613. mtx_unlock(&fcr->lock);
  614. sop->ses = cse->ses;
  615. /* return hardware/driver id */
  616. sop->crid = crypto_ses2hid(cse->cses);
  617. bail:
  618. if (error) {
  619. free(key, M_XDATA);
  620. free(mackey, M_XDATA);
  621. }
  622. return (error);
  623. }
  624. static struct csession *
  625. cse_find(struct fcrypt *fcr, u_int ses)
  626. {
  627. struct csession *cse;
  628. mtx_lock(&fcr->lock);
  629. TAILQ_FOREACH(cse, &fcr->csessions, next) {
  630. if (cse->ses == ses) {
  631. refcount_acquire(&cse->refs);
  632. mtx_unlock(&fcr->lock);
  633. return (cse);
  634. }
  635. }
  636. mtx_unlock(&fcr->lock);
  637. return (NULL);
  638. }
  639. static void
  640. cse_free(struct csession *cse)
  641. {
  642. if (!refcount_release(&cse->refs))
  643. return;
  644. crypto_freesession(cse->cses);
  645. mtx_destroy(&cse->lock);
  646. if (cse->key)
  647. free(cse->key, M_XDATA);
  648. if (cse->mackey)
  649. free(cse->mackey, M_XDATA);
  650. free(cse, M_XDATA);
  651. }
  652. static bool
  653. cse_delete(struct fcrypt *fcr, u_int ses)
  654. {
  655. struct csession *cse;
  656. mtx_lock(&fcr->lock);
  657. TAILQ_FOREACH(cse, &fcr->csessions, next) {
  658. if (cse->ses == ses) {
  659. TAILQ_REMOVE(&fcr->csessions, cse, next);
  660. mtx_unlock(&fcr->lock);
  661. cse_free(cse);
  662. return (true);
  663. }
  664. }
  665. mtx_unlock(&fcr->lock);
  666. return (false);
  667. }
  668. static struct cryptop_data *
  669. cod_alloc(struct csession *cse, size_t aad_len, size_t len)
  670. {
  671. struct cryptop_data *cod;
  672. cod = malloc(sizeof(struct cryptop_data), M_XDATA, M_WAITOK | M_ZERO);
  673. cod->cse = cse;
  674. if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_AAD) {
  675. if (aad_len != 0)
  676. cod->aad = malloc(aad_len, M_XDATA, M_WAITOK);
  677. cod->buf = malloc(len, M_XDATA, M_WAITOK);
  678. } else
  679. cod->buf = malloc(aad_len + len, M_XDATA, M_WAITOK);
  680. if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_OUTPUT)
  681. cod->obuf = malloc(len, M_XDATA, M_WAITOK);
  682. return (cod);
  683. }
  684. static void
  685. cod_free(struct cryptop_data *cod)
  686. {
  687. free(cod->aad, M_XDATA);
  688. free(cod->obuf, M_XDATA);
  689. free(cod->buf, M_XDATA);
  690. free(cod, M_XDATA);
  691. }
  692. static int
  693. cryptodev_cb(struct cryptop *crp)
  694. {
  695. struct cryptop_data *cod = crp->crp_opaque;
  696. /*
  697. * Lock to ensure the wakeup() is not missed by the loops
  698. * waiting on cod->done in cryptodev_op() and
  699. * cryptodev_aead().
  700. */
  701. mtx_lock(&cod->cse->lock);
  702. cod->done = true;
  703. mtx_unlock(&cod->cse->lock);
  704. wakeup(cod);
  705. return (0);
  706. }
  707. static int
  708. cryptodev_op(struct csession *cse, const struct crypt_op *cop)
  709. {
  710. struct cryptop_data *cod = NULL;
  711. struct cryptop *crp = NULL;
  712. char *dst;
  713. int error;
  714. if (cop->len > 256*1024-4) {
  715. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  716. return (E2BIG);
  717. }
  718. if (cse->txform) {
  719. if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) {
  720. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  721. return (EINVAL);
  722. }
  723. }
  724. if (cop->mac && cse->hashsize == 0) {
  725. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  726. return (EINVAL);
  727. }
  728. /*
  729. * The COP_F_CIPHER_FIRST flag predates explicit session
  730. * modes, but the only way it was used was for EtA so allow it
  731. * as long as it is consistent with EtA.
  732. */
  733. if (cop->flags & COP_F_CIPHER_FIRST) {
  734. if (cop->op != COP_ENCRYPT) {
  735. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  736. return (EINVAL);
  737. }
  738. }
  739. cod = cod_alloc(cse, 0, cop->len + cse->hashsize);
  740. dst = cop->dst;
  741. crp = crypto_getreq(cse->cses, M_WAITOK);
  742. error = copyin(cop->src, cod->buf, cop->len);
  743. if (error) {
  744. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  745. goto bail;
  746. }
  747. crp->crp_payload_start = 0;
  748. crp->crp_payload_length = cop->len;
  749. if (cse->hashsize)
  750. crp->crp_digest_start = cop->len;
  751. switch (cse->mode) {
  752. case CSP_MODE_COMPRESS:
  753. switch (cop->op) {
  754. case COP_ENCRYPT:
  755. crp->crp_op = CRYPTO_OP_COMPRESS;
  756. break;
  757. case COP_DECRYPT:
  758. crp->crp_op = CRYPTO_OP_DECOMPRESS;
  759. break;
  760. default:
  761. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  762. error = EINVAL;
  763. goto bail;
  764. }
  765. break;
  766. case CSP_MODE_CIPHER:
  767. switch (cop->op) {
  768. case COP_ENCRYPT:
  769. crp->crp_op = CRYPTO_OP_ENCRYPT;
  770. break;
  771. case COP_DECRYPT:
  772. crp->crp_op = CRYPTO_OP_DECRYPT;
  773. break;
  774. default:
  775. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  776. error = EINVAL;
  777. goto bail;
  778. }
  779. break;
  780. case CSP_MODE_DIGEST:
  781. switch (cop->op) {
  782. case 0:
  783. case COP_ENCRYPT:
  784. case COP_DECRYPT:
  785. crp->crp_op = CRYPTO_OP_COMPUTE_DIGEST;
  786. if (cod->obuf != NULL)
  787. crp->crp_digest_start = 0;
  788. break;
  789. default:
  790. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  791. error = EINVAL;
  792. goto bail;
  793. }
  794. break;
  795. case CSP_MODE_ETA:
  796. switch (cop->op) {
  797. case COP_ENCRYPT:
  798. crp->crp_op = CRYPTO_OP_ENCRYPT |
  799. CRYPTO_OP_COMPUTE_DIGEST;
  800. break;
  801. case COP_DECRYPT:
  802. crp->crp_op = CRYPTO_OP_DECRYPT |
  803. CRYPTO_OP_VERIFY_DIGEST;
  804. break;
  805. default:
  806. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  807. error = EINVAL;
  808. goto bail;
  809. }
  810. break;
  811. default:
  812. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  813. error = EINVAL;
  814. goto bail;
  815. }
  816. crp->crp_flags = CRYPTO_F_CBIMM | (cop->flags & COP_F_BATCH);
  817. crypto_use_buf(crp, cod->buf, cop->len + cse->hashsize);
  818. if (cod->obuf)
  819. crypto_use_output_buf(crp, cod->obuf, cop->len + cse->hashsize);
  820. crp->crp_callback = cryptodev_cb;
  821. crp->crp_opaque = cod;
  822. if (cop->iv) {
  823. if (cse->ivsize == 0) {
  824. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  825. error = EINVAL;
  826. goto bail;
  827. }
  828. error = copyin(cop->iv, crp->crp_iv, cse->ivsize);
  829. if (error) {
  830. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  831. goto bail;
  832. }
  833. crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
  834. } else if (cse->ivsize != 0) {
  835. crp->crp_iv_start = 0;
  836. crp->crp_payload_start += cse->ivsize;
  837. crp->crp_payload_length -= cse->ivsize;
  838. dst += cse->ivsize;
  839. }
  840. if (cop->mac != NULL && crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
  841. error = copyin(cop->mac, cod->buf + crp->crp_digest_start,
  842. cse->hashsize);
  843. if (error) {
  844. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  845. goto bail;
  846. }
  847. }
  848. again:
  849. /*
  850. * Let the dispatch run unlocked, then, interlock against the
  851. * callback before checking if the operation completed and going
  852. * to sleep. This insures drivers don't inherit our lock which
  853. * results in a lock order reversal between crypto_dispatch forced
  854. * entry and the crypto_done callback into us.
  855. */
  856. error = crypto_dispatch(crp);
  857. if (error != 0) {
  858. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  859. goto bail;
  860. }
  861. mtx_lock(&cse->lock);
  862. while (!cod->done)
  863. mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
  864. mtx_unlock(&cse->lock);
  865. if (crp->crp_etype == EAGAIN) {
  866. crp->crp_etype = 0;
  867. crp->crp_flags &= ~CRYPTO_F_DONE;
  868. cod->done = false;
  869. goto again;
  870. }
  871. if (crp->crp_etype != 0) {
  872. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  873. error = crp->crp_etype;
  874. goto bail;
  875. }
  876. if (cop->dst != NULL) {
  877. error = copyout(cod->obuf != NULL ? cod->obuf :
  878. cod->buf + crp->crp_payload_start, dst,
  879. crp->crp_payload_length);
  880. if (error) {
  881. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  882. goto bail;
  883. }
  884. }
  885. if (cop->mac != NULL && (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
  886. error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
  887. crp->crp_digest_start, cop->mac, cse->hashsize);
  888. if (error) {
  889. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  890. goto bail;
  891. }
  892. }
  893. bail:
  894. crypto_freereq(crp);
  895. cod_free(cod);
  896. return (error);
  897. }
  898. static int
  899. cryptodev_aead(struct csession *cse, struct crypt_aead *caead)
  900. {
  901. struct cryptop_data *cod = NULL;
  902. struct cryptop *crp = NULL;
  903. char *dst;
  904. int error;
  905. if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) {
  906. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  907. return (E2BIG);
  908. }
  909. if (cse->txform == NULL || cse->hashsize == 0 || caead->tag == NULL ||
  910. (caead->len % cse->txform->blocksize) != 0) {
  911. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  912. return (EINVAL);
  913. }
  914. /*
  915. * The COP_F_CIPHER_FIRST flag predates explicit session
  916. * modes, but the only way it was used was for EtA so allow it
  917. * as long as it is consistent with EtA.
  918. */
  919. if (caead->flags & COP_F_CIPHER_FIRST) {
  920. if (caead->op != COP_ENCRYPT) {
  921. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  922. return (EINVAL);
  923. }
  924. }
  925. cod = cod_alloc(cse, caead->aadlen, caead->len + cse->hashsize);
  926. dst = caead->dst;
  927. crp = crypto_getreq(cse->cses, M_WAITOK);
  928. if (cod->aad != NULL)
  929. error = copyin(caead->aad, cod->aad, caead->aadlen);
  930. else
  931. error = copyin(caead->aad, cod->buf, caead->aadlen);
  932. if (error) {
  933. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  934. goto bail;
  935. }
  936. crp->crp_aad = cod->aad;
  937. crp->crp_aad_start = 0;
  938. crp->crp_aad_length = caead->aadlen;
  939. if (cod->aad != NULL)
  940. crp->crp_payload_start = 0;
  941. else
  942. crp->crp_payload_start = caead->aadlen;
  943. error = copyin(caead->src, cod->buf + crp->crp_payload_start,
  944. caead->len);
  945. if (error) {
  946. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  947. goto bail;
  948. }
  949. crp->crp_payload_length = caead->len;
  950. if (caead->op == COP_ENCRYPT && cod->obuf != NULL)
  951. crp->crp_digest_start = crp->crp_payload_output_start +
  952. caead->len;
  953. else
  954. crp->crp_digest_start = crp->crp_payload_start + caead->len;
  955. switch (cse->mode) {
  956. case CSP_MODE_AEAD:
  957. case CSP_MODE_ETA:
  958. switch (caead->op) {
  959. case COP_ENCRYPT:
  960. crp->crp_op = CRYPTO_OP_ENCRYPT |
  961. CRYPTO_OP_COMPUTE_DIGEST;
  962. break;
  963. case COP_DECRYPT:
  964. crp->crp_op = CRYPTO_OP_DECRYPT |
  965. CRYPTO_OP_VERIFY_DIGEST;
  966. break;
  967. default:
  968. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  969. error = EINVAL;
  970. goto bail;
  971. }
  972. break;
  973. default:
  974. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  975. error = EINVAL;
  976. goto bail;
  977. }
  978. crp->crp_flags = CRYPTO_F_CBIMM | (caead->flags & COP_F_BATCH);
  979. crypto_use_buf(crp, cod->buf, crp->crp_payload_start + caead->len +
  980. cse->hashsize);
  981. if (cod->obuf != NULL)
  982. crypto_use_output_buf(crp, cod->obuf, caead->len +
  983. cse->hashsize);
  984. crp->crp_callback = cryptodev_cb;
  985. crp->crp_opaque = cod;
  986. if (caead->iv) {
  987. /*
  988. * Permit a 16-byte IV for AES-XTS, but only use the
  989. * first 8 bytes as a block number.
  990. */
  991. if (cse->mode == CSP_MODE_ETA &&
  992. caead->ivlen == AES_BLOCK_LEN &&
  993. cse->ivsize == AES_XTS_IV_LEN)
  994. caead->ivlen = AES_XTS_IV_LEN;
  995. if (caead->ivlen != cse->ivsize) {
  996. error = EINVAL;
  997. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  998. goto bail;
  999. }
  1000. error = copyin(caead->iv, crp->crp_iv, cse->ivsize);
  1001. if (error) {
  1002. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1003. goto bail;
  1004. }
  1005. crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
  1006. } else {
  1007. crp->crp_iv_start = crp->crp_payload_start;
  1008. crp->crp_payload_start += cse->ivsize;
  1009. crp->crp_payload_length -= cse->ivsize;
  1010. dst += cse->ivsize;
  1011. }
  1012. if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
  1013. error = copyin(caead->tag, cod->buf + crp->crp_digest_start,
  1014. cse->hashsize);
  1015. if (error) {
  1016. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1017. goto bail;
  1018. }
  1019. }
  1020. again:
  1021. /*
  1022. * Let the dispatch run unlocked, then, interlock against the
  1023. * callback before checking if the operation completed and going
  1024. * to sleep. This insures drivers don't inherit our lock which
  1025. * results in a lock order reversal between crypto_dispatch forced
  1026. * entry and the crypto_done callback into us.
  1027. */
  1028. error = crypto_dispatch(crp);
  1029. if (error != 0) {
  1030. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1031. goto bail;
  1032. }
  1033. mtx_lock(&cse->lock);
  1034. while (!cod->done)
  1035. mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
  1036. mtx_unlock(&cse->lock);
  1037. if (crp->crp_etype == EAGAIN) {
  1038. crp->crp_etype = 0;
  1039. crp->crp_flags &= ~CRYPTO_F_DONE;
  1040. cod->done = false;
  1041. goto again;
  1042. }
  1043. if (crp->crp_etype != 0) {
  1044. error = crp->crp_etype;
  1045. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1046. goto bail;
  1047. }
  1048. if (caead->dst != NULL) {
  1049. error = copyout(cod->obuf != NULL ? cod->obuf :
  1050. cod->buf + crp->crp_payload_start, dst,
  1051. crp->crp_payload_length);
  1052. if (error) {
  1053. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1054. goto bail;
  1055. }
  1056. }
  1057. if ((crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
  1058. error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
  1059. crp->crp_digest_start, caead->tag, cse->hashsize);
  1060. if (error) {
  1061. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1062. goto bail;
  1063. }
  1064. }
  1065. bail:
  1066. crypto_freereq(crp);
  1067. cod_free(cod);
  1068. return (error);
  1069. }
  1070. static void
  1071. cryptodevkey_cb(struct cryptkop *krp)
  1072. {
  1073. wakeup_one(krp);
  1074. }
  1075. static int
  1076. cryptodev_key(struct crypt_kop *kop)
  1077. {
  1078. struct cryptkop *krp = NULL;
  1079. int error = EINVAL;
  1080. int in, out, size, i;
  1081. if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
  1082. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1083. return (EFBIG);
  1084. }
  1085. in = kop->crk_iparams;
  1086. out = kop->crk_oparams;
  1087. switch (kop->crk_op) {
  1088. case CRK_MOD_EXP:
  1089. if (in == 3 && out == 1)
  1090. break;
  1091. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1092. return (EINVAL);
  1093. case CRK_MOD_EXP_CRT:
  1094. if (in == 6 && out == 1)
  1095. break;
  1096. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1097. return (EINVAL);
  1098. case CRK_DSA_SIGN:
  1099. if (in == 5 && out == 2)
  1100. break;
  1101. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1102. return (EINVAL);
  1103. case CRK_DSA_VERIFY:
  1104. if (in == 7 && out == 0)
  1105. break;
  1106. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1107. return (EINVAL);
  1108. case CRK_DH_COMPUTE_KEY:
  1109. if (in == 3 && out == 1)
  1110. break;
  1111. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1112. return (EINVAL);
  1113. default:
  1114. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1115. return (EINVAL);
  1116. }
  1117. krp = malloc(sizeof(*krp), M_XDATA, M_WAITOK | M_ZERO);
  1118. krp->krp_op = kop->crk_op;
  1119. krp->krp_status = kop->crk_status;
  1120. krp->krp_iparams = kop->crk_iparams;
  1121. krp->krp_oparams = kop->crk_oparams;
  1122. krp->krp_crid = kop->crk_crid;
  1123. krp->krp_status = 0;
  1124. krp->krp_callback = cryptodevkey_cb;
  1125. for (i = 0; i < CRK_MAXPARAM; i++) {
  1126. if (kop->crk_param[i].crp_nbits > 65536) {
  1127. /* Limit is the same as in OpenBSD */
  1128. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1129. goto fail;
  1130. }
  1131. krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
  1132. }
  1133. for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
  1134. size = (krp->krp_param[i].crp_nbits + 7) / 8;
  1135. if (size == 0)
  1136. continue;
  1137. krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
  1138. if (i >= krp->krp_iparams)
  1139. continue;
  1140. error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
  1141. if (error) {
  1142. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1143. goto fail;
  1144. }
  1145. }
  1146. error = crypto_kdispatch(krp);
  1147. if (error) {
  1148. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1149. goto fail;
  1150. }
  1151. error = tsleep(krp, PSOCK, "crydev", 0);
  1152. if (error) {
  1153. /* XXX can this happen? if so, how do we recover? */
  1154. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1155. goto fail;
  1156. }
  1157. kop->crk_crid = krp->krp_hid; /* device that did the work */
  1158. if (krp->krp_status != 0) {
  1159. error = krp->krp_status;
  1160. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1161. goto fail;
  1162. }
  1163. for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
  1164. size = (krp->krp_param[i].crp_nbits + 7) / 8;
  1165. if (size == 0)
  1166. continue;
  1167. error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
  1168. if (error) {
  1169. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1170. goto fail;
  1171. }
  1172. }
  1173. fail:
  1174. if (krp) {
  1175. kop->crk_status = krp->krp_status;
  1176. for (i = 0; i < CRK_MAXPARAM; i++) {
  1177. if (krp->krp_param[i].crp_p)
  1178. free(krp->krp_param[i].crp_p, M_XDATA);
  1179. }
  1180. free(krp, M_XDATA);
  1181. }
  1182. return (error);
  1183. }
  1184. static int
  1185. cryptodev_find(struct crypt_find_op *find)
  1186. {
  1187. device_t dev;
  1188. size_t fnlen = sizeof find->name;
  1189. if (find->crid != -1) {
  1190. dev = crypto_find_device_byhid(find->crid);
  1191. if (dev == NULL)
  1192. return (ENOENT);
  1193. strncpy(find->name, device_get_nameunit(dev), fnlen);
  1194. find->name[fnlen - 1] = '\x0';
  1195. } else {
  1196. find->name[fnlen - 1] = '\x0';
  1197. find->crid = crypto_find_driver(find->name);
  1198. if (find->crid == -1)
  1199. return (ENOENT);
  1200. }
  1201. return (0);
  1202. }
  1203. static void
  1204. fcrypt_dtor(void *data)
  1205. {
  1206. struct fcrypt *fcr = data;
  1207. struct csession *cse;
  1208. while ((cse = TAILQ_FIRST(&fcr->csessions))) {
  1209. TAILQ_REMOVE(&fcr->csessions, cse, next);
  1210. KASSERT(refcount_load(&cse->refs) == 1,
  1211. ("%s: crypto session %p with %d refs", __func__, cse,
  1212. refcount_load(&cse->refs)));
  1213. cse_free(cse);
  1214. }
  1215. mtx_destroy(&fcr->lock);
  1216. free(fcr, M_XDATA);
  1217. }
  1218. static int
  1219. crypto_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
  1220. {
  1221. struct fcrypt *fcr;
  1222. int error;
  1223. fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK | M_ZERO);
  1224. TAILQ_INIT(&fcr->csessions);
  1225. mtx_init(&fcr->lock, "fcrypt", NULL, MTX_DEF);
  1226. error = devfs_set_cdevpriv(fcr, fcrypt_dtor);
  1227. if (error)
  1228. fcrypt_dtor(fcr);
  1229. return (error);
  1230. }
  1231. static int
  1232. crypto_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
  1233. struct thread *td)
  1234. {
  1235. static struct timeval keywarn, featwarn;
  1236. struct fcrypt *fcr;
  1237. struct csession *cse;
  1238. struct session2_op *sop;
  1239. struct crypt_op *cop;
  1240. struct crypt_aead *caead;
  1241. struct crypt_kop *kop;
  1242. uint32_t ses;
  1243. int error = 0;
  1244. union {
  1245. struct session2_op sopc;
  1246. #ifdef COMPAT_FREEBSD32
  1247. struct crypt_op copc;
  1248. struct crypt_aead aeadc;
  1249. struct crypt_kop kopc;
  1250. #endif
  1251. } thunk;
  1252. #ifdef COMPAT_FREEBSD32
  1253. u_long cmd32;
  1254. void *data32;
  1255. cmd32 = 0;
  1256. data32 = NULL;
  1257. switch (cmd) {
  1258. case CIOCGSESSION32:
  1259. cmd32 = cmd;
  1260. data32 = data;
  1261. cmd = CIOCGSESSION;
  1262. data = (void *)&thunk.sopc;
  1263. session_op_from_32((struct session_op32 *)data32, &thunk.sopc);
  1264. break;
  1265. case CIOCGSESSION232:
  1266. cmd32 = cmd;
  1267. data32 = data;
  1268. cmd = CIOCGSESSION2;
  1269. data = (void *)&thunk.sopc;
  1270. session2_op_from_32((struct session2_op32 *)data32,
  1271. &thunk.sopc);
  1272. break;
  1273. case CIOCCRYPT32:
  1274. cmd32 = cmd;
  1275. data32 = data;
  1276. cmd = CIOCCRYPT;
  1277. data = (void *)&thunk.copc;
  1278. crypt_op_from_32((struct crypt_op32 *)data32, &thunk.copc);
  1279. break;
  1280. case CIOCCRYPTAEAD32:
  1281. cmd32 = cmd;
  1282. data32 = data;
  1283. cmd = CIOCCRYPTAEAD;
  1284. data = (void *)&thunk.aeadc;
  1285. crypt_aead_from_32((struct crypt_aead32 *)data32, &thunk.aeadc);
  1286. break;
  1287. case CIOCKEY32:
  1288. case CIOCKEY232:
  1289. cmd32 = cmd;
  1290. data32 = data;
  1291. if (cmd == CIOCKEY32)
  1292. cmd = CIOCKEY;
  1293. else
  1294. cmd = CIOCKEY2;
  1295. data = (void *)&thunk.kopc;
  1296. crypt_kop_from_32((struct crypt_kop32 *)data32, &thunk.kopc);
  1297. break;
  1298. }
  1299. #endif
  1300. devfs_get_cdevpriv((void **)&fcr);
  1301. switch (cmd) {
  1302. #ifdef COMPAT_FREEBSD12
  1303. case CRIOGET:
  1304. /*
  1305. * NB: This may fail in cases that the old
  1306. * implementation did not if the current process has
  1307. * restricted filesystem access (e.g. running in a
  1308. * jail that does not expose /dev/crypto or in
  1309. * capability mode).
  1310. */
  1311. error = kern_openat(td, AT_FDCWD, "/dev/crypto", UIO_SYSSPACE,
  1312. O_RDWR, 0);
  1313. if (error == 0)
  1314. *(uint32_t *)data = td->td_retval[0];
  1315. break;
  1316. #endif
  1317. case CIOCGSESSION:
  1318. case CIOCGSESSION2:
  1319. if (cmd == CIOCGSESSION) {
  1320. session2_op_from_op((void *)data, &thunk.sopc);
  1321. sop = &thunk.sopc;
  1322. } else
  1323. sop = (struct session2_op *)data;
  1324. error = cse_create(fcr, sop);
  1325. if (cmd == CIOCGSESSION && error == 0)
  1326. session2_op_to_op(sop, (void *)data);
  1327. break;
  1328. case CIOCFSESSION:
  1329. ses = *(uint32_t *)data;
  1330. if (!cse_delete(fcr, ses)) {
  1331. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1332. return (EINVAL);
  1333. }
  1334. break;
  1335. case CIOCCRYPT:
  1336. cop = (struct crypt_op *)data;
  1337. cse = cse_find(fcr, cop->ses);
  1338. if (cse == NULL) {
  1339. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1340. return (EINVAL);
  1341. }
  1342. error = cryptodev_op(cse, cop);
  1343. cse_free(cse);
  1344. break;
  1345. case CIOCKEY:
  1346. case CIOCKEY2:
  1347. if (ratecheck(&keywarn, &warninterval))
  1348. gone_in(14,
  1349. "Asymmetric crypto operations via /dev/crypto");
  1350. if (!crypto_userasymcrypto) {
  1351. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1352. return (EPERM); /* XXX compat? */
  1353. }
  1354. kop = (struct crypt_kop *)data;
  1355. if (cmd == CIOCKEY) {
  1356. /* NB: crypto core enforces s/w driver use */
  1357. kop->crk_crid =
  1358. CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
  1359. }
  1360. mtx_lock(&Giant);
  1361. error = cryptodev_key(kop);
  1362. mtx_unlock(&Giant);
  1363. break;
  1364. case CIOCASYMFEAT:
  1365. if (ratecheck(&featwarn, &warninterval))
  1366. gone_in(14,
  1367. "Asymmetric crypto features via /dev/crypto");
  1368. if (!crypto_userasymcrypto) {
  1369. /*
  1370. * NB: if user asym crypto operations are
  1371. * not permitted return "no algorithms"
  1372. * so well-behaved applications will just
  1373. * fallback to doing them in software.
  1374. */
  1375. *(int *)data = 0;
  1376. } else {
  1377. error = crypto_getfeat((int *)data);
  1378. if (error)
  1379. SDT_PROBE1(opencrypto, dev, ioctl, error,
  1380. __LINE__);
  1381. }
  1382. break;
  1383. case CIOCFINDDEV:
  1384. error = cryptodev_find((struct crypt_find_op *)data);
  1385. break;
  1386. case CIOCCRYPTAEAD:
  1387. caead = (struct crypt_aead *)data;
  1388. cse = cse_find(fcr, caead->ses);
  1389. if (cse == NULL) {
  1390. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1391. return (EINVAL);
  1392. }
  1393. error = cryptodev_aead(cse, caead);
  1394. cse_free(cse);
  1395. break;
  1396. default:
  1397. error = EINVAL;
  1398. SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
  1399. break;
  1400. }
  1401. #ifdef COMPAT_FREEBSD32
  1402. switch (cmd32) {
  1403. case CIOCGSESSION32:
  1404. if (error == 0)
  1405. session_op_to_32((void *)data, data32);
  1406. break;
  1407. case CIOCGSESSION232:
  1408. if (error == 0)
  1409. session2_op_to_32((void *)data, data32);
  1410. break;
  1411. case CIOCCRYPT32:
  1412. if (error == 0)
  1413. crypt_op_to_32((void *)data, data32);
  1414. break;
  1415. case CIOCCRYPTAEAD32:
  1416. if (error == 0)
  1417. crypt_aead_to_32((void *)data, data32);
  1418. break;
  1419. case CIOCKEY32:
  1420. case CIOCKEY232:
  1421. crypt_kop_to_32((void *)data, data32);
  1422. break;
  1423. }
  1424. #endif
  1425. return (error);
  1426. }
  1427. static struct cdevsw crypto_cdevsw = {
  1428. .d_version = D_VERSION,
  1429. .d_open = crypto_open,
  1430. .d_ioctl = crypto_ioctl,
  1431. .d_name = "crypto",
  1432. };
  1433. static struct cdev *crypto_dev;
  1434. /*
  1435. * Initialization code, both for static and dynamic loading.
  1436. */
  1437. static int
  1438. cryptodev_modevent(module_t mod, int type, void *unused)
  1439. {
  1440. switch (type) {
  1441. case MOD_LOAD:
  1442. if (bootverbose)
  1443. printf("crypto: <crypto device>\n");
  1444. crypto_dev = make_dev(&crypto_cdevsw, 0,
  1445. UID_ROOT, GID_WHEEL, 0666,
  1446. "crypto");
  1447. return 0;
  1448. case MOD_UNLOAD:
  1449. /*XXX disallow if active sessions */
  1450. destroy_dev(crypto_dev);
  1451. return 0;
  1452. }
  1453. return EINVAL;
  1454. }
  1455. static moduledata_t cryptodev_mod = {
  1456. "cryptodev",
  1457. cryptodev_modevent,
  1458. 0
  1459. };
  1460. MODULE_VERSION(cryptodev, 1);
  1461. DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
  1462. MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
  1463. MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);