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 #ifndef TINK_STREAMINGAEAD_DECRYPTING_INPUT_STREAM_H_ 18 #define TINK_STREAMINGAEAD_DECRYPTING_INPUT_STREAM_H_ 19 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "tink/input_stream.h" 25 #include "tink/primitive_set.h" 26 #include "tink/streaming_aead.h" 27 #include "tink/streamingaead/buffered_input_stream.h" 28 #include "tink/util/statusor.h" 29 30 namespace crypto { 31 namespace tink { 32 namespace streamingaead { 33 34 // A wrapper around an InputStream that holds a reference to a 35 // set of StreamingAead-primitives and upon first Next()-call probes the 36 // initial portion of the wrapped InputStream, to find a matching 37 // primitive, i.e. the primitive that is able to decrypt the stream. 38 // Once a match is found, all subsequent calls are forwarded to it. 39 class DecryptingInputStream : public crypto::tink::InputStream { 40 public: 41 // Constructs an InputStream that wraps 'input_stream', and will use 42 // (one of) provided 'primitives' to decrypt the contents of 'input_stream', 43 // using 'associated_data' as authenticated associated data 44 // of the decryption process. 45 static util::StatusOr<std::unique_ptr<InputStream>> New( 46 std::shared_ptr< 47 crypto::tink::PrimitiveSet<crypto::tink::StreamingAead>> primitives, 48 std::unique_ptr<crypto::tink::InputStream> ciphertext_source, 49 absl::string_view associated_data); 50 51 ~DecryptingInputStream() override = default; 52 util::StatusOr<int> Next(const void** data) override; 53 void BackUp(int count) override; 54 int64_t Position() const override; 55 56 private: DecryptingInputStream()57 DecryptingInputStream() {} 58 std::shared_ptr< 59 crypto::tink::PrimitiveSet<crypto::tink::StreamingAead>> primitives_; 60 std::shared_ptr<BufferedInputStream> buffered_ct_source_; 61 std::string associated_data_; 62 std::unique_ptr<crypto::tink::InputStream> matching_stream_; 63 bool attempted_matching_; 64 }; 65 66 } // namespace streamingaead 67 } // namespace tink 68 } // namespace crypto 69 70 #endif // TINK_STREAMINGAEAD_DECRYPTING_INPUT_STREAM_H_ 71