sm9: fix decrypt method ASN1 format checking issue

This commit is contained in:
Sun Yimin 2023-02-13 15:36:04 +08:00 committed by GitHub
parent aedef9d00d
commit d52750d7a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 8 deletions

View File

@ -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

View File

@ -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)
}