Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Herbert Xu | 42c271c | 2007-12-07 18:52:49 +0800 | [diff] [blame] | 16 | |
| 17 | #include <crypto/scatterwalk.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/mm.h> |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 20 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/pagemap.h> |
| 22 | #include <linux/highmem.h> |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 23 | #include <linux/scatterlist.h> |
| 24 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 25 | static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 27 | void *src = out ? buf : sgdata; |
| 28 | void *dst = out ? sgdata : buf; |
| 29 | |
| 30 | memcpy(dst, src, nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg) |
| 34 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | walk->sg = sg; |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | BUG_ON(!sg->length); |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | walk->offset = sg->offset; |
| 40 | } |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 41 | EXPORT_SYMBOL_GPL(scatterwalk_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Cong Wang | f0dfc0b | 2011-11-25 23:14:17 +0800 | [diff] [blame] | 43 | void *scatterwalk_map(struct scatter_walk *walk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
Cong Wang | f0dfc0b | 2011-11-25 23:14:17 +0800 | [diff] [blame] | 45 | return kmap_atomic(scatterwalk_page(walk)) + |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 46 | offset_in_page(walk->offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 48 | EXPORT_SYMBOL_GPL(scatterwalk_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | static void scatterwalk_pagedone(struct scatter_walk *walk, int out, |
| 51 | unsigned int more) |
| 52 | { |
Herbert Xu | 9f11672 | 2007-03-31 12:58:20 +1000 | [diff] [blame] | 53 | if (out) { |
| 54 | struct page *page; |
| 55 | |
Jens Axboe | 78c2f0b | 2007-10-22 19:40:16 +0200 | [diff] [blame] | 56 | page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT); |
Herbert Xu | 4f3e797 | 2009-02-09 14:22:14 +1100 | [diff] [blame] | 57 | if (!PageSlab(page)) |
| 58 | flush_dcache_page(page); |
Herbert Xu | 9f11672 | 2007-03-31 12:58:20 +1000 | [diff] [blame] | 59 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | if (more) { |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 62 | walk->offset += PAGE_SIZE - 1; |
| 63 | walk->offset &= PAGE_MASK; |
| 64 | if (walk->offset >= walk->sg->offset + walk->sg->length) |
Herbert Xu | b2ab4a5 | 2007-12-05 20:59:25 +1100 | [diff] [blame] | 65 | scatterwalk_start(walk, scatterwalk_sg_next(walk->sg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | void scatterwalk_done(struct scatter_walk *walk, int out, int more) |
| 70 | { |
David S. Miller | 85c6201 | 2010-05-19 14:06:29 +1000 | [diff] [blame] | 71 | if (!(scatterwalk_pagelen(walk) & (PAGE_SIZE - 1)) || !more) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | scatterwalk_pagedone(walk, out, more); |
| 73 | } |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 74 | EXPORT_SYMBOL_GPL(scatterwalk_done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 76 | void scatterwalk_copychunks(void *buf, struct scatter_walk *walk, |
| 77 | size_t nbytes, int out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 79 | for (;;) { |
| 80 | unsigned int len_this_page = scatterwalk_pagelen(walk); |
| 81 | u8 *vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 83 | if (len_this_page > nbytes) |
| 84 | len_this_page = nbytes; |
| 85 | |
Cong Wang | f0dfc0b | 2011-11-25 23:14:17 +0800 | [diff] [blame] | 86 | vaddr = scatterwalk_map(walk); |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 87 | memcpy_dir(buf, vaddr, len_this_page, out); |
Cong Wang | f0dfc0b | 2011-11-25 23:14:17 +0800 | [diff] [blame] | 88 | scatterwalk_unmap(vaddr); |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 89 | |
Herbert Xu | 4ee531a | 2007-03-31 12:16:20 +1000 | [diff] [blame] | 90 | scatterwalk_advance(walk, len_this_page); |
J. Bruce Fields | f70ee5e | 2007-03-21 08:50:12 +1100 | [diff] [blame] | 91 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 92 | if (nbytes == len_this_page) |
| 93 | break; |
| 94 | |
| 95 | buf += len_this_page; |
| 96 | nbytes -= len_this_page; |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | scatterwalk_pagedone(walk, out, 1); |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 99 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 101 | EXPORT_SYMBOL_GPL(scatterwalk_copychunks); |
Herbert Xu | 5fa0fea | 2007-08-29 16:31:34 +0800 | [diff] [blame] | 102 | |
| 103 | void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg, |
| 104 | unsigned int start, unsigned int nbytes, int out) |
| 105 | { |
| 106 | struct scatter_walk walk; |
| 107 | unsigned int offset = 0; |
| 108 | |
Herbert Xu | 6e05077 | 2007-12-14 00:44:11 +0800 | [diff] [blame] | 109 | if (!nbytes) |
| 110 | return; |
| 111 | |
Herbert Xu | 5fa0fea | 2007-08-29 16:31:34 +0800 | [diff] [blame] | 112 | for (;;) { |
| 113 | scatterwalk_start(&walk, sg); |
| 114 | |
| 115 | if (start < offset + sg->length) |
| 116 | break; |
| 117 | |
| 118 | offset += sg->length; |
Herbert Xu | b2ab4a5 | 2007-12-05 20:59:25 +1100 | [diff] [blame] | 119 | sg = scatterwalk_sg_next(sg); |
Herbert Xu | 5fa0fea | 2007-08-29 16:31:34 +0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | scatterwalk_advance(&walk, start - offset); |
| 123 | scatterwalk_copychunks(buf, &walk, nbytes, out); |
| 124 | scatterwalk_done(&walk, out, 0); |
| 125 | } |
| 126 | EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy); |