diff options
author | Wim <wim@42.be> | 2022-01-31 00:27:37 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2022-03-20 14:57:48 +0100 |
commit | e3cafeaf9292f67459ff1d186f68283bfaedf2ae (patch) | |
tree | b69c39620aa91dba695b3b935c6651c0fb37ce75 /vendor/go.mau.fi/libsignal/kdf/HKDF.go | |
parent | e7b193788a56ee7cdb02a87a9db0ad6724ef66d5 (diff) | |
download | matterbridge-msglm-e3cafeaf9292f67459ff1d186f68283bfaedf2ae.tar.gz matterbridge-msglm-e3cafeaf9292f67459ff1d186f68283bfaedf2ae.tar.bz2 matterbridge-msglm-e3cafeaf9292f67459ff1d186f68283bfaedf2ae.zip |
Add dependencies/vendor (whatsapp)
Diffstat (limited to 'vendor/go.mau.fi/libsignal/kdf/HKDF.go')
-rw-r--r-- | vendor/go.mau.fi/libsignal/kdf/HKDF.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/vendor/go.mau.fi/libsignal/kdf/HKDF.go b/vendor/go.mau.fi/libsignal/kdf/HKDF.go new file mode 100644 index 00000000..168b18ac --- /dev/null +++ b/vendor/go.mau.fi/libsignal/kdf/HKDF.go @@ -0,0 +1,47 @@ +// Package kdf provides a key derivation function to calculate key output +// and negotiate shared secrets for curve X25519 keys. +package kdf + +import ( + "crypto/sha256" + "io" + + "golang.org/x/crypto/curve25519" + "golang.org/x/crypto/hkdf" +) + +// HKDF is a hashed key derivation function type that can be used to derive keys. +type HKDF func(inputKeyMaterial, salt, info []byte, outputLength int) ([]byte, error) + +// DeriveSecrets derives the requested number of bytes using HKDF with the given +// input, salt, and info. +func DeriveSecrets(inputKeyMaterial, salt, info []byte, outputLength int) ([]byte, error) { + kdf := hkdf.New(sha256.New, inputKeyMaterial, salt, info) + + secrets := make([]byte, outputLength) + length, err := io.ReadFull(kdf, secrets) + if err != nil { + return nil, err + } + if length != outputLength { + return nil, err + } + + return secrets, nil +} + +// CalculateSharedSecret uses DH Curve25519 to find a shared secret. The result of this function +// should be used in `DeriveSecrets` to output the Root and Chain keys. +func CalculateSharedSecret(theirKey, ourKey [32]byte) [32]byte { + var sharedSecret [32]byte + curve25519.ScalarMult(&sharedSecret, &ourKey, &theirKey) + + return sharedSecret +} + +// KeyMaterial is a structure for representing a cipherkey, mac, and iv +type KeyMaterial struct { + CipherKey []byte + MacKey []byte + IV []byte +} |