presale.go 4.0 KB

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