ixp4xx_crypto.c 36 KB

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