From dd154f424097564271a29ce8c683ed35b8e6ca89 Mon Sep 17 00:00:00 2001 From: L11R Date: Fri, 16 Aug 2019 11:22:31 +0300 Subject: [PATCH 1/2] zero padding extracted into dedicated func --- privatekey.go | 11 ++--------- publickey.go | 11 ++--------- utils.go | 8 ++++++++ 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/privatekey.go b/privatekey.go index 0610d7b..9366ce8 100644 --- a/privatekey.go +++ b/privatekey.go @@ -86,15 +86,8 @@ func (k *PrivateKey) EncapsulateKEM(pub *PublicKey) ([]byte, error) { // Sometimes shared secret coordinates are less than 32 bytes; Big Endian l := len(pub.Curve.Params().P.Bytes()) - for i := 0; i < l-len(sx.Bytes()); i++ { - secret.Write([]byte{0x00}) - } - secret.Write(sx.Bytes()) - - for i := 0; i < l-len(sy.Bytes()); i++ { - secret.Write([]byte{0x00}) - } - secret.Write(sy.Bytes()) + secret.Write(zeroPad(sx.Bytes(), l)) + secret.Write(zeroPad(sy.Bytes(), l)) return kdf(secret.Bytes()) } diff --git a/publickey.go b/publickey.go index 6c37774..72469ca 100644 --- a/publickey.go +++ b/publickey.go @@ -148,15 +148,8 @@ func (k *PublicKey) DecapsulateKEM(priv *PrivateKey) ([]byte, error) { // Sometimes shared secret coordinates are less than 32 bytes; Big Endian l := len(priv.Curve.Params().P.Bytes()) - for i := 0; i < l-len(sx.Bytes()); i++ { - secret.Write([]byte{0x00}) - } - secret.Write(sx.Bytes()) - - for i := 0; i < l-len(sy.Bytes()); i++ { - secret.Write([]byte{0x00}) - } - secret.Write(sy.Bytes()) + secret.Write(zeroPad(sx.Bytes(), l)) + secret.Write(zeroPad(sy.Bytes(), l)) return kdf(secret.Bytes()) } diff --git a/utils.go b/utils.go index 063696e..e25af85 100644 --- a/utils.go +++ b/utils.go @@ -16,3 +16,11 @@ func kdf(secret []byte) (key []byte, err error) { return key, nil } + +func zeroPad(b []byte, leigth int) []byte { + for i := 0; i < leigth-len(b); i++ { + b = append([]byte{0x00}, b...) + } + + return b +} From 9e79fb5e8407927234071ae5963f787cdda609e6 Mon Sep 17 00:00:00 2001 From: L11R Date: Fri, 16 Aug 2019 11:36:33 +0300 Subject: [PATCH 2/2] code style fix --- ecies.go | 4 ++-- ecies_test.go | 4 ++-- privatekey.go | 4 ++-- publickey.go | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ecies.go b/ecies.go index 24bfd86..44e646d 100644 --- a/ecies.go +++ b/ecies.go @@ -23,7 +23,7 @@ func Encrypt(pubkey *PublicKey, msg []byte) ([]byte, error) { ct.Write(ek.PublicKey.Bytes(false)) // Derive shared secret - ss, err := ek.EncapsulateKEM(pubkey) + ss, err := ek.Encapsulate(pubkey) if err != nil { return nil, err } @@ -74,7 +74,7 @@ func Decrypt(privkey *PrivateKey, msg []byte) ([]byte, error) { msg = msg[65:] // Derive shared secret - ss, err := ethPubkey.DecapsulateKEM(privkey) + ss, err := ethPubkey.Decapsulate(privkey) if err != nil { return nil, err } diff --git a/ecies_test.go b/ecies_test.go index 7c86333..80f04ad 100644 --- a/ecies_test.go +++ b/ecies_test.go @@ -74,11 +74,11 @@ func TestKEM(t *testing.T) { k1 := NewPrivateKeyFromBytes(new(big.Int).SetInt64(2).Bytes()) k2 := NewPrivateKeyFromBytes(new(big.Int).SetInt64(3).Bytes()) - sk1, err := k1.EncapsulateKEM(k2.PublicKey) + sk1, err := k1.Encapsulate(k2.PublicKey) if !assert.NoError(t, err) { return } - sk2, err := k1.PublicKey.DecapsulateKEM(k2) + sk2, err := k1.PublicKey.Decapsulate(k2) if !assert.NoError(t, err) { return } diff --git a/privatekey.go b/privatekey.go index 9366ce8..47dffbb 100644 --- a/privatekey.go +++ b/privatekey.go @@ -71,9 +71,9 @@ func (k *PrivateKey) Hex() string { return hex.EncodeToString(k.Bytes()) } -// EncapsulateKEM encapsulates key by using Key Encapsulation Mechanism and returns symmetric key; +// Encapsulate encapsulates key by using Key Encapsulation Mechanism and returns symmetric key; // can be safely used as encryption key -func (k *PrivateKey) EncapsulateKEM(pub *PublicKey) ([]byte, error) { +func (k *PrivateKey) Encapsulate(pub *PublicKey) ([]byte, error) { if pub == nil { return nil, errors.New("public key is empty") } diff --git a/publickey.go b/publickey.go index 72469ca..b193b15 100644 --- a/publickey.go +++ b/publickey.go @@ -133,9 +133,9 @@ func (k *PublicKey) Hex(compressed bool) string { return hex.EncodeToString(k.Bytes(compressed)) } -// DecapsulateKEM decapsulates key by using Key Encapsulation Mechanism and returns symmetric key; +// Decapsulate decapsulates key by using Key Encapsulation Mechanism and returns symmetric key; // can be safely used as encryption key -func (k *PublicKey) DecapsulateKEM(priv *PrivateKey) ([]byte, error) { +func (k *PublicKey) Decapsulate(priv *PrivateKey) ([]byte, error) { if priv == nil { return nil, errors.New("public key is empty") }