ixp4xx_crypto.c 37 KB

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