2016-06-15 13:54:07 +00:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
This key store behaves as KeyStorePlain with the difference that
|
|
|
|
the private key is encrypted and on disk uses another JSON encoding.
|
|
|
|
|
|
|
|
The crypto is documented at https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2017-02-27 12:40:42 +00:00
|
|
|
package keystore
|
2016-06-15 13:54:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/aes"
|
2018-08-07 13:31:06 +00:00
|
|
|
"crypto/rand"
|
2016-06-15 13:54:07 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-08-07 13:31:06 +00:00
|
|
|
"io"
|
2016-06-15 13:54:07 +00:00
|
|
|
"io/ioutil"
|
2018-09-27 19:16:15 +00:00
|
|
|
"os"
|
2016-06-15 13:54:07 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
2016-06-15 13:54:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-05-01 11:09:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2016-06-15 13:54:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-06-28 06:53:50 +00:00
|
|
|
"github.com/google/uuid"
|
2021-06-28 11:04:35 +00:00
|
|
|
"github.com/status-im/status-go/extkeys"
|
2016-06-15 13:54:07 +00:00
|
|
|
"golang.org/x/crypto/pbkdf2"
|
|
|
|
"golang.org/x/crypto/scrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
keyHeaderKDF = "scrypt"
|
|
|
|
|
2016-11-25 05:50:30 +00:00
|
|
|
// StandardScryptN is the N parameter of Scrypt encryption algorithm, using 256MB
|
|
|
|
// memory and taking approximately 1s CPU time on a modern processor.
|
2016-06-15 13:54:07 +00:00
|
|
|
StandardScryptN = 1 << 18
|
2016-11-25 05:50:30 +00:00
|
|
|
|
|
|
|
// StandardScryptP is the P parameter of Scrypt encryption algorithm, using 256MB
|
|
|
|
// memory and taking approximately 1s CPU time on a modern processor.
|
2016-06-15 13:54:07 +00:00
|
|
|
StandardScryptP = 1
|
|
|
|
|
2016-11-25 05:50:30 +00:00
|
|
|
// LightScryptN is the N parameter of Scrypt encryption algorithm, using 4MB
|
|
|
|
// memory and taking approximately 100ms CPU time on a modern processor.
|
2016-06-15 13:54:07 +00:00
|
|
|
LightScryptN = 1 << 12
|
2016-11-25 05:50:30 +00:00
|
|
|
|
|
|
|
// LightScryptP is the P parameter of Scrypt encryption algorithm, using 4MB
|
|
|
|
// memory and taking approximately 100ms CPU time on a modern processor.
|
2016-06-15 13:54:07 +00:00
|
|
|
LightScryptP = 6
|
|
|
|
|
|
|
|
scryptR = 8
|
|
|
|
scryptDKLen = 32
|
|
|
|
)
|
|
|
|
|
|
|
|
type keyStorePassphrase struct {
|
|
|
|
keysDirPath string
|
|
|
|
scryptN int
|
|
|
|
scryptP int
|
2018-09-27 19:16:15 +00:00
|
|
|
// skipKeyFileVerification disables the security-feature which does
|
|
|
|
// reads and decrypts any newly created keyfiles. This should be 'false' in all
|
|
|
|
// cases except tests -- setting this to 'true' is not recommended.
|
|
|
|
skipKeyFileVerification bool
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ks keyStorePassphrase) GetKey(addr common.Address, filename, auth string) (*Key, error) {
|
|
|
|
// Load the key from the keystore and decrypt its contents
|
|
|
|
keyjson, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
key, err := DecryptKey(keyjson, auth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Make sure we're really operating on the requested key (no swap attacks)
|
|
|
|
if key.Address != addr {
|
|
|
|
return nil, fmt.Errorf("key content mismatch: have account %x, want %x", key.Address, addr)
|
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
2018-01-15 20:26:41 +00:00
|
|
|
// StoreKey generates a key, encrypts with 'auth' and stores in the given directory
|
2019-10-04 15:21:24 +00:00
|
|
|
func StoreKey(dir, auth string, scryptN, scryptP int) (accounts.Account, error) {
|
2018-09-27 19:16:15 +00:00
|
|
|
_, a, err := storeNewKey(&keyStorePassphrase{dir, scryptN, scryptP, false}, rand.Reader, auth)
|
2019-10-04 15:21:24 +00:00
|
|
|
return a, err
|
2018-01-15 20:26:41 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 13:54:07 +00:00
|
|
|
func (ks keyStorePassphrase) StoreKey(filename string, key *Key, auth string) error {
|
|
|
|
keyjson, err := EncryptKey(key, auth, ks.scryptN, ks.scryptP)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-27 19:16:15 +00:00
|
|
|
// Write into temporary file
|
|
|
|
tmpName, err := writeTemporaryKeyFile(filename, keyjson)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ks.skipKeyFileVerification {
|
|
|
|
// Verify that we can decrypt the file with the given password.
|
|
|
|
_, err = ks.GetKey(key.Address, tmpName, auth)
|
|
|
|
if err != nil {
|
|
|
|
msg := "An error was encountered when saving and verifying the keystore file. \n" +
|
|
|
|
"This indicates that the keystore is corrupted. \n" +
|
|
|
|
"The corrupted file is stored at \n%v\n" +
|
|
|
|
"Please file a ticket at:\n\n" +
|
|
|
|
"https://github.com/ethereum/go-ethereum/issues." +
|
|
|
|
"The error was : %s"
|
2021-06-28 06:53:50 +00:00
|
|
|
//lint:ignore ST1005 This is a message for the user
|
2018-09-27 19:16:15 +00:00
|
|
|
return fmt.Errorf(msg, tmpName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return os.Rename(tmpName, filename)
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ks keyStorePassphrase) JoinPath(filename string) string {
|
|
|
|
if filepath.IsAbs(filename) {
|
|
|
|
return filename
|
|
|
|
}
|
2018-06-20 14:51:55 +00:00
|
|
|
return filepath.Join(ks.keysDirPath, filename)
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
// Encryptdata encrypts the data given as 'data' with the password 'auth'.
|
|
|
|
func EncryptDataV3(data, auth []byte, scryptN, scryptP int) (CryptoJSON, error) {
|
2018-08-07 13:31:06 +00:00
|
|
|
|
|
|
|
salt := make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
|
|
|
|
panic("reading from crypto/rand failed: " + err.Error())
|
|
|
|
}
|
2018-12-19 10:02:07 +00:00
|
|
|
derivedKey, err := scrypt.Key(auth, salt, scryptN, scryptR, scryptP, scryptDKLen)
|
2016-06-15 13:54:07 +00:00
|
|
|
if err != nil {
|
2018-12-19 10:02:07 +00:00
|
|
|
return CryptoJSON{}, err
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
encryptKey := derivedKey[:16]
|
|
|
|
|
2018-08-07 13:31:06 +00:00
|
|
|
iv := make([]byte, aes.BlockSize) // 16
|
|
|
|
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
|
|
|
panic("reading from crypto/rand failed: " + err.Error())
|
|
|
|
}
|
2018-12-19 10:02:07 +00:00
|
|
|
cipherText, err := aesCTRXOR(encryptKey, data, iv)
|
2016-06-15 13:54:07 +00:00
|
|
|
if err != nil {
|
2018-12-19 10:02:07 +00:00
|
|
|
return CryptoJSON{}, err
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
mac := crypto.Keccak256(derivedKey[16:32], cipherText)
|
|
|
|
|
|
|
|
scryptParamsJSON := make(map[string]interface{}, 5)
|
|
|
|
scryptParamsJSON["n"] = scryptN
|
|
|
|
scryptParamsJSON["r"] = scryptR
|
|
|
|
scryptParamsJSON["p"] = scryptP
|
|
|
|
scryptParamsJSON["dklen"] = scryptDKLen
|
|
|
|
scryptParamsJSON["salt"] = hex.EncodeToString(salt)
|
|
|
|
cipherParamsJSON := cipherparamsJSON{
|
|
|
|
IV: hex.EncodeToString(iv),
|
|
|
|
}
|
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
cryptoStruct := CryptoJSON{
|
2016-06-15 13:54:07 +00:00
|
|
|
Cipher: "aes-128-ctr",
|
|
|
|
CipherText: hex.EncodeToString(cipherText),
|
|
|
|
CipherParams: cipherParamsJSON,
|
2017-10-10 09:38:49 +00:00
|
|
|
KDF: keyHeaderKDF,
|
2016-06-15 13:54:07 +00:00
|
|
|
KDFParams: scryptParamsJSON,
|
|
|
|
MAC: hex.EncodeToString(mac),
|
|
|
|
}
|
2018-12-19 10:02:07 +00:00
|
|
|
return cryptoStruct, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptKey encrypts a key using the specified scrypt parameters into a json
|
|
|
|
// blob that can be decrypted later on.
|
|
|
|
func EncryptKey(key *Key, auth string, scryptN, scryptP int) ([]byte, error) {
|
|
|
|
keyBytes := math.PaddedBigBytes(key.PrivateKey.D, 32)
|
|
|
|
cryptoStruct, err := EncryptDataV3(keyBytes, []byte(auth), scryptN, scryptP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-28 11:04:35 +00:00
|
|
|
encryptedExtendedKey, err := EncryptExtendedKey(key.ExtendedKey, auth, scryptN, scryptP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-15 13:54:07 +00:00
|
|
|
encryptedKeyJSONV3 := encryptedKeyJSONV3{
|
|
|
|
hex.EncodeToString(key.Address[:]),
|
|
|
|
cryptoStruct,
|
|
|
|
key.Id.String(),
|
|
|
|
version,
|
2021-06-28 11:04:35 +00:00
|
|
|
encryptedExtendedKey,
|
|
|
|
key.SubAccountIndex,
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
return json.Marshal(encryptedKeyJSONV3)
|
|
|
|
}
|
|
|
|
|
2021-06-28 11:04:35 +00:00
|
|
|
func EncryptExtendedKey(extKey *extkeys.ExtendedKey, auth string, scryptN, scryptP int) (CryptoJSON, error) {
|
|
|
|
if extKey == nil {
|
|
|
|
return CryptoJSON{}, nil
|
|
|
|
}
|
|
|
|
authArray := []byte(auth)
|
|
|
|
salt := make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
|
|
|
|
panic("reading from crypto/rand failed: " + err.Error())
|
|
|
|
}
|
|
|
|
derivedKey, err := scrypt.Key(authArray, salt, scryptN, scryptR, scryptP, scryptDKLen)
|
|
|
|
if err != nil {
|
|
|
|
return CryptoJSON{}, err
|
|
|
|
}
|
|
|
|
encryptKey := derivedKey[:16]
|
|
|
|
keyBytes := []byte(extKey.String())
|
|
|
|
|
|
|
|
iv := make([]byte, aes.BlockSize) // 16
|
|
|
|
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
|
|
|
panic("reading from crypto/rand failed: " + err.Error())
|
|
|
|
}
|
|
|
|
cipherText, err := aesCTRXOR(encryptKey, keyBytes, iv)
|
|
|
|
if err != nil {
|
|
|
|
return CryptoJSON{}, err
|
|
|
|
}
|
|
|
|
mac := crypto.Keccak256(derivedKey[16:32], cipherText)
|
|
|
|
|
|
|
|
scryptParamsJSON := make(map[string]interface{}, 5)
|
|
|
|
scryptParamsJSON["n"] = scryptN
|
|
|
|
scryptParamsJSON["r"] = scryptR
|
|
|
|
scryptParamsJSON["p"] = scryptP
|
|
|
|
scryptParamsJSON["dklen"] = scryptDKLen
|
|
|
|
scryptParamsJSON["salt"] = hex.EncodeToString(salt)
|
|
|
|
|
|
|
|
cipherParamsJSON := cipherparamsJSON{
|
|
|
|
IV: hex.EncodeToString(iv),
|
|
|
|
}
|
|
|
|
|
|
|
|
return CryptoJSON{
|
|
|
|
Cipher: "aes-128-ctr",
|
|
|
|
CipherText: hex.EncodeToString(cipherText),
|
|
|
|
CipherParams: cipherParamsJSON,
|
|
|
|
KDF: "scrypt",
|
|
|
|
KDFParams: scryptParamsJSON,
|
|
|
|
MAC: hex.EncodeToString(mac),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-06-15 13:54:07 +00:00
|
|
|
// DecryptKey decrypts a key from a json blob, returning the private key itself.
|
|
|
|
func DecryptKey(keyjson []byte, auth string) (*Key, error) {
|
|
|
|
// Parse the json into a simple map to fetch the key version
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal(keyjson, &m); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Depending on the version try to parse one way or another
|
|
|
|
var (
|
|
|
|
keyBytes, keyId []byte
|
|
|
|
err error
|
2021-06-28 11:04:35 +00:00
|
|
|
extKeyBytes []byte
|
|
|
|
extKey *extkeys.ExtendedKey
|
2016-06-15 13:54:07 +00:00
|
|
|
)
|
2021-06-28 11:04:35 +00:00
|
|
|
|
|
|
|
subAccountIndex, ok := m["subaccountindex"].(float64)
|
|
|
|
if !ok {
|
|
|
|
subAccountIndex = 0
|
|
|
|
}
|
|
|
|
|
2016-06-15 13:54:07 +00:00
|
|
|
if version, ok := m["version"].(string); ok && version == "1" {
|
|
|
|
k := new(encryptedKeyJSONV1)
|
|
|
|
if err := json.Unmarshal(keyjson, k); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
keyBytes, keyId, err = decryptKeyV1(k, auth)
|
2021-06-28 11:04:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
extKey, err = extkeys.NewKeyFromString(extkeys.EmptyExtendedKeyString)
|
2016-06-15 13:54:07 +00:00
|
|
|
} else {
|
|
|
|
k := new(encryptedKeyJSONV3)
|
|
|
|
if err := json.Unmarshal(keyjson, k); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
keyBytes, keyId, err = decryptKeyV3(k, auth)
|
2021-06-28 11:04:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
extKeyBytes, err = decryptExtendedKey(k, auth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
extKey, err = extkeys.NewKeyFromString(string(extKeyBytes))
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
// Handle any decryption errors and return the key
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-04 16:14:17 +00:00
|
|
|
key := crypto.ToECDSAUnsafe(keyBytes)
|
2021-06-28 06:53:50 +00:00
|
|
|
id, err := uuid.FromBytes(keyId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-15 13:54:07 +00:00
|
|
|
return &Key{
|
2021-06-28 11:04:35 +00:00
|
|
|
Id: id,
|
|
|
|
Address: crypto.PubkeyToAddress(key.PublicKey),
|
|
|
|
PrivateKey: key,
|
|
|
|
ExtendedKey: extKey,
|
|
|
|
SubAccountIndex: uint32(subAccountIndex),
|
2016-06-15 13:54:07 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
func DecryptDataV3(cryptoJson CryptoJSON, auth string) ([]byte, error) {
|
|
|
|
if cryptoJson.Cipher != "aes-128-ctr" {
|
2021-06-28 06:53:50 +00:00
|
|
|
return nil, fmt.Errorf("cipher not supported: %v", cryptoJson.Cipher)
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
2018-12-19 10:02:07 +00:00
|
|
|
mac, err := hex.DecodeString(cryptoJson.MAC)
|
2016-06-15 13:54:07 +00:00
|
|
|
if err != nil {
|
2018-12-19 10:02:07 +00:00
|
|
|
return nil, err
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
iv, err := hex.DecodeString(cryptoJson.CipherParams.IV)
|
2016-06-15 13:54:07 +00:00
|
|
|
if err != nil {
|
2018-12-19 10:02:07 +00:00
|
|
|
return nil, err
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
cipherText, err := hex.DecodeString(cryptoJson.CipherText)
|
2016-06-15 13:54:07 +00:00
|
|
|
if err != nil {
|
2018-12-19 10:02:07 +00:00
|
|
|
return nil, err
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
derivedKey, err := getKDFKey(cryptoJson, auth)
|
2016-06-15 13:54:07 +00:00
|
|
|
if err != nil {
|
2018-12-19 10:02:07 +00:00
|
|
|
return nil, err
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
calculatedMAC := crypto.Keccak256(derivedKey[16:32], cipherText)
|
|
|
|
if !bytes.Equal(calculatedMAC, mac) {
|
2018-12-19 10:02:07 +00:00
|
|
|
return nil, ErrDecrypt
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
plainText, err := aesCTRXOR(derivedKey[:16], cipherText, iv)
|
2018-12-19 10:02:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return plainText, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func decryptKeyV3(keyProtected *encryptedKeyJSONV3, auth string) (keyBytes []byte, keyId []byte, err error) {
|
|
|
|
if keyProtected.Version != version {
|
2021-06-28 06:53:50 +00:00
|
|
|
return nil, nil, fmt.Errorf("version not supported: %v", keyProtected.Version)
|
|
|
|
}
|
|
|
|
keyUUID, err := uuid.Parse(keyProtected.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2018-12-19 10:02:07 +00:00
|
|
|
}
|
2021-06-28 06:53:50 +00:00
|
|
|
keyId = keyUUID[:]
|
2018-12-19 10:02:07 +00:00
|
|
|
plainText, err := DecryptDataV3(keyProtected.Crypto, auth)
|
2016-06-15 13:54:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return plainText, keyId, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func decryptKeyV1(keyProtected *encryptedKeyJSONV1, auth string) (keyBytes []byte, keyId []byte, err error) {
|
2021-06-28 06:53:50 +00:00
|
|
|
keyUUID, err := uuid.Parse(keyProtected.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
keyId = keyUUID[:]
|
2016-06-15 13:54:07 +00:00
|
|
|
mac, err := hex.DecodeString(keyProtected.Crypto.MAC)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
iv, err := hex.DecodeString(keyProtected.Crypto.CipherParams.IV)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cipherText, err := hex.DecodeString(keyProtected.Crypto.CipherText)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
derivedKey, err := getKDFKey(keyProtected.Crypto, auth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
calculatedMAC := crypto.Keccak256(derivedKey[16:32], cipherText)
|
|
|
|
if !bytes.Equal(calculatedMAC, mac) {
|
|
|
|
return nil, nil, ErrDecrypt
|
|
|
|
}
|
|
|
|
|
|
|
|
plainText, err := aesCBCDecrypt(crypto.Keccak256(derivedKey[:16])[:16], cipherText, iv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return plainText, keyId, err
|
|
|
|
}
|
|
|
|
|
2021-06-28 11:04:35 +00:00
|
|
|
func decryptExtendedKey(keyProtected *encryptedKeyJSONV3, auth string) (plainText []byte, err error) {
|
|
|
|
if len(keyProtected.ExtendedKey.CipherText) == 0 {
|
|
|
|
return []byte(extkeys.EmptyExtendedKeyString), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyProtected.Version != version {
|
|
|
|
return nil, fmt.Errorf("Version not supported: %v", keyProtected.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyProtected.ExtendedKey.Cipher != "aes-128-ctr" {
|
|
|
|
return nil, fmt.Errorf("Cipher not supported: %v", keyProtected.ExtendedKey.Cipher)
|
|
|
|
}
|
|
|
|
|
|
|
|
mac, err := hex.DecodeString(keyProtected.ExtendedKey.MAC)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
iv, err := hex.DecodeString(keyProtected.ExtendedKey.CipherParams.IV)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cipherText, err := hex.DecodeString(keyProtected.ExtendedKey.CipherText)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
derivedKey, err := getKDFKey(keyProtected.ExtendedKey, auth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
calculatedMAC := crypto.Keccak256(derivedKey[16:32], cipherText)
|
|
|
|
if !bytes.Equal(calculatedMAC, mac) {
|
|
|
|
return nil, ErrDecrypt
|
|
|
|
}
|
|
|
|
|
|
|
|
plainText, err = aesCTRXOR(derivedKey[:16], cipherText, iv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return plainText, err
|
|
|
|
}
|
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
func getKDFKey(cryptoJSON CryptoJSON, auth string) ([]byte, error) {
|
2016-06-15 13:54:07 +00:00
|
|
|
authArray := []byte(auth)
|
|
|
|
salt, err := hex.DecodeString(cryptoJSON.KDFParams["salt"].(string))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dkLen := ensureInt(cryptoJSON.KDFParams["dklen"])
|
|
|
|
|
2017-10-10 09:38:49 +00:00
|
|
|
if cryptoJSON.KDF == keyHeaderKDF {
|
2016-06-15 13:54:07 +00:00
|
|
|
n := ensureInt(cryptoJSON.KDFParams["n"])
|
|
|
|
r := ensureInt(cryptoJSON.KDFParams["r"])
|
|
|
|
p := ensureInt(cryptoJSON.KDFParams["p"])
|
|
|
|
return scrypt.Key(authArray, salt, n, r, p, dkLen)
|
|
|
|
|
|
|
|
} else if cryptoJSON.KDF == "pbkdf2" {
|
|
|
|
c := ensureInt(cryptoJSON.KDFParams["c"])
|
|
|
|
prf := cryptoJSON.KDFParams["prf"].(string)
|
|
|
|
if prf != "hmac-sha256" {
|
2021-06-28 06:53:50 +00:00
|
|
|
return nil, fmt.Errorf("unsupported PBKDF2 PRF: %s", prf)
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
key := pbkdf2.Key(authArray, salt, c, dkLen, sha256.New)
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
return nil, fmt.Errorf("unsupported KDF: %s", cryptoJSON.KDF)
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: can we do without this when unmarshalling dynamic JSON?
|
|
|
|
// why do integers in KDF params end up as float64 and not int after
|
|
|
|
// unmarshal?
|
|
|
|
func ensureInt(x interface{}) int {
|
|
|
|
res, ok := x.(int)
|
|
|
|
if !ok {
|
|
|
|
res = int(x.(float64))
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|