blkcipher.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 <linux/crypto.h>
  17. #include <linux/errno.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include "internal.h"
  25. #include "scatterwalk.h"
  26. enum {
  27. BLKCIPHER_WALK_PHYS = 1 << 0,
  28. BLKCIPHER_WALK_SLOW = 1 << 1,
  29. BLKCIPHER_WALK_COPY = 1 << 2,
  30. BLKCIPHER_WALK_DIFF = 1 << 3,
  31. };
  32. static int blkcipher_walk_next(struct blkcipher_desc *desc,
  33. struct blkcipher_walk *walk);
  34. static int blkcipher_walk_first(struct blkcipher_desc *desc,
  35. struct blkcipher_walk *walk);
  36. static inline void blkcipher_map_src(struct blkcipher_walk *walk)
  37. {
  38. walk->src.virt.addr = scatterwalk_map(&walk->in, 0);
  39. }
  40. static inline void blkcipher_map_dst(struct blkcipher_walk *walk)
  41. {
  42. walk->dst.virt.addr = scatterwalk_map(&walk->out, 1);
  43. }
  44. static inline void blkcipher_unmap_src(struct blkcipher_walk *walk)
  45. {
  46. scatterwalk_unmap(walk->src.virt.addr, 0);
  47. }
  48. static inline void blkcipher_unmap_dst(struct blkcipher_walk *walk)
  49. {
  50. scatterwalk_unmap(walk->dst.virt.addr, 1);
  51. }
  52. static inline u8 *blkcipher_get_spot(u8 *start, unsigned int len)
  53. {
  54. if (offset_in_page(start + len) < len)
  55. return (u8 *)((unsigned long)(start + len) & PAGE_MASK);
  56. return start;
  57. }
  58. static inline unsigned int blkcipher_done_slow(struct crypto_blkcipher *tfm,
  59. struct blkcipher_walk *walk,
  60. unsigned int bsize)
  61. {
  62. u8 *addr;
  63. unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
  64. addr = (u8 *)ALIGN((unsigned long)walk->buffer, alignmask + 1);
  65. addr = blkcipher_get_spot(addr, bsize);
  66. scatterwalk_copychunks(addr, &walk->out, bsize, 1);
  67. return bsize;
  68. }
  69. static inline unsigned int blkcipher_done_fast(struct blkcipher_walk *walk,
  70. unsigned int n)
  71. {
  72. n = walk->nbytes - n;
  73. if (walk->flags & BLKCIPHER_WALK_COPY) {
  74. blkcipher_map_dst(walk);
  75. memcpy(walk->dst.virt.addr, walk->page, n);
  76. blkcipher_unmap_dst(walk);
  77. } else if (!(walk->flags & BLKCIPHER_WALK_PHYS)) {
  78. blkcipher_unmap_src(walk);
  79. if (walk->flags & BLKCIPHER_WALK_DIFF)
  80. blkcipher_unmap_dst(walk);
  81. }
  82. scatterwalk_advance(&walk->in, n);
  83. scatterwalk_advance(&walk->out, n);
  84. return n;
  85. }
  86. int blkcipher_walk_done(struct blkcipher_desc *desc,
  87. struct blkcipher_walk *walk, int err)
  88. {
  89. struct crypto_blkcipher *tfm = desc->tfm;
  90. unsigned int nbytes = 0;
  91. if (likely(err >= 0)) {
  92. unsigned int bsize = crypto_blkcipher_blocksize(tfm);
  93. unsigned int n;
  94. if (likely(!(walk->flags & BLKCIPHER_WALK_SLOW)))
  95. n = blkcipher_done_fast(walk, err);
  96. else
  97. n = blkcipher_done_slow(tfm, walk, bsize);
  98. nbytes = walk->total - n;
  99. err = 0;
  100. }
  101. scatterwalk_done(&walk->in, 0, nbytes);
  102. scatterwalk_done(&walk->out, 1, nbytes);
  103. walk->total = nbytes;
  104. walk->nbytes = nbytes;
  105. if (nbytes) {
  106. crypto_yield(desc->flags);
  107. return blkcipher_walk_next(desc, walk);
  108. }
  109. if (walk->iv != desc->info)
  110. memcpy(desc->info, walk->iv, crypto_blkcipher_ivsize(tfm));
  111. if (walk->buffer != walk->page)
  112. kfree(walk->buffer);
  113. if (walk->page)
  114. free_page((unsigned long)walk->page);
  115. return err;
  116. }
  117. EXPORT_SYMBOL_GPL(blkcipher_walk_done);
  118. static inline int blkcipher_next_slow(struct blkcipher_desc *desc,
  119. struct blkcipher_walk *walk,
  120. unsigned int bsize,
  121. unsigned int alignmask)
  122. {
  123. unsigned int n;
  124. if (walk->buffer)
  125. goto ok;
  126. walk->buffer = walk->page;
  127. if (walk->buffer)
  128. goto ok;
  129. n = bsize * 2 + (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  130. walk->buffer = kmalloc(n, GFP_ATOMIC);
  131. if (!walk->buffer)
  132. return blkcipher_walk_done(desc, walk, -ENOMEM);
  133. ok:
  134. walk->dst.virt.addr = (u8 *)ALIGN((unsigned long)walk->buffer,
  135. alignmask + 1);
  136. walk->dst.virt.addr = blkcipher_get_spot(walk->dst.virt.addr, bsize);
  137. walk->src.virt.addr = blkcipher_get_spot(walk->dst.virt.addr + bsize,
  138. bsize);
  139. scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
  140. walk->nbytes = bsize;
  141. walk->flags |= BLKCIPHER_WALK_SLOW;
  142. return 0;
  143. }
  144. static inline int blkcipher_next_copy(struct blkcipher_walk *walk)
  145. {
  146. u8 *tmp = walk->page;
  147. blkcipher_map_src(walk);
  148. memcpy(tmp, walk->src.virt.addr, walk->nbytes);
  149. blkcipher_unmap_src(walk);
  150. walk->src.virt.addr = tmp;
  151. walk->dst.virt.addr = tmp;
  152. return 0;
  153. }
  154. static inline int blkcipher_next_fast(struct blkcipher_desc *desc,
  155. struct blkcipher_walk *walk)
  156. {
  157. unsigned long diff;
  158. walk->src.phys.page = scatterwalk_page(&walk->in);
  159. walk->src.phys.offset = offset_in_page(walk->in.offset);
  160. walk->dst.phys.page = scatterwalk_page(&walk->out);
  161. walk->dst.phys.offset = offset_in_page(walk->out.offset);
  162. if (walk->flags & BLKCIPHER_WALK_PHYS)
  163. return 0;
  164. diff = walk->src.phys.offset - walk->dst.phys.offset;
  165. diff |= walk->src.virt.page - walk->dst.virt.page;
  166. blkcipher_map_src(walk);
  167. walk->dst.virt.addr = walk->src.virt.addr;
  168. if (diff) {
  169. walk->flags |= BLKCIPHER_WALK_DIFF;
  170. blkcipher_map_dst(walk);
  171. }
  172. return 0;
  173. }
  174. static int blkcipher_walk_next(struct blkcipher_desc *desc,
  175. struct blkcipher_walk *walk)
  176. {
  177. struct crypto_blkcipher *tfm = desc->tfm;
  178. unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
  179. unsigned int bsize = crypto_blkcipher_blocksize(tfm);
  180. unsigned int n;
  181. int err;
  182. n = walk->total;
  183. if (unlikely(n < bsize)) {
  184. desc->flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  185. return blkcipher_walk_done(desc, walk, -EINVAL);
  186. }
  187. walk->flags &= ~(BLKCIPHER_WALK_SLOW | BLKCIPHER_WALK_COPY |
  188. BLKCIPHER_WALK_DIFF);
  189. if (!scatterwalk_aligned(&walk->in, alignmask) ||
  190. !scatterwalk_aligned(&walk->out, alignmask)) {
  191. walk->flags |= BLKCIPHER_WALK_COPY;
  192. if (!walk->page) {
  193. walk->page = (void *)__get_free_page(GFP_ATOMIC);
  194. if (!walk->page)
  195. n = 0;
  196. }
  197. }
  198. n = scatterwalk_clamp(&walk->in, n);
  199. n = scatterwalk_clamp(&walk->out, n);
  200. if (unlikely(n < bsize)) {
  201. err = blkcipher_next_slow(desc, walk, bsize, alignmask);
  202. goto set_phys_lowmem;
  203. }
  204. walk->nbytes = n;
  205. if (walk->flags & BLKCIPHER_WALK_COPY) {
  206. err = blkcipher_next_copy(walk);
  207. goto set_phys_lowmem;
  208. }
  209. return blkcipher_next_fast(desc, walk);
  210. set_phys_lowmem:
  211. if (walk->flags & BLKCIPHER_WALK_PHYS) {
  212. walk->src.phys.page = virt_to_page(walk->src.virt.addr);
  213. walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
  214. walk->src.phys.offset &= PAGE_SIZE - 1;
  215. walk->dst.phys.offset &= PAGE_SIZE - 1;
  216. }
  217. return err;
  218. }
  219. static inline int blkcipher_copy_iv(struct blkcipher_walk *walk,
  220. struct crypto_blkcipher *tfm,
  221. unsigned int alignmask)
  222. {
  223. unsigned bs = crypto_blkcipher_blocksize(tfm);
  224. unsigned int ivsize = crypto_blkcipher_ivsize(tfm);
  225. unsigned int size = bs * 2 + ivsize + max(bs, ivsize) - (alignmask + 1);
  226. u8 *iv;
  227. size += alignmask & ~(crypto_tfm_ctx_alignment() - 1);
  228. walk->buffer = kmalloc(size, GFP_ATOMIC);
  229. if (!walk->buffer)
  230. return -ENOMEM;
  231. iv = (u8 *)ALIGN((unsigned long)walk->buffer, alignmask + 1);
  232. iv = blkcipher_get_spot(iv, bs) + bs;
  233. iv = blkcipher_get_spot(iv, bs) + bs;
  234. iv = blkcipher_get_spot(iv, ivsize);
  235. walk->iv = memcpy(iv, walk->iv, ivsize);
  236. return 0;
  237. }
  238. int blkcipher_walk_virt(struct blkcipher_desc *desc,
  239. struct blkcipher_walk *walk)
  240. {
  241. walk->flags &= ~BLKCIPHER_WALK_PHYS;
  242. return blkcipher_walk_first(desc, walk);
  243. }
  244. EXPORT_SYMBOL_GPL(blkcipher_walk_virt);
  245. int blkcipher_walk_phys(struct blkcipher_desc *desc,
  246. struct blkcipher_walk *walk)
  247. {
  248. walk->flags |= BLKCIPHER_WALK_PHYS;
  249. return blkcipher_walk_first(desc, walk);
  250. }
  251. EXPORT_SYMBOL_GPL(blkcipher_walk_phys);
  252. static int blkcipher_walk_first(struct blkcipher_desc *desc,
  253. struct blkcipher_walk *walk)
  254. {
  255. struct crypto_blkcipher *tfm = desc->tfm;
  256. unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
  257. walk->nbytes = walk->total;
  258. if (unlikely(!walk->total))
  259. return 0;
  260. walk->buffer = NULL;
  261. walk->iv = desc->info;
  262. if (unlikely(((unsigned long)walk->iv & alignmask))) {
  263. int err = blkcipher_copy_iv(walk, tfm, alignmask);
  264. if (err)
  265. return err;
  266. }
  267. scatterwalk_start(&walk->in, walk->in.sg);
  268. scatterwalk_start(&walk->out, walk->out.sg);
  269. walk->page = NULL;
  270. return blkcipher_walk_next(desc, walk);
  271. }
  272. static int setkey(struct crypto_tfm *tfm, const u8 *key,
  273. unsigned int keylen)
  274. {
  275. struct blkcipher_alg *cipher = &tfm->__crt_alg->cra_blkcipher;
  276. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  277. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  278. return -EINVAL;
  279. }
  280. return cipher->setkey(tfm, key, keylen);
  281. }
  282. static unsigned int crypto_blkcipher_ctxsize(struct crypto_alg *alg)
  283. {
  284. struct blkcipher_alg *cipher = &alg->cra_blkcipher;
  285. unsigned int len = alg->cra_ctxsize;
  286. if (cipher->ivsize) {
  287. len = ALIGN(len, (unsigned long)alg->cra_alignmask + 1);
  288. len += cipher->ivsize;
  289. }
  290. return len;
  291. }
  292. static int crypto_init_blkcipher_ops(struct crypto_tfm *tfm)
  293. {
  294. struct blkcipher_tfm *crt = &tfm->crt_blkcipher;
  295. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  296. unsigned long align = crypto_tfm_alg_alignmask(tfm) + 1;
  297. unsigned long addr;
  298. if (alg->ivsize > PAGE_SIZE / 8)
  299. return -EINVAL;
  300. crt->setkey = setkey;
  301. crt->encrypt = alg->encrypt;
  302. crt->decrypt = alg->decrypt;
  303. addr = (unsigned long)crypto_tfm_ctx(tfm);
  304. addr = ALIGN(addr, align);
  305. addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);
  306. crt->iv = (void *)addr;
  307. return 0;
  308. }
  309. static void crypto_blkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  310. __attribute_used__;
  311. static void crypto_blkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  312. {
  313. seq_printf(m, "type : blkcipher\n");
  314. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  315. seq_printf(m, "min keysize : %u\n", alg->cra_blkcipher.min_keysize);
  316. seq_printf(m, "max keysize : %u\n", alg->cra_blkcipher.max_keysize);
  317. seq_printf(m, "ivsize : %u\n", alg->cra_blkcipher.ivsize);
  318. }
  319. const struct crypto_type crypto_blkcipher_type = {
  320. .ctxsize = crypto_blkcipher_ctxsize,
  321. .init = crypto_init_blkcipher_ops,
  322. #ifdef CONFIG_PROC_FS
  323. .show = crypto_blkcipher_show,
  324. #endif
  325. };
  326. EXPORT_SYMBOL_GPL(crypto_blkcipher_type);
  327. MODULE_LICENSE("GPL");
  328. MODULE_DESCRIPTION("Generic block chaining cipher type");