ablkcipher.c 18 KB

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