1 // Copyright 2023 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_AEAD_AES_GCM_KEY_H_ 18 #define TINK_AEAD_AES_GCM_KEY_H_ 19 20 #include <string> 21 #include <utility> 22 23 #include "absl/strings/string_view.h" 24 #include "absl/types/optional.h" 25 #include "tink/aead/aead_key.h" 26 #include "tink/aead/aes_gcm_parameters.h" 27 #include "tink/partial_key_access_token.h" 28 #include "tink/restricted_data.h" 29 #include "tink/util/statusor.h" 30 31 namespace crypto { 32 namespace tink { 33 34 // Represents an AEAD that uses AES-GCM. 35 class AesGcmKey : public AeadKey { 36 public: 37 // Copyable and movable. 38 AesGcmKey(const AesGcmKey& other) = default; 39 AesGcmKey& operator=(const AesGcmKey& other) = default; 40 AesGcmKey(AesGcmKey&& other) = default; 41 AesGcmKey& operator=(AesGcmKey&& other) = default; 42 43 // Creates a new AES-GCM key. If the parameters specify a variant that uses 44 // a prefix, then the id is used to compute this prefix. 45 static util::StatusOr<AesGcmKey> Create(const AesGcmParameters& parameters, 46 const RestrictedData& key_bytes, 47 absl::optional<int> id_requirement, 48 PartialKeyAccessToken token); 49 50 // Returns the underlying AES key. GetKeyBytes(PartialKeyAccessToken token)51 const RestrictedData& GetKeyBytes(PartialKeyAccessToken token) const { 52 return key_bytes_; 53 } 54 GetOutputPrefix()55 absl::string_view GetOutputPrefix() const override { return output_prefix_; } 56 GetParameters()57 const AesGcmParameters& GetParameters() const override { return parameters_; } 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: AesGcmKey(const AesGcmParameters & parameters,const RestrictedData & key_bytes,absl::optional<int> id_requirement,std::string output_prefix)66 AesGcmKey(const AesGcmParameters& parameters, const RestrictedData& key_bytes, 67 absl::optional<int> id_requirement, 68 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 AesGcmParameters parameters_; 75 RestrictedData key_bytes_; 76 absl::optional<int> id_requirement_; 77 std::string output_prefix_; 78 }; 79 80 } // namespace tink 81 } // namespace crypto 82 83 #endif // TINK_AEAD_AES_GCM_KEY_H_ 84