Skip to content

Commit

Permalink
fix: xsalsa20 decryptsimmetric (#15000)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianToledano authored Feb 13, 2023
1 parent a90569c commit cee91a5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion crypto/armor.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func decryptPrivKey(saltBytes []byte, encBytes []byte, passphrase string) (privK
key = crypto.Sha256(key) // Get 32 bytes

privKeyBytes, err := xsalsa20symmetric.DecryptSymmetric(encBytes, key)
if err != nil && err.Error() == "Ciphertext decryption failed" {
if err != nil && err == xsalsa20symmetric.ErrCiphertextDecrypt {
return privKey, sdkerrors.ErrWrongPassword
} else if err != nil {
return privKey, err
Expand Down
7 changes: 4 additions & 3 deletions crypto/keyring/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
)

Expand Down Expand Up @@ -436,7 +437,7 @@ func TestKeyringKeybaseExportImportPrivKey(t *testing.T) {

// try import the key - wrong password
err = kb.ImportPrivKey("john2", keystr, "bad pass")
require.Equal(t, "failed to decrypt private key: ciphertext decryption failed", err.Error())
require.True(t, errors.Is(err, sdkerrors.ErrWrongPassword))

// try import the key with the correct password
require.NoError(t, kb.ImportPrivKey("john2", keystr, "somepassword"))
Expand Down Expand Up @@ -1248,7 +1249,7 @@ func TestAltKeyring_ImportExportPrivKey(t *testing.T) {
newUID := otherID
// Should fail importing with wrong password
err = kr.ImportPrivKey(newUID, armor, "wrongPass")
require.EqualError(t, err, "failed to decrypt private key: ciphertext decryption failed")
require.True(t, errors.Is(err, sdkerrors.ErrWrongPassword))

err = kr.ImportPrivKey(newUID, armor, passphrase)
require.NoError(t, err)
Expand Down Expand Up @@ -1278,7 +1279,7 @@ func TestAltKeyring_ImportExportPrivKey_ByAddress(t *testing.T) {
newUID := otherID
// Should fail importing with wrong password
err = kr.ImportPrivKey(newUID, armor, "wrongPass")
require.EqualError(t, err, "failed to decrypt private key: ciphertext decryption failed")
require.True(t, errors.Is(err, sdkerrors.ErrWrongPassword))

err = kr.ImportPrivKey(newUID, armor, passphrase)
require.NoError(t, err)
Expand Down
4 changes: 3 additions & 1 deletion crypto/xsalsa20symmetric/symmetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const (
secretLen = 32
)

var ErrCiphertextDecrypt = errors.New("ciphertext decryption failed")

// secret must be 32 bytes long. Use something like Sha256(Bcrypt(passphrase))
// The ciphertext is (secretbox.Overhead + 24) bytes longer than the plaintext.
func EncryptSymmetric(plaintext []byte, secret []byte) (ciphertext []byte) {
Expand Down Expand Up @@ -49,7 +51,7 @@ func DecryptSymmetric(ciphertext []byte, secret []byte) (plaintext []byte, err e
plaintext = make([]byte, len(ciphertext)-nonceLen-secretbox.Overhead)
_, ok := secretbox.Open(plaintext[:0], ciphertext[nonceLen:], &nonceArr, &secretArr)
if !ok {
return nil, errors.New("ciphertext decryption failed")
return nil, ErrCiphertextDecrypt
}
return plaintext, nil
}
Expand Down

0 comments on commit cee91a5

Please sign in to comment.