mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 20:26:19 +08:00
sm9: fix decrypt method ASN1 format checking issue
This commit is contained in:
parent
aedef9d00d
commit
d52750d7a8
13
sm9/sm9.go
13
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)
|
return decrypt(c, key[:key1Len], key[key1Len:], c2Bytes, c3Bytes, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decrypt decrypt chipher, ciphertext should be with ASN.1 format according
|
// Decrypt decrypt chipher, ciphertext should be with format C1||C3||C2
|
||||||
// SM9 cryptographic algorithm application specification, SM9Cipher definition.
|
|
||||||
func (priv *EncryptPrivateKey) Decrypt(uid, ciphertext []byte, opts EncrypterOpts) ([]byte, error) {
|
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)
|
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.
|
// KeyExchange key exchange struct, include internal stat in whole key exchange flow.
|
||||||
// Initiator's flow will be: NewKeyExchange -> InitKeyExchange -> transmission -> ConfirmResponder
|
// Initiator's flow will be: NewKeyExchange -> InitKeyExchange -> transmission -> ConfirmResponder
|
||||||
// Responder's flow will be: NewKeyExchange -> waiting ... -> RepondKeyExchange -> transmission -> ConfirmInitiator
|
// Responder's flow will be: NewKeyExchange -> waiting ... -> RepondKeyExchange -> transmission -> ConfirmInitiator
|
||||||
|
@ -750,7 +750,7 @@ func TestEncryptDecrypt(t *testing.T) {
|
|||||||
|
|
||||||
got, err = userKey.Decrypt(uid, cipher, opts)
|
got, err = userKey.Decrypt(uid, cipher, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatalf("encType %v, first byte %x, %v", opts.GetEncryptType(), cipher[0], err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if string(got) != string(plaintext) {
|
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))
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user