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_SUBTLE_STREAMING_AEAD_DECRYPTING_STREAM_H_ 18 #define TINK_SUBTLE_STREAMING_AEAD_DECRYPTING_STREAM_H_ 19 20 #include <memory> 21 #include <vector> 22 23 #include "tink/input_stream.h" 24 #include "tink/subtle/stream_segment_decrypter.h" 25 #include "tink/util/statusor.h" 26 27 namespace crypto { 28 namespace tink { 29 namespace subtle { 30 31 class StreamingAeadDecryptingStream : public InputStream { 32 public: 33 // A factory that produces decrypting streams. 34 // The returned stream is a wrapper around 'ciphertext_source', 35 // such that reading via the wrapper leads to AEAD-decryption of the 36 // underlying ciphertext by 'segment_decrypter', using 'associated_data' as 37 // associated authenticated data, and the read bytes are bytes of the 38 // resulting plaintext. 39 static 40 crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::InputStream>> 41 New(std::unique_ptr<StreamSegmentDecrypter> segment_decrypter, 42 std::unique_ptr<crypto::tink::InputStream> ciphertext_source); 43 44 // ----------------------- 45 // Methods of InputStream-interface implemented by this class. 46 crypto::tink::util::StatusOr<int> Next(const void** data) override; 47 void BackUp(int count) override; 48 int64_t Position() const override; 49 50 private: StreamingAeadDecryptingStream()51 StreamingAeadDecryptingStream() {} 52 std::unique_ptr<StreamSegmentDecrypter> segment_decrypter_; 53 std::unique_ptr<crypto::tink::InputStream> ct_source_; 54 std::vector<uint8_t> ct_buffer_; // ciphertext buffer 55 std::vector<uint8_t> pt_buffer_; // plaintext buffer 56 int64_t position_; // number of plaintext bytes read from this stream 57 int64_t segment_number_; // current segment number 58 crypto::tink::util::Status status_; // status of the stream 59 60 // Counters that describe the state of the data in pt_buffer_. 61 int count_backedup_; // # bytes in pt_buffer_ that were backed up 62 int pt_buffer_offset_; // offset at which *data starts in pt_buffer_ 63 64 // Flag that indicates whether the decrypting stream has been initialized. 65 // If true, the header of the ciphertext stream has been already read 66 // and processed. 67 bool is_initialized_; 68 bool read_last_segment_; 69 }; 70 71 } // namespace subtle 72 } // namespace tink 73 } // namespace crypto 74 75 #endif // TINK_SUBTLE_STREAMING_AEAD_DECRYPTING_STREAM_H_ 76