xref: /aosp_15_r20/external/federated-compute/fcp/secagg/shared/ecdh_keys.h (revision 14675a029014e728ec732f129a32e299b2da0601)
1*14675a02SAndroid Build Coastguard Worker /*
2*14675a02SAndroid Build Coastguard Worker  * Copyright 2018 Google LLC
3*14675a02SAndroid Build Coastguard Worker  *
4*14675a02SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*14675a02SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*14675a02SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*14675a02SAndroid Build Coastguard Worker  *
8*14675a02SAndroid Build Coastguard Worker  *     https://www.apache.org/licenses/LICENSE-2.0
9*14675a02SAndroid Build Coastguard Worker  *
10*14675a02SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*14675a02SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*14675a02SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*14675a02SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*14675a02SAndroid Build Coastguard Worker  * limitations under the License.
15*14675a02SAndroid Build Coastguard Worker  */
16*14675a02SAndroid Build Coastguard Worker 
17*14675a02SAndroid Build Coastguard Worker #ifndef FCP_SECAGG_SHARED_ECDH_KEYS_H_
18*14675a02SAndroid Build Coastguard Worker #define FCP_SECAGG_SHARED_ECDH_KEYS_H_
19*14675a02SAndroid Build Coastguard Worker 
20*14675a02SAndroid Build Coastguard Worker #include "fcp/secagg/shared/key.h"
21*14675a02SAndroid Build Coastguard Worker 
22*14675a02SAndroid Build Coastguard Worker // This file contains definitions for ECDH public key and private key types.
23*14675a02SAndroid Build Coastguard Worker 
24*14675a02SAndroid Build Coastguard Worker namespace fcp {
25*14675a02SAndroid Build Coastguard Worker namespace secagg {
26*14675a02SAndroid Build Coastguard Worker // A Key that serves as a private key for use with ECDH, with the NIST P-256
27*14675a02SAndroid Build Coastguard Worker // curve. Works the same as Key, but is guaranteed to have either 0 or 32 bytes.
28*14675a02SAndroid Build Coastguard Worker // A 0-byte key should not be used for anything, and represents the absence of
29*14675a02SAndroid Build Coastguard Worker // a key in a collection of keys.
30*14675a02SAndroid Build Coastguard Worker class EcdhPrivateKey : public Key {
31*14675a02SAndroid Build Coastguard Worker  public:
32*14675a02SAndroid Build Coastguard Worker   static constexpr int kSize = 32;
33*14675a02SAndroid Build Coastguard Worker 
34*14675a02SAndroid Build Coastguard Worker   // The key is blank.
EcdhPrivateKey()35*14675a02SAndroid Build Coastguard Worker   EcdhPrivateKey() : Key() {}
36*14675a02SAndroid Build Coastguard Worker 
37*14675a02SAndroid Build Coastguard Worker   // The data MUST have 32 bytes.
EcdhPrivateKey(const uint8_t * data)38*14675a02SAndroid Build Coastguard Worker   explicit EcdhPrivateKey(const uint8_t* data) : Key(data, kSize) {}
39*14675a02SAndroid Build Coastguard Worker };
40*14675a02SAndroid Build Coastguard Worker 
41*14675a02SAndroid Build Coastguard Worker // A Key that serves as a public key for use with ECDH, with the NIST P-256
42*14675a02SAndroid Build Coastguard Worker // curve. Works the same as Key, but is guaranteed to have either 0, 33, or 65
43*14675a02SAndroid Build Coastguard Worker // bytes (depending on whether the key is compressed or not). Clients and the
44*14675a02SAndroid Build Coastguard Worker // server should both produce compressed keys, but legacy Java clients send
45*14675a02SAndroid Build Coastguard Worker // keys in uncompressed format.
46*14675a02SAndroid Build Coastguard Worker // A 0-byte key should not be used for anything, and represents the absence of
47*14675a02SAndroid Build Coastguard Worker // a key in a collection of keys.
48*14675a02SAndroid Build Coastguard Worker class EcdhPublicKey : public Key {
49*14675a02SAndroid Build Coastguard Worker  public:
50*14675a02SAndroid Build Coastguard Worker   static constexpr int kSize = 33;
51*14675a02SAndroid Build Coastguard Worker   // TODO(team): Remove uncompressed support when Java SecAgg deprecated.
52*14675a02SAndroid Build Coastguard Worker   static constexpr int kUncompressedSize = 65;
53*14675a02SAndroid Build Coastguard Worker   enum Format { kCompressed, kUncompressed };
54*14675a02SAndroid Build Coastguard Worker 
55*14675a02SAndroid Build Coastguard Worker   // The key is blank.
EcdhPublicKey()56*14675a02SAndroid Build Coastguard Worker   EcdhPublicKey() : Key() {}
57*14675a02SAndroid Build Coastguard Worker 
58*14675a02SAndroid Build Coastguard Worker   // If the key is compressed, data must have 33 bytes.
59*14675a02SAndroid Build Coastguard Worker   // If the key is uncompressed, data must have 65 bytes and the uncompressed
60*14675a02SAndroid Build Coastguard Worker   // format must be specified.
61*14675a02SAndroid Build Coastguard Worker   explicit EcdhPublicKey(const uint8_t* data, Format format = kCompressed)
62*14675a02SAndroid Build Coastguard Worker       : Key(data, format == kCompressed ? kSize : kUncompressedSize) {}
63*14675a02SAndroid Build Coastguard Worker };
64*14675a02SAndroid Build Coastguard Worker }  // namespace secagg
65*14675a02SAndroid Build Coastguard Worker }  // namespace fcp
66*14675a02SAndroid Build Coastguard Worker 
67*14675a02SAndroid Build Coastguard Worker #endif  // FCP_SECAGG_SHARED_ECDH_KEYS_H_
68