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_alloc_coherent(dev,
  224. NPE_QLEN * sizeof(struct crypt_ctl),
  225. &crypt_phys, GFP_ATOMIC);
  226. if (!crypt_virt)
  227. return -ENOMEM;
  228. memset(crypt_virt, 0, NPE_QLEN * sizeof(struct crypt_ctl));
  229. return 0;
  230. }
  231. static spinlock_t desc_lock;
  232. static struct crypt_ctl *get_crypt_desc(void)
  233. {
  234. int i;
  235. static int idx = 0;
  236. unsigned long flags;
  237. spin_lock_irqsave(&desc_lock, flags);
  238. if (unlikely(!crypt_virt))
  239. setup_crypt_desc();
  240. if (unlikely(!crypt_virt)) {
  241. spin_unlock_irqrestore(&desc_lock, flags);
  242. return NULL;
  243. }
  244. i = idx;
  245. if (crypt_virt[i].ctl_flags == CTL_FLAG_UNUSED) {
  246. if (++idx >= NPE_QLEN)
  247. idx = 0;
  248. crypt_virt[i].ctl_flags = CTL_FLAG_USED;
  249. spin_unlock_irqrestore(&desc_lock, flags);
  250. return crypt_virt +i;
  251. } else {
  252. spin_unlock_irqrestore(&desc_lock, flags);
  253. return NULL;
  254. }
  255. }
  256. static spinlock_t emerg_lock;
  257. static struct crypt_ctl *get_crypt_desc_emerg(void)
  258. {
  259. int i;
  260. static int idx = NPE_QLEN;
  261. struct crypt_ctl *desc;
  262. unsigned long flags;
  263. desc = get_crypt_desc();
  264. if (desc)
  265. return desc;
  266. if (unlikely(!crypt_virt))
  267. return NULL;
  268. spin_lock_irqsave(&emerg_lock, flags);
  269. i = idx;
  270. if (crypt_virt[i].ctl_flags == CTL_FLAG_UNUSED) {
  271. if (++idx >= NPE_QLEN_TOTAL)
  272. idx = NPE_QLEN;
  273. crypt_virt[i].ctl_flags = CTL_FLAG_USED;
  274. spin_unlock_irqrestore(&emerg_lock, flags);
  275. return crypt_virt +i;
  276. } else {
  277. spin_unlock_irqrestore(&emerg_lock, flags);
  278. return NULL;
  279. }
  280. }
  281. static void free_buf_chain(struct device *dev, struct buffer_desc *buf,u32 phys)
  282. {
  283. while (buf) {
  284. struct buffer_desc *buf1;
  285. u32 phys1;
  286. buf1 = buf->next;
  287. phys1 = buf->phys_next;
  288. dma_unmap_single(dev, buf->phys_addr, buf->buf_len, buf->dir);
  289. dma_pool_free(buffer_pool, buf, phys);
  290. buf = buf1;
  291. phys = phys1;
  292. }
  293. }
  294. static struct tasklet_struct crypto_done_tasklet;
  295. static void finish_scattered_hmac(struct crypt_ctl *crypt)
  296. {
  297. struct aead_request *req = crypt->data.aead_req;
  298. struct aead_ctx *req_ctx = aead_request_ctx(req);
  299. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  300. int authsize = crypto_aead_authsize(tfm);
  301. int decryptlen = req->assoclen + req->cryptlen - authsize;
  302. if (req_ctx->encrypt) {
  303. scatterwalk_map_and_copy(req_ctx->hmac_virt,
  304. req->dst, decryptlen, authsize, 1);
  305. }
  306. dma_pool_free(buffer_pool, req_ctx->hmac_virt, crypt->icv_rev_aes);
  307. }
  308. static void one_packet(dma_addr_t phys)
  309. {
  310. struct device *dev = &pdev->dev;
  311. struct crypt_ctl *crypt;
  312. struct ixp_ctx *ctx;
  313. int failed;
  314. failed = phys & 0x1 ? -EBADMSG : 0;
  315. phys &= ~0x3;
  316. crypt = crypt_phys2virt(phys);
  317. switch (crypt->ctl_flags & CTL_FLAG_MASK) {
  318. case CTL_FLAG_PERFORM_AEAD: {
  319. struct aead_request *req = crypt->data.aead_req;
  320. struct aead_ctx *req_ctx = aead_request_ctx(req);
  321. free_buf_chain(dev, req_ctx->src, crypt->src_buf);
  322. free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
  323. if (req_ctx->hmac_virt) {
  324. finish_scattered_hmac(crypt);
  325. }
  326. req->base.complete(&req->base, failed);
  327. break;
  328. }
  329. case CTL_FLAG_PERFORM_ABLK: {
  330. struct ablkcipher_request *req = crypt->data.ablk_req;
  331. struct ablk_ctx *req_ctx = ablkcipher_request_ctx(req);
  332. if (req_ctx->dst) {
  333. free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
  334. }
  335. free_buf_chain(dev, req_ctx->src, crypt->src_buf);
  336. req->base.complete(&req->base, failed);
  337. break;
  338. }
  339. case CTL_FLAG_GEN_ICV:
  340. ctx = crypto_tfm_ctx(crypt->data.tfm);
  341. dma_pool_free(ctx_pool, crypt->regist_ptr,
  342. crypt->regist_buf->phys_addr);
  343. dma_pool_free(buffer_pool, crypt->regist_buf, crypt->src_buf);
  344. if (atomic_dec_and_test(&ctx->configuring))
  345. complete(&ctx->completion);
  346. break;
  347. case CTL_FLAG_GEN_REVAES:
  348. ctx = crypto_tfm_ctx(crypt->data.tfm);
  349. *(u32*)ctx->decrypt.npe_ctx &= cpu_to_be32(~CIPH_ENCR);
  350. if (atomic_dec_and_test(&ctx->configuring))
  351. complete(&ctx->completion);
  352. break;
  353. default:
  354. BUG();
  355. }
  356. crypt->ctl_flags = CTL_FLAG_UNUSED;
  357. }
  358. static void irqhandler(void *_unused)
  359. {
  360. tasklet_schedule(&crypto_done_tasklet);
  361. }
  362. static void crypto_done_action(unsigned long arg)
  363. {
  364. int i;
  365. for(i=0; i<4; i++) {
  366. dma_addr_t phys = qmgr_get_entry(RECV_QID);
  367. if (!phys)
  368. return;
  369. one_packet(phys);
  370. }
  371. tasklet_schedule(&crypto_done_tasklet);
  372. }
  373. static int init_ixp_crypto(struct device *dev)
  374. {
  375. int ret = -ENODEV;
  376. u32 msg[2] = { 0, 0 };
  377. if (! ( ~(*IXP4XX_EXP_CFG2) & (IXP4XX_FEATURE_HASH |
  378. IXP4XX_FEATURE_AES | IXP4XX_FEATURE_DES))) {
  379. printk(KERN_ERR "ixp_crypto: No HW crypto available\n");
  380. return ret;
  381. }
  382. npe_c = npe_request(NPE_ID);
  383. if (!npe_c)
  384. return ret;
  385. if (!npe_running(npe_c)) {
  386. ret = npe_load_firmware(npe_c, npe_name(npe_c), dev);
  387. if (ret)
  388. goto npe_release;
  389. if (npe_recv_message(npe_c, msg, "STATUS_MSG"))
  390. goto npe_error;
  391. } else {
  392. if (npe_send_message(npe_c, msg, "STATUS_MSG"))
  393. goto npe_error;
  394. if (npe_recv_message(npe_c, msg, "STATUS_MSG"))
  395. goto npe_error;
  396. }
  397. switch ((msg[1]>>16) & 0xff) {
  398. case 3:
  399. printk(KERN_WARNING "Firmware of %s lacks AES support\n",
  400. npe_name(npe_c));
  401. support_aes = 0;
  402. break;
  403. case 4:
  404. case 5:
  405. support_aes = 1;
  406. break;
  407. default:
  408. printk(KERN_ERR "Firmware of %s lacks crypto support\n",
  409. npe_name(npe_c));
  410. ret = -ENODEV;
  411. goto npe_release;
  412. }
  413. /* buffer_pool will also be used to sometimes store the hmac,
  414. * so assure it is large enough
  415. */
  416. BUILD_BUG_ON(SHA1_DIGEST_SIZE > sizeof(struct buffer_desc));
  417. buffer_pool = dma_pool_create("buffer", dev,
  418. sizeof(struct buffer_desc), 32, 0);
  419. ret = -ENOMEM;
  420. if (!buffer_pool) {
  421. goto err;
  422. }
  423. ctx_pool = dma_pool_create("context", dev,
  424. NPE_CTX_LEN, 16, 0);
  425. if (!ctx_pool) {
  426. goto err;
  427. }
  428. ret = qmgr_request_queue(SEND_QID, NPE_QLEN_TOTAL, 0, 0,
  429. "ixp_crypto:out", NULL);
  430. if (ret)
  431. goto err;
  432. ret = qmgr_request_queue(RECV_QID, NPE_QLEN, 0, 0,
  433. "ixp_crypto:in", NULL);
  434. if (ret) {
  435. qmgr_release_queue(SEND_QID);
  436. goto err;
  437. }
  438. qmgr_set_irq(RECV_QID, QUEUE_IRQ_SRC_NOT_EMPTY, irqhandler, NULL);
  439. tasklet_init(&crypto_done_tasklet, crypto_done_action, 0);
  440. qmgr_enable_irq(RECV_QID);
  441. return 0;
  442. npe_error:
  443. printk(KERN_ERR "%s not responding\n", npe_name(npe_c));
  444. ret = -EIO;
  445. err:
  446. dma_pool_destroy(ctx_pool);
  447. dma_pool_destroy(buffer_pool);
  448. npe_release:
  449. npe_release(npe_c);
  450. return ret;
  451. }
  452. static void release_ixp_crypto(struct device *dev)
  453. {
  454. qmgr_disable_irq(RECV_QID);
  455. tasklet_kill(&crypto_done_tasklet);
  456. qmgr_release_queue(SEND_QID);
  457. qmgr_release_queue(RECV_QID);
  458. dma_pool_destroy(ctx_pool);
  459. dma_pool_destroy(buffer_pool);
  460. npe_release(npe_c);
  461. if (crypt_virt) {
  462. dma_free_coherent(dev,
  463. NPE_QLEN * sizeof(struct crypt_ctl),
  464. crypt_virt, crypt_phys);
  465. }
  466. return;
  467. }
  468. static void reset_sa_dir(struct ix_sa_dir *dir)
  469. {
  470. memset(dir->npe_ctx, 0, NPE_CTX_LEN);
  471. dir->npe_ctx_idx = 0;
  472. dir->npe_mode = 0;
  473. }
  474. static int init_sa_dir(struct ix_sa_dir *dir)
  475. {
  476. dir->npe_ctx = dma_pool_alloc(ctx_pool, GFP_KERNEL, &dir->npe_ctx_phys);
  477. if (!dir->npe_ctx) {
  478. return -ENOMEM;
  479. }
  480. reset_sa_dir(dir);
  481. return 0;
  482. }
  483. static void free_sa_dir(struct ix_sa_dir *dir)
  484. {
  485. memset(dir->npe_ctx, 0, NPE_CTX_LEN);
  486. dma_pool_free(ctx_pool, dir->npe_ctx, dir->npe_ctx_phys);
  487. }
  488. static int init_tfm(struct crypto_tfm *tfm)
  489. {
  490. struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  491. int ret;
  492. atomic_set(&ctx->configuring, 0);
  493. ret = init_sa_dir(&ctx->encrypt);
  494. if (ret)
  495. return ret;
  496. ret = init_sa_dir(&ctx->decrypt);
  497. if (ret) {
  498. free_sa_dir(&ctx->encrypt);
  499. }
  500. return ret;
  501. }
  502. static int init_tfm_ablk(struct crypto_tfm *tfm)
  503. {
  504. tfm->crt_ablkcipher.reqsize = sizeof(struct ablk_ctx);
  505. return init_tfm(tfm);
  506. }
  507. static int init_tfm_aead(struct crypto_aead *tfm)
  508. {
  509. crypto_aead_set_reqsize(tfm, sizeof(struct aead_ctx));
  510. return init_tfm(crypto_aead_tfm(tfm));
  511. }
  512. static void exit_tfm(struct crypto_tfm *tfm)
  513. {
  514. struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  515. free_sa_dir(&ctx->encrypt);
  516. free_sa_dir(&ctx->decrypt);
  517. }
  518. static void exit_tfm_aead(struct crypto_aead *tfm)
  519. {
  520. exit_tfm(crypto_aead_tfm(tfm));
  521. }
  522. static int register_chain_var(struct crypto_tfm *tfm, u8 xpad, u32 target,
  523. int init_len, u32 ctx_addr, const u8 *key, int key_len)
  524. {
  525. struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  526. struct crypt_ctl *crypt;
  527. struct buffer_desc *buf;
  528. int i;
  529. u8 *pad;
  530. u32 pad_phys, buf_phys;
  531. BUILD_BUG_ON(NPE_CTX_LEN < HMAC_PAD_BLOCKLEN);
  532. pad = dma_pool_alloc(ctx_pool, GFP_KERNEL, &pad_phys);
  533. if (!pad)
  534. return -ENOMEM;
  535. buf = dma_pool_alloc(buffer_pool, GFP_KERNEL, &buf_phys);
  536. if (!buf) {
  537. dma_pool_free(ctx_pool, pad, pad_phys);
  538. return -ENOMEM;
  539. }
  540. crypt = get_crypt_desc_emerg();
  541. if (!crypt) {
  542. dma_pool_free(ctx_pool, pad, pad_phys);
  543. dma_pool_free(buffer_pool, buf, buf_phys);
  544. return -EAGAIN;
  545. }
  546. memcpy(pad, key, key_len);
  547. memset(pad + key_len, 0, HMAC_PAD_BLOCKLEN - key_len);
  548. for (i = 0; i < HMAC_PAD_BLOCKLEN; i++) {
  549. pad[i] ^= xpad;
  550. }
  551. crypt->data.tfm = tfm;
  552. crypt->regist_ptr = pad;
  553. crypt->regist_buf = buf;
  554. crypt->auth_offs = 0;
  555. crypt->auth_len = HMAC_PAD_BLOCKLEN;
  556. crypt->crypto_ctx = ctx_addr;
  557. crypt->src_buf = buf_phys;
  558. crypt->icv_rev_aes = target;
  559. crypt->mode = NPE_OP_HASH_GEN_ICV;
  560. crypt->init_len = init_len;
  561. crypt->ctl_flags |= CTL_FLAG_GEN_ICV;
  562. buf->next = 0;
  563. buf->buf_len = HMAC_PAD_BLOCKLEN;
  564. buf->pkt_len = 0;
  565. buf->phys_addr = pad_phys;
  566. atomic_inc(&ctx->configuring);
  567. qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
  568. BUG_ON(qmgr_stat_overflow(SEND_QID));
  569. return 0;
  570. }
  571. static int setup_auth(struct crypto_tfm *tfm, int encrypt, unsigned authsize,
  572. const u8 *key, int key_len, unsigned digest_len)
  573. {
  574. u32 itarget, otarget, npe_ctx_addr;
  575. unsigned char *cinfo;
  576. int init_len, ret = 0;
  577. u32 cfgword;
  578. struct ix_sa_dir *dir;
  579. struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  580. const struct ix_hash_algo *algo;
  581. dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
  582. cinfo = dir->npe_ctx + dir->npe_ctx_idx;
  583. algo = ix_hash(tfm);
  584. /* write cfg word to cryptinfo */
  585. cfgword = algo->cfgword | ( authsize << 6); /* (authsize/4) << 8 */
  586. #ifndef __ARMEB__
  587. cfgword ^= 0xAA000000; /* change the "byte swap" flags */
  588. #endif
  589. *(u32*)cinfo = cpu_to_be32(cfgword);
  590. cinfo += sizeof(cfgword);
  591. /* write ICV to cryptinfo */
  592. memcpy(cinfo, algo->icv, digest_len);
  593. cinfo += digest_len;
  594. itarget = dir->npe_ctx_phys + dir->npe_ctx_idx
  595. + sizeof(algo->cfgword);
  596. otarget = itarget + digest_len;
  597. init_len = cinfo - (dir->npe_ctx + dir->npe_ctx_idx);
  598. npe_ctx_addr = dir->npe_ctx_phys + dir->npe_ctx_idx;
  599. dir->npe_ctx_idx += init_len;
  600. dir->npe_mode |= NPE_OP_HASH_ENABLE;
  601. if (!encrypt)
  602. dir->npe_mode |= NPE_OP_HASH_VERIFY;
  603. ret = register_chain_var(tfm, HMAC_OPAD_VALUE, otarget,
  604. init_len, npe_ctx_addr, key, key_len);
  605. if (ret)
  606. return ret;
  607. return register_chain_var(tfm, HMAC_IPAD_VALUE, itarget,
  608. init_len, npe_ctx_addr, key, key_len);
  609. }
  610. static int gen_rev_aes_key(struct crypto_tfm *tfm)
  611. {
  612. struct crypt_ctl *crypt;
  613. struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  614. struct ix_sa_dir *dir = &ctx->decrypt;
  615. crypt = get_crypt_desc_emerg();
  616. if (!crypt) {
  617. return -EAGAIN;
  618. }
  619. *(u32*)dir->npe_ctx |= cpu_to_be32(CIPH_ENCR);
  620. crypt->data.tfm = tfm;
  621. crypt->crypt_offs = 0;
  622. crypt->crypt_len = AES_BLOCK128;
  623. crypt->src_buf = 0;
  624. crypt->crypto_ctx = dir->npe_ctx_phys;
  625. crypt->icv_rev_aes = dir->npe_ctx_phys + sizeof(u32);
  626. crypt->mode = NPE_OP_ENC_GEN_KEY;
  627. crypt->init_len = dir->npe_ctx_idx;
  628. crypt->ctl_flags |= CTL_FLAG_GEN_REVAES;
  629. atomic_inc(&ctx->configuring);
  630. qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
  631. BUG_ON(qmgr_stat_overflow(SEND_QID));
  632. return 0;
  633. }
  634. static int setup_cipher(struct crypto_tfm *tfm, int encrypt,
  635. const u8 *key, int key_len)
  636. {
  637. u8 *cinfo;
  638. u32 cipher_cfg;
  639. u32 keylen_cfg = 0;
  640. struct ix_sa_dir *dir;
  641. struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  642. u32 *flags = &tfm->crt_flags;
  643. dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
  644. cinfo = dir->npe_ctx;
  645. if (encrypt) {
  646. cipher_cfg = cipher_cfg_enc(tfm);
  647. dir->npe_mode |= NPE_OP_CRYPT_ENCRYPT;
  648. } else {
  649. cipher_cfg = cipher_cfg_dec(tfm);
  650. }
  651. if (cipher_cfg & MOD_AES) {
  652. switch (key_len) {
  653. case 16: keylen_cfg = MOD_AES128; break;
  654. case 24: keylen_cfg = MOD_AES192; break;
  655. case 32: keylen_cfg = MOD_AES256; break;
  656. default:
  657. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  658. return -EINVAL;
  659. }
  660. cipher_cfg |= keylen_cfg;
  661. } else if (cipher_cfg & MOD_3DES) {
  662. const u32 *K = (const u32 *)key;
  663. if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
  664. !((K[2] ^ K[4]) | (K[3] ^ K[5]))))
  665. {
  666. *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  667. return -EINVAL;
  668. }
  669. } else {
  670. u32 tmp[DES_EXPKEY_WORDS];
  671. if (des_ekey(tmp, key) == 0) {
  672. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  673. }
  674. }
  675. /* write cfg word to cryptinfo */
  676. *(u32*)cinfo = cpu_to_be32(cipher_cfg);
  677. cinfo += sizeof(cipher_cfg);
  678. /* write cipher key to cryptinfo */
  679. memcpy(cinfo, key, key_len);
  680. /* NPE wants keylen set to DES3_EDE_KEY_SIZE even for single DES */
  681. if (key_len < DES3_EDE_KEY_SIZE && !(cipher_cfg & MOD_AES)) {
  682. memset(cinfo + key_len, 0, DES3_EDE_KEY_SIZE -key_len);
  683. key_len = DES3_EDE_KEY_SIZE;
  684. }
  685. dir->npe_ctx_idx = sizeof(cipher_cfg) + key_len;
  686. dir->npe_mode |= NPE_OP_CRYPT_ENABLE;
  687. if ((cipher_cfg & MOD_AES) && !encrypt) {
  688. return gen_rev_aes_key(tfm);
  689. }
  690. return 0;
  691. }
  692. static struct buffer_desc *chainup_buffers(struct device *dev,
  693. struct scatterlist *sg, unsigned nbytes,
  694. struct buffer_desc *buf, gfp_t flags,
  695. enum dma_data_direction dir)
  696. {
  697. for (; nbytes > 0; sg = sg_next(sg)) {
  698. unsigned len = min(nbytes, sg->length);
  699. struct buffer_desc *next_buf;
  700. u32 next_buf_phys;
  701. void *ptr;
  702. nbytes -= len;
  703. ptr = sg_virt(sg);
  704. next_buf = dma_pool_alloc(buffer_pool, flags, &next_buf_phys);
  705. if (!next_buf) {
  706. buf = NULL;
  707. break;
  708. }
  709. sg_dma_address(sg) = dma_map_single(dev, ptr, len, dir);
  710. buf->next = next_buf;
  711. buf->phys_next = next_buf_phys;
  712. buf = next_buf;
  713. buf->phys_addr = sg_dma_address(sg);
  714. buf->buf_len = len;
  715. buf->dir = dir;
  716. }
  717. buf->next = NULL;
  718. buf->phys_next = 0;
  719. return buf;
  720. }
  721. static int ablk_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  722. unsigned int key_len)
  723. {
  724. struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  725. u32 *flags = &tfm->base.crt_flags;
  726. int ret;
  727. init_completion(&ctx->completion);
  728. atomic_inc(&ctx->configuring);
  729. reset_sa_dir(&ctx->encrypt);
  730. reset_sa_dir(&ctx->decrypt);
  731. ctx->encrypt.npe_mode = NPE_OP_HMAC_DISABLE;
  732. ctx->decrypt.npe_mode = NPE_OP_HMAC_DISABLE;
  733. ret = setup_cipher(&tfm->base, 0, key, key_len);
  734. if (ret)
  735. goto out;
  736. ret = setup_cipher(&tfm->base, 1, key, key_len);
  737. if (ret)
  738. goto out;
  739. if (*flags & CRYPTO_TFM_RES_WEAK_KEY) {
  740. if (*flags & CRYPTO_TFM_REQ_WEAK_KEY) {
  741. ret = -EINVAL;
  742. } else {
  743. *flags &= ~CRYPTO_TFM_RES_WEAK_KEY;
  744. }
  745. }
  746. out:
  747. if (!atomic_dec_and_test(&ctx->configuring))
  748. wait_for_completion(&ctx->completion);
  749. return ret;
  750. }
  751. static int ablk_rfc3686_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  752. unsigned int key_len)
  753. {
  754. struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  755. /* the nonce is stored in bytes at end of key */
  756. if (key_len < CTR_RFC3686_NONCE_SIZE)
  757. return -EINVAL;
  758. memcpy(ctx->nonce, key + (key_len - CTR_RFC3686_NONCE_SIZE),
  759. CTR_RFC3686_NONCE_SIZE);
  760. key_len -= CTR_RFC3686_NONCE_SIZE;
  761. return ablk_setkey(tfm, key, key_len);
  762. }
  763. static int ablk_perform(struct ablkcipher_request *req, int encrypt)
  764. {
  765. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  766. struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  767. unsigned ivsize = crypto_ablkcipher_ivsize(tfm);
  768. struct ix_sa_dir *dir;
  769. struct crypt_ctl *crypt;
  770. unsigned int nbytes = req->nbytes;
  771. enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
  772. struct ablk_ctx *req_ctx = ablkcipher_request_ctx(req);
  773. struct buffer_desc src_hook;
  774. struct device *dev = &pdev->dev;
  775. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  776. GFP_KERNEL : GFP_ATOMIC;
  777. if (qmgr_stat_full(SEND_QID))
  778. return -EAGAIN;
  779. if (atomic_read(&ctx->configuring))
  780. return -EAGAIN;
  781. dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
  782. crypt = get_crypt_desc();
  783. if (!crypt)
  784. return -ENOMEM;
  785. crypt->data.ablk_req = req;
  786. crypt->crypto_ctx = dir->npe_ctx_phys;
  787. crypt->mode = dir->npe_mode;
  788. crypt->init_len = dir->npe_ctx_idx;
  789. crypt->crypt_offs = 0;
  790. crypt->crypt_len = nbytes;
  791. BUG_ON(ivsize && !req->info);
  792. memcpy(crypt->iv, req->info, ivsize);
  793. if (req->src != req->dst) {
  794. struct buffer_desc dst_hook;
  795. crypt->mode |= NPE_OP_NOT_IN_PLACE;
  796. /* This was never tested by Intel
  797. * for more than one dst buffer, I think. */
  798. req_ctx->dst = NULL;
  799. if (!chainup_buffers(dev, req->dst, nbytes, &dst_hook,
  800. flags, DMA_FROM_DEVICE))
  801. goto free_buf_dest;
  802. src_direction = DMA_TO_DEVICE;
  803. req_ctx->dst = dst_hook.next;
  804. crypt->dst_buf = dst_hook.phys_next;
  805. } else {
  806. req_ctx->dst = NULL;
  807. }
  808. req_ctx->src = NULL;
  809. if (!chainup_buffers(dev, req->src, nbytes, &src_hook,
  810. flags, src_direction))
  811. goto free_buf_src;
  812. req_ctx->src = src_hook.next;
  813. crypt->src_buf = src_hook.phys_next;
  814. crypt->ctl_flags |= CTL_FLAG_PERFORM_ABLK;
  815. qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
  816. BUG_ON(qmgr_stat_overflow(SEND_QID));
  817. return -EINPROGRESS;
  818. free_buf_src:
  819. free_buf_chain(dev, req_ctx->src, crypt->src_buf);
  820. free_buf_dest:
  821. if (req->src != req->dst) {
  822. free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
  823. }
  824. crypt->ctl_flags = CTL_FLAG_UNUSED;
  825. return -ENOMEM;
  826. }
  827. static int ablk_encrypt(struct ablkcipher_request *req)
  828. {
  829. return ablk_perform(req, 1);
  830. }
  831. static int ablk_decrypt(struct ablkcipher_request *req)
  832. {
  833. return ablk_perform(req, 0);
  834. }
  835. static int ablk_rfc3686_crypt(struct ablkcipher_request *req)
  836. {
  837. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  838. struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  839. u8 iv[CTR_RFC3686_BLOCK_SIZE];
  840. u8 *info = req->info;
  841. int ret;
  842. /* set up counter block */
  843. memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
  844. memcpy(iv + CTR_RFC3686_NONCE_SIZE, info, CTR_RFC3686_IV_SIZE);
  845. /* initialize counter portion of counter block */
  846. *(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
  847. cpu_to_be32(1);
  848. req->info = iv;
  849. ret = ablk_perform(req, 1);
  850. req->info = info;
  851. return ret;
  852. }
  853. static int aead_perform(struct aead_request *req, int encrypt,
  854. int cryptoffset, int eff_cryptlen, u8 *iv)
  855. {
  856. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  857. struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
  858. unsigned ivsize = crypto_aead_ivsize(tfm);
  859. unsigned authsize = crypto_aead_authsize(tfm);
  860. struct ix_sa_dir *dir;
  861. struct crypt_ctl *crypt;
  862. unsigned int cryptlen;
  863. struct buffer_desc *buf, src_hook;
  864. struct aead_ctx *req_ctx = aead_request_ctx(req);
  865. struct device *dev = &pdev->dev;
  866. gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  867. GFP_KERNEL : GFP_ATOMIC;
  868. enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
  869. unsigned int lastlen;
  870. if (qmgr_stat_full(SEND_QID))
  871. return -EAGAIN;
  872. if (atomic_read(&ctx->configuring))
  873. return -EAGAIN;
  874. if (encrypt) {
  875. dir = &ctx->encrypt;
  876. cryptlen = req->cryptlen;
  877. } else {
  878. dir = &ctx->decrypt;
  879. /* req->cryptlen includes the authsize when decrypting */
  880. cryptlen = req->cryptlen -authsize;
  881. eff_cryptlen -= authsize;
  882. }
  883. crypt = get_crypt_desc();
  884. if (!crypt)
  885. return -ENOMEM;
  886. crypt->data.aead_req = req;
  887. crypt->crypto_ctx = dir->npe_ctx_phys;
  888. crypt->mode = dir->npe_mode;
  889. crypt->init_len = dir->npe_ctx_idx;
  890. crypt->crypt_offs = cryptoffset;
  891. crypt->crypt_len = eff_cryptlen;
  892. crypt->auth_offs = 0;
  893. crypt->auth_len = req->assoclen + cryptlen;
  894. BUG_ON(ivsize && !req->iv);
  895. memcpy(crypt->iv, req->iv, ivsize);
  896. buf = chainup_buffers(dev, req->src, crypt->auth_len,
  897. &src_hook, flags, src_direction);
  898. req_ctx->src = src_hook.next;
  899. crypt->src_buf = src_hook.phys_next;
  900. if (!buf)
  901. goto free_buf_src;
  902. lastlen = buf->buf_len;
  903. if (lastlen >= authsize)
  904. crypt->icv_rev_aes = buf->phys_addr +
  905. buf->buf_len - authsize;
  906. req_ctx->dst = NULL;
  907. if (req->src != req->dst) {
  908. struct buffer_desc dst_hook;
  909. crypt->mode |= NPE_OP_NOT_IN_PLACE;
  910. src_direction = DMA_TO_DEVICE;
  911. buf = chainup_buffers(dev, req->dst, crypt->auth_len,
  912. &dst_hook, flags, DMA_FROM_DEVICE);
  913. req_ctx->dst = dst_hook.next;
  914. crypt->dst_buf = dst_hook.phys_next;
  915. if (!buf)
  916. goto free_buf_dst;
  917. if (encrypt) {
  918. lastlen = buf->buf_len;
  919. if (lastlen >= authsize)
  920. crypt->icv_rev_aes = buf->phys_addr +
  921. buf->buf_len - authsize;
  922. }
  923. }
  924. if (unlikely(lastlen < authsize)) {
  925. /* The 12 hmac bytes are scattered,
  926. * we need to copy them into a safe buffer */
  927. req_ctx->hmac_virt = dma_pool_alloc(buffer_pool, flags,
  928. &crypt->icv_rev_aes);
  929. if (unlikely(!req_ctx->hmac_virt))
  930. goto free_buf_dst;
  931. if (!encrypt) {
  932. scatterwalk_map_and_copy(req_ctx->hmac_virt,
  933. req->src, cryptlen, authsize, 0);
  934. }
  935. req_ctx->encrypt = encrypt;
  936. } else {
  937. req_ctx->hmac_virt = NULL;
  938. }
  939. crypt->ctl_flags |= CTL_FLAG_PERFORM_AEAD;
  940. qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
  941. BUG_ON(qmgr_stat_overflow(SEND_QID));
  942. return -EINPROGRESS;
  943. free_buf_dst:
  944. free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
  945. free_buf_src:
  946. free_buf_chain(dev, req_ctx->src, crypt->src_buf);
  947. crypt->ctl_flags = CTL_FLAG_UNUSED;
  948. return -ENOMEM;
  949. }
  950. static int aead_setup(struct crypto_aead *tfm, unsigned int authsize)
  951. {
  952. struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
  953. u32 *flags = &tfm->base.crt_flags;
  954. unsigned digest_len = crypto_aead_maxauthsize(tfm);
  955. int ret;
  956. if (!ctx->enckey_len && !ctx->authkey_len)
  957. return 0;
  958. init_completion(&ctx->completion);
  959. atomic_inc(&ctx->configuring);
  960. reset_sa_dir(&ctx->encrypt);
  961. reset_sa_dir(&ctx->decrypt);
  962. ret = setup_cipher(&tfm->base, 0, ctx->enckey, ctx->enckey_len);
  963. if (ret)
  964. goto out;
  965. ret = setup_cipher(&tfm->base, 1, ctx->enckey, ctx->enckey_len);
  966. if (ret)
  967. goto out;
  968. ret = setup_auth(&tfm->base, 0, authsize, ctx->authkey,
  969. ctx->authkey_len, digest_len);
  970. if (ret)
  971. goto out;
  972. ret = setup_auth(&tfm->base, 1, authsize, ctx->authkey,
  973. ctx->authkey_len, digest_len);
  974. if (ret)
  975. goto out;
  976. if (*flags & CRYPTO_TFM_RES_WEAK_KEY) {
  977. if (*flags & CRYPTO_TFM_REQ_WEAK_KEY) {
  978. ret = -EINVAL;
  979. goto out;
  980. } else {
  981. *flags &= ~CRYPTO_TFM_RES_WEAK_KEY;
  982. }
  983. }
  984. out:
  985. if (!atomic_dec_and_test(&ctx->configuring))
  986. wait_for_completion(&ctx->completion);
  987. return ret;
  988. }
  989. static int aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  990. {
  991. int max = crypto_aead_maxauthsize(tfm) >> 2;
  992. if ((authsize>>2) < 1 || (authsize>>2) > max || (authsize & 3))
  993. return -EINVAL;
  994. return aead_setup(tfm, authsize);
  995. }
  996. static int aead_setkey(struct crypto_aead *tfm, const u8 *key,
  997. unsigned int keylen)
  998. {
  999. struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
  1000. struct crypto_authenc_keys keys;
  1001. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  1002. goto badkey;
  1003. if (keys.authkeylen > sizeof(ctx->authkey))
  1004. goto badkey;
  1005. if (keys.enckeylen > sizeof(ctx->enckey))
  1006. goto badkey;
  1007. memcpy(ctx->authkey, keys.authkey, keys.authkeylen);
  1008. memcpy(ctx->enckey, keys.enckey, keys.enckeylen);
  1009. ctx->authkey_len = keys.authkeylen;
  1010. ctx->enckey_len = keys.enckeylen;
  1011. return aead_setup(tfm, crypto_aead_authsize(tfm));
  1012. badkey:
  1013. crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  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");