mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-04-25 15:52:08 +00:00
For BYOP mode in the packet analyzer, perform signature validation on advert packets and display whether successful or not. This is added as we observed many corrupted advert packets that would be easily detectable as such if signature validation checks were performed. At present this MR is just to add this status in BYOP mode so there is minimal impact to the application and no performance penalty for having to perform these checks on all packets. Moving forward it probably makes sense to do these checks on all advert packets so that corrupt packets can be ignored in several contexts (like node lists for example). Let me know what you think and I can adjust as needed. --------- Co-authored-by: you <you@example.com>
28 lines
898 B
Go
28 lines
898 B
Go
// Package sigvalidate provides ed25519 signature validation for MeshCore advert packets.
|
|
package sigvalidate
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"encoding/binary"
|
|
"fmt"
|
|
)
|
|
|
|
// ValidateAdvert verifies the ed25519 signature on a MeshCore advert.
|
|
// pubKey must be 32 bytes, signature must be 64 bytes.
|
|
// The signed message is: pubKey (32) + timestamp (4 LE) + appdata.
|
|
func ValidateAdvert(pubKey, signature []byte, timestamp uint32, appdata []byte) (bool, error) {
|
|
if len(pubKey) != 32 {
|
|
return false, fmt.Errorf("invalid pubkey length: %d", len(pubKey))
|
|
}
|
|
if len(signature) != 64 {
|
|
return false, fmt.Errorf("invalid signature length: %d", len(signature))
|
|
}
|
|
|
|
message := make([]byte, 32+4+len(appdata))
|
|
copy(message[0:32], pubKey)
|
|
binary.LittleEndian.PutUint32(message[32:36], timestamp)
|
|
copy(message[36:], appdata)
|
|
|
|
return ed25519.Verify(ed25519.PublicKey(pubKey), message, signature), nil
|
|
}
|