sm3.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. Copyright Suzhou Tongji Fintech Research Institute 2017 All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package sm3
  14. import (
  15. "encoding/binary"
  16. "hash"
  17. )
  18. type SM3 struct {
  19. digest [8]uint32 // digest represents the partial evaluation of V
  20. length uint64 // length of the message
  21. unhandleMsg []byte // uint8 //
  22. }
  23. func (sm3 *SM3) ff0(x, y, z uint32) uint32 { return x ^ y ^ z }
  24. func (sm3 *SM3) ff1(x, y, z uint32) uint32 { return (x & y) | (x & z) | (y & z) }
  25. func (sm3 *SM3) gg0(x, y, z uint32) uint32 { return x ^ y ^ z }
  26. func (sm3 *SM3) gg1(x, y, z uint32) uint32 { return (x & y) | (^x & z) }
  27. func (sm3 *SM3) p0(x uint32) uint32 { return x ^ sm3.leftRotate(x, 9) ^ sm3.leftRotate(x, 17) }
  28. func (sm3 *SM3) p1(x uint32) uint32 { return x ^ sm3.leftRotate(x, 15) ^ sm3.leftRotate(x, 23) }
  29. func (sm3 *SM3) leftRotate(x uint32, i uint32) uint32 { return (x<<(i%32) | x>>(32-i%32)) }
  30. func (sm3 *SM3) pad() []byte {
  31. msg := sm3.unhandleMsg
  32. msg = append(msg, 0x80) // Append '1'
  33. blockSize := 64 // Append until the resulting message length (in bits) is congruent to 448 (mod 512)
  34. for len(msg)%blockSize != 56 {
  35. msg = append(msg, 0x00)
  36. }
  37. // append message length
  38. msg = append(msg, uint8(sm3.length>>56&0xff))
  39. msg = append(msg, uint8(sm3.length>>48&0xff))
  40. msg = append(msg, uint8(sm3.length>>40&0xff))
  41. msg = append(msg, uint8(sm3.length>>32&0xff))
  42. msg = append(msg, uint8(sm3.length>>24&0xff))
  43. msg = append(msg, uint8(sm3.length>>16&0xff))
  44. msg = append(msg, uint8(sm3.length>>8&0xff))
  45. msg = append(msg, uint8(sm3.length>>0&0xff))
  46. if len(msg)%64 != 0 {
  47. panic("------SM3 Pad: error msgLen =")
  48. }
  49. return msg
  50. }
  51. func (sm3 *SM3) update(msg []byte, nblocks int) {
  52. var w [68]uint32
  53. var w1 [64]uint32
  54. a, b, c, d, e, f, g, h := sm3.digest[0], sm3.digest[1], sm3.digest[2], sm3.digest[3], sm3.digest[4], sm3.digest[5], sm3.digest[6], sm3.digest[7]
  55. for len(msg) >= 64 {
  56. for i := 0; i < 16; i++ {
  57. w[i] = binary.BigEndian.Uint32(msg[4*i : 4*(i+1)])
  58. }
  59. for i := 16; i < 68; i++ {
  60. w[i] = sm3.p1(w[i-16]^w[i-9]^sm3.leftRotate(w[i-3], 15)) ^ sm3.leftRotate(w[i-13], 7) ^ w[i-6]
  61. }
  62. for i := 0; i < 64; i++ {
  63. w1[i] = w[i] ^ w[i+4]
  64. }
  65. A, B, C, D, E, F, G, H := a, b, c, d, e, f, g, h
  66. for i := 0; i < 16; i++ {
  67. SS1 := sm3.leftRotate(sm3.leftRotate(A, 12)+E+sm3.leftRotate(0x79cc4519, uint32(i)), 7)
  68. SS2 := SS1 ^ sm3.leftRotate(A, 12)
  69. TT1 := sm3.ff0(A, B, C) + D + SS2 + w1[i]
  70. TT2 := sm3.gg0(E, F, G) + H + SS1 + w[i]
  71. D = C
  72. C = sm3.leftRotate(B, 9)
  73. B = A
  74. A = TT1
  75. H = G
  76. G = sm3.leftRotate(F, 19)
  77. F = E
  78. E = sm3.p0(TT2)
  79. }
  80. for i := 16; i < 64; i++ {
  81. SS1 := sm3.leftRotate(sm3.leftRotate(A, 12)+E+sm3.leftRotate(0x7a879d8a, uint32(i)), 7)
  82. SS2 := SS1 ^ sm3.leftRotate(A, 12)
  83. TT1 := sm3.ff1(A, B, C) + D + SS2 + w1[i]
  84. TT2 := sm3.gg1(E, F, G) + H + SS1 + w[i]
  85. D = C
  86. C = sm3.leftRotate(B, 9)
  87. B = A
  88. A = TT1
  89. H = G
  90. G = sm3.leftRotate(F, 19)
  91. F = E
  92. E = sm3.p0(TT2)
  93. }
  94. a ^= A
  95. b ^= B
  96. c ^= C
  97. d ^= D
  98. e ^= E
  99. f ^= F
  100. g ^= G
  101. h ^= H
  102. msg = msg[64:]
  103. }
  104. sm3.digest[0], sm3.digest[1], sm3.digest[2], sm3.digest[3], sm3.digest[4], sm3.digest[5], sm3.digest[6], sm3.digest[7] = a, b, c, d, e, f, g, h
  105. }
  106. func (sm3 *SM3) update2(msg []byte, nblocks int)([8]uint32){
  107. var w [68]uint32
  108. var w1 [64]uint32
  109. a, b, c, d, e, f, g, h := sm3.digest[0], sm3.digest[1], sm3.digest[2], sm3.digest[3], sm3.digest[4], sm3.digest[5], sm3.digest[6], sm3.digest[7]
  110. for len(msg) >= 64 {
  111. for i := 0; i < 16; i++ {
  112. w[i] = binary.BigEndian.Uint32(msg[4*i : 4*(i+1)])
  113. }
  114. for i := 16; i < 68; i++ {
  115. w[i] = sm3.p1(w[i-16]^w[i-9]^sm3.leftRotate(w[i-3], 15)) ^ sm3.leftRotate(w[i-13], 7) ^ w[i-6]
  116. }
  117. for i := 0; i < 64; i++ {
  118. w1[i] = w[i] ^ w[i+4]
  119. }
  120. A, B, C, D, E, F, G, H := a, b, c, d, e, f, g, h
  121. for i := 0; i < 16; i++ {
  122. SS1 := sm3.leftRotate(sm3.leftRotate(A, 12)+E+sm3.leftRotate(0x79cc4519, uint32(i)), 7)
  123. SS2 := SS1 ^ sm3.leftRotate(A, 12)
  124. TT1 := sm3.ff0(A, B, C) + D + SS2 + w1[i]
  125. TT2 := sm3.gg0(E, F, G) + H + SS1 + w[i]
  126. D = C
  127. C = sm3.leftRotate(B, 9)
  128. B = A
  129. A = TT1
  130. H = G
  131. G = sm3.leftRotate(F, 19)
  132. F = E
  133. E = sm3.p0(TT2)
  134. }
  135. for i := 16; i < 64; i++ {
  136. SS1 := sm3.leftRotate(sm3.leftRotate(A, 12)+E+sm3.leftRotate(0x7a879d8a, uint32(i)), 7)
  137. SS2 := SS1 ^ sm3.leftRotate(A, 12)
  138. TT1 := sm3.ff1(A, B, C) + D + SS2 + w1[i]
  139. TT2 := sm3.gg1(E, F, G) + H + SS1 + w[i]
  140. D = C
  141. C = sm3.leftRotate(B, 9)
  142. B = A
  143. A = TT1
  144. H = G
  145. G = sm3.leftRotate(F, 19)
  146. F = E
  147. E = sm3.p0(TT2)
  148. }
  149. a ^= A
  150. b ^= B
  151. c ^= C
  152. d ^= D
  153. e ^= E
  154. f ^= F
  155. g ^= G
  156. h ^= H
  157. msg = msg[64:]
  158. }
  159. var digest [8]uint32
  160. digest[0], digest[1], digest[2], digest[3], digest[4], digest[5], digest[6], digest[7] = a, b, c, d, e, f, g, h
  161. return digest
  162. }
  163. func New() hash.Hash {
  164. var sm3 SM3
  165. sm3.Reset()
  166. return &sm3
  167. }
  168. // BlockSize, required by the hash.Hash interface.
  169. // BlockSize returns the hash's underlying block size.
  170. // The Write method must be able to accept any amount
  171. // of data, but it may operate more efficiently if all writes
  172. // are a multiple of the block size.
  173. func (sm3 *SM3) BlockSize() int { return 64 }
  174. // Size, required by the hash.Hash interface.
  175. // Size returns the number of bytes Sum will return.
  176. func (sm3 *SM3) Size() int { return 32 }
  177. // Reset clears the internal state by zeroing bytes in the state buffer.
  178. // This can be skipped for a newly-created hash state; the default zero-allocated state is correct.
  179. func (sm3 *SM3) Reset() {
  180. // Reset digest
  181. sm3.digest[0] = 0x7380166f
  182. sm3.digest[1] = 0x4914b2b9
  183. sm3.digest[2] = 0x172442d7
  184. sm3.digest[3] = 0xda8a0600
  185. sm3.digest[4] = 0xa96f30bc
  186. sm3.digest[5] = 0x163138aa
  187. sm3.digest[6] = 0xe38dee4d
  188. sm3.digest[7] = 0xb0fb0e4e
  189. sm3.length = 0 // Reset numberic states
  190. sm3.unhandleMsg = []byte{}
  191. }
  192. // Write, required by the hash.Hash interface.
  193. // Write (via the embedded io.Writer interface) adds more data to the running hash.
  194. // It never returns an error.
  195. func (sm3 *SM3) Write(p []byte) (int, error) {
  196. toWrite := len(p)
  197. sm3.length += uint64(len(p) * 8)
  198. msg := append(sm3.unhandleMsg, p...)
  199. nblocks := len(msg) / sm3.BlockSize()
  200. sm3.update(msg, nblocks)
  201. // Update unhandleMsg
  202. sm3.unhandleMsg = msg[nblocks*sm3.BlockSize():]
  203. return toWrite, nil
  204. }
  205. // Sum, required by the hash.Hash interface.
  206. // Sum appends the current hash to b and returns the resulting slice.
  207. // It does not change the underlying hash state.
  208. func (sm3 *SM3) Sum(in []byte) []byte {
  209. sm3.Write(in)
  210. msg := sm3.pad()
  211. //Finialize
  212. digest:=sm3.update2(msg, len(msg)/sm3.BlockSize())
  213. // save hash to in
  214. needed := sm3.Size()
  215. if cap(in)-len(in) < needed {
  216. newIn := make([]byte, len(in), len(in)+needed)
  217. copy(newIn, in)
  218. in = newIn
  219. }
  220. out := in[len(in) : len(in)+needed]
  221. for i := 0; i < 8; i++ {
  222. binary.BigEndian.PutUint32(out[i*4:], digest[i])
  223. }
  224. return out
  225. }
  226. func Sm3Sum(data []byte) []byte {
  227. var sm3 SM3
  228. sm3.Reset()
  229. sm3.Write(data)
  230. return sm3.Sum(nil)
  231. }