xref: /aosp_15_r20/external/federated-compute/fcp/secagg/shared/compute_session_id.cc (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 #include "fcp/secagg/shared/compute_session_id.h"
17 
18 #include <string>
19 
20 #include "fcp/base/monitoring.h"
21 #include "fcp/secagg/shared/math.h"
22 #include "fcp/secagg/shared/secagg_messages.pb.h"
23 #include "openssl/evp.h"
24 
25 namespace fcp {
26 namespace secagg {
27 
ComputeSessionId(const ShareKeysRequest & request)28 SessionId ComputeSessionId(const ShareKeysRequest& request) {
29   EVP_MD_CTX* ctx;
30   FCP_CHECK(ctx = EVP_MD_CTX_create());
31   FCP_CHECK(EVP_DigestInit_ex(ctx, EVP_sha256(), nullptr));
32   for (const PairOfPublicKeys& keys : request.pairs_of_public_keys()) {
33     int noise_pk_size = keys.noise_pk().size();
34     std::string noise_pk_size_data = IntToByteString(noise_pk_size);
35     int enc_pk_size = keys.enc_pk().size();
36     std::string enc_pk_size_data = IntToByteString(noise_pk_size);
37     FCP_CHECK(EVP_DigestUpdate(ctx, noise_pk_size_data.c_str(), sizeof(int)));
38     FCP_CHECK(EVP_DigestUpdate(ctx, keys.noise_pk().c_str(), noise_pk_size));
39     FCP_CHECK(EVP_DigestUpdate(ctx, enc_pk_size_data.c_str(), sizeof(int)));
40     FCP_CHECK(EVP_DigestUpdate(ctx, keys.enc_pk().c_str(), enc_pk_size));
41   }
42 
43   char digest[kSha256Length];
44   uint32_t digest_length = 0;
45   FCP_CHECK(EVP_DigestFinal_ex(ctx, reinterpret_cast<uint8_t*>(digest),
46                                &digest_length));
47   FCP_CHECK(digest_length == kSha256Length);
48   EVP_MD_CTX_destroy(ctx);
49   return {std::string(digest, kSha256Length)};
50 }
51 
52 }  // namespace secagg
53 }  // namespace fcp
54