ripemd160block.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2010 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. // RIPEMD-160 block step.
  5. // In its own file so that a faster assembly or C version
  6. // can be substituted easily.
  7. package ripemd160
  8. // work buffer indices and roll amounts for one line
  9. var _n = [80]uint{
  10. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  11. 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
  12. 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
  13. 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
  14. 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13,
  15. }
  16. var _r = [80]uint{
  17. 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
  18. 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
  19. 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
  20. 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
  21. 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6,
  22. }
  23. // same for the other parallel one
  24. var n_ = [80]uint{
  25. 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
  26. 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
  27. 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
  28. 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
  29. 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11,
  30. }
  31. var r_ = [80]uint{
  32. 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
  33. 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
  34. 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
  35. 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
  36. 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11,
  37. }
  38. func _Block(md *digest, p []byte) int {
  39. n := 0
  40. var x [16]uint32
  41. var alpha, beta uint32
  42. for len(p) >= BlockSize {
  43. a, b, c, d, e := md.s[0], md.s[1], md.s[2], md.s[3], md.s[4]
  44. aa, bb, cc, dd, ee := a, b, c, d, e
  45. j := 0
  46. for i := 0; i < 16; i++ {
  47. x[i] = uint32(p[j]) | uint32(p[j+1])<<8 | uint32(p[j+2])<<16 | uint32(p[j+3])<<24
  48. j += 4
  49. }
  50. // round 1
  51. i := 0
  52. for i < 16 {
  53. alpha = a + (b ^ c ^ d) + x[_n[i]]
  54. s := _r[i]
  55. alpha = (alpha<<s | alpha>>(32-s)) + e
  56. beta = c<<10 | c>>22
  57. a, b, c, d, e = e, alpha, b, beta, d
  58. // parallel line
  59. alpha = aa + (bb ^ (cc | ^dd)) + x[n_[i]] + 0x50a28be6
  60. s = r_[i]
  61. alpha = (alpha<<s | alpha>>(32-s)) + ee
  62. beta = cc<<10 | cc>>22
  63. aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
  64. i++
  65. }
  66. // round 2
  67. for i < 32 {
  68. alpha = a + (b&c | ^b&d) + x[_n[i]] + 0x5a827999
  69. s := _r[i]
  70. alpha = (alpha<<s | alpha>>(32-s)) + e
  71. beta = c<<10 | c>>22
  72. a, b, c, d, e = e, alpha, b, beta, d
  73. // parallel line
  74. alpha = aa + (bb&dd | cc&^dd) + x[n_[i]] + 0x5c4dd124
  75. s = r_[i]
  76. alpha = (alpha<<s | alpha>>(32-s)) + ee
  77. beta = cc<<10 | cc>>22
  78. aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
  79. i++
  80. }
  81. // round 3
  82. for i < 48 {
  83. alpha = a + (b | ^c ^ d) + x[_n[i]] + 0x6ed9eba1
  84. s := _r[i]
  85. alpha = (alpha<<s | alpha>>(32-s)) + e
  86. beta = c<<10 | c>>22
  87. a, b, c, d, e = e, alpha, b, beta, d
  88. // parallel line
  89. alpha = aa + (bb | ^cc ^ dd) + x[n_[i]] + 0x6d703ef3
  90. s = r_[i]
  91. alpha = (alpha<<s | alpha>>(32-s)) + ee
  92. beta = cc<<10 | cc>>22
  93. aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
  94. i++
  95. }
  96. // round 4
  97. for i < 64 {
  98. alpha = a + (b&d | c&^d) + x[_n[i]] + 0x8f1bbcdc
  99. s := _r[i]
  100. alpha = (alpha<<s | alpha>>(32-s)) + e
  101. beta = c<<10 | c>>22
  102. a, b, c, d, e = e, alpha, b, beta, d
  103. // parallel line
  104. alpha = aa + (bb&cc | ^bb&dd) + x[n_[i]] + 0x7a6d76e9
  105. s = r_[i]
  106. alpha = (alpha<<s | alpha>>(32-s)) + ee
  107. beta = cc<<10 | cc>>22
  108. aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
  109. i++
  110. }
  111. // round 5
  112. for i < 80 {
  113. alpha = a + (b ^ (c | ^d)) + x[_n[i]] + 0xa953fd4e
  114. s := _r[i]
  115. alpha = (alpha<<s | alpha>>(32-s)) + e
  116. beta = c<<10 | c>>22
  117. a, b, c, d, e = e, alpha, b, beta, d
  118. // parallel line
  119. alpha = aa + (bb ^ cc ^ dd) + x[n_[i]]
  120. s = r_[i]
  121. alpha = (alpha<<s | alpha>>(32-s)) + ee
  122. beta = cc<<10 | cc>>22
  123. aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
  124. i++
  125. }
  126. // combine results
  127. dd += c + md.s[1]
  128. md.s[1] = md.s[2] + d + ee
  129. md.s[2] = md.s[3] + e + aa
  130. md.s[3] = md.s[4] + a + bb
  131. md.s[4] = md.s[0] + b + cc
  132. md.s[0] = dd
  133. p = p[BlockSize:]
  134. n += BlockSize
  135. }
  136. return n
  137. }