<commit_before>package vault

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"crypto/hmac"
	"crypto/rand"
	"crypto/sha256"
	"encoding/json"
	"fmt"
	"hash"
	"io"
	"io/ioutil"

	"github.com/kopia/kopia/blob"
	"github.com/kopia/kopia/repo"

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

const (
	formatBlock           = "format"
	checksumBlock         = "checksum"
	repositoryConfigBlock = "repo"

	minPasswordLength = 12
	minKeyLength      = 16
)

var (
	purposeAESKey         = []byte("AES")
	purposeChecksumSecret = []byte("CHECKSUM")
)

type Vault struct {
	Storage   blob.Storage
	MasterKey []byte
	Format    Format
}

func (v *Vault) writeEncryptedBlock(name string, content []byte) error {
	blk, err := v.newCipher()
	if err != nil {
		return err
	}

	hash, err := v.newChecksum()
	if err != nil {
		return err
	}

	ivLength := blk.BlockSize()
	ivPlusContentLength := ivLength + len(content)
	cipherText := make([]byte, ivPlusContentLength+hash.Size())

	// Store IV at the beginning of ciphertext.
	iv := cipherText[0:ivLength]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		return err
	}

	ctr := cipher.NewCTR(blk, iv)
	ctr.XORKeyStream(cipherText[ivLength:], content)
	hash.Write(cipherText[0:ivPlusContentLength])
	copy(cipherText[ivPlusContentLength:], hash.Sum(nil))

	return v.Storage.PutBlock(name, ioutil.NopCloser(bytes.NewBuffer(cipherText)), blob.PutOptions{
		Overwrite: true,
	})
}

func (v *Vault) readEncryptedBlock(name string) ([]byte, error) {
	cipherText, err := v.Storage.GetBlock(name)
	if err != nil {
		return nil, err
	}

	hash, err := v.newChecksum()
	if err != nil {
		return nil, err
	}

	p := len(cipherText) - hash.Size()
	hash.Write(cipherText[0:p])
	expectedChecksum := hash.Sum(nil)
	actualChecksum := cipherText[p:]
	if !hmac.Equal(expectedChecksum, actualChecksum) {
		return nil, fmt.Errorf("cannot read encrypted block: incorrect checksum")
	}

	blk, err := v.newCipher()
	if err != nil {
		return nil, err
	}

	ivLength := blk.BlockSize()

	plainText := make([]byte, len(cipherText)-ivLength-hash.Size())
	iv := cipherText[0:blk.BlockSize()]

	ctr := cipher.NewCTR(blk, iv)
	ctr.XORKeyStream(plainText, cipherText[ivLength:len(cipherText)-hash.Size()])
	return plainText, nil
}

func (v *Vault) newChecksum() (hash.Hash, error) {
	switch v.Format.Checksum {
	case "hmac-sha-256":
		key := make([]byte, 32)
		v.deriveKey(purposeChecksumSecret, key)
		return hmac.New(sha256.New, key), nil

	default:
		return nil, fmt.Errorf("unsupported checksum format: %v", v.Format.Checksum)
	}

}

func (v *Vault) newCipher() (cipher.Block, error) {
	switch v.Format.Encryption {
	case "aes-256":
		k := make([]byte, 32)
		v.deriveKey(purposeAESKey, k)
		return aes.NewCipher(k)
	default:
		return nil, fmt.Errorf("unsupported encryption format: %v", v.Format.Encryption)
	}

}

func (v *Vault) deriveKey(purpose []byte, key []byte) error {
	k := hkdf.New(sha256.New, v.MasterKey, v.Format.UniqueID, purpose)
	_, err := io.ReadFull(k, key)
	return err
}

func (v *Vault) SetRepository(rc RepositoryConfig) error {
	b, err := json.Marshal(&rc)
	if err != nil {
		return err
	}

	return v.writeEncryptedBlock(repositoryConfigBlock, b)
}

func (v *Vault) RepositoryConfig() (*RepositoryConfig, error) {
	var rc RepositoryConfig

	b, err := v.readEncryptedBlock(repositoryConfigBlock)
	if err != nil {
		return nil, fmt.Errorf("unable to read repository: %v", err)
	}

	err = json.Unmarshal(b, &rc)
	if err != nil {
		return nil, err
	}

	return &rc, nil
}

func (v *Vault) OpenRepository() (repo.Repository, error) {
	rc, err := v.RepositoryConfig()
	if err != nil {
		return nil, err
	}

	storage, err := blob.NewStorage(rc.Storage)
	if err != nil {
		return nil, fmt.Errorf("unable to open repository: %v", err)
	}

	return repo.NewRepository(storage, rc.Format)
}

func (v *Vault) Get(id string, content interface{}) error {
	j, err := v.Storage.GetBlock(id)
	if err != nil {
		return nil
	}

	return json.Unmarshal(j, content)
}

func (v *Vault) Put(id string, content interface{}) error {
	j, err := json.Marshal(content)
	if err != nil {
		return err
	}
	return v.Storage.PutBlock(id, ioutil.NopCloser(bytes.NewBuffer(j)), blob.PutOptions{Overwrite: true})
}

