ixp4xx_crypto.c 36 KB

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