blake2b.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. #ifndef _CRYPTO_BLAKE2B_H
  3. #define _CRYPTO_BLAKE2B_H
  4. #include <linux/bug.h>
  5. #include <linux/types.h>
  6. #include <linux/string.h>
  7. enum blake2b_lengths {
  8. BLAKE2B_BLOCK_SIZE = 128,
  9. BLAKE2B_HASH_SIZE = 64,
  10. BLAKE2B_KEY_SIZE = 64,
  11. BLAKE2B_160_HASH_SIZE = 20,
  12. BLAKE2B_256_HASH_SIZE = 32,
  13. BLAKE2B_384_HASH_SIZE = 48,
  14. BLAKE2B_512_HASH_SIZE = 64,
  15. };
  16. struct blake2b_state {
  17. /* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */
  18. u64 h[8];
  19. u64 t[2];
  20. u64 f[2];
  21. u8 buf[BLAKE2B_BLOCK_SIZE];
  22. unsigned int buflen;
  23. unsigned int outlen;
  24. };
  25. enum blake2b_iv {
  26. BLAKE2B_IV0 = 0x6A09E667F3BCC908ULL,
  27. BLAKE2B_IV1 = 0xBB67AE8584CAA73BULL,
  28. BLAKE2B_IV2 = 0x3C6EF372FE94F82BULL,
  29. BLAKE2B_IV3 = 0xA54FF53A5F1D36F1ULL,
  30. BLAKE2B_IV4 = 0x510E527FADE682D1ULL,
  31. BLAKE2B_IV5 = 0x9B05688C2B3E6C1FULL,
  32. BLAKE2B_IV6 = 0x1F83D9ABFB41BD6BULL,
  33. BLAKE2B_IV7 = 0x5BE0CD19137E2179ULL,
  34. };
  35. static inline void __blake2b_init(struct blake2b_state *state, size_t outlen,
  36. const void *key, size_t keylen)
  37. {
  38. state->h[0] = BLAKE2B_IV0 ^ (0x01010000 | keylen << 8 | outlen);
  39. state->h[1] = BLAKE2B_IV1;
  40. state->h[2] = BLAKE2B_IV2;
  41. state->h[3] = BLAKE2B_IV3;
  42. state->h[4] = BLAKE2B_IV4;
  43. state->h[5] = BLAKE2B_IV5;
  44. state->h[6] = BLAKE2B_IV6;
  45. state->h[7] = BLAKE2B_IV7;
  46. state->t[0] = 0;
  47. state->t[1] = 0;
  48. state->f[0] = 0;
  49. state->f[1] = 0;
  50. state->buflen = 0;
  51. state->outlen = outlen;
  52. if (keylen) {
  53. memcpy(state->buf, key, keylen);
  54. memset(&state->buf[keylen], 0, BLAKE2B_BLOCK_SIZE - keylen);
  55. state->buflen = BLAKE2B_BLOCK_SIZE;
  56. }
  57. }
  58. #endif /* _CRYPTO_BLAKE2B_H */