presale.go 4.2 KB

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