ixp4xx_crypto.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. /*
  2. * Intel IXP4xx NPE-C crypto driver
  3. *
  4. * Copyright (C) 2008 Christian Hohnstaedt <chohnstaedt@innominate.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/platform_device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/dmapool.h>
  14. #include <linux/crypto.h>
  15. #include <linux/kernel.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/gfp.h>
  20. #include <linux/module.h>
  21. #include <crypto/ctr.h>
  22. #include <crypto/des.h>
  23. #include <crypto/aes.h>
  24. #include <crypto/sha.h>
  25. #include <crypto/algapi.h>
  26. #include <crypto/aead.h>
  27. #include <crypto/authenc.h>
  28. #include <crypto/scatterwalk.h>
  29. #include <mach/npe.h>
  30. #include <mach/qmgr.h>
  31. #define MAX_KEYLEN 32
  32. /* hash: cfgword + 2 * digestlen; crypt: keylen + cfgword */
  33. #define NPE_CTX_LEN 80
  34. #define AES_BLOCK128 16
  35. #define NPE_OP_HASH_VERIFY 0x01
  36. #define NPE_OP_CCM_ENABLE 0x04
  37. #define NPE_OP_CRYPT_ENABLE 0x08
  38. #define NPE_OP_HASH_ENABLE 0x10
  39. #define NPE_OP_NOT_IN_PLACE 0x20
  40. #define NPE_OP_HMAC_DISABLE 0x40
  41. #define NPE_OP_CRYPT_ENCRYPT 0x80
  42. #define NPE_OP_CCM_GEN_MIC 0xcc
  43. #define NPE_OP_HASH_GEN_ICV 0x50
  44. #define NPE_OP_ENC_GEN_KEY 0xc9
  45. #define MOD_ECB 0x0000
  46. #define MOD_CTR 0x1000
  47. #define MOD_CBC_ENC 0x2000
  48. #define MOD_CBC_DEC 0x3000
  49. #define MOD_CCM_ENC 0x4000
  50. #define MOD_CCM_DEC 0x5000
  51. #define KEYLEN_128 4
  52. #define KEYLEN_192 6
  53. #define KEYLEN_256 8
  54. #define CIPH_DECR 0x0000
  55. #define CIPH_ENCR 0x0400
  56. #define MOD_DES 0x0000
  57. #define MOD_TDEA2 0x0100
  58. #define MOD_3DES 0x0200
  59. #define MOD_AES 0x0800
  60. #define MOD_AES128 (0x0800 | KEYLEN_128)
  61. #define MOD_AES192 (0x0900 | KEYLEN_192)
  62. #define MOD_AES256 (0x0a00 | KEYLEN_256)
  63. #define MAX_IVLEN 16
  64. #define NPE_ID 2 /* NPE C */
  65. #define NPE_QLEN 16
  66. /* Space for registering when the first
  67. * NPE_QLEN crypt_ctl are busy */
  68. #define NPE_QLEN_TOTAL 64
  69. #define SEND_QID 29
  70. #define RECV_QID 30
  71. #define CTL_FLAG_UNUSED 0x0000
  72. #define CTL_FLAG_USED 0x1000
  73. #define CTL_FLAG_PERFORM_ABLK 0x0001
  74. #define CTL_FLAG_GEN_ICV 0x0002
  75. #define CTL_FLAG_GEN_REVAES 0x0004
  76. #define CTL_FLAG_PERFORM_AEAD 0x0008
  77. #define CTL_FLAG_MASK 0x000f
  78. #define HMAC_IPAD_VALUE 0x36
  79. #define HMAC_OPAD_VALUE 0x5C
  80. #define HMAC_PAD_BLOCKLEN SHA1_BLOCK_SIZE
  81. #define MD5_DIGEST_SIZE 16
  82. struct buffer_desc {
  83. u32 phys_next;
  84. #ifdef __ARMEB__
  85. u16 buf_len;
  86. u16 pkt_len;
  87. #else
  88. u16 pkt_len;
  89. u16 buf_len;
  90. #endif
  91. u32 phys_addr;
  92. u32 __reserved[4];
  93. struct buffer_desc *next;
  94. enum dma_data_direction dir;
  95. };
  96. struct crypt_ctl {
  97. #ifdef __ARMEB__
  98. u8 mode; /* NPE_OP_* operation mode */
  99. u8 init_len;
  100. u16 reserved;
  101. #else
  102. u16 reserved;
  103. u8 init_len;
  104. u8 mode; /* NPE_OP_* operation mode */
  105. #endif
  106. u8 iv[MAX_IVLEN]; /* IV for CBC mode or CTR IV for CTR mode */
  107. u32 icv_rev_aes; /* icv or rev aes */
  108. u32 src_buf;
  109. u32 dst_buf;
  110. #ifdef __ARMEB__
  111. u16 auth_offs; /* Authentication start offset */
  112. u16 auth_len; /* Authentication data length */
  113. u16 crypt_offs; /* Cryption start offset */
  114. u16 crypt_len; /* Cryption data length */
  115. #else
  116. u16 auth_len; /* Authentication data length */
  117. u16 auth_offs; /* Authentication start offset */
  118. u16 crypt_len; /* Cryption data length */
  119. u16 crypt_offs; /* Cryption start offset */
  120. #endif
  121. u32 aadAddr; /* Additional Auth Data Addr for CCM mode */
  122. u32 crypto_ctx; /* NPE Crypto Param structure address */
  123. /* Used by Host: 4*4 bytes*/
  124. unsigned ctl_flags;
  125. union {
  126. struct ablkcipher_request *ablk_req;
  127. struct aead_request *aead_req;
  128. struct crypto_tfm *tfm;
  129. } data;
  130. struct buffer_desc *regist_buf;
  131. u8 *regist_ptr;
  132. };
  133. struct ablk_ctx {
  134. struct buffer_desc *src;
  135. struct buffer_desc *dst;
  136. };
  137. struct aead_ctx {
  138. struct buffer_desc *buffer;
  139. struct scatterlist ivlist;
  140. /* used when the hmac is not on one sg entry */
  141. u8 *hmac_virt;
  142. int encrypt;
  143. };
  144. struct ix_hash_algo {
  145. u32 cfgword;
  146. unsigned char *icv;
  147. };
  148. struct ix_sa_dir {
  149. unsigned char *npe_ctx;
  150. dma_addr_t npe_ctx_phys;
  151. int npe_ctx_idx;
  152. u8 npe_mode;
  153. };
  154. struct ixp_ctx {
  155. struct ix_sa_dir encrypt;
  156. struct ix_sa_dir decrypt;
  157. int authkey_len;
  158. u8 authkey[MAX_KEYLEN];
  159. int enckey_len;
  160. u8 enckey[MAX_KEYLEN];
  161. u8 salt[MAX_IVLEN];
  162. u8 nonce[CTR_RFC3686_NONCE_SIZE];
  163. unsigned salted;
  164. atomic_t configuring;
  165. struct completion completion;
  166. };
  167. struct ixp_alg {
  168. struct crypto_alg crypto;
  169. const struct ix_hash_algo *hash;
  170. u32 cfg_enc;
  171. u32 cfg_dec;
  172. int registered;
  173. };
  174. static const struct ix_hash_algo hash_alg_md5 = {
  175. .cfgword = 0xAA010004,
  176. .icv = "\x01\x23\x45\x67\x89\xAB\xCD\xEF"
  177. "\xFE\xDC\xBA\x98\x76\x54\x32\x10",
  178. };
  179. static const struct ix_hash_algo hash_alg_sha1 = {
  180. .cfgword = 0x00000005,
  181. .icv = "\x67\x45\x23\x01\xEF\xCD\xAB\x89\x98\xBA"
  182. "\xDC\xFE\x10\x32\x54\x76\xC3\xD2\xE1\xF0",
  183. };
  184. static struct npe *npe_c;
  185. static struct dma_pool *buffer_pool = NULL;
  186. static struct dma_pool *ctx_pool = NULL;
  187. static struct crypt_ctl *crypt_virt = NULL;
  188. static dma_addr_t crypt_phys;
  189. static int support_aes = 1;
  190. #define DRIVER_NAME "ixp4xx_crypto"
  191. static struct platform_device *pdev;
  192. static inline dma_addr_t crypt_virt2phys(struct crypt_ctl *virt)
  193. {
  194. return crypt_phys + (virt - crypt_virt) * sizeof(struct crypt_ctl);
  195. }
  196. static inline struct crypt_ctl *crypt_phys2virt(dma_addr_t phys)
  197. {
  198. return crypt_virt + (phys - crypt_phys) / sizeof(struct crypt_ctl);
  199. }
  200. static inline u32 cipher_cfg_enc(struct crypto_tfm *tfm)
  201. {
  202. return container_of(tfm->__crt_alg, struct ixp_alg,crypto)->cfg_enc;
  203. }
  204. static inline u32 cipher_cfg_dec(struct crypto_tfm *tfm)
  205. {
  206. return container_of(tfm->__crt_alg, struct ixp_alg,crypto)->cfg_dec;
  207. }
  208. static inline const struct ix_hash_algo *ix_hash(struct crypto_tfm *tfm)
  209. {
  210. return container_of(tfm->__crt_alg, struct ixp_alg, crypto)->hash;
  211. }
  212. static int setup_crypt_desc(void)
  213. {
  214. struct device *dev = &pdev->dev;
  215. BUILD_BUG_ON(sizeof(struct crypt_ctl) != 64);
  216. crypt_virt = dma_alloc_coherent(dev,
  217. NPE_QLEN * sizeof(struct crypt_ctl),
  218. &crypt_phys, GFP_ATOMIC);
  219. if (!crypt_virt)
  220. return -ENOMEM;
  221. memset(crypt_virt, 0, NPE_QLEN * sizeof(struct crypt_ctl));
  222. return 0;
  223. }
  224. static spinlock_t desc_lock;
  225. static struct crypt_ctl *get_crypt_desc(void)
  226. {
  227. int i;
  228. static int idx = 0;
  229. unsigned long flags;
  230. spin_lock_irqsave(&desc_lock, flags);
  231. if (unlikely(!crypt_virt))
  232. setup_crypt_desc();
  233. if (unlikely(!crypt_virt)) {
  234. spin_unlock_irqrestore(&desc_lock, flags);
  235. return NULL;
  236. }
  237. i = idx;
  238. if (crypt_virt[i].ctl_flags == CTL_FLAG_UNUSED) {
  239. if (++idx >= NPE_QLEN)
  240. idx = 0;
  241. crypt_virt[i].ctl_flags = CTL_FLAG_USED;
  242. spin_unlock_irqrestore(&desc_lock, flags);
  243. return crypt_virt +i;
  244. } else {
  245. spin_unlock_irqrestore(&desc_lock, flags);
  246. return NULL;
  247. }
  248. }
  249. static spinlock_t emerg_lock;
  250. static struct crypt_ctl *get_crypt_desc_emerg(void)
  251. {
  252. int i;
  253. static int idx = NPE_QLEN;
  254. struct crypt_ctl *desc;
  255. unsigned long flags;
  256. desc = get_crypt_desc();
  257. if (desc)
  258. return desc;
  259. if (unlikely(!crypt_virt))
  260. return NULL;
  261. spin_lock_irqsave(&emerg_lock, flags);
  262. i = idx;
  263. if (crypt_virt[i].ctl_flags == CTL_FLAG_UNUSED) {
  264. if (++idx >= NPE_QLEN_TOTAL)
  265. idx = NPE_QLEN;
  266. crypt_virt[i].ctl_flags = CTL_FLAG_USED;
  267. spin_unlock_irqrestore(&emerg_lock, flags);
  268. return crypt_virt +i;
  269. } else {
  270. spin_unlock_irqrestore(&emerg_lock, flags);
  271. return NULL;
  272. }
  273. }
  274. static void free_buf_chain(struct device *dev, struct buffer_desc *buf,u32 phys)
  275. {
  276. while (buf) {
  277. struct buffer_desc *buf1;
  278. u32 phys1;
  279. buf1 = buf->next;
  280. phys1 = buf->phys_next;
  281. dma_unmap_single(dev, buf->phys_next, buf->buf_len, buf->dir);
  282. dma_pool_free(buffer_pool, buf, phys);
  283. buf = buf1;
  284. phys = phys1;
  285. }
  286. }
  287. static struct tasklet_struct crypto_done_tasklet;
  288. static void finish_scattered_hmac(struct crypt_ctl *crypt)
  289. {
  290. struct aead_request *req = crypt->data.aead_req;
  291. struct aead_ctx *req_ctx = aead_request_ctx(req);
  292. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  293. int authsize = crypto_aead_authsize(tfm);
  294. int decryptlen = req->cryptlen - authsize;
  295. if (req_ctx->encrypt) {
  296. scatterwalk_map_and_copy(req_ctx->hmac_virt,
  297. req->src, decryptlen, authsize, 1);
  298. }
  299. dma_pool_free(buffer_pool, req_ctx->hmac_virt, crypt->icv_rev_aes);
  300. }
  301. static void one_packet(dma_addr_t phys)
  302. {
  303. struct device *dev = &pdev->dev;
  304. struct crypt_ctl *crypt;
  305. struct ixp_ctx *ctx;
  306. int failed;
  307. failed = phys & 0x1 ? -EBADMSG : 0;
  308. phys &= ~0x3;
  309. crypt = crypt_phys2virt(phys);
  310. switch (crypt->ctl_flags & CTL_FLAG_MASK) {
  311. case CTL_FLAG_PERFORM_AEAD: {
  312. struct aead_request *req = crypt->data.aead_req;
  313. struct aead_ctx *req_ctx = aead_request_ctx(req);
  314. free_buf_chain(dev, req_ctx->buffer, crypt->src_buf);
  315. if (req_ctx->hmac_virt) {
  316. finish_scattered_hmac(crypt);
  317. }
  318. req->base.complete(&req->base, failed);
  319. break;
  320. }
  321. case CTL_FLAG_PERFORM_ABLK: {
  322. struct ablkcipher_request *req = crypt->data.ablk_req;
  323. struct ablk_ctx *req_ctx = ablkcipher_request_ctx(req);
  324. if (req_ctx->dst) {
  325. free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
  326. }
  327. free_buf_chain(dev, req_ctx->src, crypt->src_buf);
  328. req->base.complete(&req->base, failed);
  329. break;
  330. }
  331. case CTL_FLAG_GEN_ICV:
  332. ctx = crypto_tfm_ctx(crypt->data.tfm);
  333. dma_pool_free(ctx_pool, crypt->regist_ptr,
  334. crypt->regist_buf->phys_addr);
  335. dma_pool_free(buffer_pool, crypt->regist_buf, crypt->src_buf);
  336. if (atomic_dec_and_test(&ctx->configuring))
  337. complete(&ctx->completion);
  338. break;
  339. case CTL_FLAG_GEN_REVAES:
  340. ctx = crypto_tfm_ctx(crypt->data.tfm);
  341. *(u32*)ctx->decrypt.npe_ctx &= cpu_to_be32(~CIPH_ENCR);
  342. if (atomic_dec_and_test(&ctx->configuring))
  343. complete(&ctx->completion);
  344. break;
  345. default:
  346. BUG();
  347. }
  348. crypt->ctl_flags = CTL_FLAG_UNUSED;
  349. }
  350. static void irqhandler(void *_unused)
  351. {
  352. tasklet_schedule(&crypto_done_tasklet);
  353. }
  354. static void crypto_done_action(unsigned long arg)
  355. {
  356. int i;
  357. for(i=0; i<4; i++) {
  358. dma_addr_t phys = qmgr_get_entry(RECV_QID);
  359. if (!phys)
  360. return;
  361. one_packet(phys);
  362. }
  363. tasklet_schedule(&crypto_done_tasklet);
  364. }
  365. static int init_ixp_crypto(struct device *dev)
  366. {
  367. int ret = -ENODEV;
  368. u32 msg[2] = { 0, 0 };
  369. if (! ( ~(*IXP4XX_EXP_CFG2) & (IXP4XX_FEATURE_HASH |
  370. IXP4XX_FEATURE_AES | IXP4XX_FEATURE_DES))) {
  371. printk(KERN_ERR "ixp_crypto: No HW crypto available\n");
  372. return ret;
  373. }
  374. npe_c = npe_request(NPE_ID);
  375. if (!npe_c)
  376. return ret;
  377. if (!npe_running(npe_c)) {
  378. ret = npe_load_firmware(npe_c, npe_name(npe_c), dev);
  379. if (ret) {
  380. return ret;
  381. }
  382. if (npe_recv_message(npe_c, msg, "STATUS_MSG"))
  383. goto npe_error;
  384. } else {
  385. if (npe_send_message(npe_c, msg, "STATUS_MSG"))
  386. goto npe_error;
  387. if (npe_recv_message(npe_c, msg, "STATUS_MSG"))
  388. goto npe_error;
  389. }
  390. switch ((msg[1]>>16) & 0xff) {
  391. case 3:
  392. printk(KERN_WARNING "Firmware of %s lacks AES support\n",
  393. npe_name(npe_c));
  394. support_aes = 0;
  395. break;
  396. case 4:
  397. case 5:
  398. support_aes = 1;
  399. break;
  400. default:
  401. printk(KERN_ERR "Firmware of %s lacks crypto support\n",
  402. npe_name(npe_c));
  403. return -ENODEV;
  404. }
  405. /* buffer_pool will also be used to sometimes store the hmac,
  406. * so assure it is large enough
  407. */
  408. BUILD_BUG_ON(SHA1_DIGEST_SIZE > sizeof(struct buffer_desc));
  409. buffer_pool = dma_pool_create("buffer", dev,
  410. sizeof(struct buffer_desc), 32, 0);
  411. ret = -ENOMEM;
  412. if (!buffer_pool) {
  413. goto err;
  414. }
  415. ctx_pool = dma_pool_create("context", dev,
  416. NPE_CTX_LEN, 16, 0);
  417. if (!ctx_pool) {
  418. goto err;
  419. }
  420. ret = qmgr_request_queue(SEND_QID, NPE_QLEN_TOTAL, 0, 0,
  421. "ixp_crypto:out", NULL);
  422. if (ret)
  423. goto err;
  424. ret = qmgr_request_queue(RECV_QID, NPE_QLEN, 0, 0,
  425. "ixp_crypto:in", NULL);
  426. if (ret) {
  427. qmgr_release_queue(SEND_QID);
  428. goto err;
  429. }
  430. qmgr_set_irq(RECV_QID, QUEUE_IRQ_SRC_NOT_EMPTY, irqhandler, NULL);
  431. tasklet_init(&crypto_done_tasklet, crypto_done_action, 0);
  432. qmgr_enable_irq(RECV_QID);
  433. return 0;
  434. npe_error:
  435. printk(KERN_ERR "%s not responding\n", npe_name(npe_c));
  436. ret = -EIO;
  437. err:
  438. if (ctx_pool)
  439. dma_pool_destroy(ctx_pool);
  440. if (buffer_pool)
  441. dma_pool_destroy(buffer_pool);
  442. npe_release(npe_c);
  443. return ret;
  444. }
  445. static void release_ixp_crypto(struct device *dev)
  446. {
  447. qmgr_disable_irq(RECV_QID);
  448. tasklet_kill(&crypto_done_tasklet);
  449. qmgr_release_queue(SEND_QID);
  450. qmgr_release_queue(RECV_QID);
  451. dma_pool_destroy(ctx_pool);
  452. dma_pool_destroy(buffer_pool);
  453. npe_release(npe_c);
  454. if (crypt_virt) {
  455. dma_free_coherent(dev,
  456. NPE_QLEN_TOTAL * sizeof( struct crypt_ctl),
  457. crypt_virt, crypt_phys);
  458. }
  459. return;
  460. }
  461. static void reset_sa_dir(struct ix_sa_dir *dir)
  462. {
  463. memset(dir->npe_ctx, 0, NPE_CTX_LEN);
  464. dir->npe_ctx_idx = 0;
  465. dir->npe_mode = 0;
  466. }
  467. static int init_sa_dir(struct ix_sa_dir *dir)
  468. {
  469. dir->npe_ctx = dma_pool_alloc(ctx_pool, GFP_KERNEL, &dir->npe_ctx_phys);
  470. if (!dir->npe_ctx) {
  471. return -ENOMEM;
  472. }
  473. reset_sa_dir(dir);
  474. return 0;
  475. }
  476. static void free_sa_dir(struct ix_sa_dir *dir)
  477. {
  478. memset(dir->npe_ctx, 0, NPE_CTX_LEN);
  479. dma_pool_free(ctx_pool, dir->npe_ctx, dir->npe_ctx_phys);
  480. }
  481. static int init_tfm(struct crypto_tfm *tfm)
  482. {
  483. struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  484. int ret;
  485. atomic_set(&ctx->configuring, 0);
  486. ret = init_sa_dir(&ctx->encrypt);
  487. if (ret)
  488. return ret;
  489. ret = init_sa_dir(&ctx->decrypt);
  490. if (ret) {
  491. free_sa_dir(&ctx->encrypt);
  492. }
  493. return ret;
  494. }
  495. static int init_tfm_ablk(struct crypto_tfm *tfm)
  496. {
  497. tfm->crt_ablkcipher.reqsize = sizeof(struct ablk_ctx);
  498. return init_tfm(tfm);
  499. }
  500. static int init_tfm_aead(struct crypto_tfm *tfm)
  501. {
  502. tfm->crt_aead.reqsize = sizeof(struct aead_ctx);
  503. return init_tfm(tfm);
  504. }
  505. static void exit_tfm(struct crypto_tfm *tfm)
  506. {
  507. struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  508. free_sa_dir(&ctx->encrypt);
  509. free_sa_dir(&ctx->decrypt);
  510. }
  511. static int register_chain_var(struct crypto_tfm *tfm, u8 xpad, u32 target,
  512. int init_len, u32 ctx_addr, const u8 *key, int key_len)
  513. {
  514. struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  515. struct crypt_ctl *crypt;
  516. struct buffer_desc *buf;
  517. int i;
  518. u8 *pad;
  519. u32 pad_phys, buf_phys;
  520. BUILD_BUG_ON(NPE_CTX_LEN < HMAC_PAD_BLOCKLEN);
  521. pad = dma_pool_alloc(ctx_pool, GFP_KERNEL, &pad_phys);
  522. if (!pad)
  523. return -ENOMEM;
  524. buf = dma_pool_alloc(buffer_pool, GFP_KERNEL, &buf_phys);
  525. if (!buf) {
  526. dma_pool_free(ctx_pool, pad, pad_phys);
  527. return -ENOMEM;
  528. }
  529. crypt = get_crypt_desc_emerg();
  530. if (!crypt) {
  531. dma_pool_free(ctx_pool, pad, pad_phys);
  532. dma_pool_free(buffer_pool, buf, buf_phys);
  533. return -EAGAIN;
  534. }
  535. memcpy(pad, key, key_len);
  536. memset(pad + key_len, 0, HMAC_PAD_BLOCKLEN - key_len);
  537. for (i = 0; i < HMAC_PAD_BLOCKLEN; i++) {
  538. pad[i] ^= xpad;
  539. }
  540. crypt->data.tfm = tfm;
  541. crypt->regist_ptr = pad;
  542. crypt->regist_buf = buf;
  543. crypt->auth_offs = 0;
  544. crypt->auth_len = HMAC_PAD_BLOCKLEN;
  545. crypt->crypto_ctx = ctx_addr;
  546. crypt->src_buf = buf_phys;
  547. crypt->icv_rev_aes = target;
  548. crypt->mode = NPE_OP_HASH_GEN_ICV;
  549. crypt->init_len = init_len;
  550. crypt->ctl_flags |= CTL_FLAG_GEN_ICV;
  551. buf->next = 0;
  552. buf->buf_len = HMAC_PAD_BLOCKLEN;
  553. buf->pkt_len = 0;
  554. buf->phys_addr = pad_phys;
  555. atomic_inc(&ctx->configuring);
  556. qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
  557. BUG_ON(qmgr_stat_overflow(SEND_QID));
  558. return 0;
  559. }
  560. static int setup_auth(struct crypto_tfm *tfm, int encrypt, unsigned authsize,
  561. const u8 *key, int key_len, unsigned digest_len)
  562. {
  563. u32 itarget, otarget, npe_ctx_addr;
  564. unsigned char *cinfo;
  565. int init_len, ret = 0;
  566. u32 cfgword;
  567. struct ix_sa_dir *dir;
  568. struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  569. const struct ix_hash_algo *algo;
  570. dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
  571. cinfo = dir->npe_ctx + dir->npe_ctx_idx;
  572. algo = ix_hash(tfm);
  573. /* write cfg word to cryptinfo */
  574. cfgword = algo->cfgword | ( authsize << 6); /* (authsize/4) << 8 */
  575. #ifndef __ARMEB__
  576. cfgword ^= 0xAA000000; /* change the "byte swap" flags */
  577. #endif
  578. *(u32*)cinfo = cpu_to_be32(cfgword);
  579. cinfo += sizeof(cfgword);
  580. /* write ICV to cryptinfo */
  581. memcpy(cinfo, algo->icv, digest_len);
  582. cinfo += digest_len;
  583. itarget = dir->npe_ctx_phys + dir->npe_ctx_idx
  584. + sizeof(algo->cfgword);
  585. otarget = itarget + digest_len;
  586. init_len = cinfo - (dir->npe_ctx + dir->npe_ctx_idx);
  587. npe_ctx_addr = dir->npe_ctx_phys + dir->npe_ctx_idx;
  588. dir->npe_ctx_idx += init_len;
  589. dir->npe_mode |= NPE_OP_HASH_ENABLE;
  590. if (!encrypt)
  591. dir->npe_mode |= NPE_OP_HASH_VERIFY;
  592. ret = register_chain_var(tfm, HMAC_OPAD_VALUE, otarget,
  593. init_len, npe_ctx_addr, key, key_len);
  594. if (ret)
  595. return ret;
  596. return register_chain_var(tfm, HMAC_IPAD_VALUE, itarget,
  597. init_len, npe_ctx_addr, key, key_len);
  598. }
  599. static int gen_rev_aes_key(struct crypto_tfm *tfm)
  600. {
  601. struct crypt_ctl *crypt;
  602. struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  603. struct ix_sa_dir *dir = &ctx->decrypt;
  604. crypt = get_crypt_desc_emerg();
  605. if (!crypt) {
  606. return -EAGAIN;
  607. }
  608. *(u32*)dir->npe_ctx |= cpu_to_be32(CIPH_ENCR);
  609. crypt->data.tfm = tfm;
  610. crypt->crypt_offs = 0;
  611. crypt->crypt_len = AES_BLOCK128;
  612. crypt->src_buf = 0;
  613. crypt->crypto_ctx = dir->npe_ctx_phys;
  614. crypt->icv_rev_aes = dir->npe_ctx_phys + sizeof(u32);
  615. crypt->mode = NPE_OP_ENC_GEN_KEY;
  616. crypt->init_len = dir->npe_ctx_idx;
  617. crypt->ctl_flags |= CTL_FLAG_GEN_REVAES;
  618. atomic_inc(&ctx->configuring);
  619. qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
  620. BUG_ON(qmgr_stat_overflow(SEND_QID));
  621. return 0;
  622. }
  623. static int setup_cipher(struct crypto_tfm *tfm, int encrypt,
  624. const u8 *key, int key_len)
  625. {
  626. u8 *cinfo;
  627. u32 cipher_cfg;
  628. u32 keylen_cfg = 0;
  629. struct ix_sa_dir *dir;
  630. struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  631. u32 *flags = &tfm->crt_flags;
  632. dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
  633. cinfo = dir->npe_ctx;
  634. if (encrypt) {
  635. cipher_cfg = cipher_cfg_enc(tfm);
  636. dir->npe_mode |= NPE_OP_CRYPT_ENCRYPT;
  637. } else {
  638. cipher_cfg = cipher_cfg_dec(tfm);
  639. }
  640. if (cipher_cfg & MOD_AES) {
  641. switch (key_len) {
  642. case 16: keylen_cfg = MOD_AES128; break;
  643. case 24: keylen_cfg = MOD_AES192; break;
  644. case 32: keylen_cfg = MOD_AES256; break;
  645. default:
  646. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  647. return -EINVAL;
  648. }
  649. cipher_cfg |= keylen_cfg;
  650. } else if (cipher_cfg & MOD_3DES) {
  651. const u32 *K = (const u32 *)key;
  652. if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
  653. !((K[2] ^ K[4]) | (K[3] ^ K[5]))))
  654. {
  655. *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  656. return -EINVAL;
  657. }
  658. } else {
  659. u32 tmp[DES_EXPKEY_WORDS];
  660. if (des_ekey(tmp, key) == 0) {
  661. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  662. }
  663. }
  664. /* write cfg word to cryptinfo */
  665. *(u32*)cinfo = cpu_to_be32(cipher_cfg);
  666. cinfo += sizeof(cipher_cfg);
  667. /* write cipher key to cryptinfo */
  668. memcpy(cinfo, key, key_len);
  669. /* NPE wants keylen set to DES3_EDE_KEY_SIZE even for single DES */
  670. if (key_len < DES3_EDE_KEY_SIZE && !(cipher_cfg & MOD_AES)) {
  671. memset(cinfo + key_len, 0, DES3_EDE_KEY_SIZE -key_len);
  672. key_len = DES3_EDE_KEY_SIZE;
  673. }
  674. dir->npe_ctx_idx = sizeof(cipher_cfg) + key_len;
  675. dir->npe_mode |= NPE_OP_CRYPT_ENABLE;
  676. if ((cipher_cfg & MOD_AES) && !encrypt) {
  677. return gen_rev_aes_key(tfm);
  678. }
  679. return 0;
  680. }
  681. static struct buffer_desc *chainup_buffers(struct device *dev,
  682. struct scatterlist *sg, unsigned nbytes,
  683. struct buffer_desc *buf, gfp_t flags,
  684. enum dma_data_direction dir)
  685. {
  686. for (;nbytes > 0; sg = scatterwalk_sg_next(sg)) {
  687. unsigned len = min(nbytes, sg->length);
  688. struct buffer_desc *next_buf;
  689. u32 next_buf_phys;
  690. void *ptr;
  691. nbytes -= len;
  692. ptr = page_address(sg_page(sg)) + sg->offset;
  693. next_buf = dma_pool_alloc(buffer_pool, flags, &next_buf_phys);
  694. if (!next_buf) {
  695. buf = NULL;
  696. break;
  697. }
  698. sg_dma_address(sg) = dma_map_single(dev, ptr, len, dir);
  699. buf->next = next_buf;
  700. buf->phys_next = next_buf_phys;
  701. buf = next_buf;
  702. buf->phys_addr = sg_dma_address(sg);
  703. buf->buf_len = len;
  704. buf->dir = dir;
  705. }
  706. buf->next = NULL;
  707. buf->phys_next = 0;
  708. return buf;
  709. }
  710. static int ablk_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  711. unsigned int key_len)
  712. {
  713. struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  714. u32 *flags = &tfm->base.crt_flags;
  715. int ret;
  716. init_completion(&ctx->completion);
  717. atomic_inc(&ctx->configuring);
  718. reset_sa_dir(&ctx->encrypt);
  719. reset_sa_dir(&ctx->decrypt);
  720. ctx->encrypt.npe_mode = NPE_OP_HMAC_DISABLE;
  721. ctx->decrypt.npe_mode = NPE_OP_HMAC_DISABLE;
  722. ret = setup_cipher(&tfm->base, 0, key, key_len);
  723. if (ret)
  724. goto out;
  725. ret = setup_cipher(&tfm->base, 1, key, key_len);
  726. if (ret)
  727. goto out;
  728. if (*flags & CRYPTO_TFM_RES_WEAK_KEY) {
  729. if (*flags & CRYPTO_TFM_REQ_WEAK_KEY) {
  730. ret = -EINVAL;
  731. } else {
  732. *flags &= ~CRYPTO_TFM_RES_WEAK_KEY;
  733. }
  734. }
  735. out:
  736. if (!atomic_dec_and_test(&ctx->configuring))
  737. wait_for_completion(&ctx->completion);
  738. return ret;
  739. }
  740. static int ablk_rfc3686_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  741. unsigned int key_len)
  742. {
  743. struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  744. /* the nonce is stored in bytes at end of key */
  745. if (key_len < CTR_RFC3686_NONCE_SIZE)
  746. return -EINVAL;
  747. memcpy(ctx->nonce, key + (key_len - CTR_RFC3686_NONCE_SIZE),
  748. CTR_RFC3686_NONCE_SIZE);
  749. key_len -= CTR_RFC3686_NONCE_SIZE;
  750. return ablk_setkey(tfm, key, key_len);
  751. }
  752. static int ablk_perform(struct ablkcipher_request *req, int encrypt)
  753. {
  754. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  755. struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  756. unsigned ivsize = crypto_ablkcipher_ivsize(tfm);
  757. struct ix_sa_dir *dir;
  758. struct crypt_ctl *crypt;
  759. unsigned int nbytes = req->nbytes;
  760. enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
  761. struct ablk_ctx *req_ctx = ablkcipher_request_ctx(req);
  762. struct buffer_desc src_hook;
  763. struct device *dev = &pdev->dev;
  764. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  765. GFP_KERNEL : GFP_ATOMIC;
  766. if (qmgr_stat_full(SEND_QID))
  767. return -EAGAIN;
  768. if (atomic_read(&ctx->configuring))
  769. return -EAGAIN;
  770. dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
  771. crypt = get_crypt_desc();
  772. if (!crypt)
  773. return -ENOMEM;
  774. crypt->data.ablk_req = req;
  775. crypt->crypto_ctx = dir->npe_ctx_phys;
  776. crypt->mode = dir->npe_mode;
  777. crypt->init_len = dir->npe_ctx_idx;
  778. crypt->crypt_offs = 0;
  779. crypt->crypt_len = nbytes;
  780. BUG_ON(ivsize && !req->info);
  781. memcpy(crypt->iv, req->info, ivsize);
  782. if (req->src != req->dst) {
  783. struct buffer_desc dst_hook;
  784. crypt->mode |= NPE_OP_NOT_IN_PLACE;
  785. /* This was never tested by Intel
  786. * for more than one dst buffer, I think. */
  787. BUG_ON(req->dst->length < nbytes);
  788. req_ctx->dst = NULL;
  789. if (!chainup_buffers(dev, req->dst, nbytes, &dst_hook,
  790. flags, DMA_FROM_DEVICE))
  791. goto free_buf_dest;
  792. src_direction = DMA_TO_DEVICE;
  793. req_ctx->dst = dst_hook.next;
  794. crypt->dst_buf = dst_hook.phys_next;
  795. } else {
  796. req_ctx->dst = NULL;
  797. }
  798. req_ctx->src = NULL;
  799. if (!chainup_buffers(dev, req->src, nbytes, &src_hook,
  800. flags, src_direction))
  801. goto free_buf_src;
  802. req_ctx->src = src_hook.next;
  803. crypt->src_buf = src_hook.phys_next;
  804. crypt->ctl_flags |= CTL_FLAG_PERFORM_ABLK;
  805. qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
  806. BUG_ON(qmgr_stat_overflow(SEND_QID));
  807. return -EINPROGRESS;
  808. free_buf_src:
  809. free_buf_chain(dev, req_ctx->src, crypt->src_buf);
  810. free_buf_dest:
  811. if (req->src != req->dst) {
  812. free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
  813. }
  814. crypt->ctl_flags = CTL_FLAG_UNUSED;
  815. return -ENOMEM;
  816. }
  817. static int ablk_encrypt(struct ablkcipher_request *req)
  818. {
  819. return ablk_perform(req, 1);
  820. }
  821. static int ablk_decrypt(struct ablkcipher_request *req)
  822. {
  823. return ablk_perform(req, 0);
  824. }
  825. static int ablk_rfc3686_crypt(struct ablkcipher_request *req)
  826. {
  827. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  828. struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  829. u8 iv[CTR_RFC3686_BLOCK_SIZE];
  830. u8 *info = req->info;
  831. int ret;
  832. /* set up counter block */
  833. memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
  834. memcpy(iv + CTR_RFC3686_NONCE_SIZE, info, CTR_RFC3686_IV_SIZE);
  835. /* initialize counter portion of counter block */
  836. *(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
  837. cpu_to_be32(1);
  838. req->info = iv;
  839. ret = ablk_perform(req, 1);
  840. req->info = info;
  841. return ret;
  842. }
  843. static int hmac_inconsistent(struct scatterlist *sg, unsigned start,
  844. unsigned int nbytes)
  845. {
  846. int offset = 0;
  847. if (!nbytes)
  848. return 0;
  849. for (;;) {
  850. if (start < offset + sg->length)
  851. break;
  852. offset += sg->length;
  853. sg = scatterwalk_sg_next(sg);
  854. }
  855. return (start + nbytes > offset + sg->length);
  856. }
  857. static int aead_perform(struct aead_request *req, int encrypt,
  858. int cryptoffset, int eff_cryptlen, u8 *iv)
  859. {
  860. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  861. struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
  862. unsigned ivsize = crypto_aead_ivsize(tfm);
  863. unsigned authsize = crypto_aead_authsize(tfm);
  864. struct ix_sa_dir *dir;
  865. struct crypt_ctl *crypt;
  866. unsigned int cryptlen;
  867. struct buffer_desc *buf, src_hook;
  868. struct aead_ctx *req_ctx = aead_request_ctx(req);
  869. struct device *dev = &pdev->dev;
  870. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  871. GFP_KERNEL : GFP_ATOMIC;
  872. if (qmgr_stat_full(SEND_QID))
  873. return -EAGAIN;
  874. if (atomic_read(&ctx->configuring))
  875. return -EAGAIN;
  876. if (encrypt) {
  877. dir = &ctx->encrypt;
  878. cryptlen = req->cryptlen;
  879. } else {
  880. dir = &ctx->decrypt;
  881. /* req->cryptlen includes the authsize when decrypting */
  882. cryptlen = req->cryptlen -authsize;
  883. eff_cryptlen -= authsize;
  884. }
  885. crypt = get_crypt_desc();
  886. if (!crypt)
  887. return -ENOMEM;
  888. crypt->data.aead_req = req;
  889. crypt->crypto_ctx = dir->npe_ctx_phys;
  890. crypt->mode = dir->npe_mode;
  891. crypt->init_len = dir->npe_ctx_idx;
  892. crypt->crypt_offs = cryptoffset;
  893. crypt->crypt_len = eff_cryptlen;
  894. crypt->auth_offs = 0;
  895. crypt->auth_len = req->assoclen + ivsize + cryptlen;
  896. BUG_ON(ivsize && !req->iv);
  897. memcpy(crypt->iv, req->iv, ivsize);
  898. if (req->src != req->dst) {
  899. BUG(); /* -ENOTSUP because of my laziness */
  900. }
  901. /* ASSOC data */
  902. buf = chainup_buffers(dev, req->assoc, req->assoclen, &src_hook,
  903. flags, DMA_TO_DEVICE);
  904. req_ctx->buffer = src_hook.next;
  905. crypt->src_buf = src_hook.phys_next;
  906. if (!buf)
  907. goto out;
  908. /* IV */
  909. sg_init_table(&req_ctx->ivlist, 1);
  910. sg_set_buf(&req_ctx->ivlist, iv, ivsize);
  911. buf = chainup_buffers(dev, &req_ctx->ivlist, ivsize, buf, flags,
  912. DMA_BIDIRECTIONAL);
  913. if (!buf)
  914. goto free_chain;
  915. if (unlikely(hmac_inconsistent(req->src, cryptlen, authsize))) {
  916. /* The 12 hmac bytes are scattered,
  917. * we need to copy them into a safe buffer */
  918. req_ctx->hmac_virt = dma_pool_alloc(buffer_pool, flags,
  919. &crypt->icv_rev_aes);
  920. if (unlikely(!req_ctx->hmac_virt))
  921. goto free_chain;
  922. if (!encrypt) {
  923. scatterwalk_map_and_copy(req_ctx->hmac_virt,
  924. req->src, cryptlen, authsize, 0);
  925. }
  926. req_ctx->encrypt = encrypt;
  927. } else {
  928. req_ctx->hmac_virt = NULL;
  929. }
  930. /* Crypt */
  931. buf = chainup_buffers(dev, req->src, cryptlen + authsize, buf, flags,
  932. DMA_BIDIRECTIONAL);
  933. if (!buf)
  934. goto free_hmac_virt;
  935. if (!req_ctx->hmac_virt) {
  936. crypt->icv_rev_aes = buf->phys_addr + buf->buf_len - authsize;
  937. }
  938. crypt->ctl_flags |= CTL_FLAG_PERFORM_AEAD;
  939. qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
  940. BUG_ON(qmgr_stat_overflow(SEND_QID));
  941. return -EINPROGRESS;
  942. free_hmac_virt:
  943. if (req_ctx->hmac_virt) {
  944. dma_pool_free(buffer_pool, req_ctx->hmac_virt,
  945. crypt->icv_rev_aes);
  946. }
  947. free_chain:
  948. free_buf_chain(dev, req_ctx->buffer, crypt->src_buf);
  949. out:
  950. crypt->ctl_flags = CTL_FLAG_UNUSED;
  951. return -ENOMEM;
  952. }
  953. static int aead_setup(struct crypto_aead *tfm, unsigned int authsize)
  954. {
  955. struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
  956. u32 *flags = &tfm->base.crt_flags;
  957. unsigned digest_len = crypto_aead_alg(tfm)->maxauthsize;
  958. int ret;
  959. if (!ctx->enckey_len && !ctx->authkey_len)
  960. return 0;
  961. init_completion(&ctx->completion);
  962. atomic_inc(&ctx->configuring);
  963. reset_sa_dir(&ctx->encrypt);
  964. reset_sa_dir(&ctx->decrypt);
  965. ret = setup_cipher(&tfm->base, 0, ctx->enckey, ctx->enckey_len);
  966. if (ret)
  967. goto out;
  968. ret = setup_cipher(&tfm->base, 1, ctx->enckey, ctx->enckey_len);
  969. if (ret)
  970. goto out;
  971. ret = setup_auth(&tfm->base, 0, authsize, ctx->authkey,
  972. ctx->authkey_len, digest_len);
  973. if (ret)
  974. goto out;
  975. ret = setup_auth(&tfm->base, 1, authsize, ctx->authkey,
  976. ctx->authkey_len, digest_len);
  977. if (ret)
  978. goto out;
  979. if (*flags & CRYPTO_TFM_RES_WEAK_KEY) {
  980. if (*flags & CRYPTO_TFM_REQ_WEAK_KEY) {
  981. ret = -EINVAL;
  982. goto out;
  983. } else {
  984. *flags &= ~CRYPTO_TFM_RES_WEAK_KEY;
  985. }
  986. }
  987. out:
  988. if (!atomic_dec_and_test(&ctx->configuring))
  989. wait_for_completion(&ctx->completion);
  990. return ret;
  991. }
  992. static int aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  993. {
  994. int max = crypto_aead_alg(tfm)->maxauthsize >> 2;
  995. if ((authsize>>2) < 1 || (authsize>>2) > max || (authsize & 3))
  996. return -EINVAL;
  997. return aead_setup(tfm, authsize);
  998. }
  999. static int aead_setkey(struct crypto_aead *tfm, const u8 *key,
  1000. unsigned int keylen)
  1001. {
  1002. struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
  1003. struct crypto_authenc_keys keys;
  1004. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  1005. goto badkey;
  1006. if (keys.authkeylen > sizeof(ctx->authkey))
  1007. goto badkey;
  1008. if (keys.enckeylen > sizeof(ctx->enckey))
  1009. goto badkey;
  1010. memcpy(ctx->authkey, keys.authkey, keys.authkeylen);
  1011. memcpy(ctx->enckey, keys.enckey, keys.enckeylen);
  1012. ctx->authkey_len = keys.authkeylen;
  1013. ctx->enckey_len = keys.enckeylen;
  1014. return aead_setup(tfm, crypto_aead_authsize(tfm));
  1015. badkey:
  1016. crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1017. return -EINVAL;
  1018. }
  1019. static int aead_encrypt(struct aead_request *req)
  1020. {
  1021. unsigned ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(req));
  1022. return aead_perform(req, 1, req->assoclen + ivsize,
  1023. req->cryptlen, req->iv);
  1024. }
  1025. static int aead_decrypt(struct aead_request *req)
  1026. {
  1027. unsigned ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(req));
  1028. return aead_perform(req, 0, req->assoclen + ivsize,
  1029. req->cryptlen, req->iv);
  1030. }
  1031. static int aead_givencrypt(struct aead_givcrypt_request *req)
  1032. {
  1033. struct crypto_aead *tfm = aead_givcrypt_reqtfm(req);
  1034. struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
  1035. unsigned len, ivsize = crypto_aead_ivsize(tfm);
  1036. __be64 seq;
  1037. /* copied from eseqiv.c */
  1038. if (!ctx->salted) {
  1039. get_random_bytes(ctx->salt, ivsize);
  1040. ctx->salted = 1;
  1041. }
  1042. memcpy(req->areq.iv, ctx->salt, ivsize);
  1043. len = ivsize;
  1044. if (ivsize > sizeof(u64)) {
  1045. memset(req->giv, 0, ivsize - sizeof(u64));
  1046. len = sizeof(u64);
  1047. }
  1048. seq = cpu_to_be64(req->seq);
  1049. memcpy(req->giv + ivsize - len, &seq, len);
  1050. return aead_perform(&req->areq, 1, req->areq.assoclen,
  1051. req->areq.cryptlen +ivsize, req->giv);
  1052. }
  1053. static struct ixp_alg ixp4xx_algos[] = {
  1054. {
  1055. .crypto = {
  1056. .cra_name = "cbc(des)",
  1057. .cra_blocksize = DES_BLOCK_SIZE,
  1058. .cra_u = { .ablkcipher = {
  1059. .min_keysize = DES_KEY_SIZE,
  1060. .max_keysize = DES_KEY_SIZE,
  1061. .ivsize = DES_BLOCK_SIZE,
  1062. .geniv = "eseqiv",
  1063. }
  1064. }
  1065. },
  1066. .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
  1067. .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
  1068. }, {
  1069. .crypto = {
  1070. .cra_name = "ecb(des)",
  1071. .cra_blocksize = DES_BLOCK_SIZE,
  1072. .cra_u = { .ablkcipher = {
  1073. .min_keysize = DES_KEY_SIZE,
  1074. .max_keysize = DES_KEY_SIZE,
  1075. }
  1076. }
  1077. },
  1078. .cfg_enc = CIPH_ENCR | MOD_DES | MOD_ECB | KEYLEN_192,
  1079. .cfg_dec = CIPH_DECR | MOD_DES | MOD_ECB | KEYLEN_192,
  1080. }, {
  1081. .crypto = {
  1082. .cra_name = "cbc(des3_ede)",
  1083. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1084. .cra_u = { .ablkcipher = {
  1085. .min_keysize = DES3_EDE_KEY_SIZE,
  1086. .max_keysize = DES3_EDE_KEY_SIZE,
  1087. .ivsize = DES3_EDE_BLOCK_SIZE,
  1088. .geniv = "eseqiv",
  1089. }
  1090. }
  1091. },
  1092. .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
  1093. .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
  1094. }, {
  1095. .crypto = {
  1096. .cra_name = "ecb(des3_ede)",
  1097. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1098. .cra_u = { .ablkcipher = {
  1099. .min_keysize = DES3_EDE_KEY_SIZE,
  1100. .max_keysize = DES3_EDE_KEY_SIZE,
  1101. }
  1102. }
  1103. },
  1104. .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_ECB | KEYLEN_192,
  1105. .cfg_dec = CIPH_DECR | MOD_3DES | MOD_ECB | KEYLEN_192,
  1106. }, {
  1107. .crypto = {
  1108. .cra_name = "cbc(aes)",
  1109. .cra_blocksize = AES_BLOCK_SIZE,
  1110. .cra_u = { .ablkcipher = {
  1111. .min_keysize = AES_MIN_KEY_SIZE,
  1112. .max_keysize = AES_MAX_KEY_SIZE,
  1113. .ivsize = AES_BLOCK_SIZE,
  1114. .geniv = "eseqiv",
  1115. }
  1116. }
  1117. },
  1118. .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
  1119. .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
  1120. }, {
  1121. .crypto = {
  1122. .cra_name = "ecb(aes)",
  1123. .cra_blocksize = AES_BLOCK_SIZE,
  1124. .cra_u = { .ablkcipher = {
  1125. .min_keysize = AES_MIN_KEY_SIZE,
  1126. .max_keysize = AES_MAX_KEY_SIZE,
  1127. }
  1128. }
  1129. },
  1130. .cfg_enc = CIPH_ENCR | MOD_AES | MOD_ECB,
  1131. .cfg_dec = CIPH_DECR | MOD_AES | MOD_ECB,
  1132. }, {
  1133. .crypto = {
  1134. .cra_name = "ctr(aes)",
  1135. .cra_blocksize = AES_BLOCK_SIZE,
  1136. .cra_u = { .ablkcipher = {
  1137. .min_keysize = AES_MIN_KEY_SIZE,
  1138. .max_keysize = AES_MAX_KEY_SIZE,
  1139. .ivsize = AES_BLOCK_SIZE,
  1140. .geniv = "eseqiv",
  1141. }
  1142. }
  1143. },
  1144. .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
  1145. .cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
  1146. }, {
  1147. .crypto = {
  1148. .cra_name = "rfc3686(ctr(aes))",
  1149. .cra_blocksize = AES_BLOCK_SIZE,
  1150. .cra_u = { .ablkcipher = {
  1151. .min_keysize = AES_MIN_KEY_SIZE,
  1152. .max_keysize = AES_MAX_KEY_SIZE,
  1153. .ivsize = AES_BLOCK_SIZE,
  1154. .geniv = "eseqiv",
  1155. .setkey = ablk_rfc3686_setkey,
  1156. .encrypt = ablk_rfc3686_crypt,
  1157. .decrypt = ablk_rfc3686_crypt }
  1158. }
  1159. },
  1160. .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
  1161. .cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
  1162. }, {
  1163. .crypto = {
  1164. .cra_name = "authenc(hmac(md5),cbc(des))",
  1165. .cra_blocksize = DES_BLOCK_SIZE,
  1166. .cra_u = { .aead = {
  1167. .ivsize = DES_BLOCK_SIZE,
  1168. .maxauthsize = MD5_DIGEST_SIZE,
  1169. }
  1170. }
  1171. },
  1172. .hash = &hash_alg_md5,
  1173. .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
  1174. .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
  1175. }, {
  1176. .crypto = {
  1177. .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
  1178. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1179. .cra_u = { .aead = {
  1180. .ivsize = DES3_EDE_BLOCK_SIZE,
  1181. .maxauthsize = MD5_DIGEST_SIZE,
  1182. }
  1183. }
  1184. },
  1185. .hash = &hash_alg_md5,
  1186. .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
  1187. .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
  1188. }, {
  1189. .crypto = {
  1190. .cra_name = "authenc(hmac(sha1),cbc(des))",
  1191. .cra_blocksize = DES_BLOCK_SIZE,
  1192. .cra_u = { .aead = {
  1193. .ivsize = DES_BLOCK_SIZE,
  1194. .maxauthsize = SHA1_DIGEST_SIZE,
  1195. }
  1196. }
  1197. },
  1198. .hash = &hash_alg_sha1,
  1199. .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
  1200. .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
  1201. }, {
  1202. .crypto = {
  1203. .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
  1204. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1205. .cra_u = { .aead = {
  1206. .ivsize = DES3_EDE_BLOCK_SIZE,
  1207. .maxauthsize = SHA1_DIGEST_SIZE,
  1208. }
  1209. }
  1210. },
  1211. .hash = &hash_alg_sha1,
  1212. .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
  1213. .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
  1214. }, {
  1215. .crypto = {
  1216. .cra_name = "authenc(hmac(md5),cbc(aes))",
  1217. .cra_blocksize = AES_BLOCK_SIZE,
  1218. .cra_u = { .aead = {
  1219. .ivsize = AES_BLOCK_SIZE,
  1220. .maxauthsize = MD5_DIGEST_SIZE,
  1221. }
  1222. }
  1223. },
  1224. .hash = &hash_alg_md5,
  1225. .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
  1226. .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
  1227. }, {
  1228. .crypto = {
  1229. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  1230. .cra_blocksize = AES_BLOCK_SIZE,
  1231. .cra_u = { .aead = {
  1232. .ivsize = AES_BLOCK_SIZE,
  1233. .maxauthsize = SHA1_DIGEST_SIZE,
  1234. }
  1235. }
  1236. },
  1237. .hash = &hash_alg_sha1,
  1238. .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
  1239. .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
  1240. } };
  1241. #define IXP_POSTFIX "-ixp4xx"
  1242. static const struct platform_device_info ixp_dev_info __initdata = {
  1243. .name = DRIVER_NAME,
  1244. .id = 0,
  1245. .dma_mask = DMA_BIT_MASK(32),
  1246. };
  1247. static int __init ixp_module_init(void)
  1248. {
  1249. int num = ARRAY_SIZE(ixp4xx_algos);
  1250. int i, err;
  1251. pdev = platform_device_register_full(&ixp_dev_info);
  1252. if (IS_ERR(pdev))
  1253. return PTR_ERR(pdev);
  1254. spin_lock_init(&desc_lock);
  1255. spin_lock_init(&emerg_lock);
  1256. err = init_ixp_crypto(&pdev->dev);
  1257. if (err) {
  1258. platform_device_unregister(pdev);
  1259. return err;
  1260. }
  1261. for (i=0; i< num; i++) {
  1262. struct crypto_alg *cra = &ixp4xx_algos[i].crypto;
  1263. if (snprintf(cra->cra_driver_name, CRYPTO_MAX_ALG_NAME,
  1264. "%s"IXP_POSTFIX, cra->cra_name) >=
  1265. CRYPTO_MAX_ALG_NAME)
  1266. {
  1267. continue;
  1268. }
  1269. if (!support_aes && (ixp4xx_algos[i].cfg_enc & MOD_AES)) {
  1270. continue;
  1271. }
  1272. if (!ixp4xx_algos[i].hash) {
  1273. /* block ciphers */
  1274. cra->cra_type = &crypto_ablkcipher_type;
  1275. cra->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1276. CRYPTO_ALG_KERN_DRIVER_ONLY |
  1277. CRYPTO_ALG_ASYNC;
  1278. if (!cra->cra_ablkcipher.setkey)
  1279. cra->cra_ablkcipher.setkey = ablk_setkey;
  1280. if (!cra->cra_ablkcipher.encrypt)
  1281. cra->cra_ablkcipher.encrypt = ablk_encrypt;
  1282. if (!cra->cra_ablkcipher.decrypt)
  1283. cra->cra_ablkcipher.decrypt = ablk_decrypt;
  1284. cra->cra_init = init_tfm_ablk;
  1285. } else {
  1286. /* authenc */
  1287. cra->cra_type = &crypto_aead_type;
  1288. cra->cra_flags = CRYPTO_ALG_TYPE_AEAD |
  1289. CRYPTO_ALG_KERN_DRIVER_ONLY |
  1290. CRYPTO_ALG_ASYNC;
  1291. cra->cra_aead.setkey = aead_setkey;
  1292. cra->cra_aead.setauthsize = aead_setauthsize;
  1293. cra->cra_aead.encrypt = aead_encrypt;
  1294. cra->cra_aead.decrypt = aead_decrypt;
  1295. cra->cra_aead.givencrypt = aead_givencrypt;
  1296. cra->cra_init = init_tfm_aead;
  1297. }
  1298. cra->cra_ctxsize = sizeof(struct ixp_ctx);
  1299. cra->cra_module = THIS_MODULE;
  1300. cra->cra_alignmask = 3;
  1301. cra->cra_priority = 300;
  1302. cra->cra_exit = exit_tfm;
  1303. if (crypto_register_alg(cra))
  1304. printk(KERN_ERR "Failed to register '%s'\n",
  1305. cra->cra_name);
  1306. else
  1307. ixp4xx_algos[i].registered = 1;
  1308. }
  1309. return 0;
  1310. }
  1311. static void __exit ixp_module_exit(void)
  1312. {
  1313. int num = ARRAY_SIZE(ixp4xx_algos);
  1314. int i;
  1315. for (i=0; i< num; i++) {
  1316. if (ixp4xx_algos[i].registered)
  1317. crypto_unregister_alg(&ixp4xx_algos[i].crypto);
  1318. }
  1319. release_ixp_crypto(&pdev->dev);
  1320. platform_device_unregister(pdev);
  1321. }
  1322. module_init(ixp_module_init);
  1323. module_exit(ixp_module_exit);
  1324. MODULE_LICENSE("GPL");
  1325. MODULE_AUTHOR("Christian Hohnstaedt <chohnstaedt@innominate.com>");
  1326. MODULE_DESCRIPTION("IXP4xx hardware crypto");