Debian Codegraph @

encrypt.go

raw
1 package desync
2
3 import (
4 "bytes"
5 "crypto/aes"
6 "crypto/cipher"
7 "crypto/rand"
8 "crypto/sha256"
9 "errors"
10 "fmt"
11
12 "golang.org/x/crypto/chacha20poly1305"
13 )
14
15 // EncryptionKeySize is the size of the encryption key in bytes that is
16 // expected by the encryption converters. All supported algorithms use
17 // 256-bit keys.
18 const EncryptionKeySize = 32
19
20 // aeadConverter is an encryption layer for chunk storage. It
21 // encrypts/decrypts to/from storage with an AEAD algorithm using
22 // a 256-bit key. A random nonce is generated for every chunk and
23 // prepended to the ciphertext.
24 type aeadConverter struct {
25 algorithm string
26 key []byte
27 aead cipher.AEAD
28
29 // Chunk extension with identifier derived from the key.
30 extension string
31 }
32
33 var _ converter = aeadConverter{}
34
35 // NewXChaCha20Poly1305 returns a converter that encrypts chunks with
36 // XChaCha20-Poly1305 using the given 256-bit key.
37 func NewXChaCha20Poly1305(key []byte) (aeadConverter, error) {
38 if err := validateKey(key); err != nil {
39 return aeadConverter{}, err
40 }
41 aead, err := chacha20poly1305.NewX(key)
42 if err != nil {
43 return aeadConverter{}, err
44 }
45 return newAEADConverter("xchacha20-poly1305", key, aead), nil
46 }
47
48 // NewAES256GCM returns a converter that encrypts chunks with
49 // AES-256-GCM using the given 256-bit key.
50 func NewAES256GCM(key []byte) (aeadConverter, error) {
51 if err := validateKey(key); err != nil {
52 return aeadConverter{}, err
53 }
54 block, err := aes.NewCipher(key)
55 if err != nil {
56 return aeadConverter{}, err
57 }
58 aead, err := cipher.NewGCM(block)
59 if err != nil {
60 return aeadConverter{}, err
61 }
62 return newAEADConverter("aes-256-gcm", key, aead), nil
63 }
64
65 func newAEADConverter(algorithm string, key []byte, aead cipher.AEAD) aeadConverter {
66 // The extension contains a key ID, the leading 4 bytes of the SHA256
67 // hash of the key, to allow chunks encrypted with different keys to
68 // live in the same store without conflict.
69 keyHash := sha256.Sum256(key)
70 extension := fmt.Sprintf(".%s-%x", algorithm, keyHash[:4])
71 return aeadConverter{algorithm: algorithm, key: key, aead: aead, extension: extension}
72 }
73
74 // validateKey confirms the key is of the expected size for the
75 // supported 256-bit algorithms.
76 func validateKey(key []byte) error {
77 if len(key) != EncryptionKeySize {
78 return fmt.Errorf("encryption key must be %d bytes, got %d", EncryptionKeySize, len(key))
79 }
80 return nil
81 }
82
83 // encrypt for storage. The nonce is prepended to the data.
84 func (d aeadConverter) toStorage(in []byte) ([]byte, error) {
85 out := make([]byte, d.aead.NonceSize(), d.aead.NonceSize()+len(in)+d.aead.Overhead())
86 nonce := out[:d.aead.NonceSize()]
87 if _, err := rand.Read(nonce); err != nil {
88 return nil, err
89 }
90 return d.aead.Seal(out, nonce, in, nil), nil
91 }
92
93 // decrypt from storage. The nonce is taken from the start of the
94 // chunk data.
95 func (d aeadConverter) fromStorage(in []byte) ([]byte, error) {
96 if len(in) < d.aead.NonceSize() {
97 return nil, errors.New("no nonce prefix found in chunk, not encrypted or wrong algorithm")
98 }
99 nonce := in[:d.aead.NonceSize()]
100 return d.aead.Open(nil, nonce, in[d.aead.NonceSize():], nil)
101 }
102
103 func (d aeadConverter) equal(c converter) bool {
104 other, ok := c.(aeadConverter)
105 if !ok {
106 return false
107 }
108 return d.algorithm == other.algorithm && bytes.Equal(d.key, other.key)
109 }
110
111 func (d aeadConverter) storageExtension() string {
112 return d.extension
113 }