cryptosoft.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*
  2. * An OCF module that uses the linux kernel cryptoapi, based on the
  3. * original cryptosoft for BSD by Angelos D. Keromytis ([email protected])
  4. * but is mostly unrecognisable,
  5. *
  6. * Written by David McCullough <[email protected]>
  7. * Copyright (C) 2004-2007 David McCullough
  8. * Copyright (C) 2004-2005 Intel Corporation.
  9. *
  10. * LICENSE TERMS
  11. *
  12. * The free distribution and use of this software in both source and binary
  13. * form is allowed (with or without changes) provided that:
  14. *
  15. * 1. distributions of this source code include the above copyright
  16. * notice, this list of conditions and the following disclaimer;
  17. *
  18. * 2. distributions in binary form include the above copyright
  19. * notice, this list of conditions and the following disclaimer
  20. * in the documentation and/or other associated materials;
  21. *
  22. * 3. the copyright holder's name is not used to endorse products
  23. * built using this software without specific written permission.
  24. *
  25. * ALTERNATIVELY, provided that this notice is retained in full, this product
  26. * may be distributed under the terms of the GNU General Public License (GPL),
  27. * in which case the provisions of the GPL apply INSTEAD OF those given above.
  28. *
  29. * DISCLAIMER
  30. *
  31. * This software is provided 'as is' with no explicit or implied warranties
  32. * in respect of its properties, including, but not limited to, correctness
  33. * and/or fitness for purpose.
  34. * ---------------------------------------------------------------------------
  35. */
  36. #ifndef AUTOCONF_INCLUDED
  37. #include <linux/config.h>
  38. #endif
  39. #include <linux/module.h>
  40. #include <linux/init.h>
  41. #include <linux/list.h>
  42. #include <linux/slab.h>
  43. #include <linux/sched.h>
  44. #include <linux/wait.h>
  45. #include <linux/crypto.h>
  46. #include <linux/mm.h>
  47. #include <linux/skbuff.h>
  48. #include <linux/random.h>
  49. #include <asm/scatterlist.h>
  50. #include <cryptodev.h>
  51. #include <uio.h>
  52. struct {
  53. softc_device_decl sc_dev;
  54. } swcr_softc;
  55. #define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
  56. /* Software session entry */
  57. #define SW_TYPE_CIPHER 0
  58. #define SW_TYPE_HMAC 1
  59. #define SW_TYPE_AUTH2 2
  60. #define SW_TYPE_HASH 3
  61. #define SW_TYPE_COMP 4
  62. #define SW_TYPE_BLKCIPHER 5
  63. struct swcr_data {
  64. int sw_type;
  65. int sw_alg;
  66. struct crypto_tfm *sw_tfm;
  67. union {
  68. struct {
  69. char *sw_key;
  70. int sw_klen;
  71. int sw_mlen;
  72. } hmac;
  73. void *sw_comp_buf;
  74. } u;
  75. struct swcr_data *sw_next;
  76. };
  77. #ifndef CRYPTO_TFM_MODE_CBC
  78. /*
  79. * As of linux-2.6.21 this is no longer defined, and presumably no longer
  80. * needed to be passed into the crypto core code.
  81. */
  82. #define CRYPTO_TFM_MODE_CBC 0
  83. #define CRYPTO_TFM_MODE_ECB 0
  84. #endif
  85. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
  86. /*
  87. * Linux 2.6.19 introduced a new Crypto API, setup macro's to convert new
  88. * API into old API.
  89. */
  90. /* Symmetric/Block Cipher */
  91. struct blkcipher_desc
  92. {
  93. struct crypto_tfm *tfm;
  94. void *info;
  95. };
  96. #define ecb(X) #X
  97. #define cbc(X) #X
  98. #define crypto_has_blkcipher(X, Y, Z) crypto_alg_available(X, 0)
  99. #define crypto_blkcipher_cast(X) X
  100. #define crypto_blkcipher_tfm(X) X
  101. #define crypto_alloc_blkcipher(X, Y, Z) crypto_alloc_tfm(X, mode)
  102. #define crypto_blkcipher_ivsize(X) crypto_tfm_alg_ivsize(X)
  103. #define crypto_blkcipher_blocksize(X) crypto_tfm_alg_blocksize(X)
  104. #define crypto_blkcipher_setkey(X, Y, Z) crypto_cipher_setkey(X, Y, Z)
  105. #define crypto_blkcipher_encrypt_iv(W, X, Y, Z) \
  106. crypto_cipher_encrypt_iv((W)->tfm, X, Y, Z, (u8 *)((W)->info))
  107. #define crypto_blkcipher_decrypt_iv(W, X, Y, Z) \
  108. crypto_cipher_decrypt_iv((W)->tfm, X, Y, Z, (u8 *)((W)->info))
  109. /* Hash/HMAC/Digest */
  110. struct hash_desc
  111. {
  112. struct crypto_tfm *tfm;
  113. };
  114. #define hmac(X) #X
  115. #define crypto_has_hash(X, Y, Z) crypto_alg_available(X, 0)
  116. #define crypto_hash_cast(X) X
  117. #define crypto_hash_tfm(X) X
  118. #define crypto_alloc_hash(X, Y, Z) crypto_alloc_tfm(X, mode)
  119. #define crypto_hash_digestsize(X) crypto_tfm_alg_digestsize(X)
  120. #define crypto_hash_digest(W, X, Y, Z) \
  121. crypto_digest_digest((W)->tfm, X, sg_num, Z)
  122. /* Asymmetric Cipher */
  123. #define crypto_has_cipher(X, Y, Z) crypto_alg_available(X, 0)
  124. /* Compression */
  125. #define crypto_has_comp(X, Y, Z) crypto_alg_available(X, 0)
  126. #define crypto_comp_tfm(X) X
  127. #define crypto_comp_cast(X) X
  128. #define crypto_alloc_comp(X, Y, Z) crypto_alloc_tfm(X, mode)
  129. #else
  130. #define ecb(X) "ecb(" #X ")"
  131. #define cbc(X) "cbc(" #X ")"
  132. #define hmac(X) "hmac(" #X ")"
  133. #endif /* if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) */
  134. struct crypto_details
  135. {
  136. char *alg_name;
  137. int mode;
  138. int sw_type;
  139. };
  140. /*
  141. * This needs to be kept updated with CRYPTO_xxx list (cryptodev.h).
  142. * If the Algorithm is not supported, then insert a {NULL, 0, 0} entry.
  143. *
  144. * IMPORTANT: The index to the array IS CRYPTO_xxx.
  145. */
  146. static struct crypto_details crypto_details[CRYPTO_ALGORITHM_MAX + 1] = {
  147. { NULL, 0, 0 },
  148. /* CRYPTO_xxx index starts at 1 */
  149. { cbc(des), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  150. { cbc(des3_ede), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  151. { cbc(blowfish), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  152. { cbc(cast5), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  153. { cbc(skipjack), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  154. { hmac(md5), 0, SW_TYPE_HMAC },
  155. { hmac(sha1), 0, SW_TYPE_HMAC },
  156. { hmac(ripemd160), 0, SW_TYPE_HMAC },
  157. { "md5-kpdk??", 0, SW_TYPE_HASH },
  158. { "sha1-kpdk??", 0, SW_TYPE_HASH },
  159. { cbc(aes), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  160. { ecb(arc4), CRYPTO_TFM_MODE_ECB, SW_TYPE_BLKCIPHER },
  161. { "md5", 0, SW_TYPE_HASH },
  162. { "sha1", 0, SW_TYPE_HASH },
  163. { hmac(digest_null), 0, SW_TYPE_HMAC },
  164. { cbc(cipher_null), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  165. { "deflate", 0, SW_TYPE_COMP },
  166. { hmac(sha256), 0, SW_TYPE_HMAC },
  167. { hmac(sha384), 0, SW_TYPE_HMAC },
  168. { hmac(sha512), 0, SW_TYPE_HMAC },
  169. { cbc(camellia), CRYPTO_TFM_MODE_CBC, SW_TYPE_BLKCIPHER },
  170. { "sha256", 0, SW_TYPE_HASH },
  171. { "sha384", 0, SW_TYPE_HASH },
  172. { "sha512", 0, SW_TYPE_HASH },
  173. { "ripemd160", 0, SW_TYPE_HASH },
  174. };
  175. int32_t swcr_id = -1;
  176. module_param(swcr_id, int, 0444);
  177. MODULE_PARM_DESC(swcr_id, "Read-Only OCF ID for cryptosoft driver");
  178. int swcr_fail_if_compression_grows = 1;
  179. module_param(swcr_fail_if_compression_grows, int, 0644);
  180. MODULE_PARM_DESC(swcr_fail_if_compression_grows,
  181. "Treat compression that results in more data as a failure");
  182. static struct swcr_data **swcr_sessions = NULL;
  183. static u_int32_t swcr_sesnum = 0;
  184. static int swcr_process(device_t, struct cryptop *, int);
  185. static int swcr_newsession(device_t, u_int32_t *, struct cryptoini *);
  186. static int swcr_freesession(device_t, u_int64_t);
  187. static device_method_t swcr_methods = {
  188. /* crypto device methods */
  189. DEVMETHOD(cryptodev_newsession, swcr_newsession),
  190. DEVMETHOD(cryptodev_freesession,swcr_freesession),
  191. DEVMETHOD(cryptodev_process, swcr_process),
  192. };
  193. #define debug swcr_debug
  194. int swcr_debug = 0;
  195. module_param(swcr_debug, int, 0644);
  196. MODULE_PARM_DESC(swcr_debug, "Enable debug");
  197. /*
  198. * Generate a new software session.
  199. */
  200. static int
  201. swcr_newsession(device_t dev, u_int32_t *sid, struct cryptoini *cri)
  202. {
  203. struct swcr_data **swd;
  204. u_int32_t i;
  205. int error;
  206. char *algo;
  207. int mode, sw_type;
  208. dprintk("%s()\n", __FUNCTION__);
  209. if (sid == NULL || cri == NULL) {
  210. dprintk("%s,%d - EINVAL\n", __FILE__, __LINE__);
  211. return EINVAL;
  212. }
  213. if (swcr_sessions) {
  214. for (i = 1; i < swcr_sesnum; i++)
  215. if (swcr_sessions[i] == NULL)
  216. break;
  217. } else
  218. i = 1; /* NB: to silence compiler warning */
  219. if (swcr_sessions == NULL || i == swcr_sesnum) {
  220. if (swcr_sessions == NULL) {
  221. i = 1; /* We leave swcr_sessions[0] empty */
  222. swcr_sesnum = CRYPTO_SW_SESSIONS;
  223. } else
  224. swcr_sesnum *= 2;
  225. swd = kmalloc(swcr_sesnum * sizeof(struct swcr_data *), SLAB_ATOMIC);
  226. if (swd == NULL) {
  227. /* Reset session number */
  228. if (swcr_sesnum == CRYPTO_SW_SESSIONS)
  229. swcr_sesnum = 0;
  230. else
  231. swcr_sesnum /= 2;
  232. dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
  233. return ENOBUFS;
  234. }
  235. memset(swd, 0, swcr_sesnum * sizeof(struct swcr_data *));
  236. /* Copy existing sessions */
  237. if (swcr_sessions) {
  238. memcpy(swd, swcr_sessions,
  239. (swcr_sesnum / 2) * sizeof(struct swcr_data *));
  240. kfree(swcr_sessions);
  241. }
  242. swcr_sessions = swd;
  243. }
  244. swd = &swcr_sessions[i];
  245. *sid = i;
  246. while (cri) {
  247. *swd = (struct swcr_data *) kmalloc(sizeof(struct swcr_data),
  248. SLAB_ATOMIC);
  249. if (*swd == NULL) {
  250. swcr_freesession(NULL, i);
  251. dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
  252. return ENOBUFS;
  253. }
  254. memset(*swd, 0, sizeof(struct swcr_data));
  255. if (cri->cri_alg > CRYPTO_ALGORITHM_MAX) {
  256. printk("cryptosoft: Unknown algorithm 0x%x\n", cri->cri_alg);
  257. swcr_freesession(NULL, i);
  258. return EINVAL;
  259. }
  260. algo = crypto_details[cri->cri_alg].alg_name;
  261. if (!algo || !*algo) {
  262. printk("cryptosoft: Unsupported algorithm 0x%x\n", cri->cri_alg);
  263. swcr_freesession(NULL, i);
  264. return EINVAL;
  265. }
  266. mode = crypto_details[cri->cri_alg].mode;
  267. sw_type = crypto_details[cri->cri_alg].sw_type;
  268. /* Algorithm specific configuration */
  269. switch (cri->cri_alg) {
  270. case CRYPTO_NULL_CBC:
  271. cri->cri_klen = 0; /* make it work with crypto API */
  272. break;
  273. default:
  274. break;
  275. }
  276. if (sw_type == SW_TYPE_BLKCIPHER) {
  277. dprintk("%s crypto_alloc_blkcipher(%s, 0x%x)\n", __FUNCTION__,
  278. algo, mode);
  279. (*swd)->sw_tfm = crypto_blkcipher_tfm(
  280. crypto_alloc_blkcipher(algo, 0,
  281. CRYPTO_ALG_ASYNC));
  282. if (!(*swd)->sw_tfm) {
  283. dprintk("cryptosoft: crypto_alloc_blkcipher failed(%s,0x%x)\n",
  284. algo,mode);
  285. swcr_freesession(NULL, i);
  286. return EINVAL;
  287. }
  288. if (debug) {
  289. dprintk("%s key:cri->cri_klen=%d,(cri->cri_klen + 7)/8=%d",
  290. __FUNCTION__,cri->cri_klen,(cri->cri_klen + 7)/8);
  291. for (i = 0; i < (cri->cri_klen + 7) / 8; i++)
  292. {
  293. dprintk("%s0x%x", (i % 8) ? " " : "\n ",cri->cri_key[i]);
  294. }
  295. dprintk("\n");
  296. }
  297. error = crypto_blkcipher_setkey(
  298. crypto_blkcipher_cast((*swd)->sw_tfm), cri->cri_key,
  299. (cri->cri_klen + 7) / 8);
  300. if (error) {
  301. printk("cryptosoft: setkey failed %d (crt_flags=0x%x)\n", error,
  302. (*swd)->sw_tfm->crt_flags);
  303. swcr_freesession(NULL, i);
  304. return error;
  305. }
  306. } else if (sw_type == SW_TYPE_HMAC || sw_type == SW_TYPE_HASH) {
  307. dprintk("%s crypto_alloc_hash(%s, 0x%x)\n", __FUNCTION__,
  308. algo, mode);
  309. (*swd)->sw_tfm = crypto_hash_tfm(
  310. crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC));
  311. if (!(*swd)->sw_tfm) {
  312. dprintk("cryptosoft: crypto_alloc_hash failed(%s,0x%x)\n",
  313. algo, mode);
  314. swcr_freesession(NULL, i);
  315. return EINVAL;
  316. }
  317. (*swd)->u.hmac.sw_klen = (cri->cri_klen + 7) / 8;
  318. (*swd)->u.hmac.sw_key = (char *)kmalloc((*swd)->u.hmac.sw_klen,
  319. SLAB_ATOMIC);
  320. if ((*swd)->u.hmac.sw_key == NULL) {
  321. swcr_freesession(NULL, i);
  322. dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
  323. return ENOBUFS;
  324. }
  325. memcpy((*swd)->u.hmac.sw_key, cri->cri_key, (*swd)->u.hmac.sw_klen);
  326. if (cri->cri_mlen) {
  327. (*swd)->u.hmac.sw_mlen = cri->cri_mlen;
  328. } else {
  329. (*swd)->u.hmac.sw_mlen =
  330. crypto_hash_digestsize(
  331. crypto_hash_cast((*swd)->sw_tfm));
  332. }
  333. } else if (sw_type == SW_TYPE_COMP) {
  334. (*swd)->sw_tfm = crypto_comp_tfm(
  335. crypto_alloc_comp(algo, 0, CRYPTO_ALG_ASYNC));
  336. if (!(*swd)->sw_tfm) {
  337. dprintk("cryptosoft: crypto_alloc_comp failed(%s,0x%x)\n",
  338. algo, mode);
  339. swcr_freesession(NULL, i);
  340. return EINVAL;
  341. }
  342. (*swd)->u.sw_comp_buf = kmalloc(CRYPTO_MAX_DATA_LEN, SLAB_ATOMIC);
  343. if ((*swd)->u.sw_comp_buf == NULL) {
  344. swcr_freesession(NULL, i);
  345. dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
  346. return ENOBUFS;
  347. }
  348. } else {
  349. printk("cryptosoft: Unhandled sw_type %d\n", sw_type);
  350. swcr_freesession(NULL, i);
  351. return EINVAL;
  352. }
  353. (*swd)->sw_alg = cri->cri_alg;
  354. (*swd)->sw_type = sw_type;
  355. cri = cri->cri_next;
  356. swd = &((*swd)->sw_next);
  357. }
  358. return 0;
  359. }
  360. /*
  361. * Free a session.
  362. */
  363. static int
  364. swcr_freesession(device_t dev, u_int64_t tid)
  365. {
  366. struct swcr_data *swd;
  367. u_int32_t sid = CRYPTO_SESID2LID(tid);
  368. dprintk("%s()\n", __FUNCTION__);
  369. if (sid > swcr_sesnum || swcr_sessions == NULL ||
  370. swcr_sessions[sid] == NULL) {
  371. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  372. return(EINVAL);
  373. }
  374. /* Silently accept and return */
  375. if (sid == 0)
  376. return(0);
  377. while ((swd = swcr_sessions[sid]) != NULL) {
  378. swcr_sessions[sid] = swd->sw_next;
  379. if (swd->sw_tfm)
  380. crypto_free_tfm(swd->sw_tfm);
  381. if (swd->sw_type == SW_TYPE_COMP) {
  382. if (swd->u.sw_comp_buf)
  383. kfree(swd->u.sw_comp_buf);
  384. } else {
  385. if (swd->u.hmac.sw_key)
  386. kfree(swd->u.hmac.sw_key);
  387. }
  388. kfree(swd);
  389. }
  390. return 0;
  391. }
  392. /*
  393. * Process a software request.
  394. */
  395. static int
  396. swcr_process(device_t dev, struct cryptop *crp, int hint)
  397. {
  398. struct cryptodesc *crd;
  399. struct swcr_data *sw;
  400. u_int32_t lid;
  401. #define SCATTERLIST_MAX 16
  402. struct scatterlist sg[SCATTERLIST_MAX];
  403. int sg_num, sg_len, skip;
  404. struct sk_buff *skb = NULL;
  405. struct uio *uiop = NULL;
  406. dprintk("%s()\n", __FUNCTION__);
  407. /* Sanity check */
  408. if (crp == NULL) {
  409. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  410. return EINVAL;
  411. }
  412. crp->crp_etype = 0;
  413. if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
  414. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  415. crp->crp_etype = EINVAL;
  416. goto done;
  417. }
  418. lid = crp->crp_sid & 0xffffffff;
  419. if (lid >= swcr_sesnum || lid == 0 || swcr_sessions == NULL ||
  420. swcr_sessions[lid] == NULL) {
  421. crp->crp_etype = ENOENT;
  422. dprintk("%s,%d: ENOENT\n", __FILE__, __LINE__);
  423. goto done;
  424. }
  425. /*
  426. * do some error checking outside of the loop for SKB and IOV processing
  427. * this leaves us with valid skb or uiop pointers for later
  428. */
  429. if (crp->crp_flags & CRYPTO_F_SKBUF) {
  430. skb = (struct sk_buff *) crp->crp_buf;
  431. if (skb_shinfo(skb)->nr_frags >= SCATTERLIST_MAX) {
  432. printk("%s,%d: %d nr_frags > SCATTERLIST_MAX", __FILE__, __LINE__,
  433. skb_shinfo(skb)->nr_frags);
  434. goto done;
  435. }
  436. } else if (crp->crp_flags & CRYPTO_F_IOV) {
  437. uiop = (struct uio *) crp->crp_buf;
  438. if (uiop->uio_iovcnt > SCATTERLIST_MAX) {
  439. printk("%s,%d: %d uio_iovcnt > SCATTERLIST_MAX", __FILE__, __LINE__,
  440. uiop->uio_iovcnt);
  441. goto done;
  442. }
  443. }
  444. /* Go through crypto descriptors, processing as we go */
  445. for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
  446. /*
  447. * Find the crypto context.
  448. *
  449. * XXX Note that the logic here prevents us from having
  450. * XXX the same algorithm multiple times in a session
  451. * XXX (or rather, we can but it won't give us the right
  452. * XXX results). To do that, we'd need some way of differentiating
  453. * XXX between the various instances of an algorithm (so we can
  454. * XXX locate the correct crypto context).
  455. */
  456. for (sw = swcr_sessions[lid]; sw && sw->sw_alg != crd->crd_alg;
  457. sw = sw->sw_next)
  458. ;
  459. /* No such context ? */
  460. if (sw == NULL) {
  461. crp->crp_etype = EINVAL;
  462. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  463. goto done;
  464. }
  465. skip = crd->crd_skip;
  466. /*
  467. * setup the SG list skip from the start of the buffer
  468. */
  469. memset(sg, 0, sizeof(sg));
  470. if (crp->crp_flags & CRYPTO_F_SKBUF) {
  471. int i, len;
  472. sg_num = 0;
  473. sg_len = 0;
  474. if (skip < skb_headlen(skb)) {
  475. len = skb_headlen(skb) - skip;
  476. if (len + sg_len > crd->crd_len)
  477. len = crd->crd_len - sg_len;
  478. sg_set_page(&sg[sg_num],
  479. virt_to_page(skb->data + skip), len,
  480. offset_in_page(skb->data + skip));
  481. sg_len += len;
  482. sg_num++;
  483. skip = 0;
  484. } else
  485. skip -= skb_headlen(skb);
  486. for (i = 0; sg_len < crd->crd_len &&
  487. i < skb_shinfo(skb)->nr_frags &&
  488. sg_num < SCATTERLIST_MAX; i++) {
  489. if (skip < skb_shinfo(skb)->frags[i].size) {
  490. len = skb_shinfo(skb)->frags[i].size - skip;
  491. if (len + sg_len > crd->crd_len)
  492. len = crd->crd_len - sg_len;
  493. sg_set_page(&sg[sg_num],
  494. skb_shinfo(skb)->frags[i].page,
  495. len,
  496. skb_shinfo(skb)->frags[i].page_offset + skip);
  497. sg_len += len;
  498. sg_num++;
  499. skip = 0;
  500. } else
  501. skip -= skb_shinfo(skb)->frags[i].size;
  502. }
  503. } else if (crp->crp_flags & CRYPTO_F_IOV) {
  504. int len;
  505. sg_len = 0;
  506. for (sg_num = 0; sg_len <= crd->crd_len &&
  507. sg_num < uiop->uio_iovcnt &&
  508. sg_num < SCATTERLIST_MAX; sg_num++) {
  509. if (skip <= uiop->uio_iov[sg_num].iov_len) {
  510. len = uiop->uio_iov[sg_num].iov_len - skip;
  511. if (len + sg_len > crd->crd_len)
  512. len = crd->crd_len - sg_len;
  513. sg_set_page(&sg[sg_num],
  514. virt_to_page(uiop->uio_iov[sg_num].iov_base+skip),
  515. len,
  516. offset_in_page(uiop->uio_iov[sg_num].iov_base+skip));
  517. sg_len += len;
  518. skip = 0;
  519. } else
  520. skip -= uiop->uio_iov[sg_num].iov_len;
  521. }
  522. } else {
  523. sg_len = (crp->crp_ilen - skip);
  524. if (sg_len > crd->crd_len)
  525. sg_len = crd->crd_len;
  526. sg_set_page(&sg[0], virt_to_page(crp->crp_buf + skip),
  527. sg_len, offset_in_page(crp->crp_buf + skip));
  528. sg_num = 1;
  529. }
  530. switch (sw->sw_type) {
  531. case SW_TYPE_BLKCIPHER: {
  532. unsigned char iv[EALG_MAX_BLOCK_LEN];
  533. unsigned char *ivp = iv;
  534. int ivsize =
  535. crypto_blkcipher_ivsize(crypto_blkcipher_cast(sw->sw_tfm));
  536. struct blkcipher_desc desc;
  537. if (sg_len < crypto_blkcipher_blocksize(
  538. crypto_blkcipher_cast(sw->sw_tfm))) {
  539. crp->crp_etype = EINVAL;
  540. dprintk("%s,%d: EINVAL len %d < %d\n", __FILE__, __LINE__,
  541. sg_len, crypto_blkcipher_blocksize(
  542. crypto_blkcipher_cast(sw->sw_tfm)));
  543. goto done;
  544. }
  545. if (ivsize > sizeof(iv)) {
  546. crp->crp_etype = EINVAL;
  547. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  548. goto done;
  549. }
  550. if (crd->crd_flags & CRD_F_KEY_EXPLICIT) {
  551. int i, error;
  552. if (debug) {
  553. dprintk("%s key:", __FUNCTION__);
  554. for (i = 0; i < (crd->crd_klen + 7) / 8; i++)
  555. dprintk("%s0x%x", (i % 8) ? " " : "\n ",
  556. crd->crd_key[i]);
  557. dprintk("\n");
  558. }
  559. error = crypto_blkcipher_setkey(
  560. crypto_blkcipher_cast(sw->sw_tfm), crd->crd_key,
  561. (crd->crd_klen + 7) / 8);
  562. if (error) {
  563. dprintk("cryptosoft: setkey failed %d (crt_flags=0x%x)\n",
  564. error, sw->sw_tfm->crt_flags);
  565. crp->crp_etype = -error;
  566. }
  567. }
  568. memset(&desc, 0, sizeof(desc));
  569. desc.tfm = crypto_blkcipher_cast(sw->sw_tfm);
  570. if (crd->crd_flags & CRD_F_ENCRYPT) { /* encrypt */
  571. if (crd->crd_flags & CRD_F_IV_EXPLICIT) {
  572. ivp = crd->crd_iv;
  573. } else {
  574. get_random_bytes(ivp, ivsize);
  575. }
  576. /*
  577. * do we have to copy the IV back to the buffer ?
  578. */
  579. if ((crd->crd_flags & CRD_F_IV_PRESENT) == 0) {
  580. crypto_copyback(crp->crp_flags, crp->crp_buf,
  581. crd->crd_inject, ivsize, (caddr_t)ivp);
  582. }
  583. desc.info = ivp;
  584. crypto_blkcipher_encrypt_iv(&desc, sg, sg, sg_len);
  585. } else { /*decrypt */
  586. if (crd->crd_flags & CRD_F_IV_EXPLICIT) {
  587. ivp = crd->crd_iv;
  588. } else {
  589. crypto_copydata(crp->crp_flags, crp->crp_buf,
  590. crd->crd_inject, ivsize, (caddr_t)ivp);
  591. }
  592. desc.info = ivp;
  593. crypto_blkcipher_decrypt_iv(&desc, sg, sg, sg_len);
  594. }
  595. } break;
  596. case SW_TYPE_HMAC:
  597. case SW_TYPE_HASH:
  598. {
  599. char result[HASH_MAX_LEN];
  600. struct hash_desc desc;
  601. /* check we have room for the result */
  602. if (crp->crp_ilen - crd->crd_inject < sw->u.hmac.sw_mlen) {
  603. dprintk(
  604. "cryptosoft: EINVAL crp_ilen=%d, len=%d, inject=%d digestsize=%d\n",
  605. crp->crp_ilen, crd->crd_skip + sg_len, crd->crd_inject,
  606. sw->u.hmac.sw_mlen);
  607. crp->crp_etype = EINVAL;
  608. goto done;
  609. }
  610. memset(&desc, 0, sizeof(desc));
  611. desc.tfm = crypto_hash_cast(sw->sw_tfm);
  612. memset(result, 0, sizeof(result));
  613. if (sw->sw_type == SW_TYPE_HMAC) {
  614. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
  615. crypto_hmac(sw->sw_tfm, sw->u.hmac.sw_key, &sw->u.hmac.sw_klen,
  616. sg, sg_num, result);
  617. #else
  618. crypto_hash_setkey(desc.tfm, sw->u.hmac.sw_key,
  619. sw->u.hmac.sw_klen);
  620. crypto_hash_digest(&desc, sg, sg_len, result);
  621. #endif /* #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) */
  622. } else { /* SW_TYPE_HASH */
  623. crypto_hash_digest(&desc, sg, sg_len, result);
  624. }
  625. crypto_copyback(crp->crp_flags, crp->crp_buf,
  626. crd->crd_inject, sw->u.hmac.sw_mlen, result);
  627. }
  628. break;
  629. case SW_TYPE_COMP: {
  630. void *ibuf = NULL;
  631. void *obuf = sw->u.sw_comp_buf;
  632. int ilen = sg_len, olen = CRYPTO_MAX_DATA_LEN;
  633. int ret = 0;
  634. /*
  635. * we need to use an additional copy if there is more than one
  636. * input chunk since the kernel comp routines do not handle
  637. * SG yet. Otherwise we just use the input buffer as is.
  638. * Rather than allocate another buffer we just split the tmp
  639. * buffer we already have.
  640. * Perhaps we should just use zlib directly ?
  641. */
  642. if (sg_num > 1) {
  643. int blk;
  644. ibuf = obuf;
  645. for (blk = 0; blk < sg_num; blk++) {
  646. memcpy(obuf, sg_virt(&sg[blk]),
  647. sg[blk].length);
  648. obuf += sg[blk].length;
  649. }
  650. olen -= sg_len;
  651. } else
  652. ibuf = sg_virt(&sg[0]);
  653. if (crd->crd_flags & CRD_F_ENCRYPT) { /* compress */
  654. ret = crypto_comp_compress(crypto_comp_cast(sw->sw_tfm),
  655. ibuf, ilen, obuf, &olen);
  656. if (!ret && olen > crd->crd_len) {
  657. dprintk("cryptosoft: ERANGE compress %d into %d\n",
  658. crd->crd_len, olen);
  659. if (swcr_fail_if_compression_grows)
  660. ret = ERANGE;
  661. }
  662. } else { /* decompress */
  663. ret = crypto_comp_decompress(crypto_comp_cast(sw->sw_tfm),
  664. ibuf, ilen, obuf, &olen);
  665. if (!ret && (olen + crd->crd_inject) > crp->crp_olen) {
  666. dprintk("cryptosoft: ETOOSMALL decompress %d into %d, "
  667. "space for %d,at offset %d\n",
  668. crd->crd_len, olen, crp->crp_olen, crd->crd_inject);
  669. ret = ETOOSMALL;
  670. }
  671. }
  672. if (ret)
  673. dprintk("%s,%d: ret = %d\n", __FILE__, __LINE__, ret);
  674. /*
  675. * on success copy result back,
  676. * linux crpyto API returns -errno, we need to fix that
  677. */
  678. crp->crp_etype = ret < 0 ? -ret : ret;
  679. if (ret == 0) {
  680. /* copy back the result and return it's size */
  681. crypto_copyback(crp->crp_flags, crp->crp_buf,
  682. crd->crd_inject, olen, obuf);
  683. crp->crp_olen = olen;
  684. }
  685. } break;
  686. default:
  687. /* Unknown/unsupported algorithm */
  688. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  689. crp->crp_etype = EINVAL;
  690. goto done;
  691. }
  692. }
  693. done:
  694. crypto_done(crp);
  695. return 0;
  696. }
  697. static int
  698. cryptosoft_init(void)
  699. {
  700. int i, sw_type, mode;
  701. char *algo;
  702. dprintk("%s(%p)\n", __FUNCTION__, cryptosoft_init);
  703. softc_device_init(&swcr_softc, "cryptosoft", 0, swcr_methods);
  704. swcr_id = crypto_get_driverid(softc_get_device(&swcr_softc),
  705. CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_SYNC);
  706. if (swcr_id < 0) {
  707. printk("Software crypto device cannot initialize!");
  708. return -ENODEV;
  709. }
  710. #define REGISTER(alg) \
  711. crypto_register(swcr_id, alg, 0,0);
  712. for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; ++i)
  713. {
  714. algo = crypto_details[i].alg_name;
  715. if (!algo || !*algo)
  716. {
  717. dprintk("%s:Algorithm %d not supported\n", __FUNCTION__, i);
  718. continue;
  719. }
  720. mode = crypto_details[i].mode;
  721. sw_type = crypto_details[i].sw_type;
  722. switch (sw_type)
  723. {
  724. case SW_TYPE_CIPHER:
  725. if (crypto_has_cipher(algo, 0, CRYPTO_ALG_ASYNC))
  726. {
  727. REGISTER(i);
  728. }
  729. else
  730. {
  731. dprintk("%s:CIPHER algorithm %d:'%s' not supported\n",
  732. __FUNCTION__, i, algo);
  733. }
  734. break;
  735. case SW_TYPE_HMAC:
  736. if (crypto_has_hash(algo, 0, CRYPTO_ALG_ASYNC))
  737. {
  738. REGISTER(i);
  739. }
  740. else
  741. {
  742. dprintk("%s:HMAC algorithm %d:'%s' not supported\n",
  743. __FUNCTION__, i, algo);
  744. }
  745. break;
  746. case SW_TYPE_HASH:
  747. if (crypto_has_hash(algo, 0, CRYPTO_ALG_ASYNC))
  748. {
  749. REGISTER(i);
  750. }
  751. else
  752. {
  753. dprintk("%s:HASH algorithm %d:'%s' not supported\n",
  754. __FUNCTION__, i, algo);
  755. }
  756. break;
  757. case SW_TYPE_COMP:
  758. if (crypto_has_comp(algo, 0, CRYPTO_ALG_ASYNC))
  759. {
  760. REGISTER(i);
  761. }
  762. else
  763. {
  764. dprintk("%s:COMP algorithm %d:'%s' not supported\n",
  765. __FUNCTION__, i, algo);
  766. }
  767. break;
  768. case SW_TYPE_BLKCIPHER:
  769. if (crypto_has_blkcipher(algo, 0, CRYPTO_ALG_ASYNC))
  770. {
  771. REGISTER(i);
  772. }
  773. else
  774. {
  775. dprintk("%s:BLKCIPHER algorithm %d:'%s' not supported\n",
  776. __FUNCTION__, i, algo);
  777. }
  778. break;
  779. default:
  780. dprintk(
  781. "%s:Algorithm Type %d not supported (algorithm %d:'%s')\n",
  782. __FUNCTION__, sw_type, i, algo);
  783. break;
  784. }
  785. }
  786. return(0);
  787. }
  788. static void
  789. cryptosoft_exit(void)
  790. {
  791. dprintk("%s()\n", __FUNCTION__);
  792. crypto_unregister_all(swcr_id);
  793. swcr_id = -1;
  794. }
  795. module_init(cryptosoft_init);
  796. module_exit(cryptosoft_exit);
  797. MODULE_LICENSE("Dual BSD/GPL");
  798. MODULE_AUTHOR("David McCullough <[email protected]>");
  799. MODULE_DESCRIPTION("Cryptosoft (OCF module for kernel crypto)");