xref: /aosp_15_r20/external/tink/cc/daead/aes_siv_parameters.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_PARAMETERS_H_
18 #define TINK_DAEAD_AES_SIV_PARAMETERS_H_
19 
20 #include "tink/daead/deterministic_aead_parameters.h"
21 #include "tink/util/statusor.h"
22 
23 namespace crypto {
24 namespace tink {
25 
26 // Describes the parameters of an `AesSivKey`.
27 class AesSivParameters : public DeterministicAeadParameters {
28  public:
29   // Description of the output prefix prepended to the ciphertext.
30   enum class Variant : int {
31     // Prepends '0x01<big endian key id>' to the ciphertext.
32     kTink = 1,
33     // Prepends '0x00<big endian key id>' to the ciphertext.
34     kCrunchy = 2,
35     // Does not prepend any prefix (i.e., keys must have no ID requirement).
36     kNoPrefix = 3,
37     // Added to guard from failures that may be caused by future expansions.
38     kDoNotUseInsteadUseDefaultWhenWritingSwitchStatements = 20,
39   };
40 
41   // Copyable and movable.
42   AesSivParameters(const AesSivParameters& other) = default;
43   AesSivParameters& operator=(const AesSivParameters& other) = default;
44   AesSivParameters(AesSivParameters&& other) = default;
45   AesSivParameters& operator=(AesSivParameters&& other) = default;
46 
47   // Creates `AesSivParameters` object from `key_size_in_bytes` and `variant`.
48   // Only allows 32-, 48-, and 64-byte key sizes as specified in RFC 5297.
49   static util::StatusOr<AesSivParameters> Create(int key_size_in_bytes,
50                                                  Variant variant);
51 
KeySizeInBytes()52   int KeySizeInBytes() const { return key_size_in_bytes_; }
53 
GetVariant()54   Variant GetVariant() const { return variant_; }
55 
HasIdRequirement()56   bool HasIdRequirement() const override {
57     return variant_ != Variant::kNoPrefix;
58   }
59 
60   bool operator==(const Parameters& other) const override;
61 
62  private:
AesSivParameters(int key_size_in_bytes,Variant variant)63   AesSivParameters(int key_size_in_bytes, Variant variant)
64       : key_size_in_bytes_(key_size_in_bytes), variant_(variant) {}
65 
66   int key_size_in_bytes_;
67   Variant variant_;
68 };
69 
70 }  // namespace tink
71 }  // namespace crypto
72 
73 #endif  // TINK_DAEAD_AES_SIV_PARAMETERS_H_
74