sm9: add back SetMasterPublic methods

This commit is contained in:
Sun Yimin 2025-08-29 10:34:47 +08:00 committed by GitHub
parent 157e35a0c3
commit 3e4f64ef4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 0 deletions

View File

@ -235,6 +235,13 @@ func (priv *SignPrivateKey) MasterPublic() *SignMasterPublicKey {
return &SignMasterPublicKey{internal: masterKey, publicKey: masterKey.Bytes()}
}
// SetMasterPublic sets the master public key for the SignPrivateKey.
// The caller should ensure that the provided master public key is valid and corresponds
// to the SignPrivateKey. This method is NOT safe for concurrent use.
func (priv *SignPrivateKey) SetMasterPublic(master *SignMasterPublicKey) {
priv.internal.SetMasterPublicKey(master.internal)
}
// MarshalASN1 marshal signature private key to asn.1 format data according
// SM9 cryptographic algorithm application specification
func (priv *SignPrivateKey) MarshalASN1() ([]byte, error) {
@ -463,6 +470,13 @@ func (priv *EncryptPrivateKey) MasterPublic() *EncryptMasterPublicKey {
return &EncryptMasterPublicKey{publicKey: master.Bytes(), internal: master}
}
// SetMasterPublic sets the master public key for the EncryptPrivateKey.
// The caller should ensure that the provided master public key is valid and corresponds
// to the EncryptPrivateKey. This method is NOT safe for concurrent use.
func (priv *EncryptPrivateKey) SetMasterPublic(master *EncryptMasterPublicKey) {
priv.internal.SetMasterPublicKey(master.internal)
}
// MarshalASN1 marshal encryption private key to asn.1 format data according
// SM9 cryptographic algorithm application specification
func (priv *EncryptPrivateKey) MarshalASN1() ([]byte, error) {

View File

@ -21,6 +21,13 @@ func TestSignASN1(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// Test Marshal and Unmarshal
userKeyBytes := userKey.Bytes()
userKey, err = sm9.UnmarshalSignPrivateKeyRaw(userKeyBytes)
if err != nil {
t.Fatal(err)
}
userKey.SetMasterPublic(masterKey.PublicKey())
sig, err := userKey.Sign(rand.Reader, hashed, nil)
if err != nil {
t.Fatal(err)
@ -103,6 +110,13 @@ func TestEncryptDecrypt(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// Test Marshal and Unmarshal
userKeyBytes := userKey.Bytes()
userKey, err = sm9.UnmarshalEncryptPrivateKeyRaw(userKeyBytes)
if err != nil {
t.Fatal(err)
}
userKey.SetMasterPublic(masterKey.PublicKey())
encTypes := []sm9.EncrypterOpts{
sm9.DefaultEncrypterOpts, sm9.SM4ECBEncrypterOpts, sm9.SM4CBCEncrypterOpts, sm9.SM4CFBEncrypterOpts, sm9.SM4OFBEncrypterOpts,
}