blake2b.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /*
  3. * Helper functions for BLAKE2b implementations.
  4. * Keep this in sync with the corresponding BLAKE2s header.
  5. */
  6. #ifndef _CRYPTO_INTERNAL_BLAKE2B_H
  7. #define _CRYPTO_INTERNAL_BLAKE2B_H
  8. #include <crypto/blake2b.h>
  9. #include <crypto/internal/hash.h>
  10. #include <linux/string.h>
  11. void blake2b_compress_generic(struct blake2b_state *state,
  12. const u8 *block, size_t nblocks, u32 inc);
  13. static inline void blake2b_set_lastblock(struct blake2b_state *state)
  14. {
  15. state->f[0] = -1;
  16. }
  17. typedef void (*blake2b_compress_t)(struct blake2b_state *state,
  18. const u8 *block, size_t nblocks, u32 inc);
  19. static inline void __blake2b_update(struct blake2b_state *state,
  20. const u8 *in, size_t inlen,
  21. blake2b_compress_t compress)
  22. {
  23. const size_t fill = BLAKE2B_BLOCK_SIZE - state->buflen;
  24. if (unlikely(!inlen))
  25. return;
  26. if (inlen > fill) {
  27. memcpy(state->buf + state->buflen, in, fill);
  28. (*compress)(state, state->buf, 1, BLAKE2B_BLOCK_SIZE);
  29. state->buflen = 0;
  30. in += fill;
  31. inlen -= fill;
  32. }
  33. if (inlen > BLAKE2B_BLOCK_SIZE) {
  34. const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2B_BLOCK_SIZE);
  35. /* Hash one less (full) block than strictly possible */
  36. (*compress)(state, in, nblocks - 1, BLAKE2B_BLOCK_SIZE);
  37. in += BLAKE2B_BLOCK_SIZE * (nblocks - 1);
  38. inlen -= BLAKE2B_BLOCK_SIZE * (nblocks - 1);
  39. }
  40. memcpy(state->buf + state->buflen, in, inlen);
  41. state->buflen += inlen;
  42. }
  43. static inline void __blake2b_final(struct blake2b_state *state, u8 *out,
  44. blake2b_compress_t compress)
  45. {
  46. int i;
  47. blake2b_set_lastblock(state);
  48. memset(state->buf + state->buflen, 0,
  49. BLAKE2B_BLOCK_SIZE - state->buflen); /* Padding */
  50. (*compress)(state, state->buf, 1, state->buflen);
  51. for (i = 0; i < ARRAY_SIZE(state->h); i++)
  52. __cpu_to_le64s(&state->h[i]);
  53. memcpy(out, state->h, state->outlen);
  54. }
  55. /* Helper functions for shash implementations of BLAKE2b */
  56. struct blake2b_tfm_ctx {
  57. u8 key[BLAKE2B_KEY_SIZE];
  58. unsigned int keylen;
  59. };
  60. static inline int crypto_blake2b_setkey(struct crypto_shash *tfm,
  61. const u8 *key, unsigned int keylen)
  62. {
  63. struct blake2b_tfm_ctx *tctx = crypto_shash_ctx(tfm);
  64. if (keylen == 0 || keylen > BLAKE2B_KEY_SIZE)
  65. return -EINVAL;
  66. memcpy(tctx->key, key, keylen);
  67. tctx->keylen = keylen;
  68. return 0;
  69. }
  70. static inline int crypto_blake2b_init(struct shash_desc *desc)
  71. {
  72. const struct blake2b_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
  73. struct blake2b_state *state = shash_desc_ctx(desc);
  74. unsigned int outlen = crypto_shash_digestsize(desc->tfm);
  75. __blake2b_init(state, outlen, tctx->key, tctx->keylen);
  76. return 0;
  77. }
  78. static inline int crypto_blake2b_update(struct shash_desc *desc,
  79. const u8 *in, unsigned int inlen,
  80. blake2b_compress_t compress)
  81. {
  82. struct blake2b_state *state = shash_desc_ctx(desc);
  83. __blake2b_update(state, in, inlen, compress);
  84. return 0;
  85. }
  86. static inline int crypto_blake2b_final(struct shash_desc *desc, u8 *out,
  87. blake2b_compress_t compress)
  88. {
  89. struct blake2b_state *state = shash_desc_ctx(desc);
  90. __blake2b_final(state, out, compress);
  91. return 0;
  92. }
  93. #endif /* _CRYPTO_INTERNAL_BLAKE2B_H */