aboutsummaryrefslogtreecommitdiff
path: root/internal/contentenc
diff options
context:
space:
mode:
authorJakob Unterwurzacher2017-06-01 21:39:47 +0200
committerJakob Unterwurzacher2017-06-01 22:19:27 +0200
commita24faa3ba52c66dfc1707da5c8d001f2adff9ccc (patch)
treea07a03444a1689a3c1c0fb97a18f245cacf289d7 /internal/contentenc
parent53b7c17261dbf0aeb46ebb448f7c97d5c9fad986 (diff)
fusefrontend: write: consolidate and move encryption to contentenc
Collect all the plaintext and pass everything to contentenc in one call. This will allow easier parallization of the encryption. https://github.com/rfjakob/gocryptfs/issues/116
Diffstat (limited to 'internal/contentenc')
-rw-r--r--internal/contentenc/content.go18
1 files changed, 18 insertions, 0 deletions
diff --git a/internal/contentenc/content.go b/internal/contentenc/content.go
index 524296f..80bcf54 100644
--- a/internal/contentenc/content.go
+++ b/internal/contentenc/content.go
@@ -149,6 +149,24 @@ func (be *ContentEnc) DecryptBlock(ciphertext []byte, blockNo uint64, fileID []b
return plaintext, nil
}
+// EncryptBlocks is like EncryptBlock but takes multiple plaintext blocks.
+func (be *ContentEnc) EncryptBlocks(plaintextBlocks [][]byte, firstBlockNo uint64, fileID []byte) []byte {
+ // Encrypt piecewise.
+ ciphertextBlocks := make([][]byte, len(plaintextBlocks))
+ for i, v := range plaintextBlocks {
+ ciphertextBlocks[i] = be.EncryptBlock(v, firstBlockNo+uint64(i), fileID)
+ }
+ // Concatenate ciphertext into a single byte array.
+ // Size the output buffer for the maximum possible size (all blocks complete)
+ // to allocations in out.Write()
+ tmp := make([]byte, len(plaintextBlocks)*int(be.CipherBS()))
+ out := bytes.NewBuffer(tmp[:0])
+ for _, v := range ciphertextBlocks {
+ out.Write(v)
+ }
+ return out.Bytes()
+}
+
// EncryptBlock - Encrypt plaintext using a random nonce.
// blockNo and fileID are used as associated data.
// The output is nonce + ciphertext + tag.