scatterwalk.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/module.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/highmem.h>
  21. #include <linux/scatterlist.h>
  22. #include "internal.h"
  23. #include "scatterwalk.h"
  24. enum km_type crypto_km_types[] = {
  25. KM_USER0,
  26. KM_USER1,
  27. KM_SOFTIRQ0,
  28. KM_SOFTIRQ1,
  29. };
  30. EXPORT_SYMBOL_GPL(crypto_km_types);
  31. static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
  32. {
  33. void *src = out ? buf : sgdata;
  34. void *dst = out ? sgdata : buf;
  35. memcpy(dst, src, nbytes);
  36. }
  37. void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
  38. {
  39. walk->sg = sg;
  40. BUG_ON(!sg->length);
  41. walk->offset = sg->offset;
  42. }
  43. EXPORT_SYMBOL_GPL(scatterwalk_start);
  44. void *scatterwalk_map(struct scatter_walk *walk, int out)
  45. {
  46. return crypto_kmap(scatterwalk_page(walk), out) +
  47. offset_in_page(walk->offset);
  48. }
  49. EXPORT_SYMBOL_GPL(scatterwalk_map);
  50. static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
  51. unsigned int more)
  52. {
  53. if (out) {
  54. struct page *page;
  55. page = walk->sg->page + ((walk->offset - 1) >> PAGE_SHIFT);
  56. flush_dcache_page(page);
  57. }
  58. if (more) {
  59. walk->offset += PAGE_SIZE - 1;
  60. walk->offset &= PAGE_MASK;
  61. if (walk->offset >= walk->sg->offset + walk->sg->length)
  62. scatterwalk_start(walk, sg_next(walk->sg));
  63. }
  64. }
  65. void scatterwalk_done(struct scatter_walk *walk, int out, int more)
  66. {
  67. if (!offset_in_page(walk->offset) || !more)
  68. scatterwalk_pagedone(walk, out, more);
  69. }
  70. EXPORT_SYMBOL_GPL(scatterwalk_done);
  71. void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
  72. size_t nbytes, int out)
  73. {
  74. for (;;) {
  75. unsigned int len_this_page = scatterwalk_pagelen(walk);
  76. u8 *vaddr;
  77. if (len_this_page > nbytes)
  78. len_this_page = nbytes;
  79. vaddr = scatterwalk_map(walk, out);
  80. memcpy_dir(buf, vaddr, len_this_page, out);
  81. scatterwalk_unmap(vaddr, out);
  82. scatterwalk_advance(walk, len_this_page);
  83. if (nbytes == len_this_page)
  84. break;
  85. buf += len_this_page;
  86. nbytes -= len_this_page;
  87. scatterwalk_pagedone(walk, out, 1);
  88. }
  89. }
  90. EXPORT_SYMBOL_GPL(scatterwalk_copychunks);