aes-7108.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * Copyright (C) 2006 Micronas USA
  3. *
  4. * 1. Redistributions of source code must retain the above copyright
  5. * notice, this list of conditions and the following disclaimer.
  6. * 2. Redistributions in binary form must reproduce the above copyright
  7. * notice, this list of conditions and the following disclaimer in the
  8. * documentation and/or other materials provided with the distribution.
  9. * 3. The name of the author may not be used to endorse or promote products
  10. * derived from this software without specific prior written permission.
  11. *
  12. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  13. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  14. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  16. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  17. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. *
  23. * Effort sponsored in part by the Defense Advanced Research Projects
  24. * Agency (DARPA) and Air Force Research Laboratory, Air Force
  25. * Materiel Command, USAF, under agreement number F30602-01-2-0537.
  26. *
  27. */
  28. //#include <linux/config.h>
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/list.h>
  32. #include <linux/slab.h>
  33. #include <linux/sched.h>
  34. #include <linux/wait.h>
  35. #include <linux/crypto.h>
  36. #include <linux/mm.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/random.h>
  39. #include <asm/io.h>
  40. #include <asm/delay.h>
  41. //#include <asm/scatterlist.h>
  42. #include <linux/scatterlist.h>
  43. #include <linux/dma-mapping.h>
  44. #include <linux/highmem.h>
  45. #include <cryptodev.h>
  46. #include <uio.h>
  47. #include <aes-7108.h>
  48. /* Runtime mode */
  49. static int c7108_crypto_mode = C7108_AES_CTRL_MODE_CTR;
  50. //static int c7108_crypto_mode = C7108_AES_CTRL_MODE_CBC;
  51. static int32_t c7108_id = -1;
  52. static struct cipher_7108 **c7108_sessions = NULL;
  53. static u_int32_t c7108_sesnum = 0;
  54. static unsigned long iobar;
  55. /* Crypto entry points */
  56. static int c7108_process(void *, struct cryptop *, int);
  57. static int c7108_newsession(void *, u_int32_t *, struct cryptoini *);
  58. static int c7108_freesession(void *, u_int64_t);
  59. /* Globals */
  60. static int debug = 0;
  61. static spinlock_t csr_mutex;
  62. /* Generic controller-based lock */
  63. #define AES_LOCK()\
  64. spin_lock(&csr_mutex)
  65. #define AES_UNLOCK()\
  66. spin_unlock(&csr_mutex)
  67. /* 7108 AES register access */
  68. #define c7108_reg_wr8(a,d) iowrite8(d, (void*)(iobar+(a)))
  69. #define c7108_reg_wr16(a,d) iowrite16(d, (void*)(iobar+(a)))
  70. #define c7108_reg_wr32(a,d) iowrite32(d, (void*)(iobar+(a)))
  71. #define c7108_reg_rd8(a) ioread8((void*)(iobar+(a)))
  72. #define c7108_reg_rd16(a) ioread16((void*)(iobar+(a)))
  73. #define c7108_reg_rd32(a) ioread32((void*)(iobar+(a)))
  74. static int
  75. c7108_xlate_key(int klen, u8* k8ptr, u32* k32ptr)
  76. {
  77. int i, nw=0;
  78. nw = ((klen >= 256) ? 8 : (klen >= 192) ? 6 : 4);
  79. for ( i = 0; i < nw; i++) {
  80. k32ptr[i] = (k8ptr[i+3] << 24) | (k8ptr[i+2] << 16) |
  81. (k8ptr[i+1] << 8) | k8ptr[i];
  82. }
  83. return 0;
  84. }
  85. static int
  86. c7108_cache_key(int klen, u32* k32ptr, u8* k8ptr)
  87. {
  88. int i, nb=0;
  89. u8* ptr = (u8*)k32ptr;
  90. nb = ((klen >= 256) ? 32 : (klen >= 192) ? 24 : 16);
  91. for ( i = 0; i < nb; i++)
  92. k8ptr[i] = ptr[i];
  93. return 0;
  94. }
  95. static int
  96. c7108_aes_setup_dma(u32 src, u32 dst, u32 len)
  97. {
  98. if (len < 16) {
  99. printk("len < 16\n");
  100. return -10;
  101. }
  102. if (len % 16) {
  103. printk("len not multiple of 16\n");
  104. return -11;
  105. }
  106. c7108_reg_wr16(C7108_AES_DMA_SRC0_LO, (u16) src);
  107. c7108_reg_wr16(C7108_AES_DMA_SRC0_HI, (u16)((src & 0xffff0000) >> 16));
  108. c7108_reg_wr16(C7108_AES_DMA_DST0_LO, (u16) dst);
  109. c7108_reg_wr16(C7108_AES_DMA_DST0_HI, (u16)((dst & 0xffff0000) >> 16));
  110. c7108_reg_wr16(C7108_AES_DMA_LEN, (u16) ((len / 16) - 1));
  111. return 0;
  112. }
  113. static int
  114. c7108_aes_set_hw_iv(u8 iv[16])
  115. {
  116. c7108_reg_wr16(C7108_AES_IV0_LO, (u16) ((iv[1] << 8) | iv[0]));
  117. c7108_reg_wr16(C7108_AES_IV0_HI, (u16) ((iv[3] << 8) | iv[2]));
  118. c7108_reg_wr16(C7108_AES_IV1_LO, (u16) ((iv[5] << 8) | iv[4]));
  119. c7108_reg_wr16(C7108_AES_IV1_HI, (u16) ((iv[7] << 8) | iv[6]));
  120. c7108_reg_wr16(C7108_AES_IV2_LO, (u16) ((iv[9] << 8) | iv[8]));
  121. c7108_reg_wr16(C7108_AES_IV2_HI, (u16) ((iv[11] << 8) | iv[10]));
  122. c7108_reg_wr16(C7108_AES_IV3_LO, (u16) ((iv[13] << 8) | iv[12]));
  123. c7108_reg_wr16(C7108_AES_IV3_HI, (u16) ((iv[15] << 8) | iv[14]));
  124. return 0;
  125. }
  126. static void
  127. c7108_aes_read_dkey(u32 * dkey)
  128. {
  129. dkey[0] = (c7108_reg_rd16(C7108_AES_EKEY0_HI) << 16) |
  130. c7108_reg_rd16(C7108_AES_EKEY0_LO);
  131. dkey[1] = (c7108_reg_rd16(C7108_AES_EKEY1_HI) << 16) |
  132. c7108_reg_rd16(C7108_AES_EKEY1_LO);
  133. dkey[2] = (c7108_reg_rd16(C7108_AES_EKEY2_HI) << 16) |
  134. c7108_reg_rd16(C7108_AES_EKEY2_LO);
  135. dkey[3] = (c7108_reg_rd16(C7108_AES_EKEY3_HI) << 16) |
  136. c7108_reg_rd16(C7108_AES_EKEY3_LO);
  137. dkey[4] = (c7108_reg_rd16(C7108_AES_EKEY4_HI) << 16) |
  138. c7108_reg_rd16(C7108_AES_EKEY4_LO);
  139. dkey[5] = (c7108_reg_rd16(C7108_AES_EKEY5_HI) << 16) |
  140. c7108_reg_rd16(C7108_AES_EKEY5_LO);
  141. dkey[6] = (c7108_reg_rd16(C7108_AES_EKEY6_HI) << 16) |
  142. c7108_reg_rd16(C7108_AES_EKEY6_LO);
  143. dkey[7] = (c7108_reg_rd16(C7108_AES_EKEY7_HI) << 16) |
  144. c7108_reg_rd16(C7108_AES_EKEY7_LO);
  145. }
  146. static int
  147. c7108_aes_cipher(int op,
  148. u32 dst,
  149. u32 src,
  150. u32 len,
  151. int klen,
  152. u16 mode,
  153. u32 key[8],
  154. u8 iv[16])
  155. {
  156. int rv = 0, cnt=0;
  157. u16 ctrl = 0, stat = 0;
  158. AES_LOCK();
  159. /* Setup key length */
  160. if (klen == 128) {
  161. ctrl |= C7108_AES_KEY_LEN_128;
  162. } else if (klen == 192) {
  163. ctrl |= C7108_AES_KEY_LEN_192;
  164. } else if (klen == 256) {
  165. ctrl |= C7108_AES_KEY_LEN_256;
  166. } else {
  167. AES_UNLOCK();
  168. return -3;
  169. }
  170. /* Check opcode */
  171. if (C7108_AES_ENCRYPT == op) {
  172. ctrl |= C7108_AES_ENCRYPT;
  173. } else if (C7108_AES_DECRYPT == op) {
  174. ctrl |= C7108_AES_DECRYPT;
  175. } else {
  176. AES_UNLOCK();
  177. return -4;
  178. }
  179. /* check mode */
  180. if ( (mode != C7108_AES_CTRL_MODE_CBC) &&
  181. (mode != C7108_AES_CTRL_MODE_CFB) &&
  182. (mode != C7108_AES_CTRL_MODE_OFB) &&
  183. (mode != C7108_AES_CTRL_MODE_CTR) &&
  184. (mode != C7108_AES_CTRL_MODE_ECB) ) {
  185. AES_UNLOCK();
  186. return -5;
  187. }
  188. /* Now set mode */
  189. ctrl |= mode;
  190. /* For CFB, OFB, and CTR, neither backward key
  191. * expansion nor key inversion is required.
  192. */
  193. if ( (C7108_AES_DECRYPT == op) &&
  194. (C7108_AES_CTRL_MODE_CBC == mode ||
  195. C7108_AES_CTRL_MODE_ECB == mode ) ){
  196. /* Program Key */
  197. c7108_reg_wr16(C7108_AES_KEY0_LO, (u16) key[4]);
  198. c7108_reg_wr16(C7108_AES_KEY0_HI, (u16) (key[4] >> 16));
  199. c7108_reg_wr16(C7108_AES_KEY1_LO, (u16) key[5]);
  200. c7108_reg_wr16(C7108_AES_KEY1_HI, (u16) (key[5] >> 16));
  201. c7108_reg_wr16(C7108_AES_KEY2_LO, (u16) key[6]);
  202. c7108_reg_wr16(C7108_AES_KEY2_HI, (u16) (key[6] >> 16));
  203. c7108_reg_wr16(C7108_AES_KEY3_LO, (u16) key[7]);
  204. c7108_reg_wr16(C7108_AES_KEY3_HI, (u16) (key[7] >> 16));
  205. c7108_reg_wr16(C7108_AES_KEY6_LO, (u16) key[2]);
  206. c7108_reg_wr16(C7108_AES_KEY6_HI, (u16) (key[2] >> 16));
  207. c7108_reg_wr16(C7108_AES_KEY7_LO, (u16) key[3]);
  208. c7108_reg_wr16(C7108_AES_KEY7_HI, (u16) (key[3] >> 16));
  209. if (192 == klen) {
  210. c7108_reg_wr16(C7108_AES_KEY4_LO, (u16) key[7]);
  211. c7108_reg_wr16(C7108_AES_KEY4_HI, (u16) (key[7] >> 16));
  212. c7108_reg_wr16(C7108_AES_KEY5_LO, (u16) key[7]);
  213. c7108_reg_wr16(C7108_AES_KEY5_HI, (u16) (key[7] >> 16));
  214. } else if (256 == klen) {
  215. /* 256 */
  216. c7108_reg_wr16(C7108_AES_KEY4_LO, (u16) key[0]);
  217. c7108_reg_wr16(C7108_AES_KEY4_HI, (u16) (key[0] >> 16));
  218. c7108_reg_wr16(C7108_AES_KEY5_LO, (u16) key[1]);
  219. c7108_reg_wr16(C7108_AES_KEY5_HI, (u16) (key[1] >> 16));
  220. }
  221. } else {
  222. /* Program Key */
  223. c7108_reg_wr16(C7108_AES_KEY0_LO, (u16) key[0]);
  224. c7108_reg_wr16(C7108_AES_KEY0_HI, (u16) (key[0] >> 16));
  225. c7108_reg_wr16(C7108_AES_KEY1_LO, (u16) key[1]);
  226. c7108_reg_wr16(C7108_AES_KEY1_HI, (u16) (key[1] >> 16));
  227. c7108_reg_wr16(C7108_AES_KEY2_LO, (u16) key[2]);
  228. c7108_reg_wr16(C7108_AES_KEY2_HI, (u16) (key[2] >> 16));
  229. c7108_reg_wr16(C7108_AES_KEY3_LO, (u16) key[3]);
  230. c7108_reg_wr16(C7108_AES_KEY3_HI, (u16) (key[3] >> 16));
  231. c7108_reg_wr16(C7108_AES_KEY4_LO, (u16) key[4]);
  232. c7108_reg_wr16(C7108_AES_KEY4_HI, (u16) (key[4] >> 16));
  233. c7108_reg_wr16(C7108_AES_KEY5_LO, (u16) key[5]);
  234. c7108_reg_wr16(C7108_AES_KEY5_HI, (u16) (key[5] >> 16));
  235. c7108_reg_wr16(C7108_AES_KEY6_LO, (u16) key[6]);
  236. c7108_reg_wr16(C7108_AES_KEY6_HI, (u16) (key[6] >> 16));
  237. c7108_reg_wr16(C7108_AES_KEY7_LO, (u16) key[7]);
  238. c7108_reg_wr16(C7108_AES_KEY7_HI, (u16) (key[7] >> 16));
  239. }
  240. /* Set IV always */
  241. c7108_aes_set_hw_iv(iv);
  242. /* Program DMA addresses */
  243. if ((rv = c7108_aes_setup_dma(src, dst, len)) < 0) {
  244. AES_UNLOCK();
  245. return rv;
  246. }
  247. /* Start AES cipher */
  248. c7108_reg_wr16(C7108_AES_CTRL, ctrl | C7108_AES_GO);
  249. //printk("Ctrl: 0x%x\n", ctrl | C7108_AES_GO);
  250. do {
  251. /* TODO: interrupt mode */
  252. // printk("aes_stat=0x%x\n", stat);
  253. //udelay(100);
  254. } while ((cnt++ < 1000000) &&
  255. !((stat=c7108_reg_rd16(C7108_AES_CTRL))&C7108_AES_OP_DONE));
  256. if ((mode == C7108_AES_CTRL_MODE_ECB)||
  257. (mode == C7108_AES_CTRL_MODE_CBC)) {
  258. /* Save out key when the lock is held ... */
  259. c7108_aes_read_dkey(key);
  260. }
  261. AES_UNLOCK();
  262. return 0;
  263. }
  264. /*
  265. * Generate a new crypto device session.
  266. */
  267. static int
  268. c7108_newsession(void *arg, u_int32_t *sid, struct cryptoini *cri)
  269. {
  270. struct cipher_7108 **swd;
  271. u_int32_t i;
  272. char *algo;
  273. int mode, xfm_type;
  274. dprintk("%s()\n", __FUNCTION__);
  275. if (sid == NULL || cri == NULL) {
  276. dprintk("%s,%d - EINVAL\n", __FILE__, __LINE__);
  277. return EINVAL;
  278. }
  279. if (c7108_sessions) {
  280. for (i = 1; i < c7108_sesnum; i++)
  281. if (c7108_sessions[i] == NULL)
  282. break;
  283. } else
  284. i = 1; /* NB: to silence compiler warning */
  285. if (c7108_sessions == NULL || i == c7108_sesnum) {
  286. if (c7108_sessions == NULL) {
  287. i = 1; /* We leave c7108_sessions[0] empty */
  288. c7108_sesnum = CRYPTO_SW_SESSIONS;
  289. } else
  290. c7108_sesnum *= 2;
  291. swd = kmalloc(c7108_sesnum * sizeof(struct cipher_7108 *),
  292. GFP_ATOMIC);
  293. if (swd == NULL) {
  294. /* Reset session number */
  295. if (c7108_sesnum == CRYPTO_SW_SESSIONS)
  296. c7108_sesnum = 0;
  297. else
  298. c7108_sesnum /= 2;
  299. dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
  300. return ENOBUFS;
  301. }
  302. memset(swd, 0, c7108_sesnum * sizeof(struct cipher_7108 *));
  303. /* Copy existing sessions */
  304. if (c7108_sessions) {
  305. memcpy(swd, c7108_sessions,
  306. (c7108_sesnum / 2) * sizeof(struct cipher_7108 *));
  307. kfree(c7108_sessions);
  308. }
  309. c7108_sessions = swd;
  310. }
  311. swd = &c7108_sessions[i];
  312. *sid = i;
  313. while (cri) {
  314. *swd = (struct cipher_7108 *)
  315. kmalloc(sizeof(struct cipher_7108), GFP_ATOMIC);
  316. if (*swd == NULL) {
  317. c7108_freesession(NULL, i);
  318. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  319. return ENOBUFS;
  320. }
  321. memset(*swd, 0, sizeof(struct cipher_7108));
  322. algo = NULL;
  323. mode = 0;
  324. xfm_type = HW_TYPE_CIPHER;
  325. switch (cri->cri_alg) {
  326. case CRYPTO_AES_CBC:
  327. algo = "aes";
  328. mode = CRYPTO_TFM_MODE_CBC;
  329. c7108_crypto_mode = C7108_AES_CTRL_MODE_CBC;
  330. break;
  331. #if 0
  332. case CRYPTO_AES_CTR:
  333. algo = "aes_ctr";
  334. mode = CRYPTO_TFM_MODE_CBC;
  335. c7108_crypto_mode = C7108_AES_CTRL_MODE_CTR;
  336. break;
  337. case CRYPTO_AES_ECB:
  338. algo = "aes_ecb";
  339. mode = CRYPTO_TFM_MODE_CBC;
  340. c7108_crypto_mode = C7108_AES_CTRL_MODE_ECB;
  341. break;
  342. case CRYPTO_AES_OFB:
  343. algo = "aes_ofb";
  344. mode = CRYPTO_TFM_MODE_CBC;
  345. c7108_crypto_mode = C7108_AES_CTRL_MODE_OFB;
  346. break;
  347. case CRYPTO_AES_CFB:
  348. algo = "aes_cfb";
  349. mode = CRYPTO_TFM_MODE_CBC;
  350. c7108_crypto_mode = C7108_AES_CTRL_MODE_CFB;
  351. break;
  352. #endif
  353. default:
  354. printk("unsupported crypto algorithm: %d\n",
  355. cri->cri_alg);
  356. return -EINVAL;
  357. break;
  358. }
  359. if (!algo || !*algo) {
  360. printk("cypher_7108_crypto: Unknown algo 0x%x\n",
  361. cri->cri_alg);
  362. c7108_freesession(NULL, i);
  363. return EINVAL;
  364. }
  365. if (xfm_type == HW_TYPE_CIPHER) {
  366. if (debug) {
  367. dprintk("%s key:", __FUNCTION__);
  368. for (i = 0; i < (cri->cri_klen + 7) / 8; i++)
  369. dprintk("%s0x%02x", (i % 8) ? " " : "\n ",
  370. cri->cri_key[i]);
  371. dprintk("\n");
  372. }
  373. } else if (xfm_type == SW_TYPE_HMAC ||
  374. xfm_type == SW_TYPE_HASH) {
  375. printk("cypher_7108_crypto: HMAC unsupported!\n");
  376. return -EINVAL;
  377. c7108_freesession(NULL, i);
  378. } else {
  379. printk("cypher_7108_crypto: "
  380. "Unhandled xfm_type %d\n", xfm_type);
  381. c7108_freesession(NULL, i);
  382. return EINVAL;
  383. }
  384. (*swd)->cri_alg = cri->cri_alg;
  385. (*swd)->xfm_type = xfm_type;
  386. cri = cri->cri_next;
  387. swd = &((*swd)->next);
  388. }
  389. return 0;
  390. }
  391. /*
  392. * Free a session.
  393. */
  394. static int
  395. c7108_freesession(void *arg, u_int64_t tid)
  396. {
  397. struct cipher_7108 *swd;
  398. u_int32_t sid = CRYPTO_SESID2LID(tid);
  399. dprintk("%s()\n", __FUNCTION__);
  400. if (sid > c7108_sesnum || c7108_sessions == NULL ||
  401. c7108_sessions[sid] == NULL) {
  402. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  403. return(EINVAL);
  404. }
  405. /* Silently accept and return */
  406. if (sid == 0)
  407. return(0);
  408. while ((swd = c7108_sessions[sid]) != NULL) {
  409. c7108_sessions[sid] = swd->next;
  410. kfree(swd);
  411. }
  412. return 0;
  413. }
  414. /*
  415. * Process a hardware request.
  416. */
  417. static int
  418. c7108_process(void *arg, struct cryptop *crp, int hint)
  419. {
  420. struct cryptodesc *crd;
  421. struct cipher_7108 *sw;
  422. u_int32_t lid;
  423. int type;
  424. u32 hwkey[8];
  425. #define SCATTERLIST_MAX 16
  426. struct scatterlist sg[SCATTERLIST_MAX];
  427. int sg_num, sg_len, skip;
  428. struct sk_buff *skb = NULL;
  429. struct uio *uiop = NULL;
  430. dprintk("%s()\n", __FUNCTION__);
  431. /* Sanity check */
  432. if (crp == NULL) {
  433. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  434. return EINVAL;
  435. }
  436. crp->crp_etype = 0;
  437. if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
  438. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  439. crp->crp_etype = EINVAL;
  440. goto done;
  441. }
  442. lid = crp->crp_sid & 0xffffffff;
  443. if (lid >= c7108_sesnum || lid == 0 || c7108_sessions == NULL ||
  444. c7108_sessions[lid] == NULL) {
  445. crp->crp_etype = ENOENT;
  446. dprintk("%s,%d: ENOENT\n", __FILE__, __LINE__);
  447. goto done;
  448. }
  449. /*
  450. * do some error checking outside of the loop for SKB and IOV
  451. * processing this leaves us with valid skb or uiop pointers
  452. * for later
  453. */
  454. if (crp->crp_flags & CRYPTO_F_SKBUF) {
  455. skb = (struct sk_buff *) crp->crp_buf;
  456. if (skb_shinfo(skb)->nr_frags >= SCATTERLIST_MAX) {
  457. printk("%s,%d: %d nr_frags > SCATTERLIST_MAX",
  458. __FILE__, __LINE__,
  459. skb_shinfo(skb)->nr_frags);
  460. goto done;
  461. }
  462. } else if (crp->crp_flags & CRYPTO_F_IOV) {
  463. uiop = (struct uio *) crp->crp_buf;
  464. if (uiop->uio_iovcnt > SCATTERLIST_MAX) {
  465. printk("%s,%d: %d uio_iovcnt > SCATTERLIST_MAX",
  466. __FILE__, __LINE__,
  467. uiop->uio_iovcnt);
  468. goto done;
  469. }
  470. }
  471. /* Go through crypto descriptors, processing as we go */
  472. for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
  473. /*
  474. * Find the crypto context.
  475. *
  476. * XXX Note that the logic here prevents us from having
  477. * XXX the same algorithm multiple times in a session
  478. * XXX (or rather, we can but it won't give us the right
  479. * XXX results). To do that, we'd need some way of differentiating
  480. * XXX between the various instances of an algorithm (so we can
  481. * XXX locate the correct crypto context).
  482. */
  483. for (sw = c7108_sessions[lid];
  484. sw && sw->cri_alg != crd->crd_alg;
  485. sw = sw->next)
  486. ;
  487. /* No such context ? */
  488. if (sw == NULL) {
  489. crp->crp_etype = EINVAL;
  490. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  491. goto done;
  492. }
  493. skip = crd->crd_skip;
  494. /*
  495. * setup the SG list skip from the start of the buffer
  496. */
  497. memset(sg, 0, sizeof(sg));
  498. if (crp->crp_flags & CRYPTO_F_SKBUF) {
  499. int i, len;
  500. type = CRYPTO_BUF_SKBUF;
  501. sg_num = 0;
  502. sg_len = 0;
  503. if (skip < skb_headlen(skb)) {
  504. //sg[sg_num].page = virt_to_page(skb->data + skip);
  505. //sg[sg_num].offset = offset_in_page(skb->data + skip);
  506. len = skb_headlen(skb) - skip;
  507. if (len + sg_len > crd->crd_len)
  508. len = crd->crd_len - sg_len;
  509. //sg[sg_num].length = len;
  510. sg_set_page(&sg[sg_num], virt_to_page(skb->data + skip), len, offset_in_page(skb->data + skip));
  511. sg_len += sg[sg_num].length;
  512. sg_num++;
  513. skip = 0;
  514. } else
  515. skip -= skb_headlen(skb);
  516. for (i = 0; sg_len < crd->crd_len &&
  517. i < skb_shinfo(skb)->nr_frags &&
  518. sg_num < SCATTERLIST_MAX; i++) {
  519. if (skip < skb_shinfo(skb)->frags[i].size) {
  520. //sg[sg_num].page = skb_frag_page(&skb_shinfo(skb)->frags[i]);
  521. //sg[sg_num].offset = skb_shinfo(skb)->frags[i].page_offset + skip;
  522. len = skb_shinfo(skb)->frags[i].size - skip;
  523. if (len + sg_len > crd->crd_len)
  524. len = crd->crd_len - sg_len;
  525. //sg[sg_num].length = len;
  526. sg_set_page(&sg[sg_num], skb_frag_page(&skb_shinfo(skb)->frags[i]), len, skb_shinfo(skb)->frags[i].page_offset + skip);
  527. sg_len += sg[sg_num].length;
  528. sg_num++;
  529. skip = 0;
  530. } else
  531. skip -= skb_shinfo(skb)->frags[i].size;
  532. }
  533. } else if (crp->crp_flags & CRYPTO_F_IOV) {
  534. int len;
  535. type = CRYPTO_BUF_IOV;
  536. sg_len = 0;
  537. for (sg_num = 0; sg_len < crd->crd_len &&
  538. sg_num < uiop->uio_iovcnt &&
  539. sg_num < SCATTERLIST_MAX; sg_num++) {
  540. if (skip < uiop->uio_iov[sg_num].iov_len) {
  541. //sg[sg_num].page = virt_to_page(uiop->uio_iov[sg_num].iov_base+skip);
  542. //sg[sg_num].offset = offset_in_page(uiop->uio_iov[sg_num].iov_base+skip);
  543. len = uiop->uio_iov[sg_num].iov_len - skip;
  544. if (len + sg_len > crd->crd_len)
  545. len = crd->crd_len - sg_len;
  546. //sg[sg_num].length = len;
  547. sg_set_page(&sg[sg_num], virt_to_page(uiop->uio_iov[sg_num].iov_base+skip), len, offset_in_page(uiop->uio_iov[sg_num].iov_base+skip));
  548. sg_len += sg[sg_num].length;
  549. skip = 0;
  550. } else
  551. skip -= uiop->uio_iov[sg_num].iov_len;
  552. }
  553. } else {
  554. type = CRYPTO_BUF_CONTIG;
  555. //sg[0].page = virt_to_page(crp->crp_buf + skip);
  556. //sg[0].offset = offset_in_page(crp->crp_buf + skip);
  557. sg_len = (crp->crp_ilen - skip);
  558. if (sg_len > crd->crd_len)
  559. sg_len = crd->crd_len;
  560. //sg[0].length = sg_len;
  561. sg_set_page(&sg[0], virt_to_page(crp->crp_buf + skip), sg_len, offset_in_page(crp->crp_buf + skip));
  562. sg_num = 1;
  563. }
  564. if (sg_num > 0)
  565. sg_mark_end(&sg[sg_num-1]);
  566. switch (sw->xfm_type) {
  567. case HW_TYPE_CIPHER: {
  568. unsigned char iv[64];
  569. unsigned char *ivp = iv;
  570. int i;
  571. int ivsize = 16; /* fixed for AES */
  572. int blocksize = 16; /* fixed for AES */
  573. if (sg_len < blocksize) {
  574. crp->crp_etype = EINVAL;
  575. dprintk("%s,%d: EINVAL len %d < %d\n",
  576. __FILE__, __LINE__,
  577. sg_len,
  578. blocksize);
  579. goto done;
  580. }
  581. if (ivsize > sizeof(iv)) {
  582. crp->crp_etype = EINVAL;
  583. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  584. goto done;
  585. }
  586. if (crd->crd_flags & CRD_F_ENCRYPT) { /* encrypt */
  587. if (crd->crd_flags & CRD_F_IV_EXPLICIT) {
  588. ivp = crd->crd_iv;
  589. } else {
  590. get_random_bytes(ivp, ivsize);
  591. }
  592. /*
  593. * do we have to copy the IV back to the buffer ?
  594. */
  595. if ((crd->crd_flags & CRD_F_IV_PRESENT) == 0) {
  596. crypto_copyback(crp->crp_buf,
  597. crd->crd_inject,
  598. ivsize,
  599. (caddr_t)ivp);
  600. }
  601. c7108_xlate_key(crd->crd_klen,
  602. (u8*)crd->crd_key, (u32*)hwkey);
  603. /* Encrypt SG list */
  604. for (i = 0; i < sg_num; i++) {
  605. sg[i].dma_address =
  606. dma_map_single(NULL,
  607. kmap(sg_page(&sg[i])) + sg[i].offset, sg_len, DMA_BIDIRECTIONAL);
  608. #if 0
  609. printk("sg[%d]:0x%08x, off 0x%08x "
  610. "kmap 0x%08x phys 0x%08x\n",
  611. i, sg[i].page, sg[i].offset,
  612. kmap(sg[i].page) + sg[i].offset,
  613. sg[i].dma_address);
  614. #endif
  615. c7108_aes_cipher(C7108_AES_ENCRYPT,
  616. sg[i].dma_address,
  617. sg[i].dma_address,
  618. sg_len,
  619. crd->crd_klen,
  620. c7108_crypto_mode,
  621. hwkey,
  622. ivp);
  623. if ((c7108_crypto_mode == C7108_AES_CTRL_MODE_CBC)||
  624. (c7108_crypto_mode == C7108_AES_CTRL_MODE_ECB)) {
  625. /* Read back expanded key and cache it in key
  626. * context.
  627. * NOTE: for ECB/CBC modes only (not CTR, CFB, OFB)
  628. * where you set the key once.
  629. */
  630. c7108_cache_key(crd->crd_klen,
  631. (u32*)hwkey, (u8*)crd->crd_key);
  632. #if 0
  633. printk("%s expanded key:", __FUNCTION__);
  634. for (i = 0; i < (crd->crd_klen + 7) / 8; i++)
  635. printk("%s0x%02x", (i % 8) ? " " : "\n ",
  636. crd->crd_key[i]);
  637. printk("\n");
  638. #endif
  639. }
  640. }
  641. }
  642. else { /*decrypt */
  643. if (crd->crd_flags & CRD_F_IV_EXPLICIT) {
  644. ivp = crd->crd_iv;
  645. } else {
  646. crypto_copydata(crp->crp_buf, crd->crd_inject,
  647. ivsize, (caddr_t)ivp);
  648. }
  649. c7108_xlate_key(crd->crd_klen,
  650. (u8*)crd->crd_key, (u32*)hwkey);
  651. /* Decrypt SG list */
  652. for (i = 0; i < sg_num; i++) {
  653. sg[i].dma_address =
  654. dma_map_single(NULL,
  655. kmap(sg_page(&sg[i])) + sg[i].offset,
  656. sg_len, DMA_BIDIRECTIONAL);
  657. #if 0
  658. printk("sg[%d]:0x%08x, off 0x%08x "
  659. "kmap 0x%08x phys 0x%08x\n",
  660. i, sg[i].page, sg[i].offset,
  661. kmap(sg[i].page) + sg[i].offset,
  662. sg[i].dma_address);
  663. #endif
  664. c7108_aes_cipher(C7108_AES_DECRYPT,
  665. sg[i].dma_address,
  666. sg[i].dma_address,
  667. sg_len,
  668. crd->crd_klen,
  669. c7108_crypto_mode,
  670. hwkey,
  671. ivp);
  672. }
  673. }
  674. } break;
  675. case SW_TYPE_HMAC:
  676. case SW_TYPE_HASH:
  677. crp->crp_etype = EINVAL;
  678. goto done;
  679. break;
  680. case SW_TYPE_COMP:
  681. crp->crp_etype = EINVAL;
  682. goto done;
  683. break;
  684. default:
  685. /* Unknown/unsupported algorithm */
  686. dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
  687. crp->crp_etype = EINVAL;
  688. goto done;
  689. }
  690. }
  691. done:
  692. crypto_done(crp);
  693. return 0;
  694. }
  695. static struct {
  696. softc_device_decl sc_dev;
  697. } a7108dev;
  698. static device_method_t a7108_methods = {
  699. /* crypto device methods */
  700. DEVMETHOD(cryptodev_newsession, c7108_newsession),
  701. DEVMETHOD(cryptodev_freesession, c7108_freesession),
  702. DEVMETHOD(cryptodev_process, c7108_process),
  703. DEVMETHOD(cryptodev_kprocess, NULL)
  704. };
  705. static int
  706. cypher_7108_crypto_init(void)
  707. {
  708. dprintk("%s(%p)\n", __FUNCTION__, cypher_7108_crypto_init);
  709. iobar = (unsigned long)ioremap(CCU_AES_REG_BASE, 0x4000);
  710. printk("7108: AES @ 0x%08x (0x%08x phys) %s mode\n",
  711. iobar, CCU_AES_REG_BASE,
  712. c7108_crypto_mode & C7108_AES_CTRL_MODE_CBC ? "CBC" :
  713. c7108_crypto_mode & C7108_AES_CTRL_MODE_ECB ? "ECB" :
  714. c7108_crypto_mode & C7108_AES_CTRL_MODE_CTR ? "CTR" :
  715. c7108_crypto_mode & C7108_AES_CTRL_MODE_CFB ? "CFB" :
  716. c7108_crypto_mode & C7108_AES_CTRL_MODE_OFB ? "OFB" : "???");
  717. csr_mutex = SPIN_LOCK_UNLOCKED;
  718. memset(&a7108dev, 0, sizeof(a7108dev));
  719. softc_device_init(&a7108dev, "aes7108", 0, a7108_methods);
  720. c7108_id = crypto_get_driverid(softc_get_device(&a7108dev), CRYPTOCAP_F_HARDWARE);
  721. if (c7108_id < 0)
  722. panic("7108: crypto device cannot initialize!");
  723. // crypto_register(c7108_id, CRYPTO_AES_CBC, 0, 0, c7108_newsession, c7108_freesession, c7108_process, NULL);
  724. crypto_register(c7108_id, CRYPTO_AES_CBC, 0, 0);
  725. return(0);
  726. }
  727. static void
  728. cypher_7108_crypto_exit(void)
  729. {
  730. dprintk("%s()\n", __FUNCTION__);
  731. crypto_unregister_all(c7108_id);
  732. c7108_id = -1;
  733. }
  734. module_init(cypher_7108_crypto_init);
  735. module_exit(cypher_7108_crypto_exit);
  736. MODULE_LICENSE("Dual BSD/GPL");
  737. MODULE_DESCRIPTION("Cypher 7108 Crypto (OCF module for kernel crypto)");