1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
7 /* Routines for generating random numbers */
9 #ifndef mozilla_RandomNum_h_
10 #define mozilla_RandomNum_h_
12 #include "mozilla/Maybe.h"
13 #include "mozilla/Types.h"
18 * Generate cryptographically secure random bytes using the best facilities
19 * available on the current OS.
21 * Return value: true if random bytes were copied into `aBuffer` or false on
24 * Useful whenever a secure random number is needed and NSS isn't available.
25 * (Perhaps because it hasn't been initialized yet)
28 * Windows: RtlGenRandom()
29 * Android, Darwin, DragonFly, FreeBSD, OpenBSD, NetBSD: arc4random()
30 * Linux: getrandom() if available, "/dev/urandom" otherwise
31 * Other Unix: "/dev/urandom"
34 [[nodiscard
]] MFBT_API
bool GenerateRandomBytesFromOS(void* aBuffer
,
38 * Generate a cryptographically secure random 64-bit unsigned number using the
39 * best facilities available on the current OS.
41 MFBT_API Maybe
<uint64_t> RandomUint64();
44 * Like RandomUint64, but always returns a uint64_t or crashes with an assert
45 * if the underlying RandomUint64 call failed.
47 MFBT_API
uint64_t RandomUint64OrDie();
49 } // namespace mozilla
51 #endif // mozilla_RandomNum_h_