ixp4xx_crypto.c 37 KB

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