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.

1840 lines
36 KiB

  1. // Use of this source code is governed by an ISC
  2. // license that can be found in the LICENSE file.
  3. package luffa
  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 [5][8]uint32
  16. b [32]byte
  17. }
  18. // New returns a new digest compute a LUFFA512 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. for x := range kInit {
  29. for y := range kInit[x] {
  30. ref.h[x][y] = kInit[x][y]
  31. }
  32. }
  33. }
  34. // Sum appends the current hash to dst and returns the result
  35. // as a slice. It does not change the underlying hash state.
  36. func (ref *digest) Sum(dst []byte) []byte {
  37. dgt := *ref
  38. hsh := [64]byte{}
  39. dgt.Close(hsh[:], 0, 0)
  40. return append(dst, hsh[:]...)
  41. }
  42. // Write more data to the running hash, never returns an error.
  43. func (ref *digest) Write(src []byte) (int, error) {
  44. sln := uintptr(len(src))
  45. fln := len(src)
  46. buf := ref.b[:]
  47. ptr := ref.ptr
  48. if sln < (BlockSize - ptr) {
  49. copy(ref.b[ptr:], src)
  50. ref.ptr += sln
  51. return int(sln), nil
  52. }
  53. var V00, V01, V02, V03, V04, V05, V06, V07 uint32
  54. var V10, V11, V12, V13, V14, V15, V16, V17 uint32
  55. var V20, V21, V22, V23, V24, V25, V26, V27 uint32
  56. var V30, V31, V32, V33, V34, V35, V36, V37 uint32
  57. var V40, V41, V42, V43, V44, V45, V46, V47 uint32
  58. V00 = ref.h[0][0]
  59. V01 = ref.h[0][1]
  60. V02 = ref.h[0][2]
  61. V03 = ref.h[0][3]
  62. V04 = ref.h[0][4]
  63. V05 = ref.h[0][5]
  64. V06 = ref.h[0][6]
  65. V07 = ref.h[0][7]
  66. V10 = ref.h[1][0]
  67. V11 = ref.h[1][1]
  68. V12 = ref.h[1][2]
  69. V13 = ref.h[1][3]
  70. V14 = ref.h[1][4]
  71. V15 = ref.h[1][5]
  72. V16 = ref.h[1][6]
  73. V17 = ref.h[1][7]
  74. V20 = ref.h[2][0]
  75. V21 = ref.h[2][1]
  76. V22 = ref.h[2][2]
  77. V23 = ref.h[2][3]
  78. V24 = ref.h[2][4]
  79. V25 = ref.h[2][5]
  80. V26 = ref.h[2][6]
  81. V27 = ref.h[2][7]
  82. V30 = ref.h[3][0]
  83. V31 = ref.h[3][1]
  84. V32 = ref.h[3][2]
  85. V33 = ref.h[3][3]
  86. V34 = ref.h[3][4]
  87. V35 = ref.h[3][5]
  88. V36 = ref.h[3][6]
  89. V37 = ref.h[3][7]
  90. V40 = ref.h[4][0]
  91. V41 = ref.h[4][1]
  92. V42 = ref.h[4][2]
  93. V43 = ref.h[4][3]
  94. V44 = ref.h[4][4]
  95. V45 = ref.h[4][5]
  96. V46 = ref.h[4][6]
  97. V47 = ref.h[4][7]
  98. for sln > 0 {
  99. cln := BlockSize - ptr
  100. if cln > sln {
  101. cln = sln
  102. }
  103. sln -= cln
  104. copy(ref.b[ptr:], src[:cln])
  105. src = src[cln:]
  106. ptr += cln
  107. if ptr == BlockSize {
  108. {
  109. var ts uint32
  110. var M0, M1, M2, M3, M4, M5, M6, M7 uint32
  111. var a0, a1, a2, a3, a4, a5, a6, a7 uint32
  112. var b0, b1, b2, b3, b4, b5, b6, b7 uint32
  113. M0 = decUInt32be(buf[0:])
  114. M1 = decUInt32be(buf[4:])
  115. M2 = decUInt32be(buf[8:])
  116. M3 = decUInt32be(buf[12:])
  117. M4 = decUInt32be(buf[16:])
  118. M5 = decUInt32be(buf[20:])
  119. M6 = decUInt32be(buf[24:])
  120. M7 = decUInt32be(buf[28:])
  121. a0 = V00 ^ V10
  122. a1 = V01 ^ V11
  123. a2 = V02 ^ V12
  124. a3 = V03 ^ V13
  125. a4 = V04 ^ V14
  126. a5 = V05 ^ V15
  127. a6 = V06 ^ V16
  128. a7 = V07 ^ V17
  129. b0 = V20 ^ V30
  130. b1 = V21 ^ V31
  131. b2 = V22 ^ V32
  132. b3 = V23 ^ V33
  133. b4 = V24 ^ V34
  134. b5 = V25 ^ V35
  135. b6 = V26 ^ V36
  136. b7 = V27 ^ V37
  137. a0 ^= b0
  138. a1 ^= b1
  139. a2 ^= b2
  140. a3 ^= b3
  141. a4 ^= b4
  142. a5 ^= b5
  143. a6 ^= b6
  144. a7 ^= b7
  145. a0 ^= V40
  146. a1 ^= V41
  147. a2 ^= V42
  148. a3 ^= V43
  149. a4 ^= V44
  150. a5 ^= V45
  151. a6 ^= V46
  152. a7 ^= V47
  153. ts = a7
  154. a7 = a6
  155. a6 = a5
  156. a5 = a4
  157. a4 = a3 ^ ts
  158. a3 = a2 ^ ts
  159. a2 = a1
  160. a1 = a0 ^ ts
  161. a0 = ts
  162. V00 ^= a0
  163. V01 ^= a1
  164. V02 ^= a2
  165. V03 ^= a3
  166. V04 ^= a4
  167. V05 ^= a5
  168. V06 ^= a6
  169. V07 ^= a7
  170. V10 ^= a0
  171. V11 ^= a1
  172. V12 ^= a2
  173. V13 ^= a3
  174. V14 ^= a4
  175. V15 ^= a5
  176. V16 ^= a6
  177. V17 ^= a7
  178. V20 ^= a0
  179. V21 ^= a1
  180. V22 ^= a2
  181. V23 ^= a3
  182. V24 ^= a4
  183. V25 ^= a5
  184. V26 ^= a6
  185. V27 ^= a7
  186. V30 ^= a0
  187. V31 ^= a1
  188. V32 ^= a2
  189. V33 ^= a3
  190. V34 ^= a4
  191. V35 ^= a5
  192. V36 ^= a6
  193. V37 ^= a7
  194. V40 ^= a0
  195. V41 ^= a1
  196. V42 ^= a2
  197. V43 ^= a3
  198. V44 ^= a4
  199. V45 ^= a5
  200. V46 ^= a6
  201. V47 ^= a7
  202. ts = V07
  203. b7 = V06
  204. b6 = V05
  205. b5 = V04
  206. b4 = V03 ^ ts
  207. b3 = V02 ^ ts
  208. b2 = V01
  209. b1 = V00 ^ ts
  210. b0 = ts
  211. b0 ^= V10
  212. b1 ^= V11
  213. b2 ^= V12
  214. b3 ^= V13
  215. b4 ^= V14
  216. b5 ^= V15
  217. b6 ^= V16
  218. b7 ^= V17
  219. ts = V17
  220. V17 = V16
  221. V16 = V15
  222. V15 = V14
  223. V14 = V13 ^ ts
  224. V13 = V12 ^ ts
  225. V12 = V11
  226. V11 = V10 ^ ts
  227. V10 = ts
  228. V10 ^= V20
  229. V11 ^= V21
  230. V12 ^= V22
  231. V13 ^= V23
  232. V14 ^= V24
  233. V15 ^= V25
  234. V16 ^= V26
  235. V17 ^= V27
  236. ts = V27
  237. V27 = V26
  238. V26 = V25
  239. V25 = V24
  240. V24 = V23 ^ ts
  241. V23 = V22 ^ ts
  242. V22 = V21
  243. V21 = V20 ^ ts
  244. V20 = ts
  245. V20 ^= V30
  246. V21 ^= V31
  247. V22 ^= V32
  248. V23 ^= V33
  249. V24 ^= V34
  250. V25 ^= V35
  251. V26 ^= V36
  252. V27 ^= V37
  253. ts = V37
  254. V37 = V36
  255. V36 = V35
  256. V35 = V34
  257. V34 = V33 ^ ts
  258. V33 = V32 ^ ts
  259. V32 = V31
  260. V31 = V30 ^ ts
  261. V30 = ts
  262. V30 ^= V40
  263. V31 ^= V41
  264. V32 ^= V42
  265. V33 ^= V43
  266. V34 ^= V44
  267. V35 ^= V45
  268. V36 ^= V46
  269. V37 ^= V47
  270. ts = V47
  271. V47 = V46
  272. V46 = V45
  273. V45 = V44
  274. V44 = V43 ^ ts
  275. V43 = V42 ^ ts
  276. V42 = V41
  277. V41 = V40 ^ ts
  278. V40 = ts
  279. V40 ^= V00
  280. V41 ^= V01
  281. V42 ^= V02
  282. V43 ^= V03
  283. V44 ^= V04
  284. V45 ^= V05
  285. V46 ^= V06
  286. V47 ^= V07
  287. ts = b7
  288. V07 = b6
  289. V06 = b5
  290. V05 = b4
  291. V04 = b3 ^ ts
  292. V03 = b2 ^ ts
  293. V02 = b1
  294. V01 = b0 ^ ts
  295. V00 = ts
  296. V00 ^= V40
  297. V01 ^= V41
  298. V02 ^= V42
  299. V03 ^= V43
  300. V04 ^= V44
  301. V05 ^= V45
  302. V06 ^= V46
  303. V07 ^= V47
  304. ts = V47
  305. V47 = V46
  306. V46 = V45
  307. V45 = V44
  308. V44 = V43 ^ ts
  309. V43 = V42 ^ ts
  310. V42 = V41
  311. V41 = V40 ^ ts
  312. V40 = ts
  313. V40 ^= V30
  314. V41 ^= V31
  315. V42 ^= V32
  316. V43 ^= V33
  317. V44 ^= V34
  318. V45 ^= V35
  319. V46 ^= V36
  320. V47 ^= V37
  321. ts = V37
  322. V37 = V36
  323. V36 = V35
  324. V35 = V34
  325. V34 = V33 ^ ts
  326. V33 = V32 ^ ts
  327. V32 = V31
  328. V31 = V30 ^ ts
  329. V30 = ts
  330. V30 ^= V20
  331. V31 ^= V21
  332. V32 ^= V22
  333. V33 ^= V23
  334. V34 ^= V24
  335. V35 ^= V25
  336. V36 ^= V26
  337. V37 ^= V27
  338. ts = V27
  339. V27 = V26
  340. V26 = V25
  341. V25 = V24
  342. V24 = V23 ^ ts
  343. V23 = V22 ^ ts
  344. V22 = V21
  345. V21 = V20 ^ ts
  346. V20 = ts
  347. V20 ^= V10
  348. V21 ^= V11
  349. V22 ^= V12
  350. V23 ^= V13
  351. V24 ^= V14
  352. V25 ^= V15
  353. V26 ^= V16
  354. V27 ^= V17
  355. ts = V17
  356. V17 = V16
  357. V16 = V15
  358. V15 = V14
  359. V14 = V13 ^ ts
  360. V13 = V12 ^ ts
  361. V12 = V11
  362. V11 = V10 ^ ts
  363. V10 = ts
  364. V10 ^= b0
  365. V11 ^= b1
  366. V12 ^= b2
  367. V13 ^= b3
  368. V14 ^= b4
  369. V15 ^= b5
  370. V16 ^= b6
  371. V17 ^= b7
  372. V00 ^= M0
  373. V01 ^= M1
  374. V02 ^= M2
  375. V03 ^= M3
  376. V04 ^= M4
  377. V05 ^= M5
  378. V06 ^= M6
  379. V07 ^= M7
  380. ts = M7
  381. M7 = M6
  382. M6 = M5
  383. M5 = M4
  384. M4 = M3 ^ ts
  385. M3 = M2 ^ ts
  386. M2 = M1
  387. M1 = M0 ^ ts
  388. M0 = ts
  389. V10 ^= M0
  390. V11 ^= M1
  391. V12 ^= M2
  392. V13 ^= M3
  393. V14 ^= M4
  394. V15 ^= M5
  395. V16 ^= M6
  396. V17 ^= M7
  397. ts = M7
  398. M7 = M6
  399. M6 = M5
  400. M5 = M4
  401. M4 = M3 ^ ts
  402. M3 = M2 ^ ts
  403. M2 = M1
  404. M1 = M0 ^ ts
  405. M0 = ts
  406. V20 ^= M0
  407. V21 ^= M1
  408. V22 ^= M2
  409. V23 ^= M3
  410. V24 ^= M4
  411. V25 ^= M5
  412. V26 ^= M6
  413. V27 ^= M7
  414. ts = M7
  415. M7 = M6
  416. M6 = M5
  417. M5 = M4
  418. M4 = M3 ^ ts
  419. M3 = M2 ^ ts
  420. M2 = M1
  421. M1 = M0 ^ ts
  422. M0 = ts
  423. V30 ^= M0
  424. V31 ^= M1
  425. V32 ^= M2
  426. V33 ^= M3
  427. V34 ^= M4
  428. V35 ^= M5
  429. V36 ^= M6
  430. V37 ^= M7
  431. ts = M7
  432. M7 = M6
  433. M6 = M5
  434. M5 = M4
  435. M4 = M3 ^ ts
  436. M3 = M2 ^ ts
  437. M2 = M1
  438. M1 = M0 ^ ts
  439. M0 = ts
  440. V40 ^= M0
  441. V41 ^= M1
  442. V42 ^= M2
  443. V43 ^= M3
  444. V44 ^= M4
  445. V45 ^= M5
  446. V46 ^= M6
  447. V47 ^= M7
  448. }
  449. {
  450. var ul, uh, vl, vh, tws uint32
  451. var W0, W1, W2, W3, W4, W5, W6, W7, tw uint64
  452. V14 = ((V14 << 1) | (V14 >> (32 - 1)))
  453. V15 = ((V15 << 1) | (V15 >> (32 - 1)))
  454. V16 = ((V16 << 1) | (V16 >> (32 - 1)))
  455. V17 = ((V17 << 1) | (V17 >> (32 - 1)))
  456. V24 = ((V24 << 2) | (V24 >> (32 - 2)))
  457. V25 = ((V25 << 2) | (V25 >> (32 - 2)))
  458. V26 = ((V26 << 2) | (V26 >> (32 - 2)))
  459. V27 = ((V27 << 2) | (V27 >> (32 - 2)))
  460. V34 = ((V34 << 3) | (V34 >> (32 - 3)))
  461. V35 = ((V35 << 3) | (V35 >> (32 - 3)))
  462. V36 = ((V36 << 3) | (V36 >> (32 - 3)))
  463. V37 = ((V37 << 3) | (V37 >> (32 - 3)))
  464. V44 = ((V44 << 4) | (V44 >> (32 - 4)))
  465. V45 = ((V45 << 4) | (V45 >> (32 - 4)))
  466. V46 = ((V46 << 4) | (V46 >> (32 - 4)))
  467. V47 = ((V47 << 4) | (V47 >> (32 - 4)))
  468. W0 = uint64(V00) | (uint64(V10) << 32)
  469. W1 = uint64(V01) | (uint64(V11) << 32)
  470. W2 = uint64(V02) | (uint64(V12) << 32)
  471. W3 = uint64(V03) | (uint64(V13) << 32)
  472. W4 = uint64(V04) | (uint64(V14) << 32)
  473. W5 = uint64(V05) | (uint64(V15) << 32)
  474. W6 = uint64(V06) | (uint64(V16) << 32)
  475. W7 = uint64(V07) | (uint64(V17) << 32)
  476. for r := uintptr(0); r < 8; r++ {
  477. tw = W0
  478. W0 |= W1
  479. W2 ^= W3
  480. W1 = ^W1
  481. W0 ^= W3
  482. W3 &= tw
  483. W1 ^= W3
  484. W3 ^= W2
  485. W2 &= W0
  486. W0 = ^W0
  487. W2 ^= W1
  488. W1 |= W3
  489. tw ^= W1
  490. W3 ^= W2
  491. W2 &= W1
  492. W1 ^= W0
  493. W0 = tw
  494. tw = W5
  495. W5 |= W6
  496. W7 ^= W4
  497. W6 = ^W6
  498. W5 ^= W4
  499. W4 &= tw
  500. W6 ^= W4
  501. W4 ^= W7
  502. W7 &= W5
  503. W5 = ^W5
  504. W7 ^= W6
  505. W6 |= W4
  506. tw ^= W6
  507. W4 ^= W7
  508. W7 &= W6
  509. W6 ^= W5
  510. W5 = tw
  511. W4 ^= W0
  512. ul = uint32(W0)
  513. uh = uint32((W0 >> 32))
  514. vl = uint32(W4)
  515. vh = uint32((W4 >> 32))
  516. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  517. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  518. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  519. vl = ((vl << 1) | (vl >> (32 - 1)))
  520. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  521. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  522. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  523. vh = ((vh << 1) | (vh >> (32 - 1)))
  524. W0 = uint64(ul) | (uint64(uh) << 32)
  525. W4 = uint64(vl) | (uint64(vh) << 32)
  526. W5 ^= W1
  527. ul = uint32(W1)
  528. uh = uint32((W1 >> 32))
  529. vl = uint32(W5)
  530. vh = uint32((W5 >> 32))
  531. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  532. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  533. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  534. vl = ((vl << 1) | (vl >> (32 - 1)))
  535. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  536. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  537. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  538. vh = ((vh << 1) | (vh >> (32 - 1)))
  539. W1 = uint64(ul) | (uint64(uh) << 32)
  540. W5 = uint64(vl) | (uint64(vh) << 32)
  541. W6 ^= W2
  542. ul = uint32(W2)
  543. uh = uint32((W2 >> 32))
  544. vl = uint32(W6)
  545. vh = uint32((W6 >> 32))
  546. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  547. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  548. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  549. vl = ((vl << 1) | (vl >> (32 - 1)))
  550. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  551. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  552. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  553. vh = ((vh << 1) | (vh >> (32 - 1)))
  554. W2 = uint64(ul) | (uint64(uh) << 32)
  555. W6 = uint64(vl) | (uint64(vh) << 32)
  556. W7 ^= W3
  557. ul = uint32(W3)
  558. uh = uint32((W3 >> 32))
  559. vl = uint32(W7)
  560. vh = uint32((W7 >> 32))
  561. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  562. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  563. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  564. vl = ((vl << 1) | (vl >> (32 - 1)))
  565. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  566. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  567. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  568. vh = ((vh << 1) | (vh >> (32 - 1)))
  569. W3 = uint64(ul) | (uint64(uh) << 32)
  570. W7 = uint64(vl) | (uint64(vh) << 32)
  571. W0 ^= kRCW010[r]
  572. W4 ^= kRCW014[r]
  573. }
  574. V00 = uint32(W0)
  575. V10 = uint32((W0 >> 32))
  576. V01 = uint32(W1)
  577. V11 = uint32((W1 >> 32))
  578. V02 = uint32(W2)
  579. V12 = uint32((W2 >> 32))
  580. V03 = uint32(W3)
  581. V13 = uint32((W3 >> 32))
  582. V04 = uint32(W4)
  583. V14 = uint32((W4 >> 32))
  584. V05 = uint32(W5)
  585. V15 = uint32((W5 >> 32))
  586. V06 = uint32(W6)
  587. V16 = uint32((W6 >> 32))
  588. V07 = uint32(W7)
  589. V17 = uint32((W7 >> 32))
  590. W0 = uint64(V20) | (uint64(V30) << 32)
  591. W1 = uint64(V21) | (uint64(V31) << 32)
  592. W2 = uint64(V22) | (uint64(V32) << 32)
  593. W3 = uint64(V23) | (uint64(V33) << 32)
  594. W4 = uint64(V24) | (uint64(V34) << 32)
  595. W5 = uint64(V25) | (uint64(V35) << 32)
  596. W6 = uint64(V26) | (uint64(V36) << 32)
  597. W7 = uint64(V27) | (uint64(V37) << 32)
  598. for r := uintptr(0); r < 8; r++ {
  599. tw = W0
  600. W0 |= W1
  601. W2 ^= W3
  602. W1 = ^W1
  603. W0 ^= W3
  604. W3 &= tw
  605. W1 ^= W3
  606. W3 ^= W2
  607. W2 &= W0
  608. W0 = ^W0
  609. W2 ^= W1
  610. W1 |= W3
  611. tw ^= W1
  612. W3 ^= W2
  613. W2 &= W1
  614. W1 ^= W0
  615. W0 = tw
  616. tw = W5
  617. W5 |= W6
  618. W7 ^= W4
  619. W6 = ^W6
  620. W5 ^= W4
  621. W4 &= tw
  622. W6 ^= W4
  623. W4 ^= W7
  624. W7 &= W5
  625. W5 = ^W5
  626. W7 ^= W6
  627. W6 |= W4
  628. tw ^= W6
  629. W4 ^= W7
  630. W7 &= W6
  631. W6 ^= W5
  632. W5 = tw
  633. W4 ^= W0
  634. ul = uint32(W0)
  635. uh = uint32((W0 >> 32))
  636. vl = uint32(W4)
  637. vh = uint32((W4 >> 32))
  638. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  639. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  640. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  641. vl = ((vl << 1) | (vl >> (32 - 1)))
  642. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  643. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  644. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  645. vh = ((vh << 1) | (vh >> (32 - 1)))
  646. W0 = uint64(ul) | (uint64(uh) << 32)
  647. W4 = uint64(vl) | (uint64(vh) << 32)
  648. W5 ^= W1
  649. ul = uint32(W1)
  650. uh = uint32((W1 >> 32))
  651. vl = uint32(W5)
  652. vh = uint32((W5 >> 32))
  653. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  654. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  655. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  656. vl = ((vl << 1) | (vl >> (32 - 1)))
  657. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  658. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  659. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  660. vh = ((vh << 1) | (vh >> (32 - 1)))
  661. W1 = uint64(ul) | (uint64(uh) << 32)
  662. W5 = uint64(vl) | (uint64(vh) << 32)
  663. W6 ^= W2
  664. ul = uint32(W2)
  665. uh = uint32((W2 >> 32))
  666. vl = uint32(W6)
  667. vh = uint32((W6 >> 32))
  668. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  669. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  670. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  671. vl = ((vl << 1) | (vl >> (32 - 1)))
  672. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  673. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  674. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  675. vh = ((vh << 1) | (vh >> (32 - 1)))
  676. W2 = uint64(ul) | (uint64(uh) << 32)
  677. W6 = uint64(vl) | (uint64(vh) << 32)
  678. W7 ^= W3
  679. ul = uint32(W3)
  680. uh = uint32((W3 >> 32))
  681. vl = uint32(W7)
  682. vh = uint32((W7 >> 32))
  683. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  684. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  685. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  686. vl = ((vl << 1) | (vl >> (32 - 1)))
  687. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  688. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  689. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  690. vh = ((vh << 1) | (vh >> (32 - 1)))
  691. W3 = uint64(ul) | (uint64(uh) << 32)
  692. W7 = uint64(vl) | (uint64(vh) << 32)
  693. W0 ^= kRCW230[r]
  694. W4 ^= kRCW234[r]
  695. }
  696. V20 = uint32(W0)
  697. V30 = uint32((W0 >> 32))
  698. V21 = uint32(W1)
  699. V31 = uint32((W1 >> 32))
  700. V22 = uint32(W2)
  701. V32 = uint32((W2 >> 32))
  702. V23 = uint32(W3)
  703. V33 = uint32((W3 >> 32))
  704. V24 = uint32(W4)
  705. V34 = uint32((W4 >> 32))
  706. V25 = uint32(W5)
  707. V35 = uint32((W5 >> 32))
  708. V26 = uint32(W6)
  709. V36 = uint32((W6 >> 32))
  710. V27 = uint32(W7)
  711. V37 = uint32((W7 >> 32))
  712. for r := uintptr(0); r < 8; r++ {
  713. tws = V40
  714. V40 |= V41
  715. V42 ^= V43
  716. V41 = ^V41
  717. V40 ^= V43
  718. V43 &= tws
  719. V41 ^= V43
  720. V43 ^= V42
  721. V42 &= V40
  722. V40 = ^V40
  723. V42 ^= V41
  724. V41 |= V43
  725. tws ^= V41
  726. V43 ^= V42
  727. V42 &= V41
  728. V41 ^= V40
  729. V40 = tws
  730. tws = V45
  731. V45 |= V46
  732. V47 ^= V44
  733. V46 = ^V46
  734. V45 ^= V44
  735. V44 &= tws
  736. V46 ^= V44
  737. V44 ^= V47
  738. V47 &= V45
  739. V45 = ^V45
  740. V47 ^= V46
  741. V46 |= V44
  742. tws ^= V46
  743. V44 ^= V47
  744. V47 &= V46
  745. V46 ^= V45
  746. V45 = tws
  747. V44 ^= V40
  748. V40 = ((V40 << 2) | (V40 >> (32 - 2))) ^ V44
  749. V44 = ((V44 << 14) | (V44 >> (32 - 14))) ^ V40
  750. V40 = ((V40 << 10) | (V40 >> (32 - 10))) ^ V44
  751. V44 = ((V44 << 1) | (V44 >> (32 - 1)))
  752. V45 ^= V41
  753. V41 = ((V41 << 2) | (V41 >> (32 - 2))) ^ V45
  754. V45 = ((V45 << 14) | (V45 >> (32 - 14))) ^ V41
  755. V41 = ((V41 << 10) | (V41 >> (32 - 10))) ^ V45
  756. V45 = ((V45 << 1) | (V45 >> (32 - 1)))
  757. V46 ^= V42
  758. V42 = ((V42 << 2) | (V42 >> (32 - 2))) ^ V46
  759. V46 = ((V46 << 14) | (V46 >> (32 - 14))) ^ V42
  760. V42 = ((V42 << 10) | (V42 >> (32 - 10))) ^ V46
  761. V46 = ((V46 << 1) | (V46 >> (32 - 1)))
  762. V47 ^= V43
  763. V43 = ((V43 << 2) | (V43 >> (32 - 2))) ^ V47
  764. V47 = ((V47 << 14) | (V47 >> (32 - 14))) ^ V43
  765. V43 = ((V43 << 10) | (V43 >> (32 - 10))) ^ V47
  766. V47 = ((V47 << 1) | (V47 >> (32 - 1)))
  767. V40 ^= kRC40[r]
  768. V44 ^= kRC44[r]
  769. }
  770. }
  771. ptr = 0
  772. }
  773. }
  774. ref.h[0][0] = V00
  775. ref.h[0][1] = V01
  776. ref.h[0][2] = V02
  777. ref.h[0][3] = V03
  778. ref.h[0][4] = V04
  779. ref.h[0][5] = V05
  780. ref.h[0][6] = V06
  781. ref.h[0][7] = V07
  782. ref.h[1][0] = V10
  783. ref.h[1][1] = V11
  784. ref.h[1][2] = V12
  785. ref.h[1][3] = V13
  786. ref.h[1][4] = V14
  787. ref.h[1][5] = V15
  788. ref.h[1][6] = V16
  789. ref.h[1][7] = V17
  790. ref.h[2][0] = V20
  791. ref.h[2][1] = V21
  792. ref.h[2][2] = V22
  793. ref.h[2][3] = V23
  794. ref.h[2][4] = V24
  795. ref.h[2][5] = V25
  796. ref.h[2][6] = V26
  797. ref.h[2][7] = V27
  798. ref.h[3][0] = V30
  799. ref.h[3][1] = V31
  800. ref.h[3][2] = V32
  801. ref.h[3][3] = V33
  802. ref.h[3][4] = V34
  803. ref.h[3][5] = V35
  804. ref.h[3][6] = V36
  805. ref.h[3][7] = V37
  806. ref.h[4][0] = V40
  807. ref.h[4][1] = V41
  808. ref.h[4][2] = V42
  809. ref.h[4][3] = V43
  810. ref.h[4][4] = V44
  811. ref.h[4][5] = V45
  812. ref.h[4][6] = V46
  813. ref.h[4][7] = V47
  814. ref.ptr = ptr
  815. return fln, nil
  816. }
  817. // Close the digest by writing the last bits and storing the hash
  818. // in dst. This prepares the digest for reuse by calling reset. A call
  819. // to Close with a dst that is smaller then HashSize will return an error.
  820. func (ref *digest) Close(dst []byte, bits uint8, bcnt uint8) error {
  821. if ln := len(dst); HashSize > ln {
  822. return fmt.Errorf("Luffa Close: dst min length: %d, got %d", HashSize, ln)
  823. }
  824. buf := ref.b[:]
  825. ptr := ref.ptr + 1
  826. {
  827. off := uint8(0x80) >> bcnt
  828. buf[ref.ptr] = uint8((bits & -off) | off)
  829. }
  830. memset(buf[ptr:], 0)
  831. var V00, V01, V02, V03, V04, V05, V06, V07 uint32
  832. var V10, V11, V12, V13, V14, V15, V16, V17 uint32
  833. var V20, V21, V22, V23, V24, V25, V26, V27 uint32
  834. var V30, V31, V32, V33, V34, V35, V36, V37 uint32
  835. var V40, V41, V42, V43, V44, V45, V46, V47 uint32
  836. V00 = ref.h[0][0]
  837. V01 = ref.h[0][1]
  838. V02 = ref.h[0][2]
  839. V03 = ref.h[0][3]
  840. V04 = ref.h[0][4]
  841. V05 = ref.h[0][5]
  842. V06 = ref.h[0][6]
  843. V07 = ref.h[0][7]
  844. V10 = ref.h[1][0]
  845. V11 = ref.h[1][1]
  846. V12 = ref.h[1][2]
  847. V13 = ref.h[1][3]
  848. V14 = ref.h[1][4]
  849. V15 = ref.h[1][5]
  850. V16 = ref.h[1][6]
  851. V17 = ref.h[1][7]
  852. V20 = ref.h[2][0]
  853. V21 = ref.h[2][1]
  854. V22 = ref.h[2][2]
  855. V23 = ref.h[2][3]
  856. V24 = ref.h[2][4]
  857. V25 = ref.h[2][5]
  858. V26 = ref.h[2][6]
  859. V27 = ref.h[2][7]
  860. V30 = ref.h[3][0]
  861. V31 = ref.h[3][1]
  862. V32 = ref.h[3][2]
  863. V33 = ref.h[3][3]
  864. V34 = ref.h[3][4]
  865. V35 = ref.h[3][5]
  866. V36 = ref.h[3][6]
  867. V37 = ref.h[3][7]
  868. V40 = ref.h[4][0]
  869. V41 = ref.h[4][1]
  870. V42 = ref.h[4][2]
  871. V43 = ref.h[4][3]
  872. V44 = ref.h[4][4]
  873. V45 = ref.h[4][5]
  874. V46 = ref.h[4][6]
  875. V47 = ref.h[4][7]
  876. for i := uintptr(0); i < 3; i++ {
  877. {
  878. var ts uint32
  879. var M0, M1, M2, M3, M4, M5, M6, M7 uint32
  880. var a0, a1, a2, a3, a4, a5, a6, a7 uint32
  881. var b0, b1, b2, b3, b4, b5, b6, b7 uint32
  882. M0 = decUInt32be(buf[0:])
  883. M1 = decUInt32be(buf[4:])
  884. M2 = decUInt32be(buf[8:])
  885. M3 = decUInt32be(buf[12:])
  886. M4 = decUInt32be(buf[16:])
  887. M5 = decUInt32be(buf[20:])
  888. M6 = decUInt32be(buf[24:])
  889. M7 = decUInt32be(buf[28:])
  890. a0 = V00 ^ V10
  891. a1 = V01 ^ V11
  892. a2 = V02 ^ V12
  893. a3 = V03 ^ V13
  894. a4 = V04 ^ V14
  895. a5 = V05 ^ V15
  896. a6 = V06 ^ V16
  897. a7 = V07 ^ V17
  898. b0 = V20 ^ V30
  899. b1 = V21 ^ V31
  900. b2 = V22 ^ V32
  901. b3 = V23 ^ V33
  902. b4 = V24 ^ V34
  903. b5 = V25 ^ V35
  904. b6 = V26 ^ V36
  905. b7 = V27 ^ V37
  906. a0 ^= b0
  907. a1 ^= b1
  908. a2 ^= b2
  909. a3 ^= b3
  910. a4 ^= b4
  911. a5 ^= b5
  912. a6 ^= b6
  913. a7 ^= b7
  914. a0 ^= V40
  915. a1 ^= V41
  916. a2 ^= V42
  917. a3 ^= V43
  918. a4 ^= V44
  919. a5 ^= V45
  920. a6 ^= V46
  921. a7 ^= V47
  922. ts = a7
  923. a7 = a6
  924. a6 = a5
  925. a5 = a4
  926. a4 = a3 ^ ts
  927. a3 = a2 ^ ts
  928. a2 = a1
  929. a1 = a0 ^ ts
  930. a0 = ts
  931. V00 ^= a0
  932. V01 ^= a1
  933. V02 ^= a2
  934. V03 ^= a3
  935. V04 ^= a4
  936. V05 ^= a5
  937. V06 ^= a6
  938. V07 ^= a7
  939. V10 ^= a0
  940. V11 ^= a1
  941. V12 ^= a2
  942. V13 ^= a3
  943. V14 ^= a4
  944. V15 ^= a5
  945. V16 ^= a6
  946. V17 ^= a7
  947. V20 ^= a0
  948. V21 ^= a1
  949. V22 ^= a2
  950. V23 ^= a3
  951. V24 ^= a4
  952. V25 ^= a5
  953. V26 ^= a6
  954. V27 ^= a7
  955. V30 ^= a0
  956. V31 ^= a1
  957. V32 ^= a2
  958. V33 ^= a3
  959. V34 ^= a4
  960. V35 ^= a5
  961. V36 ^= a6
  962. V37 ^= a7
  963. V40 ^= a0
  964. V41 ^= a1
  965. V42 ^= a2
  966. V43 ^= a3
  967. V44 ^= a4
  968. V45 ^= a5
  969. V46 ^= a6
  970. V47 ^= a7
  971. ts = V07
  972. b7 = V06
  973. b6 = V05
  974. b5 = V04
  975. b4 = V03 ^ ts
  976. b3 = V02 ^ ts
  977. b2 = V01
  978. b1 = V00 ^ ts
  979. b0 = ts
  980. b0 ^= V10
  981. b1 ^= V11
  982. b2 ^= V12
  983. b3 ^= V13
  984. b4 ^= V14
  985. b5 ^= V15
  986. b6 ^= V16
  987. b7 ^= V17
  988. ts = V17
  989. V17 = V16
  990. V16 = V15
  991. V15 = V14
  992. V14 = V13 ^ ts
  993. V13 = V12 ^ ts
  994. V12 = V11
  995. V11 = V10 ^ ts
  996. V10 = ts
  997. V10 ^= V20
  998. V11 ^= V21
  999. V12 ^= V22
  1000. V13 ^= V23
  1001. V14 ^= V24
  1002. V15 ^= V25
  1003. V16 ^= V26
  1004. V17 ^= V27
  1005. ts = V27
  1006. V27 = V26
  1007. V26 = V25
  1008. V25 = V24
  1009. V24 = V23 ^ ts
  1010. V23 = V22 ^ ts
  1011. V22 = V21
  1012. V21 = V20 ^ ts
  1013. V20 = ts
  1014. V20 ^= V30
  1015. V21 ^= V31
  1016. V22 ^= V32
  1017. V23 ^= V33
  1018. V24 ^= V34
  1019. V25 ^= V35
  1020. V26 ^= V36
  1021. V27 ^= V37
  1022. ts = V37
  1023. V37 = V36
  1024. V36 = V35
  1025. V35 = V34
  1026. V34 = V33 ^ ts
  1027. V33 = V32 ^ ts
  1028. V32 = V31
  1029. V31 = V30 ^ ts
  1030. V30 = ts
  1031. V30 ^= V40
  1032. V31 ^= V41
  1033. V32 ^= V42
  1034. V33 ^= V43
  1035. V34 ^= V44
  1036. V35 ^= V45
  1037. V36 ^= V46
  1038. V37 ^= V47
  1039. ts = V47
  1040. V47 = V46
  1041. V46 = V45
  1042. V45 = V44
  1043. V44 = V43 ^ ts
  1044. V43 = V42 ^ ts
  1045. V42 = V41
  1046. V41 = V40 ^ ts
  1047. V40 = ts
  1048. V40 ^= V00
  1049. V41 ^= V01
  1050. V42 ^= V02
  1051. V43 ^= V03
  1052. V44 ^= V04
  1053. V45 ^= V05
  1054. V46 ^= V06
  1055. V47 ^= V07
  1056. ts = b7
  1057. V07 = b6
  1058. V06 = b5
  1059. V05 = b4
  1060. V04 = b3 ^ ts
  1061. V03 = b2 ^ ts
  1062. V02 = b1
  1063. V01 = b0 ^ ts
  1064. V00 = ts
  1065. V00 ^= V40
  1066. V01 ^= V41
  1067. V02 ^= V42
  1068. V03 ^= V43
  1069. V04 ^= V44
  1070. V05 ^= V45
  1071. V06 ^= V46
  1072. V07 ^= V47
  1073. ts = V47
  1074. V47 = V46
  1075. V46 = V45
  1076. V45 = V44
  1077. V44 = V43 ^ ts
  1078. V43 = V42 ^ ts
  1079. V42 = V41
  1080. V41 = V40 ^ ts
  1081. V40 = ts
  1082. V40 ^= V30
  1083. V41 ^= V31
  1084. V42 ^= V32
  1085. V43 ^= V33
  1086. V44 ^= V34
  1087. V45 ^= V35
  1088. V46 ^= V36
  1089. V47 ^= V37
  1090. ts = V37
  1091. V37 = V36
  1092. V36 = V35
  1093. V35 = V34
  1094. V34 = V33 ^ ts
  1095. V33 = V32 ^ ts
  1096. V32 = V31
  1097. V31 = V30 ^ ts
  1098. V30 = ts
  1099. V30 ^= V20
  1100. V31 ^= V21
  1101. V32 ^= V22
  1102. V33 ^= V23
  1103. V34 ^= V24
  1104. V35 ^= V25
  1105. V36 ^= V26
  1106. V37 ^= V27
  1107. ts = V27
  1108. V27 = V26
  1109. V26 = V25
  1110. V25 = V24
  1111. V24 = V23 ^ ts
  1112. V23 = V22 ^ ts
  1113. V22 = V21
  1114. V21 = V20 ^ ts
  1115. V20 = ts
  1116. V20 ^= V10
  1117. V21 ^= V11
  1118. V22 ^= V12
  1119. V23 ^= V13
  1120. V24 ^= V14
  1121. V25 ^= V15
  1122. V26 ^= V16
  1123. V27 ^= V17
  1124. ts = V17
  1125. V17 = V16
  1126. V16 = V15
  1127. V15 = V14
  1128. V14 = V13 ^ ts
  1129. V13 = V12 ^ ts
  1130. V12 = V11
  1131. V11 = V10 ^ ts
  1132. V10 = ts
  1133. V10 ^= b0
  1134. V11 ^= b1
  1135. V12 ^= b2
  1136. V13 ^= b3
  1137. V14 ^= b4
  1138. V15 ^= b5
  1139. V16 ^= b6
  1140. V17 ^= b7
  1141. V00 ^= M0
  1142. V01 ^= M1
  1143. V02 ^= M2
  1144. V03 ^= M3
  1145. V04 ^= M4
  1146. V05 ^= M5
  1147. V06 ^= M6
  1148. V07 ^= M7
  1149. ts = M7
  1150. M7 = M6
  1151. M6 = M5
  1152. M5 = M4
  1153. M4 = M3 ^ ts
  1154. M3 = M2 ^ ts
  1155. M2 = M1
  1156. M1 = M0 ^ ts
  1157. M0 = ts
  1158. V10 ^= M0
  1159. V11 ^= M1
  1160. V12 ^= M2
  1161. V13 ^= M3
  1162. V14 ^= M4
  1163. V15 ^= M5
  1164. V16 ^= M6
  1165. V17 ^= M7
  1166. ts = M7
  1167. M7 = M6
  1168. M6 = M5
  1169. M5 = M4
  1170. M4 = M3 ^ ts
  1171. M3 = M2 ^ ts
  1172. M2 = M1
  1173. M1 = M0 ^ ts
  1174. M0 = ts
  1175. V20 ^= M0
  1176. V21 ^= M1
  1177. V22 ^= M2
  1178. V23 ^= M3
  1179. V24 ^= M4
  1180. V25 ^= M5
  1181. V26 ^= M6
  1182. V27 ^= M7
  1183. ts = M7
  1184. M7 = M6
  1185. M6 = M5
  1186. M5 = M4
  1187. M4 = M3 ^ ts
  1188. M3 = M2 ^ ts
  1189. M2 = M1
  1190. M1 = M0 ^ ts
  1191. M0 = ts
  1192. V30 ^= M0
  1193. V31 ^= M1
  1194. V32 ^= M2
  1195. V33 ^= M3
  1196. V34 ^= M4
  1197. V35 ^= M5
  1198. V36 ^= M6
  1199. V37 ^= M7
  1200. ts = M7
  1201. M7 = M6
  1202. M6 = M5
  1203. M5 = M4
  1204. M4 = M3 ^ ts
  1205. M3 = M2 ^ ts
  1206. M2 = M1
  1207. M1 = M0 ^ ts
  1208. M0 = ts
  1209. V40 ^= M0
  1210. V41 ^= M1
  1211. V42 ^= M2
  1212. V43 ^= M3
  1213. V44 ^= M4
  1214. V45 ^= M5
  1215. V46 ^= M6
  1216. V47 ^= M7
  1217. }
  1218. {
  1219. var ul, uh, vl, vh, tws uint32
  1220. var W0, W1, W2, W3, W4, W5, W6, W7, tw uint64
  1221. V14 = ((V14 << 1) | (V14 >> (32 - 1)))
  1222. V15 = ((V15 << 1) | (V15 >> (32 - 1)))
  1223. V16 = ((V16 << 1) | (V16 >> (32 - 1)))
  1224. V17 = ((V17 << 1) | (V17 >> (32 - 1)))
  1225. V24 = ((V24 << 2) | (V24 >> (32 - 2)))
  1226. V25 = ((V25 << 2) | (V25 >> (32 - 2)))
  1227. V26 = ((V26 << 2) | (V26 >> (32 - 2)))
  1228. V27 = ((V27 << 2) | (V27 >> (32 - 2)))
  1229. V34 = ((V34 << 3) | (V34 >> (32 - 3)))
  1230. V35 = ((V35 << 3) | (V35 >> (32 - 3)))
  1231. V36 = ((V36 << 3) | (V36 >> (32 - 3)))
  1232. V37 = ((V37 << 3) | (V37 >> (32 - 3)))
  1233. V44 = ((V44 << 4) | (V44 >> (32 - 4)))
  1234. V45 = ((V45 << 4) | (V45 >> (32 - 4)))
  1235. V46 = ((V46 << 4) | (V46 >> (32 - 4)))
  1236. V47 = ((V47 << 4) | (V47 >> (32 - 4)))
  1237. W0 = uint64(V00) | (uint64(V10) << 32)
  1238. W1 = uint64(V01) | (uint64(V11) << 32)
  1239. W2 = uint64(V02) | (uint64(V12) << 32)
  1240. W3 = uint64(V03) | (uint64(V13) << 32)
  1241. W4 = uint64(V04) | (uint64(V14) << 32)
  1242. W5 = uint64(V05) | (uint64(V15) << 32)
  1243. W6 = uint64(V06) | (uint64(V16) << 32)
  1244. W7 = uint64(V07) | (uint64(V17) << 32)
  1245. for r := uintptr(0); r < 8; r++ {
  1246. tw = W0
  1247. W0 |= W1
  1248. W2 ^= W3
  1249. W1 = ^W1
  1250. W0 ^= W3
  1251. W3 &= tw
  1252. W1 ^= W3
  1253. W3 ^= W2
  1254. W2 &= W0
  1255. W0 = ^W0
  1256. W2 ^= W1
  1257. W1 |= W3
  1258. tw ^= W1
  1259. W3 ^= W2
  1260. W2 &= W1
  1261. W1 ^= W0
  1262. W0 = tw
  1263. tw = W5
  1264. W5 |= W6
  1265. W7 ^= W4
  1266. W6 = ^W6
  1267. W5 ^= W4
  1268. W4 &= tw
  1269. W6 ^= W4
  1270. W4 ^= W7
  1271. W7 &= W5
  1272. W5 = ^W5
  1273. W7 ^= W6
  1274. W6 |= W4
  1275. tw ^= W6
  1276. W4 ^= W7
  1277. W7 &= W6
  1278. W6 ^= W5
  1279. W5 = tw
  1280. W4 ^= W0
  1281. ul = uint32(W0)
  1282. uh = uint32((W0 >> 32))
  1283. vl = uint32(W4)
  1284. vh = uint32((W4 >> 32))
  1285. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  1286. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  1287. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  1288. vl = ((vl << 1) | (vl >> (32 - 1)))
  1289. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  1290. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  1291. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  1292. vh = ((vh << 1) | (vh >> (32 - 1)))
  1293. W0 = uint64(ul) | (uint64(uh) << 32)
  1294. W4 = uint64(vl) | (uint64(vh) << 32)
  1295. W5 ^= W1
  1296. ul = uint32(W1)
  1297. uh = uint32((W1 >> 32))
  1298. vl = uint32(W5)
  1299. vh = uint32((W5 >> 32))
  1300. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  1301. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  1302. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  1303. vl = ((vl << 1) | (vl >> (32 - 1)))
  1304. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  1305. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  1306. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  1307. vh = ((vh << 1) | (vh >> (32 - 1)))
  1308. W1 = uint64(ul) | (uint64(uh) << 32)
  1309. W5 = uint64(vl) | (uint64(vh) << 32)
  1310. W6 ^= W2
  1311. ul = uint32(W2)
  1312. uh = uint32((W2 >> 32))
  1313. vl = uint32(W6)
  1314. vh = uint32((W6 >> 32))
  1315. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  1316. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  1317. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  1318. vl = ((vl << 1) | (vl >> (32 - 1)))
  1319. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  1320. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  1321. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  1322. vh = ((vh << 1) | (vh >> (32 - 1)))
  1323. W2 = uint64(ul) | (uint64(uh) << 32)
  1324. W6 = uint64(vl) | (uint64(vh) << 32)
  1325. W7 ^= W3
  1326. ul = uint32(W3)
  1327. uh = uint32((W3 >> 32))
  1328. vl = uint32(W7)
  1329. vh = uint32((W7 >> 32))
  1330. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  1331. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  1332. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  1333. vl = ((vl << 1) | (vl >> (32 - 1)))
  1334. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  1335. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  1336. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  1337. vh = ((vh << 1) | (vh >> (32 - 1)))
  1338. W3 = uint64(ul) | (uint64(uh) << 32)
  1339. W7 = uint64(vl) | (uint64(vh) << 32)
  1340. W0 ^= kRCW010[r]
  1341. W4 ^= kRCW014[r]
  1342. }
  1343. V00 = uint32(W0)
  1344. V10 = uint32((W0 >> 32))
  1345. V01 = uint32(W1)
  1346. V11 = uint32((W1 >> 32))
  1347. V02 = uint32(W2)
  1348. V12 = uint32((W2 >> 32))
  1349. V03 = uint32(W3)
  1350. V13 = uint32((W3 >> 32))
  1351. V04 = uint32(W4)
  1352. V14 = uint32((W4 >> 32))
  1353. V05 = uint32(W5)
  1354. V15 = uint32((W5 >> 32))
  1355. V06 = uint32(W6)
  1356. V16 = uint32((W6 >> 32))
  1357. V07 = uint32(W7)
  1358. V17 = uint32((W7 >> 32))
  1359. W0 = uint64(V20) | (uint64(V30) << 32)
  1360. W1 = uint64(V21) | (uint64(V31) << 32)
  1361. W2 = uint64(V22) | (uint64(V32) << 32)
  1362. W3 = uint64(V23) | (uint64(V33) << 32)
  1363. W4 = uint64(V24) | (uint64(V34) << 32)
  1364. W5 = uint64(V25) | (uint64(V35) << 32)
  1365. W6 = uint64(V26) | (uint64(V36) << 32)
  1366. W7 = uint64(V27) | (uint64(V37) << 32)
  1367. for r := uintptr(0); r < 8; r++ {
  1368. tw = W0
  1369. W0 |= W1
  1370. W2 ^= W3
  1371. W1 = ^W1
  1372. W0 ^= W3
  1373. W3 &= tw
  1374. W1 ^= W3
  1375. W3 ^= W2
  1376. W2 &= W0
  1377. W0 = ^W0
  1378. W2 ^= W1
  1379. W1 |= W3
  1380. tw ^= W1
  1381. W3 ^= W2
  1382. W2 &= W1
  1383. W1 ^= W0
  1384. W0 = tw
  1385. tw = W5
  1386. W5 |= W6
  1387. W7 ^= W4
  1388. W6 = ^W6
  1389. W5 ^= W4
  1390. W4 &= tw
  1391. W6 ^= W4
  1392. W4 ^= W7
  1393. W7 &= W5
  1394. W5 = ^W5
  1395. W7 ^= W6
  1396. W6 |= W4
  1397. tw ^= W6
  1398. W4 ^= W7
  1399. W7 &= W6
  1400. W6 ^= W5
  1401. W5 = tw
  1402. W4 ^= W0
  1403. ul = uint32(W0)
  1404. uh = uint32((W0 >> 32))
  1405. vl = uint32(W4)
  1406. vh = uint32((W4 >> 32))
  1407. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  1408. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  1409. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  1410. vl = ((vl << 1) | (vl >> (32 - 1)))
  1411. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  1412. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  1413. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  1414. vh = ((vh << 1) | (vh >> (32 - 1)))
  1415. W0 = uint64(ul) | (uint64(uh) << 32)
  1416. W4 = uint64(vl) | (uint64(vh) << 32)
  1417. W5 ^= W1
  1418. ul = uint32(W1)
  1419. uh = uint32((W1 >> 32))
  1420. vl = uint32(W5)
  1421. vh = uint32((W5 >> 32))
  1422. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  1423. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  1424. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  1425. vl = ((vl << 1) | (vl >> (32 - 1)))
  1426. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  1427. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  1428. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  1429. vh = ((vh << 1) | (vh >> (32 - 1)))
  1430. W1 = uint64(ul) | (uint64(uh) << 32)
  1431. W5 = uint64(vl) | (uint64(vh) << 32)
  1432. W6 ^= W2
  1433. ul = uint32(W2)
  1434. uh = uint32((W2 >> 32))
  1435. vl = uint32(W6)
  1436. vh = uint32((W6 >> 32))
  1437. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  1438. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  1439. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  1440. vl = ((vl << 1) | (vl >> (32 - 1)))
  1441. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  1442. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  1443. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  1444. vh = ((vh << 1) | (vh >> (32 - 1)))
  1445. W2 = uint64(ul) | (uint64(uh) << 32)
  1446. W6 = uint64(vl) | (uint64(vh) << 32)
  1447. W7 ^= W3
  1448. ul = uint32(W3)
  1449. uh = uint32((W3 >> 32))
  1450. vl = uint32(W7)
  1451. vh = uint32((W7 >> 32))
  1452. ul = ((ul << 2) | (ul >> (32 - 2))) ^ vl
  1453. vl = ((vl << 14) | (vl >> (32 - 14))) ^ ul
  1454. ul = ((ul << 10) | (ul >> (32 - 10))) ^ vl
  1455. vl = ((vl << 1) | (vl >> (32 - 1)))
  1456. uh = ((uh << 2) | (uh >> (32 - 2))) ^ vh
  1457. vh = ((vh << 14) | (vh >> (32 - 14))) ^ uh
  1458. uh = ((uh << 10) | (uh >> (32 - 10))) ^ vh
  1459. vh = ((vh << 1) | (vh >> (32 - 1)))
  1460. W3 = uint64(ul) | (uint64(uh) << 32)
  1461. W7 = uint64(vl) | (uint64(vh) << 32)
  1462. W0 ^= kRCW230[r]
  1463. W4 ^= kRCW234[r]
  1464. }
  1465. V20 = uint32(W0)
  1466. V30 = uint32((W0 >> 32))
  1467. V21 = uint32(W1)
  1468. V31 = uint32((W1 >> 32))
  1469. V22 = uint32(W2)
  1470. V32 = uint32((W2 >> 32))
  1471. V23 = uint32(W3)
  1472. V33 = uint32((W3 >> 32))
  1473. V24 = uint32(W4)
  1474. V34 = uint32((W4 >> 32))
  1475. V25 = uint32(W5)
  1476. V35 = uint32((W5 >> 32))
  1477. V26 = uint32(W6)
  1478. V36 = uint32((W6 >> 32))
  1479. V27 = uint32(W7)
  1480. V37 = uint32((W7 >> 32))
  1481. for r := uintptr(0); r < 8; r++ {
  1482. tws = V40
  1483. V40 |= V41
  1484. V42 ^= V43
  1485. V41 = ^V41
  1486. V40 ^= V43
  1487. V43 &= tws
  1488. V41 ^= V43
  1489. V43 ^= V42
  1490. V42 &= V40
  1491. V40 = ^V40
  1492. V42 ^= V41
  1493. V41 |= V43
  1494. tws ^= V41
  1495. V43 ^= V42
  1496. V42 &= V41
  1497. V41 ^= V40
  1498. V40 = tws
  1499. tws = V45
  1500. V45 |= V46
  1501. V47 ^= V44
  1502. V46 = ^V46
  1503. V45 ^= V44
  1504. V44 &= tws
  1505. V46 ^= V44
  1506. V44 ^= V47
  1507. V47 &= V45
  1508. V45 = ^V45
  1509. V47 ^= V46
  1510. V46 |= V44
  1511. tws ^= V46
  1512. V44 ^= V47
  1513. V47 &= V46
  1514. V46 ^= V45
  1515. V45 = tws
  1516. V44 ^= V40
  1517. V40 = ((V40 << 2) | (V40 >> (32 - 2))) ^ V44
  1518. V44 = ((V44 << 14) | (V44 >> (32 - 14))) ^ V40
  1519. V40 = ((V40 << 10) | (V40 >> (32 - 10))) ^ V44
  1520. V44 = ((V44 << 1) | (V44 >> (32 - 1)))
  1521. V45 ^= V41
  1522. V41 = ((V41 << 2) | (V41 >> (32 - 2))) ^ V45
  1523. V45 = ((V45 << 14) | (V45 >> (32 - 14))) ^ V41
  1524. V41 = ((V41 << 10) | (V41 >> (32 - 10))) ^ V45
  1525. V45 = ((V45 << 1) | (V45 >> (32 - 1)))
  1526. V46 ^= V42
  1527. V42 = ((V42 << 2) | (V42 >> (32 - 2))) ^ V46
  1528. V46 = ((V46 << 14) | (V46 >> (32 - 14))) ^ V42
  1529. V42 = ((V42 << 10) | (V42 >> (32 - 10))) ^ V46
  1530. V46 = ((V46 << 1) | (V46 >> (32 - 1)))
  1531. V47 ^= V43
  1532. V43 = ((V43 << 2) | (V43 >> (32 - 2))) ^ V47
  1533. V47 = ((V47 << 14) | (V47 >> (32 - 14))) ^ V43
  1534. V43 = ((V43 << 10) | (V43 >> (32 - 10))) ^ V47
  1535. V47 = ((V47 << 1) | (V47 >> (32 - 1)))
  1536. V40 ^= kRC40[r]
  1537. V44 ^= kRC44[r]
  1538. }
  1539. }
  1540. switch i {
  1541. case 0:
  1542. memset(buf[:], 0)
  1543. break
  1544. case 1:
  1545. encUInt32be(dst[0:], V00^V10^V20^V30^V40)
  1546. encUInt32be(dst[4:], V01^V11^V21^V31^V41)
  1547. encUInt32be(dst[8:], V02^V12^V22^V32^V42)
  1548. encUInt32be(dst[12:], V03^V13^V23^V33^V43)
  1549. encUInt32be(dst[16:], V04^V14^V24^V34^V44)
  1550. encUInt32be(dst[20:], V05^V15^V25^V35^V45)
  1551. encUInt32be(dst[24:], V06^V16^V26^V36^V46)
  1552. encUInt32be(dst[28:], V07^V17^V27^V37^V47)
  1553. break
  1554. case 2:
  1555. encUInt32be(dst[32:], V00^V10^V20^V30^V40)
  1556. encUInt32be(dst[36:], V01^V11^V21^V31^V41)
  1557. encUInt32be(dst[40:], V02^V12^V22^V32^V42)
  1558. encUInt32be(dst[44:], V03^V13^V23^V33^V43)
  1559. encUInt32be(dst[48:], V04^V14^V24^V34^V44)
  1560. encUInt32be(dst[52:], V05^V15^V25^V35^V45)
  1561. encUInt32be(dst[56:], V06^V16^V26^V36^V46)
  1562. encUInt32be(dst[60:], V07^V17^V27^V37^V47)
  1563. break
  1564. }
  1565. }
  1566. ref.Reset()
  1567. return nil
  1568. }
  1569. // Size returns the number of bytes required to store the hash.
  1570. func (*digest) Size() int {
  1571. return HashSize
  1572. }
  1573. // BlockSize returns the block size of the hash.
  1574. func (*digest) BlockSize() int {
  1575. return int(BlockSize)
  1576. }
  1577. ////////////////
  1578. func memset(dst []byte, src byte) {
  1579. for i := range dst {
  1580. dst[i] = src
  1581. }
  1582. }
  1583. func decUInt32be(src []byte) uint32 {
  1584. return (uint32(src[0])<<24 |
  1585. uint32(src[1])<<16 |
  1586. uint32(src[2])<<8 |
  1587. uint32(src[3]))
  1588. }
  1589. func encUInt32be(dst []byte, src uint32) {
  1590. dst[0] = uint8(src >> 24)
  1591. dst[1] = uint8(src >> 16)
  1592. dst[2] = uint8(src >> 8)
  1593. dst[3] = uint8(src)
  1594. }
  1595. ////////////////
  1596. var kInit = [5][8]uint32{
  1597. {
  1598. uint32(0x6d251e69), uint32(0x44b051e0),
  1599. uint32(0x4eaa6fb4), uint32(0xdbf78465),
  1600. uint32(0x6e292011), uint32(0x90152df4),
  1601. uint32(0xee058139), uint32(0xdef610bb),
  1602. },
  1603. {
  1604. uint32(0xc3b44b95), uint32(0xd9d2f256),
  1605. uint32(0x70eee9a0), uint32(0xde099fa3),
  1606. uint32(0x5d9b0557), uint32(0x8fc944b3),
  1607. uint32(0xcf1ccf0e), uint32(0x746cd581),
  1608. },
  1609. {
  1610. uint32(0xf7efc89d), uint32(0x5dba5781),
  1611. uint32(0x04016ce5), uint32(0xad659c05),
  1612. uint32(0x0306194f), uint32(0x666d1836),
  1613. uint32(0x24aa230a), uint32(0x8b264ae7),
  1614. },
  1615. {
  1616. uint32(0x858075d5), uint32(0x36d79cce),
  1617. uint32(0xe571f7d7), uint32(0x204b1f67),
  1618. uint32(0x35870c6a), uint32(0x57e9e923),
  1619. uint32(0x14bcb808), uint32(0x7cde72ce),
  1620. },
  1621. {
  1622. uint32(0x6c68e9be), uint32(0x5ec41e22),
  1623. uint32(0xc825b7c7), uint32(0xaffb4363),
  1624. uint32(0xf5df3999), uint32(0x0fc688f1),
  1625. uint32(0xb07224cc), uint32(0x03e86cea),
  1626. },
  1627. }
  1628. var kRC40 = [8]uint32{
  1629. uint32(0xf0d2e9e3), uint32(0xac11d7fa),
  1630. uint32(0x1bcb66f2), uint32(0x6f2d9bc9),
  1631. uint32(0x78602649), uint32(0x8edae952),
  1632. uint32(0x3b6ba548), uint32(0xedae9520),
  1633. }
  1634. var kRC44 = [8]uint32{
  1635. uint32(0x5090d577), uint32(0x2d1925ab),
  1636. uint32(0xb46496ac), uint32(0xd1925ab0),
  1637. uint32(0x29131ab6), uint32(0x0fc053c3),
  1638. uint32(0x3f014f0c), uint32(0xfc053c31),
  1639. }
  1640. var kRCW010 = [8]uint64{
  1641. uint64(0xb6de10ed303994a6), uint64(0x70f47aaec0e65299),
  1642. uint64(0x0707a3d46cc33a12), uint64(0x1c1e8f51dc56983e),
  1643. uint64(0x707a3d451e00108f), uint64(0xaeb285627800423d),
  1644. uint64(0xbaca15898f5b7882), uint64(0x40a46f3e96e1db12),
  1645. }
  1646. var kRCW014 = [8]uint64{
  1647. uint64(0x01685f3de0337818), uint64(0x05a17cf4441ba90d),
  1648. uint64(0xbd09caca7f34d442), uint64(0xf4272b289389217f),
  1649. uint64(0x144ae5cce5a8bce6), uint64(0xfaa7ae2b5274baf4),
  1650. uint64(0x2e48f1c126889ba7), uint64(0xb923c7049a226e9d),
  1651. }
  1652. var kRCW230 = [8]uint64{
  1653. uint64(0xb213afa5fc20d9d2), uint64(0xc84ebe9534552e25),
  1654. uint64(0x4e608a227ad8818f), uint64(0x56d858fe8438764a),
  1655. uint64(0x343b138fbb6de032), uint64(0xd0ec4e3dedb780c8),
  1656. uint64(0x2ceb4882d9847356), uint64(0xb3ad2208a2c78434),
  1657. }
  1658. var kRCW234 = [8]uint64{
  1659. uint64(0xe028c9bfe25e72c1), uint64(0x44756f91e623bb72),
  1660. uint64(0x7e8fce325c58a4a4), uint64(0x956548be1e38e2e7),
  1661. uint64(0xfe191be278e38b9d), uint64(0x3cb226e527586719),
  1662. uint64(0x5944a28e36eda57f), uint64(0xa1c4c355703aace7),
  1663. }