2014-12-31 14:39:33 +00:00
|
|
|
/*
|
|
|
|
This file is part of go-ethereum
|
|
|
|
|
|
|
|
go-ethereum 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.
|
|
|
|
|
|
|
|
go-ethereum 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @authors
|
|
|
|
* Gustav Simonsson <gustav.simonsson@gmail.com>
|
|
|
|
* @date 2015
|
|
|
|
*
|
|
|
|
*/
|
2015-02-17 12:05:58 +00:00
|
|
|
|
2014-12-31 14:39:33 +00:00
|
|
|
/*
|
|
|
|
|
2015-01-15 18:58:38 +00: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 14:39:33 +00:00
|
|
|
|
|
|
|
Cryptography:
|
|
|
|
|
2015-05-13 16:33:31 +00:00
|
|
|
1. Encryption key is first 16 bytes of scrypt derived key
|
|
|
|
from user passphrase. Scrypt parameters
|
2014-12-31 14:39:33 +00:00
|
|
|
(work factors) [1][2] are defined as constants below.
|
2015-04-02 16:15:58 +00:00
|
|
|
2. Scrypt salt is 32 random bytes from CSPRNG.
|
2015-05-13 16:33:31 +00:00
|
|
|
It's stored in plain next in the key file.
|
|
|
|
3. MAC is SHA3-256 of concatenation of ciphertext and
|
|
|
|
last 16 bytes of scrypt derived key.
|
2015-04-02 16:15:58 +00:00
|
|
|
4. Plaintext is the EC private key bytes.
|
|
|
|
5. Encryption algo is AES 128 CBC [3][4]
|
|
|
|
6. CBC IV is 16 random bytes from CSPRNG.
|
2015-05-13 16:33:31 +00:00
|
|
|
It's stored in plain next in the key file.
|
2014-12-31 14:39:33 +00:00
|
|
|
7. Plaintext padding is PKCS #7 [5][6]
|
|
|
|
|
|
|
|
Encoding:
|
|
|
|
|
2015-05-13 16:33:31 +00:00
|
|
|
1. On disk, the ciphertext, MAC, salt and IV are encoded in a JSON object.
|
2014-12-31 14:39:33 +00:00
|
|
|
cat a key file to see the structure.
|
2015-01-15 18:58:38 +00:00
|
|
|
2. byte arrays are base64 JSON strings.
|
2014-12-31 14:39:33 +00:00
|
|
|
3. The EC private key bytes are in uncompressed form [7].
|
|
|
|
They are a big-endian byte slice of the absolute value of D [8][9].
|
|
|
|
|
|
|
|
References:
|
|
|
|
|
|
|
|
1. http://www.tarsnap.com/scrypt/scrypt-slides.pdf
|
|
|
|
2. http://stackoverflow.com/questions/11126315/what-are-optimal-scrypt-work-factors
|
|
|
|
3. http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
|
|
|
|
4. http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher-block_chaining_.28CBC.29
|
|
|
|
5. https://leanpub.com/gocrypto/read#leanpub-auto-block-cipher-modes
|
|
|
|
6. http://tools.ietf.org/html/rfc2315
|
|
|
|
7. http://bitcoin.stackexchange.com/questions/3059/what-is-a-compressed-bitcoin-key
|
|
|
|
8. http://golang.org/pkg/crypto/ecdsa/#PrivateKey
|
|
|
|
9. https://golang.org/pkg/math/big/#Int.Bytes
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2015-04-21 15:00:30 +00:00
|
|
|
"encoding/hex"
|
2014-12-31 14:39:33 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2015-01-15 16:45:45 +00:00
|
|
|
"io"
|
2014-12-31 14:39:33 +00:00
|
|
|
"os"
|
2015-05-12 12:24:11 +00:00
|
|
|
"path/filepath"
|
2015-02-17 12:05:58 +00:00
|
|
|
|
|
|
|
"code.google.com/p/go-uuid/uuid"
|
2015-04-02 19:14:25 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-02-17 12:05:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/randentropy"
|
|
|
|
"golang.org/x/crypto/scrypt"
|
2014-12-31 14:39:33 +00:00
|
|
|
)
|
|
|
|
|
2015-01-07 15:06:26 +00:00
|
|
|
const (
|
2015-04-15 11:24:12 +00:00
|
|
|
keyHeaderVersion = "1"
|
|
|
|
keyHeaderKDF = "scrypt"
|
2015-01-07 15:06:26 +00:00
|
|
|
// 2^18 / 8 / 1 uses 256MB memory and approx 1s CPU time on a modern CPU.
|
|
|
|
scryptN = 1 << 18
|
|
|
|
scryptr = 8
|
|
|
|
scryptp = 1
|
|
|
|
scryptdkLen = 32
|
|
|
|
)
|
2014-12-31 14:39:33 +00:00
|
|
|
|
2015-01-07 15:06:26 +00:00
|
|
|
type keyStorePassphrase struct {
|
2014-12-31 14:39:33 +00:00
|
|
|
keysDirPath string
|
|
|
|
}
|
|
|
|
|
2015-01-07 15:06:26 +00:00
|
|
|
func NewKeyStorePassphrase(path string) KeyStore2 {
|
2015-01-19 21:12:22 +00:00
|
|
|
return &keyStorePassphrase{path}
|
2015-01-07 15:06:26 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 16:45:45 +00:00
|
|
|
func (ks keyStorePassphrase) GenerateNewKey(rand io.Reader, auth string) (key *Key, err error) {
|
|
|
|
return GenerateNewKeyDefault(ks, rand, auth)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 19:14:25 +00:00
|
|
|
func (ks keyStorePassphrase) GetKey(keyAddr common.Address, auth string) (key *Key, err error) {
|
2015-01-25 01:07:20 +00:00
|
|
|
keyBytes, keyId, err := DecryptKey(ks, keyAddr, auth)
|
2015-01-07 15:06:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-19 19:24:30 +00:00
|
|
|
key = &Key{
|
2015-01-25 01:07:20 +00:00
|
|
|
Id: uuid.UUID(keyId),
|
|
|
|
Address: keyAddr,
|
2015-01-19 21:12:22 +00:00
|
|
|
PrivateKey: ToECDSA(keyBytes),
|
2015-01-19 19:24:30 +00:00
|
|
|
}
|
2015-01-07 15:06:26 +00:00
|
|
|
return key, err
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 19:14:25 +00:00
|
|
|
func (ks keyStorePassphrase) GetKeyAddresses() (addresses []common.Address, err error) {
|
2015-01-25 01:07:20 +00:00
|
|
|
return GetKeyAddresses(ks.keysDirPath)
|
|
|
|
}
|
|
|
|
|
2015-01-07 15:06:26 +00:00
|
|
|
func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) {
|
2014-12-31 14:39:33 +00:00
|
|
|
authArray := []byte(auth)
|
2015-04-02 15:02:56 +00:00
|
|
|
salt := randentropy.GetEntropyCSPRNG(32)
|
2014-12-31 14:39:33 +00:00
|
|
|
derivedKey, err := scrypt.Key(authArray, salt, scryptN, scryptr, scryptp, scryptdkLen)
|
|
|
|
if err != nil {
|
2015-01-07 15:06:26 +00:00
|
|
|
return err
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 16:15:58 +00:00
|
|
|
encryptKey := Sha3(derivedKey[:16])[:16]
|
|
|
|
|
2014-12-31 14:39:33 +00:00
|
|
|
keyBytes := FromECDSA(key.PrivateKey)
|
2015-04-02 16:15:58 +00:00
|
|
|
toEncrypt := PKCS7Pad(keyBytes)
|
2014-12-31 14:39:33 +00:00
|
|
|
|
2015-04-02 16:15:58 +00:00
|
|
|
AES128Block, err := aes.NewCipher(encryptKey)
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-07 15:06:26 +00:00
|
|
|
return err
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 15:02:56 +00:00
|
|
|
iv := randentropy.GetEntropyCSPRNG(aes.BlockSize) // 16
|
2015-04-02 16:15:58 +00:00
|
|
|
AES128CBCEncrypter := cipher.NewCBCEncrypter(AES128Block, iv)
|
2014-12-31 14:39:33 +00:00
|
|
|
cipherText := make([]byte, len(toEncrypt))
|
2015-04-02 16:15:58 +00:00
|
|
|
AES128CBCEncrypter.CryptBlocks(cipherText, toEncrypt)
|
|
|
|
|
2015-05-10 18:30:02 +00:00
|
|
|
mac := Sha3(derivedKey[16:32], cipherText)
|
2015-04-15 11:24:12 +00:00
|
|
|
|
2015-05-10 18:30:02 +00:00
|
|
|
scryptParamsJSON := scryptParamsJSON{
|
|
|
|
N: scryptN,
|
|
|
|
R: scryptr,
|
|
|
|
P: scryptp,
|
|
|
|
DkLen: scryptdkLen,
|
|
|
|
Salt: hex.EncodeToString(salt),
|
2015-04-15 11:24:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-10 18:30:02 +00:00
|
|
|
cipherParamsJSON := cipherparamsJSON{
|
|
|
|
IV: hex.EncodeToString(iv),
|
2015-04-15 11:24:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-10 18:30:02 +00:00
|
|
|
cryptoStruct := cryptoJSON{
|
|
|
|
Cipher: "aes-128-cbc",
|
|
|
|
CipherText: hex.EncodeToString(cipherText),
|
|
|
|
CipherParams: cipherParamsJSON,
|
|
|
|
KDF: "scrypt",
|
|
|
|
KDFParams: scryptParamsJSON,
|
|
|
|
MAC: hex.EncodeToString(mac),
|
|
|
|
Version: "1",
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
2015-05-10 18:30:02 +00:00
|
|
|
encryptedKeyJSON := encryptedKeyJSON{
|
2015-04-21 15:00:30 +00:00
|
|
|
hex.EncodeToString(key.Address[:]),
|
2015-05-10 18:30:02 +00:00
|
|
|
cryptoStruct,
|
|
|
|
key.Id.String(),
|
|
|
|
version,
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
2015-05-10 18:30:02 +00:00
|
|
|
keyJSON, err := json.Marshal(encryptedKeyJSON)
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-07 15:06:26 +00:00
|
|
|
return err
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-25 01:07:20 +00:00
|
|
|
return WriteKeyFile(key.Address, ks.keysDirPath, keyJSON)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 19:14:25 +00:00
|
|
|
func (ks keyStorePassphrase) DeleteKey(keyAddr common.Address, auth string) (err error) {
|
2014-12-31 14:39:33 +00:00
|
|
|
// only delete if correct passphrase is given
|
2015-01-25 01:07:20 +00:00
|
|
|
_, _, err = DecryptKey(ks, keyAddr, auth)
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-07 15:06:26 +00:00
|
|
|
return err
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 15:00:30 +00:00
|
|
|
keyDirPath := filepath.Join(ks.keysDirPath, hex.EncodeToString(keyAddr[:]))
|
2015-01-07 15:06:26 +00:00
|
|
|
return os.RemoveAll(keyDirPath)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 19:14:25 +00:00
|
|
|
func DecryptKey(ks keyStorePassphrase, keyAddr common.Address, auth string) (keyBytes []byte, keyId []byte, err error) {
|
2015-01-25 01:07:20 +00:00
|
|
|
fileContent, err := GetKeyFile(ks.keysDirPath, keyAddr)
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-25 01:07:20 +00:00
|
|
|
return nil, nil, err
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 16:45:45 +00:00
|
|
|
keyProtected := new(encryptedKeyJSON)
|
2014-12-31 14:39:33 +00:00
|
|
|
err = json.Unmarshal(fileContent, keyProtected)
|
|
|
|
|
2015-04-21 15:00:30 +00:00
|
|
|
keyId = uuid.Parse(keyProtected.Id)
|
|
|
|
|
|
|
|
mac, err := hex.DecodeString(keyProtected.Crypto.MAC)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-10 18:30:02 +00:00
|
|
|
iv, err := hex.DecodeString(keyProtected.Crypto.CipherParams.IV)
|
2015-04-21 15:00:30 +00: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 14:39:33 +00:00
|
|
|
|
2015-05-10 18:30:02 +00:00
|
|
|
salt, err := hex.DecodeString(keyProtected.Crypto.KDFParams.Salt)
|
2015-04-15 11:24:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-10 18:30:02 +00:00
|
|
|
n := keyProtected.Crypto.KDFParams.N
|
|
|
|
r := keyProtected.Crypto.KDFParams.R
|
|
|
|
p := keyProtected.Crypto.KDFParams.P
|
|
|
|
dkLen := keyProtected.Crypto.KDFParams.DkLen
|
2015-04-15 11:24:12 +00:00
|
|
|
|
2014-12-31 14:39:33 +00:00
|
|
|
authArray := []byte(auth)
|
2015-04-15 11:24:12 +00:00
|
|
|
derivedKey, err := scrypt.Key(authArray, salt, n, r, p, dkLen)
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-25 01:07:20 +00:00
|
|
|
return nil, nil, err
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
2015-04-02 16:15:58 +00:00
|
|
|
|
2015-05-10 18:30:02 +00:00
|
|
|
calculatedMAC := Sha3(derivedKey[16:32], cipherText)
|
2015-04-02 16:15:58 +00:00
|
|
|
if !bytes.Equal(calculatedMAC, mac) {
|
|
|
|
err = errors.New("Decryption failed: MAC mismatch")
|
2015-01-25 01:07:20 +00:00
|
|
|
return nil, nil, err
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
2015-04-02 16:15:58 +00:00
|
|
|
|
|
|
|
plainText, err := aesCBCDecrypt(Sha3(derivedKey[:16])[:16], cipherText, iv)
|
|
|
|
if err != nil {
|
2015-01-25 01:07:20 +00:00
|
|
|
return nil, nil, err
|
2015-01-15 16:45:45 +00:00
|
|
|
}
|
2015-04-02 16:15:58 +00:00
|
|
|
return plainText, keyId, err
|
2015-01-15 16:45:45 +00:00
|
|
|
}
|