1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef _CRYPTO_ECB_CBC_HELPER_H
4 #define _CRYPTO_ECB_CBC_HELPER_H
6 #include <crypto/internal/skcipher.h>
7 #include <asm/fpu/api.h>
10 * Mode helpers to instantiate parameterized skcipher ECB/CBC modes without
11 * having to rely on indirect calls and retpolines.
14 #define ECB_WALK_START(req, bsize, fpu_blocks) do { \
15 void *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req)); \
16 const int __fpu_blocks = (fpu_blocks); \
17 const int __bsize = (bsize); \
18 struct skcipher_walk walk; \
19 int err = skcipher_walk_virt(&walk, (req), false); \
20 while (walk.nbytes > 0) { \
21 unsigned int nbytes = walk.nbytes; \
22 bool do_fpu = __fpu_blocks != -1 && \
23 nbytes >= __fpu_blocks * __bsize; \
24 const u8 *src = walk.src.virt.addr; \
25 u8 *dst = walk.dst.virt.addr; \
26 u8 __maybe_unused buf[(bsize)]; \
27 if (do_fpu) kernel_fpu_begin()
29 #define CBC_WALK_START(req, bsize, fpu_blocks) \
30 ECB_WALK_START(req, bsize, fpu_blocks)
32 #define ECB_WALK_ADVANCE(blocks) do { \
33 dst += (blocks) * __bsize; \
34 src += (blocks) * __bsize; \
35 nbytes -= (blocks) * __bsize; \
38 #define ECB_BLOCK(blocks, func) do { \
39 const int __blocks = (blocks); \
40 if (do_fpu && __blocks < __fpu_blocks) { \
44 while (nbytes >= __blocks * __bsize) { \
45 (func)(ctx, dst, src); \
46 ECB_WALK_ADVANCE(blocks); \
50 #define CBC_ENC_BLOCK(func) do { \
51 const u8 *__iv = walk.iv; \
52 while (nbytes >= __bsize) { \
53 crypto_xor_cpy(dst, src, __iv, __bsize); \
54 (func)(ctx, dst, dst); \
56 ECB_WALK_ADVANCE(1); \
58 memcpy(walk.iv, __iv, __bsize); \
61 #define CBC_DEC_BLOCK(blocks, func) do { \
62 const int __blocks = (blocks); \
63 if (do_fpu && __blocks < __fpu_blocks) { \
67 while (nbytes >= __blocks * __bsize) { \
68 const u8 *__iv = src + ((blocks) - 1) * __bsize; \
70 __iv = memcpy(buf, __iv, __bsize); \
71 (func)(ctx, dst, src); \
72 crypto_xor(dst, walk.iv, __bsize); \
73 memcpy(walk.iv, __iv, __bsize); \
74 ECB_WALK_ADVANCE(blocks); \
78 #define ECB_WALK_END() \
79 if (do_fpu) kernel_fpu_end(); \
80 err = skcipher_walk_done(&walk, nbytes); \
85 #define CBC_WALK_END() ECB_WALK_END()