1 // Copyright 2019 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 #ifndef TINK_SUBTLE_DECRYPTING_RANDOM_ACCESS_STREAM_H_ 18 #define TINK_SUBTLE_DECRYPTING_RANDOM_ACCESS_STREAM_H_ 19 20 #include <memory> 21 #include <vector> 22 23 #include "absl/synchronization/mutex.h" 24 #include "tink/random_access_stream.h" 25 #include "tink/subtle/stream_segment_decrypter.h" 26 #include "tink/util/statusor.h" 27 28 namespace crypto { 29 namespace tink { 30 namespace subtle { 31 32 // A RandomAccessStream that wraps another RandomAccessStream 33 // as a ciphertext source, and provides a "plaintext access" to 34 // the plaintext data contained in the ciphertext: 35 // - PRead()-calls to this class read appropriate segments 36 // of the ciphertext, decrypt them, and return the resulting 37 // plaintext, where the 'position' and 'count' arguments 38 // refer to the plaintext bytes. 39 // - size()-call returns the size of the entire plaintext 40 // if it were to be decrypted. 41 // Instances of this class are thread safe. 42 class DecryptingRandomAccessStream : public crypto::tink::RandomAccessStream { 43 public: 44 // A factory that produces decrypting random access streams. 45 // The returned stream is a wrapper around 'ciphertext_source', 46 // such that any bytes written via the wrapper are AEAD-decrypted 47 // by 'segment_decrypter' using 'associated_data' as associated 48 // authenticated data. 49 static crypto::tink::util::StatusOr< 50 std::unique_ptr<crypto::tink::RandomAccessStream>> 51 New(std::unique_ptr<StreamSegmentDecrypter> segment_decrypter, 52 std::unique_ptr<crypto::tink::RandomAccessStream> ciphertext_source); 53 54 // ----------------------- 55 // Methods of RandomAccessStream-interface implemented by this class. 56 crypto::tink::util::Status PRead( 57 int64_t position, int count, 58 crypto::tink::util::Buffer* dest_buffer) override; 59 crypto::tink::util::StatusOr<int64_t> size() override; 60 61 private: DecryptingRandomAccessStream()62 DecryptingRandomAccessStream() {} 63 crypto::tink::util::Status PReadAndDecrypt( 64 int64_t position, int count, crypto::tink::util::Buffer* dest_buffer); 65 // Reads the specified ciphertext segment from ct_source_, decrypts it, 66 // and writes the resulting plaintext bytes to pt_segment. 67 // Uses the provided ct_buffer as a buffer for the ciphertext segment. 68 crypto::tink::util::Status ReadAndDecryptSegment( 69 int64_t segment_nr, crypto::tink::util::Buffer* ct_buffer, 70 std::vector<uint8_t>* pt_segment); 71 // Returns the segment number that contains the specified 'pt_position'. 72 int64_t GetSegmentNr(int64_t pt_position); 73 // Returns the offset within a segment for the specified 'pt_position'. 74 int GetPlaintextOffset(int64_t pt_position); 75 // Initializes this stream (if not initialized yet or in a permantent error) 76 // by reading the stream header from ct_source_ and using it initialize 77 // segment_decrypter_. 78 void InitializeIfNeeded(); 79 std::unique_ptr<StreamSegmentDecrypter> segment_decrypter_; 80 std::unique_ptr<crypto::tink::RandomAccessStream> ct_source_; 81 82 mutable absl::Mutex status_mutex_; 83 crypto::tink::util::Status status_ ABSL_GUARDED_BY(status_mutex_); 84 int header_size_; 85 int ct_offset_; 86 int ct_segment_size_; 87 int pt_segment_size_; 88 int ct_segment_overhead_; 89 int64_t segment_count_; 90 int64_t pt_size_; 91 }; 92 93 } // namespace subtle 94 } // namespace tink 95 } // namespace crypto 96 97 #endif // TINK_SUBTLE_DECRYPTING_RANDOM_ACCESS_STREAM_H_ 98