ablkcipher.c 13 KB

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