xref: /aosp_15_r20/external/federated-compute/fcp/secagg/server/ssl_bit_gen.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2020 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  *      http://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 #include "fcp/secagg/server/ssl_bit_gen.h"
18 
19 #include <type_traits>
20 
21 // Thread-safety guarantees only apply in BoringSSL.
22 #include "fcp/base/monitoring.h"
23 #include "openssl/is_boringssl.h"
24 #include "openssl/rand.h"
25 
26 namespace fcp {
27 namespace secagg {
28 
operator ()()29 SslBitGen::result_type SslBitGen::operator()() {
30   static_assert(std::is_same<uint8_t, unsigned char>::value,
31                 "uint8_t being other than unsigned char isn't supported by "
32                 "BoringSSL");
33   SslBitGen::result_type random_integer;
34   int success = RAND_bytes(reinterpret_cast<unsigned char*>(&random_integer),
35                            sizeof(random_integer));
36   // RAND_bytes always returns 1 in BoringSSL
37   FCP_CHECK(success == 1);
38 
39   return random_integer;
40 }
41 
42 }  // namespace secagg
43 }  // namespace fcp
44