xref: /aosp_15_r20/external/federated-compute/fcp/secagg/shared/ecdh_keys.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_ECDH_KEYS_H_
18 #define FCP_SECAGG_SHARED_ECDH_KEYS_H_
19 
20 #include "fcp/secagg/shared/key.h"
21 
22 // This file contains definitions for ECDH public key and private key types.
23 
24 namespace fcp {
25 namespace secagg {
26 // A Key that serves as a private key for use with ECDH, with the NIST P-256
27 // curve. Works the same as Key, but is guaranteed to have either 0 or 32 bytes.
28 // A 0-byte key should not be used for anything, and represents the absence of
29 // a key in a collection of keys.
30 class EcdhPrivateKey : public Key {
31  public:
32   static constexpr int kSize = 32;
33 
34   // The key is blank.
EcdhPrivateKey()35   EcdhPrivateKey() : Key() {}
36 
37   // The data MUST have 32 bytes.
EcdhPrivateKey(const uint8_t * data)38   explicit EcdhPrivateKey(const uint8_t* data) : Key(data, kSize) {}
39 };
40 
41 // A Key that serves as a public key for use with ECDH, with the NIST P-256
42 // curve. Works the same as Key, but is guaranteed to have either 0, 33, or 65
43 // bytes (depending on whether the key is compressed or not). Clients and the
44 // server should both produce compressed keys, but legacy Java clients send
45 // keys in uncompressed format.
46 // A 0-byte key should not be used for anything, and represents the absence of
47 // a key in a collection of keys.
48 class EcdhPublicKey : public Key {
49  public:
50   static constexpr int kSize = 33;
51   // TODO(team): Remove uncompressed support when Java SecAgg deprecated.
52   static constexpr int kUncompressedSize = 65;
53   enum Format { kCompressed, kUncompressed };
54 
55   // The key is blank.
EcdhPublicKey()56   EcdhPublicKey() : Key() {}
57 
58   // If the key is compressed, data must have 33 bytes.
59   // If the key is uncompressed, data must have 65 bytes and the uncompressed
60   // format must be specified.
61   explicit EcdhPublicKey(const uint8_t* data, Format format = kCompressed)
62       : Key(data, format == kCompressed ? kSize : kUncompressedSize) {}
63 };
64 }  // namespace secagg
65 }  // namespace fcp
66 
67 #endif  // FCP_SECAGG_SHARED_ECDH_KEYS_H_
68