1 // Copyright 2020 Google LLC
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/cc/cc_streaming_aead_wrappers.h"
18
19 #include <utility>
20
21 #include "tink/input_stream.h"
22 #include "tink/output_stream.h"
23
24 namespace crypto {
25 namespace tink {
26
NewCcEncryptingStream(StreamingAead * streaming_aead,absl::string_view aad,std::shared_ptr<PythonFileObjectAdapter> ciphertext_destination)27 util::StatusOr<std::unique_ptr<OutputStreamAdapter>> NewCcEncryptingStream(
28 StreamingAead* streaming_aead, absl::string_view aad,
29 std::shared_ptr<PythonFileObjectAdapter> ciphertext_destination) {
30 // Get a destination OutputStream from the destination
31 // PythonFileObjectAdapter.
32 std::unique_ptr<OutputStream> destination_os =
33 absl::make_unique<PythonOutputStream>(ciphertext_destination);
34
35 // Get an EncryptingStream from the destination OutputStream.
36 auto result =
37 streaming_aead->NewEncryptingStream(std::move(destination_os), aad);
38 if (!result.ok()) {
39 return result.status();
40 }
41
42 // Get an OutputStreamAdapter from the EncryptingStream
43 return absl::make_unique<OutputStreamAdapter>(std::move(result.value()));
44 }
45
NewCcDecryptingStream(StreamingAead * streaming_aead,absl::string_view aad,std::shared_ptr<PythonFileObjectAdapter> ciphertext_source)46 util::StatusOr<std::unique_ptr<InputStreamAdapter>> NewCcDecryptingStream(
47 StreamingAead* streaming_aead, absl::string_view aad,
48 std::shared_ptr<PythonFileObjectAdapter> ciphertext_source) {
49 // Get a source InputStream from the source PythonFileObjectAdapter.
50 std::unique_ptr<InputStream> source_os =
51 absl::make_unique<PythonInputStream>(ciphertext_source);
52
53 // Get a DecryptingStream from the source InputStream.
54 auto result = streaming_aead->NewDecryptingStream(std::move(source_os), aad);
55 if (!result.ok()) {
56 return result.status();
57 }
58
59 // Get an InputStreamAdapter from the DecryptingStream
60 return absl::make_unique<InputStreamAdapter>(std::move(result.value()));
61 }
62
63 } // namespace tink
64 } // namespace crypto
65