backup: a backup is sealed with the run's salt and key for its passphrase, as shared usage already is, so a sync round no longer derives two 600,000-round PBKDF2 keys (one to seal, one to open what it sealed) (#1158)

Seal drew a fresh salt each time and Open derived its key anew, so every
sync round paid PBKDF2 twice. Under -race that is slow enough that
internal/davsync's tests took 570-866s, past or near go test's default
10-minute timeout. Seal now takes the salt SealData keeps per passphrase,
and both go through the derived-key cache, which also keys on the
iteration count now. The nonce stays random per seal; Open's checks are
unchanged, and TestTampered still catches a lowered iteration count.

Verified: TestSealDerivesOnce fails without the change ("derived 0 keys
for two backups under one passphrase, want 1") and passes with it, also
under -race -count=3. internal/davsync under -race went from 866s to 24s
on an M-series Mac (TestGatewayKeysConflict alone 39s to 2.4s, old code
put back through -overlay), and from 36s to 13s without -race. The
backup, davsync, gui and root packages pass; gofmt and go vet -tags nogui
are clean; darwin, linux and windows build.
This commit is contained in:
forever
2026-10-13 09:47:00 +08:00
committed by GitHub
parent 5353c15246
commit 00a2356db0
3 changed files with 52 additions and 17 deletions
+2 -3
View File
@@ -257,8 +257,7 @@ func Seal(b Bundle, pass string) ([]byte, error) {
return nil, err
}
e := envelope{Format: format, Version: 1, KDF: "pbkdf2-sha256", Iterations: iterations,
Salt: make([]byte, 16), Nonce: make([]byte, 12)}
rand.Read(e.Salt)
Salt: saltFor(pass), Nonce: make([]byte, 12)}
rand.Read(e.Nonce)
gcm, err := aead(pass, e)
if err != nil {
@@ -318,7 +317,7 @@ func header(e envelope) []byte {
}
func aead(pass string, e envelope) (cipher.AEAD, error) {
key, err := deriveKey(pass, e)
key, err := derived(pass, e)
if err != nil {
return nil, err
}
+30
View File
@@ -2,6 +2,7 @@ package backup
import (
"bytes"
"crypto/rand"
"encoding/json"
"errors"
"os"
@@ -400,3 +401,32 @@ func TestProfileKeys(t *testing.T) {
t.Fatalf("%v", got)
}
}
// A sync seals a backup every round and opens the one on the server: the
// key is derived once a run, not once a backup, PBKDF2 being slow on
// purpose (davsync's tests ran past go test's 10 minutes under -race).
func TestSealDerivesOnce(t *testing.T) {
pass := rand.Text() // one no earlier run derived a key for
keysMu.Lock()
before := len(keys)
keysMu.Unlock()
var sealed [][]byte
for range 2 {
data, err := Seal(Bundle{Version: 1}, pass)
if err != nil {
t.Fatal(err)
}
sealed = append(sealed, data)
}
for _, data := range sealed {
if _, err := Open(data, pass); err != nil {
t.Fatal(err)
}
}
keysMu.Lock()
derivedNow := len(keys) - before
keysMu.Unlock()
if derivedNow != 1 {
t.Fatalf("derived %d keys for two backups under one passphrase, want 1", derivedNow)
}
}
+20 -14
View File
@@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"sync"
)
@@ -13,17 +14,31 @@ import (
const dataFormat = "magpie-data"
// keys are the keys derived this run, by passphrase and salt: a computer
// seals every file it shares with one salt, and opens each other
// computer's with that one's, so a sync derives a key once a computer
// rather than once a file, PBKDF2 being slow on purpose.
// seals every file it shares, and every backup, with one salt, and opens
// each other computer's with that one's, so a sync derives a key once a
// computer rather than once a file, PBKDF2 being slow on purpose.
var (
keysMu sync.Mutex
keys = map[[32]byte][]byte{}
salts = map[[32]byte][]byte{}
)
// saltFor is the salt this run seals with under pass.
func saltFor(pass string) []byte {
p := sha256.Sum256([]byte(pass))
keysMu.Lock()
defer keysMu.Unlock()
salt := salts[p]
if salt == nil {
salt = make([]byte, 16)
rand.Read(salt)
salts[p] = salt
}
return salt
}
func derived(pass string, e envelope) ([]byte, error) {
k := sha256.Sum256(append([]byte(pass+"\x00"), e.Salt...))
k := sha256.Sum256(fmt.Appendf(nil, "%s\x00%d\x00%x", pass, e.Iterations, e.Salt))
keysMu.Lock()
defer keysMu.Unlock()
if key, ok := keys[k]; ok {
@@ -42,16 +57,7 @@ func SealData(data []byte, pass string) ([]byte, error) {
if pass == "" {
return nil, errors.New("sealing needs a passphrase")
}
p := sha256.Sum256([]byte(pass))
keysMu.Lock()
salt := salts[p]
if salt == nil {
salt = make([]byte, 16)
rand.Read(salt)
salts[p] = salt
}
keysMu.Unlock()
e := envelope{Format: dataFormat, Version: 1, KDF: "pbkdf2-sha256", Iterations: iterations, Salt: salt, Nonce: make([]byte, 12)}
e := envelope{Format: dataFormat, Version: 1, KDF: "pbkdf2-sha256", Iterations: iterations, Salt: saltFor(pass), Nonce: make([]byte, 12)}
rand.Read(e.Nonce)
key, err := derived(pass, e)
if err != nil {