Files
ssw-mvp/scard/code/common/crypto/sm3.go
T
admin 3247e9fd15 init: ssw-mvp initial commit
- boot/ (Java/Maven, JNPF 7.0) — 移除旧的 boot/.git,源码直接纳入仓库
- web/ (Vue 3 + Vite + Ant Design Vue, pnpm)
- docs/, scard/, nax/
- refs/INDEX.md + refs/国标/(其余 refs/* 开源子仓库不纳入)
- 已忽略: connector_data/, jweb/, log/, web/{dist,node_modules,.pnpm}, boot/*/target/, .DS_Store, .idea, .vscode
2026-08-05 18:29:12 +08:00

207 lines
4.6 KiB
Go

// SM3 cryptographic hash algorithm
// Reference: GB/T 32905-2016, GM/T 0004-2012
//
// This is a conformant implementation: it reproduces the digests published in
// GB/T 32905-2016 Appendix A and therefore interoperates with other SM3 stacks.
package crypto
import (
"encoding/binary"
"hash"
"math/bits"
)
const (
sm3BlockSize = 64
sm3Size = 32
)
// SM3Digest implements the SM3 hash algorithm as a streaming hash.Hash.
type SM3Digest struct {
state [8]uint32
buf [sm3BlockSize]byte
buflen int // number of valid bytes in buf
length uint64 // total number of bytes written
}
// SM3IV is the SM3 initialization vector (GB/T 32905-2016 clause 4.1).
var SM3IV = [8]uint32{
0x7380166f, 0x4914b2b9, 0x172442d7, 0xda8a0600,
0xa96f30bc, 0x163138aa, 0xe38dee4d, 0xb0fb0e4e,
}
// Round constants Tj.
const (
sm3T0 uint32 = 0x79cc4519 // 0 <= j <= 15
sm3T1 uint32 = 0x7a879d8a // 16 <= j <= 63
)
// NewSM3 creates a new SM3 hash.
func NewSM3() hash.Hash {
d := new(SM3Digest)
d.Reset()
return d
}
// SM3 returns the SM3 digest of data.
func SM3(data []byte) []byte {
d := new(SM3Digest)
d.Reset()
d.Write(data)
return d.Sum(nil)
}
func (d *SM3Digest) Reset() {
d.state = SM3IV
d.buflen = 0
d.length = 0
}
func (d *SM3Digest) Size() int { return sm3Size }
func (d *SM3Digest) BlockSize() int { return sm3BlockSize }
func (d *SM3Digest) Write(p []byte) (int, error) {
n := len(p)
d.length += uint64(n)
// Fill the pending partial block first.
if d.buflen > 0 {
c := copy(d.buf[d.buflen:], p)
d.buflen += c
p = p[c:]
if d.buflen == sm3BlockSize {
d.compress(&d.state, d.buf[:])
d.buflen = 0
}
}
// Consume whole blocks straight from the input.
for len(p) >= sm3BlockSize {
d.compress(&d.state, p[:sm3BlockSize])
p = p[sm3BlockSize:]
}
// Stash the remainder.
if len(p) > 0 {
d.buflen = copy(d.buf[:], p)
}
return n, nil
}
// Sum appends the digest to b without mutating the receiver's state, so the
// hash can keep being written to afterwards.
func (d *SM3Digest) Sum(b []byte) []byte {
state := d.state
// Padding: 0x80, then zeros, then the 64-bit big-endian bit length.
var tail [2 * sm3BlockSize]byte
n := copy(tail[:], d.buf[:d.buflen])
tail[n] = 0x80
n++
// The length field must land in the final 8 bytes of a block.
padTo := sm3BlockSize
if n > sm3BlockSize-8 {
padTo = 2 * sm3BlockSize
}
for ; n < padTo-8; n++ {
tail[n] = 0
}
binary.BigEndian.PutUint64(tail[padTo-8:padTo], d.length*8)
for off := 0; off < padTo; off += sm3BlockSize {
d.compress(&state, tail[off:off+sm3BlockSize])
}
var out [sm3Size]byte
for i := 0; i < 8; i++ {
binary.BigEndian.PutUint32(out[i*4:], state[i])
}
return append(b, out[:]...)
}
// compress is the SM3 compression function CF(V, B).
func (d *SM3Digest) compress(v *[8]uint32, block []byte) {
var w [68]uint32
var w1 [64]uint32
// Message expansion.
for i := 0; i < 16; i++ {
w[i] = binary.BigEndian.Uint32(block[i*4:])
}
for j := 16; j < 68; j++ {
w[j] = sm3P1(w[j-16]^w[j-9]^bits.RotateLeft32(w[j-3], 15)) ^
bits.RotateLeft32(w[j-13], 7) ^ w[j-6]
}
for j := 0; j < 64; j++ {
w1[j] = w[j] ^ w[j+4]
}
a, b, c, dd := v[0], v[1], v[2], v[3]
e, f, g, h := v[4], v[5], v[6], v[7]
for j := 0; j < 64; j++ {
t := sm3T0
if j >= 16 {
t = sm3T1
}
a12 := bits.RotateLeft32(a, 12)
ss1 := bits.RotateLeft32(a12+e+bits.RotateLeft32(t, j%32), 7)
ss2 := ss1 ^ a12
var tt1, tt2 uint32
if j < 16 {
tt1 = (a ^ b ^ c) + dd + ss2 + w1[j]
tt2 = (e ^ f ^ g) + h + ss1 + w[j]
} else {
tt1 = ((a & b) | (a & c) | (b & c)) + dd + ss2 + w1[j]
tt2 = ((e & f) | (^e & g)) + h + ss1 + w[j]
}
dd = c
c = bits.RotateLeft32(b, 9)
b = a
a = tt1
h = g
g = bits.RotateLeft32(f, 19)
f = e
e = sm3P0(tt2)
}
// V(i+1) = ABCDEFGH xor V(i)
v[0] ^= a
v[1] ^= b
v[2] ^= c
v[3] ^= dd
v[4] ^= e
v[5] ^= f
v[6] ^= g
v[7] ^= h
}
// sm3P0 is the permutation P0(X) = X xor (X <<< 9) xor (X <<< 17).
func sm3P0(x uint32) uint32 {
return x ^ bits.RotateLeft32(x, 9) ^ bits.RotateLeft32(x, 17)
}
// sm3P1 is the permutation P1(X) = X xor (X <<< 15) xor (X <<< 23).
func sm3P1(x uint32) uint32 {
return x ^ bits.RotateLeft32(x, 15) ^ bits.RotateLeft32(x, 23)
}
// FF0 is the boolean function FFj for 0 <= j <= 15.
func FF0(x, y, z uint32) uint32 { return x ^ y ^ z }
// FF1 is the boolean function FFj for 16 <= j <= 63.
func FF1(x, y, z uint32) uint32 { return (x & y) | (x & z) | (y & z) }
// GG0 is the boolean function GGj for 0 <= j <= 15.
func GG0(x, y, z uint32) uint32 { return x ^ y ^ z }
// GG1 is the boolean function GGj for 16 <= j <= 63.
func GG1(x, y, z uint32) uint32 { return (x & y) | (^x & z) }