ablkcipher.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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/err.h>
  17. #include <linux/kernel.h>
  18. #include <linux/slab.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/cryptouser.h>
  21. #include <linux/compiler.h>
  22. #include <net/netlink.h>
  23. #include <crypto/scatterwalk.h>
  24. #include "internal.h"
  25. struct ablkcipher_buffer {
  26. struct list_head entry;
  27. struct scatter_walk dst;
  28. unsigned int len;
  29. void *data;
  30. };
  31. enum {
  32. ABLKCIPHER_WALK_SLOW = 1 << 0,
  33. };
  34. static inline void ablkcipher_buffer_write(struct ablkcipher_buffer *p)
  35. {
  36. scatterwalk_copychunks(p->data, &p->dst, p->len, 1);
  37. }
  38. void __ablkcipher_walk_complete(struct ablkcipher_walk *walk)
  39. {
  40. struct ablkcipher_buffer *p, *tmp;
  41. list_for_each_entry_safe(p, tmp, &walk->buffers, entry) {
  42. ablkcipher_buffer_write(p);
  43. list_del(&p->entry);
  44. kfree(p);
  45. }
  46. }
  47. EXPORT_SYMBOL_GPL(__ablkcipher_walk_complete);
  48. static inline void ablkcipher_queue_write(struct ablkcipher_walk *walk,
  49. struct ablkcipher_buffer *p)
  50. {
  51. p->dst = walk->out;
  52. list_add_tail(&p->entry, &walk->buffers);
  53. }
  54. /* Get a spot of the specified length that does not straddle a page.
  55. * The caller needs to ensure that there is enough space for this operation.
  56. */
  57. static inline u8 *ablkcipher_get_spot(u8 *start, unsigned int len)
  58. {
  59. u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
  60. return max(start, end_page);
  61. }
  62. static inline unsigned int ablkcipher_done_slow(struct ablkcipher_walk *walk,
  63. unsigned int bsize)
  64. {
  65. unsigned int n = bsize;
  66. for (;;) {
  67. unsigned int len_this_page = scatterwalk_pagelen(&walk->out);
  68. if (len_this_page > n)
  69. len_this_page = n;
  70. scatterwalk_advance(&walk->out, n);
  71. if (n == len_this_page)
  72. break;
  73. n -= len_this_page;
  74. scatterwalk_start(&walk->out, sg_next(walk->out.sg));
  75. }
  76. return bsize;
  77. }
  78. static inline unsigned int ablkcipher_done_fast(struct ablkcipher_walk *walk,
  79. unsigned int n)
  80. {
  81. scatterwalk_advance(&walk->in, n);
  82. scatterwalk_advance(&walk->out, n);
  83. return n;
  84. }
  85. static int ablkcipher_walk_next(struct ablkcipher_request *req,
  86. struct ablkcipher_walk *walk);
  87. int ablkcipher_walk_done(struct ablkcipher_request *req,
  88. struct ablkcipher_walk *walk, int err)
  89. {
  90. struct crypto_tfm *tfm = req->base.tfm;
  91. unsigned int nbytes = 0;
  92. if (likely(err >= 0)) {
  93. unsigned int n = walk->nbytes - err;
  94. if (likely(!(walk->flags & ABLKCIPHER_WALK_SLOW)))
  95. n = ablkcipher_done_fast(walk, n);
  96. else if (WARN_ON(err)) {
  97. err = -EINVAL;
  98. goto err;
  99. } else
  100. n = ablkcipher_done_slow(walk, n);
  101. nbytes = walk->total - n;
  102. err = 0;
  103. }
  104. scatterwalk_done(&walk->in, 0, nbytes);
  105. scatterwalk_done(&walk->out, 1, nbytes);
  106. err:
  107. walk->total = nbytes;
  108. walk->nbytes = nbytes;
  109. if (nbytes) {
  110. crypto_yield(req->base.flags);
  111. return ablkcipher_walk_next(req, walk);
  112. }
  113. if (walk->iv != req->info)
  114. memcpy(req->info, walk->iv, tfm->crt_ablkcipher.ivsize);
  115. kfree(walk->iv_buffer);
  116. return err;
  117. }
  118. EXPORT_SYMBOL_GPL(ablkcipher_walk_done);
  119. static inline int ablkcipher_next_slow(struct ablkcipher_request *req,
  120. struct ablkcipher_walk *walk,
  121. unsigned int bsize,
  122. unsigned int alignmask,
  123. void **src_p, void **dst_p)
  124. {
  125. unsigned aligned_bsize = ALIGN(bsize, alignmask + 1);
  126. struct ablkcipher_buffer *p;
  127. void *src, *dst, *base;
  128. unsigned int n;
  129. n = ALIGN(sizeof(struct ablkcipher_buffer), alignmask + 1);
  130. n += (aligned_bsize * 3 - (alignmask + 1) +
  131. (alignmask & ~(crypto_tfm_ctx_alignment() - 1)));
  132. p = kmalloc(n, GFP_ATOMIC);
  133. if (!p)
  134. return ablkcipher_walk_done(req, walk, -ENOMEM);
  135. base = p + 1;
  136. dst = (u8 *)ALIGN((unsigned long)base, alignmask + 1);
  137. src = dst = ablkcipher_get_spot(dst, bsize);
  138. p->len = bsize;
  139. p->data = dst;
  140. scatterwalk_copychunks(src, &walk->in, bsize, 0);
  141. ablkcipher_queue_write(walk, p);
  142. walk->nbytes = bsize;
  143. walk->flags |= ABLKCIPHER_WALK_SLOW;
  144. *src_p = src;
  145. *dst_p = dst;
  146. return 0;
  147. }
  148. static inline int ablkcipher_copy_iv(struct ablkcipher_walk *walk,
  149. struct crypto_tfm *tfm,
  150. unsigned int alignmask)
  151. {
  152. unsigned bs = walk->blocksize;
  153. unsigned int ivsize = tfm->crt_ablkcipher.ivsize;
  154. unsigned aligned_bs = ALIGN(bs, alignmask + 1);
  155. unsigned int size = aligned_bs * 2 + ivsize + max(aligned_bs, ivsize) -
  156. (alignmask + 1);
  157. u8 *iv;
  158. size += alignmask & ~(crypto_tfm_ctx_alignment() - 1);
  159. walk->iv_buffer = kmalloc(size, GFP_ATOMIC);
  160. if (!walk->iv_buffer)
  161. return -ENOMEM;
  162. iv = (u8 *)ALIGN((unsigned long)walk->iv_buffer, alignmask + 1);
  163. iv = ablkcipher_get_spot(iv, bs) + aligned_bs;
  164. iv = ablkcipher_get_spot(iv, bs) + aligned_bs;
  165. iv = ablkcipher_get_spot(iv, ivsize);
  166. walk->iv = memcpy(iv, walk->iv, ivsize);
  167. return 0;
  168. }
  169. static inline int ablkcipher_next_fast(struct ablkcipher_request *req,
  170. struct ablkcipher_walk *walk)
  171. {
  172. walk->src.page = scatterwalk_page(&walk->in);
  173. walk->src.offset = offset_in_page(walk->in.offset);
  174. walk->dst.page = scatterwalk_page(&walk->out);
  175. walk->dst.offset = offset_in_page(walk->out.offset);
  176. return 0;
  177. }
  178. static int ablkcipher_walk_next(struct ablkcipher_request *req,
  179. struct ablkcipher_walk *walk)
  180. {
  181. struct crypto_tfm *tfm = req->base.tfm;
  182. unsigned int alignmask, bsize, n;
  183. void *src, *dst;
  184. int err;
  185. alignmask = crypto_tfm_alg_alignmask(tfm);
  186. n = walk->total;
  187. if (unlikely(n < crypto_tfm_alg_blocksize(tfm))) {
  188. req->base.flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  189. return ablkcipher_walk_done(req, walk, -EINVAL);
  190. }
  191. walk->flags &= ~ABLKCIPHER_WALK_SLOW;
  192. src = dst = NULL;
  193. bsize = min(walk->blocksize, n);
  194. n = scatterwalk_clamp(&walk->in, n);
  195. n = scatterwalk_clamp(&walk->out, n);
  196. if (n < bsize ||
  197. !scatterwalk_aligned(&walk->in, alignmask) ||
  198. !scatterwalk_aligned(&walk->out, alignmask)) {
  199. err = ablkcipher_next_slow(req, walk, bsize, alignmask,
  200. &src, &dst);
  201. goto set_phys_lowmem;
  202. }
  203. walk->nbytes = n;
  204. return ablkcipher_next_fast(req, walk);
  205. set_phys_lowmem:
  206. if (err >= 0) {
  207. walk->src.page = virt_to_page(src);
  208. walk->dst.page = virt_to_page(dst);
  209. walk->src.offset = ((unsigned long)src & (PAGE_SIZE - 1));
  210. walk->dst.offset = ((unsigned long)dst & (PAGE_SIZE - 1));
  211. }
  212. return err;
  213. }
  214. static int ablkcipher_walk_first(struct ablkcipher_request *req,
  215. struct ablkcipher_walk *walk)
  216. {
  217. struct crypto_tfm *tfm = req->base.tfm;
  218. unsigned int alignmask;
  219. alignmask = crypto_tfm_alg_alignmask(tfm);
  220. if (WARN_ON_ONCE(in_irq()))
  221. return -EDEADLK;
  222. walk->iv = req->info;
  223. walk->nbytes = walk->total;
  224. if (unlikely(!walk->total))
  225. return 0;
  226. walk->iv_buffer = NULL;
  227. if (unlikely(((unsigned long)walk->iv & alignmask))) {
  228. int err = ablkcipher_copy_iv(walk, tfm, alignmask);
  229. if (err)
  230. return err;
  231. }
  232. scatterwalk_start(&walk->in, walk->in.sg);
  233. scatterwalk_start(&walk->out, walk->out.sg);
  234. return ablkcipher_walk_next(req, walk);
  235. }
  236. int ablkcipher_walk_phys(struct ablkcipher_request *req,
  237. struct ablkcipher_walk *walk)
  238. {
  239. walk->blocksize = crypto_tfm_alg_blocksize(req->base.tfm);
  240. return ablkcipher_walk_first(req, walk);
  241. }
  242. EXPORT_SYMBOL_GPL(ablkcipher_walk_phys);
  243. static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
  244. unsigned int keylen)
  245. {
  246. struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
  247. unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
  248. int ret;
  249. u8 *buffer, *alignbuffer;
  250. unsigned long absize;
  251. absize = keylen + alignmask;
  252. buffer = kmalloc(absize, GFP_ATOMIC);
  253. if (!buffer)
  254. return -ENOMEM;
  255. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  256. memcpy(alignbuffer, key, keylen);
  257. ret = cipher->setkey(tfm, alignbuffer, keylen);
  258. memset(alignbuffer, 0, keylen);
  259. kfree(buffer);
  260. return ret;
  261. }
  262. static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  263. unsigned int keylen)
  264. {
  265. struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
  266. unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
  267. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  268. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  269. return -EINVAL;
  270. }
  271. if ((unsigned long)key & alignmask)
  272. return setkey_unaligned(tfm, key, keylen);
  273. return cipher->setkey(tfm, key, keylen);
  274. }
  275. static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type,
  276. u32 mask)
  277. {
  278. return alg->cra_ctxsize;
  279. }
  280. static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
  281. u32 mask)
  282. {
  283. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  284. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  285. if (alg->ivsize > PAGE_SIZE / 8)
  286. return -EINVAL;
  287. crt->setkey = setkey;
  288. crt->encrypt = alg->encrypt;
  289. crt->decrypt = alg->decrypt;
  290. crt->base = __crypto_ablkcipher_cast(tfm);
  291. crt->ivsize = alg->ivsize;
  292. return 0;
  293. }
  294. #ifdef CONFIG_NET
  295. static int crypto_ablkcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  296. {
  297. struct crypto_report_blkcipher rblkcipher;
  298. strncpy(rblkcipher.type, "ablkcipher", sizeof(rblkcipher.type));
  299. strncpy(rblkcipher.geniv, alg->cra_ablkcipher.geniv ?: "<default>",
  300. sizeof(rblkcipher.geniv));
  301. rblkcipher.blocksize = alg->cra_blocksize;
  302. rblkcipher.min_keysize = alg->cra_ablkcipher.min_keysize;
  303. rblkcipher.max_keysize = alg->cra_ablkcipher.max_keysize;
  304. rblkcipher.ivsize = alg->cra_ablkcipher.ivsize;
  305. if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  306. sizeof(struct crypto_report_blkcipher), &rblkcipher))
  307. goto nla_put_failure;
  308. return 0;
  309. nla_put_failure:
  310. return -EMSGSIZE;
  311. }
  312. #else
  313. static int crypto_ablkcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  314. {
  315. return -ENOSYS;
  316. }
  317. #endif
  318. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  319. __maybe_unused;
  320. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  321. {
  322. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  323. seq_printf(m, "type : ablkcipher\n");
  324. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  325. "yes" : "no");
  326. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  327. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  328. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  329. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  330. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<default>");
  331. }
  332. const struct crypto_type crypto_ablkcipher_type = {
  333. .ctxsize = crypto_ablkcipher_ctxsize,
  334. .init = crypto_init_ablkcipher_ops,
  335. #ifdef CONFIG_PROC_FS
  336. .show = crypto_ablkcipher_show,
  337. #endif
  338. .report = crypto_ablkcipher_report,
  339. };
  340. EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
  341. static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
  342. u32 mask)
  343. {
  344. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  345. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  346. if (alg->ivsize > PAGE_SIZE / 8)
  347. return -EINVAL;
  348. crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
  349. alg->setkey : setkey;
  350. crt->encrypt = alg->encrypt;
  351. crt->decrypt = alg->decrypt;
  352. crt->base = __crypto_ablkcipher_cast(tfm);
  353. crt->ivsize = alg->ivsize;
  354. return 0;
  355. }
  356. #ifdef CONFIG_NET
  357. static int crypto_givcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  358. {
  359. struct crypto_report_blkcipher rblkcipher;
  360. strncpy(rblkcipher.type, "givcipher", sizeof(rblkcipher.type));
  361. strncpy(rblkcipher.geniv, alg->cra_ablkcipher.geniv ?: "<built-in>",
  362. sizeof(rblkcipher.geniv));
  363. rblkcipher.blocksize = alg->cra_blocksize;
  364. rblkcipher.min_keysize = alg->cra_ablkcipher.min_keysize;
  365. rblkcipher.max_keysize = alg->cra_ablkcipher.max_keysize;
  366. rblkcipher.ivsize = alg->cra_ablkcipher.ivsize;
  367. if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  368. sizeof(struct crypto_report_blkcipher), &rblkcipher))
  369. goto nla_put_failure;
  370. return 0;
  371. nla_put_failure:
  372. return -EMSGSIZE;
  373. }
  374. #else
  375. static int crypto_givcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  376. {
  377. return -ENOSYS;
  378. }
  379. #endif
  380. static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  381. __maybe_unused;
  382. static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  383. {
  384. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  385. seq_printf(m, "type : givcipher\n");
  386. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  387. "yes" : "no");
  388. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  389. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  390. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  391. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  392. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<built-in>");
  393. }
  394. const struct crypto_type crypto_givcipher_type = {
  395. .ctxsize = crypto_ablkcipher_ctxsize,
  396. .init = crypto_init_givcipher_ops,
  397. #ifdef CONFIG_PROC_FS
  398. .show = crypto_givcipher_show,
  399. #endif
  400. .report = crypto_givcipher_report,
  401. };
  402. EXPORT_SYMBOL_GPL(crypto_givcipher_type);