presale.go 3.9 KB

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