ixp4xx_crypto.c 36 KB

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