1 // Copyright 2022 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_MAC_INTERNAL_CHUNKED_MAC_IMPL_H_ 18 #define TINK_MAC_INTERNAL_CHUNKED_MAC_IMPL_H_ 19 20 #include <memory> 21 #include <string> 22 #include <utility> 23 24 #include "absl/strings/string_view.h" 25 #include "tink/chunked_mac.h" 26 #include "tink/subtle/mac/stateful_mac.h" 27 #include "tink/util/status.h" 28 #include "tink/util/statusor.h" 29 #include "proto/aes_cmac.pb.h" 30 #include "proto/hmac.pb.h" 31 32 namespace crypto { 33 namespace tink { 34 namespace internal { 35 36 class ChunkedMacComputationImpl : public ChunkedMacComputation { 37 public: ChunkedMacComputationImpl(std::unique_ptr<subtle::StatefulMac> stateful_mac)38 explicit ChunkedMacComputationImpl( 39 std::unique_ptr<subtle::StatefulMac> stateful_mac) 40 : stateful_mac_(std::move(stateful_mac)) {} 41 42 util::Status Update(absl::string_view data) override; 43 44 util::StatusOr<std::string> ComputeMac() override; 45 46 private: 47 const std::unique_ptr<subtle::StatefulMac> stateful_mac_; 48 util::Status status_ = util::OkStatus(); 49 }; 50 51 class ChunkedMacVerificationImpl : public ChunkedMacVerification { 52 public: ChunkedMacVerificationImpl(std::unique_ptr<subtle::StatefulMac> stateful_mac,absl::string_view tag)53 explicit ChunkedMacVerificationImpl( 54 std::unique_ptr<subtle::StatefulMac> stateful_mac, absl::string_view tag) 55 : stateful_mac_(std::move(stateful_mac)), tag_(tag) {} 56 57 util::Status Update(absl::string_view data) override; 58 59 util::Status VerifyMac() override; 60 61 private: 62 const std::unique_ptr<subtle::StatefulMac> stateful_mac_; 63 const std::string tag_; 64 util::Status status_ = util::OkStatus(); 65 }; 66 67 class ChunkedMacImpl : public ChunkedMac { 68 public: ChunkedMacImpl(std::unique_ptr<subtle::StatefulMacFactory> stateful_mac_factory)69 explicit ChunkedMacImpl( 70 std::unique_ptr<subtle::StatefulMacFactory> stateful_mac_factory) 71 : stateful_mac_factory_(std::move(stateful_mac_factory)) {} 72 73 util::StatusOr<std::unique_ptr<ChunkedMacComputation>> CreateComputation() 74 const override; 75 76 util::StatusOr<std::unique_ptr<ChunkedMacVerification>> CreateVerification( 77 absl::string_view tag) const override; 78 79 private: 80 std::unique_ptr<subtle::StatefulMacFactory> stateful_mac_factory_; 81 }; 82 83 // Create new Chunked CMAC instance from `key`. 84 util::StatusOr<std::unique_ptr<ChunkedMac>> NewChunkedCmac( 85 const google::crypto::tink::AesCmacKey& key); 86 87 // Create new Chunked HMAC instance from `key`. 88 util::StatusOr<std::unique_ptr<ChunkedMac>> NewChunkedHmac( 89 const google::crypto::tink::HmacKey& key); 90 91 } // namespace internal 92 } // namespace tink 93 } // namespace crypto 94 95 #endif // TINK_MAC_INTERNAL_CHUNKED_MAC_IMPL_H_ 96