blob: 3de89a424401c1918818145a3d27df627c99f7ef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Xu42c271c2007-12-07 18:52:49 +080016
17#include <crypto/scatterwalk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/mm.h>
Herbert Xu5c640972006-08-12 21:56:17 +100020#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/pagemap.h>
22#include <linux/highmem.h>
Herbert Xu5c640972006-08-12 21:56:17 +100023#include <linux/scatterlist.h>
24
Herbert Xu5c640972006-08-12 21:56:17 +100025static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Herbert Xu5c640972006-08-12 21:56:17 +100027 void *src = out ? buf : sgdata;
28 void *dst = out ? sgdata : buf;
29
30 memcpy(dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031}
32
33void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
34{
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 walk->sg = sg;
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 BUG_ON(!sg->length);
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 walk->offset = sg->offset;
40}
Herbert Xu5c640972006-08-12 21:56:17 +100041EXPORT_SYMBOL_GPL(scatterwalk_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Herbert Xu5c640972006-08-12 21:56:17 +100043void *scatterwalk_map(struct scatter_walk *walk, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Herbert Xu5c640972006-08-12 21:56:17 +100045 return crypto_kmap(scatterwalk_page(walk), out) +
46 offset_in_page(walk->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
Herbert Xu5c640972006-08-12 21:56:17 +100048EXPORT_SYMBOL_GPL(scatterwalk_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
51 unsigned int more)
52{
Herbert Xu9f116722007-03-31 12:58:20 +100053 if (out) {
54 struct page *page;
55
Jens Axboe78c2f0b2007-10-22 19:40:16 +020056 page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT);
Herbert Xu4f3e7972009-02-09 14:22:14 +110057 if (!PageSlab(page))
58 flush_dcache_page(page);
Herbert Xu9f116722007-03-31 12:58:20 +100059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 if (more) {
Herbert Xu5c640972006-08-12 21:56:17 +100062 walk->offset += PAGE_SIZE - 1;
63 walk->offset &= PAGE_MASK;
64 if (walk->offset >= walk->sg->offset + walk->sg->length)
Herbert Xub2ab4a52007-12-05 20:59:25 +110065 scatterwalk_start(walk, scatterwalk_sg_next(walk->sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
67}
68
69void scatterwalk_done(struct scatter_walk *walk, int out, int more)
70{
Herbert Xu5c640972006-08-12 21:56:17 +100071 if (!offset_in_page(walk->offset) || !more)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 scatterwalk_pagedone(walk, out, more);
73}
Herbert Xu5c640972006-08-12 21:56:17 +100074EXPORT_SYMBOL_GPL(scatterwalk_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Herbert Xu5c640972006-08-12 21:56:17 +100076void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
77 size_t nbytes, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Herbert Xu5c640972006-08-12 21:56:17 +100079 for (;;) {
80 unsigned int len_this_page = scatterwalk_pagelen(walk);
81 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Herbert Xu5c640972006-08-12 21:56:17 +100083 if (len_this_page > nbytes)
84 len_this_page = nbytes;
85
86 vaddr = scatterwalk_map(walk, out);
87 memcpy_dir(buf, vaddr, len_this_page, out);
88 scatterwalk_unmap(vaddr, out);
89
Herbert Xu4ee531a2007-03-31 12:16:20 +100090 scatterwalk_advance(walk, len_this_page);
J. Bruce Fieldsf70ee5e2007-03-21 08:50:12 +110091
Herbert Xu5c640972006-08-12 21:56:17 +100092 if (nbytes == len_this_page)
93 break;
94
95 buf += len_this_page;
96 nbytes -= len_this_page;
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 scatterwalk_pagedone(walk, out, 1);
Herbert Xuc774e932005-07-06 13:51:31 -070099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
Herbert Xu5c640972006-08-12 21:56:17 +1000101EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
Herbert Xu5fa0fea2007-08-29 16:31:34 +0800102
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
Herbert Xu6e050772007-12-14 00:44:11 +0800109 if (!nbytes)
110 return;
111
Herbert Xu5fa0fea2007-08-29 16:31:34 +0800112 for (;;) {
113 scatterwalk_start(&walk, sg);
114
115 if (start < offset + sg->length)
116 break;
117
118 offset += sg->length;
Herbert Xub2ab4a52007-12-05 20:59:25 +1100119 sg = scatterwalk_sg_next(sg);
Herbert Xu5fa0fea2007-08-29 16:31:34 +0800120 }
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);