2015-07-06 17:54:22 -07:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 09:48:40 -07:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 17:54:22 -07:00
|
|
|
//
|
2015-07-23 09:35:11 -07:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 17:54:22 -07:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 09:48:40 -07:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 17:54:22 -07:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 09:48:40 -07:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 17:54:22 -07:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 09:48:40 -07:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2014-12-31 06:39:33 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
2015-01-15 10:58:38 -08:00
|
|
|
This key store behaves as KeyStorePlain with the difference that
|
|
|
|
the private key is encrypted and on disk uses another JSON encoding.
|
2014-12-31 06:39:33 -08:00
|
|
|
|
2015-05-23 18:42:10 -07:00
|
|
|
The crypto is documented at https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition
|
2014-12-31 06:39:33 -08:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2017-01-24 01:49:20 -08:00
|
|
|
package keystore
|
2014-12-31 06:39:33 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/aes"
|
2018-07-26 04:33:13 -07:00
|
|
|
"crypto/rand"
|
2015-05-23 18:42:10 -07:00
|
|
|
"crypto/sha256"
|
2015-04-21 08:00:30 -07:00
|
|
|
"encoding/hex"
|
2014-12-31 06:39:33 -08:00
|
|
|
"encoding/json"
|
2015-05-23 18:42:10 -07:00
|
|
|
"fmt"
|
2018-07-26 04:33:13 -07:00
|
|
|
"io"
|
2016-03-02 16:15:42 -08:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2015-02-17 04:05:58 -08:00
|
|
|
|
2015-04-02 12:14:25 -07:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-02-28 06:09:11 -08:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2016-03-02 04:57:15 -08:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-09-10 08:48:20 -07:00
|
|
|
"github.com/pborman/uuid"
|
2015-05-23 18:42:10 -07:00
|
|
|
"golang.org/x/crypto/pbkdf2"
|
2015-02-17 04:05:58 -08:00
|
|
|
"golang.org/x/crypto/scrypt"
|
2014-12-31 06:39:33 -08:00
|
|
|
)
|
|
|
|
|
2015-01-07 07:06:26 -08:00
|
|
|
const (
|
2015-05-23 18:42:10 -07:00
|
|
|
keyHeaderKDF = "scrypt"
|
2015-10-23 07:49:36 -07:00
|
|
|
|
2016-09-05 09:07:57 -07:00
|
|
|
// StandardScryptN is the N parameter of Scrypt encryption algorithm, using 256MB
|
|
|
|
// memory and taking approximately 1s CPU time on a modern processor.
|
2015-10-23 07:49:36 -07:00
|
|
|
StandardScryptN = 1 << 18
|
2016-09-05 09:07:57 -07:00
|
|
|
|
|
|
|
// StandardScryptP is the P parameter of Scrypt encryption algorithm, using 256MB
|
|
|
|
// memory and taking approximately 1s CPU time on a modern processor.
|
2015-10-23 07:49:36 -07:00
|
|
|
StandardScryptP = 1
|
|
|
|
|
2016-09-05 09:07:57 -07:00
|
|
|
// LightScryptN is the N parameter of Scrypt encryption algorithm, using 4MB
|
|
|
|
// memory and taking approximately 100ms CPU time on a modern processor.
|
2015-10-23 07:49:36 -07:00
|
|
|
LightScryptN = 1 << 12
|
2016-09-05 09:07:57 -07:00
|
|
|
|
|
|
|
// LightScryptP is the P parameter of Scrypt encryption algorithm, using 4MB
|
|
|
|
// memory and taking approximately 100ms CPU time on a modern processor.
|
2015-10-23 07:49:36 -07:00
|
|
|
LightScryptP = 6
|
|
|
|
|
|
|
|
scryptR = 8
|
|
|
|
scryptDKLen = 32
|
2015-01-07 07:06:26 -08:00
|
|
|
)
|
2014-12-31 06:39:33 -08:00
|
|
|
|
2015-01-07 07:06:26 -08:00
|
|
|
type keyStorePassphrase struct {
|
2014-12-31 06:39:33 -08:00
|
|
|
keysDirPath string
|
2015-10-23 07:49:36 -07:00
|
|
|
scryptN int
|
|
|
|
scryptP int
|
2014-12-31 06:39:33 -08:00
|
|
|
}
|
|
|
|
|
2016-03-02 16:15:42 -08: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
|
2015-01-24 17:07:20 -08:00
|
|
|
}
|
|
|
|
|
2017-11-20 08:39:53 -08:00
|
|
|
// StoreKey generates a key, encrypts with 'auth' and stores in the given directory
|
|
|
|
func StoreKey(dir, auth string, scryptN, scryptP int) (common.Address, error) {
|
2018-07-26 04:33:13 -07:00
|
|
|
_, a, err := storeNewKey(&keyStorePassphrase{dir, scryptN, scryptP}, rand.Reader, auth)
|
2017-11-20 08:39:53 -08:00
|
|
|
return a.Address, err
|
|
|
|
}
|
|
|
|
|
2016-03-02 16:15:42 -08:00
|
|
|
func (ks keyStorePassphrase) StoreKey(filename string, key *Key, auth string) error {
|
2016-02-16 15:15:35 -08:00
|
|
|
keyjson, err := EncryptKey(key, auth, ks.scryptN, ks.scryptP)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-02 16:15:42 -08:00
|
|
|
return writeKeyFile(filename, keyjson)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ks keyStorePassphrase) JoinPath(filename string) string {
|
|
|
|
if filepath.IsAbs(filename) {
|
|
|
|
return filename
|
|
|
|
}
|
2018-05-03 01:37:56 -07:00
|
|
|
return filepath.Join(ks.keysDirPath, filename)
|
2016-02-16 15:15:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2014-12-31 06:39:33 -08:00
|
|
|
authArray := []byte(auth)
|
2018-07-26 04:33:13 -07:00
|
|
|
|
|
|
|
salt := make([]byte, 32)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
|
|
|
|
panic("reading from crypto/rand failed: " + err.Error())
|
|
|
|
}
|
2016-02-16 15:15:35 -08:00
|
|
|
derivedKey, err := scrypt.Key(authArray, salt, scryptN, scryptR, scryptP, scryptDKLen)
|
2014-12-31 06:39:33 -08:00
|
|
|
if err != nil {
|
2016-02-16 15:15:35 -08:00
|
|
|
return nil, err
|
2014-12-31 06:39:33 -08:00
|
|
|
}
|
2015-05-23 18:42:10 -07:00
|
|
|
encryptKey := derivedKey[:16]
|
2017-02-28 06:09:11 -08:00
|
|
|
keyBytes := math.PaddedBigBytes(key.PrivateKey.D, 32)
|
2014-12-31 06:39:33 -08:00
|
|
|
|
2018-07-26 04:33:13 -07:00
|
|
|
iv := make([]byte, aes.BlockSize) // 16
|
|
|
|
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
|
|
|
panic("reading from crypto/rand failed: " + err.Error())
|
|
|
|
}
|
2015-05-23 18:42:10 -07:00
|
|
|
cipherText, err := aesCTRXOR(encryptKey, keyBytes, iv)
|
2014-12-31 06:39:33 -08:00
|
|
|
if err != nil {
|
2016-02-16 15:15:35 -08:00
|
|
|
return nil, err
|
2014-12-31 06:39:33 -08:00
|
|
|
}
|
2016-03-02 04:57:15 -08:00
|
|
|
mac := crypto.Keccak256(derivedKey[16:32], cipherText)
|
2015-04-15 04:24:12 -07:00
|
|
|
|
2015-05-23 18:42:10 -07:00
|
|
|
scryptParamsJSON := make(map[string]interface{}, 5)
|
2016-02-16 15:15:35 -08:00
|
|
|
scryptParamsJSON["n"] = scryptN
|
|
|
|
scryptParamsJSON["r"] = scryptR
|
|
|
|
scryptParamsJSON["p"] = scryptP
|
|
|
|
scryptParamsJSON["dklen"] = scryptDKLen
|
2015-05-23 18:42:10 -07:00
|
|
|
scryptParamsJSON["salt"] = hex.EncodeToString(salt)
|
2015-04-15 04:24:12 -07:00
|
|
|
|
2015-05-10 11:30:02 -07:00
|
|
|
cipherParamsJSON := cipherparamsJSON{
|
|
|
|
IV: hex.EncodeToString(iv),
|
2015-04-15 04:24:12 -07:00
|
|
|
}
|
|
|
|
|
2015-05-10 11:30:02 -07:00
|
|
|
cryptoStruct := cryptoJSON{
|
2015-05-23 18:42:10 -07:00
|
|
|
Cipher: "aes-128-ctr",
|
2015-05-10 11:30:02 -07:00
|
|
|
CipherText: hex.EncodeToString(cipherText),
|
|
|
|
CipherParams: cipherParamsJSON,
|
2017-08-07 04:11:15 -07:00
|
|
|
KDF: keyHeaderKDF,
|
2015-05-10 11:30:02 -07:00
|
|
|
KDFParams: scryptParamsJSON,
|
|
|
|
MAC: hex.EncodeToString(mac),
|
2014-12-31 06:39:33 -08:00
|
|
|
}
|
2015-05-23 18:42:10 -07:00
|
|
|
encryptedKeyJSONV3 := encryptedKeyJSONV3{
|
2015-04-21 08:00:30 -07:00
|
|
|
hex.EncodeToString(key.Address[:]),
|
2015-05-10 11:30:02 -07:00
|
|
|
cryptoStruct,
|
|
|
|
key.Id.String(),
|
|
|
|
version,
|
2014-12-31 06:39:33 -08:00
|
|
|
}
|
2016-02-16 15:15:35 -08:00
|
|
|
return json.Marshal(encryptedKeyJSONV3)
|
2014-12-31 06:39:33 -08:00
|
|
|
}
|
|
|
|
|
2016-02-16 13:28:11 -08: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
|
2015-07-02 14:58:00 -07:00
|
|
|
m := make(map[string]interface{})
|
2016-02-16 13:28:11 -08:00
|
|
|
if err := json.Unmarshal(keyjson, &m); err != nil {
|
|
|
|
return nil, err
|
2014-12-31 06:39:33 -08:00
|
|
|
}
|
2016-02-16 13:28:11 -08:00
|
|
|
// Depending on the version try to parse one way or another
|
|
|
|
var (
|
|
|
|
keyBytes, keyId []byte
|
|
|
|
err error
|
|
|
|
)
|
2016-02-16 14:38:34 -08:00
|
|
|
if version, ok := m["version"].(string); ok && version == "1" {
|
2015-05-23 18:42:10 -07:00
|
|
|
k := new(encryptedKeyJSONV1)
|
2016-02-16 13:28:11 -08:00
|
|
|
if err := json.Unmarshal(keyjson, k); err != nil {
|
|
|
|
return nil, err
|
2015-05-23 18:42:10 -07:00
|
|
|
}
|
2016-02-16 13:28:11 -08:00
|
|
|
keyBytes, keyId, err = decryptKeyV1(k, auth)
|
2015-05-23 18:42:10 -07:00
|
|
|
} else {
|
|
|
|
k := new(encryptedKeyJSONV3)
|
2016-02-16 13:28:11 -08:00
|
|
|
if err := json.Unmarshal(keyjson, k); err != nil {
|
|
|
|
return nil, err
|
2015-05-23 18:42:10 -07:00
|
|
|
}
|
2016-02-16 13:28:11 -08:00
|
|
|
keyBytes, keyId, err = decryptKeyV3(k, auth)
|
|
|
|
}
|
|
|
|
// Handle any decryption errors and return the key
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-01 00:24:40 -07:00
|
|
|
key := crypto.ToECDSAUnsafe(keyBytes)
|
|
|
|
|
2016-02-16 13:28:11 -08:00
|
|
|
return &Key{
|
|
|
|
Id: uuid.UUID(keyId),
|
2016-03-02 04:57:15 -08:00
|
|
|
Address: crypto.PubkeyToAddress(key.PublicKey),
|
2016-02-16 13:28:11 -08:00
|
|
|
PrivateKey: key,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 18:42:10 -07:00
|
|
|
func decryptKeyV3(keyProtected *encryptedKeyJSONV3, auth string) (keyBytes []byte, keyId []byte, err error) {
|
|
|
|
if keyProtected.Version != version {
|
|
|
|
return nil, nil, fmt.Errorf("Version not supported: %v", keyProtected.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyProtected.Crypto.Cipher != "aes-128-ctr" {
|
|
|
|
return nil, nil, fmt.Errorf("Cipher not supported: %v", keyProtected.Crypto.Cipher)
|
|
|
|
}
|
2015-04-21 08:00:30 -07:00
|
|
|
|
2015-05-23 18:42:10 -07:00
|
|
|
keyId = uuid.Parse(keyProtected.Id)
|
2015-04-21 08:00:30 -07:00
|
|
|
mac, err := hex.DecodeString(keyProtected.Crypto.MAC)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-10 11:30:02 -07:00
|
|
|
iv, err := hex.DecodeString(keyProtected.Crypto.CipherParams.IV)
|
2015-04-21 08:00:30 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cipherText, err := hex.DecodeString(keyProtected.Crypto.CipherText)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2014-12-31 06:39:33 -08:00
|
|
|
|
2015-05-23 18:42:10 -07:00
|
|
|
derivedKey, err := getKDFKey(keyProtected.Crypto, auth)
|
2015-04-15 04:24:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-02 04:57:15 -08:00
|
|
|
calculatedMAC := crypto.Keccak256(derivedKey[16:32], cipherText)
|
2015-05-23 18:42:10 -07:00
|
|
|
if !bytes.Equal(calculatedMAC, mac) {
|
2016-04-01 13:41:47 -07:00
|
|
|
return nil, nil, ErrDecrypt
|
2015-05-23 18:42:10 -07:00
|
|
|
}
|
2015-04-15 04:24:12 -07:00
|
|
|
|
2015-05-23 18:42:10 -07:00
|
|
|
plainText, err := aesCTRXOR(derivedKey[:16], cipherText, iv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return plainText, keyId, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func decryptKeyV1(keyProtected *encryptedKeyJSONV1, auth string) (keyBytes []byte, keyId []byte, err error) {
|
|
|
|
keyId = uuid.Parse(keyProtected.Id)
|
|
|
|
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)
|
2014-12-31 06:39:33 -08:00
|
|
|
if err != nil {
|
2015-01-24 17:07:20 -08:00
|
|
|
return nil, nil, err
|
2014-12-31 06:39:33 -08:00
|
|
|
}
|
2015-04-02 09:15:58 -07:00
|
|
|
|
2016-03-02 04:57:15 -08:00
|
|
|
calculatedMAC := crypto.Keccak256(derivedKey[16:32], cipherText)
|
2015-04-02 09:15:58 -07:00
|
|
|
if !bytes.Equal(calculatedMAC, mac) {
|
2016-04-01 13:41:47 -07:00
|
|
|
return nil, nil, ErrDecrypt
|
2014-12-31 06:39:33 -08:00
|
|
|
}
|
2015-04-02 09:15:58 -07:00
|
|
|
|
2016-03-02 04:57:15 -08:00
|
|
|
plainText, err := aesCBCDecrypt(crypto.Keccak256(derivedKey[:16])[:16], cipherText, iv)
|
2015-04-02 09:15:58 -07:00
|
|
|
if err != nil {
|
2015-01-24 17:07:20 -08:00
|
|
|
return nil, nil, err
|
2015-01-15 08:45:45 -08:00
|
|
|
}
|
2015-04-02 09:15:58 -07:00
|
|
|
return plainText, keyId, err
|
2015-01-15 08:45:45 -08:00
|
|
|
}
|
2015-05-23 18:42:10 -07:00
|
|
|
|
|
|
|
func getKDFKey(cryptoJSON cryptoJSON, auth string) ([]byte, error) {
|
|
|
|
authArray := []byte(auth)
|
|
|
|
salt, err := hex.DecodeString(cryptoJSON.KDFParams["salt"].(string))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dkLen := ensureInt(cryptoJSON.KDFParams["dklen"])
|
|
|
|
|
2017-08-07 04:11:15 -07:00
|
|
|
if cryptoJSON.KDF == keyHeaderKDF {
|
2015-05-23 18:42:10 -07: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" {
|
2016-03-02 15:46:56 -08:00
|
|
|
return nil, fmt.Errorf("Unsupported PBKDF2 PRF: %s", prf)
|
2015-05-23 18:42:10 -07:00
|
|
|
}
|
|
|
|
key := pbkdf2.Key(authArray, salt, c, dkLen, sha256.New)
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
2016-03-02 15:46:56 -08:00
|
|
|
return nil, fmt.Errorf("Unsupported KDF: %s", cryptoJSON.KDF)
|
2015-05-23 18:42:10 -07: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
|
|
|
|
}
|