blake2s.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. * This is an implementation of the BLAKE2s hash and PRF functions.
  6. *
  7. * Information: https://blake2.net/
  8. *
  9. */
  10. #include <crypto/blake2s.h>
  11. #include <linux/bug.h>
  12. #include <linux/export.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/string.h>
  16. #include <linux/unroll.h>
  17. #include <linux/types.h>
  18. static const u8 blake2s_sigma[10][16] = {
  19. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
  20. { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
  21. { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
  22. { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
  23. { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
  24. { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
  25. { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
  26. { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
  27. { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
  28. { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
  29. };
  30. static inline void blake2s_increment_counter(struct blake2s_ctx *ctx, u32 inc)
  31. {
  32. ctx->t[0] += inc;
  33. ctx->t[1] += (ctx->t[0] < inc);
  34. }
  35. static void __maybe_unused
  36. blake2s_compress_generic(struct blake2s_ctx *ctx,
  37. const u8 *data, size_t nblocks, u32 inc)
  38. {
  39. u32 m[16];
  40. u32 v[16];
  41. int i;
  42. WARN_ON(IS_ENABLED(DEBUG) &&
  43. (nblocks > 1 && inc != BLAKE2S_BLOCK_SIZE));
  44. while (nblocks > 0) {
  45. blake2s_increment_counter(ctx, inc);
  46. memcpy(m, data, BLAKE2S_BLOCK_SIZE);
  47. le32_to_cpu_array(m, ARRAY_SIZE(m));
  48. memcpy(v, ctx->h, 32);
  49. v[ 8] = BLAKE2S_IV0;
  50. v[ 9] = BLAKE2S_IV1;
  51. v[10] = BLAKE2S_IV2;
  52. v[11] = BLAKE2S_IV3;
  53. v[12] = BLAKE2S_IV4 ^ ctx->t[0];
  54. v[13] = BLAKE2S_IV5 ^ ctx->t[1];
  55. v[14] = BLAKE2S_IV6 ^ ctx->f[0];
  56. v[15] = BLAKE2S_IV7 ^ ctx->f[1];
  57. #define G(r, i, a, b, c, d) do { \
  58. a += b + m[blake2s_sigma[r][2 * i + 0]]; \
  59. d = ror32(d ^ a, 16); \
  60. c += d; \
  61. b = ror32(b ^ c, 12); \
  62. a += b + m[blake2s_sigma[r][2 * i + 1]]; \
  63. d = ror32(d ^ a, 8); \
  64. c += d; \
  65. b = ror32(b ^ c, 7); \
  66. } while (0)
  67. /*
  68. * Unroll the rounds loop to enable constant-folding of the
  69. * blake2s_sigma values.
  70. */
  71. unrolled_full
  72. for (int r = 0; r < 10; r++) {
  73. G(r, 0, v[0], v[4], v[8], v[12]);
  74. G(r, 1, v[1], v[5], v[9], v[13]);
  75. G(r, 2, v[2], v[6], v[10], v[14]);
  76. G(r, 3, v[3], v[7], v[11], v[15]);
  77. G(r, 4, v[0], v[5], v[10], v[15]);
  78. G(r, 5, v[1], v[6], v[11], v[12]);
  79. G(r, 6, v[2], v[7], v[8], v[13]);
  80. G(r, 7, v[3], v[4], v[9], v[14]);
  81. }
  82. #undef G
  83. for (i = 0; i < 8; ++i)
  84. ctx->h[i] ^= v[i] ^ v[i + 8];
  85. data += BLAKE2S_BLOCK_SIZE;
  86. --nblocks;
  87. }
  88. }
  89. #ifdef CONFIG_CRYPTO_LIB_BLAKE2S_ARCH
  90. #include "blake2s.h" /* $(SRCARCH)/blake2s.h */
  91. #else
  92. #define blake2s_compress blake2s_compress_generic
  93. #endif
  94. static inline void blake2s_set_lastblock(struct blake2s_ctx *ctx)
  95. {
  96. ctx->f[0] = -1;
  97. }
  98. void blake2s_update(struct blake2s_ctx *ctx, const u8 *in, size_t inlen)
  99. {
  100. const size_t fill = BLAKE2S_BLOCK_SIZE - ctx->buflen;
  101. if (unlikely(!inlen))
  102. return;
  103. if (inlen > fill) {
  104. memcpy(ctx->buf + ctx->buflen, in, fill);
  105. blake2s_compress(ctx, ctx->buf, 1, BLAKE2S_BLOCK_SIZE);
  106. ctx->buflen = 0;
  107. in += fill;
  108. inlen -= fill;
  109. }
  110. if (inlen > BLAKE2S_BLOCK_SIZE) {
  111. const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2S_BLOCK_SIZE);
  112. blake2s_compress(ctx, in, nblocks - 1, BLAKE2S_BLOCK_SIZE);
  113. in += BLAKE2S_BLOCK_SIZE * (nblocks - 1);
  114. inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1);
  115. }
  116. memcpy(ctx->buf + ctx->buflen, in, inlen);
  117. ctx->buflen += inlen;
  118. }
  119. EXPORT_SYMBOL(blake2s_update);
  120. void blake2s_final(struct blake2s_ctx *ctx, u8 *out)
  121. {
  122. WARN_ON(IS_ENABLED(DEBUG) && !out);
  123. blake2s_set_lastblock(ctx);
  124. memset(ctx->buf + ctx->buflen, 0,
  125. BLAKE2S_BLOCK_SIZE - ctx->buflen); /* Padding */
  126. blake2s_compress(ctx, ctx->buf, 1, ctx->buflen);
  127. cpu_to_le32_array(ctx->h, ARRAY_SIZE(ctx->h));
  128. memcpy(out, ctx->h, ctx->outlen);
  129. memzero_explicit(ctx, sizeof(*ctx));
  130. }
  131. EXPORT_SYMBOL(blake2s_final);
  132. #ifdef blake2s_mod_init_arch
  133. static int __init blake2s_mod_init(void)
  134. {
  135. blake2s_mod_init_arch();
  136. return 0;
  137. }
  138. subsys_initcall(blake2s_mod_init);
  139. #endif
  140. MODULE_DESCRIPTION("BLAKE2s hash function");
  141. MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");