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_STREAM_SEGMENT_DECRYPTER_H_ 18 #define TINK_SUBTLE_STREAM_SEGMENT_DECRYPTER_H_ 19 20 #include <cstdint> 21 #include <vector> 22 23 #include "tink/util/status.h" 24 25 namespace crypto { 26 namespace tink { 27 namespace subtle { 28 29 // StreamSegmentDecrypter is a helper class that decrypts individual 30 // segments of a stream. 31 // 32 // Instances of this are passed to an ...DecryptingStream. Each instance 33 // of a segment decrypter must be initialized with a header of a ciphertext 34 // stream and is used to decrypt that stream. 35 // 36 // See stream_segment_encrypter.h for more info on the structure of 37 // a ciphertext stream. 38 class StreamSegmentDecrypter { 39 public: 40 // Decrypts 'ciphertext' as a segment, and writes the resulting plaintext 41 // to 'plaintext_buffer', adjusting its size as needed. 42 // 'ciphertext' and 'plaintext_buffer' must refer to distinct and 43 // non-overlapping space. 44 // Decryption uses the current value returned by get_segment_number() 45 // as the segment number, and subsequently increments the current 46 // segment number. 47 virtual util::Status DecryptSegment( 48 const std::vector<uint8_t>& ciphertext, 49 int64_t segment_number, 50 bool is_last_segment, 51 std::vector<uint8_t>* plaintext_buffer) = 0; 52 53 // Initializes this decrypter, using the information from 'header', 54 // which must be of size exactly get_header_size(). 55 virtual util::Status Init(const std::vector<uint8_t>& header) = 0; 56 57 // Returns the size (in bytes) of the header of a ciphertext stream. 58 virtual int get_header_size() const = 0; 59 60 // Returns the size (in bytes) of a plaintext segment. 61 virtual int get_plaintext_segment_size() const = 0; 62 63 // Returns the size (in bytes) of a ciphertext segment. 64 virtual int get_ciphertext_segment_size() const = 0; 65 66 // Returns the offset (in bytes) of the ciphertext within an decrypted stream. 67 // The offset is non-negative, and not larger than 68 // ciphertext_segment_size - (header_size + segment_overhead) 69 // where 70 // ciphertext_segment_size = get_ciphertext_segment_size() 71 // header_size = get_header_size() 72 // segment_overhead = ciphertext_segment_size - get_plaintext_segment_size() 73 virtual int get_ciphertext_offset() const = 0; 74 75 virtual ~StreamSegmentDecrypter() = default; 76 }; 77 78 } // namespace subtle 79 } // namespace tink 80 } // namespace crypto 81 82 #endif // TINK_SUBTLE_STREAM_SEGMENT_DECRYPTER_H_ 83