diff options
| author | Mistivia <i@mistivia.com> | 2025-11-02 15:27:18 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-11-02 15:27:18 +0800 |
| commit | e9c24f4af7ed56760f6db7941827d09f6db9020b (patch) | |
| tree | 62128c43b883ce5e3148113350978755779bb5de /teleirc/matterbridge/vendor/go.mau.fi/libsignal/kdf/HKDF.go | |
| parent | 58d5e7cfda4781d8a57ec52aefd02983835c301a (diff) | |
add matterbridge
Diffstat (limited to 'teleirc/matterbridge/vendor/go.mau.fi/libsignal/kdf/HKDF.go')
| -rw-r--r-- | teleirc/matterbridge/vendor/go.mau.fi/libsignal/kdf/HKDF.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/teleirc/matterbridge/vendor/go.mau.fi/libsignal/kdf/HKDF.go b/teleirc/matterbridge/vendor/go.mau.fi/libsignal/kdf/HKDF.go new file mode 100644 index 0000000..168b18a --- /dev/null +++ b/teleirc/matterbridge/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 +} |
