Bug 1890277: part 2) Add `require-trusted-types-for` directive to CSP parser, guarded...
[gecko.git] / mfbt / RandomNum.h
blob23a24837e9099e97dace7e1cd82ef6c8276cd75c
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"
15 namespace mozilla {
17 /**
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
22 * error.
24 * Useful whenever a secure random number is needed and NSS isn't available.
25 * (Perhaps because it hasn't been initialized yet)
27 * Current mechanisms:
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,
35 size_t aLength);
37 /**
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();
43 /**
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_