hasherstore.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package storage
  17. import (
  18. "context"
  19. "fmt"
  20. "sync/atomic"
  21. "github.com/ethereum/go-ethereum/swarm/chunk"
  22. "github.com/ethereum/go-ethereum/swarm/storage/encryption"
  23. "golang.org/x/crypto/sha3"
  24. )
  25. type hasherStore struct {
  26. store ChunkStore
  27. tag *chunk.Tag
  28. toEncrypt bool
  29. hashFunc SwarmHasher
  30. hashSize int // content hash size
  31. refSize int64 // reference size (content hash + possibly encryption key)
  32. errC chan error // global error channel
  33. doneC chan struct{} // closed by Close() call to indicate that count is the final number of chunks
  34. quitC chan struct{} // closed to quit unterminated routines
  35. // nrChunks is used with atomic functions
  36. // it is required to be at the end of the struct to ensure 64bit alignment for arm architecture
  37. // see: https://golang.org/pkg/sync/atomic/#pkg-note-BUG
  38. nrChunks uint64 // number of chunks to store
  39. }
  40. // NewHasherStore creates a hasherStore object, which implements Putter and Getter interfaces.
  41. // With the HasherStore you can put and get chunk data (which is just []byte) into a ChunkStore
  42. // and the hasherStore will take core of encryption/decryption of data if necessary
  43. func NewHasherStore(store ChunkStore, hashFunc SwarmHasher, toEncrypt bool, tag *chunk.Tag) *hasherStore {
  44. hashSize := hashFunc().Size()
  45. refSize := int64(hashSize)
  46. if toEncrypt {
  47. refSize += encryption.KeyLength
  48. }
  49. h := &hasherStore{
  50. store: store,
  51. tag: tag,
  52. toEncrypt: toEncrypt,
  53. hashFunc: hashFunc,
  54. hashSize: hashSize,
  55. refSize: refSize,
  56. errC: make(chan error),
  57. doneC: make(chan struct{}),
  58. quitC: make(chan struct{}),
  59. }
  60. return h
  61. }
  62. // Put stores the chunkData into the ChunkStore of the hasherStore and returns the reference.
  63. // If hasherStore has a chunkEncryption object, the data will be encrypted.
  64. // Asynchronous function, the data will not necessarily be stored when it returns.
  65. func (h *hasherStore) Put(ctx context.Context, chunkData ChunkData) (Reference, error) {
  66. c := chunkData
  67. var encryptionKey encryption.Key
  68. if h.toEncrypt {
  69. var err error
  70. c, encryptionKey, err = h.encryptChunkData(chunkData)
  71. if err != nil {
  72. return nil, err
  73. }
  74. }
  75. chunk := h.createChunk(c)
  76. h.storeChunk(ctx, chunk)
  77. return Reference(append(chunk.Address(), encryptionKey...)), nil
  78. }
  79. // Get returns data of the chunk with the given reference (retrieved from the ChunkStore of hasherStore).
  80. // If the data is encrypted and the reference contains an encryption key, it will be decrypted before
  81. // return.
  82. func (h *hasherStore) Get(ctx context.Context, ref Reference) (ChunkData, error) {
  83. addr, encryptionKey, err := parseReference(ref, h.hashSize)
  84. if err != nil {
  85. return nil, err
  86. }
  87. chunk, err := h.store.Get(ctx, chunk.ModeGetRequest, addr)
  88. if err != nil {
  89. return nil, err
  90. }
  91. chunkData := ChunkData(chunk.Data())
  92. toDecrypt := (encryptionKey != nil)
  93. if toDecrypt {
  94. var err error
  95. chunkData, err = h.decryptChunkData(chunkData, encryptionKey)
  96. if err != nil {
  97. return nil, err
  98. }
  99. }
  100. return chunkData, nil
  101. }
  102. // Close indicates that no more chunks will be put with the hasherStore, so the Wait
  103. // function can return when all the previously put chunks has been stored.
  104. func (h *hasherStore) Close() {
  105. close(h.doneC)
  106. }
  107. // Wait returns when
  108. // 1) the Close() function has been called and
  109. // 2) all the chunks which has been Put has been stored
  110. func (h *hasherStore) Wait(ctx context.Context) error {
  111. defer close(h.quitC)
  112. var nrStoredChunks uint64 // number of stored chunks
  113. var done bool
  114. doneC := h.doneC
  115. for {
  116. select {
  117. // if context is done earlier, just return with the error
  118. case <-ctx.Done():
  119. return ctx.Err()
  120. // doneC is closed if all chunks have been submitted, from then we just wait until all of them are also stored
  121. case <-doneC:
  122. done = true
  123. doneC = nil
  124. // a chunk has been stored, if err is nil, then successfully, so increase the stored chunk counter
  125. case err := <-h.errC:
  126. if err != nil {
  127. return err
  128. }
  129. nrStoredChunks++
  130. }
  131. // if all the chunks have been submitted and all of them are stored, then we can return
  132. if done {
  133. if nrStoredChunks >= atomic.LoadUint64(&h.nrChunks) {
  134. return nil
  135. }
  136. }
  137. }
  138. }
  139. func (h *hasherStore) createHash(chunkData ChunkData) Address {
  140. hasher := h.hashFunc()
  141. hasher.ResetWithLength(chunkData[:8]) // 8 bytes of length
  142. hasher.Write(chunkData[8:]) // minus 8 []byte length
  143. return hasher.Sum(nil)
  144. }
  145. func (h *hasherStore) createChunk(chunkData ChunkData) Chunk {
  146. hash := h.createHash(chunkData)
  147. chunk := NewChunk(hash, chunkData)
  148. return chunk
  149. }
  150. func (h *hasherStore) encryptChunkData(chunkData ChunkData) (ChunkData, encryption.Key, error) {
  151. if len(chunkData) < 8 {
  152. return nil, nil, fmt.Errorf("Invalid ChunkData, min length 8 got %v", len(chunkData))
  153. }
  154. key, encryptedSpan, encryptedData, err := h.encrypt(chunkData)
  155. if err != nil {
  156. return nil, nil, err
  157. }
  158. c := make(ChunkData, len(encryptedSpan)+len(encryptedData))
  159. copy(c[:8], encryptedSpan)
  160. copy(c[8:], encryptedData)
  161. return c, key, nil
  162. }
  163. func (h *hasherStore) decryptChunkData(chunkData ChunkData, encryptionKey encryption.Key) (ChunkData, error) {
  164. if len(chunkData) < 8 {
  165. return nil, fmt.Errorf("Invalid ChunkData, min length 8 got %v", len(chunkData))
  166. }
  167. decryptedSpan, decryptedData, err := h.decrypt(chunkData, encryptionKey)
  168. if err != nil {
  169. return nil, err
  170. }
  171. // removing extra bytes which were just added for padding
  172. length := ChunkData(decryptedSpan).Size()
  173. for length > chunk.DefaultSize {
  174. length = length + (chunk.DefaultSize - 1)
  175. length = length / chunk.DefaultSize
  176. length *= uint64(h.refSize)
  177. }
  178. c := make(ChunkData, length+8)
  179. copy(c[:8], decryptedSpan)
  180. copy(c[8:], decryptedData[:length])
  181. return c, nil
  182. }
  183. func (h *hasherStore) RefSize() int64 {
  184. return h.refSize
  185. }
  186. func (h *hasherStore) encrypt(chunkData ChunkData) (encryption.Key, []byte, []byte, error) {
  187. key := encryption.GenerateRandomKey(encryption.KeyLength)
  188. encryptedSpan, err := h.newSpanEncryption(key).Encrypt(chunkData[:8])
  189. if err != nil {
  190. return nil, nil, nil, err
  191. }
  192. encryptedData, err := h.newDataEncryption(key).Encrypt(chunkData[8:])
  193. if err != nil {
  194. return nil, nil, nil, err
  195. }
  196. return key, encryptedSpan, encryptedData, nil
  197. }
  198. func (h *hasherStore) decrypt(chunkData ChunkData, key encryption.Key) ([]byte, []byte, error) {
  199. encryptedSpan, err := h.newSpanEncryption(key).Encrypt(chunkData[:8])
  200. if err != nil {
  201. return nil, nil, err
  202. }
  203. encryptedData, err := h.newDataEncryption(key).Encrypt(chunkData[8:])
  204. if err != nil {
  205. return nil, nil, err
  206. }
  207. return encryptedSpan, encryptedData, nil
  208. }
  209. func (h *hasherStore) newSpanEncryption(key encryption.Key) encryption.Encryption {
  210. return encryption.New(key, 0, uint32(chunk.DefaultSize/h.refSize), sha3.NewLegacyKeccak256)
  211. }
  212. func (h *hasherStore) newDataEncryption(key encryption.Key) encryption.Encryption {
  213. return encryption.New(key, int(chunk.DefaultSize), 0, sha3.NewLegacyKeccak256)
  214. }
  215. func (h *hasherStore) storeChunk(ctx context.Context, ch Chunk) {
  216. atomic.AddUint64(&h.nrChunks, 1)
  217. go func() {
  218. seen, err := h.store.Put(ctx, chunk.ModePutUpload, ch)
  219. h.tag.Inc(chunk.StateStored)
  220. if seen {
  221. h.tag.Inc(chunk.StateSeen)
  222. }
  223. select {
  224. case h.errC <- err:
  225. case <-h.quitC:
  226. }
  227. }()
  228. }
  229. func parseReference(ref Reference, hashSize int) (Address, encryption.Key, error) {
  230. encryptedRefLength := hashSize + encryption.KeyLength
  231. switch len(ref) {
  232. case AddressLength:
  233. return Address(ref), nil, nil
  234. case encryptedRefLength:
  235. encKeyIdx := len(ref) - encryption.KeyLength
  236. return Address(ref[:encKeyIdx]), encryption.Key(ref[encKeyIdx:]), nil
  237. default:
  238. return nil, nil, fmt.Errorf("Invalid reference length, expected %v or %v got %v", hashSize, encryptedRefLength, len(ref))
  239. }
  240. }