blkcipher.c 10 KB

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