Skip to content

Commit

Permalink
repo: removed controls for setting format block encryption, instead w…
Browse files Browse the repository at this point in the history
…hen block encryption is enabled, the format block is automatically encrypted using default encryption algorithm
  • Loading branch information
jkowalski committed Jan 12, 2019
1 parent 840d5ab commit 766f574
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
12 changes: 8 additions & 4 deletions block/block_manager.go
Expand Up @@ -459,14 +459,15 @@ func (bm *Manager) loadPackIndexesUnlocked(ctx context.Context) ([]IndexInfo, bo
}

func (bm *Manager) tryLoadPackIndexBlocksUnlocked(ctx context.Context, blocks []IndexInfo) error {
ch, err := bm.unprocessedIndexBlocksUnlocked(blocks)
ch, unprocessedIndexesSize, err := bm.unprocessedIndexBlocksUnlocked(blocks)
if err != nil {
return err
}
if len(ch) == 0 {
return nil
}

log.Infof("downloading %v new index blocks (%v bytes)...", len(ch), unprocessedIndexesSize)
var wg sync.WaitGroup

errors := make(chan error, parallelFetches)
Expand Down Expand Up @@ -498,26 +499,29 @@ func (bm *Manager) tryLoadPackIndexBlocksUnlocked(ctx context.Context, blocks []
for err := range errors {
return err
}
log.Infof("Index blocks downloaded.")

return nil
}

// unprocessedIndexBlocksUnlocked returns a closed channel filled with block IDs that are not in committedBlocks cache.
func (bm *Manager) unprocessedIndexBlocksUnlocked(blocks []IndexInfo) (<-chan string, error) {
func (bm *Manager) unprocessedIndexBlocksUnlocked(blocks []IndexInfo) (<-chan string, int64, error) {
var totalSize int64
ch := make(chan string, len(blocks))
for _, block := range blocks {
has, err := bm.committedBlocks.cache.hasIndexBlockID(block.FileName)
if err != nil {
return nil, err
return nil, 0, err
}
if has {
log.Debugf("index block %q already in cache, skipping", block.FileName)
continue
}
ch <- block.FileName
totalSize += block.Length
}
close(ch)
return ch, nil
return ch, totalSize, nil
}

// Close closes the block manager.
Expand Down
8 changes: 2 additions & 6 deletions crypto_key_derivation.go
Expand Up @@ -6,20 +6,16 @@ import (
"io"

"golang.org/x/crypto/hkdf"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/scrypt"
)

// DefaultKeyDerivationAlgorithm is the key derivation algorithm for new configurations.
const DefaultKeyDerivationAlgorithm = "scrypt-65536-8-1"
// defaultKeyDerivationAlgorithm is the key derivation algorithm for new configurations.
const defaultKeyDerivationAlgorithm = "scrypt-65536-8-1"

func (f formatBlock) deriveMasterKeyFromPassword(password string) ([]byte, error) {
const masterKeySize = 32

switch f.KeyDerivationAlgorithm {
case "pbkdf2-sha256-100000":
return pbkdf2.Key([]byte(password), f.UniqueID, 100000, masterKeySize, sha256.New), nil

case "scrypt-65536-8-1":
return scrypt.Key([]byte(password), f.UniqueID, 65536, 8, 1, masterKeySize)

Expand Down
2 changes: 2 additions & 0 deletions format_block.go
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/kopia/repo/storage"
)

const defaultFormatEncryption = "AES256_GCM"

// FormatBlockID is the identifier of a storage block that describes repository format.
const FormatBlockID = "kopia.repository"

Expand Down
28 changes: 12 additions & 16 deletions initialize.go
Expand Up @@ -17,22 +17,12 @@ var (
BuildVersion = "v0-unofficial"
)

// DefaultEncryptionAlgorithm is the default algorithm for encrypting format block.
var DefaultEncryptionAlgorithm = "AES256_GCM"

// SupportedEncryptionAlgorithms lists all supported algorithms for encrypting format block.
var SupportedEncryptionAlgorithms = []string{DefaultEncryptionAlgorithm, "NONE"}

// NewRepositoryOptions specifies options that apply to newly created repositories.
// All fields are optional, when not provided, reasonable defaults will be used.
type NewRepositoryOptions struct {
UniqueID []byte // force the use of particular unique ID
FormatEncryptionAlgorithm string // identifier of encryption algorithm
KeyDerivationAlgorithm string // identifier of key derivation algorithm

BlockFormat block.FormattingOptions
DisableHMAC bool

UniqueID []byte // force the use of particular unique ID
BlockFormat block.FormattingOptions
DisableHMAC bool
ObjectFormat object.Format // object format
}

Expand Down Expand Up @@ -69,14 +59,20 @@ func Initialize(ctx context.Context, st storage.Storage, opt *NewRepositoryOptio
}

func formatBlockFromOptions(opt *NewRepositoryOptions) *formatBlock {
return &formatBlock{
f := &formatBlock{
Tool: "https://github.com/kopia/kopia",
BuildInfo: BuildInfo,
KeyDerivationAlgorithm: applyDefaultString(opt.KeyDerivationAlgorithm, DefaultKeyDerivationAlgorithm),
KeyDerivationAlgorithm: defaultKeyDerivationAlgorithm,
UniqueID: applyDefaultRandomBytes(opt.UniqueID, 32),
Version: "1",
EncryptionAlgorithm: applyDefaultString(opt.FormatEncryptionAlgorithm, DefaultEncryptionAlgorithm),
EncryptionAlgorithm: defaultFormatEncryption,
}

if opt.BlockFormat.Encryption == "NONE" {
f.EncryptionAlgorithm = "NONE"
}

return f
}

func repositoryObjectFormatFromOptions(opt *NewRepositoryOptions) *repositoryObjectFormat {
Expand Down
1 change: 0 additions & 1 deletion internal/repotesting/repotesting.go
Expand Up @@ -53,7 +53,6 @@ func (e *Environment) Setup(t *testing.T, opts ...func(*repo.NewRepositoryOption
Splitter: "FIXED",
MaxBlockSize: 400,
},
FormatEncryptionAlgorithm: "NONE",
}

for _, mod := range opts {
Expand Down

0 comments on commit 766f574

Please sign in to comment.