From 243698680af2966fd7d7befa5529847560ab7da3 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 13 Feb 2022 16:48:48 +0100 Subject: [PATCH] crypto: Use helpers for size calculations --- internal/cache/file.go | 5 ++--- internal/repository/key.go | 2 +- internal/restorer/filerestorer.go | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/internal/cache/file.go b/internal/cache/file.go index 0db1275a3..2ee66f520 100644 --- a/internal/cache/file.go +++ b/internal/cache/file.go @@ -8,7 +8,6 @@ import ( "runtime" "github.com/pkg/errors" - "github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/fs" "github.com/restic/restic/internal/restic" @@ -59,7 +58,7 @@ func (c *Cache) load(h restic.Handle, length int, offset int64) (io.ReadCloser, return nil, errors.WithStack(err) } - if fi.Size() <= crypto.Extension { + if fi.Size() <= int64(restic.CiphertextLength(0)) { _ = f.Close() _ = c.remove(h) return nil, errors.Errorf("cached file %v is truncated, removing", h) @@ -117,7 +116,7 @@ func (c *Cache) Save(h restic.Handle, rd io.Reader) error { return errors.Wrap(err, "Copy") } - if n <= crypto.Extension { + if n <= int64(restic.CiphertextLength(0)) { _ = f.Close() _ = fs.Remove(f.Name()) debug.Log("trying to cache truncated file %v, removing", h) diff --git a/internal/repository/key.go b/internal/repository/key.go index 5de154195..8c62d7e36 100644 --- a/internal/repository/key.go +++ b/internal/repository/key.go @@ -262,7 +262,7 @@ func AddKey(ctx context.Context, s *Repository, password, username, hostname str } nonce := crypto.NewRandomNonce() - ciphertext := make([]byte, 0, len(buf)+newkey.user.Overhead()+newkey.user.NonceSize()) + ciphertext := make([]byte, 0, restic.CiphertextLength(len(buf))) ciphertext = append(ciphertext, nonce...) ciphertext = newkey.user.Seal(ciphertext, nonce, buf, nil) newkey.Data = ciphertext diff --git a/internal/restorer/filerestorer.go b/internal/restorer/filerestorer.go index 84c7834df..206703ce3 100644 --- a/internal/restorer/filerestorer.go +++ b/internal/restorer/filerestorer.go @@ -195,7 +195,7 @@ func (r *fileRestorer) downloadPack(ctx context.Context, pack *packInfo) error { if packID.Equal(pack.id) { addBlob(blob, fileOffset) } - fileOffset += int64(blob.Length) - crypto.Extension + fileOffset += int64(restic.PlaintextLength(int(blob.Length))) }) if err != nil { // restoreFiles should have caught this error before