From 721808e543df3f0e67bd8fb905c58280fe0a9263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Mon, 24 Mar 2025 18:24:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=AF=86=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/pkg/cmb/sm2/sdk/base.go | 7 +++++-- internal/pkg/cmb/sm2/sm2.go | 12 ++++++++++-- internal/pkg/cmb/sm2/util/p256.go | 18 +++++++++--------- internal/pkg/cmb/sm2/util/util.go | 16 ++++++++++++---- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/internal/pkg/cmb/sm2/sdk/base.go b/internal/pkg/cmb/sm2/sdk/base.go index c0d6313..828594e 100644 --- a/internal/pkg/cmb/sm2/sdk/base.go +++ b/internal/pkg/cmb/sm2/sdk/base.go @@ -8,10 +8,12 @@ import ( ) type BaseSdk struct { + sm2P256xx *util.Sm2P256Curve } func NewBaseSdk() SDK { - return &BaseSdk{} + sm2P256 := util.NewP256Sm2() + return &BaseSdk{sm2P256xx: &sm2P256} } func (b *BaseSdk) Kdf(c elliptic.Curve, x, y *big.Int, c2 []byte) error { @@ -59,7 +61,8 @@ func (b *BaseSdk) GetZ(x *big.Int, y *big.Int, uid []byte) []byte { z.Write([]byte{byte(uidLen & 0xFF)}) z.Write(uid) - sm2P256 := util.NewP256Sm2() + //sm2P256 := util.NewP256Sm2() + sm2P256 := b.sm2P256xx z.Write(util.BigIntToByte(sm2P256.A)) z.Write(util.BigIntToByte(sm2P256.B)) diff --git a/internal/pkg/cmb/sm2/sm2.go b/internal/pkg/cmb/sm2/sm2.go index 961c636..52d6285 100644 --- a/internal/pkg/cmb/sm2/sm2.go +++ b/internal/pkg/cmb/sm2/sm2.go @@ -26,15 +26,21 @@ type Sm2 struct { data []byte err []error toData []byte + + sm2P256xx *util.Sm2P256Curve } func NewSm2() *Sm2 { + x := util.NewP256Sm2() + return &Sm2{ sdk: sdk.NewBaseSdk(), c3Len: 32, uid: model.DefaultUid, cipherType: model.C1C2C3, err: make([]error, 0), + + sm2P256xx: &x, } } @@ -42,8 +48,10 @@ func (s *Sm2) Encrypt() *Sm2 { if err := s.encryptErrHandler(); err != nil { return s } + c2 := make([]byte, len(s.data)) copy(c2, s.data) + var c1 []byte var kx, ky *big.Int for { @@ -247,7 +255,7 @@ func (s *Sm2) SetHexPublicKey(hexStr string) *Sm2 { if err != nil { return s.SetError(fmt.Errorf("publicKey is not hex string: %s", err.Error())) } - s.publicKey, err = util.HexToPublicKey(d) + s.publicKey, err = util.HexToPublicKey(s.sm2P256xx, d) if err != nil { return s.SetError(fmt.Errorf("parse publicKey err: %+v", err)) } @@ -259,7 +267,7 @@ func (s *Sm2) SetHexPrivateKey(hexStr string) *Sm2 { if err != nil { return s.SetError(fmt.Errorf("privateKey is not hex string: %s", err.Error())) } - s.privateKey, err = util.HexToPrivateKey(d) + s.privateKey, err = util.HexToPrivateKey(s.sm2P256xx, d) if err != nil { return s.SetError(fmt.Errorf("parse privateKey err: %+v", err)) } diff --git a/internal/pkg/cmb/sm2/util/p256.go b/internal/pkg/cmb/sm2/util/p256.go index cfc4a24..d0cb0d9 100644 --- a/internal/pkg/cmb/sm2/util/p256.go +++ b/internal/pkg/cmb/sm2/util/p256.go @@ -6,7 +6,7 @@ import ( "sync" ) -type sm2P256Curve struct { +type Sm2P256Curve struct { RInverse *big.Int *elliptic.CurveParams a, b, gx, gy sm2P256FieldElement @@ -14,7 +14,7 @@ type sm2P256Curve struct { } var initOnce sync.Once -var sm2P256 sm2P256Curve +var sm2P256 Sm2P256Curve type sm2P256FieldElement [9]uint32 type sm2P256LargeFieldElement [17]uint64 @@ -24,7 +24,7 @@ const ( bottom29Bits = 0x1FFFFFFF ) -func NewP256Sm2() sm2P256Curve { +func NewP256Sm2() Sm2P256Curve { initOnce.Do(initP256Sm2) return sm2P256 } @@ -46,12 +46,12 @@ func initP256Sm2() { sm2P256FromBig(&sm2P256.b, sm2P256.B) } -func (curve sm2P256Curve) Params() *elliptic.CurveParams { +func (curve Sm2P256Curve) Params() *elliptic.CurveParams { return sm2P256.CurveParams } // y^2 = x^3 + ax + b -func (curve sm2P256Curve) IsOnCurve(X, Y *big.Int) bool { +func (curve Sm2P256Curve) IsOnCurve(X, Y *big.Int) bool { var a, x, y, y2, x3 sm2P256FieldElement sm2P256FromBig(&x, X) @@ -75,7 +75,7 @@ func zForAffine(x, y *big.Int) *big.Int { return z } -func (curve sm2P256Curve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) { +func (curve Sm2P256Curve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) { var X1, Y1, Z1, X2, Y2, Z2, X3, Y3, Z3 sm2P256FieldElement z1 := zForAffine(x1, y1) @@ -90,7 +90,7 @@ func (curve sm2P256Curve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) { return sm2P256ToAffine(&X3, &Y3, &Z3) } -func (curve sm2P256Curve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) { +func (curve Sm2P256Curve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) { var X1, Y1, Z1 sm2P256FieldElement z1 := zForAffine(x1, y1) @@ -101,7 +101,7 @@ func (curve sm2P256Curve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) { return sm2P256ToAffine(&X1, &Y1, &Z1) } -func (curve sm2P256Curve) ScalarMult(x1, y1 *big.Int, k []byte) (*big.Int, *big.Int) { +func (curve Sm2P256Curve) ScalarMult(x1, y1 *big.Int, k []byte) (*big.Int, *big.Int) { var X, Y, Z, X1, Y1 sm2P256FieldElement sm2P256FromBig(&X1, x1) sm2P256FromBig(&Y1, y1) @@ -111,7 +111,7 @@ func (curve sm2P256Curve) ScalarMult(x1, y1 *big.Int, k []byte) (*big.Int, *big. return sm2P256ToAffine(&X, &Y, &Z) } -func (curve sm2P256Curve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) { +func (curve Sm2P256Curve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) { var scalarReversed [32]byte var X, Y, Z sm2P256FieldElement diff --git a/internal/pkg/cmb/sm2/util/util.go b/internal/pkg/cmb/sm2/util/util.go index c5a4572..32254f9 100644 --- a/internal/pkg/cmb/sm2/util/util.go +++ b/internal/pkg/cmb/sm2/util/util.go @@ -38,25 +38,30 @@ func JoinBytes(params ...[]byte) ([]byte, error) { return buffer.Bytes(), nil } -func HexToPrivateKey(d []byte) (*model.PrivateKey, error) { +func HexToPrivateKey(params *Sm2P256Curve, d []byte) (*model.PrivateKey, error) { k := new(big.Int).SetBytes(d) + c := NewP256Sm2() - params := c.Params() + + //params := c.Params() n := new(big.Int).Sub(params.N, model.One) if k.Cmp(n) >= 0 { return nil, fmt.Errorf("privateKey is overflow") } + pri := &model.PrivateKey{ PublicKey: &model.PublicKey{}, D: nil, } + pri.PublicKey.Curve = c pri.D = k pri.PublicKey.X, pri.PublicKey.Y = c.ScalarBaseMult(k.Bytes()) + return pri, nil } -func HexToPublicKey(d []byte) (*model.PublicKey, error) { +func HexToPublicKey(curve *Sm2P256Curve, d []byte) (*model.PublicKey, error) { if len(d) == 65 && d[0] == byte(0x04) { d = d[1:] } @@ -64,7 +69,10 @@ func HexToPublicKey(d []byte) (*model.PublicKey, error) { return nil, fmt.Errorf("publicKey is not 64 bytes: %d", len(d)) } pub := new(model.PublicKey) - pub.Curve = NewP256Sm2() + + //pub.Curve = NewP256Sm2() + pub.Curve = curve + pub.X = new(big.Int).SetBytes(d[:32]) pub.Y = new(big.Int).SetBytes(d[32:]) return pub, nil