1
0

sm3.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 New() hash.Hash {
  107. var sm3 SM3
  108. sm3.Reset()
  109. return &sm3
  110. }
  111. // BlockSize, required by the hash.Hash interface.
  112. // BlockSize returns the hash's underlying block size.
  113. // The Write method must be able to accept any amount
  114. // of data, but it may operate more efficiently if all writes
  115. // are a multiple of the block size.
  116. func (sm3 *SM3) BlockSize() int { return 64 }
  117. // Size, required by the hash.Hash interface.
  118. // Size returns the number of bytes Sum will return.
  119. func (sm3 *SM3) Size() int { return 32 }
  120. // Reset clears the internal state by zeroing bytes in the state buffer.
  121. // This can be skipped for a newly-created hash state; the default zero-allocated state is correct.
  122. func (sm3 *SM3) Reset() {
  123. // Reset digest
  124. sm3.digest[0] = 0x7380166f
  125. sm3.digest[1] = 0x4914b2b9
  126. sm3.digest[2] = 0x172442d7
  127. sm3.digest[3] = 0xda8a0600
  128. sm3.digest[4] = 0xa96f30bc
  129. sm3.digest[5] = 0x163138aa
  130. sm3.digest[6] = 0xe38dee4d
  131. sm3.digest[7] = 0xb0fb0e4e
  132. sm3.length = 0 // Reset numberic states
  133. sm3.unhandleMsg = []byte{}
  134. }
  135. // Write, required by the hash.Hash interface.
  136. // Write (via the embedded io.Writer interface) adds more data to the running hash.
  137. // It never returns an error.
  138. func (sm3 *SM3) Write(p []byte) (int, error) {
  139. toWrite := len(p)
  140. sm3.length += uint64(len(p) * 8)
  141. msg := append(sm3.unhandleMsg, p...)
  142. nblocks := len(msg) / sm3.BlockSize()
  143. sm3.update(msg, nblocks)
  144. // Update unhandleMsg
  145. sm3.unhandleMsg = msg[nblocks*sm3.BlockSize():]
  146. return toWrite, nil
  147. }
  148. // Sum, required by the hash.Hash interface.
  149. // Sum appends the current hash to b and returns the resulting slice.
  150. // It does not change the underlying hash state.
  151. func (sm3 *SM3) Sum(in []byte) []byte {
  152. sm3.Write(in)
  153. msg := sm3.pad()
  154. // Finialize
  155. sm3.update(msg, len(msg)/sm3.BlockSize())
  156. // save hash to in
  157. needed := sm3.Size()
  158. if cap(in)-len(in) < needed {
  159. newIn := make([]byte, len(in), len(in)+needed)
  160. copy(newIn, in)
  161. in = newIn
  162. }
  163. out := in[len(in) : len(in)+needed]
  164. for i := 0; i < 8; i++ {
  165. binary.BigEndian.PutUint32(out[i*4:], sm3.digest[i])
  166. }
  167. return out
  168. }
  169. func Sm3Sum(data []byte) []byte {
  170. var sm3 SM3
  171. sm3.Reset()
  172. sm3.Write(data)
  173. return sm3.Sum(nil)
  174. }