Blame view

crypto/scatterwalk.c 2.02 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
  /*
   * Cryptographic API.
   *
   * Cipher operations.
   *
   * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
   *               2002 Adam J. Richter <adam@yggdrasil.com>
   *               2004 Jean-Luc Cooke <jlcooke@certainkey.com>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
   */
42c271c6c   Herbert Xu   [CRYPTO] scatterw...
11
12
  
  #include <crypto/scatterwalk.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
  #include <linux/kernel.h>
  #include <linux/mm.h>
5c64097aa   Herbert Xu   [CRYPTO] scatterw...
15
  #include <linux/module.h>
5c64097aa   Herbert Xu   [CRYPTO] scatterw...
16
  #include <linux/scatterlist.h>
5c64097aa   Herbert Xu   [CRYPTO] scatterw...
17
  static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
  {
5c64097aa   Herbert Xu   [CRYPTO] scatterw...
19
20
21
22
  	void *src = out ? buf : sgdata;
  	void *dst = out ? sgdata : buf;
  
  	memcpy(dst, src, nbytes);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
  }
5c64097aa   Herbert Xu   [CRYPTO] scatterw...
24
25
  void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
  			    size_t nbytes, int out)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
  {
5c64097aa   Herbert Xu   [CRYPTO] scatterw...
27
28
29
30
31
32
  	for (;;) {
  		unsigned int len_this_page = scatterwalk_pagelen(walk);
  		u8 *vaddr;
  
  		if (len_this_page > nbytes)
  			len_this_page = nbytes;
85eccddee   Herbert Xu   crypto: scatterwa...
33
34
35
36
37
  		if (out != 2) {
  			vaddr = scatterwalk_map(walk);
  			memcpy_dir(buf, vaddr, len_this_page, out);
  			scatterwalk_unmap(vaddr);
  		}
5c64097aa   Herbert Xu   [CRYPTO] scatterw...
38

4ee531a3e   Herbert Xu   [CRYPTO] api: Use...
39
  		scatterwalk_advance(walk, len_this_page);
f70ee5ec8   J. Bruce Fields   [CRYPTO] api: sca...
40

5c64097aa   Herbert Xu   [CRYPTO] scatterw...
41
42
43
44
45
  		if (nbytes == len_this_page)
  			break;
  
  		buf += len_this_page;
  		nbytes -= len_this_page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46

85eccddee   Herbert Xu   crypto: scatterwa...
47
  		scatterwalk_pagedone(walk, out & 1, 1);
c774e93e2   Herbert Xu   [CRYPTO] Add plum...
48
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
  }
5c64097aa   Herbert Xu   [CRYPTO] scatterw...
50
  EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
5fa0fea27   Herbert Xu   [CRYPTO] scatterw...
51
52
53
54
55
  
  void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
  			      unsigned int start, unsigned int nbytes, int out)
  {
  	struct scatter_walk walk;
74412fd5d   Herbert Xu   crypto: scatterwa...
56
  	struct scatterlist tmp[2];
5fa0fea27   Herbert Xu   [CRYPTO] scatterw...
57

6e050778c   Herbert Xu   [CRYPTO] scatterw...
58
59
  	if (!nbytes)
  		return;
74412fd5d   Herbert Xu   crypto: scatterwa...
60
  	sg = scatterwalk_ffwd(tmp, sg, start);
5fa0fea27   Herbert Xu   [CRYPTO] scatterw...
61

74412fd5d   Herbert Xu   crypto: scatterwa...
62
  	scatterwalk_start(&walk, sg);
5fa0fea27   Herbert Xu   [CRYPTO] scatterw...
63
64
65
66
  	scatterwalk_copychunks(buf, &walk, nbytes, out);
  	scatterwalk_done(&walk, out, 0);
  }
  EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
257aff515   Joel Fernandes   crypto: scatterwa...
67

fc42bcba9   Herbert Xu   crypto: scatterwa...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
  				     struct scatterlist *src,
  				     unsigned int len)
  {
  	for (;;) {
  		if (!len)
  			return src;
  
  		if (src->length > len)
  			break;
  
  		len -= src->length;
  		src = sg_next(src);
  	}
fdaef75f6   Herbert Xu   crypto: scatterwa...
82
  	sg_init_table(dst, 2);
fc42bcba9   Herbert Xu   crypto: scatterwa...
83
  	sg_set_page(dst, sg_page(src), src->length - len, src->offset + len);
8c30fbe63   Eric Biggers   crypto: scatterwa...
84
  	scatterwalk_crypto_chain(dst, sg_next(src), 2);
fc42bcba9   Herbert Xu   crypto: scatterwa...
85
86
87
88
  
  	return dst;
  }
  EXPORT_SYMBOL_GPL(scatterwalk_ffwd);