func (v *Vault) List(prefix string) ([]string, error) {
	var result []string

	for b := range v.Storage.ListBlocks(prefix) {
		if b.Error != nil {
			return result, b.Error<commit_msg>make vault encrypted again<commit_after>package vault

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"crypto/hmac"
	"crypto/rand"
	"crypto/sha256"
	"encoding/json"
	"fmt"
	"hash"
	"io"
	"io/ioutil"

	"github.com/kopia/kopia/blob"
	"github.com/kopia/kopia/repo"

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

const (
	formatBlock           = "format"
	checksumBlock         = "checksum"
	repositoryConfigBlock = "repo"

	minPasswordLength = 12
	minKeyLength      = 16
)

var (
	purposeAESKey         = []byte("AES")
	purposeChecksumSecret = []byte("CHECKSUM")
)

type Vault struct {
	Storage   blob.Storage
	MasterKey []byte
	Format    Format
}

func (v *Vault) writeEncryptedBlock(name string, content []byte) error {
	blk, err := v.newCipher()
	if err != nil {
		return err
	}

	hash, err := v.newChecksum()
	if err != nil {
		return err
	}

	ivLength := blk.BlockSize()
	ivPlusContentLength := ivLength + len(content)
	cipherText := make([]byte, ivPlusContentLength+hash.Size())

	// Store IV at the beginning of ciphertext.
	iv := cipherText[0:ivLength]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		return err
	}

	ctr := cipher.NewCTR(blk, iv)
	ctr.XORKeyStream(cipherText[ivLength:], content)
	hash.Write(cipherText[0:ivPlusContentLength])
	copy(cipherText[ivPlusContentLength:], hash.Sum(nil))

	return v.Storage.PutBlock(name, ioutil.NopCloser(bytes.NewBuffer(cipherText)), blob.PutOptions{
		Overwrite: true,
	})
}

func (v *Vault) readEncryptedBlock(name string) ([]byte, error) {
	cipherText, err := v.Storage.GetBlock(name)
	if err != nil {
		return nil, err
	}

	hash, err := v.newChecksum()
	if err != nil {
		return nil, err
	}

	p := len(cipherText) - hash.Size()
	hash.Write(cipherText[0:p])
	expectedChecksum := hash.Sum(nil)
	actualChecksum := cipherText[p:]
	if !hmac.Equal(expectedChecksum, actualChecksum) {
		return nil, fmt.Errorf("cannot read encrypted block: incorrect checksum")
	}

	blk, err := v.newCipher()
	if err != nil {
		return nil, err
	}

	ivLength := blk.BlockSize()

	plainText := make([]byte, len(cipherText)-ivLength-hash.Size())
	iv := cipherText[0:blk.BlockSize()]

	ctr := cipher.NewCTR(blk, iv)
	ctr.XORKeyStream(plainText, cipherText[ivLength:len(cipherText)-hash.Size()])
	return plainText, nil
}

func (v *Vault) newChecksum() (hash.Hash, error) {
	switch v.Format.Checksum {
	case "hmac-sha-256":
		key := make([]byte, 32)
		v.deriveKey(purposeChecksumSecret, key)
		return hmac.New(sha256.New, key), nil

	default:
		return nil, fmt.Errorf("unsupported checksum format: %v", v.Format.Checksum)
	}

}

func (v *Vault) newCipher() (cipher.Block, error) {
	switch v.Format.Encryption {
	case "aes-256":
		k := make([]byte, 32)
		v.deriveKey(purposeAESKey, k)
		return aes.NewCipher(k)
	default:
		return nil, fmt.Errorf("unsupported encryption format: %v", v.Format.Encryption)
	}

}

func (v *Vault) deriveKey(purpose []byte, key []byte) error {
	k := hkdf.New(sha256.New, v.MasterKey, v.Format.UniqueID, purpose)
	_, err := io.ReadFull(k, key)
	return err
}

func (v *Vault) SetRepository(rc RepositoryConfig) error {
	b, err := json.Marshal(&rc)
	if err != nil {
		return err
	}

	return v.writeEncryptedBlock(repositoryConfigBlock, b)
}

func (v *Vault) RepositoryConfig() (*RepositoryConfig, error) {
	var rc RepositoryConfig

	b, err := v.readEncryptedBlock(repositoryConfigBlock)
	if err != nil {
		return nil, fmt.Errorf("unable to read repository: %v", err)
	}

	err = json.Unmarshal(b, &rc)
	if err != nil {
		return nil, err
	}

	return &rc, nil
}

func (v *Vault) OpenRepository() (repo.Repository, error) {
	rc, err := v.RepositoryConfig()
	if err != nil {
		return nil, err
	}

	storage, err := blob.NewStorage(rc.Storage)
	if err != nil {
		return nil, fmt.Errorf("unable to open repository: %v", err)
	}

	return repo.NewRepository(storage, rc.Format)
}

func (v *Vault) Get(id string, content interface{}) error {
	j, err := v.readEncryptedBlock(id)
	if err != nil {
		return nil
	}

	return json.Unmarshal(j, content)
}

func (v *Vault) Put(id string, content interface{}) error {
	j, err := json.Marshal(content)
	if err != nil {
		return err
	}
	return v.writeEncryptedBlock(id, j)
}

func (v *Vault) List(prefix string) ([]string, error) {
	var result []string

	for b := range v.Storage.ListBlocks(prefix) {
		if b.Error != nil {
			return result, b.Error
{'fraction_non_alphanumeric': 0.15006276389364373, 'fraction_numerical': 0.006390505534634257, 'mean_word_length': 4.158328428487345, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7007917', 'n_tokens_mistral': 3449, 'n_tokens_neox': 3261, 'n_words': 776}