gmsm/cipher/zuc_mur_test.go

139 lines
4.1 KiB
Go
Raw Normal View History

Release v0.34.0 * build(deps): bump github/codeql-action from 3.29.11 to 3.30.0 (#361) Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.29.11 to 3.30.0. - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/3c3833e0f8c1c83d449a7478aa59c036a9165498...2d92b76c45b91eb80fc44c74ce3fce0ee94e8f9d) --- updated-dependencies: - dependency-name: github/codeql-action dependency-version: 3.30.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * build(deps): bump codecov/codecov-action from 5.5.0 to 5.5.1 (#362) Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 5.5.0 to 5.5.1. - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/codecov/codecov-action/compare/fdcc8476540edceab3de004e990f80d881c6cc00...5a1091511ad55cbe89839c7260b706298ca349f7) --- updated-dependencies: - dependency-name: codecov/codecov-action dependency-version: 5.5.1 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * build(deps): bump actions/setup-go from 5.5.0 to 6.0.0 (#363) Bumps [actions/setup-go](https://github.com/actions/setup-go) from 5.5.0 to 6.0.0. - [Release notes](https://github.com/actions/setup-go/releases) - [Commits](https://github.com/actions/setup-go/compare/d35c59abb061a4a6fb18e82ac0862c26744d6ab5...44694675825211faa026b3c33043df3e48a5fa00) --- updated-dependencies: - dependency-name: actions/setup-go dependency-version: 6.0.0 dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * build(deps): bump github/codeql-action from 3.30.0 to 3.30.1 (#364) Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.30.0 to 3.30.1. - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/2d92b76c45b91eb80fc44c74ce3fce0ee94e8f9d...f1f6e5f6af878fb37288ce1c627459e94dbf7d01) --- updated-dependencies: - dependency-name: github/codeql-action dependency-version: 3.30.1 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * build(deps): bump step-security/harden-runner from 2.13.0 to 2.13.1 (#367) Bumps [step-security/harden-runner](https://github.com/step-security/harden-runner) from 2.13.0 to 2.13.1. - [Release notes](https://github.com/step-security/harden-runner/releases) - [Commits](https://github.com/step-security/harden-runner/compare/ec9f2d5744a09debf3a187a3f4f675c53b671911...f4a75cfd619ee5ce8d5b864b0d183aff3c69b55a) --- updated-dependencies: - dependency-name: step-security/harden-runner dependency-version: 2.13.1 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * build(deps): bump github/codeql-action from 3.30.1 to 3.30.2 (#368) Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.30.1 to 3.30.2. - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/f1f6e5f6af878fb37288ce1c627459e94dbf7d01...d3678e237b9c32a6c9bffb3315c335f976f3549f) --- updated-dependencies: - dependency-name: github/codeql-action dependency-version: 3.30.2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * feat(mlkem): initialize mlkem from golang standard library * chore(mlkem): refactoring, reduce alloc times * build(deps): bump github/codeql-action from 3.30.2 to 3.30.3 (#369) Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.30.2 to 3.30.3. - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/d3678e237b9c32a6c9bffb3315c335f976f3549f...192325c86100d080feab897ff886c34abd4c83a3) --- updated-dependencies: - dependency-name: github/codeql-action dependency-version: 3.30.3 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * doc(README): include MLKEM * mldsa: refactor the implementation of key and sign/verify * mldsa,slhdsa: crypto.Signer assertion * fix(slhdsa): GenerateKey slice issue #72 * fix(slhdsa): copy/paste issue * slhdsa: supplements package level document * internal/zuc: eea supports encoding.BinaryMarshaler & encoding.BinaryUnmarshaler interfaces * mlkem: use clear built-in * build(deps): bump github/codeql-action from 3.30.3 to 3.30.4 (#376) Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.30.3 to 3.30.4. - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/192325c86100d080feab897ff886c34abd4c83a3...303c0aef88fc2fe5ff6d63d3b1596bfd83dfa1f9) --- updated-dependencies: - dependency-name: github/codeql-action dependency-version: 3.30.4 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * cipher: initial support gxm & mur modes * cipher: update comments * build(deps): bump github/codeql-action from 3.30.4 to 3.30.5 (#377) Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.30.4 to 3.30.5. - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/303c0aef88fc2fe5ff6d63d3b1596bfd83dfa1f9...3599b3baa15b485a2e49ef411a7a4bb2452e7f93) --- updated-dependencies: - dependency-name: github/codeql-action dependency-version: 3.30.5 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * 增加了DRBG销毁内部状态的方法 (#378) * 增加了DRBG销毁内部状态的方法 * 统一前缀 * 修改随机数长度 * 分组和注释 * 错误函数描述 * zuc: expose methods to support encoding.BinaryMarshaler and encoding.BinaryUnmarshaler * drbg: align comments style * internal/zuc: support fast forward * internal/zuc: supplement comments --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Sun Yimin <emmansun@users.noreply.github.com> Co-authored-by: Guanyu Quan <quanguanyu@qq.com>
2025-09-30 17:57:25 +08:00
// Copyright 2025 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 cipher_test
import (
"bytes"
_cipher "crypto/cipher"
"encoding/hex"
"testing"
"github.com/emmansun/gmsm/cipher"
"github.com/emmansun/gmsm/zuc"
)
var murTestCases = []struct {
iv string
h string
k1 string
k2 string
a string
p string
result string
tagSize int
}{
// GM/T 0001.4 - 2024 Appendix C.3
{
iv: "bb8b76cfe5f0d9335029008b2a3b2b21",
h: "ee767d503bb3d5d1b585f57a0418c673",
k1: "e4b5c1f8578034ce6424f58c675597ac",
k2: "608053f6af9efda562d95dc013bea6b5",
a: "fcdd4cb97995da30efd957194eac4d2a8610470f99c88657f462f68dff7561a5",
p: "5fee5517627f17b22a96caf97b77ec7f667cc47d13c34923be2441300066a6c150b24d66c947ca7b2e708eb62bb352",
result: "cf5594bd30c0da0fb41fa6054e534d0494c9d6c4f132fc85771a473458b09583b825c662bfd82278178a845e281e5415c5d1a78a42c4dcd67db05fa1a640a0",
tagSize: 16,
},
{
iv: "2923be84e16cd6ae529049f1f1bbe9eb",
h: "27bede74018082da87d4e5b69f18bf66",
k1: "32070e0f39b7b692b4673edc3184a48e",
k2: "27636f4414510d62cc15cfe194ec4f6d",
a: "",
p: "",
result: "c0016e0772c9983d0fd9fd8c1b012845",
tagSize: 16,
},
{
iv: "2d2086832cc2fe3fd18cb51d6c5e99a5",
h: "9d6cb51623fd847f2e45d7f52f900db8",
k1: "56131c03e457f6226b5477633b873984",
k2: "a88981534db331a386de3e52fb46029b",
a: "",
p: "ffffffffffffffffffffffffffffff",
result: "234c2d51eaa582da9be3cc3828aa670a7afb7d817efa0777826f1e33a53cf3",
tagSize: 16,
},
{
iv: "b3a6db3c870c3e99245e0d1c06b747de",
h: "6db45e4f9572f4e6fe0d91acda6801d5",
k1: "edbe06afed8075576aad04afdec91d32",
k2: "61d4fca6b2c2bb48b4b1172531333620",
a: "9de18b1fdab0ca9902b9729d492c807ec599d5",
p: "",
result: "8213c29606d02bba10f13ffad1d26a42",
tagSize: 16,
},
{
iv: "b3a6db3c870c3e99245e0d1c06b747de",
h: "6db45e4f9572f4e6fe0d91acda6801d5",
k1: "edbe06afed8075576aad04afdec91d32",
k2: "61d4fca6b2c2bb48b4b1172531333620",
a: "9de18b1fdab0ca9902b9729d492c807ec599d5e980b2eac9cc53bf67d6bf14d67e2ddc8e6683ef574961ff698f61cdd1",
p: "b3124dc843bb8ba61f035a7d0938251f5dd4cbfc96f5453b130d890a1cdbae32",
result: "dabbbe23d8f0ea42e31a9bdd9706a4275d8aacd2cf27c4a4c0d0ba6fb8f31da7a276827b74509357",
tagSize: 8,
},
}
func TestMurSeal(t *testing.T) {
zucCipherCreator := func(key, iv []byte) (_cipher.Stream, error) {
return zuc.NewCipher(key, iv)
}
for i, tc := range murTestCases {
iv, _ := hex.DecodeString(tc.iv)
h, _ := hex.DecodeString(tc.h)
k1, _ := hex.DecodeString(tc.k1)
k2, _ := hex.DecodeString(tc.k2)
a, _ := hex.DecodeString(tc.a)
p, _ := hex.DecodeString(tc.p)
result, _ := hex.DecodeString(tc.result)
g, err := cipher.NewMURWithTagSize(zucCipherCreator, h, tc.tagSize)
if err != nil {
t.Errorf("case %d: NewMURWithTagSize error: %s", i, err)
continue
}
c, err := g.Seal(iv, k1, k2, nil, p, a)
if err != nil {
t.Errorf("case %d: Seal error: %s", i, err)
continue
}
if !bytes.Equal(c, result) {
t.Errorf("case %d: Seal mismatch\ngot: %x\nwant: %x", i, c, result)
continue
}
}
}
func TestMurOpen(t *testing.T) {
zucCipherCreator := func(key, iv []byte) (_cipher.Stream, error) {
return zuc.NewCipher(key, iv)
}
for i, tc := range murTestCases {
iv, _ := hex.DecodeString(tc.iv)
h, _ := hex.DecodeString(tc.h)
k1, _ := hex.DecodeString(tc.k1)
k2, _ := hex.DecodeString(tc.k2)
a, _ := hex.DecodeString(tc.a)
p, _ := hex.DecodeString(tc.p)
result, _ := hex.DecodeString(tc.result)
g, err := cipher.NewMURWithTagSize(zucCipherCreator, h, tc.tagSize)
if err != nil {
t.Errorf("case %d: NewMURWithTagSize error: %s", i, err)
continue
}
out, err := g.Open(iv, k1, k2, nil, result, a)
if err != nil {
t.Errorf("case %d: Open error: %s", i, err)
continue
}
if !bytes.Equal(out, p) {
t.Errorf("case %d: Open mismatch\ngot: %x\nwant: %x", i, out, p)
continue
}
}
}