blkcipher.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. /* 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 *blkcipher_get_spot(u8 *start, unsigned int len)
  57. {
  58. u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
  59. return start > end_page ? start : end_page;
  60. }
  61. static inline unsigned int blkcipher_done_slow(struct crypto_blkcipher *tfm,
  62. struct blkcipher_walk *walk,
  63. unsigned int bsize)
  64. {
  65. u8 *addr;
  66. unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
  67. addr = (u8 *)ALIGN((unsigned long)walk->buffer, alignmask + 1);
  68. addr = blkcipher_get_spot(addr, bsize);
  69. scatterwalk_copychunks(addr, &walk->out, bsize, 1);
  70. return bsize;
  71. }
  72. static inline unsigned int blkcipher_done_fast(struct blkcipher_walk *walk,
  73. unsigned int n)
  74. {
  75. n = walk->nbytes - n;
  76. if (walk->flags & BLKCIPHER_WALK_COPY) {
  77. blkcipher_map_dst(walk);
  78. memcpy(walk->dst.virt.addr, walk->page, n);
  79. blkcipher_unmap_dst(walk);
  80. } else if (!(walk->flags & BLKCIPHER_WALK_PHYS)) {
  81. blkcipher_unmap_src(walk);
  82. if (walk->flags & BLKCIPHER_WALK_DIFF)
  83. blkcipher_unmap_dst(walk);
  84. }
  85. scatterwalk_advance(&walk->in, n);
  86. scatterwalk_advance(&walk->out, n);
  87. return n;
  88. }
  89. int blkcipher_walk_done(struct blkcipher_desc *desc,
  90. struct blkcipher_walk *walk, int err)
  91. {
  92. struct crypto_blkcipher *tfm = desc->tfm;
  93. unsigned int nbytes = 0;
  94. if (likely(err >= 0)) {
  95. unsigned int bsize = crypto_blkcipher_blocksize(tfm);
  96. unsigned int n;
  97. if (likely(!(walk->flags & BLKCIPHER_WALK_SLOW)))
  98. n = blkcipher_done_fast(walk, err);
  99. else
  100. n = blkcipher_done_slow(tfm, walk, bsize);
  101. nbytes = walk->total - n;
  102. err = 0;
  103. }
  104. scatterwalk_done(&walk->in, 0, nbytes);
  105. scatterwalk_done(&walk->out, 1, nbytes);
  106. walk->total = nbytes;
  107. walk->nbytes = nbytes;
  108. if (nbytes) {
  109. crypto_yield(desc->flags);
  110. return blkcipher_walk_next(desc, walk);
  111. }
  112. if (walk->iv != desc->info)
  113. memcpy(desc->info, walk->iv, crypto_blkcipher_ivsize(tfm));
  114. if (walk->buffer != walk->page)
  115. kfree(walk->buffer);
  116. if (walk->page)
  117. free_page((unsigned long)walk->page);
  118. return err;
  119. }
  120. EXPORT_SYMBOL_GPL(blkcipher_walk_done);
  121. static inline int blkcipher_next_slow(struct blkcipher_desc *desc,
  122. struct blkcipher_walk *walk,
  123. unsigned int bsize,
  124. unsigned int alignmask)
  125. {
  126. unsigned int n;
  127. if (walk->buffer)
  128. goto ok;
  129. walk->buffer = walk->page;
  130. if (walk->buffer)
  131. goto ok;
  132. n = bsize * 3 - (alignmask + 1) +
  133. (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  134. walk->buffer = kmalloc(n, GFP_ATOMIC);
  135. if (!walk->buffer)
  136. return blkcipher_walk_done(desc, walk, -ENOMEM);
  137. ok:
  138. walk->dst.virt.addr = (u8 *)ALIGN((unsigned long)walk->buffer,
  139. alignmask + 1);
  140. walk->dst.virt.addr = blkcipher_get_spot(walk->dst.virt.addr, bsize);
  141. walk->src.virt.addr = blkcipher_get_spot(walk->dst.virt.addr + bsize,
  142. bsize);
  143. scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
  144. walk->nbytes = bsize;
  145. walk->flags |= BLKCIPHER_WALK_SLOW;
  146. return 0;
  147. }
  148. static inline int blkcipher_next_copy(struct blkcipher_walk *walk)
  149. {
  150. u8 *tmp = walk->page;
  151. blkcipher_map_src(walk);
  152. memcpy(tmp, walk->src.virt.addr, walk->nbytes);
  153. blkcipher_unmap_src(walk);
  154. walk->src.virt.addr = tmp;
  155. walk->dst.virt.addr = tmp;
  156. return 0;
  157. }
  158. static inline int blkcipher_next_fast(struct blkcipher_desc *desc,
  159. struct blkcipher_walk *walk)
  160. {
  161. unsigned long diff;
  162. walk->src.phys.page = scatterwalk_page(&walk->in);
  163. walk->src.phys.offset = offset_in_page(walk->in.offset);
  164. walk->dst.phys.page = scatterwalk_page(&walk->out);
  165. walk->dst.phys.offset = offset_in_page(walk->out.offset);
  166. if (walk->flags & BLKCIPHER_WALK_PHYS)
  167. return 0;
  168. diff = walk->src.phys.offset - walk->dst.phys.offset;
  169. diff |= walk->src.virt.page - walk->dst.virt.page;
  170. blkcipher_map_src(walk);
  171. walk->dst.virt.addr = walk->src.virt.addr;
  172. if (diff) {
  173. walk->flags |= BLKCIPHER_WALK_DIFF;
  174. blkcipher_map_dst(walk);
  175. }
  176. return 0;
  177. }
  178. static int blkcipher_walk_next(struct blkcipher_desc *desc,
  179. struct blkcipher_walk *walk)
  180. {
  181. struct crypto_blkcipher *tfm = desc->tfm;
  182. unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
  183. unsigned int bsize = crypto_blkcipher_blocksize(tfm);
  184. unsigned int n;
  185. int err;
  186. n = walk->total;
  187. if (unlikely(n < bsize)) {
  188. desc->flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  189. return blkcipher_walk_done(desc, walk, -EINVAL);
  190. }
  191. walk->flags &= ~(BLKCIPHER_WALK_SLOW | BLKCIPHER_WALK_COPY |
  192. BLKCIPHER_WALK_DIFF);
  193. if (!scatterwalk_aligned(&walk->in, alignmask) ||
  194. !scatterwalk_aligned(&walk->out, alignmask)) {
  195. walk->flags |= BLKCIPHER_WALK_COPY;
  196. if (!walk->page) {
  197. walk->page = (void *)__get_free_page(GFP_ATOMIC);
  198. if (!walk->page)
  199. n = 0;
  200. }
  201. }
  202. n = scatterwalk_clamp(&walk->in, n);
  203. n = scatterwalk_clamp(&walk->out, n);
  204. if (unlikely(n < bsize)) {
  205. err = blkcipher_next_slow(desc, walk, bsize, alignmask);
  206. goto set_phys_lowmem;
  207. }
  208. walk->nbytes = n;
  209. if (walk->flags & BLKCIPHER_WALK_COPY) {
  210. err = blkcipher_next_copy(walk);
  211. goto set_phys_lowmem;
  212. }
  213. return blkcipher_next_fast(desc, walk);
  214. set_phys_lowmem:
  215. if (walk->flags & BLKCIPHER_WALK_PHYS) {
  216. walk->src.phys.page = virt_to_page(walk->src.virt.addr);
  217. walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
  218. walk->src.phys.offset &= PAGE_SIZE - 1;
  219. walk->dst.phys.offset &= PAGE_SIZE - 1;
  220. }
  221. return err;
  222. }
  223. static inline int blkcipher_copy_iv(struct blkcipher_walk *walk,
  224. struct crypto_blkcipher *tfm,
  225. unsigned int alignmask)
  226. {
  227. unsigned bs = crypto_blkcipher_blocksize(tfm);
  228. unsigned int ivsize = crypto_blkcipher_ivsize(tfm);
  229. unsigned int size = bs * 2 + ivsize + max(bs, ivsize) - (alignmask + 1);
  230. u8 *iv;
  231. size += alignmask & ~(crypto_tfm_ctx_alignment() - 1);
  232. walk->buffer = kmalloc(size, GFP_ATOMIC);
  233. if (!walk->buffer)
  234. return -ENOMEM;
  235. iv = (u8 *)ALIGN((unsigned long)walk->buffer, alignmask + 1);
  236. iv = blkcipher_get_spot(iv, bs) + bs;
  237. iv = blkcipher_get_spot(iv, bs) + bs;
  238. iv = blkcipher_get_spot(iv, ivsize);
  239. walk->iv = memcpy(iv, walk->iv, ivsize);
  240. return 0;
  241. }
  242. int blkcipher_walk_virt(struct blkcipher_desc *desc,
  243. struct blkcipher_walk *walk)
  244. {
  245. walk->flags &= ~BLKCIPHER_WALK_PHYS;
  246. return blkcipher_walk_first(desc, walk);
  247. }
  248. EXPORT_SYMBOL_GPL(blkcipher_walk_virt);
  249. int blkcipher_walk_phys(struct blkcipher_desc *desc,
  250. struct blkcipher_walk *walk)
  251. {
  252. walk->flags |= BLKCIPHER_WALK_PHYS;
  253. return blkcipher_walk_first(desc, walk);
  254. }
  255. EXPORT_SYMBOL_GPL(blkcipher_walk_phys);
  256. static int blkcipher_walk_first(struct blkcipher_desc *desc,
  257. struct blkcipher_walk *walk)
  258. {
  259. struct crypto_blkcipher *tfm = desc->tfm;
  260. unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
  261. if (WARN_ON_ONCE(in_irq()))
  262. return -EDEADLK;
  263. walk->nbytes = walk->total;
  264. if (unlikely(!walk->total))
  265. return 0;
  266. walk->buffer = NULL;
  267. walk->iv = desc->info;
  268. if (unlikely(((unsigned long)walk->iv & alignmask))) {
  269. int err = blkcipher_copy_iv(walk, tfm, alignmask);
  270. if (err)
  271. return err;
  272. }
  273. scatterwalk_start(&walk->in, walk->in.sg);
  274. scatterwalk_start(&walk->out, walk->out.sg);
  275. walk->page = NULL;
  276. return blkcipher_walk_next(desc, walk);
  277. }
  278. static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
  279. {
  280. struct blkcipher_alg *cipher = &tfm->__crt_alg->cra_blkcipher;
  281. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  282. int ret;
  283. u8 *buffer, *alignbuffer;
  284. unsigned long absize;
  285. absize = keylen + alignmask;
  286. buffer = kmalloc(absize, GFP_ATOMIC);
  287. if (!buffer)
  288. return -ENOMEM;
  289. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  290. memcpy(alignbuffer, key, keylen);
  291. ret = cipher->setkey(tfm, alignbuffer, keylen);
  292. memset(alignbuffer, 0, keylen);
  293. kfree(buffer);
  294. return ret;
  295. }
  296. static int setkey(struct crypto_tfm *tfm, const u8 *key,
  297. unsigned int keylen)
  298. {
  299. struct blkcipher_alg *cipher = &tfm->__crt_alg->cra_blkcipher;
  300. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  301. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  302. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  303. return -EINVAL;
  304. }
  305. if ((unsigned long)key & alignmask)
  306. return setkey_unaligned(tfm, key, keylen);
  307. return cipher->setkey(tfm, key, keylen);
  308. }
  309. static int async_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  310. unsigned int keylen)
  311. {
  312. return setkey(crypto_ablkcipher_tfm(tfm), key, keylen);
  313. }
  314. static int async_encrypt(struct ablkcipher_request *req)
  315. {
  316. struct crypto_tfm *tfm = req->base.tfm;
  317. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  318. struct blkcipher_desc desc = {
  319. .tfm = __crypto_blkcipher_cast(tfm),
  320. .info = req->info,
  321. .flags = req->base.flags,
  322. };
  323. return alg->encrypt(&desc, req->dst, req->src, req->nbytes);
  324. }
  325. static int async_decrypt(struct ablkcipher_request *req)
  326. {
  327. struct crypto_tfm *tfm = req->base.tfm;
  328. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  329. struct blkcipher_desc desc = {
  330. .tfm = __crypto_blkcipher_cast(tfm),
  331. .info = req->info,
  332. .flags = req->base.flags,
  333. };
  334. return alg->decrypt(&desc, req->dst, req->src, req->nbytes);
  335. }
  336. static unsigned int crypto_blkcipher_ctxsize(struct crypto_alg *alg, u32 type,
  337. u32 mask)
  338. {
  339. struct blkcipher_alg *cipher = &alg->cra_blkcipher;
  340. unsigned int len = alg->cra_ctxsize;
  341. type ^= CRYPTO_ALG_ASYNC;
  342. mask &= CRYPTO_ALG_ASYNC;
  343. if ((type & mask) && cipher->ivsize) {
  344. len = ALIGN(len, (unsigned long)alg->cra_alignmask + 1);
  345. len += cipher->ivsize;
  346. }
  347. return len;
  348. }
  349. static int crypto_init_blkcipher_ops_async(struct crypto_tfm *tfm)
  350. {
  351. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  352. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  353. crt->setkey = async_setkey;
  354. crt->encrypt = async_encrypt;
  355. crt->decrypt = async_decrypt;
  356. crt->ivsize = alg->ivsize;
  357. return 0;
  358. }
  359. static int crypto_init_blkcipher_ops_sync(struct crypto_tfm *tfm)
  360. {
  361. struct blkcipher_tfm *crt = &tfm->crt_blkcipher;
  362. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  363. unsigned long align = crypto_tfm_alg_alignmask(tfm) + 1;
  364. unsigned long addr;
  365. crt->setkey = setkey;
  366. crt->encrypt = alg->encrypt;
  367. crt->decrypt = alg->decrypt;
  368. addr = (unsigned long)crypto_tfm_ctx(tfm);
  369. addr = ALIGN(addr, align);
  370. addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);
  371. crt->iv = (void *)addr;
  372. return 0;
  373. }
  374. static int crypto_init_blkcipher_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  375. {
  376. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  377. if (alg->ivsize > PAGE_SIZE / 8)
  378. return -EINVAL;
  379. type ^= CRYPTO_ALG_ASYNC;
  380. mask &= CRYPTO_ALG_ASYNC;
  381. if (type & mask)
  382. return crypto_init_blkcipher_ops_sync(tfm);
  383. else
  384. return crypto_init_blkcipher_ops_async(tfm);
  385. }
  386. static void crypto_blkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  387. __attribute__ ((unused));
  388. static void crypto_blkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  389. {
  390. seq_printf(m, "type : blkcipher\n");
  391. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  392. seq_printf(m, "min keysize : %u\n", alg->cra_blkcipher.min_keysize);
  393. seq_printf(m, "max keysize : %u\n", alg->cra_blkcipher.max_keysize);
  394. seq_printf(m, "ivsize : %u\n", alg->cra_blkcipher.ivsize);
  395. }
  396. const struct crypto_type crypto_blkcipher_type = {
  397. .ctxsize = crypto_blkcipher_ctxsize,
  398. .init = crypto_init_blkcipher_ops,
  399. #ifdef CONFIG_PROC_FS
  400. .show = crypto_blkcipher_show,
  401. #endif
  402. };
  403. EXPORT_SYMBOL_GPL(crypto_blkcipher_type);
  404. MODULE_LICENSE("GPL");
  405. MODULE_DESCRIPTION("Generic block chaining cipher type");