aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/key_store_passphrase.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2016-02-17 07:15:35 +0800
committerPéter Szilágyi <peterke@gmail.com>2016-02-24 22:28:25 +0800
commit45d08a8aa7fdc3aecdcf524f0c19946b1e068e1a (patch)
treecc4b1c130ef44fc47117e7e83e037f84b0cf4da5 /crypto/key_store_passphrase.go
parent0969b35eec7b1e4a514b9fb3d659706d9f09ee2a (diff)
downloaddexon-45d08a8aa7fdc3aecdcf524f0c19946b1e068e1a.tar
dexon-45d08a8aa7fdc3aecdcf524f0c19946b1e068e1a.tar.gz
dexon-45d08a8aa7fdc3aecdcf524f0c19946b1e068e1a.tar.bz2
dexon-45d08a8aa7fdc3aecdcf524f0c19946b1e068e1a.tar.lz
dexon-45d08a8aa7fdc3aecdcf524f0c19946b1e068e1a.tar.xz
dexon-45d08a8aa7fdc3aecdcf524f0c19946b1e068e1a.tar.zst
dexon-45d08a8aa7fdc3aecdcf524f0c19946b1e068e1a.zip
crypto: expose key encryption method, add ping-pong test
Diffstat (limited to 'crypto/key_store_passphrase.go')
-rw-r--r--crypto/key_store_passphrase.go38
1 files changed, 20 insertions, 18 deletions
diff --git a/crypto/key_store_passphrase.go b/crypto/key_store_passphrase.go
index 2700b0f1c..19e77de91 100644
--- a/crypto/key_store_passphrase.go
+++ b/crypto/key_store_passphrase.go
@@ -61,12 +61,10 @@ type keyStorePassphrase struct {
keysDirPath string
scryptN int
scryptP int
- scryptR int
- scryptDKLen int
}
func NewKeyStorePassphrase(path string, scryptN int, scryptP int) KeyStore {
- return &keyStorePassphrase{path, scryptN, scryptP, scryptR, scryptDKLen}
+ return &keyStorePassphrase{path, scryptN, scryptP}
}
func (ks keyStorePassphrase) GenerateNewKey(rand io.Reader, auth string) (key *Key, err error) {
@@ -85,12 +83,22 @@ func (ks keyStorePassphrase) GetKeyAddresses() (addresses []common.Address, err
return getKeyAddresses(ks.keysDirPath)
}
-func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) {
+func (ks keyStorePassphrase) StoreKey(key *Key, auth string) error {
+ keyjson, err := EncryptKey(key, auth, ks.scryptN, ks.scryptP)
+ if err != nil {
+ return err
+ }
+ return writeKeyFile(key.Address, ks.keysDirPath, keyjson)
+}
+
+// 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) {
authArray := []byte(auth)
salt := randentropy.GetEntropyCSPRNG(32)
- derivedKey, err := scrypt.Key(authArray, salt, ks.scryptN, ks.scryptR, ks.scryptP, ks.scryptDKLen)
+ derivedKey, err := scrypt.Key(authArray, salt, scryptN, scryptR, scryptP, scryptDKLen)
if err != nil {
- return err
+ return nil, err
}
encryptKey := derivedKey[:16]
keyBytes := FromECDSA(key.PrivateKey)
@@ -98,16 +106,15 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) {
iv := randentropy.GetEntropyCSPRNG(aes.BlockSize) // 16
cipherText, err := aesCTRXOR(encryptKey, keyBytes, iv)
if err != nil {
- return err
+ return nil, err
}
-
mac := Keccak256(derivedKey[16:32], cipherText)
scryptParamsJSON := make(map[string]interface{}, 5)
- scryptParamsJSON["n"] = ks.scryptN
- scryptParamsJSON["r"] = ks.scryptR
- scryptParamsJSON["p"] = ks.scryptP
- scryptParamsJSON["dklen"] = ks.scryptDKLen
+ scryptParamsJSON["n"] = scryptN
+ scryptParamsJSON["r"] = scryptR
+ scryptParamsJSON["p"] = scryptP
+ scryptParamsJSON["dklen"] = scryptDKLen
scryptParamsJSON["salt"] = hex.EncodeToString(salt)
cipherParamsJSON := cipherparamsJSON{
@@ -128,12 +135,7 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) {
key.Id.String(),
version,
}
- keyJSON, err := json.Marshal(encryptedKeyJSONV3)
- if err != nil {
- return err
- }
-
- return writeKeyFile(key.Address, ks.keysDirPath, keyJSON)
+ return json.Marshal(encryptedKeyJSONV3)
}
func (ks keyStorePassphrase) DeleteKey(keyAddr common.Address, auth string) error {