scatterwalk.c 3.9 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 (!(scatterwalk_pagelen(walk) & (PAGE_SIZE - 1)) || !more)
  65. scatterwalk_pagedone(walk, out, more);
  66. }
  67. EXPORT_SYMBOL_GPL(scatterwalk_done);
  68. void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
  69. size_t nbytes, int out)
  70. {
  71. for (;;) {
  72. unsigned int len_this_page = scatterwalk_pagelen(walk);
  73. u8 *vaddr;
  74. if (len_this_page > nbytes)
  75. len_this_page = nbytes;
  76. vaddr = scatterwalk_map(walk);
  77. memcpy_dir(buf, vaddr, len_this_page, out);
  78. scatterwalk_unmap(vaddr);
  79. scatterwalk_advance(walk, len_this_page);
  80. if (nbytes == len_this_page)
  81. break;
  82. buf += len_this_page;
  83. nbytes -= len_this_page;
  84. scatterwalk_pagedone(walk, out, 1);
  85. }
  86. }
  87. EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
  88. void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
  89. unsigned int start, unsigned int nbytes, int out)
  90. {
  91. struct scatter_walk walk;
  92. struct scatterlist tmp[2];
  93. if (!nbytes)
  94. return;
  95. sg = scatterwalk_ffwd(tmp, sg, start);
  96. if (sg_page(sg) == virt_to_page(buf) &&
  97. sg->offset == offset_in_page(buf))
  98. return;
  99. scatterwalk_start(&walk, sg);
  100. scatterwalk_copychunks(buf, &walk, nbytes, out);
  101. scatterwalk_done(&walk, out, 0);
  102. }
  103. EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
  104. int scatterwalk_bytes_sglen(struct scatterlist *sg, int num_bytes)
  105. {
  106. int offset = 0, n = 0;
  107. /* num_bytes is too small */
  108. if (num_bytes < sg->length)
  109. return -1;
  110. do {
  111. offset += sg->length;
  112. n++;
  113. sg = sg_next(sg);
  114. /* num_bytes is too large */
  115. if (unlikely(!sg && (num_bytes < offset)))
  116. return -1;
  117. } while (sg && (num_bytes > offset));
  118. return n;
  119. }
  120. EXPORT_SYMBOL_GPL(scatterwalk_bytes_sglen);
  121. struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
  122. struct scatterlist *src,
  123. unsigned int len)
  124. {
  125. for (;;) {
  126. if (!len)
  127. return src;
  128. if (src->length > len)
  129. break;
  130. len -= src->length;
  131. src = sg_next(src);
  132. }
  133. sg_init_table(dst, 2);
  134. sg_set_page(dst, sg_page(src), src->length - len, src->offset + len);
  135. scatterwalk_crypto_chain(dst, sg_next(src), 0, 2);
  136. return dst;
  137. }
  138. EXPORT_SYMBOL_GPL(scatterwalk_ffwd);