Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | #include <linux/kernel.h> |
| 17 | #include <linux/mm.h> |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 18 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
| 20 | #include <linux/highmem.h> |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 21 | #include <linux/scatterlist.h> |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "internal.h" |
| 24 | #include "scatterwalk.h" |
| 25 | |
| 26 | enum km_type crypto_km_types[] = { |
| 27 | KM_USER0, |
| 28 | KM_USER1, |
| 29 | KM_SOFTIRQ0, |
| 30 | KM_SOFTIRQ1, |
| 31 | }; |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 32 | EXPORT_SYMBOL_GPL(crypto_km_types); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 34 | static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 36 | void *src = out ? buf : sgdata; |
| 37 | void *dst = out ? sgdata : buf; |
| 38 | |
| 39 | memcpy(dst, src, nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg) |
| 43 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | walk->sg = sg; |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | BUG_ON(!sg->length); |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | walk->offset = sg->offset; |
| 49 | } |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 50 | EXPORT_SYMBOL_GPL(scatterwalk_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 52 | void *scatterwalk_map(struct scatter_walk *walk, int out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 54 | return crypto_kmap(scatterwalk_page(walk), out) + |
| 55 | offset_in_page(walk->offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 57 | EXPORT_SYMBOL_GPL(scatterwalk_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | static void scatterwalk_pagedone(struct scatter_walk *walk, int out, |
| 60 | unsigned int more) |
| 61 | { |
| 62 | if (out) |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 63 | flush_dcache_page(scatterwalk_page(walk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | if (more) { |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 66 | walk->offset += PAGE_SIZE - 1; |
| 67 | walk->offset &= PAGE_MASK; |
| 68 | if (walk->offset >= walk->sg->offset + walk->sg->length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | scatterwalk_start(walk, sg_next(walk->sg)); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void scatterwalk_done(struct scatter_walk *walk, int out, int more) |
| 74 | { |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 75 | if (!offset_in_page(walk->offset) || !more) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | scatterwalk_pagedone(walk, out, more); |
| 77 | } |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 78 | EXPORT_SYMBOL_GPL(scatterwalk_done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 80 | void scatterwalk_copychunks(void *buf, struct scatter_walk *walk, |
| 81 | size_t nbytes, int out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 83 | for (;;) { |
| 84 | unsigned int len_this_page = scatterwalk_pagelen(walk); |
| 85 | u8 *vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 87 | if (len_this_page > nbytes) |
| 88 | len_this_page = nbytes; |
| 89 | |
| 90 | vaddr = scatterwalk_map(walk, out); |
| 91 | memcpy_dir(buf, vaddr, len_this_page, out); |
| 92 | scatterwalk_unmap(vaddr, out); |
| 93 | |
| 94 | if (nbytes == len_this_page) |
| 95 | break; |
| 96 | |
| 97 | buf += len_this_page; |
| 98 | nbytes -= len_this_page; |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | scatterwalk_pagedone(walk, out, 1); |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 103 | scatterwalk_advance(walk, nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 105 | EXPORT_SYMBOL_GPL(scatterwalk_copychunks); |