Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
swarm/storage: Fix padding in hasherStore
Browse files Browse the repository at this point in the history
There was a bug that data was not lengthened to padding
  • Loading branch information
gbalint committed Mar 29, 2018
1 parent 582c6e5 commit 534217f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion swarm/storage/dpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func NewDPA(store ChunkStore, params *DPAParams) *DPA {
// Chunk retrieval blocks on netStore requests with a timeout so reader will
// report error if retrieval of chunks within requested range time out.
func (self *DPA) Retrieve(key Key) LazySectionReader {
getter := NewHasherStore(self.ChunkStore, self.hashFunc, true)
getter := NewHasherStore(self.ChunkStore, self.hashFunc, len(key) > self.hashFunc().Size())
return TreeJoin(key, getter, 0)
}

Expand Down
7 changes: 6 additions & 1 deletion swarm/storage/dpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func testDpaRandom(toEncrypt bool, t *testing.T) {
}

func TestDPA_capacity(t *testing.T) {
testDPA_capacity(false, t)
testDPA_capacity(true, t)
}

func testDPA_capacity(toEncrypt bool, t *testing.T) {
tdb, err := newTestDbStore(false)
if err != nil {
t.Fatalf("init dbStore failed: %v", err)
Expand All @@ -99,7 +104,7 @@ func TestDPA_capacity(t *testing.T) {
}
dpa := NewDPA(localStore, NewDPAParams())
reader, slice := generateRandomData(testDataSize)
key, wait, err := dpa.Store(reader, testDataSize, true)
key, wait, err := dpa.Store(reader, testDataSize, toEncrypt)
if err != nil {
t.Errorf("Store error: %v", err)
}
Expand Down
27 changes: 19 additions & 8 deletions swarm/storage/hasherstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type hasherStore struct {
func newChunkEncryption() *chunkEncryption {
return &chunkEncryption{
spanEncryption: encryption.New(0, math.MaxUint32, sha3.NewKeccak256),
dataEncryption: encryption.New(4096, 0, sha3.NewKeccak256),
dataEncryption: encryption.New(int(DefaultChunkSize), 0, sha3.NewKeccak256),
}
}

Expand Down Expand Up @@ -155,7 +155,6 @@ func (p *hasherStore) encryptChunkData(chunkData ChunkData) (ChunkData, encrypti
return nil, nil, err
}

c := make(ChunkData, len(chunkData))
encryptedSpan, err := p.chunkEncryption.spanEncryption.Encrypt(chunkData[:8], encryptionKey)
if err != nil {
return nil, nil, err
Expand All @@ -164,28 +163,40 @@ func (p *hasherStore) encryptChunkData(chunkData ChunkData) (ChunkData, encrypti
if err != nil {
return nil, nil, err
}
c := make(ChunkData, len(encryptedSpan)+len(encryptedData))
copy(c[:8], encryptedSpan)
copy(c[8:], encryptedData)
return c, encryptionKey, nil
}

func (p *hasherStore) decryptChunkData(chunkData ChunkData, encryptionKey encryption.Key) (ChunkData, error) {
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))
}

c := make(ChunkData, len(chunkData))
decryptedSpan, err := p.chunkEncryption.spanEncryption.Decrypt(chunkData[:8], encryptionKey)
decryptedSpan, err := h.chunkEncryption.spanEncryption.Decrypt(chunkData[:8], encryptionKey)
if err != nil {
return nil, err
}
decryptedData, err := p.chunkEncryption.dataEncryption.Decrypt(chunkData[8:], encryptionKey)

decryptedData, err := h.chunkEncryption.dataEncryption.Decrypt(chunkData[8:], encryptionKey)
if err != nil {
return nil, err
}

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

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

return c[:length+8], nil
}

func (h *hasherStore) RefSize() int64 {
Expand Down

0 comments on commit 534217f

Please sign in to comment.