cryptocteon.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * Octeon Crypto for OCF
  3. *
  4. * Written by David McCullough <[email protected]>
  5. * Copyright (C) 2009-2010 David McCullough
  6. *
  7. * LICENSE TERMS
  8. *
  9. * The free distribution and use of this software in both source and binary
  10. * form is allowed (with or without changes) provided that:
  11. *
  12. * 1. distributions of this source code include the above copyright
  13. * notice, this list of conditions and the following disclaimer;
  14. *
  15. * 2. distributions in binary form include the above copyright
  16. * notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other associated materials;
  18. *
  19. * 3. the copyright holder's name is not used to endorse products
  20. * built using this software without specific written permission.
  21. *
  22. * DISCLAIMER
  23. *
  24. * This software is provided 'as is' with no explicit or implied warranties
  25. * in respect of its properties, including, but not limited to, correctness
  26. * and/or fitness for purpose.
  27. * ---------------------------------------------------------------------------
  28. */
  29. #ifndef AUTOCONF_INCLUDED
  30. #include <linux/config.h>
  31. #endif
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/list.h>
  35. #include <linux/slab.h>
  36. #include <linux/sched.h>
  37. #include <linux/wait.h>
  38. #include <linux/crypto.h>
  39. #include <linux/mm.h>
  40. #include <linux/skbuff.h>
  41. #include <linux/random.h>
  42. #include <linux/scatterlist.h>
  43. #include <cryptodev.h>
  44. #include <uio.h>
  45. struct {
  46. softc_device_decl sc_dev;
  47. } octo_softc;
  48. #define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
  49. struct octo_sess {
  50. int octo_encalg;
  51. #define MAX_CIPHER_KEYLEN 64
  52. char octo_enckey[MAX_CIPHER_KEYLEN];
  53. int octo_encklen;
  54. int octo_macalg;
  55. #define MAX_HASH_KEYLEN 64
  56. char octo_mackey[MAX_HASH_KEYLEN];
  57. int octo_macklen;
  58. int octo_mackey_set;
  59. int octo_mlen;
  60. int octo_ivsize;
  61. #if 0
  62. int (*octo_decrypt)(struct scatterlist *sg, int sg_len,
  63. uint8_t *key, int key_len, uint8_t * iv,
  64. uint64_t *hminner, uint64_t *hmouter);
  65. int (*octo_encrypt)(struct scatterlist *sg, int sg_len,
  66. uint8_t *key, int key_len, uint8_t * iv,
  67. uint64_t *hminner, uint64_t *hmouter);
  68. #else
  69. int (*octo_encrypt)(struct octo_sess *od,
  70. struct scatterlist *sg, int sg_len,
  71. int auth_off, int auth_len,
  72. int crypt_off, int crypt_len,
  73. int icv_off, uint8_t *ivp);
  74. int (*octo_decrypt)(struct octo_sess *od,
  75. struct scatterlist *sg, int sg_len,
  76. int auth_off, int auth_len,
  77. int crypt_off, int crypt_len,
  78. int icv_off, uint8_t *ivp);
  79. #endif
  80. uint64_t octo_hminner[3];
  81. uint64_t octo_hmouter[3];
  82. };
  83. int32_t octo_id = -1;
  84. module_param(octo_id, int, 0444);
  85. MODULE_PARM_DESC(octo_id, "Read-Only OCF ID for cryptocteon driver");
  86. static struct octo_sess **octo_sessions = NULL;
  87. static u_int32_t octo_sesnum = 0;
  88. static int octo_process(device_t, struct cryptop *, int);
  89. static int octo_newsession(device_t, u_int32_t *, struct cryptoini *);
  90. static int octo_freesession(device_t, u_int64_t);
  91. static device_method_t octo_methods = {
  92. /* crypto device methods */
  93. DEVMETHOD(cryptodev_newsession, octo_newsession),
  94. DEVMETHOD(cryptodev_freesession,octo_freesession),
  95. DEVMETHOD(cryptodev_process, octo_process),
  96. };
  97. #define debug octo_debug
  98. int octo_debug = 0;
  99. module_param(octo_debug, int, 0644);
  100. MODULE_PARM_DESC(octo_debug, "Enable debug");
  101. #include "cavium_crypto.c"
  102. /*
  103. * Generate a new octo session. We artifically limit it to a single
  104. * hash/cipher or hash-cipher combo just to make it easier, most callers
  105. * do not expect more than this anyway.
  106. */
  107. static int
  108. octo_newsession(device_t dev, u_int32_t *sid, struct cryptoini *cri)
  109. {
  110. struct cryptoini *c, *encini = NULL, *macini = NULL;
  111. struct octo_sess **ocd;
  112. int i;
  113. dprintk("%s()\n", __FUNCTION__);
  114. if (sid == NULL || cri == NULL) {
  115. dprintk("%s,%d - EINVAL\n", __FILE__, __LINE__);
  116. return EINVAL;
  117. }
  118. /*
  119. * To keep it simple, we only handle hash, cipher or hash/cipher in a
  120. * session, you cannot currently do multiple ciphers/hashes in one
  121. * session even though it would be possibel to code this driver to
  122. * handle it.
  123. */
  124. for (i = 0, c = cri; c && i < 2; i++) {
  125. if (c->cri_alg == CRYPTO_MD5_HMAC ||
  126. c->cri_alg == CRYPTO_SHA1_HMAC ||
  127. c->cri_alg == CRYPTO_NULL_HMAC) {
  128. if (macini) {
  129. break;
  130. }
  131. macini = c;
  132. }
  133. if (c->cri_alg == CRYPTO_DES_CBC ||
  134. c->cri_alg == CRYPTO_3DES_CBC ||
  135. c->cri_alg == CRYPTO_AES_CBC ||
  136. c->cri_alg == CRYPTO_NULL_CBC) {
  137. if (encini) {
  138. break;
  139. }
  140. encini = c;
  141. }
  142. c = c->cri_next;
  143. }
  144. if (!macini && !encini) {
  145. dprintk("%s,%d - EINVAL bad cipher/hash or combination\n",
  146. __FILE__, __LINE__);
  147. return EINVAL;
  148. }
  149. if (c) {
  150. dprintk("%s,%d - EINVAL cannot handle chained cipher/hash combos\n",
  151. __FILE__, __LINE__);
  152. return EINVAL;
  153. }
  154. /*
  155. * So we have something we can do, lets setup the session
  156. */
  157. if (octo_sessions) {
  158. for (i = 1; i < octo_sesnum; i++)
  159. if (octo_sessions[i] == NULL)
  160. break;
  161. } else
  162. i = 1; /* NB: to silence compiler warning */
  163. if (octo_sessions == NULL || i == octo_sesnum) {
  164. if (octo_sessions == NULL) {
  165. i = 1; /* We leave octo_sessions[0] empty */
  166. octo_sesnum = CRYPTO_SW_SESSIONS;
  167. } else
  168. octo_sesnum *= 2;
  169. ocd = kmalloc(octo_sesnum * sizeof(struct octo_sess *), SLAB_ATOMIC);
  170. if (ocd == NULL) {
  171. /* Reset session number */
  172. if (octo_sesnum == CRYPTO_SW_SESSIONS)
  173. octo_sesnum = 0;
  174. else
  175. octo_sesnum /= 2;
  176. dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
  177. return ENOBUFS;
  178. }
  179. memset(ocd, 0, octo_sesnum * sizeof(struct octo_sess *));
  180. /* Copy existing sessions */
  181. if (octo_sessions) {
  182. memcpy(ocd, octo_sessions,
  183. (octo_sesnum / 2) * sizeof(struct octo_sess *));
  184. kfree(octo_sessions);
  185. }
  186. octo_sessions = ocd;
  187. }
  188. ocd = &octo_sessions[i];
  189. *sid = i;
  190. *ocd = (struct octo_sess *) kmalloc(sizeof(struct octo_sess), SLAB_ATOMIC);
  191. if (*ocd == NULL) {
  192. octo_freesession(NULL, i);
  193. dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
  194. return ENOBUFS;
  195. }
  196. memset(*ocd, 0, sizeof(struct octo_sess));
  197. if (encini && encini->cri_key) {
  198. (*ocd)->octo_encklen = (encini->cri_klen + 7) / 8;
  199. memcpy((*ocd)->octo_enckey, encini->cri_key, (*ocd)->octo_encklen);
  200. }
  201. if (macini && macini->cri_key) {
  202. (*ocd)->octo_macklen = (macini->cri_klen + 7) / 8;
  203. memcpy((*ocd)->octo_mackey, macini->cri_key, (*ocd)->octo_macklen);
  204. }
  205. (*ocd)->octo_mlen = 0;
  206. if (encini && encini->cri_mlen)
  207. (*ocd)->octo_mlen = encini->cri_mlen;
  208. else if (macini && macini->cri_mlen)
  209. (*ocd)->octo_mlen = macini->cri_mlen;
  210. else
  211. (*ocd)->octo_mlen = 12;
  212. /*
  213. * point c at the enc if it exists, otherwise the mac
  214. */
  215. c = encini ? encini : macini;
  216. switch (c->cri_alg) {
  217. case CRYPTO_DES_CBC:
  218. case CRYPTO_3DES_CBC:
  219. (*ocd)->octo_ivsize = 8;
  220. switch (macini ? macini->cri_alg : -1) {
  221. case CRYPTO_MD5_HMAC:
  222. (*ocd)->octo_encrypt = octo_des_cbc_md5_encrypt;
  223. (*ocd)->octo_decrypt = octo_des_cbc_md5_decrypt;
  224. octo_calc_hash(0, macini->cri_key, (*ocd)->octo_hminner,
  225. (*ocd)->octo_hmouter);
  226. break;
  227. case CRYPTO_SHA1_HMAC:
  228. (*ocd)->octo_encrypt = octo_des_cbc_sha1_encrypt;
  229. (*ocd)->octo_decrypt = octo_des_cbc_sha1_encrypt;
  230. octo_calc_hash(1, macini->cri_key, (*ocd)->octo_hminner,
  231. (*ocd)->octo_hmouter);
  232. break;
  233. case -1:
  234. (*ocd)->octo_encrypt = octo_des_cbc_encrypt;
  235. (*ocd)->octo_decrypt = octo_des_cbc_decrypt;
  236. break;
  237. default:
  238. octo_freesession(NULL, i);
  239. dprintk("%s,%d: EINVALn", __FILE__, __LINE__);
  240. return EINVAL;
  241. }
  242. break;
  243. case CRYPTO_AES_CBC:
  244. (*ocd)->octo_ivsize = 16;
  245. switch (macini ? macini->cri_alg : -1) {
  246. case CRYPTO_MD5_HMAC:
  247. (*ocd)->octo_encrypt = octo_aes_cbc_md5_encrypt;
  248. (*ocd)->octo_decrypt = octo_aes_cbc_md5_decrypt;
  249. octo_calc_hash(0, macini->cri_key, (*ocd)->octo_hminner,
  250. (*ocd)->octo_hmouter);
  251. break;
  252. case CRYPTO_SHA1_HMAC:
  253. (*ocd)->octo_encrypt = octo_aes_cbc_sha1_encrypt;
  254. (*ocd)->octo_decrypt = octo_aes_cbc_sha1_decrypt;
  255. octo_calc_hash(1, macini->cri_key, (*ocd)->octo_hminner,
  256. (*ocd)->octo_hmouter);
  257. break;
  258. case -1:
  259. (*ocd)->octo_encrypt = octo_aes_cbc_encrypt;
  260. (*ocd)->octo_decrypt = octo_aes_cbc_decrypt;
  261. break;
  262. default:
  263. octo_freesession(NULL, i);
  264. dprintk("%s,%d: EINVALn", __FILE__, __LINE__);
  265. return EINVAL;
  266. }
  267. break;
  268. case CRYPTO_MD5_HMAC:
  269. (*ocd)->octo_encrypt = octo_null_md5_encrypt;
  270. (*ocd)->octo_decrypt = octo_null_md5_encrypt;
  271. octo_calc_hash(0, macini->cri_key, (*ocd)->octo_hminner,
  272. (*ocd)->octo_hmouter);
  273. break;
  274. case CRYPTO_SHA1_HMAC:
  275. (*ocd)->octo_encrypt = octo_null_sha1_encrypt;
  276. (*ocd)->octo_decrypt = octo_null_sha1_encrypt;
  277. octo_calc_hash(1, macini->cri_key, (*ocd)->octo_hminner,
  278. (*ocd)->octo_hmouter);
  279. break;
  280. default:
  281. octo_freesession(NULL, i);
  282. dprintk("%s,%d: EINVALn", __FILE__, __LINE__);
  283. return EINVAL;
  284. }
  285. (*ocd)->octo_encalg = encini ? encini->cri_alg : -1;
  286. (*ocd)->octo_macalg = macini ? macini->cri_alg : -1;
  287. return 0;
  288. }
  289. /*
  290. * Free a session.
  291. */
  292. static int
  293. octo_freesession(device_t dev, u_int64_t tid)
  294. {
  295. u_int32_t sid = CRYPTO_SESID2LID(tid);
  296. dprintk("%s()\n", __FUNCTION__);
  297. if (sid > octo_sesnum || octo_sessions == NULL ||
  298. octo_sessions[sid] == NULL) {
  299. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  300. return(EINVAL);
  301. }
  302. /* Silently accept and return */
  303. if (sid == 0)
  304. return(0);
  305. if (octo_sessions[sid])
  306. kfree(octo_sessions[sid]);
  307. octo_sessions[sid] = NULL;
  308. return 0;
  309. }
  310. /*
  311. * Process a request.
  312. */
  313. static int
  314. octo_process(device_t dev, struct cryptop *crp, int hint)
  315. {
  316. struct cryptodesc *crd;
  317. struct octo_sess *od;
  318. u_int32_t lid;
  319. #define SCATTERLIST_MAX 16
  320. struct scatterlist sg[SCATTERLIST_MAX];
  321. int sg_num, sg_len;
  322. struct sk_buff *skb = NULL;
  323. struct uio *uiop = NULL;
  324. struct cryptodesc *enccrd = NULL, *maccrd = NULL;
  325. unsigned char *ivp = NULL;
  326. unsigned char iv_data[HASH_MAX_LEN];
  327. int auth_off = 0, auth_len = 0, crypt_off = 0, crypt_len = 0, icv_off = 0;
  328. dprintk("%s()\n", __FUNCTION__);
  329. /* Sanity check */
  330. if (crp == NULL) {
  331. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  332. return EINVAL;
  333. }
  334. crp->crp_etype = 0;
  335. if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
  336. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  337. crp->crp_etype = EINVAL;
  338. goto done;
  339. }
  340. lid = crp->crp_sid & 0xffffffff;
  341. if (lid >= octo_sesnum || lid == 0 || octo_sessions == NULL ||
  342. octo_sessions[lid] == NULL) {
  343. crp->crp_etype = ENOENT;
  344. dprintk("%s,%d: ENOENT\n", __FILE__, __LINE__);
  345. goto done;
  346. }
  347. od = octo_sessions[lid];
  348. /*
  349. * do some error checking outside of the loop for SKB and IOV processing
  350. * this leaves us with valid skb or uiop pointers for later
  351. */
  352. if (crp->crp_flags & CRYPTO_F_SKBUF) {
  353. skb = (struct sk_buff *) crp->crp_buf;
  354. if (skb_shinfo(skb)->nr_frags >= SCATTERLIST_MAX) {
  355. printk("%s,%d: %d nr_frags > SCATTERLIST_MAX", __FILE__, __LINE__,
  356. skb_shinfo(skb)->nr_frags);
  357. goto done;
  358. }
  359. } else if (crp->crp_flags & CRYPTO_F_IOV) {
  360. uiop = (struct uio *) crp->crp_buf;
  361. if (uiop->uio_iovcnt > SCATTERLIST_MAX) {
  362. printk("%s,%d: %d uio_iovcnt > SCATTERLIST_MAX", __FILE__, __LINE__,
  363. uiop->uio_iovcnt);
  364. goto done;
  365. }
  366. }
  367. /* point our enccrd and maccrd appropriately */
  368. crd = crp->crp_desc;
  369. if (crd->crd_alg == od->octo_encalg) enccrd = crd;
  370. if (crd->crd_alg == od->octo_macalg) maccrd = crd;
  371. crd = crd->crd_next;
  372. if (crd) {
  373. if (crd->crd_alg == od->octo_encalg) enccrd = crd;
  374. if (crd->crd_alg == od->octo_macalg) maccrd = crd;
  375. crd = crd->crd_next;
  376. }
  377. if (crd) {
  378. crp->crp_etype = EINVAL;
  379. dprintk("%s,%d: ENOENT - descriptors do not match session\n",
  380. __FILE__, __LINE__);
  381. goto done;
  382. }
  383. if (enccrd) {
  384. if (enccrd->crd_flags & CRD_F_IV_EXPLICIT) {
  385. ivp = enccrd->crd_iv;
  386. } else {
  387. ivp = iv_data;
  388. crypto_copydata(crp->crp_flags, crp->crp_buf,
  389. enccrd->crd_inject, od->octo_ivsize, (caddr_t) ivp);
  390. }
  391. if (maccrd) {
  392. auth_off = maccrd->crd_skip;
  393. auth_len = maccrd->crd_len;
  394. icv_off = maccrd->crd_inject;
  395. }
  396. crypt_off = enccrd->crd_skip;
  397. crypt_len = enccrd->crd_len;
  398. } else { /* if (maccrd) */
  399. auth_off = maccrd->crd_skip;
  400. auth_len = maccrd->crd_len;
  401. icv_off = maccrd->crd_inject;
  402. }
  403. /*
  404. * setup the SG list to cover the buffer
  405. */
  406. memset(sg, 0, sizeof(sg));
  407. if (crp->crp_flags & CRYPTO_F_SKBUF) {
  408. int i, len;
  409. sg_num = 0;
  410. sg_len = 0;
  411. len = skb_headlen(skb);
  412. sg_set_page(&sg[sg_num], virt_to_page(skb->data), len,
  413. offset_in_page(skb->data));
  414. sg_len += len;
  415. sg_num++;
  416. for (i = 0; i < skb_shinfo(skb)->nr_frags && sg_num < SCATTERLIST_MAX;
  417. i++) {
  418. len = skb_shinfo(skb)->frags[i].size;
  419. sg_set_page(&sg[sg_num], skb_shinfo(skb)->frags[i].page,
  420. len, skb_shinfo(skb)->frags[i].page_offset);
  421. sg_len += len;
  422. sg_num++;
  423. }
  424. } else if (crp->crp_flags & CRYPTO_F_IOV) {
  425. int len;
  426. sg_len = 0;
  427. for (sg_num = 0; sg_len < crp->crp_ilen &&
  428. sg_num < uiop->uio_iovcnt &&
  429. sg_num < SCATTERLIST_MAX; sg_num++) {
  430. len = uiop->uio_iov[sg_num].iov_len;
  431. sg_set_page(&sg[sg_num],
  432. virt_to_page(uiop->uio_iov[sg_num].iov_base), len,
  433. offset_in_page(uiop->uio_iov[sg_num].iov_base));
  434. sg_len += len;
  435. }
  436. } else {
  437. sg_len = crp->crp_ilen;
  438. sg_set_page(&sg[0], virt_to_page(crp->crp_buf), sg_len,
  439. offset_in_page(crp->crp_buf));
  440. sg_num = 1;
  441. }
  442. /*
  443. * setup a new explicit key
  444. */
  445. if (enccrd) {
  446. if (enccrd->crd_flags & CRD_F_KEY_EXPLICIT) {
  447. od->octo_encklen = (enccrd->crd_klen + 7) / 8;
  448. memcpy(od->octo_enckey, enccrd->crd_key, od->octo_encklen);
  449. }
  450. }
  451. if (maccrd) {
  452. if (maccrd->crd_flags & CRD_F_KEY_EXPLICIT) {
  453. od->octo_macklen = (maccrd->crd_klen + 7) / 8;
  454. memcpy(od->octo_mackey, maccrd->crd_key, od->octo_macklen);
  455. od->octo_mackey_set = 0;
  456. }
  457. if (!od->octo_mackey_set) {
  458. octo_calc_hash(maccrd->crd_alg == CRYPTO_MD5_HMAC ? 0 : 1,
  459. maccrd->crd_key, od->octo_hminner, od->octo_hmouter);
  460. od->octo_mackey_set = 1;
  461. }
  462. }
  463. if (!enccrd || (enccrd->crd_flags & CRD_F_ENCRYPT))
  464. (*od->octo_encrypt)(od, sg, sg_len,
  465. auth_off, auth_len, crypt_off, crypt_len, icv_off, ivp);
  466. else
  467. (*od->octo_decrypt)(od, sg, sg_len,
  468. auth_off, auth_len, crypt_off, crypt_len, icv_off, ivp);
  469. done:
  470. crypto_done(crp);
  471. return 0;
  472. }
  473. static int
  474. cryptocteon_init(void)
  475. {
  476. dprintk("%s(%p)\n", __FUNCTION__, cryptocteon_init);
  477. softc_device_init(&octo_softc, "cryptocteon", 0, octo_methods);
  478. octo_id = crypto_get_driverid(softc_get_device(&octo_softc),
  479. CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SYNC);
  480. if (octo_id < 0) {
  481. printk("Cryptocteon device cannot initialize!");
  482. return -ENODEV;
  483. }
  484. crypto_register(octo_id, CRYPTO_MD5_HMAC, 0,0);
  485. crypto_register(octo_id, CRYPTO_SHA1_HMAC, 0,0);
  486. //crypto_register(octo_id, CRYPTO_MD5, 0,0);
  487. //crypto_register(octo_id, CRYPTO_SHA1, 0,0);
  488. crypto_register(octo_id, CRYPTO_DES_CBC, 0,0);
  489. crypto_register(octo_id, CRYPTO_3DES_CBC, 0,0);
  490. crypto_register(octo_id, CRYPTO_AES_CBC, 0,0);
  491. return(0);
  492. }
  493. static void
  494. cryptocteon_exit(void)
  495. {
  496. dprintk("%s()\n", __FUNCTION__);
  497. crypto_unregister_all(octo_id);
  498. octo_id = -1;
  499. }
  500. module_init(cryptocteon_init);
  501. module_exit(cryptocteon_exit);
  502. MODULE_LICENSE("BSD");
  503. MODULE_AUTHOR("David McCullough <[email protected]>");
  504. MODULE_DESCRIPTION("Cryptocteon (OCF module for Cavium OCTEON crypto)");