2014-12-31 14:39:33 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
2015-05-24 01:42:10 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2014-12-31 14:39:33 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-05-24 01:42:10 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto/randentropy"
|
2014-12-31 14:39:33 +00:00
|
|
|
)
|
|
|
|
|
2015-01-07 15:06:26 +00:00
|
|
|
func TestKeyStorePlain(t *testing.T) {
|
2015-03-16 10:27:38 +00:00
|
|
|
ks := NewKeyStorePlain(common.DefaultDataDir())
|
2014-12-31 14:39:33 +00:00
|
|
|
pass := "" // not used but required by API
|
2015-02-13 14:38:18 +00:00
|
|
|
k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
k2 := new(Key)
|
2015-01-25 01:07:20 +00:00
|
|
|
k2, err = ks.GetKey(k1.Address, pass)
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-25 01:07:20 +00:00
|
|
|
if !reflect.DeepEqual(k1.Address, k2.Address) {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-25 01:07:20 +00:00
|
|
|
err = ks.DeleteKey(k2.Address, pass)
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyStorePassphrase(t *testing.T) {
|
2015-03-16 10:27:38 +00:00
|
|
|
ks := NewKeyStorePassphrase(common.DefaultDataDir())
|
2014-12-31 14:39:33 +00:00
|
|
|
pass := "foo"
|
2015-02-13 14:38:18 +00:00
|
|
|
k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
k2 := new(Key)
|
2015-01-25 01:07:20 +00:00
|
|
|
k2, err = ks.GetKey(k1.Address, pass)
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
2015-01-25 01:07:20 +00:00
|
|
|
if !reflect.DeepEqual(k1.Address, k2.Address) {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-25 01:07:20 +00:00
|
|
|
err = ks.DeleteKey(k2.Address, pass) // also to clean up created files
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
|
2015-03-16 10:27:38 +00:00
|
|
|
ks := NewKeyStorePassphrase(common.DefaultDataDir())
|
2014-12-31 14:39:33 +00:00
|
|
|
pass := "foo"
|
2015-02-13 14:38:18 +00:00
|
|
|
k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-25 01:07:20 +00:00
|
|
|
_, err = ks.GetKey(k1.Address, "bar") // wrong passphrase
|
2014-12-31 14:39:33 +00:00
|
|
|
if err == nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-25 01:07:20 +00:00
|
|
|
err = ks.DeleteKey(k1.Address, "bar") // wrong passphrase
|
2014-12-31 14:39:33 +00:00
|
|
|
if err == nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-25 01:07:20 +00:00
|
|
|
err = ks.DeleteKey(k1.Address, pass) // to clean up
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-20 22:55:13 +00:00
|
|
|
|
|
|
|
func TestImportPreSaleKey(t *testing.T) {
|
|
|
|
// file content of a presale key file generated with:
|
|
|
|
// python pyethsaletool.py genwallet
|
|
|
|
// with password "foo"
|
|
|
|
fileContent := "{\"encseed\": \"26d87f5f2bf9835f9a47eefae571bc09f9107bb13d54ff12a4ec095d01f83897494cf34f7bed2ed34126ecba9db7b62de56c9d7cd136520a0427bfb11b8954ba7ac39b90d4650d3448e31185affcd74226a68f1e94b1108e6e0a4a91cdd83eba\", \"ethaddr\": \"d4584b5f6229b7be90727b0fc8c6b91bb427821f\", \"email\": \"gustav.simonsson@gmail.com\", \"btcaddr\": \"1EVknXyFC68kKNLkh6YnKzW41svSRoaAcx\"}"
|
2015-03-16 10:27:38 +00:00
|
|
|
ks := NewKeyStorePassphrase(common.DefaultDataDir())
|
2015-01-20 22:55:13 +00:00
|
|
|
pass := "foo"
|
|
|
|
_, err := ImportPreSaleKey(ks, []byte(fileContent), pass)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2015-05-24 01:42:10 +00:00
|
|
|
|
|
|
|
// Test and utils for the key store tests in the Ethereum JSON tests;
|
|
|
|
// tests/KeyStoreTests/basic_tests.json
|
|
|
|
type KeyStoreTestV3 struct {
|
|
|
|
Json encryptedKeyJSONV3
|
|
|
|
Password string
|
|
|
|
Priv string
|
|
|
|
}
|
|
|
|
|
|
|
|
type KeyStoreTestV1 struct {
|
|
|
|
Json encryptedKeyJSONV1
|
|
|
|
Password string
|
|
|
|
Priv string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV3_PBKDF2_1(t *testing.T) {
|
|
|
|
tests := loadKeyStoreTestV3("tests/v3_test_vector.json", t)
|
|
|
|
testDecryptV3(tests["wikipage_test_vector_pbkdf2"], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV3_PBKDF2_2(t *testing.T) {
|
|
|
|
tests := loadKeyStoreTestV3("../tests/files/KeyStoreTests/basic_tests.json", t)
|
|
|
|
testDecryptV3(tests["test1"], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV3_PBKDF2_3(t *testing.T) {
|
|
|
|
tests := loadKeyStoreTestV3("../tests/files/KeyStoreTests/basic_tests.json", t)
|
|
|
|
testDecryptV3(tests["python_generated_test_with_odd_iv"], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV3_PBKDF2_4(t *testing.T) {
|
|
|
|
tests := loadKeyStoreTestV3("../tests/files/KeyStoreTests/basic_tests.json", t)
|
|
|
|
testDecryptV3(tests["evilnonce"], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV3_Scrypt_1(t *testing.T) {
|
|
|
|
tests := loadKeyStoreTestV3("tests/v3_test_vector.json", t)
|
|
|
|
testDecryptV3(tests["wikipage_test_vector_scrypt"], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV3_Scrypt_2(t *testing.T) {
|
|
|
|
tests := loadKeyStoreTestV3("../tests/files/KeyStoreTests/basic_tests.json", t)
|
|
|
|
testDecryptV3(tests["test2"], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV1_1(t *testing.T) {
|
|
|
|
tests := loadKeyStoreTestV1("tests/v1_test_vector.json", t)
|
|
|
|
testDecryptV1(tests["test1"], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV1_2(t *testing.T) {
|
|
|
|
ks := NewKeyStorePassphrase("tests/v1")
|
|
|
|
addr := common.HexToAddress("cb61d5a9c4896fb9658090b597ef0e7be6f7b67e")
|
|
|
|
k, err := ks.GetKey(addr, "g")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if k.Address != addr {
|
|
|
|
t.Fatal(fmt.Errorf("Unexpected address: %v, expected %v", k.Address, addr))
|
|
|
|
}
|
|
|
|
|
|
|
|
privHex := hex.EncodeToString(FromECDSA(k.PrivateKey))
|
|
|
|
expectedHex := "d1b1178d3529626a1a93e073f65028370d14c7eb0936eb42abef05db6f37ad7d"
|
|
|
|
if privHex != expectedHex {
|
|
|
|
t.Fatal(fmt.Errorf("Unexpected privkey: %v, expected %v", privHex, expectedHex))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDecryptV3(test KeyStoreTestV3, t *testing.T) {
|
|
|
|
privBytes, _, err := decryptKeyV3(&test.Json, test.Password)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
privHex := hex.EncodeToString(privBytes)
|
|
|
|
if test.Priv != privHex {
|
|
|
|
t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDecryptV1(test KeyStoreTestV1, t *testing.T) {
|
|
|
|
privBytes, _, err := decryptKeyV1(&test.Json, test.Password)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
privHex := hex.EncodeToString(privBytes)
|
|
|
|
if test.Priv != privHex {
|
|
|
|
t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadKeyStoreTestV3(file string, t *testing.T) map[string]KeyStoreTestV3 {
|
|
|
|
tests := make(map[string]KeyStoreTestV3)
|
|
|
|
err := common.LoadJSON(file, &tests)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return tests
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadKeyStoreTestV1(file string, t *testing.T) map[string]KeyStoreTestV1 {
|
|
|
|
tests := make(map[string]KeyStoreTestV1)
|
|
|
|
err := common.LoadJSON(file, &tests)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return tests
|
|
|
|
}
|