diff options
author | Felix Lange <fjl@users.noreply.github.com> | 2017-02-18 16:24:12 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2017-02-18 16:24:12 +0800 |
commit | 9b0af513867fad4aeb3516e4711dd0ea4f5bc90c (patch) | |
tree | b37d808d57873c6aec550431534e26602dfd0475 /crypto/ecies | |
parent | bf21549faa7de6e2b920855468b14856c6f503c4 (diff) | |
download | dexon-9b0af513867fad4aeb3516e4711dd0ea4f5bc90c.tar.gz dexon-9b0af513867fad4aeb3516e4711dd0ea4f5bc90c.tar.zst dexon-9b0af513867fad4aeb3516e4711dd0ea4f5bc90c.zip |
crypto: add btcec fallback for sign/recover without cgo (#3680)
* vendor: add github.com/btcsuite/btcd/btcec
* crypto: add btcec fallback for sign/recover without cgo
This commit adds a non-cgo fallback implementation of secp256k1
operations.
* crypto, core/vm: remove wrappers for sha256, ripemd160
Diffstat (limited to 'crypto/ecies')
-rw-r--r-- | crypto/ecies/asn1.go | 6 | ||||
-rw-r--r-- | crypto/ecies/ecies_test.go | 41 | ||||
-rw-r--r-- | crypto/ecies/params.go | 6 |
3 files changed, 37 insertions, 16 deletions
diff --git a/crypto/ecies/asn1.go b/crypto/ecies/asn1.go index 508a645cd..d3e77d849 100644 --- a/crypto/ecies/asn1.go +++ b/crypto/ecies/asn1.go @@ -42,7 +42,7 @@ import ( "hash" "math/big" - "github.com/ethereum/go-ethereum/crypto/secp256k1" + ethcrypto "github.com/ethereum/go-ethereum/crypto" ) var ( @@ -120,7 +120,7 @@ func (curve secgNamedCurve) Equal(curve2 secgNamedCurve) bool { func namedCurveFromOID(curve secgNamedCurve) elliptic.Curve { switch { case curve.Equal(secgNamedCurveS256): - return secp256k1.S256() + return ethcrypto.S256() case curve.Equal(secgNamedCurveP256): return elliptic.P256() case curve.Equal(secgNamedCurveP384): @@ -139,7 +139,7 @@ func oidFromNamedCurve(curve elliptic.Curve) (secgNamedCurve, bool) { return secgNamedCurveP384, true case elliptic.P521(): return secgNamedCurveP521, true - case secp256k1.S256(): + case ethcrypto.S256(): return secgNamedCurveS256, true } diff --git a/crypto/ecies/ecies_test.go b/crypto/ecies/ecies_test.go index 3b3517baf..7c454aa73 100644 --- a/crypto/ecies/ecies_test.go +++ b/crypto/ecies/ecies_test.go @@ -31,7 +31,6 @@ package ecies import ( "bytes" - "crypto/ecdsa" "crypto/elliptic" "crypto/rand" "crypto/sha256" @@ -42,7 +41,7 @@ import ( "math/big" "testing" - "github.com/ethereum/go-ethereum/crypto/secp256k1" + "github.com/ethereum/go-ethereum/crypto" ) var dumpEnc bool @@ -150,7 +149,7 @@ func TestSharedKey(t *testing.T) { func TestSharedKeyPadding(t *testing.T) { // sanity checks prv0 := hexKey("1adf5c18167d96a1f9a0b1ef63be8aa27eaf6032c233b2b38f7850cf5b859fd9") - prv1 := hexKey("97a076fc7fcd9208240668e31c9abee952cbb6e375d1b8febc7499d6e16f1a") + prv1 := hexKey("0097a076fc7fcd9208240668e31c9abee952cbb6e375d1b8febc7499d6e16f1a") x0, _ := new(big.Int).SetString("1a8ed022ff7aec59dc1b440446bdda5ff6bcb3509a8b109077282b361efffbd8", 16) x1, _ := new(big.Int).SetString("6ab3ac374251f638d0abb3ef596d1dc67955b507c104e5f2009724812dc027b8", 16) y0, _ := new(big.Int).SetString("e040bd480b1deccc3bc40bd5b1fdcb7bfd352500b477cb9471366dbd4493f923", 16) @@ -354,7 +353,7 @@ func BenchmarkGenSharedKeyP256(b *testing.B) { // Benchmark the generation of S256 shared keys. func BenchmarkGenSharedKeyS256(b *testing.B) { - prv, err := GenerateKey(rand.Reader, secp256k1.S256(), nil) + prv, err := GenerateKey(rand.Reader, crypto.S256(), nil) if err != nil { fmt.Println(err.Error()) b.FailNow() @@ -597,6 +596,29 @@ func TestBasicKeyValidation(t *testing.T) { } } +func TestBox(t *testing.T) { + prv1 := hexKey("4b50fa71f5c3eeb8fdc452224b2395af2fcc3d125e06c32c82e048c0559db03f") + prv2 := hexKey("d0b043b4c5d657670778242d82d68a29d25d7d711127d17b8e299f156dad361a") + pub2 := &prv2.PublicKey + + message := []byte("Hello, world.") + ct, err := Encrypt(rand.Reader, pub2, message, nil, nil) + if err != nil { + t.Fatal(err) + } + + pt, err := prv2.Decrypt(rand.Reader, ct, nil, nil) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(pt, message) { + t.Fatal("ecies: plaintext doesn't match message") + } + if _, err = prv1.Decrypt(rand.Reader, ct, nil, nil); err == nil { + t.Fatal("ecies: encryption should not have succeeded") + } +} + // Verify GenerateShared against static values - useful when // debugging changes in underlying libs func TestSharedKeyStatic(t *testing.T) { @@ -628,11 +650,10 @@ func TestSharedKeyStatic(t *testing.T) { } } -// TODO: remove after refactoring packages crypto and crypto/ecies func hexKey(prv string) *PrivateKey { - priv := new(ecdsa.PrivateKey) - priv.PublicKey.Curve = secp256k1.S256() - priv.D, _ = new(big.Int).SetString(prv, 16) - priv.PublicKey.X, priv.PublicKey.Y = secp256k1.S256().ScalarBaseMult(priv.D.Bytes()) - return ImportECDSA(priv) + key, err := crypto.HexToECDSA(prv) + if err != nil { + panic(err) + } + return ImportECDSA(key) } diff --git a/crypto/ecies/params.go b/crypto/ecies/params.go index 511c53ebc..826d90c84 100644 --- a/crypto/ecies/params.go +++ b/crypto/ecies/params.go @@ -42,11 +42,11 @@ import ( "fmt" "hash" - "github.com/ethereum/go-ethereum/crypto/secp256k1" + ethcrypto "github.com/ethereum/go-ethereum/crypto" ) var ( - DefaultCurve = secp256k1.S256() + DefaultCurve = ethcrypto.S256() ErrUnsupportedECDHAlgorithm = fmt.Errorf("ecies: unsupported ECDH algorithm") ErrUnsupportedECIESParameters = fmt.Errorf("ecies: unsupported ECIES parameters") ) @@ -100,7 +100,7 @@ var ( ) var paramsFromCurve = map[elliptic.Curve]*ECIESParams{ - secp256k1.S256(): ECIES_AES128_SHA256, + ethcrypto.S256(): ECIES_AES128_SHA256, elliptic.P256(): ECIES_AES128_SHA256, elliptic.P384(): ECIES_AES256_SHA384, elliptic.P521(): ECIES_AES256_SHA512, |