1
0

md4block.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2009 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. // MD4 block step.
  5. // In its own file so that a faster assembly or C version
  6. // can be substituted easily.
  7. package md4
  8. var shift1 = []uint{3, 7, 11, 19}
  9. var shift2 = []uint{3, 5, 9, 13}
  10. var shift3 = []uint{3, 9, 11, 15}
  11. var xIndex2 = []uint{0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15}
  12. var xIndex3 = []uint{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}
  13. func _Block(dig *digest, p []byte) int {
  14. a := dig.s[0]
  15. b := dig.s[1]
  16. c := dig.s[2]
  17. d := dig.s[3]
  18. n := 0
  19. var X [16]uint32
  20. for len(p) >= _Chunk {
  21. aa, bb, cc, dd := a, b, c, d
  22. j := 0
  23. for i := 0; i < 16; i++ {
  24. X[i] = uint32(p[j]) | uint32(p[j+1])<<8 | uint32(p[j+2])<<16 | uint32(p[j+3])<<24
  25. j += 4
  26. }
  27. // If this needs to be made faster in the future,
  28. // the usual trick is to unroll each of these
  29. // loops by a factor of 4; that lets you replace
  30. // the shift[] lookups with constants and,
  31. // with suitable variable renaming in each
  32. // unrolled body, delete the a, b, c, d = d, a, b, c
  33. // (or you can let the optimizer do the renaming).
  34. //
  35. // The index variables are uint so that % by a power
  36. // of two can be optimized easily by a compiler.
  37. // Round 1.
  38. for i := uint(0); i < 16; i++ {
  39. x := i
  40. s := shift1[i%4]
  41. f := ((c ^ d) & b) ^ d
  42. a += f + X[x]
  43. a = a<<s | a>>(32-s)
  44. a, b, c, d = d, a, b, c
  45. }
  46. // Round 2.
  47. for i := uint(0); i < 16; i++ {
  48. x := xIndex2[i]
  49. s := shift2[i%4]
  50. g := (b & c) | (b & d) | (c & d)
  51. a += g + X[x] + 0x5a827999
  52. a = a<<s | a>>(32-s)
  53. a, b, c, d = d, a, b, c
  54. }
  55. // Round 3.
  56. for i := uint(0); i < 16; i++ {
  57. x := xIndex3[i]
  58. s := shift3[i%4]
  59. h := b ^ c ^ d
  60. a += h + X[x] + 0x6ed9eba1
  61. a = a<<s | a>>(32-s)
  62. a, b, c, d = d, a, b, c
  63. }
  64. a += aa
  65. b += bb
  66. c += cc
  67. d += dd
  68. p = p[_Chunk:]
  69. n += _Chunk
  70. }
  71. dig.s[0] = a
  72. dig.s[1] = b
  73. dig.s[2] = c
  74. dig.s[3] = d
  75. return n
  76. }