aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/storage/hasherstore.go
blob: 766207eae0d877a9f2fd552a4d8e15ba05093e2d (plain) (blame)
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
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package storage

import (
    "context"
    "fmt"
    "sync"

    "github.com/ethereum/go-ethereum/crypto/sha3"
    "github.com/ethereum/go-ethereum/swarm/chunk"
    "github.com/ethereum/go-ethereum/swarm/storage/encryption"
)

type hasherStore struct {
    store     ChunkStore
    toEncrypt bool
    hashFunc  SwarmHasher
    hashSize  int   // content hash size
    refSize   int64 // reference size (content hash + possibly encryption key)
    wg        *sync.WaitGroup
    closed    chan struct{}
}

// NewHasherStore creates a hasherStore object, which implements Putter and Getter interfaces.
// With the HasherStore you can put and get chunk data (which is just []byte) into a ChunkStore
// and the hasherStore will take core of encryption/decryption of data if necessary
func NewHasherStore(chunkStore ChunkStore, hashFunc SwarmHasher, toEncrypt bool) *hasherStore {
    hashSize := hashFunc().Size()
    refSize := int64(hashSize)
    if toEncrypt {
        refSize += encryption.KeyLength
    }

    return &hasherStore{
        store:     chunkStore,
        toEncrypt: toEncrypt,
        hashFunc:  hashFunc,
        hashSize:  hashSize,
        refSize:   refSize,
        wg:        &sync.WaitGroup{},
        closed:    make(chan struct{}),
    }
}

// Put stores the chunkData into the ChunkStore of the hasherStore and returns the reference.
// If hasherStore has a chunkEncryption object, the data will be encrypted.
// Asynchronous function, the data will not necessarily be stored when it returns.
func (h *hasherStore) Put(ctx context.Context, chunkData ChunkData) (Reference, error) {
    c := chunkData
    size := chunkData.Size()
    var encryptionKey encryption.Key
    if h.toEncrypt {
        var err error
        c, encryptionKey, err = h.encryptChunkData(chunkData)
        if err != nil {
            return nil, err
        }
    }
    chunk := h.createChunk(c, size)

    h.storeChunk(ctx, chunk)

    return Reference(append(chunk.Addr, encryptionKey...)), nil
}

// Get returns data of the chunk with the given reference (retrieved from the ChunkStore of hasherStore).
// If the data is encrypted and the reference contains an encryption key, it will be decrypted before
// return.
func (h *hasherStore) Get(ctx context.Context, ref Reference) (ChunkData, error) {
    key, encryptionKey, err := parseReference(ref, h.hashSize)
    if err != nil {
        return nil, err
    }
    toDecrypt := (encryptionKey != nil)

    chunk, err := h.store.Get(ctx, key)
    if err != nil {
        return nil, err
    }

    chunkData := chunk.SData
    if toDecrypt {
        var err error
        chunkData, err = h.decryptChunkData(chunkData, encryptionKey)
        if err != nil {
            return nil, err
        }
    }
    return chunkData, nil
}

// Close indicates that no more chunks will be put with the hasherStore, so the Wait
// function can return when all the previously put chunks has been stored.
func (h *hasherStore) Close() {
    close(h.closed)
}

// Wait returns when
//    1) the Close() function has been called and
//    2) all the chunks which has been Put has been stored
func (h *hasherStore) Wait(ctx context.Context) error {
    <-h.closed
    h.wg.Wait()
    return nil
}

func (h *hasherStore) createHash(chunkData ChunkData) Address {
    hasher := h.hashFunc()
    hasher.ResetWithLength(chunkData[:8]) // 8 bytes of length
    hasher.Write(chunkData[8:])           // minus 8 []byte length
    return hasher.Sum(nil)
}

func (h *hasherStore) createChunk(chunkData ChunkData, chunkSize int64) *Chunk {
    hash := h.createHash(chunkData)
    chunk := NewChunk(hash, nil)
    chunk.SData = chunkData
    chunk.Size = chunkSize

    return chunk
}

