ablkcipher.c 17 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. if (walk->iv_buffer)
  119. kfree(walk->iv_buffer);
  120. return err;
  121. }
  122. EXPORT_SYMBOL_GPL(ablkcipher_walk_done);
  123. static inline int ablkcipher_next_slow(struct ablkcipher_request *req,
  124. struct ablkcipher_walk *walk,
  125. unsigned int bsize,
  126. unsigned int alignmask,
  127. void **src_p, void **dst_p)
  128. {
  129. unsigned aligned_bsize = ALIGN(bsize, alignmask + 1);
  130. struct ablkcipher_buffer *p;
  131. void *src, *dst, *base;
  132. unsigned int n;
  133. n = ALIGN(sizeof(struct ablkcipher_buffer), alignmask + 1);
  134. n += (aligned_bsize * 3 - (alignmask + 1) +
  135. (alignmask & ~(crypto_tfm_ctx_alignment() - 1)));
  136. p = kmalloc(n, GFP_ATOMIC);
  137. if (!p)
  138. return ablkcipher_walk_done(req, walk, -ENOMEM);
  139. base = p + 1;
  140. dst = (u8 *)ALIGN((unsigned long)base, alignmask + 1);
  141. src = dst = ablkcipher_get_spot(dst, bsize);
  142. p->len = bsize;
  143. p->data = dst;
  144. scatterwalk_copychunks(src, &walk->in, bsize, 0);
  145. ablkcipher_queue_write(walk, p);
  146. walk->nbytes = bsize;
  147. walk->flags |= ABLKCIPHER_WALK_SLOW;
  148. *src_p = src;
  149. *dst_p = dst;
  150. return 0;
  151. }
  152. static inline int ablkcipher_copy_iv(struct ablkcipher_walk *walk,
  153. struct crypto_tfm *tfm,
  154. unsigned int alignmask)
  155. {
  156. unsigned bs = walk->blocksize;
  157. unsigned int ivsize = tfm->crt_ablkcipher.ivsize;
  158. unsigned aligned_bs = ALIGN(bs, alignmask + 1);
  159. unsigned int size = aligned_bs * 2 + ivsize + max(aligned_bs, ivsize) -
  160. (alignmask + 1);
  161. u8 *iv;
  162. size += alignmask & ~(crypto_tfm_ctx_alignment() - 1);
  163. walk->iv_buffer = kmalloc(size, GFP_ATOMIC);
  164. if (!walk->iv_buffer)
  165. return -ENOMEM;
  166. iv = (u8 *)ALIGN((unsigned long)walk->iv_buffer, alignmask + 1);
  167. iv = ablkcipher_get_spot(iv, bs) + aligned_bs;
  168. iv = ablkcipher_get_spot(iv, bs) + aligned_bs;
  169. iv = ablkcipher_get_spot(iv, ivsize);
  170. walk->iv = memcpy(iv, walk->iv, ivsize);
  171. return 0;
  172. }
  173. static inline int ablkcipher_next_fast(struct ablkcipher_request *req,
  174. struct ablkcipher_walk *walk)
  175. {
  176. walk->src.page = scatterwalk_page(&walk->in);
  177. walk->src.offset = offset_in_page(walk->in.offset);
  178. walk->dst.page = scatterwalk_page(&walk->out);
  179. walk->dst.offset = offset_in_page(walk->out.offset);
  180. return 0;
  181. }
  182. static int ablkcipher_walk_next(struct ablkcipher_request *req,
  183. struct ablkcipher_walk *walk)
  184. {
  185. struct crypto_tfm *tfm = req->base.tfm;
  186. unsigned int alignmask, bsize, n;
  187. void *src, *dst;
  188. int err;
  189. alignmask = crypto_tfm_alg_alignmask(tfm);
  190. n = walk->total;
  191. if (unlikely(n < crypto_tfm_alg_blocksize(tfm))) {
  192. req->base.flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  193. return ablkcipher_walk_done(req, walk, -EINVAL);
  194. }
  195. walk->flags &= ~ABLKCIPHER_WALK_SLOW;
  196. src = dst = NULL;
  197. bsize = min(walk->blocksize, n);
  198. n = scatterwalk_clamp(&walk->in, n);
  199. n = scatterwalk_clamp(&walk->out, n);
  200. if (n < bsize ||
  201. !scatterwalk_aligned(&walk->in, alignmask) ||
  202. !scatterwalk_aligned(&walk->out, alignmask)) {
  203. err = ablkcipher_next_slow(req, walk, bsize, alignmask,
  204. &src, &dst);
  205. goto set_phys_lowmem;
  206. }
  207. walk->nbytes = n;
  208. return ablkcipher_next_fast(req, walk);
  209. set_phys_lowmem:
  210. if (err >= 0) {
  211. walk->src.page = virt_to_page(src);
  212. walk->dst.page = virt_to_page(dst);
  213. walk->src.offset = ((unsigned long)src & (PAGE_SIZE - 1));
  214. walk->dst.offset = ((unsigned long)dst & (PAGE_SIZE - 1));
  215. }
  216. return err;
  217. }
  218. static int ablkcipher_walk_first(struct ablkcipher_request *req,
  219. struct ablkcipher_walk *walk)
  220. {
  221. struct crypto_tfm *tfm = req->base.tfm;
  222. unsigned int alignmask;
  223. alignmask = crypto_tfm_alg_alignmask(tfm);
  224. if (WARN_ON_ONCE(in_irq()))
  225. return -EDEADLK;
  226. walk->nbytes = walk->total;
  227. if (unlikely(!walk->total))
  228. return 0;
  229. walk->iv_buffer = NULL;
  230. walk->iv = req->info;
  231. if (unlikely(((unsigned long)walk->iv & alignmask))) {
  232. int err = ablkcipher_copy_iv(walk, tfm, alignmask);
  233. if (err)
  234. return err;
  235. }
  236. scatterwalk_start(&walk->in, walk->in.sg);
  237. scatterwalk_start(&walk->out, walk->out.sg);
  238. return ablkcipher_walk_next(req, walk);
  239. }
  240. int ablkcipher_walk_phys(struct ablkcipher_request *req,
  241. struct ablkcipher_walk *walk)
  242. {
  243. walk->blocksize = crypto_tfm_alg_blocksize(req->base.tfm);
  244. return ablkcipher_walk_first(req, walk);
  245. }
  246. EXPORT_SYMBOL_GPL(ablkcipher_walk_phys);
  247. static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
  248. unsigned int keylen)
  249. {
  250. struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
  251. unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
  252. int ret;
  253. u8 *buffer, *alignbuffer;
  254. unsigned long absize;
  255. absize = keylen + alignmask;
  256. buffer = kmalloc(absize, GFP_ATOMIC);
  257. if (!buffer)
  258. return -ENOMEM;
  259. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  260. memcpy(alignbuffer, key, keylen);
  261. ret = cipher->setkey(tfm, alignbuffer, keylen);
  262. memset(alignbuffer, 0, keylen);
  263. kfree(buffer);
  264. return ret;
  265. }
  266. static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  267. unsigned int keylen)
  268. {
  269. struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
  270. unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
  271. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  272. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  273. return -EINVAL;
  274. }
  275. if ((unsigned long)key & alignmask)
  276. return setkey_unaligned(tfm, key, keylen);
  277. return cipher->setkey(tfm, key, keylen);
  278. }
  279. static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type,
  280. u32 mask)
  281. {
  282. return alg->cra_ctxsize;
  283. }
  284. int skcipher_null_givencrypt(struct skcipher_givcrypt_request *req)
  285. {
  286. return crypto_ablkcipher_encrypt(&req->creq);
  287. }
  288. int skcipher_null_givdecrypt(struct skcipher_givcrypt_request *req)
  289. {
  290. return crypto_ablkcipher_decrypt(&req->creq);
  291. }
  292. static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
  293. u32 mask)
  294. {
  295. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  296. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  297. if (alg->ivsize > PAGE_SIZE / 8)
  298. return -EINVAL;
  299. crt->setkey = setkey;
  300. crt->encrypt = alg->encrypt;
  301. crt->decrypt = alg->decrypt;
  302. if (!alg->ivsize) {
  303. crt->givencrypt = skcipher_null_givencrypt;
  304. crt->givdecrypt = skcipher_null_givdecrypt;
  305. }
  306. crt->base = __crypto_ablkcipher_cast(tfm);
  307. crt->ivsize = alg->ivsize;
  308. return 0;
  309. }
  310. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  311. __attribute__ ((unused));
  312. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  313. {
  314. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  315. seq_printf(m, "type : ablkcipher\n");
  316. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  317. "yes" : "no");
  318. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  319. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  320. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  321. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  322. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<default>");
  323. }
  324. const struct crypto_type crypto_ablkcipher_type = {
  325. .ctxsize = crypto_ablkcipher_ctxsize,
  326. .init = crypto_init_ablkcipher_ops,
  327. #ifdef CONFIG_PROC_FS
  328. .show = crypto_ablkcipher_show,
  329. #endif
  330. };
  331. EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
  332. static int no_givdecrypt(struct skcipher_givcrypt_request *req)
  333. {
  334. return -ENOSYS;
  335. }
  336. static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
  337. u32 mask)
  338. {
  339. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  340. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  341. if (alg->ivsize > PAGE_SIZE / 8)
  342. return -EINVAL;
  343. crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
  344. alg->setkey : setkey;
  345. crt->encrypt = alg->encrypt;
  346. crt->decrypt = alg->decrypt;
  347. crt->givencrypt = alg->givencrypt;
  348. crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
  349. crt->base = __crypto_ablkcipher_cast(tfm);
  350. crt->ivsize = alg->ivsize;
  351. return 0;
  352. }
  353. static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  354. __attribute__ ((unused));
  355. static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  356. {
  357. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  358. seq_printf(m, "type : givcipher\n");
  359. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  360. "yes" : "no");
  361. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  362. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  363. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  364. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  365. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<built-in>");
  366. }
  367. const struct crypto_type crypto_givcipher_type = {
  368. .ctxsize = crypto_ablkcipher_ctxsize,
  369. .init = crypto_init_givcipher_ops,
  370. #ifdef CONFIG_PROC_FS
  371. .show = crypto_givcipher_show,
  372. #endif
  373. };
  374. EXPORT_SYMBOL_GPL(crypto_givcipher_type);
  375. const char *crypto_default_geniv(const struct crypto_alg *alg)
  376. {
  377. if (((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  378. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  379. alg->cra_ablkcipher.ivsize) !=
  380. alg->cra_blocksize)
  381. return "chainiv";
  382. return alg->cra_flags & CRYPTO_ALG_ASYNC ?
  383. "eseqiv" : skcipher_default_geniv;
  384. }
  385. static int crypto_givcipher_default(struct crypto_alg *alg, u32 type, u32 mask)
  386. {
  387. struct rtattr *tb[3];
  388. struct {
  389. struct rtattr attr;
  390. struct crypto_attr_type data;
  391. } ptype;
  392. struct {
  393. struct rtattr attr;
  394. struct crypto_attr_alg data;
  395. } palg;
  396. struct crypto_template *tmpl;
  397. struct crypto_instance *inst;
  398. struct crypto_alg *larval;
  399. const char *geniv;
  400. int err;
  401. larval = crypto_larval_lookup(alg->cra_driver_name,
  402. (type & ~CRYPTO_ALG_TYPE_MASK) |
  403. CRYPTO_ALG_TYPE_GIVCIPHER,
  404. mask | CRYPTO_ALG_TYPE_MASK);
  405. err = PTR_ERR(larval);
  406. if (IS_ERR(larval))
  407. goto out;
  408. err = -EAGAIN;
  409. if (!crypto_is_larval(larval))
  410. goto drop_larval;
  411. ptype.attr.rta_len = sizeof(ptype);
  412. ptype.attr.rta_type = CRYPTOA_TYPE;
  413. ptype.data.type = type | CRYPTO_ALG_GENIV;
  414. /* GENIV tells the template that we're making a default geniv. */
  415. ptype.data.mask = mask | CRYPTO_ALG_GENIV;
  416. tb[0] = &ptype.attr;
  417. palg.attr.rta_len = sizeof(palg);
  418. palg.attr.rta_type = CRYPTOA_ALG;
  419. /* Must use the exact name to locate ourselves. */
  420. memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
  421. tb[1] = &palg.attr;
  422. tb[2] = NULL;
  423. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  424. CRYPTO_ALG_TYPE_BLKCIPHER)
  425. geniv = alg->cra_blkcipher.geniv;
  426. else
  427. geniv = alg->cra_ablkcipher.geniv;
  428. if (!geniv)
  429. geniv = crypto_default_geniv(alg);
  430. tmpl = crypto_lookup_template(geniv);
  431. err = -ENOENT;
  432. if (!tmpl)
  433. goto kill_larval;
  434. inst = tmpl->alloc(tb);
  435. err = PTR_ERR(inst);
  436. if (IS_ERR(inst))
  437. goto put_tmpl;
  438. if ((err = crypto_register_instance(tmpl, inst))) {
  439. tmpl->free(inst);
  440. goto put_tmpl;
  441. }
  442. /* Redo the lookup to use the instance we just registered. */
  443. err = -EAGAIN;
  444. put_tmpl:
  445. crypto_tmpl_put(tmpl);
  446. kill_larval:
  447. crypto_larval_kill(larval);
  448. drop_larval:
  449. crypto_mod_put(larval);
  450. out:
  451. crypto_mod_put(alg);
  452. return err;
  453. }
  454. static struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type,
  455. u32 mask)
  456. {
  457. struct crypto_alg *alg;
  458. alg = crypto_alg_mod_lookup(name, type, mask);
  459. if (IS_ERR(alg))
  460. return alg;
  461. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  462. CRYPTO_ALG_TYPE_GIVCIPHER)
  463. return alg;
  464. if (!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  465. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  466. alg->cra_ablkcipher.ivsize))
  467. return alg;
  468. crypto_mod_put(alg);
  469. alg = crypto_alg_mod_lookup(name, type | CRYPTO_ALG_TESTED,
  470. mask & ~CRYPTO_ALG_TESTED);
  471. if (IS_ERR(alg))
  472. return alg;
  473. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  474. CRYPTO_ALG_TYPE_GIVCIPHER) {
  475. if ((alg->cra_flags ^ type ^ ~mask) & CRYPTO_ALG_TESTED) {
  476. crypto_mod_put(alg);
  477. alg = ERR_PTR(-ENOENT);
  478. }
  479. return alg;
  480. }
  481. BUG_ON(!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  482. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  483. alg->cra_ablkcipher.ivsize));
  484. return ERR_PTR(crypto_givcipher_default(alg, type, mask));
  485. }
  486. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
  487. u32 type, u32 mask)
  488. {
  489. struct crypto_alg *alg;
  490. int err;
  491. type = crypto_skcipher_type(type);
  492. mask = crypto_skcipher_mask(mask);
  493. alg = crypto_lookup_skcipher(name, type, mask);
  494. if (IS_ERR(alg))
  495. return PTR_ERR(alg);
  496. err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
  497. crypto_mod_put(alg);
  498. return err;
  499. }
  500. EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
  501. struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
  502. u32 type, u32 mask)
  503. {
  504. struct crypto_tfm *tfm;
  505. int err;
  506. type = crypto_skcipher_type(type);
  507. mask = crypto_skcipher_mask(mask);
  508. for (;;) {
  509. struct crypto_alg *alg;
  510. alg = crypto_lookup_skcipher(alg_name, type, mask);
  511. if (IS_ERR(alg)) {
  512. err = PTR_ERR(alg);
  513. goto err;
  514. }
  515. tfm = __crypto_alloc_tfm(alg, type, mask);
  516. if (!IS_ERR(tfm))
  517. return __crypto_ablkcipher_cast(tfm);
  518. crypto_mod_put(alg);
  519. err = PTR_ERR(tfm);
  520. err:
  521. if (err != -EAGAIN)
  522. break;
  523. if (signal_pending(current)) {
  524. err = -EINTR;
  525. break;
  526. }
  527. }
  528. return ERR_PTR(err);
  529. }
  530. EXPORT_SYMBOL_GPL(crypto_alloc_ablkcipher);
  531. static int __init skcipher_module_init(void)
  532. {
  533. skcipher_default_geniv = num_possible_cpus() > 1 ?
  534. "eseqiv" : "chainiv";
  535. return 0;
  536. }
  537. static void skcipher_module_exit(void)
  538. {
  539. }
  540. module_init(skcipher_module_init);
  541. module_exit(skcipher_module_exit);