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