Skip to content

Commit 3e4f64e

Browse files
authored
sm9: add back SetMasterPublic methods
1 parent 157e35a commit 3e4f64e

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

sm9/sm9_key.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,13 @@ func (priv *SignPrivateKey) MasterPublic() *SignMasterPublicKey {
235235
return &SignMasterPublicKey{internal: masterKey, publicKey: masterKey.Bytes()}
236236
}
237237

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

473+
// SetMasterPublic sets the master public key for the EncryptPrivateKey.
474+
// The caller should ensure that the provided master public key is valid and corresponds
475+
// to the EncryptPrivateKey. This method is NOT safe for concurrent use.
476+
func (priv *EncryptPrivateKey) SetMasterPublic(master *EncryptMasterPublicKey) {
477+
priv.internal.SetMasterPublicKey(master.internal)
478+
}
479+
466480
// MarshalASN1 marshal encryption private key to asn.1 format data according
467481
// SM9 cryptographic algorithm application specification
468482
func (priv *EncryptPrivateKey) MarshalASN1() ([]byte, error) {

sm9/sm9_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ func TestSignASN1(t *testing.T) {
2121
if err != nil {
2222
t.Fatal(err)
2323
}
24+
// Test Marshal and Unmarshal
25+
userKeyBytes := userKey.Bytes()
26+
userKey, err = sm9.UnmarshalSignPrivateKeyRaw(userKeyBytes)
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
userKey.SetMasterPublic(masterKey.PublicKey())
2431
sig, err := userKey.Sign(rand.Reader, hashed, nil)
2532
if err != nil {
2633
t.Fatal(err)
@@ -103,6 +110,13 @@ func TestEncryptDecrypt(t *testing.T) {
103110
if err != nil {
104111
t.Fatal(err)
105112
}
113+
// Test Marshal and Unmarshal
114+
userKeyBytes := userKey.Bytes()
115+
userKey, err = sm9.UnmarshalEncryptPrivateKeyRaw(userKeyBytes)
116+
if err != nil {
117+
t.Fatal(err)
118+
}
119+
userKey.SetMasterPublic(masterKey.PublicKey())
106120
encTypes := []sm9.EncrypterOpts{
107121
sm9.DefaultEncrypterOpts, sm9.SM4ECBEncrypterOpts, sm9.SM4CBCEncrypterOpts, sm9.SM4CFBEncrypterOpts, sm9.SM4OFBEncrypterOpts,
108122
}

0 commit comments

Comments
 (0)