xref: /aosp_15_r20/external/tink/cc/subtle/nonce_based_streaming_aead.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2019 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 
17 #include "tink/subtle/nonce_based_streaming_aead.h"
18 
19 #include <memory>
20 #include <utility>
21 
22 #include "absl/strings/string_view.h"
23 #include "tink/input_stream.h"
24 #include "tink/output_stream.h"
25 #include "tink/random_access_stream.h"
26 #include "tink/streaming_aead.h"
27 #include "tink/subtle/decrypting_random_access_stream.h"
28 #include "tink/subtle/stream_segment_decrypter.h"
29 #include "tink/subtle/stream_segment_encrypter.h"
30 #include "tink/subtle/streaming_aead_decrypting_stream.h"
31 #include "tink/subtle/streaming_aead_encrypting_stream.h"
32 #include "tink/util/statusor.h"
33 
34 namespace crypto {
35 namespace tink {
36 namespace subtle {
37 
38 crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::OutputStream>>
NewEncryptingStream(std::unique_ptr<crypto::tink::OutputStream> ciphertext_destination,absl::string_view associated_data) const39     NonceBasedStreamingAead::NewEncryptingStream(
40         std::unique_ptr<crypto::tink::OutputStream> ciphertext_destination,
41         absl::string_view associated_data) const {
42   auto segment_encrypter_result = NewSegmentEncrypter(associated_data);
43   if (!segment_encrypter_result.ok()) return segment_encrypter_result.status();
44   return StreamingAeadEncryptingStream::New(
45       std::move(segment_encrypter_result.value()),
46       std::move(ciphertext_destination));
47 }
48 
49 crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::InputStream>>
NewDecryptingStream(std::unique_ptr<crypto::tink::InputStream> ciphertext_source,absl::string_view associated_data) const50     NonceBasedStreamingAead::NewDecryptingStream(
51         std::unique_ptr<crypto::tink::InputStream> ciphertext_source,
52         absl::string_view associated_data) const {
53   auto segment_decrypter_result = NewSegmentDecrypter(associated_data);
54   if (!segment_decrypter_result.ok()) return segment_decrypter_result.status();
55   return StreamingAeadDecryptingStream::New(
56       std::move(segment_decrypter_result.value()),
57       std::move(ciphertext_source));
58 }
59 
60 crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::RandomAccessStream>>
NewDecryptingRandomAccessStream(std::unique_ptr<crypto::tink::RandomAccessStream> ciphertext_source,absl::string_view associated_data) const61     NonceBasedStreamingAead::NewDecryptingRandomAccessStream(
62         std::unique_ptr<crypto::tink::RandomAccessStream> ciphertext_source,
63         absl::string_view associated_data) const {
64   auto segment_decrypter_result = NewSegmentDecrypter(associated_data);
65   if (!segment_decrypter_result.ok()) return segment_decrypter_result.status();
66   return DecryptingRandomAccessStream::New(
67       std::move(segment_decrypter_result.value()),
68       std::move(ciphertext_source));
69 }
70 
71 }  // namespace subtle
72 }  // namespace tink
73 }  // namespace crypto
74