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