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/streamingaead/streaming_aead_wrapper.h"
18
19 #include <memory>
20 #include <utility>
21 #include <vector>
22
23 #include "absl/status/status.h"
24 #include "tink/crypto_format.h"
25 #include "tink/input_stream.h"
26 #include "tink/output_stream.h"
27 #include "tink/primitive_set.h"
28 #include "tink/random_access_stream.h"
29 #include "tink/streaming_aead.h"
30 #include "tink/streamingaead/decrypting_input_stream.h"
31 #include "tink/streamingaead/decrypting_random_access_stream.h"
32 #include "tink/util/status.h"
33 #include "tink/util/statusor.h"
34
35 namespace crypto {
36 namespace tink {
37
38 using ::crypto::tink::util::Status;
39 using ::crypto::tink::util::StatusOr;
40
41 namespace {
42
Validate(PrimitiveSet<StreamingAead> * primitives)43 Status Validate(PrimitiveSet<StreamingAead>* primitives) {
44 if (primitives == nullptr) {
45 return Status(absl::StatusCode::kInternal,
46 "primitive set must be non-NULL");
47 }
48 if (primitives->get_primary() == nullptr) {
49 return Status(absl::StatusCode::kInvalidArgument,
50 "primitive set has no primary");
51 }
52 return util::OkStatus();
53 }
54
55 class StreamingAeadSetWrapper: public StreamingAead {
56 public:
StreamingAeadSetWrapper(std::unique_ptr<PrimitiveSet<StreamingAead>> primitives)57 explicit StreamingAeadSetWrapper(
58 std::unique_ptr<PrimitiveSet<StreamingAead>> primitives)
59 : primitives_(std::move(primitives)) {}
60
61 crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::OutputStream>>
62 NewEncryptingStream(
63 std::unique_ptr<crypto::tink::OutputStream> ciphertext_destination,
64 absl::string_view associated_data) const override;
65
66 crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::InputStream>>
67 NewDecryptingStream(
68 std::unique_ptr<crypto::tink::InputStream> ciphertext_source,
69 absl::string_view associated_data) const override;
70
71 crypto::tink::util::StatusOr<
72 std::unique_ptr<crypto::tink::RandomAccessStream>>
73 NewDecryptingRandomAccessStream(
74 std::unique_ptr<crypto::tink::RandomAccessStream> ciphertext_source,
75 absl::string_view associated_data) const override;
76
77 ~StreamingAeadSetWrapper() override = default;
78
79 private:
80 // We use a shared_ptr here to ensure that primitives_ stays alive
81 // as long as it might be needed by some decrypting stream returned
82 // by NewDecryptingStream. This can happen after this wrapper
83 // is destroyed, as we refer to primitives_ only when the user attempts
84 // to read some data from the decrypting stream.
85 std::shared_ptr<PrimitiveSet<StreamingAead>> primitives_;
86 }; // class StreamingAeadSetWrapper
87
88 StatusOr<std::unique_ptr<OutputStream>>
NewEncryptingStream(std::unique_ptr<OutputStream> ciphertext_destination,absl::string_view associated_data) const89 StreamingAeadSetWrapper::NewEncryptingStream(
90 std::unique_ptr<OutputStream> ciphertext_destination,
91 absl::string_view associated_data) const {
92 return primitives_->get_primary()->get_primitive().NewEncryptingStream(
93 std::move(ciphertext_destination), associated_data);
94 }
95
96 StatusOr<std::unique_ptr<InputStream>>
NewDecryptingStream(std::unique_ptr<InputStream> ciphertext_source,absl::string_view associated_data) const97 StreamingAeadSetWrapper::NewDecryptingStream(
98 std::unique_ptr<InputStream> ciphertext_source,
99 absl::string_view associated_data) const {
100 return {streamingaead::DecryptingInputStream::New(
101 primitives_, std::move(ciphertext_source), associated_data)};
102 }
103
104 StatusOr<std::unique_ptr<RandomAccessStream>>
NewDecryptingRandomAccessStream(std::unique_ptr<RandomAccessStream> ciphertext_source,absl::string_view associated_data) const105 StreamingAeadSetWrapper::NewDecryptingRandomAccessStream(
106 std::unique_ptr<RandomAccessStream> ciphertext_source,
107 absl::string_view associated_data) const {
108 return {streamingaead::DecryptingRandomAccessStream::New(
109 primitives_, std::move(ciphertext_source), associated_data)};
110 }
111
112 } // anonymous namespace
113
Wrap(std::unique_ptr<PrimitiveSet<StreamingAead>> streaming_aead_set) const114 StatusOr<std::unique_ptr<StreamingAead>> StreamingAeadWrapper::Wrap(
115 std::unique_ptr<PrimitiveSet<StreamingAead>> streaming_aead_set) const {
116 auto status = Validate(streaming_aead_set.get());
117 if (!status.ok()) return status;
118 std::unique_ptr<StreamingAead> streaming_aead =
119 absl::make_unique<StreamingAeadSetWrapper>(
120 std::move(streaming_aead_set));
121 return std::move(streaming_aead);
122 }
123
124 } // namespace tink
125 } // namespace crypto
126