-
Notifications
You must be signed in to change notification settings - Fork 338
/
decrypt_store.go
92 lines (76 loc) · 2.45 KB
/
decrypt_store.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
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package store
import (
"context"
"encoding/binary"
"github.com/ethersphere/bee/pkg/encryption"
storage "github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
"golang.org/x/crypto/sha3"
)
type decryptingStore struct {
storage.Getter
}
func New(s storage.Getter) storage.Getter {
return &decryptingStore{s}
}
func (s *decryptingStore) Get(ctx context.Context, addr swarm.Address) (ch swarm.Chunk, err error) {
switch l := len(addr.Bytes()); l {
case swarm.HashSize:
// normal, unencrypted content
return s.Getter.Get(ctx, addr)
case encryption.ReferenceSize:
// encrypted reference
ref := addr.Bytes()
address := swarm.NewAddress(ref[:swarm.HashSize])
ch, err := s.Getter.Get(ctx, address)
if err != nil {
return nil, err
}
d, err := decryptChunkData(ch.Data(), ref[swarm.HashSize:])
if err != nil {
return nil, err
}
return swarm.NewChunk(address, d), nil
default:
return nil, storage.ErrReferenceLength
}
}
func decryptChunkData(chunkData []byte, encryptionKey encryption.Key) ([]byte, error) {
decryptedSpan, decryptedData, err := decrypt(chunkData, encryptionKey)
if err != nil {
return nil, err
}
// removing extra bytes which were just added for padding
length := binary.LittleEndian.Uint64(decryptedSpan)
refSize := int64(swarm.HashSize + encryption.KeyLength)
for length > swarm.ChunkSize {
length = length + (swarm.ChunkSize - 1)
length = length / swarm.ChunkSize
length *= uint64(refSize)
}
c := make([]byte, length+8)
copy(c[:8], decryptedSpan)
copy(c[8:], decryptedData[:length])
return c, nil
}
func decrypt(chunkData []byte, key encryption.Key) ([]byte, []byte, error) {
decryptedSpan, err := newSpanEncryption(key).Decrypt(chunkData[:swarm.SpanSize])
if err != nil {
return nil, nil, err
}
decryptedData, err := newDataEncryption(key).Decrypt(chunkData[swarm.SpanSize:])
if err != nil {
return nil, nil, err
}
return decryptedSpan, decryptedData, nil
}
func newSpanEncryption(key encryption.Key) encryption.Interface {
refSize := int64(swarm.HashSize + encryption.KeyLength)
return encryption.New(key, 0, uint32(swarm.ChunkSize/refSize), sha3.NewLegacyKeccak256)
}
func newDataEncryption(key encryption.Key) encryption.Interface {
return encryption.New(key, int(swarm.ChunkSize), 0, sha3.NewLegacyKeccak256)
}