Skip to content

Commit

Permalink
FIX: ECDH HKDF option parsing to child as array
Browse files Browse the repository at this point in the history
  • Loading branch information
go-compile committed Jun 16, 2022
1 parent bf04dc2 commit 7c5ee93
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
7 changes: 5 additions & 2 deletions ecdh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rome

import (
"crypto/sha256"
"fmt"
"hash"

"golang.org/x/crypto/hkdf"
Expand All @@ -15,8 +16,8 @@ func (k *ECPublicKey) DH(hash hash.Hash, g PrivateKey, options ...Option) ([]byt
x, y := k.ecdsa.ScalarMult(k.ecdsa.X, k.ecdsa.Y, g.PrivateRaw())

// generate shared secret
for _, opt := range options {
switch o := opt.(type) {
for i := range options {
switch o := options[i].(type) {
case OptionHKDF:
kdf := hkdf.New(sha256.New, append(x.Bytes(), y.Bytes()...), o.Salt, nil)

Expand All @@ -26,6 +27,8 @@ func (k *ECPublicKey) DH(hash hash.Hash, g PrivateKey, options ...Option) ([]byt
}

return secret, nil
default:
fmt.Printf("%v", o)
}
}

Expand Down
4 changes: 2 additions & 2 deletions ecies.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (k *ECPublicKey) Encrypt(m []byte, c Cipher, hash hash.Hash, options ...Opt
}

// perform ECDH with provided hash function and the new ephemeral key
secret, err := k.DH(hash, k2)
secret, err := k.DH(hash, k2, options...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (k *ECKey) Decrypt(ciphertext []byte, c Cipher, hash hash.Hash, options ...
// trim public key prefix
ciphertext = ciphertext[len(public):]

secret, err := key.DH(hash, k)
secret, err := key.DH(hash, k, options...)
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 @@ -40,12 +40,12 @@ func TestECIESHKDF(t *testing.T) {
}

msg := []byte("This is the secret message 123.")
ciphertext, err := k.ECPublic().Encrypt(msg, rome.CipherAES_GCM, sha256.New(), rome.NewHKDF(sha512.New, 64, nil))
ciphertext, err := k.ECPublic().Encrypt(msg, rome.CipherAES_GCM, nil, rome.NewHKDF(sha512.New, 32, nil))
if err != nil {
t.Fatal(err)
}

plaintext, err := k.Decrypt(ciphertext, rome.CipherAES_GCM, sha256.New(), rome.NewHKDF(sha512.New, 64, nil))
plaintext, err := k.Decrypt(ciphertext, rome.CipherAES_GCM, nil, rome.NewHKDF(sha512.New, 32, nil))
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type OptionHKDF struct {

// NewHKDF allows you to use HKDF in your ECDH.
// Salt can be nil and keysize usually should be 32 (256bit)
func NewHKDF(h func() hash.Hash, keysize int, salt []byte) Option {
func NewHKDF(h func() hash.Hash, keysize int, salt []byte) OptionHKDF {
return OptionHKDF{
KeySize: keysize,
Salt: salt,
Expand Down

0 comments on commit 7c5ee93

Please sign in to comment.