-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt.go
404 lines (361 loc) · 10.2 KB
/
crypt.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package dshards
import (
"bytes"
"crypto"
"crypto/cipher"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/binary"
"fmt"
"math/big"
)
// SymmetricKey types these bytes as a symmetric key, which should be treated
// with the same care as a private key.
type SymmetricKey []byte
// PrivateShard is encrypted content that contains the symmetric key. Sharing a
// PrivateShard grants access to the decrypted content.
type PrivateShard struct {
// Encrypted shard content.
Content []byte
// The IDSC address. Sharing this publicly also shares the symmetric
// key.
AddressAndKey IDSC
}
// PublicShard returns a public version of this shard, which allows sharing the
// encrypted content without also granting access to the decrypted content.
//
// Note that the PublicShard and PrivateShard's Content share the same
// underlying backing byte slice to save memory.
func (p PrivateShard) PublicShard() (PublicShard, error) {
urn, err := p.AddressAndKey.URN()
return PublicShard{
Content: p.Content,
Address: urn,
}, err
}
// PublicShard is encrypted content lacking the symmetric key. Sharing a
// PublicShard does not grant access to the decrypted contents.
type PublicShard struct {
// Encrypted shard content.
Content []byte
// The URN address. Sharing this publicly allows everyone to freely
// find the address to this bit of content without sharing the
// symmetric key.
Address URN
}
// Encrypt applies the Datashards encryption and sharding algorithm.
func Encrypt(plain []byte, key SymmetricKey, s Suite) (rootIdx int, priv []PrivateShard, err error) {
r := raw{content: plain}
return encrypt(r, key, s)
}
func encrypt(c chunker, key SymmetricKey, s Suite) (rootIdx int, priv []PrivateShard, err error) {
var plain [][]byte
plain, err = c.Chunk()
if err != nil {
return
}
priv = make([]PrivateShard, len(plain))
m := manifest{
urns: make([]URN, len(plain)),
}
// Use entry-point IV if content fits within a single shard. Otherwise,
// use the content IV.
var ivFn ivFunc
if len(plain) == 1 {
ivFn = ivEntryPoint
} else {
ivFn = ivContent
}
for i, plainChunk := range plain {
priv[i], err = encryptChunk(plainChunk, key, s, uint64(i), ivFn)
if err != nil {
return
}
m.urns[i], err = priv[i].AddressAndKey.URN()
if err != nil {
return
}
}
if len(priv) == 1 {
rootIdx = 0
} else {
var more []PrivateShard
rootIdx, more, err = encrypt(m, key, s)
if err != nil {
return
}
rootIdx += len(priv)
priv = append(priv, more...)
}
return
}
func encryptChunk(plain []byte, key SymmetricKey, s Suite, ctr uint64, ivFn ivFunc) (priv PrivateShard, err error) {
var block cipher.Block
block, err = s.blockCipher(key)
if err != nil {
return
}
var ch crypto.Hash
ch, err = s.ivHash()
if err != nil {
return
}
var iv []byte
iv, err = ivFn(ctr, key)
if err != nil {
return
}
h := ch.New()
h.Write(iv)
ivo := h.Sum(nil)
ivt := ivo[:block.BlockSize()]
stream := cipher.NewCTR(block, ivt)
ciphertext := make([]byte, len(plain))
stream.XORKeyStream(ciphertext, plain)
var idsc IDSC
idsc, err = NewIDSC(s, ciphertext, key)
if err != nil {
return
}
priv = PrivateShard{
Content: ciphertext,
AddressAndKey: idsc,
}
return
}
// Result holds one and only one outcome of a decrypt operation. It may be
// needed for future decryption calls.
type Result struct {
fetch []URN
content []byte
// Internal: If a manifest exists, the length of the content specified
// in the manifest. Set when 'fetch' is set.
contentLen int64
}
// ToFetch contains additional URN addresses to obtain and decrypt using
// DecryptAll. The SymmetricKey for the additional URNs is the same as
// the SymmetricKey used in the PrivateShard that gave this Result.
//
// How to obtain the additional URN addresses is up to the client. Order of
// results is significant: future calls to decrypt must supply shards in the
// same order as listed by the URNs.
//
// Empty if no more to fetch.
func (r *Result) ToFetch() []URN {
return r.fetch
}
// Content is the unencrypted content.
func (r *Result) Content() []byte {
return r.content
}
// Decrypt applies the Datashards decryption algorithm onto the root Datashard.
//
// The Result will either indicate that more data is needed or provide the
// decrypted content. If more data is needed, use DecryptFetchedResult.
func Decrypt(root PrivateShard, s Suite) (r *Result, err error) {
var pt []byte
pt, err = decryptChunk(root.Content, root.AddressAndKey.symmKey, s, 0, ivEntryPoint)
if err != nil {
return
}
r, err = decode(pt)
return
}
// DecryptFetchedResult applies the Datashards decryption algorithm to the
// Datashards obtained from a Result that indicated more data was needed in
// ToFetch.
//
// The results in priv must be in the same order as listed in the Result.
func DecryptFetchedResult(prev *Result, priv []PrivateShard, s Suite) (next *Result, err error) {
next = &Result{}
// Decrypt all chunks.
for i, pr := range priv {
var pt []byte
pt, err = decryptChunk(pr.Content, pr.AddressAndKey.symmKey, s, uint64(i), ivContent)
if err != nil {
return
}
var r *Result
r, err = decode(pt)
if err != nil {
return
} else if len(r.fetch) > 0 {
err = fmt.Errorf("malformed datashard: decrypting %dth fetched results encountered unexpected type %q", i, kManifest)
return
}
next.content = append(next.content, r.content...)
}
// Check the length and maybe eliminate padding.
if int64(len(next.content)) < prev.contentLen {
err = fmt.Errorf("malformed datashard: decrypting yielded %d of %d bytes", len(next.content), prev.contentLen)
return
} else if int64(len(next.content)) > prev.contentLen {
next.content = next.content[:prev.contentLen]
}
// Determine if this is a manifest -- an error will arise if it is not.
maybeManifest, errM := decode(next.content)
if errM == nil {
next = maybeManifest
} else {
// Ignore, it is raw content
}
return
}
func decryptChunk(ciphertext []byte, key SymmetricKey, s Suite, ctr uint64, ivFn ivFunc) (plaintext []byte, err error) {
var block cipher.Block
block, err = s.blockCipher(key)
if err != nil {
return
}
var ch crypto.Hash
ch, err = s.ivHash()
if err != nil {
return
}
var iv []byte
iv, err = ivFn(ctr, key)
if err != nil {
return
}
h := ch.New()
h.Write(iv)
ivo := h.Sum(nil)
ivt := ivo[:block.BlockSize()]
stream := cipher.NewCTR(block, ivt)
plaintext = make([]byte, len(ciphertext))
stream.XORKeyStream(plaintext, ciphertext)
return
}
// Initialization vector
const (
ivEntryPointPrefix = "entry-point"
ivContentPrefix = "content"
)
type ivFunc func(ctr uint64, key SymmetricKey) ([]byte, error)
// Generates the initialization vector required for the first entry-point
// datashard.
//
// The initialization vector is not truncated to any particular size.
func ivEntryPoint(ctr uint64, key SymmetricKey) ([]byte, error) {
return generateIV(ivEntryPointPrefix, ctr, key)
}
// Generates the initialization vector required for a content datashard.
//
// The initialization vector is not truncated to any particular size.
func ivContent(ctr uint64, key SymmetricKey) ([]byte, error) {
return generateIV(ivContentPrefix, ctr, key)
}
// generateIV applies the initialization vector creation algorithm.
func generateIV(prefix string, ctr uint64, key SymmetricKey) ([]byte, error) {
cbuf := new(bytes.Buffer)
err := binary.Write(cbuf, binary.LittleEndian, ctr)
return append([]byte(prefix), append(cbuf.Bytes(), key...)...), err
}
// MDSC
// toReadKey generates the read symmetric key from the write symmetric key.
func toReadKey(writeKey SymmetricKey) SymmetricKey {
r := sha256.Sum256(writeKey)
return r[:]
}
type pubKey struct {
N *big.Int `syrup:"n"`
E int `syrup:"e"`
}
type privKey struct {
D *big.Int `syrup:"d"`
Dp *big.Int `syrup:"dp"`
Dq *big.Int `syrup:"dq"`
E int `syrup:"e"`
N *big.Int `syrup:"n"`
P *big.Int `syrup:"p"`
Q *big.Int `syrup:"q"`
QInv *big.Int `syrup:"qInv"`
}
// Encrypts the write key for a KeyData entry.
func encryptWriteKey(plain []byte, key SymmetricKey, s Suite) (enc []byte, err error) {
var block cipher.Block
block, err = s.blockCipher(key)
if err != nil {
return
}
// Zeroed IV
iv := make([]byte, block.BlockSize())
stream := cipher.NewCTR(block, iv)
enc = make([]byte, len(plain))
stream.XORKeyStream(enc, plain)
return
}
// Decrypts the write key for a KeyData entry.
func decryptWriteKey(crypt []byte, key SymmetricKey, s Suite) (plain []byte, err error) {
var block cipher.Block
block, err = s.blockCipher(key)
if err != nil {
return
}
// Zeroed IV
iv := make([]byte, block.BlockSize())
stream := cipher.NewCTR(block, iv)
plain = make([]byte, len(crypt))
stream.XORKeyStream(plain, crypt)
return
}
// Encrypts the URN in a history entry
func encryptURN(plain []byte, key SymmetricKey, s Suite) (crypt, iv []byte, err error) {
var block cipher.Block
block, err = s.blockCipher(key)
if err != nil {
return
}
iv = make([]byte, block.BlockSize())
var n int
n, err = rand.Read(iv)
if err != nil {
return
} else if n != block.BlockSize() {
err = fmt.Errorf("crypto/rand read %d of %d bytes", n, block.BlockSize())
return
}
stream := cipher.NewCTR(block, iv)
crypt = make([]byte, len(plain))
stream.XORKeyStream(crypt, plain)
return
}
// Decrypts the URN in a history entry
func decryptURN(crypt, iv []byte, key SymmetricKey, s Suite) (plain []byte, err error) {
var block cipher.Block
block, err = s.blockCipher(key)
if err != nil {
return
}
stream := cipher.NewCTR(block, iv)
plain = make([]byte, len(crypt))
stream.XORKeyStream(plain, crypt)
return
}
// Signs the revision in a history entry
func signRevision(priv *rsa.PrivateKey, toSign []byte, s Suite) (sig []byte, err error) {
var ch crypto.Hash
ch, err = s.historySignatureHash()
if err != nil {
return
}
h := ch.New()
h.Write(toSign)
hashToSign := h.Sum(nil)
sig, err = rsa.SignPKCS1v15(rand.Reader, priv, ch, hashToSign)
return
}
// Verifies the revision in a history entry
func verifyRevision(pub *rsa.PublicKey, toVerify, sig []byte, s Suite) (err error) {
var ch crypto.Hash
ch, err = s.historySignatureHash()
if err != nil {
return
}
h := ch.New()
h.Write(toVerify)
hashToVerify := h.Sum(nil)
err = rsa.VerifyPKCS1v15(pub, ch, hashToVerify, sig)
return
}