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. if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  323. sizeof(struct crypto_report_blkcipher), &rblkcipher))
  324. goto nla_put_failure;
  325. return 0;
  326. nla_put_failure:
  327. return -EMSGSIZE;
  328. }
  329. #else
  330. static int crypto_ablkcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  331. {
  332. return -ENOSYS;
  333. }
  334. #endif
  335. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  336. __attribute__ ((unused));
  337. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  338. {
  339. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  340. seq_printf(m, "type : ablkcipher\n");
  341. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  342. "yes" : "no");
  343. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  344. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  345. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  346. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  347. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<default>");
  348. }
  349. const struct crypto_type crypto_ablkcipher_type = {
  350. .ctxsize = crypto_ablkcipher_ctxsize,
  351. .init = crypto_init_ablkcipher_ops,
  352. #ifdef CONFIG_PROC_FS
  353. .show = crypto_ablkcipher_show,
  354. #endif
  355. .report = crypto_ablkcipher_report,
  356. };
  357. EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
  358. static int no_givdecrypt(struct skcipher_givcrypt_request *req)
  359. {
  360. return -ENOSYS;
  361. }
  362. static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
  363. u32 mask)
  364. {
  365. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  366. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  367. if (alg->ivsize > PAGE_SIZE / 8)
  368. return -EINVAL;
  369. crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
  370. alg->setkey : setkey;
  371. crt->encrypt = alg->encrypt;
  372. crt->decrypt = alg->decrypt;
  373. crt->givencrypt = alg->givencrypt;
  374. crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
  375. crt->base = __crypto_ablkcipher_cast(tfm);
  376. crt->ivsize = alg->ivsize;
  377. return 0;
  378. }
  379. #ifdef CONFIG_NET
  380. static int crypto_givcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  381. {
  382. struct crypto_report_blkcipher rblkcipher;
  383. snprintf(rblkcipher.type, CRYPTO_MAX_ALG_NAME, "%s", "givcipher");
  384. snprintf(rblkcipher.geniv, CRYPTO_MAX_ALG_NAME, "%s",
  385. alg->cra_ablkcipher.geniv ?: "<built-in>");
  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 alg->cra_flags & CRYPTO_ALG_ASYNC ?
  434. "eseqiv" : skcipher_default_geniv;
  435. }
  436. static int crypto_givcipher_default(struct crypto_alg *alg, u32 type, u32 mask)
  437. {
  438. struct rtattr *tb[3];
  439. struct {
  440. struct rtattr attr;
  441. struct crypto_attr_type data;
  442. } ptype;
  443. struct {
  444. struct rtattr attr;
  445. struct crypto_attr_alg data;
  446. } palg;
  447. struct crypto_template *tmpl;
  448. struct crypto_instance *inst;
  449. struct crypto_alg *larval;
  450. const char *geniv;
  451. int err;
  452. larval = crypto_larval_lookup(alg->cra_driver_name,
  453. (type & ~CRYPTO_ALG_TYPE_MASK) |
  454. CRYPTO_ALG_TYPE_GIVCIPHER,
  455. mask | CRYPTO_ALG_TYPE_MASK);
  456. err = PTR_ERR(larval);
  457. if (IS_ERR(larval))
  458. goto out;
  459. err = -EAGAIN;
  460. if (!crypto_is_larval(larval))
  461. goto drop_larval;
  462. ptype.attr.rta_len = sizeof(ptype);
  463. ptype.attr.rta_type = CRYPTOA_TYPE;
  464. ptype.data.type = type | CRYPTO_ALG_GENIV;
  465. /* GENIV tells the template that we're making a default geniv. */
  466. ptype.data.mask = mask | CRYPTO_ALG_GENIV;
  467. tb[0] = &ptype.attr;
  468. palg.attr.rta_len = sizeof(palg);
  469. palg.attr.rta_type = CRYPTOA_ALG;
  470. /* Must use the exact name to locate ourselves. */
  471. memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
  472. tb[1] = &palg.attr;
  473. tb[2] = NULL;
  474. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  475. CRYPTO_ALG_TYPE_BLKCIPHER)
  476. geniv = alg->cra_blkcipher.geniv;
  477. else
  478. geniv = alg->cra_ablkcipher.geniv;
  479. if (!geniv)
  480. geniv = crypto_default_geniv(alg);
  481. tmpl = crypto_lookup_template(geniv);
  482. err = -ENOENT;
  483. if (!tmpl)
  484. goto kill_larval;
  485. inst = tmpl->alloc(tb);
  486. err = PTR_ERR(inst);
  487. if (IS_ERR(inst))
  488. goto put_tmpl;
  489. if ((err = crypto_register_instance(tmpl, inst))) {
  490. tmpl->free(inst);
  491. goto put_tmpl;
  492. }
  493. /* Redo the lookup to use the instance we just registered. */
  494. err = -EAGAIN;
  495. put_tmpl:
  496. crypto_tmpl_put(tmpl);
  497. kill_larval:
  498. crypto_larval_kill(larval);
  499. drop_larval:
  500. crypto_mod_put(larval);
  501. out:
  502. crypto_mod_put(alg);
  503. return err;
  504. }
  505. struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type, u32 mask)
  506. {
  507. struct crypto_alg *alg;
  508. alg = crypto_alg_mod_lookup(name, type, mask);
  509. if (IS_ERR(alg))
  510. return alg;
  511. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  512. CRYPTO_ALG_TYPE_GIVCIPHER)
  513. return alg;
  514. if (!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  515. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  516. alg->cra_ablkcipher.ivsize))
  517. return alg;
  518. crypto_mod_put(alg);
  519. alg = crypto_alg_mod_lookup(name, type | CRYPTO_ALG_TESTED,
  520. mask & ~CRYPTO_ALG_TESTED);
  521. if (IS_ERR(alg))
  522. return alg;
  523. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  524. CRYPTO_ALG_TYPE_GIVCIPHER) {
  525. if ((alg->cra_flags ^ type ^ ~mask) & CRYPTO_ALG_TESTED) {
  526. crypto_mod_put(alg);
  527. alg = ERR_PTR(-ENOENT);
  528. }
  529. return alg;
  530. }
  531. BUG_ON(!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  532. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  533. alg->cra_ablkcipher.ivsize));
  534. return ERR_PTR(crypto_givcipher_default(alg, type, mask));
  535. }
  536. EXPORT_SYMBOL_GPL(crypto_lookup_skcipher);
  537. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
  538. u32 type, u32 mask)
  539. {
  540. struct crypto_alg *alg;
  541. int err;
  542. type = crypto_skcipher_type(type);
  543. mask = crypto_skcipher_mask(mask);
  544. alg = crypto_lookup_skcipher(name, type, mask);
  545. if (IS_ERR(alg))
  546. return PTR_ERR(alg);
  547. err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
  548. crypto_mod_put(alg);
  549. return err;
  550. }
  551. EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
  552. struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
  553. u32 type, u32 mask)
  554. {
  555. struct crypto_tfm *tfm;
  556. int err;
  557. type = crypto_skcipher_type(type);
  558. mask = crypto_skcipher_mask(mask);
  559. for (;;) {
  560. struct crypto_alg *alg;
  561. alg = crypto_lookup_skcipher(alg_name, type, mask);
  562. if (IS_ERR(alg)) {
  563. err = PTR_ERR(alg);
  564. goto err;
  565. }
  566. tfm = __crypto_alloc_tfm(alg, type, mask);
  567. if (!IS_ERR(tfm))
  568. return __crypto_ablkcipher_cast(tfm);
  569. crypto_mod_put(alg);
  570. err = PTR_ERR(tfm);
  571. err:
  572. if (err != -EAGAIN)
  573. break;
  574. if (signal_pending(current)) {
  575. err = -EINTR;
  576. break;
  577. }
  578. }
  579. return ERR_PTR(err);
  580. }
  581. EXPORT_SYMBOL_GPL(crypto_alloc_ablkcipher);
  582. static int __init skcipher_module_init(void)
  583. {
  584. skcipher_default_geniv = num_possible_cpus() > 1 ?
  585. "eseqiv" : "chainiv";
  586. return 0;
  587. }
  588. static void skcipher_module_exit(void)
  589. {
  590. }
  591. module_init(skcipher_module_init);
  592. module_exit(skcipher_module_exit);