From 5b84f9efce2b2921345180cd4121e6f4ee6331e5 Mon Sep 17 00:00:00 2001 From: Dillen Meijboom Date: Tue, 7 Dec 2021 15:30:25 +0100 Subject: [PATCH] Switched to P256 as default curve and fixed tests so that the ethereum dependency can be removed --- ecies_test.go | 41 ++++++++++++++--------------------------- go.sum | 0 params.go | 11 ++++------- 3 files changed, 18 insertions(+), 34 deletions(-) create mode 100644 go.sum diff --git a/ecies_test.go b/ecies_test.go index 0a6aeb2..3275c49 100644 --- a/ecies_test.go +++ b/ecies_test.go @@ -31,6 +31,7 @@ package ecies import ( "bytes" + "crypto/ecdsa" "crypto/elliptic" "crypto/rand" "crypto/sha256" @@ -38,8 +39,6 @@ import ( "fmt" "math/big" "testing" - - "github.com/ethereum/go-ethereum/crypto" ) func TestKDF(t *testing.T) { @@ -104,10 +103,10 @@ func TestSharedKeyPadding(t *testing.T) { // sanity checks prv0 := hexKey("1adf5c18167d96a1f9a0b1ef63be8aa27eaf6032c233b2b38f7850cf5b859fd9") prv1 := hexKey("0097a076fc7fcd9208240668e31c9abee952cbb6e375d1b8febc7499d6e16f1a") - x0, _ := new(big.Int).SetString("1a8ed022ff7aec59dc1b440446bdda5ff6bcb3509a8b109077282b361efffbd8", 16) - x1, _ := new(big.Int).SetString("6ab3ac374251f638d0abb3ef596d1dc67955b507c104e5f2009724812dc027b8", 16) - y0, _ := new(big.Int).SetString("e040bd480b1deccc3bc40bd5b1fdcb7bfd352500b477cb9471366dbd4493f923", 16) - y1, _ := new(big.Int).SetString("8ad915f2b503a8be6facab6588731fefeb584fd2dfa9a77a5e0bba1ec439e4fa", 16) + x0, _ := new(big.Int).SetString("894f0b45e976ff1d368ecb31aa5fdd47e3edb1b980b7d3bf7a7b543a5b2964a0", 16) + x1, _ := new(big.Int).SetString("99a279d52118fffbaa3f2ac2d60f3bacf10e6cf86f46ee7f3b39b29ec78a94f2", 16) + y0, _ := new(big.Int).SetString("c942f48766dc44c2a6e808691091de40d84f9b9df5394f6df99454a209d7843e", 16) + y1, _ := new(big.Int).SetString("7ca3ebd2ea8ac4913c8c0c8cac4571316abab06b2a076caa9369a1ae7fd2d8ce", 16) if prv0.PublicKey.X.Cmp(x0) != 0 { t.Errorf("mismatched prv0.X:\nhave: %x\nwant: %x\n", prv0.PublicKey.X.Bytes(), x0.Bytes()) @@ -186,21 +185,6 @@ func BenchmarkGenSharedKeyP256(b *testing.B) { } } -// Benchmark the generation of S256 shared keys. -func BenchmarkGenSharedKeyS256(b *testing.B) { - prv, err := GenerateKey(rand.Reader, crypto.S256(), nil) - if err != nil { - b.Fatal(err) - } - b.ResetTimer() - for i := 0; i < b.N; i++ { - _, err := prv.GenerateShared(&prv.PublicKey, 16, 16) - if err != nil { - b.Fatal(err) - } - } -} - // Verify that an encrypted message can be successfully decrypted. func TestEncryptDecrypt(t *testing.T) { prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) @@ -407,18 +391,21 @@ func TestSharedKeyStatic(t *testing.T) { t.Fatal(ErrBadSharedKeys) } - sk := decode("167ccc13ac5e8a26b131c3446030c60fbfac6aa8e31149d0869f93626a4cdf62") + sk := decode("2b71c59bb0495360d20642360998981d3d00c74f6e72ec4d94f1391662f00d10") if !bytes.Equal(sk1, sk) { t.Fatalf("shared secret mismatch: want: %x have: %x", sk, sk1) } } func hexKey(prv string) *PrivateKey { - key, err := crypto.HexToECDSA(prv) - if err != nil { - panic(err) - } - return ImportECDSA(key) + b := decode(prv) + + privateKey := &ecdsa.PrivateKey{} + privateKey.PublicKey.Curve = elliptic.P256() + privateKey.D = (&big.Int{}).SetBytes(b) + privateKey.X, privateKey.Y = privateKey.PublicKey.Curve.ScalarBaseMult(b) + + return ImportECDSA(privateKey) } func decode(s string) []byte { diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/params.go b/params.go index 0bd3877..7a30c6c 100644 --- a/params.go +++ b/params.go @@ -41,12 +41,10 @@ import ( "crypto/sha512" "fmt" "hash" - - ethcrypto "github.com/ethereum/go-ethereum/crypto" ) var ( - DefaultCurve = ethcrypto.S256() + DefaultCurve = elliptic.P256() ErrUnsupportedECDHAlgorithm = fmt.Errorf("ecies: unsupported ECDH algorithm") ErrUnsupportedECIESParameters = fmt.Errorf("ecies: unsupported ECIES parameters") ErrInvalidKeyLen = fmt.Errorf("ecies: invalid key size (> %d) in ECIESParams", maxKeyLen) @@ -106,10 +104,9 @@ var ( ) var paramsFromCurve = map[elliptic.Curve]*ECIESParams{ - ethcrypto.S256(): ECIES_AES128_SHA256, - elliptic.P256(): ECIES_AES128_SHA256, - elliptic.P384(): ECIES_AES256_SHA384, - elliptic.P521(): ECIES_AES256_SHA512, + elliptic.P256(): ECIES_AES128_SHA256, + elliptic.P384(): ECIES_AES256_SHA384, + elliptic.P521(): ECIES_AES256_SHA512, } func AddParamsForCurve(curve elliptic.Curve, params *ECIESParams) {