|
| 1 | +package mintkey |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/tendermint/crypto/bcrypt" |
| 8 | + |
| 9 | + emintEncoding "github.com/cosmos/ethermint/crypto/encoding" |
| 10 | + "github.com/tendermint/tendermint/crypto" |
| 11 | + "github.com/tendermint/tendermint/crypto/armor" |
| 12 | + |
| 13 | + "github.com/tendermint/tendermint/crypto/xsalsa20symmetric" |
| 14 | + |
| 15 | + cmn "github.com/tendermint/tendermint/libs/common" |
| 16 | + |
| 17 | + "github.com/cosmos/cosmos-sdk/crypto/keys/keyerror" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + blockTypePrivKey = "ETHERMINT PRIVATE KEY" |
| 22 | + blockTypeKeyInfo = "ETHERMINT KEY INFO" |
| 23 | + blockTypePubKey = "ETHERMINT PUBLIC KEY" |
| 24 | +) |
| 25 | + |
| 26 | +// Make bcrypt security parameter var, so it can be changed within the lcd test |
| 27 | +// Making the bcrypt security parameter a var shouldn't be a security issue: |
| 28 | +// One can't verify an invalid key by maliciously changing the bcrypt |
| 29 | +// parameter during a runtime vulnerability. The main security |
| 30 | +// threat this then exposes would be something that changes this during |
| 31 | +// runtime before the user creates their key. This vulnerability must |
| 32 | +// succeed to update this to that same value before every subsequent call |
| 33 | +// to the keys command in future startups / or the attacker must get access |
| 34 | +// to the filesystem. However, with a similar threat model (changing |
| 35 | +// variables in runtime), one can cause the user to sign a different tx |
| 36 | +// than what they see, which is a significantly cheaper attack then breaking |
| 37 | +// a bcrypt hash. (Recall that the nonce still exists to break rainbow tables) |
| 38 | +// For further notes on security parameter choice, see README.md |
| 39 | +var BcryptSecurityParameter = 12 |
| 40 | + |
| 41 | +//----------------------------------------------------------------- |
| 42 | +// add armor |
| 43 | + |
| 44 | +// Armor the InfoBytes |
| 45 | +func ArmorInfoBytes(bz []byte) string { |
| 46 | + return armorBytes(bz, blockTypeKeyInfo) |
| 47 | +} |
| 48 | + |
| 49 | +// Armor the PubKeyBytes |
| 50 | +func ArmorPubKeyBytes(bz []byte) string { |
| 51 | + return armorBytes(bz, blockTypePubKey) |
| 52 | +} |
| 53 | + |
| 54 | +func armorBytes(bz []byte, blockType string) string { |
| 55 | + header := map[string]string{ |
| 56 | + "type": "Info", |
| 57 | + "version": "0.0.0", |
| 58 | + } |
| 59 | + return armor.EncodeArmor(blockType, header, bz) |
| 60 | +} |
| 61 | + |
| 62 | +//----------------------------------------------------------------- |
| 63 | +// remove armor |
| 64 | + |
| 65 | +// Unarmor the InfoBytes |
| 66 | +func UnarmorInfoBytes(armorStr string) (bz []byte, err error) { |
| 67 | + return unarmorBytes(armorStr, blockTypeKeyInfo) |
| 68 | +} |
| 69 | + |
| 70 | +// Unarmor the PubKeyBytes |
| 71 | +func UnarmorPubKeyBytes(armorStr string) (bz []byte, err error) { |
| 72 | + return unarmorBytes(armorStr, blockTypePubKey) |
| 73 | +} |
| 74 | + |
| 75 | +func unarmorBytes(armorStr, blockType string) (bz []byte, err error) { |
| 76 | + bType, header, bz, err := armor.DecodeArmor(armorStr) |
| 77 | + if err != nil { |
| 78 | + return |
| 79 | + } |
| 80 | + if bType != blockType { |
| 81 | + err = fmt.Errorf("Unrecognized armor type %q, expected: %q", bType, blockType) |
| 82 | + return |
| 83 | + } |
| 84 | + if header["version"] != "0.0.0" { |
| 85 | + err = fmt.Errorf("Unrecognized version: %v", header["version"]) |
| 86 | + return |
| 87 | + } |
| 88 | + return |
| 89 | +} |
| 90 | + |
| 91 | +//----------------------------------------------------------------- |
| 92 | +// encrypt/decrypt with armor |
| 93 | + |
| 94 | +// Encrypt and armor the private key. |
| 95 | +func EncryptArmorPrivKey(privKey crypto.PrivKey, passphrase string) string { |
| 96 | + saltBytes, encBytes := encryptPrivKey(privKey, passphrase) |
| 97 | + header := map[string]string{ |
| 98 | + "kdf": "bcrypt", |
| 99 | + "salt": fmt.Sprintf("%X", saltBytes), |
| 100 | + } |
| 101 | + armorStr := armor.EncodeArmor(blockTypePrivKey, header, encBytes) |
| 102 | + return armorStr |
| 103 | +} |
| 104 | + |
| 105 | +// encrypt the given privKey with the passphrase using a randomly |
| 106 | +// generated salt and the xsalsa20 cipher. returns the salt and the |
| 107 | +// encrypted priv key. |
| 108 | +func encryptPrivKey(privKey crypto.PrivKey, passphrase string) (saltBytes []byte, encBytes []byte) { |
| 109 | + saltBytes = crypto.CRandBytes(16) |
| 110 | + key, err := bcrypt.GenerateFromPassword(saltBytes, []byte(passphrase), BcryptSecurityParameter) |
| 111 | + if err != nil { |
| 112 | + cmn.Exit("Error generating bcrypt key from passphrase: " + err.Error()) |
| 113 | + } |
| 114 | + key = crypto.Sha256(key) // get 32 bytes |
| 115 | + privKeyBytes := privKey.Bytes() |
| 116 | + return saltBytes, xsalsa20symmetric.EncryptSymmetric(privKeyBytes, key) |
| 117 | +} |
| 118 | + |
| 119 | +// Unarmor and decrypt the private key. |
| 120 | +func UnarmorDecryptPrivKey(armorStr string, passphrase string) (crypto.PrivKey, error) { |
| 121 | + var privKey crypto.PrivKey |
| 122 | + blockType, header, encBytes, err := armor.DecodeArmor(armorStr) |
| 123 | + if err != nil { |
| 124 | + return privKey, err |
| 125 | + } |
| 126 | + if blockType != blockTypePrivKey { |
| 127 | + return privKey, fmt.Errorf("Unrecognized armor type: %v", blockType) |
| 128 | + } |
| 129 | + if header["kdf"] != "bcrypt" { |
| 130 | + return privKey, fmt.Errorf("Unrecognized KDF type: %v", header["KDF"]) |
| 131 | + } |
| 132 | + if header["salt"] == "" { |
| 133 | + return privKey, fmt.Errorf("Missing salt bytes") |
| 134 | + } |
| 135 | + saltBytes, err := hex.DecodeString(header["salt"]) |
| 136 | + if err != nil { |
| 137 | + return privKey, fmt.Errorf("Error decoding salt: %v", err.Error()) |
| 138 | + } |
| 139 | + privKey, err = decryptPrivKey(saltBytes, encBytes, passphrase) |
| 140 | + return privKey, err |
| 141 | +} |
| 142 | + |
| 143 | +func decryptPrivKey(saltBytes []byte, encBytes []byte, passphrase string) (privKey crypto.PrivKey, err error) { |
| 144 | + key, err := bcrypt.GenerateFromPassword(saltBytes, []byte(passphrase), BcryptSecurityParameter) |
| 145 | + if err != nil { |
| 146 | + cmn.Exit("Error generating bcrypt key from passphrase: " + err.Error()) |
| 147 | + } |
| 148 | + key = crypto.Sha256(key) // Get 32 bytes |
| 149 | + privKeyBytes, err := xsalsa20symmetric.DecryptSymmetric(encBytes, key) |
| 150 | + if err != nil && err.Error() == "Ciphertext decryption failed" { |
| 151 | + return privKey, keyerror.NewErrWrongPassword() |
| 152 | + } else if err != nil { |
| 153 | + return privKey, err |
| 154 | + } |
| 155 | + privKey, err = emintEncoding.PrivKeyFromBytes(privKeyBytes) |
| 156 | + return privKey, err |
| 157 | +} |
0 commit comments