Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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)  * Symmetric key cipher operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Generic encrypt/decrypt wrapper for ciphers, handles operations across
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * multiple page boundaries by using temporary blocks.  In user context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * the kernel is given a chance to schedule us once per page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
^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/internal/aead.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <crypto/internal/cipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/cryptouser.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	SKCIPHER_WALK_PHYS = 1 << 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	SKCIPHER_WALK_SLOW = 1 << 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	SKCIPHER_WALK_COPY = 1 << 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	SKCIPHER_WALK_DIFF = 1 << 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	SKCIPHER_WALK_SLEEP = 1 << 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) struct skcipher_walk_buffer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct list_head entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct scatter_walk dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	u8 buffer[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static int skcipher_walk_next(struct skcipher_walk *walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static inline void skcipher_unmap(struct scatter_walk *walk, void *vaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	if (PageHighMem(scatterwalk_page(walk)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		kunmap_atomic(vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static inline void *skcipher_map(struct scatter_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct page *page = scatterwalk_page(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return (PageHighMem(page) ? kmap_atomic(page) : page_address(page)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	       offset_in_page(walk->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static inline void skcipher_map_src(struct skcipher_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	walk->src.virt.addr = skcipher_map(&walk->in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static inline void skcipher_map_dst(struct skcipher_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	walk->dst.virt.addr = skcipher_map(&walk->out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static inline void skcipher_unmap_src(struct skcipher_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	skcipher_unmap(&walk->in, walk->src.virt.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) static inline void skcipher_unmap_dst(struct skcipher_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	skcipher_unmap(&walk->out, walk->dst.virt.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static inline gfp_t skcipher_walk_gfp(struct skcipher_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	return walk->flags & SKCIPHER_WALK_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) /* Get a spot of the specified length that does not straddle a page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * The caller needs to ensure that there is enough space for this operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static inline u8 *skcipher_get_spot(u8 *start, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	return max(start, end_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static int skcipher_done_slow(struct skcipher_walk *walk, unsigned int bsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	u8 *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	addr = (u8 *)ALIGN((unsigned long)walk->buffer, walk->alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	addr = skcipher_get_spot(addr, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	scatterwalk_copychunks(addr, &walk->out, bsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			       (walk->flags & SKCIPHER_WALK_PHYS) ? 2 : 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int skcipher_walk_done(struct skcipher_walk *walk, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	unsigned int n = walk->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	unsigned int nbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		goto finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (likely(err >= 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		n -= err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		nbytes = walk->total - n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (likely(!(walk->flags & (SKCIPHER_WALK_PHYS |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 				    SKCIPHER_WALK_SLOW |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 				    SKCIPHER_WALK_COPY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 				    SKCIPHER_WALK_DIFF)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unmap_src:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		skcipher_unmap_src(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	} else if (walk->flags & SKCIPHER_WALK_DIFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		skcipher_unmap_dst(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		goto unmap_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	} else if (walk->flags & SKCIPHER_WALK_COPY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		skcipher_map_dst(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		memcpy(walk->dst.virt.addr, walk->page, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		skcipher_unmap_dst(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	} else if (unlikely(walk->flags & SKCIPHER_WALK_SLOW)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		if (err > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			 * Didn't process all bytes.  Either the algorithm is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			 * broken, or this was the last step and it turned out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			 * the message wasn't evenly divisible into blocks but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			 * the algorithm requires it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			nbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			n = skcipher_done_slow(walk, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	walk->total = nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	walk->nbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	scatterwalk_advance(&walk->in, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	scatterwalk_advance(&walk->out, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	scatterwalk_done(&walk->in, 0, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	scatterwalk_done(&walk->out, 1, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		crypto_yield(walk->flags & SKCIPHER_WALK_SLEEP ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			     CRYPTO_TFM_REQ_MAY_SLEEP : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		return skcipher_walk_next(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) finish:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	/* Short-circuit for the common/fast path. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (!((unsigned long)walk->buffer | (unsigned long)walk->page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (walk->flags & SKCIPHER_WALK_PHYS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (walk->iv != walk->oiv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		memcpy(walk->oiv, walk->iv, walk->ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (walk->buffer != walk->page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		kfree(walk->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if (walk->page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		free_page((unsigned long)walk->page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) EXPORT_SYMBOL_GPL(skcipher_walk_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) void skcipher_walk_complete(struct skcipher_walk *walk, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	struct skcipher_walk_buffer *p, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	list_for_each_entry_safe(p, tmp, &walk->buffers, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		data = p->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			data = PTR_ALIGN(&p->buffer[0], walk->alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			data = skcipher_get_spot(data, walk->stride);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		scatterwalk_copychunks(data, &p->dst, p->len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		if (offset_in_page(p->data) + p->len + walk->stride >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		    PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			free_page((unsigned long)p->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		list_del(&p->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	if (!err && walk->iv != walk->oiv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		memcpy(walk->oiv, walk->iv, walk->ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (walk->buffer != walk->page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		kfree(walk->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (walk->page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		free_page((unsigned long)walk->page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) EXPORT_SYMBOL_GPL(skcipher_walk_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static void skcipher_queue_write(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 				 struct skcipher_walk_buffer *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	p->dst = walk->out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	list_add_tail(&p->entry, &walk->buffers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static int skcipher_next_slow(struct skcipher_walk *walk, unsigned int bsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	bool phys = walk->flags & SKCIPHER_WALK_PHYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	unsigned alignmask = walk->alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	struct skcipher_walk_buffer *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	unsigned a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	unsigned n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	void *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (!phys) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		if (!walk->buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			walk->buffer = walk->page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		buffer = walk->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		if (buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			goto ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	/* Start with the minimum alignment of kmalloc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	a = crypto_tfm_ctx_alignment() - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	n = bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (phys) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		/* Calculate the minimum alignment of p->buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		a &= (sizeof(*p) ^ (sizeof(*p) - 1)) >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		n += sizeof(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	/* Minimum size to align p->buffer by alignmask. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	n += alignmask & ~a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	/* Minimum size to ensure p->buffer does not straddle a page. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	n += (bsize - 1) & ~(alignmask | a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	v = kzalloc(n, skcipher_walk_gfp(walk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (!v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		return skcipher_walk_done(walk, -ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (phys) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		p = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		p->len = bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		skcipher_queue_write(walk, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		buffer = p->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		walk->buffer = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		buffer = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) ok:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	walk->dst.virt.addr = PTR_ALIGN(buffer, alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	walk->dst.virt.addr = skcipher_get_spot(walk->dst.virt.addr, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	walk->src.virt.addr = walk->dst.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	walk->nbytes = bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	walk->flags |= SKCIPHER_WALK_SLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static int skcipher_next_copy(struct skcipher_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct skcipher_walk_buffer *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	u8 *tmp = walk->page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	skcipher_map_src(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	memcpy(tmp, walk->src.virt.addr, walk->nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	skcipher_unmap_src(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	walk->src.virt.addr = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	walk->dst.virt.addr = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (!(walk->flags & SKCIPHER_WALK_PHYS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	p = kmalloc(sizeof(*p), skcipher_walk_gfp(walk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	p->data = walk->page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	p->len = walk->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	skcipher_queue_write(walk, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	if (offset_in_page(walk->page) + walk->nbytes + walk->stride >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	    PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		walk->page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		walk->page += walk->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static int skcipher_next_fast(struct skcipher_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	unsigned long diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	walk->src.phys.page = scatterwalk_page(&walk->in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	walk->src.phys.offset = offset_in_page(walk->in.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	walk->dst.phys.page = scatterwalk_page(&walk->out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	walk->dst.phys.offset = offset_in_page(walk->out.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if (walk->flags & SKCIPHER_WALK_PHYS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	diff = walk->src.phys.offset - walk->dst.phys.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	diff |= walk->src.virt.page - walk->dst.virt.page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	skcipher_map_src(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	walk->dst.virt.addr = walk->src.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	if (diff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		walk->flags |= SKCIPHER_WALK_DIFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		skcipher_map_dst(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static int skcipher_walk_next(struct skcipher_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	unsigned int bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	unsigned int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	walk->flags &= ~(SKCIPHER_WALK_SLOW | SKCIPHER_WALK_COPY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			 SKCIPHER_WALK_DIFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	n = walk->total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	bsize = min(walk->stride, max(n, walk->blocksize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	n = scatterwalk_clamp(&walk->in, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	n = scatterwalk_clamp(&walk->out, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	if (unlikely(n < bsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		if (unlikely(walk->total < walk->blocksize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			return skcipher_walk_done(walk, -EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) slow_path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		err = skcipher_next_slow(walk, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		goto set_phys_lowmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	if (unlikely((walk->in.offset | walk->out.offset) & walk->alignmask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		if (!walk->page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			gfp_t gfp = skcipher_walk_gfp(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 			walk->page = (void *)__get_free_page(gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			if (!walk->page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 				goto slow_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		walk->nbytes = min_t(unsigned, n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 				     PAGE_SIZE - offset_in_page(walk->page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		walk->flags |= SKCIPHER_WALK_COPY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		err = skcipher_next_copy(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		goto set_phys_lowmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	walk->nbytes = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	return skcipher_next_fast(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) set_phys_lowmem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	if (!err && (walk->flags & SKCIPHER_WALK_PHYS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		walk->src.phys.page = virt_to_page(walk->src.virt.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		walk->src.phys.offset &= PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		walk->dst.phys.offset &= PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static int skcipher_copy_iv(struct skcipher_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	unsigned a = crypto_tfm_ctx_alignment() - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	unsigned alignmask = walk->alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	unsigned ivsize = walk->ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	unsigned bs = walk->stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	unsigned aligned_bs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	unsigned size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	u8 *iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	aligned_bs = ALIGN(bs, alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	/* Minimum size to align buffer by alignmask. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	size = alignmask & ~a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (walk->flags & SKCIPHER_WALK_PHYS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		size += ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		size += aligned_bs + ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		/* Minimum size to ensure buffer does not straddle a page. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		size += (bs - 1) & ~(alignmask | a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	walk->buffer = kmalloc(size, skcipher_walk_gfp(walk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	if (!walk->buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	iv = PTR_ALIGN(walk->buffer, alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	iv = skcipher_get_spot(iv, bs) + aligned_bs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	walk->iv = memcpy(iv, walk->iv, walk->ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static int skcipher_walk_first(struct skcipher_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	if (WARN_ON_ONCE(in_irq()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		return -EDEADLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	walk->buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	if (unlikely(((unsigned long)walk->iv & walk->alignmask))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		int err = skcipher_copy_iv(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	walk->page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	return skcipher_walk_next(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) static int skcipher_walk_skcipher(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 				  struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	walk->total = req->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	walk->nbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	walk->iv = req->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	walk->oiv = req->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (unlikely(!walk->total))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	scatterwalk_start(&walk->in, req->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	scatterwalk_start(&walk->out, req->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	walk->flags &= ~SKCIPHER_WALK_SLEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	walk->flags |= req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		       SKCIPHER_WALK_SLEEP : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	walk->blocksize = crypto_skcipher_blocksize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	walk->stride = crypto_skcipher_walksize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	walk->ivsize = crypto_skcipher_ivsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	walk->alignmask = crypto_skcipher_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	return skcipher_walk_first(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) int skcipher_walk_virt(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		       struct skcipher_request *req, bool atomic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	might_sleep_if(req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	walk->flags &= ~SKCIPHER_WALK_PHYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	err = skcipher_walk_skcipher(walk, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	walk->flags &= atomic ? ~SKCIPHER_WALK_SLEEP : ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) EXPORT_SYMBOL_GPL(skcipher_walk_virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) void skcipher_walk_atomise(struct skcipher_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	walk->flags &= ~SKCIPHER_WALK_SLEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) EXPORT_SYMBOL_GPL(skcipher_walk_atomise);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) int skcipher_walk_async(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 			struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	walk->flags |= SKCIPHER_WALK_PHYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	INIT_LIST_HEAD(&walk->buffers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	return skcipher_walk_skcipher(walk, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) EXPORT_SYMBOL_GPL(skcipher_walk_async);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) static int skcipher_walk_aead_common(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 				     struct aead_request *req, bool atomic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	walk->nbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	walk->iv = req->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	walk->oiv = req->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	if (unlikely(!walk->total))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	walk->flags &= ~SKCIPHER_WALK_PHYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	scatterwalk_start(&walk->in, req->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	scatterwalk_start(&walk->out, req->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	scatterwalk_copychunks(NULL, &walk->in, req->assoclen, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	scatterwalk_copychunks(NULL, &walk->out, req->assoclen, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	scatterwalk_done(&walk->in, 0, walk->total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	scatterwalk_done(&walk->out, 0, walk->total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	if (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		walk->flags |= SKCIPHER_WALK_SLEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		walk->flags &= ~SKCIPHER_WALK_SLEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	walk->blocksize = crypto_aead_blocksize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	walk->stride = crypto_aead_chunksize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	walk->ivsize = crypto_aead_ivsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	walk->alignmask = crypto_aead_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	err = skcipher_walk_first(walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	if (atomic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		walk->flags &= ~SKCIPHER_WALK_SLEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 			       struct aead_request *req, bool atomic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	walk->total = req->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	return skcipher_walk_aead_common(walk, req, atomic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 			       struct aead_request *req, bool atomic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	walk->total = req->cryptlen - crypto_aead_authsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	return skcipher_walk_aead_common(walk, req, atomic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) static void skcipher_set_needkey(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	if (crypto_skcipher_max_keysize(tfm) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		crypto_skcipher_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 				     const u8 *key, unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	unsigned long alignmask = crypto_skcipher_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	u8 *buffer, *alignbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	unsigned long absize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	absize = keylen + alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	buffer = kmalloc(absize, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	memcpy(alignbuffer, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	ret = cipher->setkey(tfm, alignbuffer, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	kfree_sensitive(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) int crypto_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 			   unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	unsigned long alignmask = crypto_skcipher_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	if (keylen < cipher->min_keysize || keylen > cipher->max_keysize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	if ((unsigned long)key & alignmask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		err = skcipher_setkey_unaligned(tfm, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		err = cipher->setkey(tfm, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		skcipher_set_needkey(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) EXPORT_SYMBOL_GPL(crypto_skcipher_setkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) int crypto_skcipher_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	struct crypto_alg *alg = tfm->base.__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	unsigned int cryptlen = req->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	crypto_stats_get(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		ret = -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 		ret = crypto_skcipher_alg(tfm)->encrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	crypto_stats_skcipher_encrypt(cryptlen, ret, alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) EXPORT_SYMBOL_GPL(crypto_skcipher_encrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) int crypto_skcipher_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	struct crypto_alg *alg = tfm->base.__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	unsigned int cryptlen = req->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	crypto_stats_get(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		ret = -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 		ret = crypto_skcipher_alg(tfm)->decrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	crypto_stats_skcipher_decrypt(cryptlen, ret, alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) EXPORT_SYMBOL_GPL(crypto_skcipher_decrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	alg->exit(skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	skcipher_set_needkey(skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	if (alg->exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		skcipher->base.exit = crypto_skcipher_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	if (alg->init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 		return alg->init(skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) static void crypto_skcipher_free_instance(struct crypto_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	struct skcipher_instance *skcipher =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		container_of(inst, struct skcipher_instance, s.base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	skcipher->free(skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	__maybe_unused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 						     base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	seq_printf(m, "type         : skcipher\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	seq_printf(m, "async        : %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		   alg->cra_flags & CRYPTO_ALG_ASYNC ?  "yes" : "no");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	seq_printf(m, "min keysize  : %u\n", skcipher->min_keysize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	seq_printf(m, "max keysize  : %u\n", skcipher->max_keysize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	seq_printf(m, "ivsize       : %u\n", skcipher->ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	seq_printf(m, "chunksize    : %u\n", skcipher->chunksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	seq_printf(m, "walksize     : %u\n", skcipher->walksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) #ifdef CONFIG_NET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	struct crypto_report_blkcipher rblkcipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 						     base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	memset(&rblkcipher, 0, sizeof(rblkcipher));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	strscpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	rblkcipher.blocksize = alg->cra_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	rblkcipher.min_keysize = skcipher->min_keysize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	rblkcipher.max_keysize = skcipher->max_keysize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	rblkcipher.ivsize = skcipher->ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	return nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		       sizeof(rblkcipher), &rblkcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) static const struct crypto_type crypto_skcipher_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	.extsize = crypto_alg_extsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	.init_tfm = crypto_skcipher_init_tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	.free = crypto_skcipher_free_instance,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	.show = crypto_skcipher_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	.report = crypto_skcipher_report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	.maskclear = ~CRYPTO_ALG_TYPE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	.maskset = CRYPTO_ALG_TYPE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	.type = CRYPTO_ALG_TYPE_SKCIPHER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	.tfmsize = offsetof(struct crypto_skcipher, base),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 			 struct crypto_instance *inst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 			 const char *name, u32 type, u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	spawn->base.frontend = &crypto_skcipher_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 					      u32 type, u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	return crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) struct crypto_sync_skcipher *crypto_alloc_sync_skcipher(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 				const char *alg_name, u32 type, u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	struct crypto_skcipher *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	/* Only sync algorithms allowed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	mask |= CRYPTO_ALG_ASYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	tfm = crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	 * Make sure we do not allocate something that might get used with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 	 * an on-stack request: check the request size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	if (!IS_ERR(tfm) && WARN_ON(crypto_skcipher_reqsize(tfm) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 				    MAX_SYNC_SKCIPHER_REQSIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 		crypto_free_skcipher(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	return (struct crypto_sync_skcipher *)tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) EXPORT_SYMBOL_GPL(crypto_alloc_sync_skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) int crypto_has_skcipher(const char *alg_name, u32 type, u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	return crypto_type_has_alg(alg_name, &crypto_skcipher_type, type, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) EXPORT_SYMBOL_GPL(crypto_has_skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) static int skcipher_prepare_alg(struct skcipher_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	struct crypto_alg *base = &alg->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	    alg->walksize > PAGE_SIZE / 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	if (!alg->chunksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 		alg->chunksize = base->cra_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	if (!alg->walksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 		alg->walksize = alg->chunksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	base->cra_type = &crypto_skcipher_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 	base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) int crypto_register_skcipher(struct skcipher_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	struct crypto_alg *base = &alg->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 	err = skcipher_prepare_alg(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 	return crypto_register_alg(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) EXPORT_SYMBOL_GPL(crypto_register_skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) void crypto_unregister_skcipher(struct skcipher_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 	crypto_unregister_alg(&alg->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) int crypto_register_skciphers(struct skcipher_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 		ret = crypto_register_skcipher(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 	for (--i; i >= 0; --i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 		crypto_unregister_skcipher(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) EXPORT_SYMBOL_GPL(crypto_register_skciphers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 	for (i = count - 1; i >= 0; --i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 		crypto_unregister_skcipher(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) int skcipher_register_instance(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 			   struct skcipher_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 	if (WARN_ON(!inst->free))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 	err = skcipher_prepare_alg(&inst->alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 	return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) EXPORT_SYMBOL_GPL(skcipher_register_instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) static int skcipher_setkey_simple(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 				  unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 	struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 	crypto_cipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 	crypto_cipher_set_flags(cipher, crypto_skcipher_get_flags(tfm) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 				CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 	return crypto_cipher_setkey(cipher, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) static int skcipher_init_tfm_simple(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	struct skcipher_instance *inst = skcipher_alg_instance(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 	struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 	struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 	struct crypto_cipher *cipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 	cipher = crypto_spawn_cipher(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 	if (IS_ERR(cipher))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 		return PTR_ERR(cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 	ctx->cipher = cipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) static void skcipher_exit_tfm_simple(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 	struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 	crypto_free_cipher(ctx->cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) static void skcipher_free_instance_simple(struct skcipher_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 	crypto_drop_cipher(skcipher_instance_ctx(inst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 	kfree(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)  * skcipher_alloc_instance_simple - allocate instance of simple block cipher mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)  * Allocate an skcipher_instance for a simple block cipher mode of operation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)  * e.g. cbc or ecb.  The instance context will have just a single crypto_spawn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924)  * that for the underlying cipher.  The {min,max}_keysize, ivsize, blocksize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)  * alignmask, and priority are set from the underlying cipher but can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)  * overridden if needed.  The tfm context defaults to skcipher_ctx_simple, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927)  * default ->setkey(), ->init(), and ->exit() methods are installed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929)  * @tmpl: the template being instantiated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)  * @tb: the template parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932)  * Return: a pointer to the new instance, or an ERR_PTR().  The caller still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)  *	   needs to register the instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) struct skcipher_instance *skcipher_alloc_instance_simple(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 	struct crypto_template *tmpl, struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) 	u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) 	struct skcipher_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 	struct crypto_cipher_spawn *spawn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) 	struct crypto_alg *cipher_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) 	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) 	if (!inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) 	spawn = skcipher_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) 	err = crypto_grab_cipher(spawn, skcipher_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) 				 crypto_attr_alg_name(tb[1]), 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) 	cipher_alg = crypto_spawn_cipher_alg(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) 	err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) 				  cipher_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) 	inst->free = skcipher_free_instance_simple;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) 	/* Default algorithm properties, can be overridden */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) 	inst->alg.base.cra_blocksize = cipher_alg->cra_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) 	inst->alg.base.cra_alignmask = cipher_alg->cra_alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) 	inst->alg.base.cra_priority = cipher_alg->cra_priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) 	inst->alg.min_keysize = cipher_alg->cra_cipher.cia_min_keysize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) 	inst->alg.max_keysize = cipher_alg->cra_cipher.cia_max_keysize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) 	inst->alg.ivsize = cipher_alg->cra_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) 	/* Use skcipher_ctx_simple by default, can be overridden */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) 	inst->alg.base.cra_ctxsize = sizeof(struct skcipher_ctx_simple);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) 	inst->alg.setkey = skcipher_setkey_simple;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) 	inst->alg.init = skcipher_init_tfm_simple;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) 	inst->alg.exit = skcipher_exit_tfm_simple;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) 	return inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) err_free_inst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) 	skcipher_free_instance_simple(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) EXPORT_SYMBOL_GPL(skcipher_alloc_instance_simple);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) MODULE_DESCRIPTION("Symmetric key cipher type");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) MODULE_IMPORT_NS(CRYPTO_INTERNAL);