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_AES_CMAC_KEY_H_ 18 #define TINK_MAC_AES_CMAC_KEY_H_ 19 20 #include <string> 21 #include <utility> 22 23 #include "absl/types/optional.h" 24 #include "tink/mac/aes_cmac_parameters.h" 25 #include "tink/mac/mac_key.h" 26 #include "tink/partial_key_access_token.h" 27 #include "tink/restricted_data.h" 28 #include "tink/util/statusor.h" 29 30 namespace crypto { 31 namespace tink { 32 33 class AesCmacKey : public MacKey { 34 public: 35 // Copyable and movable. 36 AesCmacKey(const AesCmacKey& other) = default; 37 AesCmacKey& operator=(const AesCmacKey& other) = default; 38 AesCmacKey(AesCmacKey&& other) = default; 39 AesCmacKey& operator=(AesCmacKey&& other) = default; 40 41 // Creates a new AES-CMAC key. If the parameters specify a variant that uses 42 // a prefix, then the id is used to compute this prefix. 43 static util::StatusOr<AesCmacKey> Create(const AesCmacParameters& parameters, 44 const RestrictedData& key_bytes, 45 absl::optional<int> id_requirement, 46 PartialKeyAccessToken token); 47 48 // Returns the underlying AES key. GetKeyBytes(PartialKeyAccessToken token)49 const RestrictedData& GetKeyBytes(PartialKeyAccessToken token) const { 50 return key_bytes_; 51 } 52 GetOutputPrefix()53 absl::string_view GetOutputPrefix() const override { return output_prefix_; } 54 GetParameters()55 const AesCmacParameters& GetParameters() const override { 56 return parameters_; 57 } 58 GetIdRequirement()59 absl::optional<int> GetIdRequirement() const override { 60 return id_requirement_; 61 } 62 63 bool operator==(const Key& other) const override; 64 65 private: AesCmacKey(const AesCmacParameters & parameters,const RestrictedData & key_bytes,absl::optional<int> id_requirement,std::string output_prefix)66 AesCmacKey(const AesCmacParameters& parameters, 67 const RestrictedData& key_bytes, 68 absl::optional<int> id_requirement, std::string output_prefix) 69 : parameters_(parameters), 70 key_bytes_(key_bytes), 71 id_requirement_(id_requirement), 72 output_prefix_(std::move(output_prefix)) {} 73 74 static util::StatusOr<std::string> ComputeOutputPrefix( 75 const AesCmacParameters& parameters, absl::optional<int> id_requirement); 76 77 AesCmacParameters parameters_; 78 RestrictedData key_bytes_; 79 absl::optional<int> id_requirement_; 80 std::string output_prefix_; 81 }; 82 83 } // namespace tink 84 } // namespace crypto 85 86 #endif // TINK_MAC_AES_CMAC_KEY_H_ 87