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_NONCE_BASED_STREAMING_AEAD_H_ 18 #define TINK_SUBTLE_NONCE_BASED_STREAMING_AEAD_H_ 19 20 #include <memory> 21 22 #include "absl/strings/string_view.h" 23 #include "tink/input_stream.h" 24 #include "tink/output_stream.h" 25 #include "tink/random_access_stream.h" 26 #include "tink/streaming_aead.h" 27 #include "tink/subtle/stream_segment_decrypter.h" 28 #include "tink/subtle/stream_segment_encrypter.h" 29 #include "tink/util/statusor.h" 30 31 namespace crypto { 32 namespace tink { 33 namespace subtle { 34 35 // An abstract class for StreamingAead using the nonce based online encryption 36 // scheme proposed in "Online Authenticated-Encryption and its Nonce-Reuse 37 // Misuse-Resistance" by Hoang, Reyhanitabar, Rogaway and Vizár 38 // (https://eprint.iacr.org/2015/189.pdf) 39 class NonceBasedStreamingAead : public StreamingAead { 40 public: 41 // Methods of StreamingAead-interface implemented by this class. 42 crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::OutputStream>> 43 NewEncryptingStream( 44 std::unique_ptr<crypto::tink::OutputStream> ciphertext_destination, 45 absl::string_view associated_data) const override; 46 47 crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::InputStream>> 48 NewDecryptingStream( 49 std::unique_ptr<crypto::tink::InputStream> ciphertext_source, 50 absl::string_view associated_data) const override; 51 52 crypto::tink::util::StatusOr< 53 std::unique_ptr<crypto::tink::RandomAccessStream>> 54 NewDecryptingRandomAccessStream( 55 std::unique_ptr<crypto::tink::RandomAccessStream> ciphertext_source, 56 absl::string_view associated_data) const override; 57 58 protected: 59 // Methods to be implemented by a subclass of this class. 60 61 // Returns a new StreamSegmentEncrypter that uses `associated_data` for AEAD. 62 virtual crypto::tink::util::StatusOr<std::unique_ptr<StreamSegmentEncrypter>> 63 NewSegmentEncrypter(absl::string_view associated_data) const = 0; 64 65 // Returns a new StreamSegmentDecrypter that uses `associated_data` for AEAD. 66 virtual crypto::tink::util::StatusOr<std::unique_ptr<StreamSegmentDecrypter>> 67 NewSegmentDecrypter(absl::string_view associated_data) const = 0; 68 }; 69 70 } // namespace subtle 71 } // namespace tink 72 } // namespace crypto 73 74 #endif // TINK_SUBTLE_NONCE_BASED_STREAMING_AEAD_H_ 75