Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CryptographicRandom.h « crypto « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c8ca256b86b5f0750c3c790d00769e1459936b3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright 2020-2022 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#ifndef MUMBLE_CRYPTOGRAPHICRANDOM_H_
#define MUMBLE_CRYPTOGRAPHICRANDOM_H_

#include <stdint.h>

/// CryptographicRandom provides useful utilities for acquiring
/// pseudo-random data for use in cryptography.
class CryptographicRandom {
public:
	/// Fill the buffer at |buf| with |numBytes| bytes of pseudo-random data.
	/// The value of |numBytes| must be >= 0. Otherwise, program execution is aborted.
	///
	/// This function is equivalent to the arc4random_buf() function from OpenBSD.
	///
	/// This function should not ever fail. It is considered a catastrophic failure if
	/// it does. The program is aborted if the function fails, because it is generally
	/// impossible to recover from such a scenario.
	static void fillBuffer(void *buf, int numBytes);

	/// Return a pseudo-random number in the range of 0 to (2**32)-1.
	///
	/// This function is equivalent to the arc4random() function from OpenBSD.
	///
	/// This function should not ever fail. It is considered a catastrophic failure if
	/// it does. The program is aborted if the function fails, because it is generally
	/// impossible to recover from such a scenario.
	static uint32_t uint32();

	/// Calculate a uniformly distributed random number less than |upperBound|
	/// avoiding "modulo bias".
	///
	/// This function is equivalent to the arc4random_uniform() function from OpenBSD.
	///
	/// This function should not ever fail. It is considered a catastrophic failure if
	/// it does. The program is aborted if the function fails, because it is generally
	/// impossible to recover from such a scenario.
	static uint32_t uniform(uint32_t upperBound);
};

#endif