1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Handle partial blocks for block hash. 4 * 5 * Copyright (c) 2015 Linaro Ltd <ard.biesheuvel@linaro.org> 6 * Copyright (c) 2025 Herbert Xu <herbert@gondor.apana.org.au> 7 */ 8 9 #ifndef _CRYPTO_INTERNAL_BLOCKHASH_H 10 #define _CRYPTO_INTERNAL_BLOCKHASH_H 11 12 #include <linux/string.h> 13 #include <linux/types.h> 14 15 #define BLOCK_HASH_UPDATE_BASE(block_fn, state, src, nbytes, bs, dv, \ 16 buf, buflen) \ 17 ({ \ 18 typeof(block_fn) *_block_fn = &(block_fn); \ 19 typeof(state + 0) _state = (state); \ 20 unsigned int _buflen = (buflen); \ 21 size_t _nbytes = (nbytes); \ 22 unsigned int _bs = (bs); \ 23 const u8 *_src = (src); \ 24 u8 *_buf = (buf); \ 25 while ((_buflen + _nbytes) >= _bs) { \ 26 const u8 *data = _src; \ 27 size_t len = _nbytes; \ 28 size_t blocks; \ 29 int remain; \ 30 if (_buflen) { \ 31 remain = _bs - _buflen; \ 32 memcpy(_buf + _buflen, _src, remain); \ 33 data = _buf; \ 34 len = _bs; \ 35 } \ 36 remain = len % bs; \ 37 blocks = (len - remain) / (dv); \ 38 (*_block_fn)(_state, data, blocks); \ 39 _src += len - remain - _buflen; \ 40 _nbytes -= len - remain - _buflen; \ 41 _buflen = 0; \ 42 } \ 43 memcpy(_buf + _buflen, _src, _nbytes); \ 44 _buflen += _nbytes; \ 45 }) 46 47 #define BLOCK_HASH_UPDATE(block, state, src, nbytes, bs, buf, buflen) \ 48 BLOCK_HASH_UPDATE_BASE(block, state, src, nbytes, bs, 1, buf, buflen) 49 #define BLOCK_HASH_UPDATE_BLOCKS(block, state, src, nbytes, bs, buf, buflen) \ 50 BLOCK_HASH_UPDATE_BASE(block, state, src, nbytes, bs, bs, buf, buflen) 51 52 #endif /* _CRYPTO_INTERNAL_BLOCKHASH_H */ 53