Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Herbert Xu | 42c271c | 2007-12-07 18:52:49 +0800 | [diff] [blame] | 3 | * Cryptographic scatter and gather helpers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
| 6 | * Copyright (c) 2002 Adam J. Richter <adam@yggdrasil.com> |
| 7 | * Copyright (c) 2004 Jean-Luc Cooke <jlcooke@certainkey.com> |
Herbert Xu | 42c271c | 2007-12-07 18:52:49 +0800 | [diff] [blame] | 8 | * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef _CRYPTO_SCATTERWALK_H |
| 12 | #define _CRYPTO_SCATTERWALK_H |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Herbert Xu | 42c271c | 2007-12-07 18:52:49 +0800 | [diff] [blame] | 14 | #include <crypto/algapi.h> |
Andy Shevchenko | 244d22f | 2021-12-10 16:30:09 +0200 | [diff] [blame] | 15 | |
Herbert Xu | 42c271c | 2007-12-07 18:52:49 +0800 | [diff] [blame] | 16 | #include <linux/highmem.h> |
Andy Shevchenko | 244d22f | 2021-12-10 16:30:09 +0200 | [diff] [blame] | 17 | #include <linux/mm.h> |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 18 | #include <linux/scatterlist.h> |
| 19 | |
Steffen Klassert | 079f2f7 | 2010-11-22 11:25:50 +0100 | [diff] [blame] | 20 | static inline void scatterwalk_crypto_chain(struct scatterlist *head, |
Eric Biggers | 8c30fbe | 2018-07-23 10:01:33 -0700 | [diff] [blame] | 21 | struct scatterlist *sg, int num) |
Steffen Klassert | 079f2f7 | 2010-11-22 11:25:50 +0100 | [diff] [blame] | 22 | { |
Steffen Klassert | 079f2f7 | 2010-11-22 11:25:50 +0100 | [diff] [blame] | 23 | if (sg) |
Dan Williams | c56f6d1 | 2015-08-07 18:15:13 +0200 | [diff] [blame] | 24 | sg_chain(head, num, sg); |
Steffen Klassert | 079f2f7 | 2010-11-22 11:25:50 +0100 | [diff] [blame] | 25 | else |
| 26 | sg_mark_end(head); |
| 27 | } |
| 28 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 29 | static inline unsigned int scatterwalk_pagelen(struct scatter_walk *walk) |
| 30 | { |
| 31 | unsigned int len = walk->sg->offset + walk->sg->length - walk->offset; |
| 32 | unsigned int len_this_page = offset_in_page(~walk->offset) + 1; |
| 33 | return len_this_page > len ? len : len_this_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 36 | static inline unsigned int scatterwalk_clamp(struct scatter_walk *walk, |
| 37 | unsigned int nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 39 | unsigned int len_this_page = scatterwalk_pagelen(walk); |
| 40 | return nbytes > len_this_page ? len_this_page : nbytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static inline void scatterwalk_advance(struct scatter_walk *walk, |
| 44 | unsigned int nbytes) |
| 45 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | walk->offset += nbytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Herbert Xu | 9547737 | 2005-07-06 13:52:09 -0700 | [diff] [blame] | 49 | static inline unsigned int scatterwalk_aligned(struct scatter_walk *walk, |
| 50 | unsigned int alignmask) |
| 51 | { |
| 52 | return !(walk->offset & alignmask); |
| 53 | } |
| 54 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 55 | static inline struct page *scatterwalk_page(struct scatter_walk *walk) |
| 56 | { |
Jens Axboe | 78c2f0b | 2007-10-22 19:40:16 +0200 | [diff] [blame] | 57 | return sg_page(walk->sg) + (walk->offset >> PAGE_SHIFT); |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 58 | } |
| 59 | |
Cong Wang | f0dfc0b | 2011-11-25 23:14:17 +0800 | [diff] [blame] | 60 | static inline void scatterwalk_unmap(void *vaddr) |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 61 | { |
Cong Wang | f0dfc0b | 2011-11-25 23:14:17 +0800 | [diff] [blame] | 62 | kunmap_atomic(vaddr); |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 63 | } |
| 64 | |
Herbert Xu | ac02725 | 2016-07-12 13:18:00 +0800 | [diff] [blame] | 65 | static inline void scatterwalk_start(struct scatter_walk *walk, |
| 66 | struct scatterlist *sg) |
| 67 | { |
| 68 | walk->sg = sg; |
| 69 | walk->offset = sg->offset; |
| 70 | } |
| 71 | |
| 72 | static inline void *scatterwalk_map(struct scatter_walk *walk) |
| 73 | { |
| 74 | return kmap_atomic(scatterwalk_page(walk)) + |
| 75 | offset_in_page(walk->offset); |
| 76 | } |
| 77 | |
| 78 | static inline void scatterwalk_pagedone(struct scatter_walk *walk, int out, |
| 79 | unsigned int more) |
| 80 | { |
| 81 | if (out) { |
| 82 | struct page *page; |
| 83 | |
| 84 | page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT); |
Herbert Xu | b0d2193 | 2021-06-24 14:32:15 +0800 | [diff] [blame] | 85 | flush_dcache_page(page); |
Herbert Xu | ac02725 | 2016-07-12 13:18:00 +0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | if (more && walk->offset >= walk->sg->offset + walk->sg->length) |
| 89 | scatterwalk_start(walk, sg_next(walk->sg)); |
| 90 | } |
| 91 | |
| 92 | static inline void scatterwalk_done(struct scatter_walk *walk, int out, |
| 93 | int more) |
| 94 | { |
| 95 | if (!more || walk->offset >= walk->sg->offset + walk->sg->length || |
| 96 | !(walk->offset & (PAGE_SIZE - 1))) |
| 97 | scatterwalk_pagedone(walk, out, more); |
| 98 | } |
| 99 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 100 | void scatterwalk_copychunks(void *buf, struct scatter_walk *walk, |
| 101 | size_t nbytes, int out); |
Cong Wang | f0dfc0b | 2011-11-25 23:14:17 +0800 | [diff] [blame] | 102 | void *scatterwalk_map(struct scatter_walk *walk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Herbert Xu | 5fa0fea | 2007-08-29 16:31:34 +0800 | [diff] [blame] | 104 | void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg, |
| 105 | unsigned int start, unsigned int nbytes, int out); |
| 106 | |
Herbert Xu | fc42bcb | 2015-05-21 15:10:59 +0800 | [diff] [blame] | 107 | struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2], |
| 108 | struct scatterlist *src, |
| 109 | unsigned int len); |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | #endif /* _CRYPTO_SCATTERWALK_H */ |