From a24faa3ba52c66dfc1707da5c8d001f2adff9ccc Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Thu, 1 Jun 2017 21:39:47 +0200 Subject: 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 --- internal/contentenc/content.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'internal/contentenc/content.go') 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. -- cgit v1.2.3