mirror of
https://github.com/emmansun/gmsm.git
synced 2025-09-16 03:43:50 +08:00
![github-actions[bot]](/assets/img/avatar_default.png)
* 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](3c3833e0f8...2d92b76c45
) --- 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](fdcc847654...5a1091511a
) --- 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](d35c59abb0...4469467582
) --- 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](2d92b76c45...f1f6e5f6af
) --- 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](ec9f2d5744...f4a75cfd61
) --- 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](f1f6e5f6af...d3678e237b
) --- 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](d3678e237b...192325c861
) --- 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 --------- 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>
103 lines
2.6 KiB
Go
103 lines
2.6 KiB
Go
// 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.
|
|
|
|
//go:build go1.24
|
|
|
|
package mldsa
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/sha256"
|
|
"crypto/sha3"
|
|
"crypto/sha512"
|
|
"encoding/asn1"
|
|
"errors"
|
|
"hash"
|
|
|
|
"github.com/emmansun/gmsm/sm3"
|
|
)
|
|
|
|
var (
|
|
// Digest Algorithms
|
|
OIDDigestAlgorithmSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1}
|
|
OIDDigestAlgorithmSHA512 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 3}
|
|
OIDDigestAlgorithmSHA3_256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 8}
|
|
OIDDigestAlgorithmSHA3_384 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 9}
|
|
OIDDigestAlgorithmSHA3_512 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 10}
|
|
OIDDigestAlgorithmSHAKE128 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 11}
|
|
OIDDigestAlgorithmSHAKE256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 12}
|
|
OIDDigestAlgorithmSM3 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 401}
|
|
)
|
|
|
|
var ErrUnsupportedDigestAlgorithm = errors.New("mldsa: unsupported digest algorithm")
|
|
|
|
type xofHashAdapter struct {
|
|
*sha3.SHAKE
|
|
size int
|
|
}
|
|
|
|
func (h *xofHashAdapter) Write(p []byte) (n int, err error) {
|
|
return h.SHAKE.Write(p)
|
|
}
|
|
|
|
func (h *xofHashAdapter) Reset() {
|
|
h.SHAKE.Reset()
|
|
}
|
|
|
|
func (h *xofHashAdapter) Size() int {
|
|
return h.size
|
|
}
|
|
|
|
func (x *xofHashAdapter) BlockSize() int {
|
|
return x.SHAKE.BlockSize()
|
|
}
|
|
|
|
func (x *xofHashAdapter) Sum(b []byte) []byte {
|
|
buf := make([]byte, x.size)
|
|
x.Read(buf)
|
|
return append(b, buf...)
|
|
}
|
|
|
|
func getHashByOID(oid asn1.ObjectIdentifier) (hash.Hash, error) {
|
|
switch {
|
|
case oid.Equal(OIDDigestAlgorithmSHA256):
|
|
return sha256.New(), nil
|
|
case oid.Equal(OIDDigestAlgorithmSHA512):
|
|
return sha512.New(), nil
|
|
case oid.Equal(OIDDigestAlgorithmSHA3_256):
|
|
return sha3.New256(), nil
|
|
case oid.Equal(OIDDigestAlgorithmSHA3_384):
|
|
return sha3.New384(), nil
|
|
case oid.Equal(OIDDigestAlgorithmSHA3_512):
|
|
return sha3.New512(), nil
|
|
case oid.Equal(OIDDigestAlgorithmSHAKE128):
|
|
return &xofHashAdapter{sha3.NewSHAKE128(), 32}, nil
|
|
case oid.Equal(OIDDigestAlgorithmSHAKE256):
|
|
return &xofHashAdapter{sha3.NewSHAKE256(), 64}, nil
|
|
case oid.Equal(OIDDigestAlgorithmSM3):
|
|
return sm3.New(), nil
|
|
default:
|
|
return nil, ErrUnsupportedDigestAlgorithm
|
|
}
|
|
}
|
|
|
|
func preHash(oid asn1.ObjectIdentifier, data []byte) ([]byte, error) {
|
|
h, err := getHashByOID(oid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
h.Write(data)
|
|
oidBytes, _ := asn1.Marshal(oid)
|
|
return h.Sum(oidBytes), nil
|
|
}
|
|
|
|
type Options struct {
|
|
Context []byte
|
|
PrehashOID asn1.ObjectIdentifier
|
|
}
|
|
|
|
func (opts *Options) HashFunc() crypto.Hash {
|
|
return crypto.Hash(0)
|
|
}
|