ablkcipher.c 18 KB

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