ixp4xx_crypto.c 37 KB

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