-
Notifications
You must be signed in to change notification settings - Fork 0
/
sm3block.go
100 lines (83 loc) · 1.96 KB
/
sm3block.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
/*
* Package sm3 implements the Chinese SM3 Digest Algorithm,
* according to "go/src/crypto/sha256"
* author: weizhang <d5c5ceb0@gmail.com>
* 2017.02.24
*/
package sm3
func block(dig *digest, p []byte) {
blockGeneric(dig, p)
}
func blockGeneric(dig *digest, p []byte) {
var w [68]uint32
var w1 [64]uint32
var ss1, ss2, tt1, tt2 uint32
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]
for len(p) >= chunk {
for i := 0; i < 16; i++ {
j := i * 4
w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[j+2])<<8 | uint32(p[j+3])
}
for i := 16; i < 68; i++ {
w[i] = sm3_p1(w[i-16]^w[i-9]^sm3_rotl(w[i-3], 15)) ^ sm3_rotl(w[i-13], 7) ^ w[i-6]
}
for i := 0; i < 64; i++ {
w1[i] = w[i] ^ w[i+4]
}
a, b, c, d, e, f, g, h := h0, h1, h2, h3, h4, h5, h6, h7
for j := 0; j < 64; j++ {
ss1 = sm3_rotl(sm3_rotl(a, 12)+e+sm3_rotl(sm3_t(j), uint32(j)), 7)
ss2 = ss1 ^ sm3_rotl(a, 12)
tt1 = sm3_ff(a, b, c, j) + d + ss2 + w1[j]
tt2 = sm3_gg(e, f, g, j) + h + ss1 + w[j]
d = c
c = sm3_rotl(b, 9)
b = a
a = tt1
h = g
g = sm3_rotl(f, 19)
f = e
e = sm3_p0(tt2)
}
h0 ^= a
h1 ^= b
h2 ^= c
h3 ^= d
h4 ^= e
h5 ^= f
h6 ^= g
h7 ^= h
p = p[chunk:]
}
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
}
func sm3_t(j int) uint32 {
if j >= 16 {
return 0x7A879D8A
} else {
return 0x79CC4519
}
}
func sm3_ff(x, y, z uint32, j int) uint32 {
if j >= 16 {
return ((x | y) & (x | z) & (y | z))
} else {
return x ^ y ^ z
}
}
func sm3_gg(x, y, z uint32, j int) uint32 {
if j >= 16 {
return ((x & y) | ((^x) & z))
} else {
return x ^ y ^ z
}
}
func sm3_rotl(x, n uint32) uint32 {
return (x << (n % 32)) | (x >> (32 - (n % 32)))
}
func sm3_p0(x uint32) uint32 {
return x ^ sm3_rotl(x, 9) ^ sm3_rotl(x, 17)
}
func sm3_p1(x uint32) uint32 {
return x ^ sm3_rotl(x, 15) ^ sm3_rotl(x, 23)
}