ixp4xx_crypto.c 36 KB

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