Skip to content

Commit

Permalink
Merge pull request #6 from ecies/kem-suffix-remove
Browse files Browse the repository at this point in the history
Kem suffix remove
  • Loading branch information
savely-krasovsky authored Aug 16, 2019
2 parents ddc93ae + 9e79fb5 commit a6e40f9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 26 deletions.
4 changes: 2 additions & 2 deletions ecies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions ecies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 4 additions & 11 deletions privatekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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())
}
Expand Down
15 changes: 4 additions & 11 deletions publickey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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())
}
Expand Down
8 changes: 8 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit a6e40f9

Please sign in to comment.