-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
encrypted_data.go
386 lines (336 loc) · 11 KB
/
encrypted_data.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Copyright 2017 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package kbfscrypto
import (
"bytes"
"encoding/hex"
"fmt"
"reflect"
"github.com/keybase/client/go/kbfs/cache"
"github.com/keybase/client/go/kbfs/kbfscodec"
"github.com/keybase/client/go/kbfs/kbfshash"
"github.com/keybase/client/go/libkb"
"github.com/pkg/errors"
"golang.org/x/crypto/nacl/box"
"golang.org/x/crypto/nacl/secretbox"
)
// EncryptionVer denotes a version for the encryption method.
type EncryptionVer int
const (
// EncryptionSecretbox is the encryption version that uses
// nacl/secretbox or nacl/box.
EncryptionSecretbox EncryptionVer = 1
// EncryptionSecretboxWithKeyNonce is the encryption version that
// uses nacl/secretbox or nacl/box, with a nonce derived from a
// secret key.
EncryptionSecretboxWithKeyNonce EncryptionVer = 2
)
func (v EncryptionVer) String() string {
switch v {
case EncryptionSecretbox:
return "EncryptionSecretbox"
case EncryptionSecretboxWithKeyNonce:
return "EncryptionSecretboxWithKeyNonce"
default:
return fmt.Sprintf("EncryptionVer(%d)", v)
}
}
// ToHashType returns the type of the hash that should be used for the
// given encryption version.
func (v EncryptionVer) ToHashType() kbfshash.HashType {
switch v {
case EncryptionSecretbox:
return kbfshash.SHA256Hash
case EncryptionSecretboxWithKeyNonce:
return kbfshash.SHA256HashV2
default:
return kbfshash.InvalidHash
}
}
// encryptedData is encrypted data with a nonce and a version.
type encryptedData struct {
// Exported only for serialization purposes. Should only be
// used by implementations of Crypto.
Version EncryptionVer `codec:"v"`
EncryptedData []byte `codec:"e"`
Nonce []byte `codec:"n"`
}
// Size implements the cache.Measurable interface.
func (ed encryptedData) Size() int {
return cache.IntSize /* ed.Version */ +
cache.PtrSize + len(ed.EncryptedData) + cache.PtrSize + len(ed.Nonce)
}
func (ed encryptedData) String() string {
if reflect.DeepEqual(ed, encryptedData{}) {
return "EncryptedData{}"
}
return fmt.Sprintf("%s{data=%s, nonce=%s}",
ed.Version, hex.EncodeToString(ed.EncryptedData),
hex.EncodeToString(ed.Nonce))
}
func (ed encryptedData) Nonce24() (nonce [24]byte, err error) {
if len(ed.Nonce) != len(nonce) {
return nonce, errors.WithStack(InvalidNonceError{ed.Nonce})
}
copy(nonce[:], ed.Nonce)
return nonce, nil
}
// encryptDataWithNonce encrypts the given data with the given
// symmetric key and nonce.
func encryptDataWithNonce(
data []byte, key [32]byte, nonce [24]byte, ver EncryptionVer) (
encryptedData, error) {
sealedData := secretbox.Seal(nil, data, &nonce, &key)
return encryptedData{
Version: ver,
Nonce: nonce[:],
EncryptedData: sealedData,
}, nil
}
// encryptData encrypts the given data with the given symmetric key.
func encryptData(data []byte, key [32]byte) (encryptedData, error) {
var nonce [24]byte
err := RandRead(nonce[:])
if err != nil {
return encryptedData{}, err
}
return encryptDataWithNonce(data, key, nonce, EncryptionSecretbox)
}
// decryptData decrypts the given encrypted data with the given
// symmetric key and nonce.
func decryptData(
encryptedData encryptedData, key [32]byte, nonce [24]byte) ([]byte, error) {
switch encryptedData.Version {
case EncryptionSecretbox:
// We're good, no nonce check needed.
case EncryptionSecretboxWithKeyNonce:
if !bytes.Equal(nonce[:], encryptedData.Nonce) {
return nil, errors.WithStack(InvalidNonceError{encryptedData.Nonce})
}
default:
return nil, errors.WithStack(
UnknownEncryptionVer{encryptedData.Version})
}
decryptedData, ok := secretbox.Open(
nil, encryptedData.EncryptedData, &nonce, &key)
if !ok {
return nil, errors.WithStack(
libkb.DecryptionError{Cause: libkb.ErrorCause{Err: errors.New("Cannot open secret box")}})
}
return decryptedData, nil
}
// EncryptedTLFCryptKeyClientHalf is an encrypted
// TLFCryptKeyClientHalf object.
type EncryptedTLFCryptKeyClientHalf struct {
encryptedData
}
// EncryptTLFCryptKeyClientHalf encrypts a TLFCryptKeyClientHalf
// using both a TLF's ephemeral private key and a device pubkey.
func EncryptTLFCryptKeyClientHalf(
privateKey TLFEphemeralPrivateKey, publicKey CryptPublicKey,
clientHalf TLFCryptKeyClientHalf) (
encryptedClientHalf EncryptedTLFCryptKeyClientHalf, err error) {
var nonce [24]byte
err = RandRead(nonce[:])
if err != nil {
return EncryptedTLFCryptKeyClientHalf{}, err
}
keypair, err := libkb.ImportKeypairFromKID(publicKey.KID())
if err != nil {
return EncryptedTLFCryptKeyClientHalf{}, errors.WithStack(err)
}
dhKeyPair, ok := keypair.(libkb.NaclDHKeyPair)
if !ok {
return EncryptedTLFCryptKeyClientHalf{}, errors.WithStack(
libkb.KeyCannotEncryptError{})
}
clientHalfData := clientHalf.Data()
privateKeyData := privateKey.Data()
encryptedBytes := box.Seal(nil, clientHalfData[:], &nonce, (*[32]byte)(&dhKeyPair.Public), &privateKeyData)
return EncryptedTLFCryptKeyClientHalf{
encryptedData{
Version: EncryptionSecretbox,
EncryptedData: encryptedBytes,
Nonce: nonce[:],
},
}, nil
}
// EncryptedPrivateMetadata is an encrypted PrivateMetadata object.
type EncryptedPrivateMetadata struct {
encryptedData
}
// EncryptEncodedPrivateMetadata encrypts an encoded PrivateMetadata
// object.
func EncryptEncodedPrivateMetadata(encodedPrivateMetadata []byte, key TLFCryptKey) (
encryptedPrivateMetadata EncryptedPrivateMetadata, err error) {
encryptedData, err := encryptData(encodedPrivateMetadata, key.Data())
if err != nil {
return EncryptedPrivateMetadata{}, err
}
return EncryptedPrivateMetadata{encryptedData}, nil
}
// DecryptPrivateMetadata decrypts a PrivateMetadata object, but does
// not decode it.
func DecryptPrivateMetadata(
encryptedPrivateMetadata EncryptedPrivateMetadata, key TLFCryptKey) (
[]byte, error) {
if encryptedPrivateMetadata.encryptedData.Version ==
EncryptionSecretboxWithKeyNonce {
// Only blocks should have v2 encryption.
return nil, errors.WithStack(InvalidEncryptionVer{
encryptedPrivateMetadata.encryptedData.Version})
}
nonce, err := encryptedPrivateMetadata.encryptedData.Nonce24()
if err != nil {
return nil, err
}
return decryptData(
encryptedPrivateMetadata.encryptedData, key.Data(), nonce)
}
// EncryptedBlock is an encrypted Block object.
type EncryptedBlock struct {
encryptedData
}
// EncryptPaddedEncodedBlock encrypts a padded, encoded block.
func EncryptPaddedEncodedBlock(
paddedEncodedBlock []byte, tlfCryptKey TLFCryptKey,
blockServerHalf BlockCryptKeyServerHalf, ver EncryptionVer) (
encryptedBlock EncryptedBlock, err error) {
var ed encryptedData
switch ver {
case EncryptionSecretbox:
key := UnmaskBlockCryptKey(blockServerHalf, tlfCryptKey)
ed, err = encryptData(paddedEncodedBlock, key.Data())
if err != nil {
return EncryptedBlock{}, err
}
case EncryptionSecretboxWithKeyNonce:
key := MakeBlockHashKey(blockServerHalf, tlfCryptKey)
ed, err = encryptDataWithNonce(
paddedEncodedBlock, key.cryptKey(), key.nonce(),
EncryptionSecretboxWithKeyNonce)
if err != nil {
return EncryptedBlock{}, err
}
default:
return EncryptedBlock{}, errors.WithStack(UnknownEncryptionVer{ver})
}
return EncryptedBlock{ed}, nil
}
// DecryptBlock decrypts a block, but does not unpad or decode it.
func DecryptBlock(
encryptedBlock EncryptedBlock, tlfCryptKey TLFCryptKey,
blockServerHalf BlockCryptKeyServerHalf) ([]byte, error) {
switch encryptedBlock.encryptedData.Version {
case EncryptionSecretbox:
nonce, err := encryptedBlock.encryptedData.Nonce24()
if err != nil {
return nil, err
}
key := UnmaskBlockCryptKey(blockServerHalf, tlfCryptKey)
return decryptData(encryptedBlock.encryptedData, key.Data(), nonce)
case EncryptionSecretboxWithKeyNonce:
key := MakeBlockHashKey(blockServerHalf, tlfCryptKey)
return decryptData(
encryptedBlock.encryptedData, key.cryptKey(), key.nonce())
default:
return nil, errors.WithStack(
InvalidEncryptionVer{encryptedBlock.encryptedData.Version})
}
}
// EncryptedTLFCryptKeys is an encrypted TLFCryptKey array.
type EncryptedTLFCryptKeys struct {
encryptedData
}
// EncryptTLFCryptKeys encrypts a TLFCryptKey array.
func EncryptTLFCryptKeys(codec kbfscodec.Codec, oldKeys []TLFCryptKey, key TLFCryptKey) (
encryptedTLFCryptKeys EncryptedTLFCryptKeys, err error) {
encodedKeys, err := codec.Encode(oldKeys)
if err != nil {
return EncryptedTLFCryptKeys{}, err
}
encryptedData, err := encryptData(encodedKeys, key.Data())
if err != nil {
return EncryptedTLFCryptKeys{}, err
}
return EncryptedTLFCryptKeys{encryptedData}, nil
}
// DecryptTLFCryptKeys decrypts a TLFCryptKey array, but does not
// decode it.
func DecryptTLFCryptKeys(
codec kbfscodec.Codec, encryptedTLFCryptKeys EncryptedTLFCryptKeys, key TLFCryptKey) (
[]TLFCryptKey, error) {
if encryptedTLFCryptKeys.encryptedData.Version ==
EncryptionSecretboxWithKeyNonce {
// Only blocks should have v2 encryption.
return nil, errors.WithStack(
InvalidEncryptionVer{encryptedTLFCryptKeys.encryptedData.Version})
}
nonce, err := encryptedTLFCryptKeys.encryptedData.Nonce24()
if err != nil {
return nil, err
}
encodedKeys, err := decryptData(
encryptedTLFCryptKeys.encryptedData, key.Data(), nonce)
if err != nil {
return nil, err
}
var oldKeys []TLFCryptKey
err = codec.Decode(encodedKeys, &oldKeys)
if err != nil {
return nil, err
}
return oldKeys, nil
}
// EncryptedMerkleLeaf is an encrypted MerkleLeaf object.
type EncryptedMerkleLeaf struct {
encryptedData
}
// MakeEncryptedMerkleLeaf constructs an EncryptedMerkleLeaf.
func MakeEncryptedMerkleLeaf(
version EncryptionVer, data []byte, nonce *[24]byte) EncryptedMerkleLeaf {
return EncryptedMerkleLeaf{
encryptedData{
Version: version,
EncryptedData: data,
Nonce: nonce[:],
},
}
}
// PrepareMerkleLeaf verifies the correctness of the given leaf, and
// returns its nonce.
func PrepareMerkleLeaf(encryptedMerkleLeaf EncryptedMerkleLeaf) (
nonce [24]byte, err error) {
if encryptedMerkleLeaf.Version != EncryptionSecretbox {
return nonce,
errors.WithStack(UnknownEncryptionVer{
Ver: encryptedMerkleLeaf.Version})
}
if len(encryptedMerkleLeaf.Nonce) != len(nonce) {
return nonce,
errors.WithStack(InvalidNonceError{
Nonce: encryptedMerkleLeaf.Nonce})
}
copy(nonce[:], encryptedMerkleLeaf.Nonce)
return nonce, nil
}
// DecryptMerkleLeaf decrypts an EncryptedMerkleLeaf using the given
// private TLF key and ephemeral public key.
func DecryptMerkleLeaf(
privateKey TLFPrivateKey, publicKey TLFEphemeralPublicKey,
encryptedMerkleLeaf EncryptedMerkleLeaf) ([]byte, error) {
nonce, err := PrepareMerkleLeaf(encryptedMerkleLeaf)
if err != nil {
return nil, err
}
publicKeyData := publicKey.Data()
privateKeyData := privateKey.Data()
decryptedData, ok := box.Open(nil, encryptedMerkleLeaf.EncryptedData,
&nonce, &publicKeyData, &privateKeyData)
if !ok {
return nil, errors.WithStack(
libkb.DecryptionError{Cause: libkb.ErrorCause{Err: errors.New("Cannot open box")}})
}
return decryptedData, nil
}