Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | */ | |
42c271c6 HX |
16 | |
17 | #include <crypto/scatterwalk.h> | |
1da177e4 LT |
18 | #include <linux/kernel.h> |
19 | #include <linux/mm.h> | |
5c64097a | 20 | #include <linux/module.h> |
5c64097a HX |
21 | #include <linux/scatterlist.h> |
22 | ||
5c64097a | 23 | static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out) |
1da177e4 | 24 | { |
5c64097a HX |
25 | void *src = out ? buf : sgdata; |
26 | void *dst = out ? sgdata : buf; | |
27 | ||
28 | memcpy(dst, src, nbytes); | |
1da177e4 LT |
29 | } |
30 | ||
5c64097a HX |
31 | void scatterwalk_copychunks(void *buf, struct scatter_walk *walk, |
32 | size_t nbytes, int out) | |
1da177e4 | 33 | { |
5c64097a HX |
34 | for (;;) { |
35 | unsigned int len_this_page = scatterwalk_pagelen(walk); | |
36 | u8 *vaddr; | |
37 | ||
38 | if (len_this_page > nbytes) | |
39 | len_this_page = nbytes; | |
40 | ||
85eccdde HX |
41 | if (out != 2) { |
42 | vaddr = scatterwalk_map(walk); | |
43 | memcpy_dir(buf, vaddr, len_this_page, out); | |
44 | scatterwalk_unmap(vaddr); | |
45 | } | |
5c64097a | 46 | |
4ee531a3 | 47 | scatterwalk_advance(walk, len_this_page); |
f70ee5ec | 48 | |
5c64097a HX |
49 | if (nbytes == len_this_page) |
50 | break; | |
51 | ||
52 | buf += len_this_page; | |
53 | nbytes -= len_this_page; | |
1da177e4 | 54 | |
85eccdde | 55 | scatterwalk_pagedone(walk, out & 1, 1); |
c774e93e | 56 | } |
1da177e4 | 57 | } |
5c64097a | 58 | EXPORT_SYMBOL_GPL(scatterwalk_copychunks); |
5fa0fea2 HX |
59 | |
60 | void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg, | |
61 | unsigned int start, unsigned int nbytes, int out) | |
62 | { | |
63 | struct scatter_walk walk; | |
74412fd5 | 64 | struct scatterlist tmp[2]; |
5fa0fea2 | 65 | |
6e050778 HX |
66 | if (!nbytes) |
67 | return; | |
68 | ||
74412fd5 | 69 | sg = scatterwalk_ffwd(tmp, sg, start); |
5fa0fea2 | 70 | |
74412fd5 HX |
71 | if (sg_page(sg) == virt_to_page(buf) && |
72 | sg->offset == offset_in_page(buf)) | |
73 | return; | |
5fa0fea2 | 74 | |
74412fd5 | 75 | scatterwalk_start(&walk, sg); |
5fa0fea2 HX |
76 | scatterwalk_copychunks(buf, &walk, nbytes, out); |
77 | scatterwalk_done(&walk, out, 0); | |
78 | } | |
79 | EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy); | |
257aff51 | 80 | |
fc42bcba HX |
81 | struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2], |
82 | struct scatterlist *src, | |
83 | unsigned int len) | |
84 | { | |
85 | for (;;) { | |
86 | if (!len) | |
87 | return src; | |
88 | ||
89 | if (src->length > len) | |
90 | break; | |
91 | ||
92 | len -= src->length; | |
93 | src = sg_next(src); | |
94 | } | |
95 | ||
fdaef75f | 96 | sg_init_table(dst, 2); |
fc42bcba HX |
97 | sg_set_page(dst, sg_page(src), src->length - len, src->offset + len); |
98 | scatterwalk_crypto_chain(dst, sg_next(src), 0, 2); | |
99 | ||
100 | return dst; | |
101 | } | |
102 | EXPORT_SYMBOL_GPL(scatterwalk_ffwd); |