diff options
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/crypto.go | 112 | ||||
-rw-r--r-- | crypto/key.go | 30 | ||||
-rw-r--r-- | crypto/key_store_passphrase.go | 92 | ||||
-rw-r--r-- | crypto/key_store_plain.go | 49 | ||||
-rw-r--r-- | crypto/key_store_test.go | 31 |
5 files changed, 210 insertions, 104 deletions
diff --git a/crypto/crypto.go b/crypto/crypto.go index 93453b91c..d56b9112f 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -1,12 +1,20 @@ package crypto import ( + "crypto/aes" + "crypto/cipher" "crypto/ecdsa" "crypto/elliptic" "crypto/rand" "crypto/sha256" "fmt" + "encoding/hex" + "encoding/json" + "errors" + + "code.google.com/p/go-uuid/uuid" + "code.google.com/p/go.crypto/pbkdf2" "code.google.com/p/go.crypto/ripemd160" "github.com/ethereum/go-ethereum/crypto/secp256k1" "github.com/ethereum/go-ethereum/crypto/sha3" @@ -84,7 +92,7 @@ func ToECDSAPub(pub []byte) *ecdsa.PublicKey { } func FromECDSAPub(pub *ecdsa.PublicKey) []byte { - if pub == nil { + if pub == nil || pub.X == nil || pub.Y == nil { return nil } return elliptic.Marshal(S256(), pub.X, pub.Y) @@ -118,3 +126,105 @@ func Decrypt(prv *ecdsa.PrivateKey, ct []byte) ([]byte, error) { key := ecies.ImportECDSA(prv) return key.Decrypt(rand.Reader, ct, nil, nil) } + +// creates a Key and stores that in the given KeyStore by decrypting a presale key JSON +func ImportPreSaleKey(keyStore KeyStore2, keyJSON []byte, password string) (*Key, error) { + key, err := decryptPreSaleKey(keyJSON, password) + if err != nil { + return nil, err + } + key.Id = uuid.NewRandom() + err = keyStore.StoreKey(key, password) + return key, err +} + +func decryptPreSaleKey(fileContent []byte, password string) (key *Key, err error) { + preSaleKeyStruct := struct { + EncSeed string + EthAddr string + Email string + BtcAddr string + }{} + err = json.Unmarshal(fileContent, &preSaleKeyStruct) + if err != nil { + return nil, err + } + encSeedBytes, err := hex.DecodeString(preSaleKeyStruct.EncSeed) + iv := encSeedBytes[:16] + cipherText := encSeedBytes[16:] + /* + See https://github.com/ethereum/pyethsaletool + + pyethsaletool generates the encryption key from password by + 2000 rounds of PBKDF2 with HMAC-SHA-256 using password as salt (:(). + 16 byte key length within PBKDF2 and resulting key is used as AES key + */ + passBytes := []byte(password) + derivedKey := pbkdf2.Key(passBytes, passBytes, 2000, 16, sha256.New) + plainText, err := aesCBCDecrypt(derivedKey, cipherText, iv) + ethPriv := Sha3(plainText) + ecKey := ToECDSA(ethPriv) + key = &Key{ + Id: nil, + Address: PubkeyToAddress(ecKey.PublicKey), + PrivateKey: ecKey, + } + derivedAddr := ethutil.Bytes2Hex(key.Address) + expectedAddr := preSaleKeyStruct.EthAddr + if derivedAddr != expectedAddr { + err = errors.New("decrypted addr not equal to expected addr") + } + return key, err +} + +func aesCBCDecrypt(key []byte, cipherText []byte, iv []byte) (plainText []byte, err error) { + aesBlock, err := aes.NewCipher(key) + if err != nil { + return plainText, err + } + decrypter := cipher.NewCBCDecrypter(aesBlock, iv) + paddedPlainText := make([]byte, len(cipherText)) + decrypter.CryptBlocks(paddedPlainText, cipherText) + plainText = PKCS7Unpad(paddedPlainText) + if plainText == nil { + err = errors.New("Decryption failed: PKCS7Unpad failed after decryption") + } + return plainText, err +} + +// From https://leanpub.com/gocrypto/read#leanpub-auto-block-cipher-modes +func PKCS7Pad(in []byte) []byte { + padding := 16 - (len(in) % 16) + if padding == 0 { + padding = 16 + } + for i := 0; i < padding; i++ { + in = append(in, byte(padding)) + } + return in +} + +func PKCS7Unpad(in []byte) []byte { + if len(in) == 0 { + return nil + } + + padding := in[len(in)-1] + if int(padding) > len(in) || padding > aes.BlockSize { + return nil + } else if padding == 0 { + return nil + } + + for i := len(in) - 1; i > len(in)-int(padding)-1; i-- { + if in[i] != padding { + return nil + } + } + return in[:len(in)-int(padding)] +} + +func PubkeyToAddress(p ecdsa.PublicKey) []byte { + pubBytes := FromECDSAPub(&p) + return Sha3(pubBytes[1:])[12:] +} diff --git a/crypto/key.go b/crypto/key.go index d371ad4dc..b9ad34f47 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -33,7 +33,9 @@ import ( ) type Key struct { - Id *uuid.UUID // Version 4 "random" for unique id not derived from key data + Id uuid.UUID // Version 4 "random" for unique id not derived from key data + // to simplify lookups we also store the address + Address []byte // we only store privkey as pubkey/address can be derived from it // privkey in this struct is always in plaintext PrivateKey *ecdsa.PrivateKey @@ -41,6 +43,7 @@ type Key struct { type plainKeyJSON struct { Id []byte + Address []byte PrivateKey []byte } @@ -51,18 +54,15 @@ type cipherJSON struct { } type encryptedKeyJSON struct { - Id []byte - Crypto cipherJSON -} - -func (k *Key) Address() []byte { - pubBytes := FromECDSAPub(&k.PrivateKey.PublicKey) - return Sha3(pubBytes)[12:] + Id []byte + Address []byte + Crypto cipherJSON } func (k *Key) MarshalJSON() (j []byte, err error) { jStruct := plainKeyJSON{ - *k.Id, + k.Id, + k.Address, FromECDSA(k.PrivateKey), } j, err = json.Marshal(jStruct) @@ -78,8 +78,8 @@ func (k *Key) UnmarshalJSON(j []byte) (err error) { u := new(uuid.UUID) *u = keyJSON.Id - k.Id = u - + k.Id = *u + k.Address = keyJSON.Address k.PrivateKey = ToECDSA(keyJSON.PrivateKey) return err @@ -99,9 +99,11 @@ func NewKey(rand io.Reader) *Key { privateKeyMarshalled := elliptic.Marshal(S256(), x, y) privateKeyECDSA := ToECDSA(privateKeyMarshalled) - key := new(Key) id := uuid.NewRandom() - key.Id = &id - key.PrivateKey = privateKeyECDSA + key := &Key{ + Id: id, + Address: PubkeyToAddress(privateKeyECDSA.PublicKey), + PrivateKey: privateKeyECDSA, + } return key } diff --git a/crypto/key_store_passphrase.go b/crypto/key_store_passphrase.go index e30a0a785..0862b7886 100644 --- a/crypto/key_store_passphrase.go +++ b/crypto/key_store_passphrase.go @@ -69,6 +69,7 @@ import ( "crypto/aes" "crypto/cipher" crand "crypto/rand" + "encoding/hex" "encoding/json" "errors" "io" @@ -96,21 +97,26 @@ func (ks keyStorePassphrase) GenerateNewKey(rand io.Reader, auth string) (key *K return GenerateNewKeyDefault(ks, rand, auth) } -func (ks keyStorePassphrase) GetKey(keyId *uuid.UUID, auth string) (key *Key, err error) { - keyBytes, err := DecryptKey(ks, keyId, auth) +func (ks keyStorePassphrase) GetKey(keyAddr []byte, auth string) (key *Key, err error) { + keyBytes, keyId, err := DecryptKey(ks, keyAddr, auth) if err != nil { return nil, err } key = &Key{ - Id: keyId, + Id: uuid.UUID(keyId), + Address: keyAddr, PrivateKey: ToECDSA(keyBytes), } return key, err } +func (ks keyStorePassphrase) GetKeyAddresses() (addresses [][]byte, err error) { + return GetKeyAddresses(ks.keysDirPath) +} + func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) { authArray := []byte(auth) - salt := getEntropyCSPRNG(32) + salt := GetEntropyCSPRNG(32) derivedKey, err := scrypt.Key(authArray, salt, scryptN, scryptr, scryptp, scryptdkLen) if err != nil { return err @@ -125,7 +131,7 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) { return err } - iv := getEntropyCSPRNG(aes.BlockSize) // 16 + iv := GetEntropyCSPRNG(aes.BlockSize) // 16 AES256CBCEncrypter := cipher.NewCBCEncrypter(AES256Block, iv) cipherText := make([]byte, len(toEncrypt)) AES256CBCEncrypter.CryptBlocks(cipherText, toEncrypt) @@ -136,7 +142,8 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) { cipherText, } keyStruct := encryptedKeyJSON{ - *key.Id, + key.Id, + key.Address, cipherStruct, } keyJSON, err := json.Marshal(keyStruct) @@ -144,66 +151,53 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) { return err } - return WriteKeyFile(key.Id.String(), ks.keysDirPath, keyJSON) + return WriteKeyFile(key.Address, ks.keysDirPath, keyJSON) } -func (ks keyStorePassphrase) DeleteKey(keyId *uuid.UUID, auth string) (err error) { +func (ks keyStorePassphrase) DeleteKey(keyAddr []byte, auth string) (err error) { // only delete if correct passphrase is given - _, err = DecryptKey(ks, keyId, auth) + _, _, err = DecryptKey(ks, keyAddr, auth) if err != nil { return err } - keyDirPath := path.Join(ks.keysDirPath, keyId.String()) + keyDirPath := path.Join(ks.keysDirPath, hex.EncodeToString(keyAddr)) return os.RemoveAll(keyDirPath) } -func DecryptKey(ks keyStorePassphrase, keyId *uuid.UUID, auth string) (keyBytes []byte, err error) { - fileContent, err := GetKeyFile(ks.keysDirPath, keyId) +func DecryptKey(ks keyStorePassphrase, keyAddr []byte, auth string) (keyBytes []byte, keyId []byte, err error) { + fileContent, err := GetKeyFile(ks.keysDirPath, keyAddr) if err != nil { - return nil, err + return nil, nil, err } keyProtected := new(encryptedKeyJSON) err = json.Unmarshal(fileContent, keyProtected) + keyId = keyProtected.Id salt := keyProtected.Crypto.Salt - iv := keyProtected.Crypto.IV - cipherText := keyProtected.Crypto.CipherText authArray := []byte(auth) derivedKey, err := scrypt.Key(authArray, salt, scryptN, scryptr, scryptp, scryptdkLen) if err != nil { - return nil, err + return nil, nil, err } - - AES256Block, err := aes.NewCipher(derivedKey) + plainText, err := aesCBCDecrypt(derivedKey, cipherText, iv) if err != nil { - return nil, err - } - - AES256CBCDecrypter := cipher.NewCBCDecrypter(AES256Block, iv) - paddedPlainText := make([]byte, len(cipherText)) - AES256CBCDecrypter.CryptBlocks(paddedPlainText, cipherText) - - plainText := PKCS7Unpad(paddedPlainText) - if plainText == nil { - err = errors.New("Decryption failed: PKCS7Unpad failed after decryption") - return nil, err + return nil, nil, err } - keyBytes = plainText[:len(plainText)-32] keyBytesHash := plainText[len(plainText)-32:] if !bytes.Equal(Sha3(keyBytes), keyBytesHash) { err = errors.New("Decryption failed: checksum mismatch") - return nil, err + return nil, nil, err } - return keyBytes, err + return keyBytes, keyId, err } -func getEntropyCSPRNG(n int) []byte { +func GetEntropyCSPRNG(n int) []byte { mainBuff := make([]byte, n) _, err := io.ReadFull(crand.Reader, mainBuff) if err != nil { @@ -211,35 +205,3 @@ func getEntropyCSPRNG(n int) []byte { } return mainBuff } - -// From https://leanpub.com/gocrypto/read#leanpub-auto-block-cipher-modes -func PKCS7Pad(in []byte) []byte { - padding := 16 - (len(in) % 16) - if padding == 0 { - padding = 16 - } - for i := 0; i < padding; i++ { - in = append(in, byte(padding)) - } - return in -} - -func PKCS7Unpad(in []byte) []byte { - if len(in) == 0 { - return nil - } - - padding := in[len(in)-1] - if int(padding) > len(in) || padding > aes.BlockSize { - return nil - } else if padding == 0 { - return nil - } - - for i := len(in) - 1; i > len(in)-int(padding)-1; i-- { - if in[i] != padding { - return nil - } - } - return in[:len(in)-int(padding)] -} diff --git a/crypto/key_store_plain.go b/crypto/key_store_plain.go index b6e2a309a..6b76962a0 100644 --- a/crypto/key_store_plain.go +++ b/crypto/key_store_plain.go @@ -24,7 +24,7 @@ package crypto import ( - "code.google.com/p/go-uuid/uuid" + "encoding/hex" "encoding/json" "fmt" "io" @@ -38,9 +38,10 @@ import ( type KeyStore2 interface { // create new key using io.Reader entropy source and optionally using auth string GenerateNewKey(io.Reader, string) (*Key, error) - GetKey(*uuid.UUID, string) (*Key, error) // key from id and auth string - StoreKey(*Key, string) error // store key optionally using auth string - DeleteKey(*uuid.UUID, string) error // delete key by id and auth string + GetKey([]byte, string) (*Key, error) // key from addr and auth string + GetKeyAddresses() ([][]byte, error) // get all addresses + StoreKey(*Key, string) error // store key optionally using auth string + DeleteKey([]byte, string) error // delete key by addr and auth string } type keyStorePlain struct { @@ -72,8 +73,8 @@ func GenerateNewKeyDefault(ks KeyStore2, rand io.Reader, auth string) (key *Key, return key, err } -func (ks keyStorePlain) GetKey(keyId *uuid.UUID, auth string) (key *Key, err error) { - fileContent, err := GetKeyFile(ks.keysDirPath, keyId) +func (ks keyStorePlain) GetKey(keyAddr []byte, auth string) (key *Key, err error) { + fileContent, err := GetKeyFile(ks.keysDirPath, keyAddr) if err != nil { return nil, err } @@ -83,32 +84,50 @@ func (ks keyStorePlain) GetKey(keyId *uuid.UUID, auth string) (key *Key, err err return key, err } +func (ks keyStorePlain) GetKeyAddresses() (addresses [][]byte, err error) { + return GetKeyAddresses(ks.keysDirPath) +} + func (ks keyStorePlain) StoreKey(key *Key, auth string) (err error) { keyJSON, err := json.Marshal(key) if err != nil { return err } - err = WriteKeyFile(key.Id.String(), ks.keysDirPath, keyJSON) + err = WriteKeyFile(key.Address, ks.keysDirPath, keyJSON) return err } -func (ks keyStorePlain) DeleteKey(keyId *uuid.UUID, auth string) (err error) { - keyDirPath := path.Join(ks.keysDirPath, keyId.String()) +func (ks keyStorePlain) DeleteKey(keyAddr []byte, auth string) (err error) { + keyDirPath := path.Join(ks.keysDirPath, hex.EncodeToString(keyAddr)) err = os.RemoveAll(keyDirPath) return err } -func GetKeyFile(keysDirPath string, keyId *uuid.UUID) (fileContent []byte, err error) { - id := keyId.String() - return ioutil.ReadFile(path.Join(keysDirPath, id, id)) +func GetKeyFile(keysDirPath string, keyAddr []byte) (fileContent []byte, err error) { + fileName := hex.EncodeToString(keyAddr) + return ioutil.ReadFile(path.Join(keysDirPath, fileName, fileName)) } -func WriteKeyFile(id string, keysDirPath string, content []byte) (err error) { - keyDirPath := path.Join(keysDirPath, id) - keyFilePath := path.Join(keyDirPath, id) +func WriteKeyFile(addr []byte, keysDirPath string, content []byte) (err error) { + addrHex := hex.EncodeToString(addr) + keyDirPath := path.Join(keysDirPath, addrHex) + keyFilePath := path.Join(keyDirPath, addrHex) err = os.MkdirAll(keyDirPath, 0700) // read, write and dir search for user if err != nil { return err } return ioutil.WriteFile(keyFilePath, content, 0600) // read, write for user } + +func GetKeyAddresses(keysDirPath string) (addresses [][]byte, err error) { + fileInfos, err := ioutil.ReadDir(keysDirPath) + if err != nil { + return nil, err + } + addresses = make([][]byte, len(fileInfos)) + for i, fileInfo := range fileInfos { + addresses[i] = make([]byte, 40) + addresses[i] = []byte(fileInfo.Name()) + } + return addresses, err +} diff --git a/crypto/key_store_test.go b/crypto/key_store_test.go index 16c0e476d..0d229ab65 100644 --- a/crypto/key_store_test.go +++ b/crypto/key_store_test.go @@ -15,12 +15,12 @@ func TestKeyStorePlain(t *testing.T) { } k2 := new(Key) - k2, err = ks.GetKey(k1.Id, pass) + k2, err = ks.GetKey(k1.Address, pass) if err != nil { t.Fatal(err) } - if !reflect.DeepEqual(k1.Id, k2.Id) { + if !reflect.DeepEqual(k1.Address, k2.Address) { t.Fatal(err) } @@ -28,7 +28,7 @@ func TestKeyStorePlain(t *testing.T) { t.Fatal(err) } - err = ks.DeleteKey(k2.Id, pass) + err = ks.DeleteKey(k2.Address, pass) if err != nil { t.Fatal(err) } @@ -42,11 +42,11 @@ func TestKeyStorePassphrase(t *testing.T) { t.Fatal(err) } k2 := new(Key) - k2, err = ks.GetKey(k1.Id, pass) + k2, err = ks.GetKey(k1.Address, pass) if err != nil { t.Fatal(err) } - if !reflect.DeepEqual(k1.Id, k2.Id) { + if !reflect.DeepEqual(k1.Address, k2.Address) { t.Fatal(err) } @@ -54,7 +54,7 @@ func TestKeyStorePassphrase(t *testing.T) { t.Fatal(err) } - err = ks.DeleteKey(k2.Id, pass) // also to clean up created files + err = ks.DeleteKey(k2.Address, pass) // also to clean up created files if err != nil { t.Fatal(err) } @@ -68,17 +68,30 @@ func TestKeyStorePassphraseDecryptionFail(t *testing.T) { t.Fatal(err) } - _, err = ks.GetKey(k1.Id, "bar") // wrong passphrase + _, err = ks.GetKey(k1.Address, "bar") // wrong passphrase if err == nil { t.Fatal(err) } - err = ks.DeleteKey(k1.Id, "bar") // wrong passphrase + err = ks.DeleteKey(k1.Address, "bar") // wrong passphrase if err == nil { t.Fatal(err) } - err = ks.DeleteKey(k1.Id, pass) // to clean up + err = ks.DeleteKey(k1.Address, pass) // to clean up + if err != nil { + t.Fatal(err) + } +} + +func TestImportPreSaleKey(t *testing.T) { + // file content of a presale key file generated with: + // python pyethsaletool.py genwallet + // with password "foo" + fileContent := "{\"encseed\": \"26d87f5f2bf9835f9a47eefae571bc09f9107bb13d54ff12a4ec095d01f83897494cf34f7bed2ed34126ecba9db7b62de56c9d7cd136520a0427bfb11b8954ba7ac39b90d4650d3448e31185affcd74226a68f1e94b1108e6e0a4a91cdd83eba\", \"ethaddr\": \"d4584b5f6229b7be90727b0fc8c6b91bb427821f\", \"email\": \"gustav.simonsson@gmail.com\", \"btcaddr\": \"1EVknXyFC68kKNLkh6YnKzW41svSRoaAcx\"}" + ks := NewKeyStorePassphrase(DefaultDataDir()) + pass := "foo" + _, err := ImportPreSaleKey(ks, []byte(fileContent), pass) if err != nil { t.Fatal(err) } |