Linux Audio

Check our new training course

Loading...
v3.15
  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
 17#include <crypto/scatterwalk.h>
 18#include <linux/kernel.h>
 19#include <linux/mm.h>
 20#include <linux/module.h>
 21#include <linux/pagemap.h>
 22#include <linux/highmem.h>
 23#include <linux/scatterlist.h>
 24
 25static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
 26{
 27	void *src = out ? buf : sgdata;
 28	void *dst = out ? sgdata : buf;
 29
 30	memcpy(dst, src, nbytes);
 31}
 32
 33void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
 34{
 35	walk->sg = sg;
 36
 37	BUG_ON(!sg->length);
 38
 39	walk->offset = sg->offset;
 40}
 41EXPORT_SYMBOL_GPL(scatterwalk_start);
 42
 43void *scatterwalk_map(struct scatter_walk *walk)
 44{
 45	return kmap_atomic(scatterwalk_page(walk)) +
 46	       offset_in_page(walk->offset);
 47}
 48EXPORT_SYMBOL_GPL(scatterwalk_map);
 49
 50static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
 51				 unsigned int more)
 52{
 53	if (out) {
 54		struct page *page;
 55
 56		page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT);
 57		if (!PageSlab(page))
 58			flush_dcache_page(page);
 59	}
 60
 61	if (more) {
 62		walk->offset += PAGE_SIZE - 1;
 63		walk->offset &= PAGE_MASK;
 64		if (walk->offset >= walk->sg->offset + walk->sg->length)
 65			scatterwalk_start(walk, scatterwalk_sg_next(walk->sg));
 66	}
 67}
 68
 69void scatterwalk_done(struct scatter_walk *walk, int out, int more)
 70{
 71	if (!(scatterwalk_pagelen(walk) & (PAGE_SIZE - 1)) || !more)
 72		scatterwalk_pagedone(walk, out, more);
 73}
 74EXPORT_SYMBOL_GPL(scatterwalk_done);
 75
 76void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
 77			    size_t nbytes, int out)
 78{
 79	for (;;) {
 80		unsigned int len_this_page = scatterwalk_pagelen(walk);
 81		u8 *vaddr;
 82
 83		if (len_this_page > nbytes)
 84			len_this_page = nbytes;
 85
 86		vaddr = scatterwalk_map(walk);
 87		memcpy_dir(buf, vaddr, len_this_page, out);
 88		scatterwalk_unmap(vaddr);
 
 
 89
 90		scatterwalk_advance(walk, len_this_page);
 91
 92		if (nbytes == len_this_page)
 93			break;
 94
 95		buf += len_this_page;
 96		nbytes -= len_this_page;
 97
 98		scatterwalk_pagedone(walk, out, 1);
 99	}
100}
101EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
102
103void 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
109	if (!nbytes)
110		return;
111
112	for (;;) {
113		scatterwalk_start(&walk, sg);
114
115		if (start < offset + sg->length)
116			break;
117
118		offset += sg->length;
119		sg = scatterwalk_sg_next(sg);
120	}
121
122	scatterwalk_advance(&walk, start - offset);
123	scatterwalk_copychunks(buf, &walk, nbytes, out);
124	scatterwalk_done(&walk, out, 0);
125}
126EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
127
128int scatterwalk_bytes_sglen(struct scatterlist *sg, int num_bytes)
 
 
129{
130	int offset = 0, n = 0;
 
 
 
 
 
 
 
 
 
131
132	/* num_bytes is too small */
133	if (num_bytes < sg->length)
134		return -1;
135
136	do {
137		offset += sg->length;
138		n++;
139		sg = scatterwalk_sg_next(sg);
140
141		/* num_bytes is too large */
142		if (unlikely(!sg && (num_bytes < offset)))
143			return -1;
144	} while (sg && (num_bytes > offset));
145
146	return n;
147}
148EXPORT_SYMBOL_GPL(scatterwalk_bytes_sglen);
v4.10.11
 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
17#include <crypto/scatterwalk.h>
18#include <linux/kernel.h>
19#include <linux/mm.h>
20#include <linux/module.h>
 
 
21#include <linux/scatterlist.h>
22
23static 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
28	memcpy(dst, src, nbytes);
29}
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
32			    size_t nbytes, int out)
33{
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
41		if (out != 2) {
42			vaddr = scatterwalk_map(walk);
43			memcpy_dir(buf, vaddr, len_this_page, out);
44			scatterwalk_unmap(vaddr);
45		}
46
47		scatterwalk_advance(walk, len_this_page);
48
49		if (nbytes == len_this_page)
50			break;
51
52		buf += len_this_page;
53		nbytes -= len_this_page;
54
55		scatterwalk_pagedone(walk, out & 1, 1);
56	}
57}
58EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
59
60void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
61			      unsigned int start, unsigned int nbytes, int out)
62{
63	struct scatter_walk walk;
64	struct scatterlist tmp[2];
65
66	if (!nbytes)
67		return;
68
69	sg = scatterwalk_ffwd(tmp, sg, start);
 
 
 
 
 
 
 
 
70
71	scatterwalk_start(&walk, sg);
72	scatterwalk_copychunks(buf, &walk, nbytes, out);
73	scatterwalk_done(&walk, out, 0);
74}
75EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
76
77struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
78				     struct scatterlist *src,
79				     unsigned int len)
80{
81	for (;;) {
82		if (!len)
83			return src;
84
85		if (src->length > len)
86			break;
87
88		len -= src->length;
89		src = sg_next(src);
90	}
91
92	sg_init_table(dst, 2);
93	sg_set_page(dst, sg_page(src), src->length - len, src->offset + len);
94	scatterwalk_crypto_chain(dst, sg_next(src), 0, 2);
 
 
 
 
 
 
 
 
 
 
95
96	return dst;
97}
98EXPORT_SYMBOL_GPL(scatterwalk_ffwd);