ablkcipher.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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 <crypto/scatterwalk.h>
  26. #include "internal.h"
  27. static const char *skcipher_default_geniv __read_mostly;
  28. struct ablkcipher_buffer {
  29. struct list_head entry;
  30. struct scatter_walk dst;
  31. unsigned int len;
  32. void *data;
  33. };
  34. enum {
  35. ABLKCIPHER_WALK_SLOW = 1 << 0,
  36. };
  37. static inline void ablkcipher_buffer_write(struct ablkcipher_buffer *p)
  38. {
  39. scatterwalk_copychunks(p->data, &p->dst, p->len, 1);
  40. }
  41. void __ablkcipher_walk_complete(struct ablkcipher_walk *walk)
  42. {
  43. struct ablkcipher_buffer *p, *tmp;
  44. list_for_each_entry_safe(p, tmp, &walk->buffers, entry) {
  45. ablkcipher_buffer_write(p);
  46. list_del(&p->entry);
  47. kfree(p);
  48. }
  49. }
  50. EXPORT_SYMBOL_GPL(__ablkcipher_walk_complete);
  51. static inline void ablkcipher_queue_write(struct ablkcipher_walk *walk,
  52. struct ablkcipher_buffer *p)
  53. {
  54. p->dst = walk->out;
  55. list_add_tail(&p->entry, &walk->buffers);
  56. }
  57. /* Get a spot of the specified length that does not straddle a page.
  58. * The caller needs to ensure that there is enough space for this operation.
  59. */
  60. static inline u8 *ablkcipher_get_spot(u8 *start, unsigned int len)
  61. {
  62. u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
  63. return max(start, end_page);
  64. }
  65. static inline unsigned int ablkcipher_done_slow(struct ablkcipher_walk *walk,
  66. unsigned int bsize)
  67. {
  68. unsigned int n = bsize;
  69. for (;;) {
  70. unsigned int len_this_page = scatterwalk_pagelen(&walk->out);
  71. if (len_this_page > n)
  72. len_this_page = n;
  73. scatterwalk_advance(&walk->out, n);
  74. if (n == len_this_page)
  75. break;
  76. n -= len_this_page;
  77. scatterwalk_start(&walk->out, scatterwalk_sg_next(walk->out.sg));
  78. }
  79. return bsize;
  80. }
  81. static inline unsigned int ablkcipher_done_fast(struct ablkcipher_walk *walk,
  82. unsigned int n)
  83. {
  84. scatterwalk_advance(&walk->in, n);
  85. scatterwalk_advance(&walk->out, n);
  86. return n;
  87. }
  88. static int ablkcipher_walk_next(struct ablkcipher_request *req,
  89. struct ablkcipher_walk *walk);
  90. int ablkcipher_walk_done(struct ablkcipher_request *req,
  91. struct ablkcipher_walk *walk, int err)
  92. {
  93. struct crypto_tfm *tfm = req->base.tfm;
  94. unsigned int nbytes = 0;
  95. if (likely(err >= 0)) {
  96. unsigned int n = walk->nbytes - err;
  97. if (likely(!(walk->flags & ABLKCIPHER_WALK_SLOW)))
  98. n = ablkcipher_done_fast(walk, n);
  99. else if (WARN_ON(err)) {
  100. err = -EINVAL;
  101. goto err;
  102. } else
  103. n = ablkcipher_done_slow(walk, n);
  104. nbytes = walk->total - n;
  105. err = 0;
  106. }
  107. scatterwalk_done(&walk->in, 0, nbytes);
  108. scatterwalk_done(&walk->out, 1, nbytes);
  109. err:
  110. walk->total = nbytes;
  111. walk->nbytes = nbytes;
  112. if (nbytes) {
  113. crypto_yield(req->base.flags);
  114. return ablkcipher_walk_next(req, walk);
  115. }
  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->nbytes = walk->total;
  226. if (unlikely(!walk->total))
  227. return 0;
  228. walk->iv_buffer = NULL;
  229. walk->iv = req->info;
  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. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  310. __attribute__ ((unused));
  311. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  312. {
  313. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  314. seq_printf(m, "type : ablkcipher\n");
  315. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  316. "yes" : "no");
  317. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  318. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  319. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  320. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  321. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<default>");
  322. }
  323. const struct crypto_type crypto_ablkcipher_type = {
  324. .ctxsize = crypto_ablkcipher_ctxsize,
  325. .init = crypto_init_ablkcipher_ops,
  326. #ifdef CONFIG_PROC_FS
  327. .show = crypto_ablkcipher_show,
  328. #endif
  329. };
  330. EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
  331. static int no_givdecrypt(struct skcipher_givcrypt_request *req)
  332. {
  333. return -ENOSYS;
  334. }
  335. static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
  336. u32 mask)
  337. {
  338. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  339. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  340. if (alg->ivsize > PAGE_SIZE / 8)
  341. return -EINVAL;
  342. crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
  343. alg->setkey : setkey;
  344. crt->encrypt = alg->encrypt;
  345. crt->decrypt = alg->decrypt;
  346. crt->givencrypt = alg->givencrypt;
  347. crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
  348. crt->base = __crypto_ablkcipher_cast(tfm);
  349. crt->ivsize = alg->ivsize;
  350. return 0;
  351. }
  352. static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  353. __attribute__ ((unused));
  354. static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  355. {
  356. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  357. seq_printf(m, "type : givcipher\n");
  358. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  359. "yes" : "no");
  360. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  361. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  362. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  363. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  364. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<built-in>");
  365. }
  366. const struct crypto_type crypto_givcipher_type = {
  367. .ctxsize = crypto_ablkcipher_ctxsize,
  368. .init = crypto_init_givcipher_ops,
  369. #ifdef CONFIG_PROC_FS
  370. .show = crypto_givcipher_show,
  371. #endif
  372. };
  373. EXPORT_SYMBOL_GPL(crypto_givcipher_type);
  374. const char *crypto_default_geniv(const struct crypto_alg *alg)
  375. {
  376. if (((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  377. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  378. alg->cra_ablkcipher.ivsize) !=
  379. alg->cra_blocksize)
  380. return "chainiv";
  381. return alg->cra_flags & CRYPTO_ALG_ASYNC ?
  382. "eseqiv" : skcipher_default_geniv;
  383. }
  384. static int crypto_givcipher_default(struct crypto_alg *alg, u32 type, u32 mask)
  385. {
  386. struct rtattr *tb[3];
  387. struct {
  388. struct rtattr attr;
  389. struct crypto_attr_type data;
  390. } ptype;
  391. struct {
  392. struct rtattr attr;
  393. struct crypto_attr_alg data;
  394. } palg;
  395. struct crypto_template *tmpl;
  396. struct crypto_instance *inst;
  397. struct crypto_alg *larval;
  398. const char *geniv;
  399. int err;
  400. larval = crypto_larval_lookup(alg->cra_driver_name,
  401. (type & ~CRYPTO_ALG_TYPE_MASK) |
  402. CRYPTO_ALG_TYPE_GIVCIPHER,
  403. mask | CRYPTO_ALG_TYPE_MASK);
  404. err = PTR_ERR(larval);
  405. if (IS_ERR(larval))
  406. goto out;
  407. err = -EAGAIN;
  408. if (!crypto_is_larval(larval))
  409. goto drop_larval;
  410. ptype.attr.rta_len = sizeof(ptype);
  411. ptype.attr.rta_type = CRYPTOA_TYPE;
  412. ptype.data.type = type | CRYPTO_ALG_GENIV;
  413. /* GENIV tells the template that we're making a default geniv. */
  414. ptype.data.mask = mask | CRYPTO_ALG_GENIV;
  415. tb[0] = &ptype.attr;
  416. palg.attr.rta_len = sizeof(palg);
  417. palg.attr.rta_type = CRYPTOA_ALG;
  418. /* Must use the exact name to locate ourselves. */
  419. memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
  420. tb[1] = &palg.attr;
  421. tb[2] = NULL;
  422. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  423. CRYPTO_ALG_TYPE_BLKCIPHER)
  424. geniv = alg->cra_blkcipher.geniv;
  425. else
  426. geniv = alg->cra_ablkcipher.geniv;
  427. if (!geniv)
  428. geniv = crypto_default_geniv(alg);
  429. tmpl = crypto_lookup_template(geniv);
  430. err = -ENOENT;
  431. if (!tmpl)
  432. goto kill_larval;
  433. inst = tmpl->alloc(tb);
  434. err = PTR_ERR(inst);
  435. if (IS_ERR(inst))
  436. goto put_tmpl;
  437. if ((err = crypto_register_instance(tmpl, inst))) {
  438. tmpl->free(inst);
  439. goto put_tmpl;
  440. }
  441. /* Redo the lookup to use the instance we just registered. */
  442. err = -EAGAIN;
  443. put_tmpl:
  444. crypto_tmpl_put(tmpl);
  445. kill_larval:
  446. crypto_larval_kill(larval);
  447. drop_larval:
  448. crypto_mod_put(larval);
  449. out:
  450. crypto_mod_put(alg);
  451. return err;
  452. }
  453. static struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type,
  454. u32 mask)
  455. {
  456. struct crypto_alg *alg;
  457. alg = crypto_alg_mod_lookup(name, type, mask);
  458. if (IS_ERR(alg))
  459. return alg;
  460. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  461. CRYPTO_ALG_TYPE_GIVCIPHER)
  462. return alg;
  463. if (!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  464. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  465. alg->cra_ablkcipher.ivsize))
  466. return alg;
  467. crypto_mod_put(alg);
  468. alg = crypto_alg_mod_lookup(name, type | CRYPTO_ALG_TESTED,
  469. mask & ~CRYPTO_ALG_TESTED);
  470. if (IS_ERR(alg))
  471. return alg;
  472. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  473. CRYPTO_ALG_TYPE_GIVCIPHER) {
  474. if ((alg->cra_flags ^ type ^ ~mask) & CRYPTO_ALG_TESTED) {
  475. crypto_mod_put(alg);
  476. alg = ERR_PTR(-ENOENT);
  477. }
  478. return alg;
  479. }
  480. BUG_ON(!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  481. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  482. alg->cra_ablkcipher.ivsize));
  483. return ERR_PTR(crypto_givcipher_default(alg, type, mask));
  484. }
  485. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
  486. u32 type, u32 mask)
  487. {
  488. struct crypto_alg *alg;
  489. int err;
  490. type = crypto_skcipher_type(type);
  491. mask = crypto_skcipher_mask(mask);
  492. alg = crypto_lookup_skcipher(name, type, mask);
  493. if (IS_ERR(alg))
  494. return PTR_ERR(alg);
  495. err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
  496. crypto_mod_put(alg);
  497. return err;
  498. }
  499. EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
  500. struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
  501. u32 type, u32 mask)
  502. {
  503. struct crypto_tfm *tfm;
  504. int err;
  505. type = crypto_skcipher_type(type);
  506. mask = crypto_skcipher_mask(mask);
  507. for (;;) {
  508. struct crypto_alg *alg;
  509. alg = crypto_lookup_skcipher(alg_name, type, mask);
  510. if (IS_ERR(alg)) {
  511. err = PTR_ERR(alg);
  512. goto err;
  513. }
  514. tfm = __crypto_alloc_tfm(alg, type, mask);
  515. if (!IS_ERR(tfm))
  516. return __crypto_ablkcipher_cast(tfm);
  517. crypto_mod_put(alg);
  518. err = PTR_ERR(tfm);
  519. err:
  520. if (err != -EAGAIN)
  521. break;
  522. if (signal_pending(current)) {
  523. err = -EINTR;
  524. break;
  525. }
  526. }
  527. return ERR_PTR(err);
  528. }
  529. EXPORT_SYMBOL_GPL(crypto_alloc_ablkcipher);
  530. static int __init skcipher_module_init(void)
  531. {
  532. skcipher_default_geniv = num_possible_cpus() > 1 ?
  533. "eseqiv" : "chainiv";
  534. return 0;
  535. }
  536. static void skcipher_module_exit(void)
  537. {
  538. }
  539. module_init(skcipher_module_init);
  540. module_exit(skcipher_module_exit);