You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

352 lines
11 KiB

  1. // Use of this source code is governed by an ISC
  2. // license that can be found in the LICENSE file.
  3. package blake
  4. import (
  5. "fmt"
  6. "gitlab.com/nitya-sattva/go-x11/hash"
  7. )
  8. // HashSize holds the size of a hash in bytes.
  9. const HashSize = int(64)
  10. // BlockSize holds the size of a block in bytes.
  11. const BlockSize = uintptr(128)
  12. ////////////////
  13. type digest struct {
  14. ptr uintptr
  15. h [8]uint64
  16. t [2]uint64
  17. b [BlockSize]byte
  18. }
  19. // New returns a new digest compute a BLAKE512 hash.
  20. func New() hash.Digest {
  21. ref := &digest{}
  22. ref.Reset()
  23. return ref
  24. }
  25. ////////////////
  26. // Reset resets the digest to its initial state.
  27. func (ref *digest) Reset() {
  28. ref.ptr = 0
  29. copy(ref.h[:], kInit[:])
  30. ref.t[0], ref.t[1] = 0, 0
  31. }
  32. // Sum appends the current hash to dst and returns the result
  33. // as a slice. It does not change the underlying hash state.
  34. func (ref *digest) Sum(dst []byte) []byte {
  35. dgt := *ref
  36. hsh := [64]byte{}
  37. dgt.Close(hsh[:], 0, 0)
  38. return append(dst, hsh[:]...)
  39. }
  40. // Write more data to the running hash, never returns an error.
  41. func (ref *digest) Write(src []byte) (int, error) {
  42. sln := uintptr(len(src))
  43. fln := len(src)
  44. ptr := ref.ptr
  45. if sln < (BlockSize - ptr) {
  46. copy(ref.b[ptr:], src)
  47. ref.ptr += sln
  48. return int(sln), nil
  49. }
  50. for sln > 0 {
  51. cln := BlockSize - ptr
  52. if cln > sln {
  53. cln = sln
  54. }
  55. sln -= cln
  56. copy(ref.b[ptr:], src[:cln])
  57. src = src[cln:]
  58. ptr += cln
  59. if ptr == BlockSize {
  60. h := ref.h[:]
  61. t := ref.t[:]
  62. t[0] = t[0] + 1024
  63. if t[0] < 1024 {
  64. t[1] += 1
  65. }
  66. ptr = 0
  67. var vec [16]uint64
  68. vec[0x0] = h[0]
  69. vec[0x1] = h[1]
  70. vec[0x2] = h[2]
  71. vec[0x3] = h[3]
  72. vec[0x4] = h[4]
  73. vec[0x5] = h[5]
  74. vec[0x6] = h[6]
  75. vec[0x7] = h[7]
  76. vec[0x8] = kSpec[0]
  77. vec[0x9] = kSpec[1]
  78. vec[0xA] = kSpec[2]
  79. vec[0xB] = kSpec[3]
  80. vec[0xC] = t[0] ^ kSpec[4]
  81. vec[0xD] = t[0] ^ kSpec[5]
  82. vec[0xE] = t[1] ^ kSpec[6]
  83. vec[0xF] = t[1] ^ kSpec[7]
  84. var mat [16]uint64
  85. mat[0x0] = decUInt64be(ref.b[0:])
  86. mat[0x1] = decUInt64be(ref.b[8:])
  87. mat[0x2] = decUInt64be(ref.b[16:])
  88. mat[0x3] = decUInt64be(ref.b[24:])
  89. mat[0x4] = decUInt64be(ref.b[32:])
  90. mat[0x5] = decUInt64be(ref.b[40:])
  91. mat[0x6] = decUInt64be(ref.b[48:])
  92. mat[0x7] = decUInt64be(ref.b[56:])
  93. mat[0x8] = decUInt64be(ref.b[64:])
  94. mat[0x9] = decUInt64be(ref.b[72:])
  95. mat[0xA] = decUInt64be(ref.b[80:])
  96. mat[0xB] = decUInt64be(ref.b[88:])
  97. mat[0xC] = decUInt64be(ref.b[96:])
  98. mat[0xD] = decUInt64be(ref.b[104:])
  99. mat[0xE] = decUInt64be(ref.b[112:])
  100. mat[0xF] = decUInt64be(ref.b[120:])
  101. var sig []uint8
  102. for r := uint8(0); r < 16; r++ {
  103. sig = kSigma[r][:]
  104. vec[0x0] = (vec[0x0] + vec[0x4] + (mat[sig[0x0]] ^ kSpec[sig[0x1]]))
  105. vec[0xC] = (((vec[0xC] ^ vec[0x0]) << (32)) | ((vec[0xC] ^ vec[0x0]) >> 32))
  106. vec[0x8] = (vec[0x8] + vec[0xC])
  107. vec[0x4] = (((vec[0x4] ^ vec[0x8]) << (39)) | ((vec[0x4] ^ vec[0x8]) >> 25))
  108. vec[0x0] = (vec[0x0] + vec[0x4] + (mat[sig[0x1]] ^ kSpec[sig[0x0]]))
  109. vec[0xC] = (((vec[0xC] ^ vec[0x0]) << (48)) | ((vec[0xC] ^ vec[0x0]) >> 16))
  110. vec[0x8] = (vec[0x8] + vec[0xC])
  111. vec[0x4] = (((vec[0x4] ^ vec[0x8]) << (53)) | ((vec[0x4] ^ vec[0x8]) >> 11))
  112. vec[0x1] = (vec[0x1] + vec[0x5] + (mat[sig[0x2]] ^ kSpec[sig[0x3]]))
  113. vec[0xD] = (((vec[0xD] ^ vec[0x1]) << (32)) | ((vec[0xD] ^ vec[0x1]) >> 32))
  114. vec[0x9] = (vec[0x9] + vec[0xD])
  115. vec[0x5] = (((vec[0x5] ^ vec[0x9]) << (39)) | ((vec[0x5] ^ vec[0x9]) >> 25))
  116. vec[0x1] = (vec[0x1] + vec[0x5] + (mat[sig[0x3]] ^ kSpec[sig[0x2]]))
  117. vec[0xD] = (((vec[0xD] ^ vec[0x1]) << (48)) | ((vec[0xD] ^ vec[0x1]) >> 16))
  118. vec[0x9] = (vec[0x9] + vec[0xD])
  119. vec[0x5] = (((vec[0x5] ^ vec[0x9]) << (53)) | ((vec[0x5] ^ vec[0x9]) >> 11))
  120. vec[0x2] = (vec[0x2] + vec[0x6] + (mat[sig[0x4]] ^ kSpec[sig[0x5]]))
  121. vec[0xE] = (((vec[0xE] ^ vec[0x2]) << (32)) | ((vec[0xE] ^ vec[0x2]) >> 32))
  122. vec[0xA] = (vec[0xA] + vec[0xE])
  123. vec[0x6] = (((vec[0x6] ^ vec[0xA]) << (39)) | ((vec[0x6] ^ vec[0xA]) >> 25))
  124. vec[0x2] = (vec[0x2] + vec[0x6] + (mat[sig[0x5]] ^ kSpec[sig[0x4]]))
  125. vec[0xE] = (((vec[0xE] ^ vec[0x2]) << (48)) | ((vec[0xE] ^ vec[0x2]) >> 16))
  126. vec[0xA] = (vec[0xA] + vec[0xE])
  127. vec[0x6] = (((vec[0x6] ^ vec[0xA]) << (53)) | ((vec[0x6] ^ vec[0xA]) >> 11))
  128. vec[0x3] = (vec[0x3] + vec[0x7] + (mat[sig[0x6]] ^ kSpec[sig[0x7]]))
  129. vec[0xF] = (((vec[0xF] ^ vec[0x3]) << (32)) | ((vec[0xF] ^ vec[0x3]) >> 32))
  130. vec[0xB] = (vec[0xB] + vec[0xF])
  131. vec[0x7] = (((vec[0x7] ^ vec[0xB]) << (39)) | ((vec[0x7] ^ vec[0xB]) >> 25))
  132. vec[0x3] = (vec[0x3] + vec[0x7] + (mat[sig[0x7]] ^ kSpec[sig[0x6]]))
  133. vec[0xF] = (((vec[0xF] ^ vec[0x3]) << (48)) | ((vec[0xF] ^ vec[0x3]) >> 16))
  134. vec[0xB] = (vec[0xB] + vec[0xF])
  135. vec[0x7] = (((vec[0x7] ^ vec[0xB]) << (53)) | ((vec[0x7] ^ vec[0xB]) >> 11))
  136. vec[0x0] = (vec[0x0] + vec[0x5] + (mat[sig[0x8]] ^ kSpec[sig[0x9]]))
  137. vec[0xF] = (((vec[0xF] ^ vec[0x0]) << (32)) | ((vec[0xF] ^ vec[0x0]) >> 32))
  138. vec[0xA] = (vec[0xA] + vec[0xF])
  139. vec[0x5] = (((vec[0x5] ^ vec[0xA]) << (39)) | ((vec[0x5] ^ vec[0xA]) >> 25))
  140. vec[0x0] = (vec[0x0] + vec[0x5] + (mat[sig[0x9]] ^ kSpec[sig[0x8]]))
  141. vec[0xF] = (((vec[0xF] ^ vec[0x0]) << (48)) | ((vec[0xF] ^ vec[0x0]) >> 16))
  142. vec[0xA] = (vec[0xA] + vec[0xF])
  143. vec[0x5] = (((vec[0x5] ^ vec[0xA]) << (53)) | ((vec[0x5] ^ vec[0xA]) >> 11))
  144. vec[0x1] = (vec[0x1] + vec[0x6] + (mat[sig[0xA]] ^ kSpec[sig[0xB]]))
  145. vec[0xC] = (((vec[0xC] ^ vec[0x1]) << (32)) | ((vec[0xC] ^ vec[0x1]) >> 32))
  146. vec[0xB] = (vec[0xB] + vec[0xC])
  147. vec[0x6] = (((vec[0x6] ^ vec[0xB]) << (39)) | ((vec[0x6] ^ vec[0xB]) >> 25))
  148. vec[0x1] = (vec[0x1] + vec[0x6] + (mat[sig[0xB]] ^ kSpec[sig[0xA]]))
  149. vec[0xC] = (((vec[0xC] ^ vec[0x1]) << (48)) | ((vec[0xC] ^ vec[0x1]) >> 16))
  150. vec[0xB] = (vec[0xB] + vec[0xC])
  151. vec[0x6] = (((vec[0x6] ^ vec[0xB]) << (53)) | ((vec[0x6] ^ vec[0xB]) >> 11))
  152. vec[0x2] = (vec[0x2] + vec[0x7] + (mat[sig[0xC]] ^ kSpec[sig[0xD]]))
  153. vec[0xD] = (((vec[0xD] ^ vec[0x2]) << (32)) | ((vec[0xD] ^ vec[0x2]) >> 32))
  154. vec[0x8] = (vec[0x8] + vec[0xD])
  155. vec[0x7] = (((vec[0x7] ^ vec[0x8]) << (39)) | ((vec[0x7] ^ vec[0x8]) >> 25))
  156. vec[0x2] = (vec[0x2] + vec[0x7] + (mat[sig[0xD]] ^ kSpec[sig[0xC]]))
  157. vec[0xD] = (((vec[0xD] ^ vec[0x2]) << (48)) | ((vec[0xD] ^ vec[0x2]) >> 16))
  158. vec[0x8] = (vec[0x8] + vec[0xD])
  159. vec[0x7] = (((vec[0x7] ^ vec[0x8]) << (53)) | ((vec[0x7] ^ vec[0x8]) >> 11))
  160. vec[0x3] = (vec[0x3] + vec[0x4] + (mat[sig[0xE]] ^ kSpec[sig[0xF]]))
  161. vec[0xE] = (((vec[0xE] ^ vec[0x3]) << (32)) | ((vec[0xE] ^ vec[0x3]) >> 32))
  162. vec[0x9] = (vec[0x9] + vec[0xE])
  163. vec[0x4] = (((vec[0x4] ^ vec[0x9]) << (39)) | ((vec[0x4] ^ vec[0x9]) >> 25))
  164. vec[0x3] = (vec[0x3] + vec[0x4] + (mat[sig[0xF]] ^ kSpec[sig[0xE]]))
  165. vec[0xE] = (((vec[0xE] ^ vec[0x3]) << (48)) | ((vec[0xE] ^ vec[0x3]) >> 16))
  166. vec[0x9] = (vec[0x9] + vec[0xE])
  167. vec[0x4] = (((vec[0x4] ^ vec[0x9]) << (53)) | ((vec[0x4] ^ vec[0x9]) >> 11))
  168. }
  169. h[0] ^= vec[0x0] ^ vec[0x8]
  170. h[1] ^= vec[0x1] ^ vec[0x9]
  171. h[2] ^= vec[0x2] ^ vec[0xA]
  172. h[3] ^= vec[0x3] ^ vec[0xB]
  173. h[4] ^= vec[0x4] ^ vec[0xC]
  174. h[5] ^= vec[0x5] ^ vec[0xD]
  175. h[6] ^= vec[0x6] ^ vec[0xE]
  176. h[7] ^= vec[0x7] ^ vec[0xF]
  177. }
  178. }
  179. ref.ptr = ptr
  180. return fln, nil
  181. }
  182. // Close the digest by writing the last bits and storing the hash
  183. // in dst. This prepares the digest for reuse by calling reset. A call
  184. // to Close with a dst that is smaller then HashSize will return an error.
  185. func (ref *digest) Close(dst []byte, bits uint8, bcnt uint8) error {
  186. if ln := len(dst); HashSize > ln {
  187. return fmt.Errorf("Blake Close: dst min length: %d, got %d", HashSize, ln)
  188. }
  189. ptr := ref.ptr
  190. bln := uint64((ref.ptr << 3) + uintptr(bcnt))
  191. tpl := ref.t[0] + bln
  192. tph := ref.t[1]
  193. var buf [BlockSize]uint8
  194. {
  195. off := uint8(0x80) >> bcnt
  196. buf[ptr] = uint8((bits & -off) | off)
  197. }
  198. if ptr == 0 && bcnt == 0 {
  199. ref.t[0] = uint64(0xFFFFFFFFFFFFFC00)
  200. ref.t[1] = uint64(0xFFFFFFFFFFFFFFFF)
  201. } else if ref.t[0] == 0 {
  202. ref.t[0] = uint64(0xFFFFFFFFFFFFFC00) + bln
  203. ref.t[1] = uint64(ref.t[1] - 1)
  204. } else {
  205. ref.t[0] -= 1024 - bln
  206. }
  207. if bln <= 894 {
  208. buf[111] |= 1
  209. encUInt64be(buf[112:], tph)
  210. encUInt64be(buf[120:], tpl)
  211. ref.Write(buf[ptr:])
  212. } else {
  213. ref.Write(buf[ptr:])
  214. ref.t[0] = uint64(0xFFFFFFFFFFFFFC00)
  215. ref.t[1] = uint64(0xFFFFFFFFFFFFFFFF)
  216. memset(buf[:112], 0)
  217. buf[111] = 1
  218. encUInt64be(buf[112:], tph)
  219. encUInt64be(buf[120:], tpl)
  220. ref.Write(buf[:])
  221. }
  222. for k := uintptr(0); k < 8; k++ {
  223. encUInt64be(dst[(k<<3):], ref.h[k])
  224. }
  225. ref.Reset()
  226. return nil
  227. }
  228. // Size returns the number of bytes Sum will return.
  229. func (*digest) Size() int {
  230. return HashSize
  231. }
  232. // BlockSize returns the block size of the hash.
  233. func (*digest) BlockSize() int {
  234. return int(BlockSize)
  235. }
  236. ////////////////
  237. func memset(dst []byte, src byte) {
  238. for i := range dst {
  239. dst[i] = src
  240. }
  241. }
  242. func decUInt64be(src []byte) uint64 {
  243. return (uint64(src[0])<<56 |
  244. uint64(src[1])<<48 |
  245. uint64(src[2])<<40 |
  246. uint64(src[3])<<32 |
  247. uint64(src[4])<<24 |
  248. uint64(src[5])<<16 |
  249. uint64(src[6])<<8 |
  250. uint64(src[7]))
  251. }
  252. func encUInt64be(dst []byte, src uint64) {
  253. dst[0] = uint8(src >> 56)
  254. dst[1] = uint8(src >> 48)
  255. dst[2] = uint8(src >> 40)
  256. dst[3] = uint8(src >> 32)
  257. dst[4] = uint8(src >> 24)
  258. dst[5] = uint8(src >> 16)
  259. dst[6] = uint8(src >> 8)
  260. dst[7] = uint8(src)
  261. }
  262. ////////////////
  263. var kInit = [8]uint64{
  264. uint64(0x6A09E667F3BCC908), uint64(0xBB67AE8584CAA73B),
  265. uint64(0x3C6EF372FE94F82B), uint64(0xA54FF53A5F1D36F1),
  266. uint64(0x510E527FADE682D1), uint64(0x9B05688C2B3E6C1F),
  267. uint64(0x1F83D9ABFB41BD6B), uint64(0x5BE0CD19137E2179),
  268. }
  269. var kSpec = [16]uint64{
  270. uint64(0x243F6A8885A308D3), uint64(0x13198A2E03707344),
  271. uint64(0xA4093822299F31D0), uint64(0x082EFA98EC4E6C89),
  272. uint64(0x452821E638D01377), uint64(0xBE5466CF34E90C6C),
  273. uint64(0xC0AC29B7C97C50DD), uint64(0x3F84D5B5B5470917),
  274. uint64(0x9216D5D98979FB1B), uint64(0xD1310BA698DFB5AC),
  275. uint64(0x2FFD72DBD01ADFB7), uint64(0xB8E1AFED6A267E96),
  276. uint64(0xBA7C9045F12C7F99), uint64(0x24A19947B3916CF7),
  277. uint64(0x0801F2E2858EFC16), uint64(0x636920D871574E69),
  278. }
  279. var kSigma = [16][16]uint8{
  280. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  281. {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
  282. {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
  283. {7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
  284. {9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
  285. {2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
  286. {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
  287. {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
  288. {6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
  289. {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0},
  290. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  291. {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
  292. {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
  293. {7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
  294. {9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
  295. {2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
  296. }