diff --git a/sm9/sm9.go b/sm9/sm9.go index 200b29b..78b12aa 100644 --- a/sm9/sm9.go +++ b/sm9/sm9.go @@ -529,16 +529,17 @@ func DecryptASN1(priv *EncryptPrivateKey, uid, ciphertext []byte) ([]byte, error return decrypt(c, key[:key1Len], key[key1Len:], c2Bytes, c3Bytes, opts) } -// Decrypt decrypt chipher, ciphertext should be with ASN.1 format according -// SM9 cryptographic algorithm application specification, SM9Cipher definition. +// Decrypt decrypt chipher, ciphertext should be with format C1||C3||C2 func (priv *EncryptPrivateKey) Decrypt(uid, ciphertext []byte, opts EncrypterOpts) ([]byte, error) { - if ciphertext[0] == 0x30 { // should be ASN.1 format - return DecryptASN1(priv, uid, ciphertext) - } - // fallback to C1||C3||C2 raw format return Decrypt(priv, uid, ciphertext, opts) } +// DecryptASN1 decrypt chipher, ciphertext should be with ASN.1 format according +// SM9 cryptographic algorithm application specification, SM9Cipher definition. +func (priv *EncryptPrivateKey) DecryptASN1(uid, ciphertext []byte) ([]byte, error) { + return DecryptASN1(priv, uid, ciphertext) +} + // KeyExchange key exchange struct, include internal stat in whole key exchange flow. // Initiator's flow will be: NewKeyExchange -> InitKeyExchange -> transmission -> ConfirmResponder // Responder's flow will be: NewKeyExchange -> waiting ... -> RepondKeyExchange -> transmission -> ConfirmInitiator diff --git a/sm9/sm9_test.go b/sm9/sm9_test.go index 23bb723..5ae86dd 100644 --- a/sm9/sm9_test.go +++ b/sm9/sm9_test.go @@ -750,7 +750,7 @@ func TestEncryptDecrypt(t *testing.T) { got, err = userKey.Decrypt(uid, cipher, opts) if err != nil { - t.Fatal(err) + t.Fatalf("encType %v, first byte %x, %v", opts.GetEncryptType(), cipher[0], err) } if string(got) != string(plaintext) { @@ -789,7 +789,7 @@ func TestEncryptDecryptASN1(t *testing.T) { t.Errorf("expected %v, got %v\n", string(plaintext), string(got)) } - got, err = userKey.Decrypt(uid, cipher, opts) + got, err = userKey.DecryptASN1(uid, cipher) if err != nil { t.Fatal(err) }