forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattachment_crypto.go
182 lines (142 loc) · 5.25 KB
/
attachment_crypto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package bertyprotocol
import (
"bytes"
"crypto/cipher"
"fmt"
"io"
"math/big"
libp2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
"go.uber.org/zap"
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/hkdf"
"golang.org/x/crypto/nacl/secretbox"
"golang.org/x/crypto/sha3"
"berty.tech/berty/v2/go/internal/cryptoutil"
"berty.tech/berty/v2/go/internal/streamutil"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
)
// ⚠⚠⚠ FIXME: Needs thorough security review ⚠⚠⚠
const (
attachmentCipherblockSize = 64 * 1024
attachmentNonceIV = 0
attachmentKeyV0Prefix = "/libp2psk+xchacha20poly1305_64_0/" // TODO: replace when multikey rolls out
)
// - DATA ENCRYPTION
type attachmentCipher struct {
nonce *big.Int
nonceBuf [chacha20poly1305.NonceSizeX]byte
aead cipher.AEAD
}
func attachmentNewCipher(sk libp2pcrypto.PrivKey) (*attachmentCipher, error) {
key, err := sk.Raw()
if err != nil {
return nil, errcode.ErrInvalidInput.Wrap(err)
}
if len(key) < chacha20poly1305.KeySize {
return nil, errcode.ErrInvalidInput.Wrap(fmt.Errorf("bad key size, got %d, expected to be >= %d", len(key), chacha20poly1305.KeySize))
}
aead, err := chacha20poly1305.NewX(key[:chacha20poly1305.KeySize])
if err != nil {
return nil, errcode.ErrCryptoCipherInit.Wrap(err)
}
ac := attachmentCipher{
aead: aead,
nonce: big.NewInt(attachmentNonceIV),
}
bigIntFillBytes(ac.nonce, ac.nonceBuf[:])
return &ac, nil
}
var bigOne = big.NewInt(1)
func attachmentSealer(plaintext io.Reader, l *zap.Logger) (libp2pcrypto.PrivKey, *io.PipeReader, error) {
sk, _, err := libp2pcrypto.GenerateKeyPair(libp2pcrypto.Ed25519, 0)
if err != nil {
return nil, nil, errcode.ErrCryptoKeyGeneration.Wrap(err)
}
ac, err := attachmentNewCipher(sk)
if err != nil {
return nil, nil, errcode.ErrCryptoCipherInit.Wrap(err)
}
return sk, streamutil.FuncBlockTransformer(make([]byte, attachmentCipherblockSize-16), plaintext, l, func(pt []byte) ([]byte, error) {
ct := ac.aead.Seal([]byte(nil), ac.nonceBuf[:], pt, []byte(nil))
ac.nonce.Add(ac.nonce, bigOne)
bigIntFillBytes(ac.nonce, ac.nonceBuf[:])
return ct, nil
}), nil
}
func attachmentOpener(ciphertext io.Reader, sk libp2pcrypto.PrivKey, l *zap.Logger) (*io.PipeReader, error) {
ac, err := attachmentNewCipher(sk)
if err != nil {
return nil, errcode.ErrCryptoCipherInit.Wrap(err)
}
return streamutil.FuncBlockTransformer(make([]byte, attachmentCipherblockSize), ciphertext, l, func(ct []byte) ([]byte, error) {
pt, err := ac.aead.Open([]byte(nil), ac.nonceBuf[:], ct, []byte(nil))
if err != nil {
return nil, errcode.ErrCryptoDecrypt.Wrap(err)
}
ac.nonce.Add(ac.nonce, bigOne)
bigIntFillBytes(ac.nonce, ac.nonceBuf[:])
return pt, nil
}), nil
}
// - KEY SERIALIZATION
func attachmentKeyMarshal(sk libp2pcrypto.PrivKey) ([]byte, error) {
skBytes, err := libp2pcrypto.MarshalPrivateKey(sk)
if err != nil {
return nil, errcode.ErrSerialization.Wrap(err)
}
return append([]byte(attachmentKeyV0Prefix), skBytes...), nil
}
func attachmentKeyUnmarshal(s []byte) (libp2pcrypto.PrivKey, error) {
if len(s) <= len(attachmentKeyV0Prefix) || !bytes.Equal(s[:len(attachmentKeyV0Prefix)], []byte(attachmentKeyV0Prefix)) {
return nil, errcode.ErrInvalidInput.Wrap(fmt.Errorf("invalid secret prefix %s", s))
}
skBytes := s[len(attachmentKeyV0Prefix):]
sk, err := libp2pcrypto.UnmarshalPrivateKey(skBytes)
if err != nil {
return nil, errcode.ErrDeserialization.Wrap(err)
}
return sk, nil
}
// - CID ENCRYPTION
func attachmentCIDEncryptionKey(source *[cryptoutil.KeySize]byte) (*[cryptoutil.KeySize]byte, error) {
hkdf := hkdf.New(sha3.New256, source[:], nil, []byte("cid encryption v0"))
var key [cryptoutil.KeySize]byte
if _, err := io.ReadFull(hkdf, key[:]); err != nil {
return nil, errcode.ErrStreamRead.Wrap(err)
}
return &key, nil
}
func attachmentCIDEncrypt(sk *[cryptoutil.KeySize]byte, cid []byte) ([]byte, error) {
nonce, err := cryptoutil.GenerateNonce()
if err != nil {
return nil, errcode.ErrCryptoNonceGeneration.Wrap(err)
}
return append(nonce[:], secretbox.Seal(nil, cid, nonce, sk)...), nil
}
func attachmentCIDDecrypt(sk *[cryptoutil.KeySize]byte, eCID []byte) ([]byte, error) {
if len(eCID) <= cryptoutil.NonceSize {
return nil, errcode.ErrInvalidInput.Wrap(fmt.Errorf("encrypted cid too small, got %v, expected to be > %v", len(eCID), cryptoutil.NonceSize))
}
var nonce [cryptoutil.NonceSize]byte
_ = copy(nonce[:], eCID[:cryptoutil.NonceSize])
cid, ok := secretbox.Open(nil, eCID[cryptoutil.NonceSize:], &nonce, sk)
if !ok {
return nil, errcode.ErrCryptoDecrypt
}
return cid, nil
}
func attachmentCIDSliceEncrypt(g *protocoltypes.Group, cids [][]byte) ([][]byte, error) {
sk, err := attachmentCIDEncryptionKey(g.GetSharedSecret())
if err != nil {
return nil, errcode.ErrCryptoKeyDerivation.Wrap(err)
}
return mapBufArray(cids, func(cid []byte) ([]byte, error) { return attachmentCIDEncrypt(sk, cid) })
}
func attachmentCIDSliceDecrypt(g *protocoltypes.Group, eCIDs [][]byte) ([][]byte, error) {
sk, err := attachmentCIDEncryptionKey(g.GetSharedSecret())
if err != nil {
return nil, errcode.ErrCryptoKeyDerivation.Wrap(err)
}
return mapBufArray(eCIDs, func(eCID []byte) ([]byte, error) { return attachmentCIDDecrypt(sk, eCID) })
}