diff options
Diffstat (limited to 'vendor/github.com/Rhymen/go-whatsapp/crypto/hkdf/hkdf.go')
-rw-r--r-- | vendor/github.com/Rhymen/go-whatsapp/crypto/hkdf/hkdf.go | 49 |
1 files changed, 22 insertions, 27 deletions
diff --git a/vendor/github.com/Rhymen/go-whatsapp/crypto/hkdf/hkdf.go b/vendor/github.com/Rhymen/go-whatsapp/crypto/hkdf/hkdf.go index e0be0587..3d7361f0 100644 --- a/vendor/github.com/Rhymen/go-whatsapp/crypto/hkdf/hkdf.go +++ b/vendor/github.com/Rhymen/go-whatsapp/crypto/hkdf/hkdf.go @@ -9,7 +9,6 @@ second stage "expands" this key into several additional pseudorandom keys (the o package hkdf import ( - "crypto/hmac" "crypto/sha256" "fmt" "golang.org/x/crypto/hkdf" @@ -20,33 +19,29 @@ import ( Expand expands a given key with the HKDF algorithm. */ func Expand(key []byte, length int, info string) ([]byte, error) { + var h io.Reader if info == "" { - keyBlock := hmac.New(sha256.New, key) - var out, last []byte - - var blockIndex byte = 1 - for i := 0; len(out) < length; i++ { - keyBlock.Reset() - //keyBlock.Write(append(append(last, []byte(info)...), blockIndex)) - keyBlock.Write(last) - keyBlock.Write([]byte(info)) - keyBlock.Write([]byte{blockIndex}) - last = keyBlock.Sum(nil) - blockIndex += 1 - out = append(out, last...) - } - return out[:length], nil + /* + Only used during initial login + Pseudorandom Key is provided by server and has not to be created + */ + h = hkdf.Expand(sha256.New, key, []byte(info)) } else { - h := hkdf.New(sha256.New, key, nil, []byte(info)) - out := make([]byte, length) - n, err := io.ReadAtLeast(h, out, length) - if err != nil { - return nil, err - } - if n != length { - return nil, fmt.Errorf("new key to short") - } - - return out[:length], nil + /* + Used every other time + Pseudorandom Key is created during kdf.New + This is the normal that crypto/hkdf is used + */ + h = hkdf.New(sha256.New, key, nil, []byte(info)) + } + out := make([]byte, length) + n, err := io.ReadAtLeast(h, out, length) + if err != nil { + return nil, err } + if n != length { + return nil, fmt.Errorf("new key to short") + } + + return out, nil } |