/*
* Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
- * (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
+ * (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
#include "config.h"
#include "RandomNumber.h"
+#include "CryptographicallyRandomNumber.h"
#include "RandomNumberSeed.h"
#include <limits>
#include <stdint.h>
#include <stdlib.h>
-namespace WTF {
+#if USE(MERSENNE_TWISTER_19937)
+extern "C" {
+#include "mt19937ar.c"
+}
+#endif
-double weakRandomNumber()
-{
-#if COMPILER(MSVC) && defined(_CRT_RAND_S)
- // rand_s is incredibly slow on windows so we fall back on rand for Math.random
- return (rand() + (rand() / (RAND_MAX + 1.0))) / (RAND_MAX + 1.0);
-#else
- return randomNumber();
+#if PLATFORM(BREWMP)
+#include <AEEAppGen.h>
+#include <AEESource.h>
+#include <AEEStdLib.h>
+#include <wtf/brew/RefPtrBrew.h>
+#include <wtf/brew/ShellBrew.h>
#endif
-}
+
+namespace WTF {
double randomNumber()
{
-#if !ENABLE(JSC_MULTIPLE_THREADS)
+#if USE(OS_RANDOMNESS)
+ uint32_t bits = cryptographicallyRandomNumber();
+ return static_cast<double>(bits) / (static_cast<double>(std::numeric_limits<uint32_t>::max()) + 1.0);
+#else
+ // Without OS_RANDOMNESS, we fall back to other random number generators
+ // that might not be cryptographically secure. Ideally, most ports would
+ // define USE(OS_RANDOMNESS).
+
+#if !ENABLE(WTF_MULTIPLE_THREADS)
static bool s_initialized = false;
if (!s_initialized) {
initializeRandomNumberGenerator();
s_initialized = true;
}
#endif
-
-#if COMPILER(MSVC) && defined(_CRT_RAND_S)
+
+#if USE(MERSENNE_TWISTER_19937)
+ return genrand_res53();
+#elif PLATFORM(BREWMP)
uint32_t bits;
- rand_s(&bits);
- return static_cast<double>(bits) / (static_cast<double>(std::numeric_limits<uint32_t>::max()) + 1.0);
-#elif PLATFORM(DARWIN)
- uint32_t bits = arc4random();
- return static_cast<double>(bits) / (static_cast<double>(std::numeric_limits<uint32_t>::max()) + 1.0);
-#elif PLATFORM(UNIX)
- uint32_t part1 = random() & (RAND_MAX - 1);
- uint32_t part2 = random() & (RAND_MAX - 1);
- // random only provides 31 bits
- uint64_t fullRandom = part1;
- fullRandom <<= 31;
- fullRandom |= part2;
+ // Is this a cryptographically strong source of random numbers? If so, we
+ // should move this into OSRandomSource.
+ // http://csrc.nist.gov/groups/STM/cmvp/documents/140-1/140sp/140sp851.pdf
+ // is slightly unclear on this point, although it seems to imply that it is
+ // secure.
+ RefPtr<ISource> randomSource = createRefPtrInstance<ISource>(AEECLSID_RANDOM);
+ ISOURCE_Read(randomSource.get(), reinterpret_cast<char*>(&bits), 4);
- // Mask off the low 53bits
- fullRandom &= (1LL << 53) - 1;
- return static_cast<double>(fullRandom)/static_cast<double>(1LL << 53);
+ return static_cast<double>(bits) / (static_cast<double>(std::numeric_limits<uint32_t>::max()) + 1.0);
#else
uint32_t part1 = rand() & (RAND_MAX - 1);
uint32_t part2 = rand() & (RAND_MAX - 1);
fullRandom &= (1LL << 53) - 1;
return static_cast<double>(fullRandom)/static_cast<double>(1LL << 53);
#endif
+#endif
}
}