From b1202e472bc6b69a460e3366e9ff517ee2b31acc Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Tue, 17 Nov 2026 07:54:26 +0100 Subject: [PATCH] wip Signed-off-by: Jakob Borg --- internal/dht/keys.go | 11 ++++++++--- internal/dht/keys_test.go | 2 +- internal/dht/records.go | 7 +++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/internal/dht/keys.go b/internal/dht/keys.go index 1c4167c39..fa283be75 100644 --- a/internal/dht/keys.go +++ b/internal/dht/keys.go @@ -19,14 +19,19 @@ import ( "golang.org/x/crypto/hkdf" ) +const ( + discoveryKeySalt = "syncthing-dht-record-key-v1" + encryptionKeySalt = "syncthing-dht-encryption-key-v1" +) + // deriveRecordKey derives the daily discovery lookup key for id. func deriveRecordKey(id protocol.DeviceID, date time.Time) [32]byte { - return deriveDiscoveryKey(id, date, "syncthing-dht-record-key-v1") + return deriveDiscoveryKey(id, date, discoveryKeySalt) } // deriveEncryptionKey derives the daily record encryption key for id. func deriveEncryptionKey(id protocol.DeviceID, date time.Time) [32]byte { - return deriveDiscoveryKey(id, date, "syncthing-dht-encryption-key-v1") + return deriveDiscoveryKey(id, date, encryptionKeySalt) } func deriveDiscoveryKey(id protocol.DeviceID, date time.Time, salt string) [32]byte { @@ -84,7 +89,7 @@ func (c *discoveryKeyCache) get(id protocol.DeviceID, date time.Time) discoveryK // discoveryDay staggers daily rotation across devices. The first eight bytes // of the device ID select a cutoff in seconds after UTC midnight. func discoveryDay(id protocol.DeviceID, now time.Time) string { - offset := time.Duration(binary.BigEndian.Uint64(id[:8])%(24*60*60)) * time.Second + offset := time.Duration(binary.BigEndian.Uint64(id[:8])%86400) * time.Second return now.UTC().Add(-offset).Format(time.DateOnly) } diff --git a/internal/dht/keys_test.go b/internal/dht/keys_test.go index 61138cf8f..019d03d50 100644 --- a/internal/dht/keys_test.go +++ b/internal/dht/keys_test.go @@ -17,7 +17,7 @@ import ( func TestDiscoveryKeyDerivation(t *testing.T) { id := protocol.NewDeviceID([]byte("test certificate")) - offset := time.Duration(binary.BigEndian.Uint64(id[:8])%(24*60*60)) * time.Second + offset := time.Duration(binary.BigEndian.Uint64(id[:8])%84600) * time.Second date := time.Date(2026, time.November, 16, 0, 0, 0, 0, time.UTC).Add(offset) for _, tc := range []struct { name string diff --git a/internal/dht/records.go b/internal/dht/records.go index e460bb19d..ec79c9c56 100644 --- a/internal/dht/records.go +++ b/internal/dht/records.go @@ -30,8 +30,9 @@ import ( const ( RecordLifetime = 2 * time.Hour - dhtRecordNamespace = "device" - dhtSignatureDomain = "syncthing-dht-discovery-v1\x00" + dhtRecordNamespace = "device" + dhtSignatureDomain = "syncthing-dht-discovery-v1\x00" + encryptedRecordDomain = "syncthing-dht-encrypted-record-v1\x00" ) // dhtRecordKey encodes the public half of the daily record signing key. @@ -157,8 +158,6 @@ func dhtSignatureAlgorithm(public crypto.PublicKey) (x509.SignatureAlgorithm, er // them from the network type dhtRecordValidator struct{} -const encryptedRecordDomain = "syncthing-dht-encrypted-record-v1\x00" - // wrapperMetadata is also used as AEAD additional data, binding encryption // to the wrapper version, sequence and expiration. func wrapperMetadata(w *discoproto.DHTWrapper) ([]byte, error) {