xref: /aosp_15_r20/external/federated-compute/fcp/secagg/shared/key.h (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2018 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef FCP_SECAGG_SHARED_KEY_H_
18 #define FCP_SECAGG_SHARED_KEY_H_
19 
20 #include <cstdint>
21 #include <string>
22 
23 namespace fcp {
24 namespace secagg {
25 // An immutable type that encapsulates a key to be used with OpenSSL. Stores the
26 // key as std::string, but for better interaction with the OpenSSL API, the Key
27 // API treats the key as either a string or a const uint8_t*.
28 //
29 // Note that this doesn't replace any OpenSSL structure, it simply allows for
30 // storage of keys at rest without needing to store associated OpenSSL data.
31 class Key {
32  public:
Key()33   Key() : data_("") {}
34 
Key(const uint8_t * data,int size)35   Key(const uint8_t* data, int size)
36       : data_(reinterpret_cast<const char*>(data), size) {}
37 
data()38   inline const uint8_t* data() const {
39     return reinterpret_cast<const uint8_t*>(data_.c_str());
40   }
41 
size()42   inline const int size() const { return data_.size(); }
43 
AsString()44   inline const std::string AsString() const { return data_; }
45 
46   friend inline bool operator==(const Key& lhs, const Key& rhs) {
47     return lhs.data_ == rhs.data_;
48   }
49 
50  private:
51   std::string data_;  // The binary key data.
52 };
53 }  // namespace secagg
54 }  // namespace fcp
55 
56 #endif  // FCP_SECAGG_SHARED_KEY_H_
57