mirror of
https://github.com/emmansun/gmsm.git
synced 2026-11-23 12:51:59 +08:00
test(sm3): add hash and KDF semantics vectors
Reference SM3 written from GB/T 32905-2016, self-checked against the abc and abcd x16 digests before emitting. Thirteen hash cases cover the padding boundaries at 55, 56, 63, 64, 65, 119, 120, 127 and 128 bytes; eleven KDF cases cover keyLen 0, 1, 31, 32, 33, 63, 64, 65, 96 and 129 plus an empty Z. The consumer requires one-shot and three-chunk streaming to agree, so incremental and one-shot hashing are pinned to the same digest. The case schema gains BitLen, KeyLen, HID and the ZUC frame fields (Count, Bearer, Direction) used by the suites added in this phase.
This commit is contained in:
1 parent
f1fb733f55
commit
1810897919
5 files changed
+585
No files matched your search
@@ -0,0 +1,254 @@
|
||||
// Copyright 2026 Sun Yimin. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package sm3vectors builds the SM3 hash and KDF vector suite.
|
||||
//
|
||||
// Every expected value is produced by the reference implementation below,
|
||||
// which is written from GB/T 32905-2016 rather than from the package under
|
||||
// test. The reference is validated against the published "abc" and
|
||||
// sixteen-"abcd" digests before a single case is emitted, so the suite pins
|
||||
// the library to the standard instead of to itself.
|
||||
//
|
||||
// The hash cases straddle the padding boundary: a message of 55 bytes still
|
||||
// fits the length field into the same block, 56 bytes forces an extra block,
|
||||
// and 63/64/65 and 119/120 straddle the same boundary one and two blocks
|
||||
// later. The KDF cases cover the output lengths around the 32-byte digest
|
||||
// size, where an off-by-one in the counter loop shows up as an extra or a
|
||||
// missing block.
|
||||
package sm3vectors
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/emmansun/gmsm/internal/cryptotest/vectors"
|
||||
)
|
||||
|
||||
const fileName = "sm3_semantics.json"
|
||||
|
||||
var (
|
||||
refIV = [8]uint32{
|
||||
0x7380166f, 0x4914b2b9, 0x172442d7, 0xda8a0600,
|
||||
0xa96f30bc, 0x163138aa, 0xe38dee4d, 0xb0fb0e4e,
|
||||
}
|
||||
// Published digests used to validate the reference before it is used.
|
||||
abcDigest = "66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0"
|
||||
// GB/T 32905-2016 A.2: "abcd" repeated sixteen times.
|
||||
abcd16Digest = "debe9ff92275b8a138604889c18e5a4d6fdb70e5387e5765293dcba39c0c5732"
|
||||
)
|
||||
|
||||
func rotl(x uint32, n uint) uint32 { return x<<n | x>>(32-n) }
|
||||
|
||||
func p0(x uint32) uint32 { return x ^ rotl(x, 9) ^ rotl(x, 17) }
|
||||
|
||||
func p1(x uint32) uint32 { return x ^ rotl(x, 15) ^ rotl(x, 23) }
|
||||
|
||||
// compress applies the message expansion and the 64-round compression
|
||||
// function of GB/T 32905-2016 5.3.3 to one 64-byte block.
|
||||
func compress(v *[8]uint32, block []byte) {
|
||||
var w [68]uint32
|
||||
var w1 [64]uint32
|
||||
for i := 0; i < 16; i++ {
|
||||
w[i] = binary.BigEndian.Uint32(block[i*4:])
|
||||
}
|
||||
for j := 16; j < 68; j++ {
|
||||
w[j] = p1(w[j-16]^w[j-9]^rotl(w[j-3], 15)) ^ rotl(w[j-13], 7) ^ w[j-6]
|
||||
}
|
||||
for j := 0; j < 64; j++ {
|
||||
w1[j] = w[j] ^ w[j+4]
|
||||
}
|
||||
|
||||
a, b, c, d, e, f, g, h := v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]
|
||||
for j := 0; j < 64; j++ {
|
||||
var ff, gg, tj uint32
|
||||
if j < 16 {
|
||||
ff = a ^ b ^ c
|
||||
gg = e ^ f ^ g
|
||||
tj = 0x79cc4519
|
||||
} else {
|
||||
ff = (a & b) | (a & c) | (b & c)
|
||||
gg = (e & f) | (^e & g)
|
||||
tj = 0x7a879d8a
|
||||
}
|
||||
ss1 := rotl(rotl(a, 12)+e+rotl(tj, uint(j%32)), 7)
|
||||
ss2 := ss1 ^ rotl(a, 12)
|
||||
tt1 := ff + d + ss2 + w1[j]
|
||||
tt2 := gg + h + ss1 + w[j]
|
||||
d, c, b, a = c, rotl(b, 9), a, tt1
|
||||
h, g, f, e = g, rotl(f, 19), e, p0(tt2)
|
||||
}
|
||||
v[0] ^= a
|
||||
v[1] ^= b
|
||||
v[2] ^= c
|
||||
v[3] ^= d
|
||||
v[4] ^= e
|
||||
v[5] ^= f
|
||||
v[6] ^= g
|
||||
v[7] ^= h
|
||||
}
|
||||
|
||||
// sm3Ref is the reference digest. It is independent of the package under
|
||||
// test: the padding, the message expansion and the compression function are
|
||||
// transcribed from the standard.
|
||||
func sm3Ref(msg []byte) [32]byte {
|
||||
padded := make([]byte, 0, len(msg)+72)
|
||||
padded = append(padded, msg...)
|
||||
padded = append(padded, 0x80)
|
||||
for len(padded)%64 != 56 {
|
||||
padded = append(padded, 0)
|
||||
}
|
||||
padded = binary.BigEndian.AppendUint64(padded, uint64(len(msg))*8)
|
||||
|
||||
v := refIV
|
||||
for i := 0; i < len(padded); i += 64 {
|
||||
compress(&v, padded[i:i+64])
|
||||
}
|
||||
var out [32]byte
|
||||
for i := range 8 {
|
||||
binary.BigEndian.PutUint32(out[i*4:], v[i])
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// kdfRef derives outLen bytes the way GB/T 32918.4-2016 5.4.3 defines the
|
||||
// SM3-based KDF: the concatenation of SM3(z || counter), counter a 32-bit
|
||||
// big-endian integer starting at one, truncated to outLen.
|
||||
func kdfRef(z []byte, outLen int) []byte {
|
||||
var out []byte
|
||||
for counter := uint32(1); len(out) < outLen; counter++ {
|
||||
block := make([]byte, 0, len(z)+4)
|
||||
block = append(block, z...)
|
||||
block = binary.BigEndian.AppendUint32(block, counter)
|
||||
digest := sm3Ref(block)
|
||||
out = append(out, digest[:]...)
|
||||
}
|
||||
return out[:outLen]
|
||||
}
|
||||
|
||||
func seq(n int) []byte {
|
||||
out := make([]byte, n)
|
||||
for i := range out {
|
||||
out[i] = byte(i)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func hexStr(b []byte) string { return hex.EncodeToString(b) }
|
||||
|
||||
// buildHash pins the digest of messages that sit on the padding boundary.
|
||||
func buildHash() []vectors.Case {
|
||||
messages := []struct {
|
||||
tcID int
|
||||
comment string
|
||||
msg []byte
|
||||
}{
|
||||
{1, "GB/T 32905-2016 A.1: abc", []byte("abc")},
|
||||
{2, "empty message", nil},
|
||||
{3, "one byte", seq(1)},
|
||||
{4, "three bytes", seq(3)},
|
||||
{5, "55 bytes: the length field still fits the first block", seq(55)},
|
||||
{6, "56 bytes: the length field forces an extra block", seq(56)},
|
||||
{7, "63 bytes", seq(63)},
|
||||
{8, "64 bytes: one full block", seq(64)},
|
||||
{9, "65 bytes", seq(65)},
|
||||
{10, "119 bytes", seq(119)},
|
||||
{11, "120 bytes", seq(120)},
|
||||
{12, "127 bytes", seq(127)},
|
||||
{13, "128 bytes: two full blocks", seq(128)},
|
||||
}
|
||||
cases := make([]vectors.Case, 0, len(messages))
|
||||
for _, m := range messages {
|
||||
digest := sm3Ref(m.msg)
|
||||
cases = append(cases, vectors.Case{
|
||||
TCID: m.tcID,
|
||||
Comment: m.comment,
|
||||
Msg: hexStr(m.msg),
|
||||
Expected: hexStr(digest[:]),
|
||||
})
|
||||
}
|
||||
return cases
|
||||
}
|
||||
|
||||
// buildKDF pins the KDF output lengths around the digest size. A zero length
|
||||
// must return an empty key, not one block; a length of 33 must return two
|
||||
// blocks truncated to one byte past the first block.
|
||||
func buildKDF() []vectors.Case {
|
||||
z := seq(80)
|
||||
lengths := []struct {
|
||||
tcID int
|
||||
comment string
|
||||
z []byte
|
||||
keyLen int
|
||||
}{
|
||||
{1, "zero-length output", z, 0},
|
||||
{2, "one byte", z, 1},
|
||||
{3, "one byte short of a block", z, 31},
|
||||
{4, "exactly one block", z, 32},
|
||||
{5, "one byte into the second block", z, 33},
|
||||
{6, "one byte short of two blocks", z, 63},
|
||||
{7, "exactly two blocks", z, 64},
|
||||
{8, "one byte into the third block", z, 65},
|
||||
{9, "exactly three blocks", z, 96},
|
||||
{10, "four blocks plus one byte", z, 129},
|
||||
{11, "empty shared secret", nil, 32},
|
||||
}
|
||||
cases := make([]vectors.Case, 0, len(lengths))
|
||||
for _, c := range lengths {
|
||||
cases = append(cases, vectors.Case{
|
||||
TCID: c.tcID,
|
||||
Comment: c.comment,
|
||||
Input: hexStr(c.z),
|
||||
KeyLen: c.keyLen,
|
||||
Expected: hexStr(kdfRef(c.z, c.keyLen)),
|
||||
})
|
||||
}
|
||||
return cases
|
||||
}
|
||||
|
||||
// BuildSuite builds the SM3 suite.
|
||||
func BuildSuite() (*vectors.Suite, error) {
|
||||
if got := sm3Ref([]byte("abc")); hexStr(got[:]) != abcDigest {
|
||||
return nil, fmt.Errorf("reference SM3 disagrees with GB/T 32905-2016 A.1: got %x, want %s", got, abcDigest)
|
||||
}
|
||||
abcd16 := make([]byte, 0, 64)
|
||||
for range 16 {
|
||||
abcd16 = append(abcd16, "abcd"...)
|
||||
}
|
||||
if got := sm3Ref(abcd16); hexStr(got[:]) != abcd16Digest {
|
||||
return nil, fmt.Errorf("reference SM3 disagrees with GB/T 32905-2016 A.2: got %x, want %s", got, abcd16Digest)
|
||||
}
|
||||
|
||||
suite := &vectors.Suite{
|
||||
Schema: vectors.SemanticsSchema,
|
||||
Algorithm: "sm3",
|
||||
Generator: "go test ./internal/cryptotest/sm3vectors -run TestVectorsUpToDate -update",
|
||||
Groups: []vectors.Group{
|
||||
{
|
||||
Type: "SM3Hash",
|
||||
Comment: "SM3 digests at and around the 64-byte block and 56-byte length-field boundaries. The expected values come from an independent implementation written from GB/T 32905-2016.",
|
||||
Tests: buildHash(),
|
||||
},
|
||||
{
|
||||
Type: "SM3KDF",
|
||||
Comment: "SM3-based KDF output lengths around the 32-byte digest size, plus the zero-length output and an empty shared secret. The expected values come from the same independent SM3.",
|
||||
Tests: buildKDF(),
|
||||
},
|
||||
},
|
||||
}
|
||||
if err := suite.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return suite, nil
|
||||
}
|
||||
|
||||
// MarshalSuite renders a suite the way the committed files are stored.
|
||||
func MarshalSuite(s *vectors.Suite) ([]byte, error) {
|
||||
data, err := json.MarshalIndent(s, "", " ")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return append(data, '\n'), nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// Copyright 2026 Sun Yimin. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package sm3vectors
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var update = flag.Bool("update", false, "rewrite the committed vector files")
|
||||
|
||||
// TestVectorsUpToDate pins the committed vector file to the deterministic
|
||||
// output of the builders. Run `go test ./internal/cryptotest/sm3vectors -run
|
||||
// TestVectorsUpToDate -update` after changing a case so the regenerated
|
||||
// vectors are reviewed like code.
|
||||
func TestVectorsUpToDate(t *testing.T) {
|
||||
suite, err := BuildSuite()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want, err := MarshalSuite(suite)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join("..", "vectors", "testdata", fileName)
|
||||
|
||||
if *update {
|
||||
if err := os.WriteFile(path, want, 0o644); err != nil {
|
||||
t.Fatalf("writing %s: %v", path, err)
|
||||
}
|
||||
t.Logf("updated %s", path)
|
||||
return
|
||||
}
|
||||
|
||||
got, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("reading %s (run with -update to create it): %v", path, err)
|
||||
}
|
||||
if !bytes.Equal(got, want) {
|
||||
t.Fatalf("%s is stale: run `go test ./internal/cryptotest/sm3vectors -run TestVectorsUpToDate -update` and review the diff", path)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSuiteLoads runs the same validation the consumer test relies on.
|
||||
func TestSuiteLoads(t *testing.T) {
|
||||
suite, err := BuildSuite()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := suite.Group("SM3Hash"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := suite.Group("SM3KDF"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := suite.Validate(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
{
|
||||
"schema": "gmsm-semantics-vectors/v1",
|
||||
"algorithm": "sm3",
|
||||
"generator": "go test ./internal/cryptotest/sm3vectors -run TestVectorsUpToDate -update",
|
||||
"testGroups": [
|
||||
{
|
||||
"type": "SM3Hash",
|
||||
"comment": "SM3 digests at and around the 64-byte block and 56-byte length-field boundaries. The expected values come from an independent implementation written from GB/T 32905-2016.",
|
||||
"tests": [
|
||||
{
|
||||
"tcId": 1,
|
||||
"comment": "GB/T 32905-2016 A.1: abc",
|
||||
"expected": "66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0",
|
||||
"msg": "616263"
|
||||
},
|
||||
{
|
||||
"tcId": 2,
|
||||
"comment": "empty message",
|
||||
"expected": "1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b"
|
||||
},
|
||||
{
|
||||
"tcId": 3,
|
||||
"comment": "one byte",
|
||||
"expected": "2daef60e7a0b8f5e024c81cd2ab3109f2b4f155cf83adeb2ae5532f74a157fdf",
|
||||
"msg": "00"
|
||||
},
|
||||
{
|
||||
"tcId": 4,
|
||||
"comment": "three bytes",
|
||||
"expected": "d18c17ef9f985ba361f06b599a1ec3cae9bd5de6967cac33944e5f8b67b6dcc2",
|
||||
"msg": "000102"
|
||||
},
|
||||
{
|
||||
"tcId": 5,
|
||||
"comment": "55 bytes: the length field still fits the first block",
|
||||
"expected": "a79cf9dcee3404abf7f769698201647fd9d3ff61d629d0f58bb4b5579a427db8",
|
||||
"msg": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f30313233343536"
|
||||
},
|
||||
{
|
||||
"tcId": 6,
|
||||
"comment": "56 bytes: the length field forces an extra block",
|
||||
"expected": "62f7363b15f4de76dd925c493b9d6d00d4ba0ef2a1f334c1d0f13b293aeb40d1",
|
||||
"msg": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637"
|
||||
},
|
||||
{
|
||||
"tcId": 7,
|
||||
"comment": "63 bytes",
|
||||
"expected": "6165e4cbb15cde01c6226e0015a47f710f8f8e1f2c296700033bb34d9212109c",
|
||||
"msg": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e"
|
||||
},
|
||||
{
|
||||
"tcId": 8,
|
||||
"comment": "64 bytes: one full block",
|
||||
"expected": "93566f236d157aae078d1ddb5cebdbba1520b5142e22a8915564345ba2ae1d63",
|
||||
"msg": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"
|
||||
},
|
||||
{
|
||||
"tcId": 9,
|
||||
"comment": "65 bytes",
|
||||
"expected": "c886e6814be748285a10b28ae62ddacd85db830cd2cf3a2bfa2f729c15f63618",
|
||||
"msg": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f40"
|
||||
},
|
||||
{
|
||||
"tcId": 10,
|
||||
"comment": "119 bytes",
|
||||
"expected": "8f3ea392a89a7119982d6634660db1a95f35d68267a2235e3255998a857f4fbf",
|
||||
"msg": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f70717273747576"
|
||||
},
|
||||
{
|
||||
"tcId": 11,
|
||||
"comment": "120 bytes",
|
||||
"expected": "6babee35e6a1515af9d6255109c24f3c08897829422c6225d235fd4c8527e9ec",
|
||||
"msg": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f7071727374757677"
|
||||
},
|
||||
{
|
||||
"tcId": 12,
|
||||
"comment": "127 bytes",
|
||||
"expected": "bca3436d828517a6a6893a9e309e06e7b7b29c6e3f78b4814b23efe149962980",
|
||||
"msg": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e"
|
||||
},
|
||||
{
|
||||
"tcId": 13,
|
||||
"comment": "128 bytes: two full blocks",
|
||||
"expected": "a9e7985473ca09df1510d83b572f72375430756c4a661b00724afeb8b75dd0a5",
|
||||
"msg": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "SM3KDF",
|
||||
"comment": "SM3-based KDF output lengths around the 32-byte digest size, plus the zero-length output and an empty shared secret. The expected values come from the same independent SM3.",
|
||||
"tests": [
|
||||
{
|
||||
"tcId": 1,
|
||||
"comment": "zero-length output",
|
||||
"input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f"
|
||||
},
|
||||
{
|
||||
"tcId": 2,
|
||||
"comment": "one byte",
|
||||
"expected": "5d",
|
||||
"input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
|
||||
"keyLen": 1
|
||||
},
|
||||
{
|
||||
"tcId": 3,
|
||||
"comment": "one byte short of a block",
|
||||
"expected": "5de130c11a304b490118246859ef35c30b9283b22cb017a8af1bad372d4938",
|
||||
"input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
|
||||
"keyLen": 31
|
||||
},
|
||||
{
|
||||
"tcId": 4,
|
||||
"comment": "exactly one block",
|
||||
"expected": "5de130c11a304b490118246859ef35c30b9283b22cb017a8af1bad372d49383a",
|
||||
"input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
|
||||
"keyLen": 32
|
||||
},
|
||||
{
|
||||
"tcId": 5,
|
||||
"comment": "one byte into the second block",
|
||||
"expected": "5de130c11a304b490118246859ef35c30b9283b22cb017a8af1bad372d49383a87",
|
||||
"input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
|
||||
"keyLen": 33
|
||||
},
|
||||
{
|
||||
"tcId": 6,
|
||||
"comment": "one byte short of two blocks",
|
||||
"expected": "5de130c11a304b490118246859ef35c30b9283b22cb017a8af1bad372d49383a87f985ac3f0cdf4146d900bb4ff17215f057f2ecf15f18d9a57de1ff24651a",
|
||||
"input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
|
||||
"keyLen": 63
|
||||
},
|
||||
{
|
||||
"tcId": 7,
|
||||
"comment": "exactly two blocks",
|
||||
"expected": "5de130c11a304b490118246859ef35c30b9283b22cb017a8af1bad372d49383a87f985ac3f0cdf4146d900bb4ff17215f057f2ecf15f18d9a57de1ff24651a73",
|
||||
"input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
|
||||
"keyLen": 64
|
||||
},
|
||||
{
|
||||
"tcId": 8,
|
||||
"comment": "one byte into the third block",
|
||||
"expected": "5de130c11a304b490118246859ef35c30b9283b22cb017a8af1bad372d49383a87f985ac3f0cdf4146d900bb4ff17215f057f2ecf15f18d9a57de1ff24651a73b6",
|
||||
"input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
|
||||
"keyLen": 65
|
||||
},
|
||||
{
|
||||
"tcId": 9,
|
||||
"comment": "exactly three blocks",
|
||||
"expected": "5de130c11a304b490118246859ef35c30b9283b22cb017a8af1bad372d49383a87f985ac3f0cdf4146d900bb4ff17215f057f2ecf15f18d9a57de1ff24651a73b64f13a44553a1a7d54e98a9621bd067236a8c53a0bf8104c3759931c4eda377",
|
||||
"input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
|
||||
"keyLen": 96
|
||||
},
|
||||
{
|
||||
"tcId": 10,
|
||||
"comment": "four blocks plus one byte",
|
||||
"expected": "5de130c11a304b490118246859ef35c30b9283b22cb017a8af1bad372d49383a87f985ac3f0cdf4146d900bb4ff17215f057f2ecf15f18d9a57de1ff24651a73b64f13a44553a1a7d54e98a9621bd067236a8c53a0bf8104c3759931c4eda377b55b0d251ad0872b15061ab9ea77751f76571110b3282b58c5863b98638cea39f3",
|
||||
"input": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
|
||||
"keyLen": 129
|
||||
},
|
||||
{
|
||||
"tcId": 11,
|
||||
"comment": "empty shared secret",
|
||||
"expected": "88c0cffa4c713446a03f1fff1630aa6353bdb53e2a9272146be7a82fde06afa3",
|
||||
"keyLen": 32
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -84,6 +84,7 @@ type Case struct {
|
||||
// knows.
|
||||
Pub string `json:"pub,omitempty"` // SEC 1 point, 04||X||Y
|
||||
UID string `json:"uid,omitempty"` // empty means the library default
|
||||
HID int `json:"hid,omitempty"` // SM9 hierarchy identifier
|
||||
Msg string `json:"msg,omitempty"`
|
||||
Hash string `json:"hash,omitempty"` // precomputed e for Verify
|
||||
R string `json:"r,omitempty"`
|
||||
@@ -107,6 +108,19 @@ type Case struct {
|
||||
TagSize int `json:"tagSize,omitempty"`
|
||||
NonceSize int `json:"nonceSize,omitempty"`
|
||||
Sector uint64 `json:"sector,omitempty"`
|
||||
// BitLen is the number of message bits for bit-oriented MACs; zero
|
||||
// means the whole Input buffer.
|
||||
BitLen int `json:"bitLen,omitempty"`
|
||||
// KeyLen is the requested output length of a key derivation function.
|
||||
KeyLen int `json:"keyLen,omitempty"`
|
||||
|
||||
// Count, Bearer and Direction are the ZUC frame parameters. Count is
|
||||
// the 32-bit frame counter, Bearer the 5-bit bearer identity and
|
||||
// Direction the 1-bit transmission direction flag; Nonce carries the
|
||||
// frame IV derived from them.
|
||||
Count uint32 `json:"count,omitempty"`
|
||||
Bearer uint32 `json:"bearer,omitempty"`
|
||||
Direction uint32 `json:"direction,omitempty"`
|
||||
}
|
||||
|
||||
// Load unmarshals a committed vector file by name, e.g.
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
// Copyright 2026 Sun Yimin. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package sm3
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/emmansun/gmsm/internal/cryptotest/vectors"
|
||||
)
|
||||
|
||||
// TestSemanticsVectors replays the committed SM3 hash and KDF vectors. The
|
||||
// hash cases cover the padding boundary, and each one is also fed to the
|
||||
// hash in three uneven chunks: a streaming implementation that mishandles a
|
||||
// buffered block boundary must disagree with the one-shot digest.
|
||||
func TestSemanticsVectors(t *testing.T) {
|
||||
suite, err := vectors.Load("sm3_semantics.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Run("SM3Hash", func(t *testing.T) { testHashVectors(t, suite) })
|
||||
t.Run("SM3KDF", func(t *testing.T) { testKDFVectors(t, suite) })
|
||||
}
|
||||
|
||||
func testHashVectors(t *testing.T, suite *vectors.Suite) {
|
||||
group, err := suite.Group("SM3Hash")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range group.Tests {
|
||||
t.Run(fmt.Sprintf("tc%d", tc.TCID), func(t *testing.T) {
|
||||
msg := semanticsHex(t, "msg", tc.Msg)
|
||||
want := semanticsHex(t, "expected", tc.Expected)
|
||||
|
||||
got := Sum(msg)
|
||||
if !bytes.Equal(got[:], want) {
|
||||
t.Fatalf("Sum() = %x, want %x: %s", got, want, tc.Comment)
|
||||
}
|
||||
|
||||
h := New()
|
||||
cut1, cut2 := len(msg)/3, 2*len(msg)/3
|
||||
h.Write(msg[:cut1])
|
||||
h.Write(msg[cut1:cut2])
|
||||
h.Write(msg[cut2:])
|
||||
if streamed := h.Sum(nil); !bytes.Equal(streamed, want) {
|
||||
t.Fatalf("streamed Sum() = %x, want %x: %s", streamed, want, tc.Comment)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func testKDFVectors(t *testing.T, suite *vectors.Suite) {
|
||||
group, err := suite.Group("SM3KDF")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range group.Tests {
|
||||
t.Run(fmt.Sprintf("tc%d", tc.TCID), func(t *testing.T) {
|
||||
z := semanticsHex(t, "input", tc.Input)
|
||||
want := semanticsHex(t, "expected", tc.Expected)
|
||||
got := Kdf(z, tc.KeyLen)
|
||||
if !bytes.Equal(got, want) {
|
||||
t.Fatalf("Kdf(z, %d) = %x, want %x: %s", tc.KeyLen, got, want, tc.Comment)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func semanticsHex(t *testing.T, name, value string) []byte {
|
||||
t.Helper()
|
||||
if value == "" {
|
||||
return nil
|
||||
}
|
||||
data, err := hex.DecodeString(value)
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", name, err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
Reference in new issue
Block a user