blkcipher.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 <crypto/aead.h>
  17. #include <crypto/internal/skcipher.h>
  18. #include <crypto/scatterwalk.h>
  19. #include <linux/errno.h>
  20. #include <linux/hardirq.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/slab.h>
  25. #include <linux/string.h>
  26. #include <linux/cryptouser.h>
  27. #include <net/netlink.h>
  28. #include "internal.h"
  29. enum {
  30. BLKCIPHER_WALK_PHYS = 1 << 0,
  31. BLKCIPHER_WALK_SLOW = 1 << 1,
  32. BLKCIPHER_WALK_COPY = 1 << 2,
  33. BLKCIPHER_WALK_DIFF = 1 << 3,
  34. };
  35. static int blkcipher_walk_next(struct blkcipher_desc *desc,
  36. struct blkcipher_walk *walk);
  37. static int blkcipher_walk_first(struct blkcipher_desc *desc,
  38. struct blkcipher_walk *walk);
  39. static inline void blkcipher_map_src(struct blkcipher_walk *walk)
  40. {
  41. walk->src.virt.addr = scatterwalk_map(&walk->in);
  42. }
  43. static inline void blkcipher_map_dst(struct blkcipher_walk *walk)
  44. {
  45. walk->dst.virt.addr = scatterwalk_map(&walk->out);
  46. }
  47. static inline void blkcipher_unmap_src(struct blkcipher_walk *walk)
  48. {
  49. scatterwalk_unmap(walk->src.virt.addr);
  50. }
  51. static inline void blkcipher_unmap_dst(struct blkcipher_walk *walk)
  52. {
  53. scatterwalk_unmap(walk->dst.virt.addr);
  54. }
  55. /* Get a spot of the specified length that does not straddle a page.
  56. * The caller needs to ensure that there is enough space for this operation.
  57. */
  58. static inline u8 *blkcipher_get_spot(u8 *start, unsigned int len)
  59. {
  60. u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
  61. return max(start, end_page);
  62. }
  63. static inline unsigned int blkcipher_done_slow(struct blkcipher_walk *walk,
  64. unsigned int bsize)
  65. {
  66. u8 *addr;
  67. addr = (u8 *)ALIGN((unsigned long)walk->buffer, walk->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. if (walk->flags & BLKCIPHER_WALK_COPY) {
  76. blkcipher_map_dst(walk);
  77. memcpy(walk->dst.virt.addr, walk->page, n);
  78. blkcipher_unmap_dst(walk);
  79. } else if (!(walk->flags & BLKCIPHER_WALK_PHYS)) {
  80. if (walk->flags & BLKCIPHER_WALK_DIFF)
  81. blkcipher_unmap_dst(walk);
  82. blkcipher_unmap_src(walk);
  83. }
  84. scatterwalk_advance(&walk->in, n);
  85. scatterwalk_advance(&walk->out, n);
  86. return n;
  87. }
  88. int blkcipher_walk_done(struct blkcipher_desc *desc,
  89. struct blkcipher_walk *walk, int err)
  90. {
  91. unsigned int nbytes = 0;
  92. if (likely(err >= 0)) {
  93. unsigned int n = walk->nbytes - err;
  94. if (likely(!(walk->flags & BLKCIPHER_WALK_SLOW)))
  95. n = blkcipher_done_fast(walk, n);
  96. else if (WARN_ON(err)) {
  97. err = -EINVAL;
  98. goto err;
  99. } else
  100. n = blkcipher_done_slow(walk, n);
  101. nbytes = walk->total - n;
  102. err = 0;
  103. }
  104. scatterwalk_done(&walk->in, 0, nbytes);
  105. scatterwalk_done(&walk->out, 1, nbytes);
  106. err:
  107. walk->total = nbytes;
  108. walk->nbytes = nbytes;
  109. if (nbytes) {
  110. crypto_yield(desc->flags);
  111. return blkcipher_walk_next(desc, walk);
  112. }
  113. if (walk->iv != desc->info)
  114. memcpy(desc->info, walk->iv, walk->ivsize);
  115. if (walk->buffer != walk->page)
  116. kfree(walk->buffer);
  117. if (walk->page)
  118. free_page((unsigned long)walk->page);
  119. return err;
  120. }
  121. EXPORT_SYMBOL_GPL(blkcipher_walk_done);
  122. static inline int blkcipher_next_slow(struct blkcipher_desc *desc,
  123. struct blkcipher_walk *walk,
  124. unsigned int bsize,
  125. unsigned int alignmask)
  126. {
  127. unsigned int n;
  128. unsigned aligned_bsize = ALIGN(bsize, alignmask + 1);
  129. if (walk->buffer)
  130. goto ok;
  131. walk->buffer = walk->page;
  132. if (walk->buffer)
  133. goto ok;
  134. n = aligned_bsize * 3 - (alignmask + 1) +
  135. (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  136. walk->buffer = kmalloc(n, GFP_ATOMIC);
  137. if (!walk->buffer)
  138. return blkcipher_walk_done(desc, walk, -ENOMEM);
  139. ok:
  140. walk->dst.virt.addr = (u8 *)ALIGN((unsigned long)walk->buffer,
  141. alignmask + 1);
  142. walk->dst.virt.addr = blkcipher_get_spot(walk->dst.virt.addr, bsize);
  143. walk->src.virt.addr = blkcipher_get_spot(walk->dst.virt.addr +
  144. aligned_bsize, bsize);
  145. scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
  146. walk->nbytes = bsize;
  147. walk->flags |= BLKCIPHER_WALK_SLOW;
  148. return 0;
  149. }
  150. static inline int blkcipher_next_copy(struct blkcipher_walk *walk)
  151. {
  152. u8 *tmp = walk->page;
  153. blkcipher_map_src(walk);
  154. memcpy(tmp, walk->src.virt.addr, walk->nbytes);
  155. blkcipher_unmap_src(walk);
  156. walk->src.virt.addr = tmp;
  157. walk->dst.virt.addr = tmp;
  158. return 0;
  159. }
  160. static inline int blkcipher_next_fast(struct blkcipher_desc *desc,
  161. struct blkcipher_walk *walk)
  162. {
  163. unsigned long diff;
  164. walk->src.phys.page = scatterwalk_page(&walk->in);
  165. walk->src.phys.offset = offset_in_page(walk->in.offset);
  166. walk->dst.phys.page = scatterwalk_page(&walk->out);
  167. walk->dst.phys.offset = offset_in_page(walk->out.offset);
  168. if (walk->flags & BLKCIPHER_WALK_PHYS)
  169. return 0;
  170. diff = walk->src.phys.offset - walk->dst.phys.offset;
  171. diff |= walk->src.virt.page - walk->dst.virt.page;
  172. blkcipher_map_src(walk);
  173. walk->dst.virt.addr = walk->src.virt.addr;
  174. if (diff) {
  175. walk->flags |= BLKCIPHER_WALK_DIFF;
  176. blkcipher_map_dst(walk);
  177. }
  178. return 0;
  179. }
  180. static int blkcipher_walk_next(struct blkcipher_desc *desc,
  181. struct blkcipher_walk *walk)
  182. {
  183. unsigned int bsize;
  184. unsigned int n;
  185. int err;
  186. n = walk->total;
  187. if (unlikely(n < walk->cipher_blocksize)) {
  188. desc->flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  189. return blkcipher_walk_done(desc, walk, -EINVAL);
  190. }
  191. bsize = min(walk->walk_blocksize, n);
  192. walk->flags &= ~(BLKCIPHER_WALK_SLOW | BLKCIPHER_WALK_COPY |
  193. BLKCIPHER_WALK_DIFF);
  194. if (!scatterwalk_aligned(&walk->in, walk->alignmask) ||
  195. !scatterwalk_aligned(&walk->out, walk->alignmask)) {
  196. walk->flags |= BLKCIPHER_WALK_COPY;
  197. if (!walk->page) {
  198. walk->page = (void *)__get_free_page(GFP_ATOMIC);
  199. if (!walk->page)
  200. n = 0;
  201. }
  202. }
  203. n = scatterwalk_clamp(&walk->in, n);
  204. n = scatterwalk_clamp(&walk->out, n);
  205. if (unlikely(n < bsize)) {
  206. err = blkcipher_next_slow(desc, walk, bsize, walk->alignmask);
  207. goto set_phys_lowmem;
  208. }
  209. walk->nbytes = n;
  210. if (walk->flags & BLKCIPHER_WALK_COPY) {
  211. err = blkcipher_next_copy(walk);
  212. goto set_phys_lowmem;
  213. }
  214. return blkcipher_next_fast(desc, walk);
  215. set_phys_lowmem:
  216. if (walk->flags & BLKCIPHER_WALK_PHYS) {
  217. walk->src.phys.page = virt_to_page(walk->src.virt.addr);
  218. walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
  219. walk->src.phys.offset &= PAGE_SIZE - 1;
  220. walk->dst.phys.offset &= PAGE_SIZE - 1;
  221. }
  222. return err;
  223. }
  224. static inline int blkcipher_copy_iv(struct blkcipher_walk *walk)
  225. {
  226. unsigned bs = walk->walk_blocksize;
  227. unsigned aligned_bs = ALIGN(bs, walk->alignmask + 1);
  228. unsigned int size = aligned_bs * 2 +
  229. walk->ivsize + max(aligned_bs, walk->ivsize) -
  230. (walk->alignmask + 1);
  231. u8 *iv;
  232. size += walk->alignmask & ~(crypto_tfm_ctx_alignment() - 1);
  233. walk->buffer = kmalloc(size, GFP_ATOMIC);
  234. if (!walk->buffer)
  235. return -ENOMEM;
  236. iv = (u8 *)ALIGN((unsigned long)walk->buffer, walk->alignmask + 1);
  237. iv = blkcipher_get_spot(iv, bs) + aligned_bs;
  238. iv = blkcipher_get_spot(iv, bs) + aligned_bs;
  239. iv = blkcipher_get_spot(iv, walk->ivsize);
  240. walk->iv = memcpy(iv, walk->iv, walk->ivsize);
  241. return 0;
  242. }
  243. int blkcipher_walk_virt(struct blkcipher_desc *desc,
  244. struct blkcipher_walk *walk)
  245. {
  246. walk->flags &= ~BLKCIPHER_WALK_PHYS;
  247. walk->walk_blocksize = crypto_blkcipher_blocksize(desc->tfm);
  248. walk->cipher_blocksize = walk->walk_blocksize;
  249. walk->ivsize = crypto_blkcipher_ivsize(desc->tfm);
  250. walk->alignmask = crypto_blkcipher_alignmask(desc->tfm);
  251. return blkcipher_walk_first(desc, walk);
  252. }
  253. EXPORT_SYMBOL_GPL(blkcipher_walk_virt);
  254. int blkcipher_walk_phys(struct blkcipher_desc *desc,
  255. struct blkcipher_walk *walk)
  256. {
  257. walk->flags |= BLKCIPHER_WALK_PHYS;
  258. walk->walk_blocksize = crypto_blkcipher_blocksize(desc->tfm);
  259. walk->cipher_blocksize = walk->walk_blocksize;
  260. walk->ivsize = crypto_blkcipher_ivsize(desc->tfm);
  261. walk->alignmask = crypto_blkcipher_alignmask(desc->tfm);
  262. return blkcipher_walk_first(desc, walk);
  263. }
  264. EXPORT_SYMBOL_GPL(blkcipher_walk_phys);
  265. static int blkcipher_walk_first(struct blkcipher_desc *desc,
  266. struct blkcipher_walk *walk)
  267. {
  268. if (WARN_ON_ONCE(in_irq()))
  269. return -EDEADLK;
  270. walk->iv = desc->info;
  271. walk->nbytes = walk->total;
  272. if (unlikely(!walk->total))
  273. return 0;
  274. walk->buffer = NULL;
  275. if (unlikely(((unsigned long)walk->iv & walk->alignmask))) {
  276. int err = blkcipher_copy_iv(walk);
  277. if (err)
  278. return err;
  279. }
  280. scatterwalk_start(&walk->in, walk->in.sg);
  281. scatterwalk_start(&walk->out, walk->out.sg);
  282. walk->page = NULL;
  283. return blkcipher_walk_next(desc, walk);
  284. }
  285. int blkcipher_walk_virt_block(struct blkcipher_desc *desc,
  286. struct blkcipher_walk *walk,
  287. unsigned int blocksize)
  288. {
  289. walk->flags &= ~BLKCIPHER_WALK_PHYS;
  290. walk->walk_blocksize = blocksize;
  291. walk->cipher_blocksize = crypto_blkcipher_blocksize(desc->tfm);
  292. walk->ivsize = crypto_blkcipher_ivsize(desc->tfm);
  293. walk->alignmask = crypto_blkcipher_alignmask(desc->tfm);
  294. return blkcipher_walk_first(desc, walk);
  295. }
  296. EXPORT_SYMBOL_GPL(blkcipher_walk_virt_block);
  297. int blkcipher_aead_walk_virt_block(struct blkcipher_desc *desc,
  298. struct blkcipher_walk *walk,
  299. struct crypto_aead *tfm,
  300. unsigned int blocksize)
  301. {
  302. walk->flags &= ~BLKCIPHER_WALK_PHYS;
  303. walk->walk_blocksize = blocksize;
  304. walk->cipher_blocksize = crypto_aead_blocksize(tfm);
  305. walk->ivsize = crypto_aead_ivsize(tfm);
  306. walk->alignmask = crypto_aead_alignmask(tfm);
  307. return blkcipher_walk_first(desc, walk);
  308. }
  309. EXPORT_SYMBOL_GPL(blkcipher_aead_walk_virt_block);
  310. static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key,
  311. unsigned int keylen)
  312. {
  313. struct blkcipher_alg *cipher = &tfm->__crt_alg->cra_blkcipher;
  314. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  315. int ret;
  316. u8 *buffer, *alignbuffer;
  317. unsigned long absize;
  318. absize = keylen + alignmask;
  319. buffer = kmalloc(absize, GFP_ATOMIC);
  320. if (!buffer)
  321. return -ENOMEM;
  322. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  323. memcpy(alignbuffer, key, keylen);
  324. ret = cipher->setkey(tfm, alignbuffer, keylen);
  325. memset(alignbuffer, 0, keylen);
  326. kfree(buffer);
  327. return ret;
  328. }
  329. static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
  330. {
  331. struct blkcipher_alg *cipher = &tfm->__crt_alg->cra_blkcipher;
  332. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  333. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  334. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  335. return -EINVAL;
  336. }
  337. if ((unsigned long)key & alignmask)
  338. return setkey_unaligned(tfm, key, keylen);
  339. return cipher->setkey(tfm, key, keylen);
  340. }
  341. static int async_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  342. unsigned int keylen)
  343. {
  344. return setkey(crypto_ablkcipher_tfm(tfm), key, keylen);
  345. }
  346. static int async_encrypt(struct ablkcipher_request *req)
  347. {
  348. struct crypto_tfm *tfm = req->base.tfm;
  349. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  350. struct blkcipher_desc desc = {
  351. .tfm = __crypto_blkcipher_cast(tfm),
  352. .info = req->info,
  353. .flags = req->base.flags,
  354. };
  355. return alg->encrypt(&desc, req->dst, req->src, req->nbytes);
  356. }
  357. static int async_decrypt(struct ablkcipher_request *req)
  358. {
  359. struct crypto_tfm *tfm = req->base.tfm;
  360. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  361. struct blkcipher_desc desc = {
  362. .tfm = __crypto_blkcipher_cast(tfm),
  363. .info = req->info,
  364. .flags = req->base.flags,
  365. };
  366. return alg->decrypt(&desc, req->dst, req->src, req->nbytes);
  367. }
  368. static unsigned int crypto_blkcipher_ctxsize(struct crypto_alg *alg, u32 type,
  369. u32 mask)
  370. {
  371. struct blkcipher_alg *cipher = &alg->cra_blkcipher;
  372. unsigned int len = alg->cra_ctxsize;
  373. if ((mask & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_MASK &&
  374. cipher->ivsize) {
  375. len = ALIGN(len, (unsigned long)alg->cra_alignmask + 1);
  376. len += cipher->ivsize;
  377. }
  378. return len;
  379. }
  380. static int crypto_init_blkcipher_ops_async(struct crypto_tfm *tfm)
  381. {
  382. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  383. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  384. crt->setkey = async_setkey;
  385. crt->encrypt = async_encrypt;
  386. crt->decrypt = async_decrypt;
  387. crt->base = __crypto_ablkcipher_cast(tfm);
  388. crt->ivsize = alg->ivsize;
  389. return 0;
  390. }
  391. static int crypto_init_blkcipher_ops_sync(struct crypto_tfm *tfm)
  392. {
  393. struct blkcipher_tfm *crt = &tfm->crt_blkcipher;
  394. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  395. unsigned long align = crypto_tfm_alg_alignmask(tfm) + 1;
  396. unsigned long addr;
  397. crt->setkey = setkey;
  398. crt->encrypt = alg->encrypt;
  399. crt->decrypt = alg->decrypt;
  400. addr = (unsigned long)crypto_tfm_ctx(tfm);
  401. addr = ALIGN(addr, align);
  402. addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);
  403. crt->iv = (void *)addr;
  404. return 0;
  405. }
  406. static int crypto_init_blkcipher_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  407. {
  408. struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  409. if (alg->ivsize > PAGE_SIZE / 8)
  410. return -EINVAL;
  411. if ((mask & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_MASK)
  412. return crypto_init_blkcipher_ops_sync(tfm);
  413. else
  414. return crypto_init_blkcipher_ops_async(tfm);
  415. }
  416. #ifdef CONFIG_NET
  417. static int crypto_blkcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  418. {
  419. struct crypto_report_blkcipher rblkcipher;
  420. strncpy(rblkcipher.type, "blkcipher", sizeof(rblkcipher.type));
  421. strncpy(rblkcipher.geniv, alg->cra_blkcipher.geniv ?: "<default>",
  422. sizeof(rblkcipher.geniv));
  423. rblkcipher.blocksize = alg->cra_blocksize;
  424. rblkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
  425. rblkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
  426. rblkcipher.ivsize = alg->cra_blkcipher.ivsize;
  427. if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  428. sizeof(struct crypto_report_blkcipher), &rblkcipher))
  429. goto nla_put_failure;
  430. return 0;
  431. nla_put_failure:
  432. return -EMSGSIZE;
  433. }
  434. #else
  435. static int crypto_blkcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  436. {
  437. return -ENOSYS;
  438. }
  439. #endif
  440. static void crypto_blkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  441. __attribute__ ((unused));
  442. static void crypto_blkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  443. {
  444. seq_printf(m, "type : blkcipher\n");
  445. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  446. seq_printf(m, "min keysize : %u\n", alg->cra_blkcipher.min_keysize);
  447. seq_printf(m, "max keysize : %u\n", alg->cra_blkcipher.max_keysize);
  448. seq_printf(m, "ivsize : %u\n", alg->cra_blkcipher.ivsize);
  449. seq_printf(m, "geniv : %s\n", alg->cra_blkcipher.geniv ?:
  450. "<default>");
  451. }
  452. const struct crypto_type crypto_blkcipher_type = {
  453. .ctxsize = crypto_blkcipher_ctxsize,
  454. .init = crypto_init_blkcipher_ops,
  455. #ifdef CONFIG_PROC_FS
  456. .show = crypto_blkcipher_show,
  457. #endif
  458. .report = crypto_blkcipher_report,
  459. };
  460. EXPORT_SYMBOL_GPL(crypto_blkcipher_type);
  461. MODULE_LICENSE("GPL");
  462. MODULE_DESCRIPTION("Generic block chaining cipher type");