fix: sort public keys in deriveKeys so both sides compute matching send/recv keys

This commit is contained in:
xieyao
2026-06-17 05:07:28 +08:00
parent 807c4cf323
commit a8b411c888

View File

@@ -1,6 +1,7 @@
package sdk
import (
"bytes"
"context"
"crypto/cipher"
"crypto/sha256"
@@ -635,10 +636,16 @@ func isBroadcastMAC(dst []byte) bool {
}
func deriveKeys(psk, localPub, remotePub []byte) ([32]byte, [32]byte) {
var first, second []byte
if bytes.Compare(localPub, remotePub) <= 0 {
first, second = localPub, remotePub
} else {
first, second = remotePub, localPub
}
h := sha256.New()
h.Write(psk)
h.Write(localPub)
h.Write(remotePub)
h.Write(first)
h.Write(second)
var sendKey [32]byte
copy(sendKey[:], h.Sum(nil))