blake2s.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. 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. /**
  21. * struct blake2s_ctx - Context for hashing a message with BLAKE2s
  22. * @h: compression function state
  23. * @t: block counter
  24. * @f: finalization indicator
  25. * @buf: partial block buffer; 'buflen' bytes are valid
  26. * @buflen: number of bytes buffered in @buf
  27. * @outlen: length of output hash value in bytes, at most BLAKE2S_HASH_SIZE
  28. */
  29. struct blake2s_ctx {
  30. /* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */
  31. u32 h[8];
  32. u32 t[2];
  33. u32 f[2];
  34. u8 buf[BLAKE2S_BLOCK_SIZE];
  35. unsigned int buflen;
  36. unsigned int outlen;
  37. };
  38. enum blake2s_iv {
  39. BLAKE2S_IV0 = 0x6A09E667UL,
  40. BLAKE2S_IV1 = 0xBB67AE85UL,
  41. BLAKE2S_IV2 = 0x3C6EF372UL,
  42. BLAKE2S_IV3 = 0xA54FF53AUL,
  43. BLAKE2S_IV4 = 0x510E527FUL,
  44. BLAKE2S_IV5 = 0x9B05688CUL,
  45. BLAKE2S_IV6 = 0x1F83D9ABUL,
  46. BLAKE2S_IV7 = 0x5BE0CD19UL,
  47. };
  48. static inline void __blake2s_init(struct blake2s_ctx *ctx, size_t outlen,
  49. const void *key, size_t keylen)
  50. {
  51. ctx->h[0] = BLAKE2S_IV0 ^ (0x01010000 | keylen << 8 | outlen);
  52. ctx->h[1] = BLAKE2S_IV1;
  53. ctx->h[2] = BLAKE2S_IV2;
  54. ctx->h[3] = BLAKE2S_IV3;
  55. ctx->h[4] = BLAKE2S_IV4;
  56. ctx->h[5] = BLAKE2S_IV5;
  57. ctx->h[6] = BLAKE2S_IV6;
  58. ctx->h[7] = BLAKE2S_IV7;
  59. ctx->t[0] = 0;
  60. ctx->t[1] = 0;
  61. ctx->f[0] = 0;
  62. ctx->f[1] = 0;
  63. ctx->buflen = 0;
  64. ctx->outlen = outlen;
  65. if (keylen) {
  66. memcpy(ctx->buf, key, keylen);
  67. memset(&ctx->buf[keylen], 0, BLAKE2S_BLOCK_SIZE - keylen);
  68. ctx->buflen = BLAKE2S_BLOCK_SIZE;
  69. }
  70. }
  71. /**
  72. * blake2s_init() - Initialize a BLAKE2s context for a new message (unkeyed)
  73. * @ctx: the context to initialize
  74. * @outlen: length of output hash value in bytes, at most BLAKE2S_HASH_SIZE
  75. *
  76. * Context: Any context.
  77. */
  78. static inline void blake2s_init(struct blake2s_ctx *ctx, size_t outlen)
  79. {
  80. __blake2s_init(ctx, outlen, NULL, 0);
  81. }
  82. /**
  83. * blake2s_init_key() - Initialize a BLAKE2s context for a new message (keyed)
  84. * @ctx: the context to initialize
  85. * @outlen: length of output hash value in bytes, at most BLAKE2S_HASH_SIZE
  86. * @key: the key
  87. * @keylen: the key length in bytes, at most BLAKE2S_KEY_SIZE
  88. *
  89. * Context: Any context.
  90. */
  91. static inline void blake2s_init_key(struct blake2s_ctx *ctx, size_t outlen,
  92. const void *key, size_t keylen)
  93. {
  94. WARN_ON(IS_ENABLED(DEBUG) && (!outlen || outlen > BLAKE2S_HASH_SIZE ||
  95. !key || !keylen || keylen > BLAKE2S_KEY_SIZE));
  96. __blake2s_init(ctx, outlen, key, keylen);
  97. }
  98. /**
  99. * blake2s_update() - Update a BLAKE2s context with message data
  100. * @ctx: the context to update; must have been initialized
  101. * @in: the message data
  102. * @inlen: the data length in bytes
  103. *
  104. * This can be called any number of times.
  105. *
  106. * Context: Any context.
  107. */
  108. void blake2s_update(struct blake2s_ctx *ctx, const u8 *in, size_t inlen);
  109. /**
  110. * blake2s_final() - Finish computing a BLAKE2s hash
  111. * @ctx: the context to finalize; must have been initialized
  112. * @out: (output) the resulting BLAKE2s hash. Its length will be equal to the
  113. * @outlen that was passed to blake2s_init() or blake2s_init_key().
  114. *
  115. * After finishing, this zeroizes @ctx. So the caller does not need to do it.
  116. *
  117. * Context: Any context.
  118. */
  119. void blake2s_final(struct blake2s_ctx *ctx, u8 *out);
  120. /**
  121. * blake2s() - Compute BLAKE2s hash in one shot
  122. * @key: the key, or NULL for an unkeyed hash
  123. * @keylen: the key length in bytes (at most BLAKE2S_KEY_SIZE), or 0 for an
  124. * unkeyed hash
  125. * @in: the message data
  126. * @inlen: the data length in bytes
  127. * @out: (output) the resulting BLAKE2s hash, with length @outlen
  128. * @outlen: length of output hash value in bytes, at most BLAKE2S_HASH_SIZE
  129. *
  130. * Context: Any context.
  131. */
  132. static inline void blake2s(const u8 *key, size_t keylen,
  133. const u8 *in, size_t inlen,
  134. u8 *out, size_t outlen)
  135. {
  136. struct blake2s_ctx ctx;
  137. WARN_ON(IS_ENABLED(DEBUG) && ((!in && inlen > 0) || !out || !outlen ||
  138. outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE ||
  139. (!key && keylen)));
  140. __blake2s_init(&ctx, outlen, key, keylen);
  141. blake2s_update(&ctx, in, inlen);
  142. blake2s_final(&ctx, out);
  143. }
  144. #endif /* _CRYPTO_BLAKE2S_H */