blkcipher.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. * Block chaining cipher operations.
  3. *
  4. * Generic encrypt/decrypt wrapper for ciphers, handles operations across
  5. * multiple page boundaries by using temporary blocks. In user context,
  6. * the kernel is given a chance to schedule us once per page.
  7. *
  8. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. */
  16. #include <crypto/internal/skcipher.h>
  17. #include <crypto/scatterwalk.h>
  18. #include <linux/errno.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/slab.h>
  25. #include <linux/string.h>
  26. #include <linux/cryptouser.h>
  27. #include <net/netlink.h>
  28. #include "internal.h"
  29. enum {
  30. BLKCIPHER_WALK_PHYS = 1 << 0,
  31. BLKCIPHER_WALK_SLOW = 1 << 1,
  32. BLKCIPHER_WALK_COPY = 1 << 2,
  33. BLKCIPHER_WALK_DIFF = 1 << 3,
  34. };
  35. static int blkcipher_walk_next(struct blkcipher_desc *desc,
  36. struct blkcipher_walk *walk);
  37. static int blkcipher_walk_first(struct blkcipher_desc *desc,
  38. struct blkcipher_walk *walk);
  39. static inline void blkcipher_map_src(struct blkcipher_walk *walk)
  40. {
  41. walk->src.virt.addr = scatterwalk_map(&walk->in);
  42. }
  43. static inline void blkcipher_map_dst(struct blkcipher_walk *walk)
  44. {
  45. walk->dst.virt.addr = scatterwalk_map(&walk->out);
  46. }
  47. static inline void blkcipher_unmap_src(struct blkcipher_walk *walk)
  48. {
  49. scatterwalk_unmap(walk->src.virt.addr);
  50. }
  51. static inline void blkcipher_unmap_dst(struct blkcipher_walk *walk)
  52. {
  53. scatterwalk_unmap(walk->dst.virt.addr);
  54. }
  55. /* Get a spot of the specified length that does not straddle a page.
  56. * The caller needs to ensure that there is enough space for this operation.
  57. */
  58. static inline u8 *blkcipher_get_spot(u8 *start, unsigned int len)
  59. {
  60. u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
  61. return max(start, end_page);
  62. }
  63. static inline unsigned int blkcipher_done_slow(struct crypto_blkcipher *tfm,
  64. struct blkcipher_walk *walk,
  65. unsigned int bsize)
  66. {
  67. u8 *addr;
  68. unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
  69. addr = (u8 *)ALIGN((unsigned long)walk->buffer, alignmask + 1);
  70. addr = blkcipher_get_spot(addr, bsize);
  71. scatterwalk_copychunks(addr, &walk->out, bsize, 1);
  72. return bsize;
  73. }
  74. static inline unsigned int blkcipher_done_fast(struct blkcipher_walk *walk,
  75. unsigned int n)
  76. {
  77. if (walk->flags & BLKCIPHER_WALK_COPY) {
  78. blkcipher_map_dst(walk);
  79. memcpy(walk->dst.virt.addr, walk->page, n);
  80. blkcipher_unmap_dst(walk);
  81. } else if (!(walk->flags & BLKCIPHER_WALK_PHYS)) {
  82. if (walk->flags & BLKCIPHER_WALK_DIFF)
  83. blkcipher_unmap_dst(walk);
  84. blkcipher_unmap_src(walk);
  85. }
  86. scatterwalk_advance(&walk->in, n);
  87. scatterwalk_advance(&walk->out, n);
  88. return n;
  89. }
  90. int blkcipher_walk_done(struct blkcipher_desc *desc,
  91. struct blkcipher_walk *walk, int err)
  92. {
  93. struct crypto_blkcipher *tfm = desc->tfm;
  94. unsigned int nbytes = 0;
  95. if (likely(err >= 0)) {
  96. unsigned int n = walk->nbytes - err;
  97. if (likely(!(walk->flags & BLKCIPHER_WALK_SLOW)))
  98. n = blkcipher_done_fast(walk, n);
  99. else if (WARN_ON(err)) {
  100. err = -EINVAL;
  101. goto err;
  102. } else
  103. n = blkcipher_done_slow(tfm, 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(desc->flags);
  114. return blkcipher_walk_next(desc, walk);
  115. }
  116. if (walk->iv != desc->info)
  117. memcpy(desc->info, walk->iv, crypto_blkcipher_ivsize(tfm));
  118. if (walk->buffer != walk->page)
  119. kfree(walk->buffer);
  120. if (walk->page)
  121. free_page((unsigned long)walk->page);
  122. return err;
  123. }
  124. EXPORT_SYMBOL_GPL(blkcipher_walk_done);
  125. static inline int blkcipher_next_slow(struct blkcipher_desc *desc,
  126. struct blkcipher_walk *walk,
  127. unsigned int bsize,
  128. unsigned int alignmask)
  129. {
  130. unsigned int n;
  131. unsigned aligned_bsize = ALIGN(bsize, alignmask + 1);
  132. if (walk->buffer)
  133. goto ok;
  134. walk->buffer = walk->page;
  135. if (walk->buffer)
  136. goto ok;
  137. n = aligned_bsize * 3 - (alignmask + 1) +
  138. (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  139. walk->buffer = kmalloc(n, GFP_ATOMIC);
  140. if (!walk->buffer)
  141. return blkcipher_walk_done(desc, walk, -ENOMEM);
  142. ok:
  143. walk->dst.virt.addr = (u8 *)ALIGN((unsigned long)walk->buffer,
  144. alignmask + 1);
  145. walk->dst.virt.addr = blkcipher_get_spot(walk->dst.virt.addr, bsize);
  146. walk->src.virt.addr = blkcipher_get_spot(walk->dst.virt.addr +
  147. aligned_bsize, bsize);
  148. scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
  149. walk->nbytes = bsize;
  150. walk->flags |= BLKCIPHER_WALK_SLOW;
  151. return 0;
  152. }
  153. static inline int blkcipher_next_copy(struct blkcipher_walk *walk)
  154. {
  155. u8 *tmp = walk->page;
  156. blkcipher_map_src(walk);
  157. memcpy(tmp, walk->src.virt.addr, walk->nbytes);
  158. blkcipher_unmap_src(walk);
  159. walk->src.virt.addr = tmp;
  160. walk->dst.virt.addr = tmp;
  161. return 0;
  162. }
  163. static inline int blkcipher_next_fast(struct blkcipher_desc *desc,
  164. struct blkcipher_walk *walk)
  165. {
  166. unsigned long diff;
  167. walk->src.phys.page = scatterwalk_page(&walk->in);
  168. walk->src.phys.offset = offset_in_page(walk->in.offset);
  169. walk->dst.phys.page = scatterwalk_page(&walk->out);
  170. walk->dst.phys.offset = offset_in_page(walk->out.offset);
  171. if (walk->flags & BLKCIPHER_WALK_PHYS)
  172. return 0;
  173. diff = walk->src.phys.offset - walk->dst.phys.offset;
  174. diff |= walk->src.virt.page - walk->dst.virt.page;
  175. blkcipher_map_src(walk);
  176. walk->dst.virt.addr = walk->src.virt.addr;
  177. if (diff) {
  178. walk->flags |= BLKCIPHER_WALK_DIFF;
  179. blkcipher_map_dst(walk);
  180. }
  181. return 0;
  182. }
  183. static int blkcipher_walk_next(struct blkcipher_desc *desc,
  184. struct blkcipher_walk *walk)
  185. {
  186. struct crypto_blkcipher *tfm = desc->tfm;
  187. unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
  188. unsigned int bsize;
  189. unsigned int n;
  190. int err;
  191. n = walk->total;
  192. if (unlikely(n < crypto_blkcipher_blocksize(tfm))) {
  193. desc->flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  194. return blkcipher_walk_done(desc, walk, -EINVAL);
  195. }
  196. walk->flags &= ~(BLKCIPHER_WALK_SLOW | BLKCIPHER_WALK_COPY |
  197. BLKCIPHER_WALK_DIFF);
  198. if (!scatterwalk_aligned(&walk->in, alignmask) ||
  199. !scatterwalk_aligned(&walk->out, alignmask)) {
  200. walk->flags |= BLKCIPHER_WALK_COPY;
  201. if (!walk->page) {
  202. walk->page = (void *)__get_free_page(GFP_ATOMIC);
  203. if (!walk->page)
  204. n = 0;
  205. }
  206. }
  207. bsize = min(walk->blocksize, n);
  208. n = scatterwalk_clamp(&walk->in, n);
  209. n = scatterwalk_clamp(&walk->out, n);
  210. if (unlikely(n < bsize)) {
  211. err = blkcipher_next_slow(desc, walk, bsize, alignmask);
  212. goto set_phys_lowmem;
  213. }
  214. walk->nbytes = n;
  215. if (walk->flags & BLKCIPHER_WALK_COPY) {
  216. err = blkcipher_next_copy(walk);
  217. goto set_phys_lowmem;
  218. }
  219. return blkcipher_next_fast(desc, walk);
  220. set_phys_lowmem:
  221. if (walk->flags & BLKCIPHER_WALK_PHYS) {
  222. walk->src.phys.page = virt_to_page(walk->src.virt.addr);
  223. walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
  224. walk->src.phys.offset &= PAGE_SIZE - 1;
  225. walk->dst.phys.offset &= PAGE_SIZE - 1;
  226. }
  227. return err;
  228. }
  229. static inline int blkcipher_copy_iv(struct blkcipher_walk *walk,
  230. struct crypto_blkcipher *tfm,
  231. unsigned int alignmask)
  232. {
  233. unsigned bs = walk->blocksize;
  234. unsigned int ivsize = crypto_blkcipher_ivsize(tfm);
  235. unsigned aligned_bs = ALIGN(bs, alignmask + 1);
  236. unsigned int size = aligned_bs * 2 + ivsize + max(aligned_bs, ivsize) -
  237. (alignmask + 1);
  238. u8 *iv;
  239. size += alignmask & ~(crypto_tfm_ctx_alignment() - 1);
  240. walk->buffer = kmalloc(size, GFP_ATOMIC);
  241. if (!walk->buffer)
  242. return -ENOMEM;
  243. iv = (u8 *)ALIGN((unsigned long)walk->buffer, alignmask + 1);
  244. iv = blkcipher_get_spot(iv, bs) + aligned_bs;
  245. iv = blkcipher_get_spot(iv, bs) + aligned_bs;
  246. iv = blkcipher_get_spot(iv, ivsize);
  247. walk->iv = memcpy(iv, walk->iv, ivsize);
  248. return 0;
  249. }
  250. int blkcipher_walk_virt(struct blkcipher_desc *desc,
  251. struct blkcipher_walk *walk)
  252. {
  253. walk->flags &= ~BLKCIPHER_WALK_PHYS;
  254. walk->blocksize = crypto_blkcipher_blocksize(desc->tfm);
  255. return blkcipher_walk_first(desc, walk);
  256. }
  257. EXPORT_SYMBOL_GPL(blkcipher_walk_virt);
  258. int blkcipher_walk_phys(struct blkcipher_desc *desc,
  259. struct blkcipher_walk *walk)
  260. {
  261. walk->flags |= BLKCIPHER_WALK_PHYS;
  262. walk->blocksize = crypto_blkcipher_blocksize(desc->tfm);
  263. return blkcipher_walk_first(desc, walk);
  264. }
  265. EXPORT_SYMBOL_GPL(blkcipher_walk_phys);
  266. static int blkcipher_walk_first(struct blkcipher_desc *desc,
  267. struct blkcipher_walk *walk)
  268. {
  269. struct crypto_blkcipher *tfm = desc->tfm;
  270. unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
  271. if (WARN_ON_ONCE(in_irq()))
  272. return -EDEADLK;
  273. walk->nbytes = walk->total;
  274. if (unlikely(!walk->total))
  275. return 0;
  276. walk->buffer = NULL;
  277. walk->iv = desc->info;
  278. if (unlikely(((unsigned long)walk->iv & alignmask))) {
  279. int err = blkcipher_copy_iv(walk, tfm, alignmask);
  280. if (err)
  281. return err;
  282. }
  283. scatterwalk_start(&walk->in, walk->in.sg);
  284. scatterwalk_start(&walk->out, walk->out.sg);
  285. walk->page = NULL;
  286. return blkcipher_walk_next(desc, walk);
  287. }
  288. int blkcipher_walk_virt_block(struct blkcipher_desc *desc,
  289. struct blkcipher_walk *walk,
  290. unsigned int blocksize)
  291. {
  292. walk->flags &= ~BLKCIPHER_WALK_PHYS;
  293. walk->blocksize = blocksize;
  294. return blkcipher_walk_first(desc, walk);
  295. }
  296. EXPORT_SYMBOL_GPL(blkcipher_walk_virt_block);
  297. static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key,
  298. unsigned int keylen)
  299. {
  300. struct blkcipher_alg *cipher = &tfm->__crt_alg->cra_blkcipher;
  301. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  302. int ret;
  303. u8 *buffer, *alignbuffer;
  304. unsigned long absize;
  305. absize = keylen + alignmask;
  306. buffer = kmalloc(absize, GFP_ATOMIC);
  307. if (!buffer)
  308. return -ENOMEM;
  309. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  310. memcpy(alignbuffer, key, keylen);
  311. ret = cipher->setkey(tfm, alignbuffer, keylen);
  312. memset(alignbuffer, 0, keylen);
  313. kfree(buffer);
  314. return ret;
  315. }
  316. static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
  317. {
  318. struct blkcipher_alg *cipher = &tfm->__crt_alg->cra_blkcipher;
  319. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  320. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  321. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  322. return -EINVAL;
  323. }
  324. if ((unsigned long)key & alignmask)
  325. return setkey_unaligned(tfm, key, keylen);
  326. return cipher->setkey(tfm, key, keylen);
  327. }
  328. static int async_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  329. unsigned int keylen)
  330. {
  331. return setkey(crypto_ablkcipher_tfm(tfm), key, keylen);
  332. }
  333. static int async_encrypt(struct ablkcipher_request *req)
  334. {
  335. struct crypto_tfm *tfm = req->base.tfm;
  336. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  337. struct blkcipher_desc desc = {
  338. .tfm = __crypto_blkcipher_cast(tfm),
  339. .info = req->info,
  340. .flags = req->base.flags,
  341. };
  342. return alg->encrypt(&desc, req->dst, req->src, req->nbytes);
  343. }
  344. static int async_decrypt(struct ablkcipher_request *req)
  345. {
  346. struct crypto_tfm *tfm = req->base.tfm;
  347. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  348. struct blkcipher_desc desc = {
  349. .tfm = __crypto_blkcipher_cast(tfm),
  350. .info = req->info,
  351. .flags = req->base.flags,
  352. };
  353. return alg->decrypt(&desc, req->dst, req->src, req->nbytes);
  354. }
  355. static unsigned int crypto_blkcipher_ctxsize(struct crypto_alg *alg, u32 type,
  356. u32 mask)
  357. {
  358. struct blkcipher_alg *cipher = &alg->cra_blkcipher;
  359. unsigned int len = alg->cra_ctxsize;
  360. if ((mask & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_MASK &&
  361. cipher->ivsize) {
  362. len = ALIGN(len, (unsigned long)alg->cra_alignmask + 1);
  363. len += cipher->ivsize;
  364. }
  365. return len;
  366. }
  367. static int crypto_init_blkcipher_ops_async(struct crypto_tfm *tfm)
  368. {
  369. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  370. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  371. crt->setkey = async_setkey;
  372. crt->encrypt = async_encrypt;
  373. crt->decrypt = async_decrypt;
  374. if (!alg->ivsize) {
  375. crt->givencrypt = skcipher_null_givencrypt;
  376. crt->givdecrypt = skcipher_null_givdecrypt;
  377. }
  378. crt->base = __crypto_ablkcipher_cast(tfm);
  379. crt->ivsize = alg->ivsize;
  380. return 0;
  381. }
  382. static int crypto_init_blkcipher_ops_sync(struct crypto_tfm *tfm)
  383. {
  384. struct blkcipher_tfm *crt = &tfm->crt_blkcipher;
  385. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  386. unsigned long align = crypto_tfm_alg_alignmask(tfm) + 1;
  387. unsigned long addr;
  388. crt->setkey = setkey;
  389. crt->encrypt = alg->encrypt;
  390. crt->decrypt = alg->decrypt;
  391. addr = (unsigned long)crypto_tfm_ctx(tfm);
  392. addr = ALIGN(addr, align);
  393. addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);
  394. crt->iv = (void *)addr;
  395. return 0;
  396. }
  397. static int crypto_init_blkcipher_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  398. {
  399. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  400. if (alg->ivsize > PAGE_SIZE / 8)
  401. return -EINVAL;
  402. if ((mask & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_MASK)
  403. return crypto_init_blkcipher_ops_sync(tfm);
  404. else
  405. return crypto_init_blkcipher_ops_async(tfm);
  406. }
  407. #ifdef CONFIG_NET
  408. static int crypto_blkcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  409. {
  410. struct crypto_report_blkcipher rblkcipher;
  411. snprintf(rblkcipher.type, CRYPTO_MAX_ALG_NAME, "%s", "blkcipher");
  412. snprintf(rblkcipher.geniv, CRYPTO_MAX_ALG_NAME, "%s",
  413. alg->cra_blkcipher.geniv ?: "<default>");
  414. rblkcipher.blocksize = alg->cra_blocksize;
  415. rblkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
  416. rblkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
  417. rblkcipher.ivsize = alg->cra_blkcipher.ivsize;
  418. NLA_PUT(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  419. sizeof(struct crypto_report_blkcipher), &rblkcipher);
  420. return 0;
  421. nla_put_failure:
  422. return -EMSGSIZE;
  423. }
  424. #else
  425. static int crypto_blkcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  426. {
  427. return -ENOSYS;
  428. }
  429. #endif
  430. static void crypto_blkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  431. __attribute__ ((unused));
  432. static void crypto_blkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  433. {
  434. seq_printf(m, "type : blkcipher\n");
  435. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  436. seq_printf(m, "min keysize : %u\n", alg->cra_blkcipher.min_keysize);
  437. seq_printf(m, "max keysize : %u\n", alg->cra_blkcipher.max_keysize);
  438. seq_printf(m, "ivsize : %u\n", alg->cra_blkcipher.ivsize);
  439. seq_printf(m, "geniv : %s\n", alg->cra_blkcipher.geniv ?:
  440. "<default>");
  441. }
  442. const struct crypto_type crypto_blkcipher_type = {
  443. .ctxsize = crypto_blkcipher_ctxsize,
  444. .init = crypto_init_blkcipher_ops,
  445. #ifdef CONFIG_PROC_FS
  446. .show = crypto_blkcipher_show,
  447. #endif
  448. .report = crypto_blkcipher_report,
  449. };
  450. EXPORT_SYMBOL_GPL(crypto_blkcipher_type);
  451. static int crypto_grab_nivcipher(struct crypto_skcipher_spawn *spawn,
  452. const char *name, u32 type, u32 mask)
  453. {
  454. struct crypto_alg *alg;
  455. int err;
  456. type = crypto_skcipher_type(type);
  457. mask = crypto_skcipher_mask(mask)| CRYPTO_ALG_GENIV;
  458. alg = crypto_alg_mod_lookup(name, type, mask);
  459. if (IS_ERR(alg))
  460. return PTR_ERR(alg);
  461. err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
  462. crypto_mod_put(alg);
  463. return err;
  464. }
  465. struct crypto_instance *skcipher_geniv_alloc(struct crypto_template *tmpl,
  466. struct rtattr **tb, u32 type,
  467. u32 mask)
  468. {
  469. struct {
  470. int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
  471. unsigned int keylen);
  472. int (*encrypt)(struct ablkcipher_request *req);
  473. int (*decrypt)(struct ablkcipher_request *req);
  474. unsigned int min_keysize;
  475. unsigned int max_keysize;
  476. unsigned int ivsize;
  477. const char *geniv;
  478. } balg;
  479. const char *name;
  480. struct crypto_skcipher_spawn *spawn;
  481. struct crypto_attr_type *algt;
  482. struct crypto_instance *inst;
  483. struct crypto_alg *alg;
  484. int err;
  485. algt = crypto_get_attr_type(tb);
  486. err = PTR_ERR(algt);
  487. if (IS_ERR(algt))
  488. return ERR_PTR(err);
  489. if ((algt->type ^ (CRYPTO_ALG_TYPE_GIVCIPHER | CRYPTO_ALG_GENIV)) &
  490. algt->mask)
  491. return ERR_PTR(-EINVAL);
  492. name = crypto_attr_alg_name(tb[1]);
  493. err = PTR_ERR(name);
  494. if (IS_ERR(name))
  495. return ERR_PTR(err);
  496. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  497. if (!inst)
  498. return ERR_PTR(-ENOMEM);
  499. spawn = crypto_instance_ctx(inst);
  500. /* Ignore async algorithms if necessary. */
  501. mask |= crypto_requires_sync(algt->type, algt->mask);
  502. crypto_set_skcipher_spawn(spawn, inst);
  503. err = crypto_grab_nivcipher(spawn, name, type, mask);
  504. if (err)
  505. goto err_free_inst;
  506. alg = crypto_skcipher_spawn_alg(spawn);
  507. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  508. CRYPTO_ALG_TYPE_BLKCIPHER) {
  509. balg.ivsize = alg->cra_blkcipher.ivsize;
  510. balg.min_keysize = alg->cra_blkcipher.min_keysize;
  511. balg.max_keysize = alg->cra_blkcipher.max_keysize;
  512. balg.setkey = async_setkey;
  513. balg.encrypt = async_encrypt;
  514. balg.decrypt = async_decrypt;
  515. balg.geniv = alg->cra_blkcipher.geniv;
  516. } else {
  517. balg.ivsize = alg->cra_ablkcipher.ivsize;
  518. balg.min_keysize = alg->cra_ablkcipher.min_keysize;
  519. balg.max_keysize = alg->cra_ablkcipher.max_keysize;
  520. balg.setkey = alg->cra_ablkcipher.setkey;
  521. balg.encrypt = alg->cra_ablkcipher.encrypt;
  522. balg.decrypt = alg->cra_ablkcipher.decrypt;
  523. balg.geniv = alg->cra_ablkcipher.geniv;
  524. }
  525. err = -EINVAL;
  526. if (!balg.ivsize)
  527. goto err_drop_alg;
  528. /*
  529. * This is only true if we're constructing an algorithm with its
  530. * default IV generator. For the default generator we elide the
  531. * template name and double-check the IV generator.
  532. */
  533. if (algt->mask & CRYPTO_ALG_GENIV) {
  534. if (!balg.geniv)
  535. balg.geniv = crypto_default_geniv(alg);
  536. err = -EAGAIN;
  537. if (strcmp(tmpl->name, balg.geniv))
  538. goto err_drop_alg;
  539. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  540. memcpy(inst->alg.cra_driver_name, alg->cra_driver_name,
  541. CRYPTO_MAX_ALG_NAME);
  542. } else {
  543. err = -ENAMETOOLONG;
  544. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  545. "%s(%s)", tmpl->name, alg->cra_name) >=
  546. CRYPTO_MAX_ALG_NAME)
  547. goto err_drop_alg;
  548. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  549. "%s(%s)", tmpl->name, alg->cra_driver_name) >=
  550. CRYPTO_MAX_ALG_NAME)
  551. goto err_drop_alg;
  552. }
  553. inst->alg.cra_flags = CRYPTO_ALG_TYPE_GIVCIPHER | CRYPTO_ALG_GENIV;
  554. inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
  555. inst->alg.cra_priority = alg->cra_priority;
  556. inst->alg.cra_blocksize = alg->cra_blocksize;
  557. inst->alg.cra_alignmask = alg->cra_alignmask;
  558. inst->alg.cra_type = &crypto_givcipher_type;
  559. inst->alg.cra_ablkcipher.ivsize = balg.ivsize;
  560. inst->alg.cra_ablkcipher.min_keysize = balg.min_keysize;
  561. inst->alg.cra_ablkcipher.max_keysize = balg.max_keysize;
  562. inst->alg.cra_ablkcipher.geniv = balg.geniv;
  563. inst->alg.cra_ablkcipher.setkey = balg.setkey;
  564. inst->alg.cra_ablkcipher.encrypt = balg.encrypt;
  565. inst->alg.cra_ablkcipher.decrypt = balg.decrypt;
  566. out:
  567. return inst;
  568. err_drop_alg:
  569. crypto_drop_skcipher(spawn);
  570. err_free_inst:
  571. kfree(inst);
  572. inst = ERR_PTR(err);
  573. goto out;
  574. }
  575. EXPORT_SYMBOL_GPL(skcipher_geniv_alloc);
  576. void skcipher_geniv_free(struct crypto_instance *inst)
  577. {
  578. crypto_drop_skcipher(crypto_instance_ctx(inst));
  579. kfree(inst);
  580. }
  581. EXPORT_SYMBOL_GPL(skcipher_geniv_free);
  582. int skcipher_geniv_init(struct crypto_tfm *tfm)
  583. {
  584. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  585. struct crypto_ablkcipher *cipher;
  586. cipher = crypto_spawn_skcipher(crypto_instance_ctx(inst));
  587. if (IS_ERR(cipher))
  588. return PTR_ERR(cipher);
  589. tfm->crt_ablkcipher.base = cipher;
  590. tfm->crt_ablkcipher.reqsize += crypto_ablkcipher_reqsize(cipher);
  591. return 0;
  592. }
  593. EXPORT_SYMBOL_GPL(skcipher_geniv_init);
  594. void skcipher_geniv_exit(struct crypto_tfm *tfm)
  595. {
  596. crypto_free_ablkcipher(tfm->crt_ablkcipher.base);
  597. }
  598. EXPORT_SYMBOL_GPL(skcipher_geniv_exit);
  599. MODULE_LICENSE("GPL");
  600. MODULE_DESCRIPTION("Generic block chaining cipher type");