func (h *hasherStore) encryptChunkData(chunkData ChunkData) (ChunkData, encryption.Key, error) {
    if len(chunkData) < 8 {
        return nil, nil, fmt.Errorf("Invalid ChunkData, min length 8 got %v", len(chunkData))
    }

    key, encryptedSpan, encryptedData, err := h.encrypt(chunkData)
    if err != nil {
        return nil, nil, err
    }
    c := make(ChunkData, len(encryptedSpan)+len(encryptedData))
    copy(c[:8], encryptedSpan)
    copy(c[8:], encryptedData)
    return c, key, nil
}

func (h *hasherStore) decryptChunkData(chunkData ChunkData, encryptionKey encryption.Key) (ChunkData, error) {
    if len(chunkData) < 8 {
        return nil, fmt.Errorf("Invalid ChunkData, min length 8 got %v", len(chunkData))
    }

    decryptedSpan, decryptedData, err := h.decrypt(chunkData, encryptionKey)
    if err != nil {
        return nil, err
    }

    // removing extra bytes which were just added for padding
    length := ChunkData(decryptedSpan).Size()
    for length > chunk.DefaultSize {
        length = length + (chunk.DefaultSize - 1)
        length = length / chunk.DefaultSize
        length *= h.refSize
    }

    c := make(ChunkData, length+8)
    copy(c[:8], decryptedSpan)
    copy(c[8:], decryptedData[:length])

    return c, nil
}

func (h *hasherStore) RefSize() int64 {
    return h.refSize
}

func (h *hasherStore) encrypt(chunkData ChunkData) (encryption.Key, []byte, []byte, error) {
    key := encryption.GenerateRandomKey(encryption.KeyLength)
    encryptedSpan, err := h.newSpanEncryption(key).Encrypt(chunkData[:8])
    if err != nil {
        return nil, nil, nil, err
    }
    encryptedData, err := h.newDataEncryption(key).Encrypt(chunkData[8:])
    if err != nil {
        return nil, nil, nil, err
    }
    return key, encryptedSpan, encryptedData, nil
}

func (h *hasherStore) decrypt(chunkData ChunkData, key encryption.Key) ([]byte, []byte, error) {
    encryptedSpan, err := h.newSpanEncryption(key).Encrypt(chunkData[:8])
    if err != nil {
        return nil, nil, err
    }
    encryptedData, err := h.newDataEncryption(key).Encrypt(chunkData[8:])
    if err != nil {
        return nil, nil, err
    }
    return encryptedSpan, encryptedData, nil
}

func (h *hasherStore) newSpanEncryption(key encryption.Key) encryption.Encryption {
    return encryption.New(key, 0, uint32(chunk.DefaultSize/h.refSize), sha3.NewKeccak256)
}

func (h *hasherStore) newDataEncryption(key encryption.Key) encryption.Encryption {
    return encryption.New(key, int(chunk.DefaultSize), 0, sha3.NewKeccak256)
}

func (h *hasherStore) storeChunk(ctx context.Context, chunk *Chunk) {
    h.wg.Add(1)
    go func() {
        <-chunk.dbStoredC
        h.wg.Done()
    }()
    h.store.Put(ctx, chunk)
}

func parseReference(ref Reference, hashSize int) (Address, encryption.Key, error) {
    encryptedKeyLength := hashSize + encryption.KeyLength
    switch len(ref) {
    case KeyLength:
        return Address(ref), nil, nil
    case encryptedKeyLength:
        encKeyIdx := len(ref) - encryption.KeyLength
        return Address(ref[:encKeyIdx]), encryption.Key(ref[encKeyIdx:]), nil
    default:
        return nil, nil, fmt.Errorf("Invalid reference length, expected %v or %v got %v", hashSize, encryptedKeyLength, len(ref))
    }

}