blob: e93a8f6f35d48b32f76815c0a9b6676ccbd65775 [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 */
16#include <linux/kernel.h>
17#include <linux/mm.h>
Herbert Xu5c640972006-08-12 21:56:17 +100018#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/pagemap.h>
20#include <linux/highmem.h>
Herbert Xu5c640972006-08-12 21:56:17 +100021#include <linux/scatterlist.h>
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "internal.h"
24#include "scatterwalk.h"
25
26enum km_type crypto_km_types[] = {
27 KM_USER0,
28 KM_USER1,
29 KM_SOFTIRQ0,
30 KM_SOFTIRQ1,
31};
Herbert Xu5c640972006-08-12 21:56:17 +100032EXPORT_SYMBOL_GPL(crypto_km_types);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Herbert Xu5c640972006-08-12 21:56:17 +100034static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Herbert Xu5c640972006-08-12 21:56:17 +100036 void *src = out ? buf : sgdata;
37 void *dst = out ? sgdata : buf;
38
39 memcpy(dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
42void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
43{
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 walk->sg = sg;
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 BUG_ON(!sg->length);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 walk->offset = sg->offset;
49}
Herbert Xu5c640972006-08-12 21:56:17 +100050EXPORT_SYMBOL_GPL(scatterwalk_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Herbert Xu5c640972006-08-12 21:56:17 +100052void *scatterwalk_map(struct scatter_walk *walk, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Herbert Xu5c640972006-08-12 21:56:17 +100054 return crypto_kmap(scatterwalk_page(walk), out) +
55 offset_in_page(walk->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
Herbert Xu5c640972006-08-12 21:56:17 +100057EXPORT_SYMBOL_GPL(scatterwalk_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
60 unsigned int more)
61{
Herbert Xu9f116722007-03-31 12:58:20 +100062 if (out) {
63 struct page *page;
64
65 page = walk->sg->page + ((walk->offset - 1) >> PAGE_SHIFT);
66 flush_dcache_page(page);
67 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 if (more) {
Herbert Xu5c640972006-08-12 21:56:17 +100070 walk->offset += PAGE_SIZE - 1;
71 walk->offset &= PAGE_MASK;
72 if (walk->offset >= walk->sg->offset + walk->sg->length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 scatterwalk_start(walk, sg_next(walk->sg));
74 }
75}
76
77void scatterwalk_done(struct scatter_walk *walk, int out, int more)
78{
Herbert Xu5c640972006-08-12 21:56:17 +100079 if (!offset_in_page(walk->offset) || !more)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 scatterwalk_pagedone(walk, out, more);
81}
Herbert Xu5c640972006-08-12 21:56:17 +100082EXPORT_SYMBOL_GPL(scatterwalk_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Herbert Xu5c640972006-08-12 21:56:17 +100084void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
85 size_t nbytes, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Herbert Xu5c640972006-08-12 21:56:17 +100087 for (;;) {
88 unsigned int len_this_page = scatterwalk_pagelen(walk);
89 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Herbert Xu5c640972006-08-12 21:56:17 +100091 if (len_this_page > nbytes)
92 len_this_page = nbytes;
93
94 vaddr = scatterwalk_map(walk, out);
95 memcpy_dir(buf, vaddr, len_this_page, out);
96 scatterwalk_unmap(vaddr, out);
97
Herbert Xu4ee531a2007-03-31 12:16:20 +100098 scatterwalk_advance(walk, len_this_page);
J. Bruce Fieldsf70ee5e2007-03-21 08:50:12 +110099
Herbert Xu5c640972006-08-12 21:56:17 +1000100 if (nbytes == len_this_page)
101 break;
102
103 buf += len_this_page;
104 nbytes -= len_this_page;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 scatterwalk_pagedone(walk, out, 1);
Herbert Xuc774e932005-07-06 13:51:31 -0700107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
Herbert Xu5c640972006-08-12 21:56:17 +1000109EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
Herbert Xu5fa0fea2007-08-29 16:31:34 +0800110
111void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
112 unsigned int start, unsigned int nbytes, int out)
113{
114 struct scatter_walk walk;
115 unsigned int offset = 0;
116
117 for (;;) {
118 scatterwalk_start(&walk, sg);
119
120 if (start < offset + sg->length)
121 break;
122
123 offset += sg->length;
124 sg = sg_next(sg);
125 }
126
127 scatterwalk_advance(&walk, start - offset);
128 scatterwalk_copychunks(buf, &walk, nbytes, out);
129 scatterwalk_done(&walk, out, 0);
130}
131EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);