blockhash.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #ifndef _CRYPTO_INTERNAL_BLOCKHASH_H
  9. #define _CRYPTO_INTERNAL_BLOCKHASH_H
  10. #include <linux/string.h>
  11. #include <linux/types.h>
  12. #define BLOCK_HASH_UPDATE_BASE(block_fn, state, src, nbytes, bs, dv, \
  13. buf, buflen) \
  14. ({ \
  15. typeof(block_fn) *_block_fn = &(block_fn); \
  16. typeof(state + 0) _state = (state); \
  17. unsigned int _buflen = (buflen); \
  18. size_t _nbytes = (nbytes); \
  19. unsigned int _bs = (bs); \
  20. const u8 *_src = (src); \
  21. u8 *_buf = (buf); \
  22. while ((_buflen + _nbytes) >= _bs) { \
  23. const u8 *data = _src; \
  24. size_t len = _nbytes; \
  25. size_t blocks; \
  26. int remain; \
  27. if (_buflen) { \
  28. remain = _bs - _buflen; \
  29. memcpy(_buf + _buflen, _src, remain); \
  30. data = _buf; \
  31. len = _bs; \
  32. } \
  33. remain = len % bs; \
  34. blocks = (len - remain) / (dv); \
  35. (*_block_fn)(_state, data, blocks); \
  36. _src += len - remain - _buflen; \
  37. _nbytes -= len - remain - _buflen; \
  38. _buflen = 0; \
  39. } \
  40. memcpy(_buf + _buflen, _src, _nbytes); \
  41. _buflen += _nbytes; \
  42. })
  43. #define BLOCK_HASH_UPDATE(block, state, src, nbytes, bs, buf, buflen) \
  44. BLOCK_HASH_UPDATE_BASE(block, state, src, nbytes, bs, 1, buf, buflen)
  45. #define BLOCK_HASH_UPDATE_BLOCKS(block, state, src, nbytes, bs, buf, buflen) \
  46. BLOCK_HASH_UPDATE_BASE(block, state, src, nbytes, bs, bs, buf, buflen)
  47. #endif /* _CRYPTO_INTERNAL_BLOCKHASH_H */