mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-27 20:56:18 +08:00
sm9: get around assignment copies lock value to issue
This commit is contained in:
parent
66eae26312
commit
a1b44f1a64
@ -16,8 +16,8 @@ import (
|
|||||||
|
|
||||||
// SignMasterPrivateKey master private key for sign, generated by KGC
|
// SignMasterPrivateKey master private key for sign, generated by KGC
|
||||||
type SignMasterPrivateKey struct {
|
type SignMasterPrivateKey struct {
|
||||||
SignMasterPublicKey // master public key
|
*SignMasterPublicKey // master public key
|
||||||
D *big.Int // master private key
|
D *big.Int // master private key
|
||||||
}
|
}
|
||||||
|
|
||||||
// SignMasterPublicKey master public key for sign, generated by KGC
|
// SignMasterPublicKey master public key for sign, generated by KGC
|
||||||
@ -31,14 +31,14 @@ type SignMasterPublicKey struct {
|
|||||||
|
|
||||||
// SignPrivateKey user private key for sign, generated by KGC
|
// SignPrivateKey user private key for sign, generated by KGC
|
||||||
type SignPrivateKey struct {
|
type SignPrivateKey struct {
|
||||||
PrivateKey *bn256.G1 // user private key
|
PrivateKey *bn256.G1 // user private key
|
||||||
SignMasterPublicKey // master public key
|
*SignMasterPublicKey // master public key
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncryptMasterPrivateKey master private key for encryption, generated by KGC
|
// EncryptMasterPrivateKey master private key for encryption, generated by KGC
|
||||||
type EncryptMasterPrivateKey struct {
|
type EncryptMasterPrivateKey struct {
|
||||||
EncryptMasterPublicKey // master public key
|
*EncryptMasterPublicKey // master public key
|
||||||
D *big.Int // master private key
|
D *big.Int // master private key
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncryptMasterPublicKey master private key for encryption, generated by KGC
|
// EncryptMasterPublicKey master private key for encryption, generated by KGC
|
||||||
@ -52,8 +52,8 @@ type EncryptMasterPublicKey struct {
|
|||||||
|
|
||||||
// EncryptPrivateKey user private key for encryption, generated by KGC
|
// EncryptPrivateKey user private key for encryption, generated by KGC
|
||||||
type EncryptPrivateKey struct {
|
type EncryptPrivateKey struct {
|
||||||
PrivateKey *bn256.G2 // user private key
|
PrivateKey *bn256.G2 // user private key
|
||||||
EncryptMasterPublicKey // master public key
|
*EncryptMasterPublicKey // master public key
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateSignMasterKey generates a master public and private key pair for DSA usage.
|
// GenerateSignMasterKey generates a master public and private key pair for DSA usage.
|
||||||
@ -70,6 +70,7 @@ func GenerateSignMasterKey(rand io.Reader) (*SignMasterPrivateKey, error) {
|
|||||||
|
|
||||||
priv := new(SignMasterPrivateKey)
|
priv := new(SignMasterPrivateKey)
|
||||||
priv.D = new(big.Int).SetBytes(kBytes)
|
priv.D = new(big.Int).SetBytes(kBytes)
|
||||||
|
priv.SignMasterPublicKey = new(SignMasterPublicKey)
|
||||||
priv.MasterPublicKey = p
|
priv.MasterPublicKey = p
|
||||||
return priv, nil
|
return priv, nil
|
||||||
}
|
}
|
||||||
@ -102,6 +103,7 @@ func (master *SignMasterPrivateKey) UnmarshalASN1(der []byte) error {
|
|||||||
return errors.New("sm9: invalid sign master private key asn1 data")
|
return errors.New("sm9: invalid sign master private key asn1 data")
|
||||||
}
|
}
|
||||||
master.D = d
|
master.D = d
|
||||||
|
master.SignMasterPublicKey = new(SignMasterPublicKey)
|
||||||
p, err := new(bn256.G2).ScalarBaseMult(bn256.NormalizeScalar(d.Bytes()))
|
p, err := new(bn256.G2).ScalarBaseMult(bn256.NormalizeScalar(d.Bytes()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -144,7 +146,7 @@ func (master *SignMasterPrivateKey) GenerateUserKey(uid []byte, hid byte) (*Sign
|
|||||||
|
|
||||||
// Public returns the public key corresponding to priv.
|
// Public returns the public key corresponding to priv.
|
||||||
func (master *SignMasterPrivateKey) Public() *SignMasterPublicKey {
|
func (master *SignMasterPrivateKey) Public() *SignMasterPublicKey {
|
||||||
return &master.SignMasterPublicKey
|
return master.SignMasterPublicKey
|
||||||
}
|
}
|
||||||
|
|
||||||
// pair generate the basepoint once
|
// pair generate the basepoint once
|
||||||
@ -257,13 +259,13 @@ func (pub *SignMasterPublicKey) ParseFromPEM(data []byte) error {
|
|||||||
|
|
||||||
// MasterPublic returns the master public key corresponding to priv.
|
// MasterPublic returns the master public key corresponding to priv.
|
||||||
func (priv *SignPrivateKey) MasterPublic() *SignMasterPublicKey {
|
func (priv *SignPrivateKey) MasterPublic() *SignMasterPublicKey {
|
||||||
return &priv.SignMasterPublicKey
|
return priv.SignMasterPublicKey
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMasterPublicKey bind the sign master public key to it.
|
// SetMasterPublicKey bind the sign master public key to it.
|
||||||
func (priv *SignPrivateKey) SetMasterPublicKey(pub *SignMasterPublicKey) {
|
func (priv *SignPrivateKey) SetMasterPublicKey(pub *SignMasterPublicKey) {
|
||||||
if priv.SignMasterPublicKey.MasterPublicKey == nil {
|
if priv.SignMasterPublicKey.MasterPublicKey == nil {
|
||||||
priv.SignMasterPublicKey = *pub
|
priv.SignMasterPublicKey = pub
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -357,6 +359,7 @@ func GenerateEncryptMasterKey(rand io.Reader) (*EncryptMasterPrivateKey, error)
|
|||||||
|
|
||||||
priv := new(EncryptMasterPrivateKey)
|
priv := new(EncryptMasterPrivateKey)
|
||||||
priv.D = new(big.Int).SetBytes(kBytes)
|
priv.D = new(big.Int).SetBytes(kBytes)
|
||||||
|
priv.EncryptMasterPublicKey = new(EncryptMasterPublicKey)
|
||||||
p, err := new(bn256.G1).ScalarBaseMult(kBytes)
|
p, err := new(bn256.G1).ScalarBaseMult(kBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -399,7 +402,7 @@ func (master *EncryptMasterPrivateKey) GenerateUserKey(uid []byte, hid byte) (*E
|
|||||||
|
|
||||||
// Public returns the public key corresponding to priv.
|
// Public returns the public key corresponding to priv.
|
||||||
func (master *EncryptMasterPrivateKey) Public() *EncryptMasterPublicKey {
|
func (master *EncryptMasterPrivateKey) Public() *EncryptMasterPublicKey {
|
||||||
return &master.EncryptMasterPublicKey
|
return master.EncryptMasterPublicKey
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalASN1 marshal encrypt master private key to asn.1 format data according
|
// MarshalASN1 marshal encrypt master private key to asn.1 format data according
|
||||||
@ -430,6 +433,7 @@ func (master *EncryptMasterPrivateKey) UnmarshalASN1(der []byte) error {
|
|||||||
return errors.New("sm9: invalid encrypt master private key asn1 data")
|
return errors.New("sm9: invalid encrypt master private key asn1 data")
|
||||||
}
|
}
|
||||||
master.D = d
|
master.D = d
|
||||||
|
master.EncryptMasterPublicKey = new(EncryptMasterPublicKey)
|
||||||
p, err := new(bn256.G1).ScalarBaseMult(bn256.NormalizeScalar(d.Bytes()))
|
p, err := new(bn256.G1).ScalarBaseMult(bn256.NormalizeScalar(d.Bytes()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -529,13 +533,13 @@ func (pub *EncryptMasterPublicKey) UnmarshalASN1(der []byte) error {
|
|||||||
|
|
||||||
// MasterPublic returns the master public key corresponding to priv.
|
// MasterPublic returns the master public key corresponding to priv.
|
||||||
func (priv *EncryptPrivateKey) MasterPublic() *EncryptMasterPublicKey {
|
func (priv *EncryptPrivateKey) MasterPublic() *EncryptMasterPublicKey {
|
||||||
return &priv.EncryptMasterPublicKey
|
return priv.EncryptMasterPublicKey
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMasterPublicKey bind the encrypt master public key to it.
|
// SetMasterPublicKey bind the encrypt master public key to it.
|
||||||
func (priv *EncryptPrivateKey) SetMasterPublicKey(pub *EncryptMasterPublicKey) {
|
func (priv *EncryptPrivateKey) SetMasterPublicKey(pub *EncryptMasterPublicKey) {
|
||||||
if priv.EncryptMasterPublicKey.MasterPublicKey == nil {
|
if priv.EncryptMasterPublicKey.MasterPublicKey == nil {
|
||||||
priv.EncryptMasterPublicKey = *pub
|
priv.EncryptMasterPublicKey = pub
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user