blake2s.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  4. */
  5. #ifndef _CRYPTO_BLAKE2S_H
  6. #define _CRYPTO_BLAKE2S_H
  7. #include <linux/bug.h>
  8. #include <linux/kconfig.h>
  9. #include <linux/types.h>
  10. #include <linux/string.h>
  11. enum blake2s_lengths {
  12. BLAKE2S_BLOCK_SIZE = 64,
  13. BLAKE2S_HASH_SIZE = 32,
  14. BLAKE2S_KEY_SIZE = 32,
  15. BLAKE2S_128_HASH_SIZE = 16,
  16. BLAKE2S_160_HASH_SIZE = 20,
  17. BLAKE2S_224_HASH_SIZE = 28,
  18. BLAKE2S_256_HASH_SIZE = 32,
  19. };
  20. struct blake2s_state {
  21. /* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */
  22. u32 h[8];
  23. u32 t[2];
  24. u32 f[2];
  25. u8 buf[BLAKE2S_BLOCK_SIZE];
  26. unsigned int buflen;
  27. unsigned int outlen;
  28. };
  29. enum blake2s_iv {
  30. BLAKE2S_IV0 = 0x6A09E667UL,
  31. BLAKE2S_IV1 = 0xBB67AE85UL,
  32. BLAKE2S_IV2 = 0x3C6EF372UL,
  33. BLAKE2S_IV3 = 0xA54FF53AUL,
  34. BLAKE2S_IV4 = 0x510E527FUL,
  35. BLAKE2S_IV5 = 0x9B05688CUL,
  36. BLAKE2S_IV6 = 0x1F83D9ABUL,
  37. BLAKE2S_IV7 = 0x5BE0CD19UL,
  38. };
  39. static inline void __blake2s_init(struct blake2s_state *state, size_t outlen,
  40. const void *key, size_t keylen)
  41. {
  42. state->h[0] = BLAKE2S_IV0 ^ (0x01010000 | keylen << 8 | outlen);
  43. state->h[1] = BLAKE2S_IV1;
  44. state->h[2] = BLAKE2S_IV2;
  45. state->h[3] = BLAKE2S_IV3;
  46. state->h[4] = BLAKE2S_IV4;
  47. state->h[5] = BLAKE2S_IV5;
  48. state->h[6] = BLAKE2S_IV6;
  49. state->h[7] = BLAKE2S_IV7;
  50. state->t[0] = 0;
  51. state->t[1] = 0;
  52. state->f[0] = 0;
  53. state->f[1] = 0;
  54. state->buflen = 0;
  55. state->outlen = outlen;
  56. if (keylen) {
  57. memcpy(state->buf, key, keylen);
  58. memset(&state->buf[keylen], 0, BLAKE2S_BLOCK_SIZE - keylen);
  59. state->buflen = BLAKE2S_BLOCK_SIZE;
  60. }
  61. }
  62. static inline void blake2s_init(struct blake2s_state *state,
  63. const size_t outlen)
  64. {
  65. __blake2s_init(state, outlen, NULL, 0);
  66. }
  67. static inline void blake2s_init_key(struct blake2s_state *state,
  68. const size_t outlen, const void *key,
  69. const size_t keylen)
  70. {
  71. WARN_ON(IS_ENABLED(DEBUG) && (!outlen || outlen > BLAKE2S_HASH_SIZE ||
  72. !key || !keylen || keylen > BLAKE2S_KEY_SIZE));
  73. __blake2s_init(state, outlen, key, keylen);
  74. }
  75. void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen);
  76. void blake2s_final(struct blake2s_state *state, u8 *out);
  77. static inline void blake2s(u8 *out, const u8 *in, const u8 *key,
  78. const size_t outlen, const size_t inlen,
  79. const size_t keylen)
  80. {
  81. struct blake2s_state state;
  82. WARN_ON(IS_ENABLED(DEBUG) && ((!in && inlen > 0) || !out || !outlen ||
  83. outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE ||
  84. (!key && keylen)));
  85. __blake2s_init(&state, outlen, key, keylen);
  86. blake2s_update(&state, in, inlen);
  87. blake2s_final(&state, out);
  88. }
  89. #endif /* _CRYPTO_BLAKE2S_H */