blake2.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * BLAKE2 (RFC 7693) implementation for PuTTY.
  3. *
  4. * The BLAKE2 hash family includes BLAKE2s, in which the hash state is
  5. * operated on as a collection of 32-bit integers, and BLAKE2b, based
  6. * on 64-bit integers. At present this code implements BLAKE2b only.
  7. */
  8. #include <assert.h>
  9. #include "ssh.h"
  10. static inline uint64_t ror(uint64_t x, unsigned rotation)
  11. {
  12. unsigned lshift = 63 & -rotation, rshift = 63 & rotation;
  13. return (x << lshift) | (x >> rshift);
  14. }
  15. /* RFC 7963 section 2.1 */
  16. enum { R1 = 32, R2 = 24, R3 = 16, R4 = 63 };
  17. /* RFC 7693 section 2.6 */
  18. static const uint64_t iv[] = {
  19. 0x6a09e667f3bcc908, /* floor(2^64 * frac(sqrt(2))) */
  20. 0xbb67ae8584caa73b, /* floor(2^64 * frac(sqrt(3))) */
  21. 0x3c6ef372fe94f82b, /* floor(2^64 * frac(sqrt(5))) */
  22. 0xa54ff53a5f1d36f1, /* floor(2^64 * frac(sqrt(7))) */
  23. 0x510e527fade682d1, /* floor(2^64 * frac(sqrt(11))) */
  24. 0x9b05688c2b3e6c1f, /* floor(2^64 * frac(sqrt(13))) */
  25. 0x1f83d9abfb41bd6b, /* floor(2^64 * frac(sqrt(17))) */
  26. 0x5be0cd19137e2179, /* floor(2^64 * frac(sqrt(19))) */
  27. };
  28. /* RFC 7693 section 2.7 */
  29. static const unsigned char sigma[][16] = {
  30. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  31. {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
  32. {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
  33. { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
  34. { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
  35. { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
  36. {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
  37. {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
  38. { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
  39. {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0},
  40. /* This array recycles if you have more than 10 rounds. BLAKE2b
  41. * has 12, so we repeat the first two rows again. */
  42. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  43. {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
  44. };
  45. static inline void g_half(uint64_t v[16], unsigned a, unsigned b, unsigned c,
  46. unsigned d, uint64_t x, unsigned r1, unsigned r2)
  47. {
  48. v[a] += v[b] + x;
  49. v[d] ^= v[a];
  50. v[d] = ror(v[d], r1);
  51. v[c] += v[d];
  52. v[b] ^= v[c];
  53. v[b] = ror(v[b], r2);
  54. }
  55. static inline void g(uint64_t v[16], unsigned a, unsigned b, unsigned c,
  56. unsigned d, uint64_t x, uint64_t y)
  57. {
  58. g_half(v, a, b, c, d, x, R1, R2);
  59. g_half(v, a, b, c, d, y, R3, R4);
  60. }
  61. static inline void f(uint64_t h[8], uint64_t m[16], uint64_t offset_hi,
  62. uint64_t offset_lo, unsigned final)
  63. {
  64. uint64_t v[16];
  65. memcpy(v, h, 8 * sizeof(*v));
  66. memcpy(v + 8, iv, 8 * sizeof(*v));
  67. v[12] ^= offset_lo;
  68. v[13] ^= offset_hi;
  69. v[14] ^= -(uint64_t)final;
  70. for (unsigned round = 0; round < 12; round++) {
  71. const unsigned char *s = sigma[round];
  72. g(v, 0, 4, 8, 12, m[s[ 0]], m[s[ 1]]);
  73. g(v, 1, 5, 9, 13, m[s[ 2]], m[s[ 3]]);
  74. g(v, 2, 6, 10, 14, m[s[ 4]], m[s[ 5]]);
  75. g(v, 3, 7, 11, 15, m[s[ 6]], m[s[ 7]]);
  76. g(v, 0, 5, 10, 15, m[s[ 8]], m[s[ 9]]);
  77. g(v, 1, 6, 11, 12, m[s[10]], m[s[11]]);
  78. g(v, 2, 7, 8, 13, m[s[12]], m[s[13]]);
  79. g(v, 3, 4, 9, 14, m[s[14]], m[s[15]]);
  80. }
  81. for (unsigned i = 0; i < 8; i++)
  82. h[i] ^= v[i] ^ v[i+8];
  83. smemclr(v, sizeof(v));
  84. }
  85. static inline void f_outer(uint64_t h[8], uint8_t blk[128], uint64_t offset_hi,
  86. uint64_t offset_lo, unsigned final)
  87. {
  88. uint64_t m[16];
  89. for (unsigned i = 0; i < 16; i++)
  90. m[i] = GET_64BIT_LSB_FIRST(blk + 8*i);
  91. f(h, m, offset_hi, offset_lo, final);
  92. smemclr(m, sizeof(m));
  93. }
  94. typedef struct blake2b {
  95. uint64_t h[8];
  96. unsigned hashlen;
  97. uint8_t block[128];
  98. size_t used;
  99. uint64_t lenhi, lenlo;
  100. BinarySink_IMPLEMENTATION;
  101. ssh_hash hash;
  102. } blake2b;
  103. static void blake2b_write(BinarySink *bs, const void *vp, size_t len);
  104. static ssh_hash *blake2b_new_inner(unsigned hashlen)
  105. {
  106. assert(hashlen <= ssh_blake2b.hlen);
  107. blake2b *s = snew(blake2b);
  108. s->hash.vt = &ssh_blake2b;
  109. s->hashlen = hashlen;
  110. BinarySink_INIT(s, blake2b_write);
  111. BinarySink_DELEGATE_INIT(&s->hash, s);
  112. return &s->hash;
  113. }
  114. static ssh_hash *blake2b_new(const ssh_hashalg *alg)
  115. {
  116. return blake2b_new_inner(alg->hlen);
  117. }
  118. ssh_hash *blake2b_new_general(unsigned hashlen)
  119. {
  120. ssh_hash *h = blake2b_new_inner(hashlen);
  121. ssh_hash_reset(h);
  122. return h;
  123. }
  124. static void blake2b_reset(ssh_hash *hash)
  125. {
  126. blake2b *s = container_of(hash, blake2b, hash);
  127. /* Initialise the hash to the standard IV */
  128. memcpy(s->h, iv, sizeof(s->h));
  129. /* XOR in the parameters: secret key length (here always 0) in
  130. * byte 1, and hash length in byte 0. */
  131. s->h[0] ^= 0x01010000 ^ s->hashlen;
  132. s->used = 0;
  133. s->lenhi = s->lenlo = 0;
  134. }
  135. static void blake2b_copyfrom(ssh_hash *hcopy, ssh_hash *horig)
  136. {
  137. blake2b *copy = container_of(hcopy, blake2b, hash);
  138. blake2b *orig = container_of(horig, blake2b, hash);
  139. memcpy(copy, orig, sizeof(*copy));
  140. BinarySink_COPIED(copy);
  141. BinarySink_DELEGATE_INIT(&copy->hash, copy);
  142. }
  143. static void blake2b_free(ssh_hash *hash)
  144. {
  145. blake2b *s = container_of(hash, blake2b, hash);
  146. smemclr(s, sizeof(*s));
  147. sfree(s);
  148. }
  149. static void blake2b_write(BinarySink *bs, const void *vp, size_t len)
  150. {
  151. blake2b *s = BinarySink_DOWNCAST(bs, blake2b);
  152. const uint8_t *p = vp;
  153. while (len > 0) {
  154. if (s->used == sizeof(s->block)) {
  155. f_outer(s->h, s->block, s->lenhi, s->lenlo, 0);
  156. s->used = 0;
  157. }
  158. size_t chunk = sizeof(s->block) - s->used;
  159. if (chunk > len)
  160. chunk = len;
  161. memcpy(s->block + s->used, p, chunk);
  162. s->used += chunk;
  163. p += chunk;
  164. len -= chunk;
  165. s->lenlo += chunk;
  166. s->lenhi += (s->lenlo < chunk);
  167. }
  168. }
  169. static void blake2b_digest(ssh_hash *hash, uint8_t *digest)
  170. {
  171. blake2b *s = container_of(hash, blake2b, hash);
  172. memset(s->block + s->used, 0, sizeof(s->block) - s->used);
  173. f_outer(s->h, s->block, s->lenhi, s->lenlo, 1);
  174. uint8_t hash_pre[128];
  175. for (unsigned i = 0; i < 8; i++)
  176. PUT_64BIT_LSB_FIRST(hash_pre + 8*i, s->h[i]);
  177. memcpy(digest, hash_pre, s->hashlen);
  178. smemclr(hash_pre, sizeof(hash_pre));
  179. }
  180. const ssh_hashalg ssh_blake2b = {
  181. .new = blake2b_new,
  182. .reset = blake2b_reset,
  183. .copyfrom = blake2b_copyfrom,
  184. .digest = blake2b_digest,
  185. .free = blake2b_free,
  186. .hlen = 64,
  187. .blocklen = 128,
  188. HASHALG_NAMES_BARE("BLAKE2b-64"),
  189. };