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.

403 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 cubed
  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(32)
  12. ////////////////
  13. type digest struct {
  14. ptr uintptr
  15. h [32]uint32
  16. b [BlockSize]byte
  17. }
  18. // New returns a new digest compute a CUBEHASH512 hash.
  19. func New() hash.Digest {
  20. ref := &digest{}
  21. ref.Reset()
  22. return ref
  23. }
  24. ////////////////
  25. // Reset resets the digest to its initial state.
  26. func (ref *digest) Reset() {
  27. ref.ptr = 0
  28. copy(ref.h[:], kInit[:])
  29. }
  30. // Sum appends the current hash to dst and returns the result
  31. // as a slice. It does not change the underlying hash state.
  32. func (ref *digest) Sum(dst []byte) []byte {
  33. dgt := *ref
  34. hsh := [64]byte{}
  35. dgt.Close(hsh[:], 0, 0)
  36. return append(dst, hsh[:]...)
  37. }
  38. // Write more data to the running hash, never returns an error.
  39. func (ref *digest) Write(src []byte) (int, error) {
  40. sln := uintptr(len(src))
  41. fln := len(src)
  42. ptr := ref.ptr
  43. if sln < (BlockSize - ptr) {
  44. copy(ref.b[ptr:], src[:sln])
  45. ref.ptr += sln
  46. return int(sln), nil
  47. }
  48. st := ref.h[:]
  49. buf := ref.b[:]
  50. for sln > 0 {
  51. cln := BlockSize - ptr
  52. if cln > sln {
  53. cln = sln
  54. }
  55. sln -= cln
  56. copy(buf[ptr:], src[:cln])
  57. src = src[cln:]
  58. ptr += cln
  59. if ptr == BlockSize {
  60. st[0x00] ^= decUInt32le(buf[0:])
  61. st[0x01] ^= decUInt32le(buf[4:])
  62. st[0x02] ^= decUInt32le(buf[8:])
  63. st[0x03] ^= decUInt32le(buf[12:])
  64. st[0x04] ^= decUInt32le(buf[16:])
  65. st[0x05] ^= decUInt32le(buf[20:])
  66. st[0x06] ^= decUInt32le(buf[24:])
  67. st[0x07] ^= decUInt32le(buf[28:])
  68. runRounds(st)
  69. runRounds(st)
  70. runRounds(st)
  71. runRounds(st)
  72. runRounds(st)
  73. runRounds(st)
  74. runRounds(st)
  75. runRounds(st)
  76. ptr = 0
  77. }
  78. }
  79. ref.ptr = ptr
  80. return fln, nil
  81. }
  82. // Close the digest by writing the last bits and storing the hash
  83. // in dst. This prepares the digest for reuse by calling reset. A call
  84. // to Close with a dst that is smaller then HashSize will return an error.
  85. func (ref *digest) Close(dst []byte, bits uint8, bcnt uint8) error {
  86. if ln := len(dst); HashSize > ln {
  87. return fmt.Errorf("Cubed Close: dst min length: %d, got %d", HashSize, ln)
  88. }
  89. st := ref.h[:]
  90. {
  91. buf := ref.b[:]
  92. ptr := ref.ptr + 1
  93. z := uint8(0x80) >> bcnt
  94. buf[ref.ptr] = uint8((bits & -z) | z)
  95. memset(buf[ptr:], 0)
  96. st[0x00] ^= decUInt32le(buf[0:])
  97. st[0x01] ^= decUInt32le(buf[4:])
  98. st[0x02] ^= decUInt32le(buf[8:])
  99. st[0x03] ^= decUInt32le(buf[12:])
  100. st[0x04] ^= decUInt32le(buf[16:])
  101. st[0x05] ^= decUInt32le(buf[20:])
  102. st[0x06] ^= decUInt32le(buf[24:])
  103. st[0x07] ^= decUInt32le(buf[28:])
  104. }
  105. for i := uint8(0); i < 11; i++ {
  106. runRounds(st)
  107. runRounds(st)
  108. runRounds(st)
  109. runRounds(st)
  110. runRounds(st)
  111. runRounds(st)
  112. runRounds(st)
  113. runRounds(st)
  114. if i == 0 {
  115. st[0x1F] ^= uint32(1)
  116. }
  117. }
  118. for i := uint8(0); i < 16; i++ {
  119. encUInt32le(dst[(i<<2):], ref.h[i])
  120. }
  121. ref.Reset()
  122. return nil
  123. }
  124. // Size returns the number of bytes required to store the hash.
  125. func (*digest) Size() int {
  126. return int(HashSize)
  127. }
  128. // BlockSize returns the block size of the hash.
  129. func (*digest) BlockSize() int {
  130. return int(BlockSize)
  131. }
  132. ////////////////
  133. func memset(dst []byte, src byte) {
  134. for i := range dst {
  135. dst[i] = src
  136. }
  137. }
  138. func decUInt32le(src []byte) uint32 {
  139. return (uint32(src[0]) |
  140. uint32(src[1])<<8 |
  141. uint32(src[2])<<16 |
  142. uint32(src[3])<<24)
  143. }
  144. func encUInt32le(dst []byte, src uint32) {
  145. dst[0] = uint8(src)
  146. dst[1] = uint8(src >> 8)
  147. dst[2] = uint8(src >> 16)
  148. dst[3] = uint8(src >> 24)
  149. }
  150. func runRounds(st []uint32) {
  151. st[0x10] = (st[0x00] + st[0x10])
  152. st[0x00] = (st[0x00] << 7) | (st[0x00] >> (32 - 7))
  153. st[0x11] = (st[0x01] + st[0x11])
  154. st[0x01] = (st[0x01] << 7) | (st[0x01] >> (32 - 7))
  155. st[0x12] = (st[0x02] + st[0x12])
  156. st[0x02] = (st[0x02] << 7) | (st[0x02] >> (32 - 7))
  157. st[0x13] = (st[0x03] + st[0x13])
  158. st[0x03] = (st[0x03] << 7) | (st[0x03] >> (32 - 7))
  159. st[0x14] = (st[0x04] + st[0x14])
  160. st[0x04] = (st[0x04] << 7) | (st[0x04] >> (32 - 7))
  161. st[0x15] = (st[0x05] + st[0x15])
  162. st[0x05] = (st[0x05] << 7) | (st[0x05] >> (32 - 7))
  163. st[0x16] = (st[0x06] + st[0x16])
  164. st[0x06] = (st[0x06] << 7) | (st[0x06] >> (32 - 7))
  165. st[0x17] = (st[0x07] + st[0x17])
  166. st[0x07] = (st[0x07] << 7) | (st[0x07] >> (32 - 7))
  167. st[0x18] = (st[0x08] + st[0x18])
  168. st[0x08] = (st[0x08] << 7) | (st[0x08] >> (32 - 7))
  169. st[0x19] = (st[0x09] + st[0x19])
  170. st[0x09] = (st[0x09] << 7) | (st[0x09] >> (32 - 7))
  171. st[0x1A] = (st[0x0A] + st[0x1A])
  172. st[0x0A] = (st[0x0A] << 7) | (st[0x0A] >> (32 - 7))
  173. st[0x1B] = (st[0x0B] + st[0x1B])
  174. st[0x0B] = (st[0x0B] << 7) | (st[0x0B] >> (32 - 7))
  175. st[0x1C] = (st[0x0C] + st[0x1C])
  176. st[0x0C] = (st[0x0C] << 7) | (st[0x0C] >> (32 - 7))
  177. st[0x1D] = (st[0x0D] + st[0x1D])
  178. st[0x0D] = (st[0x0D] << 7) | (st[0x0D] >> (32 - 7))
  179. st[0x1E] = (st[0x0E] + st[0x1E])
  180. st[0x0E] = (st[0x0E] << 7) | (st[0x0E] >> (32 - 7))
  181. st[0x1F] = (st[0x0F] + st[0x1F])
  182. st[0x0F] = (st[0x0F] << 7) | (st[0x0F] >> (32 - 7))
  183. st[0x08] ^= st[0x10]
  184. st[0x09] ^= st[0x11]
  185. st[0x0A] ^= st[0x12]
  186. st[0x0B] ^= st[0x13]
  187. st[0x0C] ^= st[0x14]
  188. st[0x0D] ^= st[0x15]
  189. st[0x0E] ^= st[0x16]
  190. st[0x0F] ^= st[0x17]
  191. st[0x00] ^= st[0x18]
  192. st[0x01] ^= st[0x19]
  193. st[0x02] ^= st[0x1A]
  194. st[0x03] ^= st[0x1B]
  195. st[0x04] ^= st[0x1C]
  196. st[0x05] ^= st[0x1D]
  197. st[0x06] ^= st[0x1E]
  198. st[0x07] ^= st[0x1F]
  199. st[0x12] = (st[0x08] + st[0x12])
  200. st[0x08] = (st[0x08] << 11) | (st[0x08] >> (32 - 11))
  201. st[0x13] = (st[0x09] + st[0x13])
  202. st[0x09] = (st[0x09] << 11) | (st[0x09] >> (32 - 11))
  203. st[0x10] = (st[0x0A] + st[0x10])
  204. st[0x0A] = (st[0x0A] << 11) | (st[0x0A] >> (32 - 11))
  205. st[0x11] = (st[0x0B] + st[0x11])
  206. st[0x0B] = (st[0x0B] << 11) | (st[0x0B] >> (32 - 11))
  207. st[0x16] = (st[0x0C] + st[0x16])
  208. st[0x0C] = (st[0x0C] << 11) | (st[0x0C] >> (32 - 11))
  209. st[0x17] = (st[0x0D] + st[0x17])
  210. st[0x0D] = (st[0x0D] << 11) | (st[0x0D] >> (32 - 11))
  211. st[0x14] = (st[0x0E] + st[0x14])
  212. st[0x0E] = (st[0x0E] << 11) | (st[0x0E] >> (32 - 11))
  213. st[0x15] = (st[0x0F] + st[0x15])
  214. st[0x0F] = (st[0x0F] << 11) | (st[0x0F] >> (32 - 11))
  215. st[0x1A] = (st[0x00] + st[0x1A])
  216. st[0x00] = (st[0x00] << 11) | (st[0x00] >> (32 - 11))
  217. st[0x1B] = (st[0x01] + st[0x1B])
  218. st[0x01] = (st[0x01] << 11) | (st[0x01] >> (32 - 11))
  219. st[0x18] = (st[0x02] + st[0x18])
  220. st[0x02] = (st[0x02] << 11) | (st[0x02] >> (32 - 11))
  221. st[0x19] = (st[0x03] + st[0x19])
  222. st[0x03] = (st[0x03] << 11) | (st[0x03] >> (32 - 11))
  223. st[0x1E] = (st[0x04] + st[0x1E])
  224. st[0x04] = (st[0x04] << 11) | (st[0x04] >> (32 - 11))
  225. st[0x1F] = (st[0x05] + st[0x1F])
  226. st[0x05] = (st[0x05] << 11) | (st[0x05] >> (32 - 11))
  227. st[0x1C] = (st[0x06] + st[0x1C])
  228. st[0x06] = (st[0x06] << 11) | (st[0x06] >> (32 - 11))
  229. st[0x1D] = (st[0x07] + st[0x1D])
  230. st[0x07] = (st[0x07] << 11) | (st[0x07] >> (32 - 11))
  231. st[0x0C] ^= st[0x12]
  232. st[0x0D] ^= st[0x13]
  233. st[0x0E] ^= st[0x10]
  234. st[0x0F] ^= st[0x11]
  235. st[0x08] ^= st[0x16]
  236. st[0x09] ^= st[0x17]
  237. st[0x0A] ^= st[0x14]
  238. st[0x0B] ^= st[0x15]
  239. st[0x04] ^= st[0x1A]
  240. st[0x05] ^= st[0x1B]
  241. st[0x06] ^= st[0x18]
  242. st[0x07] ^= st[0x19]
  243. st[0x00] ^= st[0x1E]
  244. st[0x01] ^= st[0x1F]
  245. st[0x02] ^= st[0x1C]
  246. st[0x03] ^= st[0x1D]
  247. st[0x13] = (st[0x0C] + st[0x13])
  248. st[0x0C] = (st[0x0C] << 7) | (st[0x0C] >> (32 - 7))
  249. st[0x12] = (st[0x0D] + st[0x12])
  250. st[0x0D] = (st[0x0D] << 7) | (st[0x0D] >> (32 - 7))
  251. st[0x11] = (st[0x0E] + st[0x11])
  252. st[0x0E] = (st[0x0E] << 7) | (st[0x0E] >> (32 - 7))
  253. st[0x10] = (st[0x0F] + st[0x10])
  254. st[0x0F] = (st[0x0F] << 7) | (st[0x0F] >> (32 - 7))
  255. st[0x17] = (st[0x08] + st[0x17])
  256. st[0x08] = (st[0x08] << 7) | (st[0x08] >> (32 - 7))
  257. st[0x16] = (st[0x09] + st[0x16])
  258. st[0x09] = (st[0x09] << 7) | (st[0x09] >> (32 - 7))
  259. st[0x15] = (st[0x0A] + st[0x15])
  260. st[0x0A] = (st[0x0A] << 7) | (st[0x0A] >> (32 - 7))
  261. st[0x14] = (st[0x0B] + st[0x14])
  262. st[0x0B] = (st[0x0B] << 7) | (st[0x0B] >> (32 - 7))
  263. st[0x1B] = (st[0x04] + st[0x1B])
  264. st[0x04] = (st[0x04] << 7) | (st[0x04] >> (32 - 7))
  265. st[0x1A] = (st[0x05] + st[0x1A])
  266. st[0x05] = (st[0x05] << 7) | (st[0x05] >> (32 - 7))
  267. st[0x19] = (st[0x06] + st[0x19])
  268. st[0x06] = (st[0x06] << 7) | (st[0x06] >> (32 - 7))
  269. st[0x18] = (st[0x07] + st[0x18])
  270. st[0x07] = (st[0x07] << 7) | (st[0x07] >> (32 - 7))
  271. st[0x1F] = (st[0x00] + st[0x1F])
  272. st[0x00] = (st[0x00] << 7) | (st[0x00] >> (32 - 7))
  273. st[0x1E] = (st[0x01] + st[0x1E])
  274. st[0x01] = (st[0x01] << 7) | (st[0x01] >> (32 - 7))
  275. st[0x1D] = (st[0x02] + st[0x1D])
  276. st[0x02] = (st[0x02] << 7) | (st[0x02] >> (32 - 7))
  277. st[0x1C] = (st[0x03] + st[0x1C])
  278. st[0x03] = (st[0x03] << 7) | (st[0x03] >> (32 - 7))
  279. st[0x04] ^= st[0x13]
  280. st[0x05] ^= st[0x12]
  281. st[0x06] ^= st[0x11]
  282. st[0x07] ^= st[0x10]
  283. st[0x00] ^= st[0x17]
  284. st[0x01] ^= st[0x16]
  285. st[0x02] ^= st[0x15]
  286. st[0x03] ^= st[0x14]
  287. st[0x0C] ^= st[0x1B]
  288. st[0x0D] ^= st[0x1A]
  289. st[0x0E] ^= st[0x19]
  290. st[0x0F] ^= st[0x18]
  291. st[0x08] ^= st[0x1F]
  292. st[0x09] ^= st[0x1E]
  293. st[0x0A] ^= st[0x1D]
  294. st[0x0B] ^= st[0x1C]
  295. st[0x11] = (st[0x04] + st[0x11])
  296. st[0x04] = (st[0x04] << 11) | (st[0x04] >> (32 - 11))
  297. st[0x10] = (st[0x05] + st[0x10])
  298. st[0x05] = (st[0x05] << 11) | (st[0x05] >> (32 - 11))
  299. st[0x13] = (st[0x06] + st[0x13])
  300. st[0x06] = (st[0x06] << 11) | (st[0x06] >> (32 - 11))
  301. st[0x12] = (st[0x07] + st[0x12])
  302. st[0x07] = (st[0x07] << 11) | (st[0x07] >> (32 - 11))
  303. st[0x15] = (st[0x00] + st[0x15])
  304. st[0x00] = (st[0x00] << 11) | (st[0x00] >> (32 - 11))
  305. st[0x14] = (st[0x01] + st[0x14])
  306. st[0x01] = (st[0x01] << 11) | (st[0x01] >> (32 - 11))
  307. st[0x17] = (st[0x02] + st[0x17])
  308. st[0x02] = (st[0x02] << 11) | (st[0x02] >> (32 - 11))
  309. st[0x16] = (st[0x03] + st[0x16])
  310. st[0x03] = (st[0x03] << 11) | (st[0x03] >> (32 - 11))
  311. st[0x19] = (st[0x0C] + st[0x19])
  312. st[0x0C] = (st[0x0C] << 11) | (st[0x0C] >> (32 - 11))
  313. st[0x18] = (st[0x0D] + st[0x18])
  314. st[0x0D] = (st[0x0D] << 11) | (st[0x0D] >> (32 - 11))
  315. st[0x1B] = (st[0x0E] + st[0x1B])
  316. st[0x0E] = (st[0x0E] << 11) | (st[0x0E] >> (32 - 11))
  317. st[0x1A] = (st[0x0F] + st[0x1A])
  318. st[0x0F] = (st[0x0F] << 11) | (st[0x0F] >> (32 - 11))
  319. st[0x1D] = (st[0x08] + st[0x1D])
  320. st[0x08] = (st[0x08] << 11) | (st[0x08] >> (32 - 11))
  321. st[0x1C] = (st[0x09] + st[0x1C])
  322. st[0x09] = (st[0x09] << 11) | (st[0x09] >> (32 - 11))
  323. st[0x1F] = (st[0x0A] + st[0x1F])
  324. st[0x0A] = (st[0x0A] << 11) | (st[0x0A] >> (32 - 11))
  325. st[0x1E] = (st[0x0B] + st[0x1E])
  326. st[0x0B] = (st[0x0B] << 11) | (st[0x0B] >> (32 - 11))
  327. st[0x00] ^= st[0x11]
  328. st[0x01] ^= st[0x10]
  329. st[0x02] ^= st[0x13]
  330. st[0x03] ^= st[0x12]
  331. st[0x04] ^= st[0x15]
  332. st[0x05] ^= st[0x14]
  333. st[0x06] ^= st[0x17]
  334. st[0x07] ^= st[0x16]
  335. st[0x08] ^= st[0x19]
  336. st[0x09] ^= st[0x18]
  337. st[0x0A] ^= st[0x1B]
  338. st[0x0B] ^= st[0x1A]
  339. st[0x0C] ^= st[0x1D]
  340. st[0x0D] ^= st[0x1C]
  341. st[0x0E] ^= st[0x1F]
  342. st[0x0F] ^= st[0x1E]
  343. }
  344. ////////////////
  345. var kInit = [32]uint32{
  346. uint32(0x2AEA2A61), uint32(0x50F494D4), uint32(0x2D538B8B),
  347. uint32(0x4167D83E), uint32(0x3FEE2313), uint32(0xC701CF8C),
  348. uint32(0xCC39968E), uint32(0x50AC5695), uint32(0x4D42C787),
  349. uint32(0xA647A8B3), uint32(0x97CF0BEF), uint32(0x825B4537),
  350. uint32(0xEEF864D2), uint32(0xF22090C4), uint32(0xD0E5CD33),
  351. uint32(0xA23911AE), uint32(0xFCD398D9), uint32(0x148FE485),
  352. uint32(0x1B017BEF), uint32(0xB6444532), uint32(0x6A536159),
  353. uint32(0x2FF5781C), uint32(0x91FA7934), uint32(0x0DBADEA9),
  354. uint32(0xD65C8A2B), uint32(0xA5A70E75), uint32(0xB1C62456),
  355. uint32(0xBC796576), uint32(0x1921C8F7), uint32(0xE7989AF1),
  356. uint32(0x7795D246), uint32(0xD43E3B44),
  357. }