This repository has been archived by the owner on Feb 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
/
encrypted_data.go
230 lines (193 loc) · 6.33 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
// 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 (
"encoding/hex"
"fmt"
"reflect"
"github.com/keybase/client/go/libkb"
"github.com/keybase/kbfs/cache"
"github.com/keybase/kbfs/kbfscodec"
"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
)
func (v EncryptionVer) String() string {
switch v {
case EncryptionSecretbox:
return "EncryptionSecretbox"
default:
return fmt.Sprintf("EncryptionVer(%d)", v)
}
}
// 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))
}
// 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
}
sealedData := secretbox.Seal(nil, data, &nonce, &key)
return encryptedData{
Version: EncryptionSecretbox,
Nonce: nonce[:],
EncryptedData: sealedData,
}, nil
}
// decryptData decrypts the given encrypted data with the given
// symmetric key.
func decryptData(encryptedData encryptedData, key [32]byte) ([]byte, error) {
if encryptedData.Version != EncryptionSecretbox {
return nil, errors.WithStack(
UnknownEncryptionVer{encryptedData.Version})
}
var nonce [24]byte
if len(encryptedData.Nonce) != len(nonce) {
return nil, errors.WithStack(
InvalidNonceError{encryptedData.Nonce})
}
copy(nonce[:], encryptedData.Nonce)
decryptedData, ok := secretbox.Open(
nil, encryptedData.EncryptedData, &nonce, &key)
if !ok {
return nil, errors.WithStack(libkb.DecryptionError{})
}
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) {
return decryptData(encryptedPrivateMetadata.encryptedData, key.Data())
}
// EncryptedBlock is an encrypted Block object.
type EncryptedBlock struct {
encryptedData
}
// EncryptPaddedEncodedBlock encrypts a padded, encoded block.
func EncryptPaddedEncodedBlock(paddedEncodedBlock []byte, key BlockCryptKey) (
encryptedBlock EncryptedBlock, err error) {
encryptedData, err := encryptData(paddedEncodedBlock, key.Data())
if err != nil {
return EncryptedBlock{}, err
}
return EncryptedBlock{encryptedData}, nil
}
// DecryptBlock decrypts a block, but does not unpad or decode it.
func DecryptBlock(encryptedBlock EncryptedBlock, key BlockCryptKey) (
[]byte, error) {
return decryptData(encryptedBlock.encryptedData, key.Data())
}
// 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) {
encodedKeys, err := decryptData(encryptedTLFCryptKeys.encryptedData, key.Data())
if err != nil {
return nil, err
}
var oldKeys []TLFCryptKey
err = codec.Decode(encodedKeys, &oldKeys)
if err != nil {
return nil, err
}
return oldKeys, nil
}