tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

RandomNum.h (1635B)


      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/. */
      6 
      7 /* Routines for generating random numbers */
      8 
      9 #ifndef mozilla_RandomNum_h_
     10 #define mozilla_RandomNum_h_
     11 
     12 #include "mozilla/Maybe.h"
     13 #include "mozilla/Types.h"
     14 
     15 namespace mozilla {
     16 
     17 /**
     18 *  Generate cryptographically secure random bytes using the best facilities
     19 *  available on the current OS.
     20 *
     21 *  Return value: true if random bytes were copied into `aBuffer` or false on
     22 *  error.
     23 *
     24 *  Useful whenever a secure random number is needed and NSS isn't available.
     25 *  (Perhaps because it hasn't been initialized yet)
     26 *
     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"
     32 *
     33 */
     34 [[nodiscard]] MFBT_API bool GenerateRandomBytesFromOS(void* aBuffer,
     35                                                      size_t aLength);
     36 
     37 /**
     38 *  Generate a cryptographically secure random 64-bit unsigned number using the
     39 *  best facilities available on the current OS.
     40 */
     41 MFBT_API Maybe<uint64_t> RandomUint64();
     42 
     43 /**
     44 *  Like RandomUint64, but always returns a uint64_t or crashes with an assert
     45 *  if the underlying RandomUint64 call failed.
     46 */
     47 MFBT_API uint64_t RandomUint64OrDie();
     48 
     49 }  // namespace mozilla
     50 
     51 #endif  // mozilla_RandomNum_h_