ablkcipher.c 18 KB

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