github.com/halybang/go-ethereum@v1.0.5-0.20180325041310-3b262bc1367c/accounts/keystore/presale.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package keystore 18 19 import ( 20 "crypto/aes" 21 "crypto/cipher" 22 "crypto/sha256" 23 "encoding/hex" 24 "encoding/json" 25 "errors" 26 "fmt" 27 28 "github.com/pborman/uuid" 29 "github.com/wanchain/go-wanchain/accounts" 30 "github.com/wanchain/go-wanchain/crypto" 31 "golang.org/x/crypto/pbkdf2" 32 ) 33 34 // creates a Key and stores that in the given KeyStore by decrypting a presale key JSON 35 func importPreSaleKey(keyStore keyStore, keyJSON []byte, password string) (accounts.Account, *Key, error) { 36 key, err := decryptPreSaleKey(keyJSON, password) 37 if err != nil { 38 return accounts.Account{}, nil, err 39 } 40 key.Id = uuid.NewRandom() 41 42 // add a second sk field for wanchain address 43 sk, err := crypto.GenerateKey() 44 if err != nil { 45 return accounts.Account{}, nil, err 46 } 47 key.PrivateKey2 = sk 48 49 a := accounts.Account{Address: key.Address, URL: accounts.URL{Scheme: KeyStoreScheme, Path: keyStore.JoinPath(keyFileName(key.Address))}} 50 err = keyStore.StoreKey(a.URL.Path, key, password) 51 return a, key, err 52 } 53 54 func decryptPreSaleKey(fileContent []byte, password string) (key *Key, err error) { 55 preSaleKeyStruct := struct { 56 EncSeed string 57 EthAddr string 58 Email string 59 BtcAddr string 60 }{} 61 err = json.Unmarshal(fileContent, &preSaleKeyStruct) 62 if err != nil { 63 return nil, err 64 } 65 encSeedBytes, err := hex.DecodeString(preSaleKeyStruct.EncSeed) 66 if err != nil { 67 return nil, errors.New("invalid hex in encSeed") 68 } 69 iv := encSeedBytes[:16] 70 cipherText := encSeedBytes[16:] 71 /* 72 See https://github.com/ethereum/pyethsaletool 73 74 pyethsaletool generates the encryption key from password by 75 2000 rounds of PBKDF2 with HMAC-SHA-256 using password as salt (:(). 76 16 byte key length within PBKDF2 and resulting key is used as AES key 77 */ 78 passBytes := []byte(password) 79 derivedKey := pbkdf2.Key(passBytes, passBytes, 2000, 16, sha256.New) 80 plainText, err := aesCBCDecrypt(derivedKey, cipherText, iv) 81 if err != nil { 82 return nil, err 83 } 84 ethPriv := crypto.Keccak256(plainText) 85 ecKey := crypto.ToECDSAUnsafe(ethPriv) 86 87 key = &Key{ 88 Id: nil, 89 Address: crypto.PubkeyToAddress(ecKey.PublicKey), 90 PrivateKey: ecKey, 91 } 92 derivedAddr := hex.EncodeToString(key.Address.Bytes()) // needed because .Hex() gives leading "0x" 93 expectedAddr := preSaleKeyStruct.EthAddr 94 if derivedAddr != expectedAddr { 95 err = fmt.Errorf("decrypted addr '%s' not equal to expected addr '%s'", derivedAddr, expectedAddr) 96 } 97 return key, err 98 } 99 100 func aesCTRXOR(key, inText, iv []byte) ([]byte, error) { 101 // AES-128 is selected due to size of encryptKey. 102 aesBlock, err := aes.NewCipher(key) 103 if err != nil { 104 return nil, err 105 } 106 stream := cipher.NewCTR(aesBlock, iv) 107 outText := make([]byte, len(inText)) 108 stream.XORKeyStream(outText, inText) 109 return outText, err 110 } 111 112 func aesCBCDecrypt(key, cipherText, iv []byte) ([]byte, error) { 113 aesBlock, err := aes.NewCipher(key) 114 if err != nil { 115 return nil, err 116 } 117 decrypter := cipher.NewCBCDecrypter(aesBlock, iv) 118 paddedPlaintext := make([]byte, len(cipherText)) 119 decrypter.CryptBlocks(paddedPlaintext, cipherText) 120 plaintext := pkcs7Unpad(paddedPlaintext) 121 if plaintext == nil { 122 return nil, ErrDecrypt 123 } 124 return plaintext, err 125 } 126 127 // From https://leanpub.com/gocrypto/read#leanpub-auto-block-cipher-modes 128 func pkcs7Unpad(in []byte) []byte { 129 if len(in) == 0 { 130 return nil 131 } 132 133 padding := in[len(in)-1] 134 if int(padding) > len(in) || padding > aes.BlockSize { 135 return nil 136 } else if padding == 0 { 137 return nil 138 } 139 140 for i := len(in) - 1; i > len(in)-int(padding)-1; i-- { 141 if in[i] != padding { 142 return nil 143 } 144 } 145 return in[:len(in)-int(padding)] 146 }