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.

sha256block.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright (c) 2009 The Go Authors. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are
  5. met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above
  9. copyright notice, this list of conditions and the following disclaimer
  10. in the documentation and/or other materials provided with the
  11. distribution.
  12. * Neither the name of Google Inc. nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // SHA256 block step.
  28. // In its own file so that a faster assembly or C version
  29. // can be substituted easily.
  30. package migrations
  31. import "math/bits"
  32. var _K = []uint32{
  33. 0x428a2f98,
  34. 0x71374491,
  35. 0xb5c0fbcf,
  36. 0xe9b5dba5,
  37. 0x3956c25b,
  38. 0x59f111f1,
  39. 0x923f82a4,
  40. 0xab1c5ed5,
  41. 0xd807aa98,
  42. 0x12835b01,
  43. 0x243185be,
  44. 0x550c7dc3,
  45. 0x72be5d74,
  46. 0x80deb1fe,
  47. 0x9bdc06a7,
  48. 0xc19bf174,
  49. 0xe49b69c1,
  50. 0xefbe4786,
  51. 0x0fc19dc6,
  52. 0x240ca1cc,
  53. 0x2de92c6f,
  54. 0x4a7484aa,
  55. 0x5cb0a9dc,
  56. 0x76f988da,
  57. 0x983e5152,
  58. 0xa831c66d,
  59. 0xb00327c8,
  60. 0xbf597fc7,
  61. 0xc6e00bf3,
  62. 0xd5a79147,
  63. 0x06ca6351,
  64. 0x14292967,
  65. 0x27b70a85,
  66. 0x2e1b2138,
  67. 0x4d2c6dfc,
  68. 0x53380d13,
  69. 0x650a7354,
  70. 0x766a0abb,
  71. 0x81c2c92e,
  72. 0x92722c85,
  73. 0xa2bfe8a1,
  74. 0xa81a664b,
  75. 0xc24b8b70,
  76. 0xc76c51a3,
  77. 0xd192e819,
  78. 0xd6990624,
  79. 0xf40e3585,
  80. 0x106aa070,
  81. 0x19a4c116,
  82. 0x1e376c08,
  83. 0x2748774c,
  84. 0x34b0bcb5,
  85. 0x391c0cb3,
  86. 0x4ed8aa4a,
  87. 0x5b9cca4f,
  88. 0x682e6ff3,
  89. 0x748f82ee,
  90. 0x78a5636f,
  91. 0x84c87814,
  92. 0x8cc70208,
  93. 0x90befffa,
  94. 0xa4506ceb,
  95. 0xbef9a3f7,
  96. 0xc67178f2,
  97. }
  98. func sha256BlockGeneric(dig *digest, p []byte) {
  99. var w [64]uint32
  100. h0, h1, h2, h3, h4, h5, h6, h7 := dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4], dig.h[5], dig.h[6], dig.h[7]
  101. for len(p) >= chunk {
  102. // Can interlace the computation of w with the
  103. // rounds below if needed for speed.
  104. for i := 0; i < 16; i++ {
  105. j := i * 4
  106. w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[j+2])<<8 | uint32(p[j+3])
  107. }
  108. for i := 16; i < 64; i++ {
  109. v1 := w[i-2]
  110. t1 := (bits.RotateLeft32(v1, -17)) ^ (bits.RotateLeft32(v1, -19)) ^ (v1 >> 10)
  111. v2 := w[i-15]
  112. t2 := (bits.RotateLeft32(v2, -7)) ^ (bits.RotateLeft32(v2, -18)) ^ (v2 >> 3)
  113. w[i] = t1 + w[i-7] + t2 + w[i-16]
  114. }
  115. a, b, c, d, e, f, g, h := h0, h1, h2, h3, h4, h5, h6, h7
  116. for i := 0; i < 64; i++ {
  117. t1 := h + ((bits.RotateLeft32(e, -6)) ^ (bits.RotateLeft32(e, -11)) ^ (bits.RotateLeft32(e, -25))) + ((e & f) ^ (^e & g)) + _K[i] + w[i]
  118. t2 := ((bits.RotateLeft32(a, -2)) ^ (bits.RotateLeft32(a, -13)) ^ (bits.RotateLeft32(a, -22))) + ((a & b) ^ (a & c) ^ (b & c))
  119. h = g
  120. g = f
  121. f = e
  122. e = d + t1
  123. d = c
  124. c = b
  125. b = a
  126. a = t1 + t2
  127. }
  128. h0 += a
  129. h1 += b
  130. h2 += c
  131. h3 += d
  132. h4 += e
  133. h5 += f
  134. h6 += g
  135. h7 += h
  136. p = p[chunk:]
  137. }
  138. dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4], dig.h[5], dig.h[6], dig.h[7] = h0, h1, h2, h3, h4, h5, h6, h7
  139. }