sha256block.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. //go:build !386 && !amd64
  5. // +build !386,!amd64
  6. // SHA256 block step.
  7. // In its own file so that a faster assembly or C version
  8. // can be substituted easily.
  9. package sha256
  10. var _K = []uint32{
  11. 0x428a2f98,
  12. 0x71374491,
  13. 0xb5c0fbcf,
  14. 0xe9b5dba5,
  15. 0x3956c25b,
  16. 0x59f111f1,
  17. 0x923f82a4,
  18. 0xab1c5ed5,
  19. 0xd807aa98,
  20. 0x12835b01,
  21. 0x243185be,
  22. 0x550c7dc3,
  23. 0x72be5d74,
  24. 0x80deb1fe,
  25. 0x9bdc06a7,
  26. 0xc19bf174,
  27. 0xe49b69c1,
  28. 0xefbe4786,
  29. 0x0fc19dc6,
  30. 0x240ca1cc,
  31. 0x2de92c6f,
  32. 0x4a7484aa,
  33. 0x5cb0a9dc,
  34. 0x76f988da,
  35. 0x983e5152,
  36. 0xa831c66d,
  37. 0xb00327c8,
  38. 0xbf597fc7,
  39. 0xc6e00bf3,
  40. 0xd5a79147,
  41. 0x06ca6351,
  42. 0x14292967,
  43. 0x27b70a85,
  44. 0x2e1b2138,
  45. 0x4d2c6dfc,
  46. 0x53380d13,
  47. 0x650a7354,
  48. 0x766a0abb,
  49. 0x81c2c92e,
  50. 0x92722c85,
  51. 0xa2bfe8a1,
  52. 0xa81a664b,
  53. 0xc24b8b70,
  54. 0xc76c51a3,
  55. 0xd192e819,
  56. 0xd6990624,
  57. 0xf40e3585,
  58. 0x106aa070,
  59. 0x19a4c116,
  60. 0x1e376c08,
  61. 0x2748774c,
  62. 0x34b0bcb5,
  63. 0x391c0cb3,
  64. 0x4ed8aa4a,
  65. 0x5b9cca4f,
  66. 0x682e6ff3,
  67. 0x748f82ee,
  68. 0x78a5636f,
  69. 0x84c87814,
  70. 0x8cc70208,
  71. 0x90befffa,
  72. 0xa4506ceb,
  73. 0xbef9a3f7,
  74. 0xc67178f2,
  75. }
  76. func block(dig *digest, p []byte) {
  77. var w [64]uint32
  78. 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]
  79. for len(p) >= chunk {
  80. // Can interlace the computation of w with the
  81. // rounds below if needed for speed.
  82. for i := 0; i < 16; i++ {
  83. j := i * 4
  84. w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[j+2])<<8 | uint32(p[j+3])
  85. }
  86. for i := 16; i < 64; i++ {
  87. v1 := w[i-2]
  88. t1 := (v1>>17 | v1<<(32-17)) ^ (v1>>19 | v1<<(32-19)) ^ (v1 >> 10)
  89. v2 := w[i-15]
  90. t2 := (v2>>7 | v2<<(32-7)) ^ (v2>>18 | v2<<(32-18)) ^ (v2 >> 3)
  91. w[i] = t1 + w[i-7] + t2 + w[i-16]
  92. }
  93. a, b, c, d, e, f, g, h := h0, h1, h2, h3, h4, h5, h6, h7
  94. for i := 0; i < 64; i++ {
  95. t1 := h + ((e>>6 | e<<(32-6)) ^ (e>>11 | e<<(32-11)) ^ (e>>25 | e<<(32-25))) + ((e & f) ^ (^e & g)) + _K[i] + w[i]
  96. t2 := ((a>>2 | a<<(32-2)) ^ (a>>13 | a<<(32-13)) ^ (a>>22 | a<<(32-22))) + ((a & b) ^ (a & c) ^ (b & c))
  97. h = g
  98. g = f
  99. f = e
  100. e = d + t1
  101. d = c
  102. c = b
  103. b = a
  104. a = t1 + t2
  105. }
  106. h0 += a
  107. h1 += b
  108. h2 += c
  109. h3 += d
  110. h4 += e
  111. h5 += f
  112. h6 += g
  113. h7 += h
  114. p = p[chunk:]
  115. }
  116. 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
  117. }