aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/crypto.go
diff options
context:
space:
mode:
authorGustav Simonsson <gustav.simonsson@gmail.com>2015-01-21 06:55:13 +0800
committerGustav Simonsson <gustav.simonsson@gmail.com>2015-01-21 23:35:43 +0800
commit1f8290ca44df31cc4c4b68b30eef412476ed24e0 (patch)
treed3cef09eedffc76848b43e8f339593578156545c /crypto/crypto.go
parent4df2e1ef5c20ebd7004a595de7e6a032699106ad (diff)
downloaddexon-1f8290ca44df31cc4c4b68b30eef412476ed24e0.tar.gz
dexon-1f8290ca44df31cc4c4b68b30eef412476ed24e0.tar.zst
dexon-1f8290ca44df31cc4c4b68b30eef412476ed24e0.zip
Add ImportPreSaleKey
* ImportPreSaleKey takes a KeyStore, a presale key JSON (e.g. file content) and a password string. It stores the key in the given key store. * Refactored common AES decryption and moved some functions to crypto.go
Diffstat (limited to 'crypto/crypto.go')
-rw-r--r--crypto/crypto.go104
1 files changed, 104 insertions, 0 deletions
diff --git a/crypto/crypto.go b/crypto/crypto.go
index ac84c6204..066d23dcb 100644
--- a/crypto/crypto.go
+++ b/crypto/crypto.go
@@ -1,12 +1,19 @@
package crypto
import (
+ "crypto/aes"
+ "crypto/cipher"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
+ "errors"
+ "code.google.com/p/go-uuid/uuid"
+ "code.google.com/p/go.crypto/pbkdf2"
"code.google.com/p/go.crypto/ripemd160"
+ "encoding/hex"
+ "encoding/json"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/ethutil"
@@ -113,3 +120,100 @@ func Decrypt(prv *ecdsa.PrivateKey, ct []byte) ([]byte, error) {
key := ecies.ImportECDSA(prv)
return key.Decrypt(rand.Reader, ct, nil, nil)
}
+
+// creates a Key and stores that in the given KeyStore by decrypting a presale key JSON
+func ImportPreSaleKey(keyStore KeyStore2, keyJSON []byte, password string) (*Key, error) {
+ key, err := decryptPreSaleKey(keyJSON, password)
+ if err != nil {
+ return nil, err
+ }
+ id := uuid.NewRandom()
+ key.Id = &id
+ err = keyStore.StoreKey(key, password)
+ return key, err
+}
+
+func decryptPreSaleKey(fileContent []byte, password string) (key *Key, err error) {
+ preSaleKeyStruct := struct {
+ EncSeed string
+ EthAddr string
+ Email string
+ BtcAddr string
+ }{}
+ err = json.Unmarshal(fileContent, &preSaleKeyStruct)
+ if err != nil {
+ return nil, err
+ }
+ encSeedBytes, err := hex.DecodeString(preSaleKeyStruct.EncSeed)
+ iv := encSeedBytes[:16]
+ cipherText := encSeedBytes[16:]
+ /*
+ See https://github.com/ethereum/pyethsaletool
+
+ pyethsaletool generates the encryption key from password by
+ 2000 rounds of PBKDF2 with HMAC-SHA-256 using password as salt (:().
+ 16 byte key length within PBKDF2 and resulting key is used as AES key
+ */
+ passBytes := []byte(password)
+ derivedKey := pbkdf2.Key(passBytes, passBytes, 2000, 16, sha256.New)
+ plainText, err := aes_cbc_decrypt(derivedKey, cipherText, iv)
+ ethPriv := Sha3(plainText)
+ ecKey := ToECDSA(ethPriv)
+ key = &Key{
+ Id: nil,
+ PrivateKey: ecKey,
+ }
+ derivedAddr := ethutil.Bytes2Hex(key.Address())
+ expectedAddr := preSaleKeyStruct.EthAddr
+ if derivedAddr != expectedAddr {
+ err = errors.New("decrypted addr not equal to expected addr")
+ }
+ return key, err
+}
+
+func aes_cbc_decrypt(key []byte, cipherText []byte, iv []byte) (plainText []byte, err error) {
+ aesBlock, err := aes.NewCipher(key)
+ if err != nil {
+ return plainText, err
+ }
+ decrypter := cipher.NewCBCDecrypter(aesBlock, iv)
+ paddedPlainText := make([]byte, len(cipherText))
+ decrypter.CryptBlocks(paddedPlainText, cipherText)
+ plainText = PKCS7Unpad(paddedPlainText)
+ if plainText == nil {
+ err = errors.New("Decryption failed: PKCS7Unpad failed after decryption")
+ }
+ return plainText, err
+}
+
+// From https://leanpub.com/gocrypto/read#leanpub-auto-block-cipher-modes
+func PKCS7Pad(in []byte) []byte {
+ padding := 16 - (len(in) % 16)
+ if padding == 0 {
+ padding = 16
+ }
+ for i := 0; i < padding; i++ {
+ in = append(in, byte(padding))
+ }
+ return in
+}
+
+func PKCS7Unpad(in []byte) []byte {
+ if len(in) == 0 {
+ return nil
+ }
+
+ padding := in[len(in)-1]
+ if int(padding) > len(in) || padding > aes.BlockSize {
+ return nil
+ } else if padding == 0 {
+ return nil
+ }
+
+ for i := len(in) - 1; i > len(in)-int(padding)-1; i-- {
+ if in[i] != padding {
+ return nil
+ }
+ }
+ return in[:len(in)-int(padding)]
+}