xref: /aosp_15_r20/external/tink/cc/aead/aes_gcm_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_AEAD_AES_GCM_PARAMETERS_H_
18 #define TINK_AEAD_AES_GCM_PARAMETERS_H_
19 
20 #include "tink/aead/aead_parameters.h"
21 #include "tink/util/statusor.h"
22 
23 namespace crypto {
24 namespace tink {
25 
26 // Describes the parameters of an `AesGcmKey`.
27 class AesGcmParameters : public AeadParameters {
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   // Creates AES-GCM parameters instances.
42   class Builder {
43    public:
44     // Copyable and movable.
45     Builder(const Builder& other) = default;
46     Builder& operator=(const Builder& other) = default;
47     Builder(Builder&& other) = default;
48     Builder& operator=(Builder&& other) = default;
49 
50     // Creates initially empty parameters builder.
51     Builder() = default;
52 
53     Builder& SetKeySizeInBytes(int key_size);
54     Builder& SetIvSizeInBytes(int iv_size);
55     Builder& SetTagSizeInBytes(int tag_size);
56     Builder& SetVariant(Variant variant);
57 
58     // Creates AES-GCM parameters object from this builder.
59     util::StatusOr<AesGcmParameters> Build();
60 
61    private:
62     int key_size_in_bytes_;
63     int iv_size_in_bytes_;
64     int tag_size_in_bytes_;
65     Variant variant_;
66   };
67 
68   // Copyable and movable.
69   AesGcmParameters(const AesGcmParameters& other) = default;
70   AesGcmParameters& operator=(const AesGcmParameters& other) = default;
71   AesGcmParameters(AesGcmParameters&& other) = default;
72   AesGcmParameters& operator=(AesGcmParameters&& other) = default;
73 
KeySizeInBytes()74   int KeySizeInBytes() const { return key_size_in_bytes_; }
75 
IvSizeInBytes()76   int IvSizeInBytes() const { return iv_size_in_bytes_; }
77 
TagSizeInBytes()78   int TagSizeInBytes() const { return tag_size_in_bytes_; }
79 
GetVariant()80   Variant GetVariant() const { return variant_; }
81 
HasIdRequirement()82   bool HasIdRequirement() const override {
83     return variant_ != Variant::kNoPrefix;
84   }
85 
86   bool operator==(const Parameters& other) const override;
87 
88  private:
AesGcmParameters(int key_size_in_bytes,int iv_size_in_bytes,int tag_size_in_bytes,Variant variant)89   AesGcmParameters(int key_size_in_bytes, int iv_size_in_bytes,
90                    int tag_size_in_bytes, Variant variant)
91       : key_size_in_bytes_(key_size_in_bytes),
92         iv_size_in_bytes_(iv_size_in_bytes),
93         tag_size_in_bytes_(tag_size_in_bytes),
94         variant_(variant) {}
95 
96   int key_size_in_bytes_;
97   int iv_size_in_bytes_;
98   int tag_size_in_bytes_;
99   Variant variant_;
100 };
101 
102 }  // namespace tink
103 }  // namespace crypto
104 
105 #endif  // TINK_AEAD_AES_GCM_PARAMETERS_H_
106