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