scatterwalk.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Cipher operations.
  5. *
  6. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  7. * 2002 Adam J. Richter <adam@yggdrasil.com>
  8. * 2004 Jean-Luc Cooke <jlcooke@certainkey.com>
  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/scatterwalk.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/module.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/highmem.h>
  22. #include <linux/scatterlist.h>
  23. static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
  24. {
  25. void *src = out ? buf : sgdata;
  26. void *dst = out ? sgdata : buf;
  27. memcpy(dst, src, nbytes);
  28. }
  29. void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
  30. {
  31. walk->sg = sg;
  32. BUG_ON(!sg->length);
  33. walk->offset = sg->offset;
  34. }
  35. EXPORT_SYMBOL_GPL(scatterwalk_start);
  36. void *scatterwalk_map(struct scatter_walk *walk)
  37. {
  38. return kmap_atomic(scatterwalk_page(walk)) +
  39. offset_in_page(walk->offset);
  40. }
  41. EXPORT_SYMBOL_GPL(scatterwalk_map);
  42. static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
  43. unsigned int more)
  44. {
  45. if (out) {
  46. struct page *page;
  47. page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT);
  48. /* Test ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE first as
  49. * PageSlab cannot be optimised away per se due to
  50. * use of volatile pointer.
  51. */
  52. if (ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE && !PageSlab(page))
  53. flush_dcache_page(page);
  54. }
  55. if (more) {
  56. walk->offset += PAGE_SIZE - 1;
  57. walk->offset &= PAGE_MASK;
  58. if (walk->offset >= walk->sg->offset + walk->sg->length)
  59. scatterwalk_start(walk, sg_next(walk->sg));
  60. }
  61. }
  62. void scatterwalk_done(struct scatter_walk *walk, int out, int more)
  63. {
  64. if (!more || walk->offset >= walk->sg->offset + walk->sg->length ||
  65. !(walk->offset & (PAGE_SIZE - 1)))
  66. scatterwalk_pagedone(walk, out, more);
  67. }
  68. EXPORT_SYMBOL_GPL(scatterwalk_done);
  69. void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
  70. size_t nbytes, int out)
  71. {
  72. for (;;) {
  73. unsigned int len_this_page = scatterwalk_pagelen(walk);
  74. u8 *vaddr;
  75. if (len_this_page > nbytes)
  76. len_this_page = nbytes;
  77. vaddr = scatterwalk_map(walk);
  78. memcpy_dir(buf, vaddr, len_this_page, out);
  79. scatterwalk_unmap(vaddr);
  80. scatterwalk_advance(walk, len_this_page);
  81. if (nbytes == len_this_page)
  82. break;
  83. buf += len_this_page;
  84. nbytes -= len_this_page;
  85. scatterwalk_pagedone(walk, out, 1);
  86. }
  87. }
  88. EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
  89. void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
  90. unsigned int start, unsigned int nbytes, int out)
  91. {
  92. struct scatter_walk walk;
  93. struct scatterlist tmp[2];
  94. if (!nbytes)
  95. return;
  96. sg = scatterwalk_ffwd(tmp, sg, start);
  97. if (sg_page(sg) == virt_to_page(buf) &&
  98. sg->offset == offset_in_page(buf))
  99. return;
  100. scatterwalk_start(&walk, sg);
  101. scatterwalk_copychunks(buf, &walk, nbytes, out);
  102. scatterwalk_done(&walk, out, 0);
  103. }
  104. EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
  105. int scatterwalk_bytes_sglen(struct scatterlist *sg, int num_bytes)
  106. {
  107. int offset = 0, n = 0;
  108. /* num_bytes is too small */
  109. if (num_bytes < sg->length)
  110. return -1;
  111. do {
  112. offset += sg->length;
  113. n++;
  114. sg = sg_next(sg);
  115. /* num_bytes is too large */
  116. if (unlikely(!sg && (num_bytes < offset)))
  117. return -1;
  118. } while (sg && (num_bytes > offset));
  119. return n;
  120. }
  121. EXPORT_SYMBOL_GPL(scatterwalk_bytes_sglen);
  122. struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
  123. struct scatterlist *src,
  124. unsigned int len)
  125. {
  126. for (;;) {
  127. if (!len)
  128. return src;
  129. if (src->length > len)
  130. break;
  131. len -= src->length;
  132. src = sg_next(src);
  133. }
  134. sg_init_table(dst, 2);
  135. sg_set_page(dst, sg_page(src), src->length - len, src->offset + len);
  136. scatterwalk_crypto_chain(dst, sg_next(src), 0, 2);
  137. return dst;
  138. }
  139. EXPORT_SYMBOL_GPL(scatterwalk_ffwd);