xref: /aosp_15_r20/external/federated-compute/fcp/secagg/shared/prng.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_PRNG_H_
18 #define FCP_SECAGG_SHARED_PRNG_H_
19 
20 #include <cstdint>
21 #include <vector>
22 
23 namespace fcp {
24 namespace secagg {
25 
26 // An interface for a secure pseudo-random number generator.
27 class SecurePrng {
28  public:
29   virtual uint8_t Rand8() = 0;
30   virtual uint64_t Rand64() = 0;
31   virtual ~SecurePrng() = default;
32 };
33 
34 // Extension of SecurePrng interface that supports batch mode - getting multiple
35 // pseudo-random numbers in a single call.
36 class SecureBatchPrng : public SecurePrng {
37  public:
38   // Get the maximum size of a buffer that can be filled by RandBuffer() in a
39   // single call.
40   virtual size_t GetMaxBufferSize() const = 0;
41 
42   // Fills the provided buffer with pseudorandom bytes. Returns the number of
43   // bytes that has been generated, which can be smaller than the requested
44   // buffer_size if it exceeds the maximum buffer size.
45   virtual int RandBuffer(uint8_t* buffer, int buffer_size) = 0;
46 };
47 
48 }  // namespace secagg
49 }  // namespace fcp
50 
51 #endif  // FCP_SECAGG_SHARED_PRNG_H_
52