xref: /aosp_15_r20/external/tink/cc/streaming_aead.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1*e7b1675dSTing-Kang Chang // Copyright 2018 Google Inc.
2*e7b1675dSTing-Kang Chang //
3*e7b1675dSTing-Kang Chang // Licensed under the Apache License, Version 2.0 (the "License");
4*e7b1675dSTing-Kang Chang // you may not use this file except in compliance with the License.
5*e7b1675dSTing-Kang Chang // You may obtain a copy of the License at
6*e7b1675dSTing-Kang Chang //
7*e7b1675dSTing-Kang Chang //     http://www.apache.org/licenses/LICENSE-2.0
8*e7b1675dSTing-Kang Chang //
9*e7b1675dSTing-Kang Chang // Unless required by applicable law or agreed to in writing, software
10*e7b1675dSTing-Kang Chang // distributed under the License is distributed on an "AS IS" BASIS,
11*e7b1675dSTing-Kang Chang // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*e7b1675dSTing-Kang Chang // See the License for the specific language governing permissions and
13*e7b1675dSTing-Kang Chang // limitations under the License.
14*e7b1675dSTing-Kang Chang //
15*e7b1675dSTing-Kang Chang ///////////////////////////////////////////////////////////////////////////////
16*e7b1675dSTing-Kang Chang 
17*e7b1675dSTing-Kang Chang #ifndef TINK_STREAMING_AEAD_H_
18*e7b1675dSTing-Kang Chang #define TINK_STREAMING_AEAD_H_
19*e7b1675dSTing-Kang Chang 
20*e7b1675dSTing-Kang Chang #include <memory>
21*e7b1675dSTing-Kang Chang 
22*e7b1675dSTing-Kang Chang #include "absl/strings/string_view.h"
23*e7b1675dSTing-Kang Chang #include "tink/input_stream.h"
24*e7b1675dSTing-Kang Chang #include "tink/output_stream.h"
25*e7b1675dSTing-Kang Chang #include "tink/random_access_stream.h"
26*e7b1675dSTing-Kang Chang #include "tink/util/statusor.h"
27*e7b1675dSTing-Kang Chang 
28*e7b1675dSTing-Kang Chang namespace crypto {
29*e7b1675dSTing-Kang Chang namespace tink {
30*e7b1675dSTing-Kang Chang 
31*e7b1675dSTing-Kang Chang 
32*e7b1675dSTing-Kang Chang // An interface for streaming authenticated encryption with associated data.
33*e7b1675dSTing-Kang Chang // Streaming encryption is typically used for encrypting large plaintexts such
34*e7b1675dSTing-Kang Chang // as large files.  Tink may eventually contain multiple interfaces for
35*e7b1675dSTing-Kang Chang // streaming encryption depending on the supported properties. This interface
36*e7b1675dSTing-Kang Chang // supports a streaming interface for symmetric encryption with
37*e7b1675dSTing-Kang Chang // authentication. The underlying encryption modes are selected so that partial
38*e7b1675dSTing-Kang Chang // plaintext can be obtained fast by decrypting and authenticating just a part
39*e7b1675dSTing-Kang Chang // of the ciphertext.
40*e7b1675dSTing-Kang Chang class StreamingAead {
41*e7b1675dSTing-Kang Chang  public:
42*e7b1675dSTing-Kang Chang   // Returns a wrapper around 'ciphertext_destination', such that any bytes
43*e7b1675dSTing-Kang Chang   // written via the wrapper are AEAD-encrypted using 'associated_data' as
44*e7b1675dSTing-Kang Chang   // associated authenticated data. The associated data is not included in the
45*e7b1675dSTing-Kang Chang   // ciphertext and has to be passed in as parameter for decryption.
46*e7b1675dSTing-Kang Chang   // ByteCount() of the wrapper returns the number of written plaintext bytes.
47*e7b1675dSTing-Kang Chang   // Closing the wrapper results in closing of the wrapped stream.
48*e7b1675dSTing-Kang Chang   virtual crypto::tink::util::StatusOr<
49*e7b1675dSTing-Kang Chang       std::unique_ptr<crypto::tink::OutputStream>>
50*e7b1675dSTing-Kang Chang   NewEncryptingStream(
51*e7b1675dSTing-Kang Chang       std::unique_ptr<crypto::tink::OutputStream> ciphertext_destination,
52*e7b1675dSTing-Kang Chang       absl::string_view associated_data) const = 0;
53*e7b1675dSTing-Kang Chang 
54*e7b1675dSTing-Kang Chang   // Returns a wrapper around 'ciphertext_source', such that reading
55*e7b1675dSTing-Kang Chang   // via the wrapper leads to AEAD-decryption of the underlying ciphertext,
56*e7b1675dSTing-Kang Chang   // using 'associated_data' as associated authenticated data, and the
57*e7b1675dSTing-Kang Chang   // read bytes are bytes of the resulting plaintext.
58*e7b1675dSTing-Kang Chang   // ByteCount() of the wrapper returns the number of read plaintext bytes.
59*e7b1675dSTing-Kang Chang   virtual crypto::tink::util::StatusOr<
60*e7b1675dSTing-Kang Chang       std::unique_ptr<crypto::tink::InputStream>>
61*e7b1675dSTing-Kang Chang   NewDecryptingStream(
62*e7b1675dSTing-Kang Chang       std::unique_ptr<crypto::tink::InputStream> ciphertext_source,
63*e7b1675dSTing-Kang Chang       absl::string_view associated_data) const = 0;
64*e7b1675dSTing-Kang Chang 
65*e7b1675dSTing-Kang Chang   // Returns a wrapper around 'ciphertext_source', such that reading
66*e7b1675dSTing-Kang Chang   // via the wrapper leads to AEAD-decryption of the underlying ciphertext,
67*e7b1675dSTing-Kang Chang   // using 'associated_data' as associated authenticated data, and the
68*e7b1675dSTing-Kang Chang   // read bytes are bytes of the resulting plaintext.
69*e7b1675dSTing-Kang Chang   // Note that the returned wrapper's size()-method reports size that is
70*e7b1675dSTing-Kang Chang   // not checked for integrity.  For example, if the ciphertext file has been
71*e7b1675dSTing-Kang Chang   // truncated then size() will return a wrong result.  Reading the last block
72*e7b1675dSTing-Kang Chang   // of the plaintext will verify whether size() is correct.
73*e7b1675dSTing-Kang Chang   // Reading through the wrapper is thread safe.
74*e7b1675dSTing-Kang Chang   virtual crypto::tink::util::StatusOr<
75*e7b1675dSTing-Kang Chang       std::unique_ptr<crypto::tink::RandomAccessStream>>
76*e7b1675dSTing-Kang Chang   NewDecryptingRandomAccessStream(
77*e7b1675dSTing-Kang Chang       std::unique_ptr<crypto::tink::RandomAccessStream> ciphertext_source,
78*e7b1675dSTing-Kang Chang       absl::string_view associated_data) const = 0;
79*e7b1675dSTing-Kang Chang 
80*e7b1675dSTing-Kang Chang   virtual ~StreamingAead() = default;
81*e7b1675dSTing-Kang Chang };
82*e7b1675dSTing-Kang Chang 
83*e7b1675dSTing-Kang Chang }  // namespace tink
84*e7b1675dSTing-Kang Chang }  // namespace crypto
85*e7b1675dSTing-Kang Chang 
86*e7b1675dSTing-Kang Chang #endif  // TINK_STREAMING_AEAD_H_
87