Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / quota / EncryptingOutputStream.h
blob9a23037731f55e94d2c2d5847882a5bd99406178
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_quota_EncryptingOutputStream_h
8 #define mozilla_dom_quota_EncryptingOutputStream_h
10 // Local includes
11 #include "EncryptedBlock.h" // for EncryptedBlock
13 // Global includes
14 #include <cstddef>
15 #include <cstdint>
16 #include "ErrorList.h"
17 #include "mozilla/InitializedOnce.h"
18 #include "mozilla/Maybe.h"
19 #include "mozilla/NotNull.h"
20 #include "nsCOMPtr.h"
21 #include "nsIOutputStream.h"
22 #include "nsISupports.h"
23 #include "nsTArray.h"
24 #include "nscore.h"
26 class nsIInputStream;
27 class nsIRandomGenerator;
29 namespace mozilla::dom::quota {
30 class EncryptingOutputStreamBase : public nsIOutputStream {
31 public:
32 NS_DECL_THREADSAFE_ISUPPORTS
34 NS_IMETHOD Write(const char* aBuf, uint32_t aCount, uint32_t* _retval) final;
35 NS_IMETHOD WriteFrom(nsIInputStream* aFromStream, uint32_t aCount,
36 uint32_t* _retval) final;
37 NS_IMETHOD IsNonBlocking(bool* _retval) final;
39 protected:
40 EncryptingOutputStreamBase(nsCOMPtr<nsIOutputStream> aBaseStream,
41 size_t aBlockSize);
43 virtual ~EncryptingOutputStreamBase() = default;
45 nsresult WriteAll(const char* aBuf, uint32_t aCount,
46 uint32_t* aBytesWrittenOut);
48 InitializedOnce<const NotNull<nsCOMPtr<nsIOutputStream>>> mBaseStream;
49 const size_t mBlockSize;
52 // Wraps another nsIOutputStream using the CipherStrategy to encrypt it a
53 // page-based manner. Essentially, the CipherStrategy is not actually
54 // necessarily doing encryption, but any transformation to a page requiring some
55 // fixed-size reserved size per page.
57 // Paired with DecryptingInputStream which can be used to read the data written
58 // to the underlying stream, using the same (or more generally, a compatible)
59 // CipherStrategy, when created with the same key (assuming a symmetric cipher
60 // is being used; in principle, an asymmetric cipher would probably also work).
61 template <typename CipherStrategy>
62 class EncryptingOutputStream final : public EncryptingOutputStreamBase {
63 public:
64 // Construct a new blocking output stream to encrypt data to
65 // the given base stream. The base stream must also be blocking.
66 // The encryption block size may optionally be set to a value
67 // up to kMaxBlockSize.
68 explicit EncryptingOutputStream(nsCOMPtr<nsIOutputStream> aBaseStream,
69 size_t aBlockSize,
70 typename CipherStrategy::KeyType aKey);
72 private:
73 ~EncryptingOutputStream();
75 nsresult FlushToBaseStream();
77 bool EnsureBuffers();
79 CipherStrategy mCipherStrategy;
81 // Buffer holding copied plain data. This must be copied here
82 // so that the encryption can be performed on a single flat buffer.
83 // XXX This is only necessary if the data written doesn't contain a portion of
84 // effective block size at a block boundary.
85 nsTArray<uint8_t> mBuffer;
87 nsCOMPtr<nsIRandomGenerator> mRandomGenerator;
89 // The next byte in the plain data to copy incoming data to.
90 size_t mNextByte = 0;
92 // Buffer holding the resulting encrypted data.
93 using EncryptedBlockType = EncryptedBlock<CipherStrategy::BlockPrefixLength,
94 CipherStrategy::BasicBlockSize>;
95 Maybe<EncryptedBlockType> mEncryptedBlock;
97 public:
98 NS_IMETHOD Close() override;
99 NS_IMETHOD Flush() override;
100 NS_IMETHOD StreamStatus() override;
101 NS_IMETHOD WriteSegments(nsReadSegmentFun aReader, void* aClosure,
102 uint32_t aCount, uint32_t* _retval) override;
105 } // namespace mozilla::dom::quota
107 #endif