From 6b23094cff77d7e485e0a2ae5698884f63c87ce7 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Thu, 2 Apr 2015 18:15:58 +0200 Subject: Improve key store passphrase crypto * Change MAC-then-Encrypt to Encrypt-then-MAC * Change AES256 to AES128 * Use first 16 bytes of KDF derived key for AES and remaining 16 for MAC --- crypto/key.go | 1 + 1 file changed, 1 insertion(+) (limited to 'crypto/key.go') diff --git a/crypto/key.go b/crypto/key.go index 0b84bfec1..654d9e83d 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -48,6 +48,7 @@ type plainKeyJSON struct { } type cipherJSON struct { + MAC []byte Salt []byte IV []byte CipherText []byte -- cgit v1.2.3 From da9fe951da4005761a014316c46771d628dc058e Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Thu, 2 Apr 2015 21:14:25 +0200 Subject: Use common.Address type for accounts.Address --- crypto/key.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'crypto/key.go') diff --git a/crypto/key.go b/crypto/key.go index 654d9e83d..5e1f3637e 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -30,12 +30,13 @@ import ( "io" "code.google.com/p/go-uuid/uuid" + "github.com/ethereum/go-ethereum/common" ) type Key struct { Id uuid.UUID // Version 4 "random" for unique id not derived from key data // to simplify lookups we also store the address - Address []byte + Address common.Address // we only store privkey as pubkey/address can be derived from it // privkey in this struct is always in plaintext PrivateKey *ecdsa.PrivateKey @@ -63,7 +64,7 @@ type encryptedKeyJSON struct { func (k *Key) MarshalJSON() (j []byte, err error) { jStruct := plainKeyJSON{ k.Id, - k.Address, + k.Address.Bytes(), FromECDSA(k.PrivateKey), } j, err = json.Marshal(jStruct) @@ -80,7 +81,7 @@ func (k *Key) UnmarshalJSON(j []byte) (err error) { u := new(uuid.UUID) *u = keyJSON.Id k.Id = *u - k.Address = keyJSON.Address + k.Address = common.BytesToAddress(keyJSON.Address) k.PrivateKey = ToECDSA(keyJSON.PrivateKey) return err @@ -90,7 +91,7 @@ func NewKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key { id := uuid.NewRandom() key := &Key{ Id: id, - Address: PubkeyToAddress(privateKeyECDSA.PublicKey), + Address: common.BytesToAddress(PubkeyToAddress(privateKeyECDSA.PublicKey)), PrivateKey: privateKeyECDSA, } return key -- cgit v1.2.3 From 29a5a92d13cad45794c6e42cb97260a9ab9900ab Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Wed, 15 Apr 2015 13:24:12 +0200 Subject: Add key header to encrypted keys * Add key header containing key version, kdf and kdf params * Store key header as JSON in the key file * Read in KDF params from key header * Include key header in MAC calculation and MAC verification --- crypto/key.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'crypto/key.go') diff --git a/crypto/key.go b/crypto/key.go index 5e1f3637e..067a5a294 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -48,17 +48,32 @@ type plainKeyJSON struct { PrivateKey []byte } +type encryptedKeyJSON struct { + Id []byte + Address []byte + Crypto cipherJSON +} + type cipherJSON struct { MAC []byte Salt []byte IV []byte + KeyHeader keyHeaderJSON CipherText []byte } -type encryptedKeyJSON struct { - Id []byte - Address []byte - Crypto cipherJSON +type keyHeaderJSON struct { + Version string + Kdf string + KdfParams scryptParamsJSON // TODO: make more generic? +} + +type scryptParamsJSON struct { + N int + R int + P int + DkLen int + SaltLen int } func (k *Key) MarshalJSON() (j []byte, err error) { -- cgit v1.2.3 From cd88295f5ac360aaaf63be94ae09f202a4b8630f Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Wed, 15 Apr 2015 15:47:00 +0200 Subject: Add key header to unencrypted key file --- crypto/key.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'crypto/key.go') diff --git a/crypto/key.go b/crypto/key.go index 067a5a294..0f36a7f6b 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -45,27 +45,28 @@ type Key struct { type plainKeyJSON struct { Id []byte Address []byte + KeyHeader keyHeaderJSON PrivateKey []byte } type encryptedKeyJSON struct { - Id []byte - Address []byte - Crypto cipherJSON + Id []byte + Address []byte + KeyHeader keyHeaderJSON + Crypto cipherJSON } type cipherJSON struct { MAC []byte Salt []byte IV []byte - KeyHeader keyHeaderJSON CipherText []byte } type keyHeaderJSON struct { Version string Kdf string - KdfParams scryptParamsJSON // TODO: make more generic? + KdfParams *scryptParamsJSON // TODO: make more generic? } type scryptParamsJSON struct { @@ -77,9 +78,15 @@ type scryptParamsJSON struct { } func (k *Key) MarshalJSON() (j []byte, err error) { + keyHeader := keyHeaderJSON{ + Version: "1", + Kdf: "", + KdfParams: nil, + } jStruct := plainKeyJSON{ k.Id, k.Address.Bytes(), + keyHeader, FromECDSA(k.PrivateKey), } j, err = json.Marshal(jStruct) -- cgit v1.2.3 From 313eec33ad7add82bfdd00e3a076091fa990c799 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Tue, 21 Apr 2015 15:52:10 +0200 Subject: Revert "Add key header to unencrypted key file" This reverts commit a94d4ba0b53c4558ab838aaed635a2ff66ddfa53. --- crypto/key.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'crypto/key.go') diff --git a/crypto/key.go b/crypto/key.go index 0f36a7f6b..067a5a294 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -45,28 +45,27 @@ type Key struct { type plainKeyJSON struct { Id []byte Address []byte - KeyHeader keyHeaderJSON PrivateKey []byte } type encryptedKeyJSON struct { - Id []byte - Address []byte - KeyHeader keyHeaderJSON - Crypto cipherJSON + Id []byte + Address []byte + Crypto cipherJSON } type cipherJSON struct { MAC []byte Salt []byte IV []byte + KeyHeader keyHeaderJSON CipherText []byte } type keyHeaderJSON struct { Version string Kdf string - KdfParams *scryptParamsJSON // TODO: make more generic? + KdfParams scryptParamsJSON // TODO: make more generic? } type scryptParamsJSON struct { @@ -78,15 +77,9 @@ type scryptParamsJSON struct { } func (k *Key) MarshalJSON() (j []byte, err error) { - keyHeader := keyHeaderJSON{ - Version: "1", - Kdf: "", - KdfParams: nil, - } jStruct := plainKeyJSON{ k.Id, k.Address.Bytes(), - keyHeader, FromECDSA(k.PrivateKey), } j, err = json.Marshal(jStruct) -- cgit v1.2.3 From f98e002d9851b8df53a09a0e3189f2e8fdec48df Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Tue, 21 Apr 2015 17:00:30 +0200 Subject: Address pull request comments; key header and hex encoding * Remove key header from unencrypted key file format and replace it with a version field * Change encoding of bytes in key files from base64 to hex --- crypto/key.go | 52 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 17 deletions(-) (limited to 'crypto/key.go') diff --git a/crypto/key.go b/crypto/key.go index 067a5a294..1af69d795 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -26,6 +26,7 @@ package crypto import ( "bytes" "crypto/ecdsa" + "encoding/hex" "encoding/json" "io" @@ -33,6 +34,10 @@ import ( "github.com/ethereum/go-ethereum/common" ) +const ( + version = "1" +) + type Key struct { Id uuid.UUID // Version 4 "random" for unique id not derived from key data // to simplify lookups we also store the address @@ -43,29 +48,31 @@ type Key struct { } type plainKeyJSON struct { - Id []byte - Address []byte - PrivateKey []byte + Version string + Id string + Address string + PrivateKey string } type encryptedKeyJSON struct { - Id []byte - Address []byte + Version string + Id string + Address string Crypto cipherJSON } type cipherJSON struct { - MAC []byte - Salt []byte - IV []byte + MAC string + Salt string + IV string KeyHeader keyHeaderJSON - CipherText []byte + CipherText string } type keyHeaderJSON struct { Version string Kdf string - KdfParams scryptParamsJSON // TODO: make more generic? + KdfParams scryptParamsJSON } type scryptParamsJSON struct { @@ -78,9 +85,10 @@ type scryptParamsJSON struct { func (k *Key) MarshalJSON() (j []byte, err error) { jStruct := plainKeyJSON{ - k.Id, - k.Address.Bytes(), - FromECDSA(k.PrivateKey), + version, + k.Id.String(), + hex.EncodeToString(k.Address[:]), + hex.EncodeToString(FromECDSA(k.PrivateKey)), } j, err = json.Marshal(jStruct) return j, err @@ -94,12 +102,22 @@ func (k *Key) UnmarshalJSON(j []byte) (err error) { } u := new(uuid.UUID) - *u = keyJSON.Id + *u = uuid.Parse(keyJSON.Id) k.Id = *u - k.Address = common.BytesToAddress(keyJSON.Address) - k.PrivateKey = ToECDSA(keyJSON.PrivateKey) + addr, err := hex.DecodeString(keyJSON.Address) + if err != nil { + return err + } + + privkey, err := hex.DecodeString(keyJSON.PrivateKey) + if err != nil { + return err + } + + k.Address = common.BytesToAddress(addr) + k.PrivateKey = ToECDSA(privkey) - return err + return nil } func NewKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key { -- cgit v1.2.3 From 2c1b0ff17e020f300ed9d5a5a244f59b4febfe66 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Sun, 10 May 2015 20:30:02 +0200 Subject: Update key store to new spec but keep address field for now * Also fix address types post-rebase --- crypto/key.go | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'crypto/key.go') diff --git a/crypto/key.go b/crypto/key.go index 1af69d795..0c5ce4254 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -48,47 +48,47 @@ type Key struct { } type plainKeyJSON struct { - Version string - Id string - Address string - PrivateKey string + Address string `json:"address"` + PrivateKey string `json:"privatekey"` + Id string `json:"id"` + Version string `json:"version"` } type encryptedKeyJSON struct { - Version string - Id string - Address string - Crypto cipherJSON + Address string `json:"address"` + Crypto cryptoJSON + Id string `json:"id"` + Version string `json:"version"` } -type cipherJSON struct { - MAC string - Salt string - IV string - KeyHeader keyHeaderJSON - CipherText string +type cryptoJSON struct { + Cipher string `json:"cipher"` + CipherText string `json:"ciphertext"` + CipherParams cipherparamsJSON `json:"cipherparams"` + KDF string `json:"kdf"` + KDFParams scryptParamsJSON `json:"kdfparams"` + MAC string `json:"mac"` + Version string `json:"version"` } -type keyHeaderJSON struct { - Version string - Kdf string - KdfParams scryptParamsJSON +type cipherparamsJSON struct { + IV string `json:"iv"` } type scryptParamsJSON struct { - N int - R int - P int - DkLen int - SaltLen int + N int `json:"n"` + R int `json:"r"` + P int `json:"p"` + DkLen int `json:"dklen"` + Salt string `json:"salt"` } func (k *Key) MarshalJSON() (j []byte, err error) { jStruct := plainKeyJSON{ - version, - k.Id.String(), hex.EncodeToString(k.Address[:]), hex.EncodeToString(FromECDSA(k.PrivateKey)), + k.Id.String(), + version, } j, err = json.Marshal(jStruct) return j, err -- cgit v1.2.3