加密调整
This commit is contained in:
parent
3ac5a26048
commit
721808e543
|
|
@ -8,10 +8,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type BaseSdk struct {
|
type BaseSdk struct {
|
||||||
|
sm2P256xx *util.Sm2P256Curve
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBaseSdk() SDK {
|
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 {
|
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([]byte{byte(uidLen & 0xFF)})
|
||||||
z.Write(uid)
|
z.Write(uid)
|
||||||
|
|
||||||
sm2P256 := util.NewP256Sm2()
|
//sm2P256 := util.NewP256Sm2()
|
||||||
|
sm2P256 := b.sm2P256xx
|
||||||
|
|
||||||
z.Write(util.BigIntToByte(sm2P256.A))
|
z.Write(util.BigIntToByte(sm2P256.A))
|
||||||
z.Write(util.BigIntToByte(sm2P256.B))
|
z.Write(util.BigIntToByte(sm2P256.B))
|
||||||
|
|
|
||||||
|
|
@ -26,15 +26,21 @@ type Sm2 struct {
|
||||||
data []byte
|
data []byte
|
||||||
err []error
|
err []error
|
||||||
toData []byte
|
toData []byte
|
||||||
|
|
||||||
|
sm2P256xx *util.Sm2P256Curve
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSm2() *Sm2 {
|
func NewSm2() *Sm2 {
|
||||||
|
x := util.NewP256Sm2()
|
||||||
|
|
||||||
return &Sm2{
|
return &Sm2{
|
||||||
sdk: sdk.NewBaseSdk(),
|
sdk: sdk.NewBaseSdk(),
|
||||||
c3Len: 32,
|
c3Len: 32,
|
||||||
uid: model.DefaultUid,
|
uid: model.DefaultUid,
|
||||||
cipherType: model.C1C2C3,
|
cipherType: model.C1C2C3,
|
||||||
err: make([]error, 0),
|
err: make([]error, 0),
|
||||||
|
|
||||||
|
sm2P256xx: &x,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,8 +48,10 @@ func (s *Sm2) Encrypt() *Sm2 {
|
||||||
if err := s.encryptErrHandler(); err != nil {
|
if err := s.encryptErrHandler(); err != nil {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
c2 := make([]byte, len(s.data))
|
c2 := make([]byte, len(s.data))
|
||||||
copy(c2, s.data)
|
copy(c2, s.data)
|
||||||
|
|
||||||
var c1 []byte
|
var c1 []byte
|
||||||
var kx, ky *big.Int
|
var kx, ky *big.Int
|
||||||
for {
|
for {
|
||||||
|
|
@ -247,7 +255,7 @@ func (s *Sm2) SetHexPublicKey(hexStr string) *Sm2 {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return s.SetError(fmt.Errorf("publicKey is not hex string: %s", err.Error()))
|
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 {
|
if err != nil {
|
||||||
return s.SetError(fmt.Errorf("parse publicKey err: %+v", err))
|
return s.SetError(fmt.Errorf("parse publicKey err: %+v", err))
|
||||||
}
|
}
|
||||||
|
|
@ -259,7 +267,7 @@ func (s *Sm2) SetHexPrivateKey(hexStr string) *Sm2 {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return s.SetError(fmt.Errorf("privateKey is not hex string: %s", err.Error()))
|
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 {
|
if err != nil {
|
||||||
return s.SetError(fmt.Errorf("parse privateKey err: %+v", err))
|
return s.SetError(fmt.Errorf("parse privateKey err: %+v", err))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type sm2P256Curve struct {
|
type Sm2P256Curve struct {
|
||||||
RInverse *big.Int
|
RInverse *big.Int
|
||||||
*elliptic.CurveParams
|
*elliptic.CurveParams
|
||||||
a, b, gx, gy sm2P256FieldElement
|
a, b, gx, gy sm2P256FieldElement
|
||||||
|
|
@ -14,7 +14,7 @@ type sm2P256Curve struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var initOnce sync.Once
|
var initOnce sync.Once
|
||||||
var sm2P256 sm2P256Curve
|
var sm2P256 Sm2P256Curve
|
||||||
|
|
||||||
type sm2P256FieldElement [9]uint32
|
type sm2P256FieldElement [9]uint32
|
||||||
type sm2P256LargeFieldElement [17]uint64
|
type sm2P256LargeFieldElement [17]uint64
|
||||||
|
|
@ -24,7 +24,7 @@ const (
|
||||||
bottom29Bits = 0x1FFFFFFF
|
bottom29Bits = 0x1FFFFFFF
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewP256Sm2() sm2P256Curve {
|
func NewP256Sm2() Sm2P256Curve {
|
||||||
initOnce.Do(initP256Sm2)
|
initOnce.Do(initP256Sm2)
|
||||||
return sm2P256
|
return sm2P256
|
||||||
}
|
}
|
||||||
|
|
@ -46,12 +46,12 @@ func initP256Sm2() {
|
||||||
sm2P256FromBig(&sm2P256.b, sm2P256.B)
|
sm2P256FromBig(&sm2P256.b, sm2P256.B)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (curve sm2P256Curve) Params() *elliptic.CurveParams {
|
func (curve Sm2P256Curve) Params() *elliptic.CurveParams {
|
||||||
return sm2P256.CurveParams
|
return sm2P256.CurveParams
|
||||||
}
|
}
|
||||||
|
|
||||||
// y^2 = x^3 + ax + b
|
// 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
|
var a, x, y, y2, x3 sm2P256FieldElement
|
||||||
|
|
||||||
sm2P256FromBig(&x, X)
|
sm2P256FromBig(&x, X)
|
||||||
|
|
@ -75,7 +75,7 @@ func zForAffine(x, y *big.Int) *big.Int {
|
||||||
return z
|
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
|
var X1, Y1, Z1, X2, Y2, Z2, X3, Y3, Z3 sm2P256FieldElement
|
||||||
|
|
||||||
z1 := zForAffine(x1, y1)
|
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)
|
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
|
var X1, Y1, Z1 sm2P256FieldElement
|
||||||
|
|
||||||
z1 := zForAffine(x1, y1)
|
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)
|
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
|
var X, Y, Z, X1, Y1 sm2P256FieldElement
|
||||||
sm2P256FromBig(&X1, x1)
|
sm2P256FromBig(&X1, x1)
|
||||||
sm2P256FromBig(&Y1, y1)
|
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)
|
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 scalarReversed [32]byte
|
||||||
var X, Y, Z sm2P256FieldElement
|
var X, Y, Z sm2P256FieldElement
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,25 +38,30 @@ func JoinBytes(params ...[]byte) ([]byte, error) {
|
||||||
return buffer.Bytes(), nil
|
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)
|
k := new(big.Int).SetBytes(d)
|
||||||
|
|
||||||
c := NewP256Sm2()
|
c := NewP256Sm2()
|
||||||
params := c.Params()
|
|
||||||
|
//params := c.Params()
|
||||||
n := new(big.Int).Sub(params.N, model.One)
|
n := new(big.Int).Sub(params.N, model.One)
|
||||||
if k.Cmp(n) >= 0 {
|
if k.Cmp(n) >= 0 {
|
||||||
return nil, fmt.Errorf("privateKey is overflow")
|
return nil, fmt.Errorf("privateKey is overflow")
|
||||||
}
|
}
|
||||||
|
|
||||||
pri := &model.PrivateKey{
|
pri := &model.PrivateKey{
|
||||||
PublicKey: &model.PublicKey{},
|
PublicKey: &model.PublicKey{},
|
||||||
D: nil,
|
D: nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
pri.PublicKey.Curve = c
|
pri.PublicKey.Curve = c
|
||||||
pri.D = k
|
pri.D = k
|
||||||
pri.PublicKey.X, pri.PublicKey.Y = c.ScalarBaseMult(k.Bytes())
|
pri.PublicKey.X, pri.PublicKey.Y = c.ScalarBaseMult(k.Bytes())
|
||||||
|
|
||||||
return pri, nil
|
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) {
|
if len(d) == 65 && d[0] == byte(0x04) {
|
||||||
d = d[1:]
|
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))
|
return nil, fmt.Errorf("publicKey is not 64 bytes: %d", len(d))
|
||||||
}
|
}
|
||||||
pub := new(model.PublicKey)
|
pub := new(model.PublicKey)
|
||||||
pub.Curve = NewP256Sm2()
|
|
||||||
|
//pub.Curve = NewP256Sm2()
|
||||||
|
pub.Curve = curve
|
||||||
|
|
||||||
pub.X = new(big.Int).SetBytes(d[:32])
|
pub.X = new(big.Int).SetBytes(d[:32])
|
||||||
pub.Y = new(big.Int).SetBytes(d[32:])
|
pub.Y = new(big.Int).SetBytes(d[32:])
|
||||||
return pub, nil
|
return pub, nil
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue