diff --git a/internal/backup/backup.go b/internal/backup/backup.go index 6610be435..4827ee1ca 100644 --- a/internal/backup/backup.go +++ b/internal/backup/backup.go @@ -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 } diff --git a/internal/backup/backup_test.go b/internal/backup/backup_test.go index 027a56883..97213d8d6 100644 --- a/internal/backup/backup_test.go +++ b/internal/backup/backup_test.go @@ -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) + } +} diff --git a/internal/backup/data.go b/internal/backup/data.go index 40a863ef8..335273c1d 100644 --- a/internal/backup/data.go +++ b/internal/backup/data.go @@ -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 {