ablkcipher.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 <[email protected]>
  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 <net/netlink.h>
  22. #include <crypto/scatterwalk.h>
  23. #include "internal.h"
  24. struct ablkcipher_buffer {
  25. struct list_head entry;
  26. struct scatter_walk dst;
  27. unsigned int len;
  28. void *data;
  29. };
  30. enum {
  31. ABLKCIPHER_WALK_SLOW = 1 << 0,
  32. };
  33. static inline void ablkcipher_buffer_write(struct ablkcipher_buffer *p)
  34. {
  35. scatterwalk_copychunks(p->data, &p->dst, p->len, 1);
  36. }
  37. void __ablkcipher_walk_complete(struct ablkcipher_walk *walk)
  38. {
  39. struct ablkcipher_buffer *p, *tmp;
  40. list_for_each_entry_safe(p, tmp, &walk->buffers, entry) {
  41. ablkcipher_buffer_write(p);
  42. list_del(&p->entry);
  43. kfree(p);
  44. }
  45. }
  46. EXPORT_SYMBOL_GPL(__ablkcipher_walk_complete);
  47. static inline void ablkcipher_queue_write(struct ablkcipher_walk *walk,
  48. struct ablkcipher_buffer *p)
  49. {
  50. p->dst = walk->out;
  51. list_add_tail(&p->entry, &walk->buffers);
  52. }
  53. /* Get a spot of the specified length that does not straddle a page.
  54. * The caller needs to ensure that there is enough space for this operation.
  55. */
  56. static inline u8 *ablkcipher_get_spot(u8 *start, unsigned int len)
  57. {
  58. u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
  59. return max(start, end_page);
  60. }
  61. static inline void ablkcipher_done_slow(struct ablkcipher_walk *walk,
  62. unsigned int n)
  63. {
  64. for (;;) {
  65. unsigned int len_this_page = scatterwalk_pagelen(&walk->out);
  66. if (len_this_page > n)
  67. len_this_page = n;
  68. scatterwalk_advance(&walk->out, n);
  69. if (n == len_this_page)
  70. break;
  71. n -= len_this_page;
  72. scatterwalk_start(&walk->out, sg_next(walk->out.sg));
  73. }
  74. }
  75. static inline void ablkcipher_done_fast(struct ablkcipher_walk *walk,
  76. unsigned int n)
  77. {
  78. scatterwalk_advance(&walk->in, n);
  79. scatterwalk_advance(&walk->out, n);
  80. }
  81. static int ablkcipher_walk_next(struct ablkcipher_request *req,
  82. struct ablkcipher_walk *walk);
  83. int ablkcipher_walk_done(struct ablkcipher_request *req,
  84. struct ablkcipher_walk *walk, int err)
  85. {
  86. struct crypto_tfm *tfm = req->base.tfm;
  87. unsigned int n; /* bytes processed */
  88. bool more;
  89. if (unlikely(err < 0))
  90. goto finish;
  91. n = walk->nbytes - err;
  92. walk->total -= n;
  93. more = (walk->total != 0);
  94. if (likely(!(walk->flags & ABLKCIPHER_WALK_SLOW))) {
  95. ablkcipher_done_fast(walk, n);
  96. } else {
  97. if (WARN_ON(err)) {
  98. /* unexpected case; didn't process all bytes */
  99. err = -EINVAL;
  100. goto finish;
  101. }
  102. ablkcipher_done_slow(walk, n);
  103. }
  104. scatterwalk_done(&walk->in, 0, more);
  105. scatterwalk_done(&walk->out, 1, more);
  106. if (more) {
  107. crypto_yield(req->base.flags);
  108. return ablkcipher_walk_next(req, walk);
  109. }
  110. err = 0;
  111. finish:
  112. walk->nbytes = 0;
  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.geniv[sizeof(rblkcipher.geniv) - 1] = '\0';
  302. rblkcipher.blocksize = alg->cra_blocksize;
  303. rblkcipher.min_keysize = alg->cra_ablkcipher.min_keysize;
  304. rblkcipher.max_keysize = alg->cra_ablkcipher.max_keysize;
  305. rblkcipher.ivsize = alg->cra_ablkcipher.ivsize;
  306. if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  307. sizeof(struct crypto_report_blkcipher), &rblkcipher))
  308. goto nla_put_failure;
  309. return 0;
  310. nla_put_failure:
  311. return -EMSGSIZE;
  312. }
  313. #else
  314. static int crypto_ablkcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  315. {
  316. return -ENOSYS;
  317. }
  318. #endif
  319. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  320. __attribute__ ((unused));
  321. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  322. {
  323. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  324. seq_printf(m, "type : ablkcipher\n");
  325. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  326. "yes" : "no");
  327. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  328. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  329. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  330. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  331. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<default>");
  332. }
  333. const struct crypto_type crypto_ablkcipher_type = {
  334. .ctxsize = crypto_ablkcipher_ctxsize,
  335. .init = crypto_init_ablkcipher_ops,
  336. #ifdef CONFIG_PROC_FS
  337. .show = crypto_ablkcipher_show,
  338. #endif
  339. .report = crypto_ablkcipher_report,
  340. };
  341. EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
  342. static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
  343. u32 mask)
  344. {
  345. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  346. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  347. if (alg->ivsize > PAGE_SIZE / 8)
  348. return -EINVAL;
  349. crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
  350. alg->setkey : setkey;
  351. crt->encrypt = alg->encrypt;
  352. crt->decrypt = alg->decrypt;
  353. crt->base = __crypto_ablkcipher_cast(tfm);
  354. crt->ivsize = alg->ivsize;
  355. return 0;
  356. }
  357. #ifdef CONFIG_NET
  358. static int crypto_givcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  359. {
  360. struct crypto_report_blkcipher rblkcipher;
  361. strncpy(rblkcipher.type, "givcipher", sizeof(rblkcipher.type));
  362. strncpy(rblkcipher.geniv, alg->cra_ablkcipher.geniv ?: "<built-in>",
  363. sizeof(rblkcipher.geniv));
  364. rblkcipher.geniv[sizeof(rblkcipher.geniv) - 1] = '\0';
  365. rblkcipher.blocksize = alg->cra_blocksize;
  366. rblkcipher.min_keysize = alg->cra_ablkcipher.min_keysize;
  367. rblkcipher.max_keysize = alg->cra_ablkcipher.max_keysize;
  368. rblkcipher.ivsize = alg->cra_ablkcipher.ivsize;
  369. if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  370. sizeof(struct crypto_report_blkcipher), &rblkcipher))
  371. goto nla_put_failure;
  372. return 0;
  373. nla_put_failure:
  374. return -EMSGSIZE;
  375. }
  376. #else
  377. static int crypto_givcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  378. {
  379. return -ENOSYS;
  380. }
  381. #endif
  382. static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  383. __attribute__ ((unused));
  384. static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  385. {
  386. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  387. seq_printf(m, "type : givcipher\n");
  388. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  389. "yes" : "no");
  390. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  391. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  392. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  393. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  394. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<built-in>");
  395. }
  396. const struct crypto_type crypto_givcipher_type = {
  397. .ctxsize = crypto_ablkcipher_ctxsize,
  398. .init = crypto_init_givcipher_ops,
  399. #ifdef CONFIG_PROC_FS
  400. .show = crypto_givcipher_show,
  401. #endif
  402. .report = crypto_givcipher_report,
  403. };
  404. EXPORT_SYMBOL_GPL(crypto_givcipher_type);