ablkcipher.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. * Asynchronous block chaining cipher operations.
  3. *
  4. * This is the asynchronous version of blkcipher.c indicating completion
  5. * via a callback.
  6. *
  7. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. */
  15. #include <crypto/internal/skcipher.h>
  16. #include <linux/cpumask.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/cryptouser.h>
  26. #include <net/netlink.h>
  27. #include <crypto/scatterwalk.h>
  28. #include "internal.h"
  29. static const char *skcipher_default_geniv __read_mostly;
  30. struct ablkcipher_buffer {
  31. struct list_head entry;
  32. struct scatter_walk dst;
  33. unsigned int len;
  34. void *data;
  35. };
  36. enum {
  37. ABLKCIPHER_WALK_SLOW = 1 << 0,
  38. };
  39. static inline void ablkcipher_buffer_write(struct ablkcipher_buffer *p)
  40. {
  41. scatterwalk_copychunks(p->data, &p->dst, p->len, 1);
  42. }
  43. void __ablkcipher_walk_complete(struct ablkcipher_walk *walk)
  44. {
  45. struct ablkcipher_buffer *p, *tmp;
  46. list_for_each_entry_safe(p, tmp, &walk->buffers, entry) {
  47. ablkcipher_buffer_write(p);
  48. list_del(&p->entry);
  49. kfree(p);
  50. }
  51. }
  52. EXPORT_SYMBOL_GPL(__ablkcipher_walk_complete);
  53. static inline void ablkcipher_queue_write(struct ablkcipher_walk *walk,
  54. struct ablkcipher_buffer *p)
  55. {
  56. p->dst = walk->out;
  57. list_add_tail(&p->entry, &walk->buffers);
  58. }
  59. /* Get a spot of the specified length that does not straddle a page.
  60. * The caller needs to ensure that there is enough space for this operation.
  61. */
  62. static inline u8 *ablkcipher_get_spot(u8 *start, unsigned int len)
  63. {
  64. u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
  65. return max(start, end_page);
  66. }
  67. static inline unsigned int ablkcipher_done_slow(struct ablkcipher_walk *walk,
  68. unsigned int bsize)
  69. {
  70. unsigned int n = bsize;
  71. for (;;) {
  72. unsigned int len_this_page = scatterwalk_pagelen(&walk->out);
  73. if (len_this_page > n)
  74. len_this_page = n;
  75. scatterwalk_advance(&walk->out, n);
  76. if (n == len_this_page)
  77. break;
  78. n -= len_this_page;
  79. scatterwalk_start(&walk->out, scatterwalk_sg_next(walk->out.sg));
  80. }
  81. return bsize;
  82. }
  83. static inline unsigned int ablkcipher_done_fast(struct ablkcipher_walk *walk,
  84. unsigned int n)
  85. {
  86. scatterwalk_advance(&walk->in, n);
  87. scatterwalk_advance(&walk->out, n);
  88. return n;
  89. }
  90. static int ablkcipher_walk_next(struct ablkcipher_request *req,
  91. struct ablkcipher_walk *walk);
  92. int ablkcipher_walk_done(struct ablkcipher_request *req,
  93. struct ablkcipher_walk *walk, int err)
  94. {
  95. struct crypto_tfm *tfm = req->base.tfm;
  96. unsigned int nbytes = 0;
  97. if (likely(err >= 0)) {
  98. unsigned int n = walk->nbytes - err;
  99. if (likely(!(walk->flags & ABLKCIPHER_WALK_SLOW)))
  100. n = ablkcipher_done_fast(walk, n);
  101. else if (WARN_ON(err)) {
  102. err = -EINVAL;
  103. goto err;
  104. } else
  105. n = ablkcipher_done_slow(walk, n);
  106. nbytes = walk->total - n;
  107. err = 0;
  108. }
  109. scatterwalk_done(&walk->in, 0, nbytes);
  110. scatterwalk_done(&walk->out, 1, nbytes);
  111. err:
  112. walk->total = nbytes;
  113. walk->nbytes = nbytes;
  114. if (nbytes) {
  115. crypto_yield(req->base.flags);
  116. return ablkcipher_walk_next(req, walk);
  117. }
  118. if (walk->iv != req->info)
  119. memcpy(req->info, walk->iv, tfm->crt_ablkcipher.ivsize);
  120. kfree(walk->iv_buffer);
  121. return err;
  122. }
  123. EXPORT_SYMBOL_GPL(ablkcipher_walk_done);
  124. static inline int ablkcipher_next_slow(struct ablkcipher_request *req,
  125. struct ablkcipher_walk *walk,
  126. unsigned int bsize,
  127. unsigned int alignmask,
  128. void **src_p, void **dst_p)
  129. {
  130. unsigned aligned_bsize = ALIGN(bsize, alignmask + 1);
  131. struct ablkcipher_buffer *p;
  132. void *src, *dst, *base;
  133. unsigned int n;
  134. n = ALIGN(sizeof(struct ablkcipher_buffer), alignmask + 1);
  135. n += (aligned_bsize * 3 - (alignmask + 1) +
  136. (alignmask & ~(crypto_tfm_ctx_alignment() - 1)));
  137. p = kmalloc(n, GFP_ATOMIC);
  138. if (!p)
  139. return ablkcipher_walk_done(req, walk, -ENOMEM);
  140. base = p + 1;
  141. dst = (u8 *)ALIGN((unsigned long)base, alignmask + 1);
  142. src = dst = ablkcipher_get_spot(dst, bsize);
  143. p->len = bsize;
  144. p->data = dst;
  145. scatterwalk_copychunks(src, &walk->in, bsize, 0);
  146. ablkcipher_queue_write(walk, p);
  147. walk->nbytes = bsize;
  148. walk->flags |= ABLKCIPHER_WALK_SLOW;
  149. *src_p = src;
  150. *dst_p = dst;
  151. return 0;
  152. }
  153. static inline int ablkcipher_copy_iv(struct ablkcipher_walk *walk,
  154. struct crypto_tfm *tfm,
  155. unsigned int alignmask)
  156. {
  157. unsigned bs = walk->blocksize;
  158. unsigned int ivsize = tfm->crt_ablkcipher.ivsize;
  159. unsigned aligned_bs = ALIGN(bs, alignmask + 1);
  160. unsigned int size = aligned_bs * 2 + ivsize + max(aligned_bs, ivsize) -
  161. (alignmask + 1);
  162. u8 *iv;
  163. size += alignmask & ~(crypto_tfm_ctx_alignment() - 1);
  164. walk->iv_buffer = kmalloc(size, GFP_ATOMIC);
  165. if (!walk->iv_buffer)
  166. return -ENOMEM;
  167. iv = (u8 *)ALIGN((unsigned long)walk->iv_buffer, alignmask + 1);
  168. iv = ablkcipher_get_spot(iv, bs) + aligned_bs;
  169. iv = ablkcipher_get_spot(iv, bs) + aligned_bs;
  170. iv = ablkcipher_get_spot(iv, ivsize);
  171. walk->iv = memcpy(iv, walk->iv, ivsize);
  172. return 0;
  173. }
  174. static inline int ablkcipher_next_fast(struct ablkcipher_request *req,
  175. struct ablkcipher_walk *walk)
  176. {
  177. walk->src.page = scatterwalk_page(&walk->in);
  178. walk->src.offset = offset_in_page(walk->in.offset);
  179. walk->dst.page = scatterwalk_page(&walk->out);
  180. walk->dst.offset = offset_in_page(walk->out.offset);
  181. return 0;
  182. }
  183. static int ablkcipher_walk_next(struct ablkcipher_request *req,
  184. struct ablkcipher_walk *walk)
  185. {
  186. struct crypto_tfm *tfm = req->base.tfm;
  187. unsigned int alignmask, bsize, n;
  188. void *src, *dst;
  189. int err;
  190. alignmask = crypto_tfm_alg_alignmask(tfm);
  191. n = walk->total;
  192. if (unlikely(n < crypto_tfm_alg_blocksize(tfm))) {
  193. req->base.flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  194. return ablkcipher_walk_done(req, walk, -EINVAL);
  195. }
  196. walk->flags &= ~ABLKCIPHER_WALK_SLOW;
  197. src = dst = NULL;
  198. bsize = min(walk->blocksize, n);
  199. n = scatterwalk_clamp(&walk->in, n);
  200. n = scatterwalk_clamp(&walk->out, n);
  201. if (n < bsize ||
  202. !scatterwalk_aligned(&walk->in, alignmask) ||
  203. !scatterwalk_aligned(&walk->out, alignmask)) {
  204. err = ablkcipher_next_slow(req, walk, bsize, alignmask,
  205. &src, &dst);
  206. goto set_phys_lowmem;
  207. }
  208. walk->nbytes = n;
  209. return ablkcipher_next_fast(req, walk);
  210. set_phys_lowmem:
  211. if (err >= 0) {
  212. walk->src.page = virt_to_page(src);
  213. walk->dst.page = virt_to_page(dst);
  214. walk->src.offset = ((unsigned long)src & (PAGE_SIZE - 1));
  215. walk->dst.offset = ((unsigned long)dst & (PAGE_SIZE - 1));
  216. }
  217. return err;
  218. }
  219. static int ablkcipher_walk_first(struct ablkcipher_request *req,
  220. struct ablkcipher_walk *walk)
  221. {
  222. struct crypto_tfm *tfm = req->base.tfm;
  223. unsigned int alignmask;
  224. alignmask = crypto_tfm_alg_alignmask(tfm);
  225. if (WARN_ON_ONCE(in_irq()))
  226. return -EDEADLK;
  227. walk->nbytes = walk->total;
  228. if (unlikely(!walk->total))
  229. return 0;
  230. walk->iv_buffer = NULL;
  231. walk->iv = req->info;
  232. if (unlikely(((unsigned long)walk->iv & alignmask))) {
  233. int err = ablkcipher_copy_iv(walk, tfm, alignmask);
  234. if (err)
  235. return err;
  236. }
  237. scatterwalk_start(&walk->in, walk->in.sg);
  238. scatterwalk_start(&walk->out, walk->out.sg);
  239. return ablkcipher_walk_next(req, walk);
  240. }
  241. int ablkcipher_walk_phys(struct ablkcipher_request *req,
  242. struct ablkcipher_walk *walk)
  243. {
  244. walk->blocksize = crypto_tfm_alg_blocksize(req->base.tfm);
  245. return ablkcipher_walk_first(req, walk);
  246. }
  247. EXPORT_SYMBOL_GPL(ablkcipher_walk_phys);
  248. static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
  249. unsigned int keylen)
  250. {
  251. struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
  252. unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
  253. int ret;
  254. u8 *buffer, *alignbuffer;
  255. unsigned long absize;
  256. absize = keylen + alignmask;
  257. buffer = kmalloc(absize, GFP_ATOMIC);
  258. if (!buffer)
  259. return -ENOMEM;
  260. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  261. memcpy(alignbuffer, key, keylen);
  262. ret = cipher->setkey(tfm, alignbuffer, keylen);
  263. memset(alignbuffer, 0, keylen);
  264. kfree(buffer);
  265. return ret;
  266. }
  267. static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  268. unsigned int keylen)
  269. {
  270. struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
  271. unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
  272. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  273. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  274. return -EINVAL;
  275. }
  276. if ((unsigned long)key & alignmask)
  277. return setkey_unaligned(tfm, key, keylen);
  278. return cipher->setkey(tfm, key, keylen);
  279. }
  280. static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type,
  281. u32 mask)
  282. {
  283. return alg->cra_ctxsize;
  284. }
  285. int skcipher_null_givencrypt(struct skcipher_givcrypt_request *req)
  286. {
  287. return crypto_ablkcipher_encrypt(&req->creq);
  288. }
  289. int skcipher_null_givdecrypt(struct skcipher_givcrypt_request *req)
  290. {
  291. return crypto_ablkcipher_decrypt(&req->creq);
  292. }
  293. static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
  294. u32 mask)
  295. {
  296. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  297. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  298. if (alg->ivsize > PAGE_SIZE / 8)
  299. return -EINVAL;
  300. crt->setkey = setkey;
  301. crt->encrypt = alg->encrypt;
  302. crt->decrypt = alg->decrypt;
  303. if (!alg->ivsize) {
  304. crt->givencrypt = skcipher_null_givencrypt;
  305. crt->givdecrypt = skcipher_null_givdecrypt;
  306. }
  307. crt->base = __crypto_ablkcipher_cast(tfm);
  308. crt->ivsize = alg->ivsize;
  309. return 0;
  310. }
  311. #ifdef CONFIG_NET
  312. static int crypto_ablkcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  313. {
  314. struct crypto_report_blkcipher rblkcipher;
  315. snprintf(rblkcipher.type, CRYPTO_MAX_ALG_NAME, "%s", "ablkcipher");
  316. snprintf(rblkcipher.geniv, CRYPTO_MAX_ALG_NAME, "%s",
  317. alg->cra_ablkcipher.geniv ?: "<default>");
  318. rblkcipher.blocksize = alg->cra_blocksize;
  319. rblkcipher.min_keysize = alg->cra_ablkcipher.min_keysize;
  320. rblkcipher.max_keysize = alg->cra_ablkcipher.max_keysize;
  321. rblkcipher.ivsize = alg->cra_ablkcipher.ivsize;
  322. NLA_PUT(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  323. sizeof(struct crypto_report_blkcipher), &rblkcipher);
  324. return 0;
  325. nla_put_failure:
  326. return -EMSGSIZE;
  327. }
  328. #else
  329. static int crypto_ablkcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  330. {
  331. return -ENOSYS;
  332. }
  333. #endif
  334. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  335. __attribute__ ((unused));
  336. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  337. {
  338. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  339. seq_printf(m, "type : ablkcipher\n");
  340. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  341. "yes" : "no");
  342. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  343. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  344. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  345. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  346. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<default>");
  347. }
  348. const struct crypto_type crypto_ablkcipher_type = {
  349. .ctxsize = crypto_ablkcipher_ctxsize,
  350. .init = crypto_init_ablkcipher_ops,
  351. #ifdef CONFIG_PROC_FS
  352. .show = crypto_ablkcipher_show,
  353. #endif
  354. .report = crypto_ablkcipher_report,
  355. };
  356. EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
  357. static int no_givdecrypt(struct skcipher_givcrypt_request *req)
  358. {
  359. return -ENOSYS;
  360. }
  361. static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
  362. u32 mask)
  363. {
  364. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  365. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  366. if (alg->ivsize > PAGE_SIZE / 8)
  367. return -EINVAL;
  368. crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
  369. alg->setkey : setkey;
  370. crt->encrypt = alg->encrypt;
  371. crt->decrypt = alg->decrypt;
  372. crt->givencrypt = alg->givencrypt;
  373. crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
  374. crt->base = __crypto_ablkcipher_cast(tfm);
  375. crt->ivsize = alg->ivsize;
  376. return 0;
  377. }
  378. #ifdef CONFIG_NET
  379. static int crypto_givcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  380. {
  381. struct crypto_report_blkcipher rblkcipher;
  382. snprintf(rblkcipher.type, CRYPTO_MAX_ALG_NAME, "%s", "givcipher");
  383. snprintf(rblkcipher.geniv, CRYPTO_MAX_ALG_NAME, "%s",
  384. alg->cra_ablkcipher.geniv ?: "<built-in>");
  385. rblkcipher.blocksize = alg->cra_blocksize;
  386. rblkcipher.min_keysize = alg->cra_ablkcipher.min_keysize;
  387. rblkcipher.max_keysize = alg->cra_ablkcipher.max_keysize;
  388. rblkcipher.ivsize = alg->cra_ablkcipher.ivsize;
  389. NLA_PUT(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  390. sizeof(struct crypto_report_blkcipher), &rblkcipher);
  391. return 0;
  392. nla_put_failure:
  393. return -EMSGSIZE;
  394. }
  395. #else
  396. static int crypto_givcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  397. {
  398. return -ENOSYS;
  399. }
  400. #endif
  401. static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  402. __attribute__ ((unused));
  403. static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  404. {
  405. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  406. seq_printf(m, "type : givcipher\n");
  407. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  408. "yes" : "no");
  409. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  410. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  411. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  412. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  413. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<built-in>");
  414. }
  415. const struct crypto_type crypto_givcipher_type = {
  416. .ctxsize = crypto_ablkcipher_ctxsize,
  417. .init = crypto_init_givcipher_ops,
  418. #ifdef CONFIG_PROC_FS
  419. .show = crypto_givcipher_show,
  420. #endif
  421. .report = crypto_givcipher_report,
  422. };
  423. EXPORT_SYMBOL_GPL(crypto_givcipher_type);
  424. const char *crypto_default_geniv(const struct crypto_alg *alg)
  425. {
  426. if (((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  427. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  428. alg->cra_ablkcipher.ivsize) !=
  429. alg->cra_blocksize)
  430. return "chainiv";
  431. return alg->cra_flags & CRYPTO_ALG_ASYNC ?
  432. "eseqiv" : skcipher_default_geniv;
  433. }
  434. static int crypto_givcipher_default(struct crypto_alg *alg, u32 type, u32 mask)
  435. {
  436. struct rtattr *tb[3];
  437. struct {
  438. struct rtattr attr;
  439. struct crypto_attr_type data;
  440. } ptype;
  441. struct {
  442. struct rtattr attr;
  443. struct crypto_attr_alg data;
  444. } palg;
  445. struct crypto_template *tmpl;
  446. struct crypto_instance *inst;
  447. struct crypto_alg *larval;
  448. const char *geniv;
  449. int err;
  450. larval = crypto_larval_lookup(alg->cra_driver_name,
  451. (type & ~CRYPTO_ALG_TYPE_MASK) |
  452. CRYPTO_ALG_TYPE_GIVCIPHER,
  453. mask | CRYPTO_ALG_TYPE_MASK);
  454. err = PTR_ERR(larval);
  455. if (IS_ERR(larval))
  456. goto out;
  457. err = -EAGAIN;
  458. if (!crypto_is_larval(larval))
  459. goto drop_larval;
  460. ptype.attr.rta_len = sizeof(ptype);
  461. ptype.attr.rta_type = CRYPTOA_TYPE;
  462. ptype.data.type = type | CRYPTO_ALG_GENIV;
  463. /* GENIV tells the template that we're making a default geniv. */
  464. ptype.data.mask = mask | CRYPTO_ALG_GENIV;
  465. tb[0] = &ptype.attr;
  466. palg.attr.rta_len = sizeof(palg);
  467. palg.attr.rta_type = CRYPTOA_ALG;
  468. /* Must use the exact name to locate ourselves. */
  469. memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
  470. tb[1] = &palg.attr;
  471. tb[2] = NULL;
  472. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  473. CRYPTO_ALG_TYPE_BLKCIPHER)
  474. geniv = alg->cra_blkcipher.geniv;
  475. else
  476. geniv = alg->cra_ablkcipher.geniv;
  477. if (!geniv)
  478. geniv = crypto_default_geniv(alg);
  479. tmpl = crypto_lookup_template(geniv);
  480. err = -ENOENT;
  481. if (!tmpl)
  482. goto kill_larval;
  483. inst = tmpl->alloc(tb);
  484. err = PTR_ERR(inst);
  485. if (IS_ERR(inst))
  486. goto put_tmpl;
  487. if ((err = crypto_register_instance(tmpl, inst))) {
  488. tmpl->free(inst);
  489. goto put_tmpl;
  490. }
  491. /* Redo the lookup to use the instance we just registered. */
  492. err = -EAGAIN;
  493. put_tmpl:
  494. crypto_tmpl_put(tmpl);
  495. kill_larval:
  496. crypto_larval_kill(larval);
  497. drop_larval:
  498. crypto_mod_put(larval);
  499. out:
  500. crypto_mod_put(alg);
  501. return err;
  502. }
  503. static struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type,
  504. u32 mask)
  505. {
  506. struct crypto_alg *alg;
  507. alg = crypto_alg_mod_lookup(name, type, mask);
  508. if (IS_ERR(alg))
  509. return alg;
  510. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  511. CRYPTO_ALG_TYPE_GIVCIPHER)
  512. return alg;
  513. if (!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  514. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  515. alg->cra_ablkcipher.ivsize))
  516. return alg;
  517. crypto_mod_put(alg);
  518. alg = crypto_alg_mod_lookup(name, type | CRYPTO_ALG_TESTED,
  519. mask & ~CRYPTO_ALG_TESTED);
  520. if (IS_ERR(alg))
  521. return alg;
  522. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  523. CRYPTO_ALG_TYPE_GIVCIPHER) {
  524. if ((alg->cra_flags ^ type ^ ~mask) & CRYPTO_ALG_TESTED) {
  525. crypto_mod_put(alg);
  526. alg = ERR_PTR(-ENOENT);
  527. }
  528. return alg;
  529. }
  530. BUG_ON(!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  531. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  532. alg->cra_ablkcipher.ivsize));
  533. return ERR_PTR(crypto_givcipher_default(alg, type, mask));
  534. }
  535. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
  536. u32 type, u32 mask)
  537. {
  538. struct crypto_alg *alg;
  539. int err;
  540. type = crypto_skcipher_type(type);
  541. mask = crypto_skcipher_mask(mask);
  542. alg = crypto_lookup_skcipher(name, type, mask);
  543. if (IS_ERR(alg))
  544. return PTR_ERR(alg);
  545. err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
  546. crypto_mod_put(alg);
  547. return err;
  548. }
  549. EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
  550. struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
  551. u32 type, u32 mask)
  552. {
  553. struct crypto_tfm *tfm;
  554. int err;
  555. type = crypto_skcipher_type(type);
  556. mask = crypto_skcipher_mask(mask);
  557. for (;;) {
  558. struct crypto_alg *alg;
  559. alg = crypto_lookup_skcipher(alg_name, type, mask);
  560. if (IS_ERR(alg)) {
  561. err = PTR_ERR(alg);
  562. goto err;
  563. }
  564. tfm = __crypto_alloc_tfm(alg, type, mask);
  565. if (!IS_ERR(tfm))
  566. return __crypto_ablkcipher_cast(tfm);
  567. crypto_mod_put(alg);
  568. err = PTR_ERR(tfm);
  569. err:
  570. if (err != -EAGAIN)
  571. break;
  572. if (signal_pending(current)) {
  573. err = -EINTR;
  574. break;
  575. }
  576. }
  577. return ERR_PTR(err);
  578. }
  579. EXPORT_SYMBOL_GPL(crypto_alloc_ablkcipher);
  580. static int __init skcipher_module_init(void)
  581. {
  582. skcipher_default_geniv = num_possible_cpus() > 1 ?
  583. "eseqiv" : "chainiv";
  584. return 0;
  585. }
  586. static void skcipher_module_exit(void)
  587. {
  588. }
  589. module_init(skcipher_module_init);
  590. module_exit(skcipher_module_exit);