blake2b_generic.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package blake2b
  5. import (
  6. "encoding/binary"
  7. "math/bits"
  8. )
  9. // the precomputed values for BLAKE2b
  10. // there are 12 16-byte arrays - one for each round
  11. // the entries are calculated from the sigma constants.
  12. var precomputed = [12][16]byte{
  13. {0, 2, 4, 6, 1, 3, 5, 7, 8, 10, 12, 14, 9, 11, 13, 15},
  14. {14, 4, 9, 13, 10, 8, 15, 6, 1, 0, 11, 5, 12, 2, 7, 3},
  15. {11, 12, 5, 15, 8, 0, 2, 13, 10, 3, 7, 9, 14, 6, 1, 4},
  16. {7, 3, 13, 11, 9, 1, 12, 14, 2, 5, 4, 15, 6, 10, 0, 8},
  17. {9, 5, 2, 10, 0, 7, 4, 15, 14, 11, 6, 3, 1, 12, 8, 13},
  18. {2, 6, 0, 8, 12, 10, 11, 3, 4, 7, 15, 1, 13, 5, 14, 9},
  19. {12, 1, 14, 4, 5, 15, 13, 10, 0, 6, 9, 8, 7, 3, 2, 11},
  20. {13, 7, 12, 3, 11, 14, 1, 9, 5, 15, 8, 2, 0, 4, 6, 10},
  21. {6, 14, 11, 0, 15, 9, 3, 8, 12, 13, 1, 10, 2, 7, 4, 5},
  22. {10, 8, 7, 1, 2, 4, 6, 5, 15, 9, 3, 13, 11, 14, 12, 0},
  23. {0, 2, 4, 6, 1, 3, 5, 7, 8, 10, 12, 14, 9, 11, 13, 15}, // equal to the first
  24. {14, 4, 9, 13, 10, 8, 15, 6, 1, 0, 11, 5, 12, 2, 7, 3}, // equal to the second
  25. }
  26. func hashBlocksGeneric(h *[8]uint64, c *[2]uint64, flag uint64, blocks []byte) {
  27. var m [16]uint64
  28. c0, c1 := c[0], c[1]
  29. for i := 0; i < len(blocks); {
  30. c0 += BlockSize
  31. if c0 < BlockSize {
  32. c1++
  33. }
  34. v0, v1, v2, v3, v4, v5, v6, v7 := h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]
  35. v8, v9, v10, v11, v12, v13, v14, v15 := iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]
  36. v12 ^= c0
  37. v13 ^= c1
  38. v14 ^= flag
  39. for j := range m {
  40. m[j] = binary.LittleEndian.Uint64(blocks[i:])
  41. i += 8
  42. }
  43. for j := range precomputed {
  44. s := &(precomputed[j])
  45. v0 += m[s[0]]
  46. v0 += v4
  47. v12 ^= v0
  48. v12 = bits.RotateLeft64(v12, -32)
  49. v8 += v12
  50. v4 ^= v8
  51. v4 = bits.RotateLeft64(v4, -24)
  52. v1 += m[s[1]]
  53. v1 += v5
  54. v13 ^= v1
  55. v13 = bits.RotateLeft64(v13, -32)
  56. v9 += v13
  57. v5 ^= v9
  58. v5 = bits.RotateLeft64(v5, -24)
  59. v2 += m[s[2]]
  60. v2 += v6
  61. v14 ^= v2
  62. v14 = bits.RotateLeft64(v14, -32)
  63. v10 += v14
  64. v6 ^= v10
  65. v6 = bits.RotateLeft64(v6, -24)
  66. v3 += m[s[3]]
  67. v3 += v7
  68. v15 ^= v3
  69. v15 = bits.RotateLeft64(v15, -32)
  70. v11 += v15
  71. v7 ^= v11
  72. v7 = bits.RotateLeft64(v7, -24)
  73. v0 += m[s[4]]
  74. v0 += v4
  75. v12 ^= v0
  76. v12 = bits.RotateLeft64(v12, -16)
  77. v8 += v12
  78. v4 ^= v8
  79. v4 = bits.RotateLeft64(v4, -63)
  80. v1 += m[s[5]]
  81. v1 += v5
  82. v13 ^= v1
  83. v13 = bits.RotateLeft64(v13, -16)
  84. v9 += v13
  85. v5 ^= v9
  86. v5 = bits.RotateLeft64(v5, -63)
  87. v2 += m[s[6]]
  88. v2 += v6
  89. v14 ^= v2
  90. v14 = bits.RotateLeft64(v14, -16)
  91. v10 += v14
  92. v6 ^= v10
  93. v6 = bits.RotateLeft64(v6, -63)
  94. v3 += m[s[7]]
  95. v3 += v7
  96. v15 ^= v3
  97. v15 = bits.RotateLeft64(v15, -16)
  98. v11 += v15
  99. v7 ^= v11
  100. v7 = bits.RotateLeft64(v7, -63)
  101. v0 += m[s[8]]
  102. v0 += v5
  103. v15 ^= v0
  104. v15 = bits.RotateLeft64(v15, -32)
  105. v10 += v15
  106. v5 ^= v10
  107. v5 = bits.RotateLeft64(v5, -24)
  108. v1 += m[s[9]]
  109. v1 += v6
  110. v12 ^= v1
  111. v12 = bits.RotateLeft64(v12, -32)
  112. v11 += v12
  113. v6 ^= v11
  114. v6 = bits.RotateLeft64(v6, -24)
  115. v2 += m[s[10]]
  116. v2 += v7
  117. v13 ^= v2
  118. v13 = bits.RotateLeft64(v13, -32)
  119. v8 += v13
  120. v7 ^= v8
  121. v7 = bits.RotateLeft64(v7, -24)
  122. v3 += m[s[11]]
  123. v3 += v4
  124. v14 ^= v3
  125. v14 = bits.RotateLeft64(v14, -32)
  126. v9 += v14
  127. v4 ^= v9
  128. v4 = bits.RotateLeft64(v4, -24)
  129. v0 += m[s[12]]
  130. v0 += v5
  131. v15 ^= v0
  132. v15 = bits.RotateLeft64(v15, -16)
  133. v10 += v15
  134. v5 ^= v10
  135. v5 = bits.RotateLeft64(v5, -63)
  136. v1 += m[s[13]]
  137. v1 += v6
  138. v12 ^= v1
  139. v12 = bits.RotateLeft64(v12, -16)
  140. v11 += v12
  141. v6 ^= v11
  142. v6 = bits.RotateLeft64(v6, -63)
  143. v2 += m[s[14]]
  144. v2 += v7
  145. v13 ^= v2
  146. v13 = bits.RotateLeft64(v13, -16)
  147. v8 += v13
  148. v7 ^= v8
  149. v7 = bits.RotateLeft64(v7, -63)
  150. v3 += m[s[15]]
  151. v3 += v4
  152. v14 ^= v3
  153. v14 = bits.RotateLeft64(v14, -16)
  154. v9 += v14
  155. v4 ^= v9
  156. v4 = bits.RotateLeft64(v4, -63)
  157. }
  158. h[0] ^= v0 ^ v8
  159. h[1] ^= v1 ^ v9
  160. h[2] ^= v2 ^ v10
  161. h[3] ^= v3 ^ v11
  162. h[4] ^= v4 ^ v12
  163. h[5] ^= v5 ^ v13
  164. h[6] ^= v6 ^ v14
  165. h[7] ^= v7 ^ v15
  166. }
  167. c[0], c[1] = c0, c1
  168. }