blake2b.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /**
  17. * struct blake2b_ctx - Context for hashing a message with BLAKE2b
  18. * @h: compression function state
  19. * @t: block counter
  20. * @f: finalization indicator
  21. * @buf: partial block buffer; 'buflen' bytes are valid
  22. * @buflen: number of bytes buffered in @buf
  23. * @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE
  24. */
  25. struct blake2b_ctx {
  26. /* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */
  27. u64 h[8];
  28. u64 t[2];
  29. u64 f[2];
  30. u8 buf[BLAKE2B_BLOCK_SIZE];
  31. unsigned int buflen;
  32. unsigned int outlen;
  33. };
  34. enum blake2b_iv {
  35. BLAKE2B_IV0 = 0x6A09E667F3BCC908ULL,
  36. BLAKE2B_IV1 = 0xBB67AE8584CAA73BULL,
  37. BLAKE2B_IV2 = 0x3C6EF372FE94F82BULL,
  38. BLAKE2B_IV3 = 0xA54FF53A5F1D36F1ULL,
  39. BLAKE2B_IV4 = 0x510E527FADE682D1ULL,
  40. BLAKE2B_IV5 = 0x9B05688C2B3E6C1FULL,
  41. BLAKE2B_IV6 = 0x1F83D9ABFB41BD6BULL,
  42. BLAKE2B_IV7 = 0x5BE0CD19137E2179ULL,
  43. };
  44. static inline void __blake2b_init(struct blake2b_ctx *ctx, size_t outlen,
  45. const void *key, size_t keylen)
  46. {
  47. ctx->h[0] = BLAKE2B_IV0 ^ (0x01010000 | keylen << 8 | outlen);
  48. ctx->h[1] = BLAKE2B_IV1;
  49. ctx->h[2] = BLAKE2B_IV2;
  50. ctx->h[3] = BLAKE2B_IV3;
  51. ctx->h[4] = BLAKE2B_IV4;
  52. ctx->h[5] = BLAKE2B_IV5;
  53. ctx->h[6] = BLAKE2B_IV6;
  54. ctx->h[7] = BLAKE2B_IV7;
  55. ctx->t[0] = 0;
  56. ctx->t[1] = 0;
  57. ctx->f[0] = 0;
  58. ctx->f[1] = 0;
  59. ctx->buflen = 0;
  60. ctx->outlen = outlen;
  61. if (keylen) {
  62. memcpy(ctx->buf, key, keylen);
  63. memset(&ctx->buf[keylen], 0, BLAKE2B_BLOCK_SIZE - keylen);
  64. ctx->buflen = BLAKE2B_BLOCK_SIZE;
  65. }
  66. }
  67. /**
  68. * blake2b_init() - Initialize a BLAKE2b context for a new message (unkeyed)
  69. * @ctx: the context to initialize
  70. * @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE
  71. *
  72. * Context: Any context.
  73. */
  74. static inline void blake2b_init(struct blake2b_ctx *ctx, size_t outlen)
  75. {
  76. __blake2b_init(ctx, outlen, NULL, 0);
  77. }
  78. /**
  79. * blake2b_init_key() - Initialize a BLAKE2b context for a new message (keyed)
  80. * @ctx: the context to initialize
  81. * @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE
  82. * @key: the key
  83. * @keylen: the key length in bytes, at most BLAKE2B_KEY_SIZE
  84. *
  85. * Context: Any context.
  86. */
  87. static inline void blake2b_init_key(struct blake2b_ctx *ctx, size_t outlen,
  88. const void *key, size_t keylen)
  89. {
  90. WARN_ON(IS_ENABLED(DEBUG) && (!outlen || outlen > BLAKE2B_HASH_SIZE ||
  91. !key || !keylen || keylen > BLAKE2B_KEY_SIZE));
  92. __blake2b_init(ctx, outlen, key, keylen);
  93. }
  94. /**
  95. * blake2b_update() - Update a BLAKE2b context with message data
  96. * @ctx: the context to update; must have been initialized
  97. * @in: the message data
  98. * @inlen: the data length in bytes
  99. *
  100. * This can be called any number of times.
  101. *
  102. * Context: Any context.
  103. */
  104. void blake2b_update(struct blake2b_ctx *ctx, const u8 *in, size_t inlen);
  105. /**
  106. * blake2b_final() - Finish computing a BLAKE2b hash
  107. * @ctx: the context to finalize; must have been initialized
  108. * @out: (output) the resulting BLAKE2b hash. Its length will be equal to the
  109. * @outlen that was passed to blake2b_init() or blake2b_init_key().
  110. *
  111. * After finishing, this zeroizes @ctx. So the caller does not need to do it.
  112. *
  113. * Context: Any context.
  114. */
  115. void blake2b_final(struct blake2b_ctx *ctx, u8 *out);
  116. /**
  117. * blake2b() - Compute BLAKE2b hash in one shot
  118. * @key: the key, or NULL for an unkeyed hash
  119. * @keylen: the key length in bytes (at most BLAKE2B_KEY_SIZE), or 0 for an
  120. * unkeyed hash
  121. * @in: the message data
  122. * @inlen: the data length in bytes
  123. * @out: (output) the resulting BLAKE2b hash, with length @outlen
  124. * @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE
  125. *
  126. * Context: Any context.
  127. */
  128. static inline void blake2b(const u8 *key, size_t keylen,
  129. const u8 *in, size_t inlen,
  130. u8 *out, size_t outlen)
  131. {
  132. struct blake2b_ctx ctx;
  133. WARN_ON(IS_ENABLED(DEBUG) && ((!in && inlen > 0) || !out || !outlen ||
  134. outlen > BLAKE2B_HASH_SIZE || keylen > BLAKE2B_KEY_SIZE ||
  135. (!key && keylen)));
  136. __blake2b_init(&ctx, outlen, key, keylen);
  137. blake2b_update(&ctx, in, inlen);
  138. blake2b_final(&ctx, out);
  139. }
  140. #endif /* _CRYPTO_BLAKE2B_H */