md5block.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright 2013 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. // DO NOT EDIT.
  5. // Generate with: go run gen.go -full -output md5block.go
  6. package md5
  7. import (
  8. "runtime"
  9. "unsafe"
  10. )
  11. const x86 = runtime.GOARCH == "amd64" || runtime.GOARCH == "386"
  12. var littleEndian bool
  13. func init() {
  14. x := uint32(0x04030201)
  15. y := [4]byte{0x1, 0x2, 0x3, 0x4}
  16. littleEndian = *(*[4]byte)(unsafe.Pointer(&x)) == y
  17. }
  18. func blockGeneric(dig *digest, p []byte) {
  19. a := dig.s[0]
  20. b := dig.s[1]
  21. c := dig.s[2]
  22. d := dig.s[3]
  23. var X *[16]uint32
  24. var xbuf [16]uint32
  25. for len(p) >= chunk {
  26. aa, bb, cc, dd := a, b, c, d
  27. // This is a constant condition - it is not evaluated on each iteration.
  28. if x86 {
  29. // MD5 was designed so that x86 processors can just iterate
  30. // over the block data directly as uint32s, and we generate
  31. // less code and run 1.3x faster if we take advantage of that.
  32. // My apologies.
  33. X = (*[16]uint32)(unsafe.Pointer(&p[0]))
  34. } else if littleEndian && uintptr(unsafe.Pointer(&p[0]))&(unsafe.Alignof(uint32(0))-1) == 0 {
  35. X = (*[16]uint32)(unsafe.Pointer(&p[0]))
  36. } else {
  37. X = &xbuf
  38. j := 0
  39. for i := 0; i < 16; i++ {
  40. X[i&15] = uint32(p[j]) | uint32(p[j+1])<<8 | uint32(p[j+2])<<16 | uint32(p[j+3])<<24
  41. j += 4
  42. }
  43. }
  44. // Round 1.
  45. a += (((c ^ d) & b) ^ d) + X[0] + 3614090360
  46. a = a<<7 | a>>(32-7) + b
  47. d += (((b ^ c) & a) ^ c) + X[1] + 3905402710
  48. d = d<<12 | d>>(32-12) + a
  49. c += (((a ^ b) & d) ^ b) + X[2] + 606105819
  50. c = c<<17 | c>>(32-17) + d
  51. b += (((d ^ a) & c) ^ a) + X[3] + 3250441966
  52. b = b<<22 | b>>(32-22) + c
  53. a += (((c ^ d) & b) ^ d) + X[4] + 4118548399
  54. a = a<<7 | a>>(32-7) + b
  55. d += (((b ^ c) & a) ^ c) + X[5] + 1200080426
  56. d = d<<12 | d>>(32-12) + a
  57. c += (((a ^ b) & d) ^ b) + X[6] + 2821735955
  58. c = c<<17 | c>>(32-17) + d
  59. b += (((d ^ a) & c) ^ a) + X[7] + 4249261313
  60. b = b<<22 | b>>(32-22) + c
  61. a += (((c ^ d) & b) ^ d) + X[8] + 1770035416
  62. a = a<<7 | a>>(32-7) + b
  63. d += (((b ^ c) & a) ^ c) + X[9] + 2336552879
  64. d = d<<12 | d>>(32-12) + a
  65. c += (((a ^ b) & d) ^ b) + X[10] + 4294925233
  66. c = c<<17 | c>>(32-17) + d
  67. b += (((d ^ a) & c) ^ a) + X[11] + 2304563134
  68. b = b<<22 | b>>(32-22) + c
  69. a += (((c ^ d) & b) ^ d) + X[12] + 1804603682
  70. a = a<<7 | a>>(32-7) + b
  71. d += (((b ^ c) & a) ^ c) + X[13] + 4254626195
  72. d = d<<12 | d>>(32-12) + a
  73. c += (((a ^ b) & d) ^ b) + X[14] + 2792965006
  74. c = c<<17 | c>>(32-17) + d
  75. b += (((d ^ a) & c) ^ a) + X[15] + 1236535329
  76. b = b<<22 | b>>(32-22) + c
  77. // Round 2.
  78. a += (((b ^ c) & d) ^ c) + X[(1+5*0)&15] + 4129170786
  79. a = a<<5 | a>>(32-5) + b
  80. d += (((a ^ b) & c) ^ b) + X[(1+5*1)&15] + 3225465664
  81. d = d<<9 | d>>(32-9) + a
  82. c += (((d ^ a) & b) ^ a) + X[(1+5*2)&15] + 643717713
  83. c = c<<14 | c>>(32-14) + d
  84. b += (((c ^ d) & a) ^ d) + X[(1+5*3)&15] + 3921069994
  85. b = b<<20 | b>>(32-20) + c
  86. a += (((b ^ c) & d) ^ c) + X[(1+5*4)&15] + 3593408605
  87. a = a<<5 | a>>(32-5) + b
  88. d += (((a ^ b) & c) ^ b) + X[(1+5*5)&15] + 38016083
  89. d = d<<9 | d>>(32-9) + a
  90. c += (((d ^ a) & b) ^ a) + X[(1+5*6)&15] + 3634488961
  91. c = c<<14 | c>>(32-14) + d
  92. b += (((c ^ d) & a) ^ d) + X[(1+5*7)&15] + 3889429448
  93. b = b<<20 | b>>(32-20) + c
  94. a += (((b ^ c) & d) ^ c) + X[(1+5*8)&15] + 568446438
  95. a = a<<5 | a>>(32-5) + b
  96. d += (((a ^ b) & c) ^ b) + X[(1+5*9)&15] + 3275163606
  97. d = d<<9 | d>>(32-9) + a
  98. c += (((d ^ a) & b) ^ a) + X[(1+5*10)&15] + 4107603335
  99. c = c<<14 | c>>(32-14) + d
  100. b += (((c ^ d) & a) ^ d) + X[(1+5*11)&15] + 1163531501
  101. b = b<<20 | b>>(32-20) + c
  102. a += (((b ^ c) & d) ^ c) + X[(1+5*12)&15] + 2850285829
  103. a = a<<5 | a>>(32-5) + b
  104. d += (((a ^ b) & c) ^ b) + X[(1+5*13)&15] + 4243563512
  105. d = d<<9 | d>>(32-9) + a
  106. c += (((d ^ a) & b) ^ a) + X[(1+5*14)&15] + 1735328473
  107. c = c<<14 | c>>(32-14) + d
  108. b += (((c ^ d) & a) ^ d) + X[(1+5*15)&15] + 2368359562
  109. b = b<<20 | b>>(32-20) + c
  110. // Round 3.
  111. a += (b ^ c ^ d) + X[(5+3*0)&15] + 4294588738
  112. a = a<<4 | a>>(32-4) + b
  113. d += (a ^ b ^ c) + X[(5+3*1)&15] + 2272392833
  114. d = d<<11 | d>>(32-11) + a
  115. c += (d ^ a ^ b) + X[(5+3*2)&15] + 1839030562
  116. c = c<<16 | c>>(32-16) + d
  117. b += (c ^ d ^ a) + X[(5+3*3)&15] + 4259657740
  118. b = b<<23 | b>>(32-23) + c
  119. a += (b ^ c ^ d) + X[(5+3*4)&15] + 2763975236
  120. a = a<<4 | a>>(32-4) + b
  121. d += (a ^ b ^ c) + X[(5+3*5)&15] + 1272893353
  122. d = d<<11 | d>>(32-11) + a
  123. c += (d ^ a ^ b) + X[(5+3*6)&15] + 4139469664
  124. c = c<<16 | c>>(32-16) + d
  125. b += (c ^ d ^ a) + X[(5+3*7)&15] + 3200236656
  126. b = b<<23 | b>>(32-23) + c
  127. a += (b ^ c ^ d) + X[(5+3*8)&15] + 681279174
  128. a = a<<4 | a>>(32-4) + b
  129. d += (a ^ b ^ c) + X[(5+3*9)&15] + 3936430074
  130. d = d<<11 | d>>(32-11) + a
  131. c += (d ^ a ^ b) + X[(5+3*10)&15] + 3572445317
  132. c = c<<16 | c>>(32-16) + d
  133. b += (c ^ d ^ a) + X[(5+3*11)&15] + 76029189
  134. b = b<<23 | b>>(32-23) + c
  135. a += (b ^ c ^ d) + X[(5+3*12)&15] + 3654602809
  136. a = a<<4 | a>>(32-4) + b
  137. d += (a ^ b ^ c) + X[(5+3*13)&15] + 3873151461
  138. d = d<<11 | d>>(32-11) + a
  139. c += (d ^ a ^ b) + X[(5+3*14)&15] + 530742520
  140. c = c<<16 | c>>(32-16) + d
  141. b += (c ^ d ^ a) + X[(5+3*15)&15] + 3299628645
  142. b = b<<23 | b>>(32-23) + c
  143. // Round 4.
  144. a += (c ^ (b | ^d)) + X[(7*0)&15] + 4096336452
  145. a = a<<6 | a>>(32-6) + b
  146. d += (b ^ (a | ^c)) + X[(7*1)&15] + 1126891415
  147. d = d<<10 | d>>(32-10) + a
  148. c += (a ^ (d | ^b)) + X[(7*2)&15] + 2878612391
  149. c = c<<15 | c>>(32-15) + d
  150. b += (d ^ (c | ^a)) + X[(7*3)&15] + 4237533241
  151. b = b<<21 | b>>(32-21) + c
  152. a += (c ^ (b | ^d)) + X[(7*4)&15] + 1700485571
  153. a = a<<6 | a>>(32-6) + b
  154. d += (b ^ (a | ^c)) + X[(7*5)&15] + 2399980690
  155. d = d<<10 | d>>(32-10) + a
  156. c += (a ^ (d | ^b)) + X[(7*6)&15] + 4293915773
  157. c = c<<15 | c>>(32-15) + d
  158. b += (d ^ (c | ^a)) + X[(7*7)&15] + 2240044497
  159. b = b<<21 | b>>(32-21) + c
  160. a += (c ^ (b | ^d)) + X[(7*8)&15] + 1873313359
  161. a = a<<6 | a>>(32-6) + b
  162. d += (b ^ (a | ^c)) + X[(7*9)&15] + 4264355552
  163. d = d<<10 | d>>(32-10) + a
  164. c += (a ^ (d | ^b)) + X[(7*10)&15] + 2734768916
  165. c = c<<15 | c>>(32-15) + d
  166. b += (d ^ (c | ^a)) + X[(7*11)&15] + 1309151649
  167. b = b<<21 | b>>(32-21) + c
  168. a += (c ^ (b | ^d)) + X[(7*12)&15] + 4149444226
  169. a = a<<6 | a>>(32-6) + b
  170. d += (b ^ (a | ^c)) + X[(7*13)&15] + 3174756917
  171. d = d<<10 | d>>(32-10) + a
  172. c += (a ^ (d | ^b)) + X[(7*14)&15] + 718787259
  173. c = c<<15 | c>>(32-15) + d
  174. b += (d ^ (c | ^a)) + X[(7*15)&15] + 3951481745
  175. b = b<<21 | b>>(32-21) + c
  176. a += aa
  177. b += bb
  178. c += cc
  179. d += dd
  180. p = p[chunk:]
  181. }
  182. dig.s[0] = a
  183. dig.s[1] = b
  184. dig.s[2] = c
  185. dig.s[3] = d
  186. }