xref: /aosp_15_r20/external/tink/cc/daead/aes_siv_key.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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_DAEAD_AES_SIV_KEY_H_
18 #define TINK_DAEAD_AES_SIV_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/daead/aes_siv_parameters.h"
26 #include "tink/daead/deterministic_aead_key.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 a Deterministic AEAD that uses AES-SIV.
35 class AesSivKey : public DeterministicAeadKey {
36  public:
37   // Copyable and movable.
38   AesSivKey(const AesSivKey& other) = default;
39   AesSivKey& operator=(const AesSivKey& other) = default;
40   AesSivKey(AesSivKey&& other) = default;
41   AesSivKey& operator=(AesSivKey&& other) = default;
42 
43   // Creates a new AES-SIV 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<AesSivKey> Create(const AesSivParameters& parameters,
46                                           const RestrictedData& key_bytes,
47                                           absl::optional<int> id_requirement,
48                                           PartialKeyAccessToken token);
49 
50   // Returns the underlying AES-SIV 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 AesSivParameters& 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:
AesSivKey(const AesSivParameters & parameters,const RestrictedData & key_bytes,absl::optional<int> id_requirement,std::string output_prefix)66   AesSivKey(const AesSivParameters& parameters, const RestrictedData& key_bytes,
67             absl::optional<int> id_requirement, std::string output_prefix)
68       : parameters_(parameters),
69         key_bytes_(key_bytes),
70         id_requirement_(id_requirement),
71         output_prefix_(std::move(output_prefix)) {}
72 
73   AesSivParameters parameters_;
74   RestrictedData key_bytes_;
75   absl::optional<int> id_requirement_;
76   std::string output_prefix_;
77 };
78 
79 }  // namespace tink
80 }  // namespace crypto
81 
82 #endif  // TINK_DAEAD_AES_SIV_KEY_H_
83