^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Cryptographic API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Cipher operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * 2002 Adam J. Richter <adam@yggdrasil.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * 2004 Jean-Luc Cooke <jlcooke@certainkey.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) void *src = out ? buf : sgdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) void *dst = out ? sgdata : buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) memcpy(dst, src, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) size_t nbytes, int out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned int len_this_page = scatterwalk_pagelen(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) u8 *vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (len_this_page > nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) len_this_page = nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (out != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) vaddr = scatterwalk_map(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) memcpy_dir(buf, vaddr, len_this_page, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) scatterwalk_unmap(vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) scatterwalk_advance(walk, len_this_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (nbytes == len_this_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) buf += len_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) nbytes -= len_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) scatterwalk_pagedone(walk, out & 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned int start, unsigned int nbytes, int out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct scatter_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct scatterlist tmp[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (!nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) sg = scatterwalk_ffwd(tmp, sg, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) scatterwalk_start(&walk, sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) scatterwalk_copychunks(buf, &walk, nbytes, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) scatterwalk_done(&walk, out, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct scatterlist *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (src->length > len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) len -= src->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) src = sg_next(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) sg_init_table(dst, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) sg_set_page(dst, sg_page(src), src->length - len, src->offset + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) scatterwalk_crypto_chain(dst, sg_next(src), 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) EXPORT_SYMBOL_GPL(scatterwalk_ffwd);