presale.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. package keystore
  17. import (
  18. "crypto/aes"
  19. "crypto/cipher"
  20. "crypto/sha256"
  21. "encoding/hex"
  22. "encoding/json"
  23. "errors"
  24. "fmt"
  25. "github.com/ethereum/go-ethereum/accounts"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/google/uuid"
  28. "golang.org/x/crypto/pbkdf2"
  29. )
  30. // creates a Key and stores that in the given KeyStore by decrypting a presale key JSON
  31. func importPreSaleKey(keyStore keyStore, keyJSON []byte, password string) (accounts.Account, *Key, error) {
  32. key, err := decryptPreSaleKey(keyJSON, password)
  33. if err != nil {
  34. return accounts.Account{}, nil, err
  35. }
  36. key.Id, err = uuid.NewRandom()
  37. if err != nil {
  38. return accounts.Account{}, nil, err
  39. }
  40. a := accounts.Account{
  41. Address: key.Address,
  42. URL: accounts.URL{
  43. Scheme: KeyStoreScheme,
  44. Path: keyStore.JoinPath(keyFileName(key.Address)),
  45. },
  46. }
  47. err = keyStore.StoreKey(a.URL.Path, key, password)
  48. return a, key, err
  49. }
  50. func decryptPreSaleKey(fileContent []byte, password string) (key *Key, err error) {
  51. preSaleKeyStruct := struct {
  52. EncSeed string
  53. EthAddr string
  54. Email string
  55. BtcAddr string
  56. }{}
  57. err = json.Unmarshal(fileContent, &preSaleKeyStruct)
  58. if err != nil {
  59. return nil, err
  60. }
  61. encSeedBytes, err := hex.DecodeString(preSaleKeyStruct.EncSeed)
  62. if err != nil {
  63. return nil, errors.New("invalid hex in encSeed")
  64. }
  65. if len(encSeedBytes) < 16 {
  66. return nil, errors.New("invalid encSeed, too short")
  67. }
  68. iv := encSeedBytes[:16]
  69. cipherText := encSeedBytes[16:]
  70. /*
  71. See https://github.com/ethereum/pyethsaletool
  72. pyethsaletool generates the encryption key from password by
  73. 2000 rounds of PBKDF2 with HMAC-SHA-256 using password as salt (:().
  74. 16 byte key length within PBKDF2 and resulting key is used as AES key
  75. */
  76. passBytes := []byte(password)
  77. derivedKey := pbkdf2.Key(passBytes, passBytes, 2000, 16, sha256.New)
  78. plainText, err := aesCBCDecrypt(derivedKey, cipherText, iv)
  79. if err != nil {
  80. return nil, err
  81. }
  82. ethPriv := crypto.Keccak256(plainText)
  83. ecKey := crypto.ToECDSAUnsafe(ethPriv)
  84. key = &Key{
  85. Id: uuid.UUID{},
  86. Address: crypto.PubkeyToAddress(ecKey.PublicKey),
  87. PrivateKey: ecKey,
  88. }
  89. derivedAddr := hex.EncodeToString(key.Address.Bytes()) // needed because .Hex() gives leading "0x"
  90. expectedAddr := preSaleKeyStruct.EthAddr
  91. if derivedAddr != expectedAddr {
  92. err = fmt.Errorf("decrypted addr '%s' not equal to expected addr '%s'", derivedAddr, expectedAddr)
  93. }
  94. return key, err
  95. }
  96. func aesCTRXOR(key, inText, iv []byte) ([]byte, error) {
  97. // AES-128 is selected due to size of encryptKey.
  98. aesBlock, err := aes.NewCipher(key)
  99. if err != nil {
  100. return nil, err
  101. }
  102. stream := cipher.NewCTR(aesBlock, iv)
  103. outText := make([]byte, len(inText))
  104. stream.XORKeyStream(outText, inText)
  105. return outText, err
  106. }
  107. func aesCBCDecrypt(key, cipherText, iv []byte) ([]byte, error) {
  108. aesBlock, err := aes.NewCipher(key)
  109. if err != nil {
  110. return nil, err
  111. }
  112. decrypter := cipher.NewCBCDecrypter(aesBlock, iv)
  113. paddedPlaintext := make([]byte, len(cipherText))
  114. decrypter.CryptBlocks(paddedPlaintext, cipherText)
  115. plaintext := pkcs7Unpad(paddedPlaintext)
  116. if plaintext == nil {
  117. return nil, ErrDecrypt
  118. }
  119. return plaintext, err
  120. }
  121. // From https://leanpub.com/gocrypto/read#leanpub-auto-block-cipher-modes
  122. func pkcs7Unpad(in []byte) []byte {
  123. if len(in) == 0 {
  124. return nil
  125. }
  126. padding := in[len(in)-1]
  127. if int(padding) > len(in) || padding > aes.BlockSize {
  128. return nil
  129. } else if padding == 0 {
  130. return nil
  131. }
  132. for i := len(in) - 1; i > len(in)-int(padding)-1; i-- {
  133. if in[i] != padding {
  134. return nil
  135. }
  136. }
  137. return in[:len(in)-int(padding)]
  138. }