|
|
@ -143,41 +143,36 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) { |
|
|
|
cipherText := make([]byte, len(toEncrypt)) |
|
|
|
cipherText := make([]byte, len(toEncrypt)) |
|
|
|
AES128CBCEncrypter.CryptBlocks(cipherText, toEncrypt) |
|
|
|
AES128CBCEncrypter.CryptBlocks(cipherText, toEncrypt) |
|
|
|
|
|
|
|
|
|
|
|
paramsJSON := scryptParamsJSON{ |
|
|
|
mac := Sha3(derivedKey[16:32], cipherText) |
|
|
|
N: scryptN, |
|
|
|
|
|
|
|
R: scryptr, |
|
|
|
|
|
|
|
P: scryptp, |
|
|
|
|
|
|
|
DkLen: scryptdkLen, |
|
|
|
|
|
|
|
SaltLen: 32, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
keyHeaderJSON := keyHeaderJSON{ |
|
|
|
scryptParamsJSON := scryptParamsJSON{ |
|
|
|
Version: keyHeaderVersion, |
|
|
|
N: scryptN, |
|
|
|
Kdf: keyHeaderKDF, |
|
|
|
R: scryptr, |
|
|
|
KdfParams: paramsJSON, |
|
|
|
P: scryptp, |
|
|
|
|
|
|
|
DkLen: scryptdkLen, |
|
|
|
|
|
|
|
Salt: hex.EncodeToString(salt), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
keyHeaderJSONStr, err := json.Marshal(keyHeaderJSON) |
|
|
|
cipherParamsJSON := cipherparamsJSON{ |
|
|
|
if err != nil { |
|
|
|
IV: hex.EncodeToString(iv), |
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mac := Sha3(keyHeaderJSONStr, derivedKey[16:32], cipherText) |
|
|
|
cryptoStruct := cryptoJSON{ |
|
|
|
|
|
|
|
Cipher: "aes-128-cbc", |
|
|
|
cipherStruct := cipherJSON{ |
|
|
|
CipherText: hex.EncodeToString(cipherText), |
|
|
|
hex.EncodeToString(mac), |
|
|
|
CipherParams: cipherParamsJSON, |
|
|
|
hex.EncodeToString(salt), |
|
|
|
KDF: "scrypt", |
|
|
|
hex.EncodeToString(iv), |
|
|
|
KDFParams: scryptParamsJSON, |
|
|
|
keyHeaderJSON, |
|
|
|
MAC: hex.EncodeToString(mac), |
|
|
|
hex.EncodeToString(cipherText), |
|
|
|
Version: "1", |
|
|
|
} |
|
|
|
} |
|
|
|
keyStruct := encryptedKeyJSON{ |
|
|
|
encryptedKeyJSON := encryptedKeyJSON{ |
|
|
|
version, |
|
|
|
|
|
|
|
key.Id.String(), |
|
|
|
|
|
|
|
hex.EncodeToString(key.Address[:]), |
|
|
|
hex.EncodeToString(key.Address[:]), |
|
|
|
cipherStruct, |
|
|
|
cryptoStruct, |
|
|
|
|
|
|
|
key.Id.String(), |
|
|
|
|
|
|
|
version, |
|
|
|
} |
|
|
|
} |
|
|
|
keyJSON, err := json.Marshal(keyStruct) |
|
|
|
keyJSON, err := json.Marshal(encryptedKeyJSON) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -212,33 +207,25 @@ func DecryptKey(ks keyStorePassphrase, keyAddr common.Address, auth string) (key |
|
|
|
return nil, nil, err |
|
|
|
return nil, nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
salt, err := hex.DecodeString(keyProtected.Crypto.Salt) |
|
|
|
iv, err := hex.DecodeString(keyProtected.Crypto.CipherParams.IV) |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iv, err := hex.DecodeString(keyProtected.Crypto.IV) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, nil, err |
|
|
|
return nil, nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
keyHeader := keyProtected.Crypto.KeyHeader |
|
|
|
|
|
|
|
cipherText, err := hex.DecodeString(keyProtected.Crypto.CipherText) |
|
|
|
cipherText, err := hex.DecodeString(keyProtected.Crypto.CipherText) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, nil, err |
|
|
|
return nil, nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// used in MAC
|
|
|
|
salt, err := hex.DecodeString(keyProtected.Crypto.KDFParams.Salt) |
|
|
|
keyHeaderJSONStr, err := json.Marshal(keyHeader) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, nil, err |
|
|
|
return nil, nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO: make this more generic when we support different KDF params / key versions
|
|
|
|
n := keyProtected.Crypto.KDFParams.N |
|
|
|
n := keyHeader.KdfParams.N |
|
|
|
r := keyProtected.Crypto.KDFParams.R |
|
|
|
r := keyHeader.KdfParams.R |
|
|
|
p := keyProtected.Crypto.KDFParams.P |
|
|
|
p := keyHeader.KdfParams.P |
|
|
|
dkLen := keyProtected.Crypto.KDFParams.DkLen |
|
|
|
dkLen := keyHeader.KdfParams.DkLen |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
authArray := []byte(auth) |
|
|
|
authArray := []byte(auth) |
|
|
|
derivedKey, err := scrypt.Key(authArray, salt, n, r, p, dkLen) |
|
|
|
derivedKey, err := scrypt.Key(authArray, salt, n, r, p, dkLen) |
|
|
@ -246,7 +233,7 @@ func DecryptKey(ks keyStorePassphrase, keyAddr common.Address, auth string) (key |
|
|
|
return nil, nil, err |
|
|
|
return nil, nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
calculatedMAC := Sha3(keyHeaderJSONStr, derivedKey[16:32], cipherText) |
|
|
|
calculatedMAC := Sha3(derivedKey[16:32], cipherText) |
|
|
|
if !bytes.Equal(calculatedMAC, mac) { |
|
|
|
if !bytes.Equal(calculatedMAC, mac) { |
|
|
|
err = errors.New("Decryption failed: MAC mismatch") |
|
|
|
err = errors.New("Decryption failed: MAC mismatch") |
|
|
|
return nil, nil, err |
|
|
|
return nil, nil, err |
|
|
|