Source/JavaScriptCore/Android.mk

@@LOCAL_SRC_FILES := \
161161 \
162162 wtf/Assertions.cpp \
163163 wtf/ByteArray.cpp \
 164 wtf/CryptographicallyRandomNumber.cpp \
164165 wtf/CurrentTime.cpp \
165166 wtf/DateMath.cpp \
166167 wtf/DecimalNumber.cpp \
167168 wtf/FastMalloc.cpp \
168169 wtf/HashTable.cpp \
169170 wtf/MainThread.cpp \
 171 wtf/OSRandomSource.cpp \
170172 wtf/PageAllocationAligned.cpp\
171173 wtf/PageBlock.cpp\
172174 wtf/RandomNumber.cpp \
173175 wtf/RefCountedLeakCounter.cpp \
174  wtf/StackBounds.cpp \
 176 wtf/StackBounds.cpp \
175177 wtf/TCSystemAlloc.cpp \
176178 wtf/ThreadIdentifierDataPthreads.cpp \
177179 wtf/Threading.cpp \

Source/JavaScriptCore/Android.v8.wtf.mk

@@LOCAL_SRC_FILES := \
3434 \
3535 wtf/Assertions.cpp \
3636 wtf/ByteArray.cpp \
 37 wtf/CryptographicallyRandomNumber.cpp \
3738 wtf/CurrentTime.cpp \
3839 wtf/DateMath.cpp \
3940 wtf/DecimalNumber.cpp \
4041 wtf/FastMalloc.cpp \
4142 wtf/HashTable.cpp \
4243 wtf/MainThread.cpp \
 44 wtf/OSRandomSource.cpp \
4345 wtf/RandomNumber.cpp \
4446 wtf/RefCountedLeakCounter.cpp \
45  wtf/StackBounds.cpp \
 47 wtf/StackBounds.cpp \
4648 wtf/TCSystemAlloc.cpp \
4749 wtf/ThreadIdentifierDataPthreads.cpp \
4850 wtf/Threading.cpp \

Source/JavaScriptCore/ChangeLog

 12011-02-09 Adam Barth <abarth@webkit.org>
 2
 3 Reviewed by NOBODY (OOPS!).
 4
 5 Add WTF::cryptographicallyRandomNumber
 6 https://bugs.webkit.org/show_bug.cgi?id=54083
 7
 8 Introduce a cryptographically strong random number generator to WTF.
 9 The random number generator is based on arc4random as found in:
 10
 11 http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/crypt/arc4random.c?rev=1.22
 12
 13 I've changed to source to WebKit style and abstracted the operating
 14 system interaction to OSRandomSource. We'll use this functionality to
 15 expose a cryptographically strong random number generator to
 16 JavaScript.
 17
 18 * Android.mk:
 19 * Android.v8.wtf.mk:
 20 * GNUmakefile.am:
 21 * JavaScriptCore.gypi:
 22 * JavaScriptCore.vcproj/WTF/WTF.vcproj:
 23 * JavaScriptCore.xcodeproj/project.pbxproj:
 24 * config.h:
 25 * wtf/CMakeLists.txt:
 26 * wtf/CryptographicallyRandomNumber.cpp: Added.
 27 (WTF::initMutexIfNeeded):
 28 (WTF::init):
 29 (WTF::addRandomData):
 30 (WTF::stir):
 31 (WTF::stirIfNeeded):
 32 (WTF::getByte):
 33 (WTF::getWord):
 34 (WTF::cryptographicallyRandomNumber):
 35 (WTF::cryptographicallyRandomValues):
 36 * wtf/CryptographicallyRandomNumber.h: Added.
 37 * wtf/OSRandomSource.cpp: Added.
 38 (WTF::randomValuesFromOS):
 39 * wtf/OSRandomSource.h: Added.
 40 * wtf/wtf.pri:
 41
1422011-02-08 Geoffrey Garen <ggaren@apple.com>
243
344 Reviewed by Sam Weinig.

Source/JavaScriptCore/GNUmakefile.am

@@javascriptcore_sources += \
443443 Source/JavaScriptCore/wtf/ByteArray.cpp \
444444 Source/JavaScriptCore/wtf/ByteArray.h \
445445 Source/JavaScriptCore/wtf/CrossThreadRefCounted.h \
 446 Source/JavaScriptCore/wtf/CryptographicallyRandomNumber.cpp \
 447 Source/JavaScriptCore/wtf/CryptographicallyRandomNumber.h \
446448 Source/JavaScriptCore/wtf/CurrentTime.cpp \
447449 Source/JavaScriptCore/wtf/CurrentTime.h \
448450 Source/JavaScriptCore/wtf/DateMath.cpp \

@@javascriptcore_sources += \
491493 Source/JavaScriptCore/wtf/NotFound.h \
492494 Source/JavaScriptCore/wtf/NullPtr.h \
493495 Source/JavaScriptCore/wtf/OSAllocator.h \
 496 Source/JavaScriptCore/wtf/OSRandomSource.cpp \
 497 Source/JavaScriptCore/wtf/OSRandomSource.h \
494498 Source/JavaScriptCore/wtf/OwnArrayPtr.h \
495499 Source/JavaScriptCore/wtf/OwnFastMallocPtr.h \
496500 Source/JavaScriptCore/wtf/OwnPtrCommon.h \

Source/JavaScriptCore/JavaScriptCore.gypi

374374 'wtf/chromium/ChromiumThreading.h',
375375 'wtf/chromium/MainThreadChromium.cpp',
376376 'wtf/CrossThreadRefCounted.h',
 377 'wtf/CryptographicallyRandomNumber.cpp',
 378 'wtf/CryptographicallyRandomNumber.h',
377379 'wtf/CurrentTime.cpp',
378380 'wtf/CurrentTime.h',
379381 'wtf/DateMath.cpp',

416418 'wtf/OSAllocatorPosix.cpp',
417419 'wtf/OSAllocatorWin.cpp',
418420 'wtf/OSAllocator.h',
 421 'wtf/OSRandomSource.cpp',
 422 'wtf/OSRandomSource.h',
419423 'wtf/OwnArrayPtr.h',
420424 'wtf/OwnFastMallocPtr.h',
421425 'wtf/OwnPtr.h',

Source/JavaScriptCore/JavaScriptCore.vcproj/WTF/WTF.vcproj

561561 >
562562 </File>
563563 <File
 564 RelativePath="..\..\wtf\CryptographicallyRandomNumber.cpp"
 565 >
 566 </File>
 567 <File
 568 RelativePath="..\..\wtf\CryptographicallyRandomNumber.h"
 569 >
 570 </File>
 571 <File
564572 RelativePath="..\..\wtf\CurrentTime.cpp"
565573 >
566574 </File>

753761 >
754762 </File>
755763 <File
 764 RelativePath="..\..\wtf\OSRandomSource.cpp"
 765 >
 766 </File>
 767 <File
 768 RelativePath="..\..\wtf\OSRandomSource.h"
 769 >
 770 </File>
 771 <File
756772 RelativePath="..\..\wtf\OwnArrayPtr.h"
757773 >
758774 </File>

Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj

338338 971EDEA61169E0D3005E4262 /* Terminator.h in Headers */ = {isa = PBXBuildFile; fileRef = 97F6903A1169DF7F00A6BB46 /* Terminator.h */; settings = {ATTRIBUTES = (Private, ); }; };
339339 972A485F12661E0800F4514F /* URLCharacterTypes.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 972A485D12661E0800F4514F /* URLCharacterTypes.cpp */; };
340340 972A48771266256F00F4514F /* URLEscape.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 972A48741266256F00F4514F /* URLEscape.cpp */; };
 341 97941A5713029AAB004A3447 /* OSRandomSource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 97941A3F130299DB004A3447 /* OSRandomSource.cpp */; };
 342 97941A5A13029ACC004A3447 /* OSRandomSource.h in Headers */ = {isa = PBXBuildFile; fileRef = 97941A40130299DB004A3447 /* OSRandomSource.h */; };
 343 97941A7E1302A098004A3447 /* CryptographicallyRandomNumber.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 97941A7C1302A098004A3447 /* CryptographicallyRandomNumber.cpp */; };
 344 97941A7F1302A098004A3447 /* CryptographicallyRandomNumber.h in Headers */ = {isa = PBXBuildFile; fileRef = 97941A7D1302A098004A3447 /* CryptographicallyRandomNumber.h */; };
341345 A1712B3B11C7B212007A5315 /* RegExpCache.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A1712B3A11C7B212007A5315 /* RegExpCache.cpp */; };
342346 A1712B3F11C7B228007A5315 /* RegExpCache.h in Headers */ = {isa = PBXBuildFile; fileRef = A1712B3E11C7B228007A5315 /* RegExpCache.h */; settings = {ATTRIBUTES = (Private, ); }; };
343347 A1712B4111C7B235007A5315 /* RegExpKey.h in Headers */ = {isa = PBXBuildFile; fileRef = A1712B4011C7B235007A5315 /* RegExpKey.h */; settings = {ATTRIBUTES = (Private, ); }; };

10061010 972A48741266256F00F4514F /* URLEscape.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = URLEscape.cpp; path = url/src/URLEscape.cpp; sourceTree = "<group>"; };
10071011 972A48751266256F00F4514F /* URLEscape.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = URLEscape.h; path = url/src/URLEscape.h; sourceTree = "<group>"; };
10081012 972A48761266256F00F4514F /* URLQueryCanonicalizer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = URLQueryCanonicalizer.h; path = url/src/URLQueryCanonicalizer.h; sourceTree = "<group>"; };
 1013 97941A3F130299DB004A3447 /* OSRandomSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = OSRandomSource.cpp; sourceTree = "<group>"; };
 1014 97941A40130299DB004A3447 /* OSRandomSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OSRandomSource.h; sourceTree = "<group>"; };
 1015 97941A7C1302A098004A3447 /* CryptographicallyRandomNumber.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CryptographicallyRandomNumber.cpp; sourceTree = "<group>"; };
 1016 97941A7D1302A098004A3447 /* CryptographicallyRandomNumber.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CryptographicallyRandomNumber.h; sourceTree = "<group>"; };
10091017 97F6903A1169DF7F00A6BB46 /* Terminator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Terminator.h; sourceTree = "<group>"; };
10101018 A1712B3A11C7B212007A5315 /* RegExpCache.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RegExpCache.cpp; sourceTree = "<group>"; };
10111019 A1712B3E11C7B228007A5315 /* RegExpCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RegExpCache.h; sourceTree = "<group>"; };

15371545 A7A1F7AB0F252B3C00E184E2 /* ByteArray.h */,
15381546 FDA15C1612B03028003A583A /* Complex.h */,
15391547 0BDFFAD40FC6171000D69EF4 /* CrossThreadRefCounted.h */,
 1548 97941A7C1302A098004A3447 /* CryptographicallyRandomNumber.cpp */,
 1549 97941A7D1302A098004A3447 /* CryptographicallyRandomNumber.h */,
15401550 180B9AEF0F16C569009BDBC5 /* CurrentTime.cpp */,
15411551 180B9AF00F16C569009BDBC5 /* CurrentTime.h */,
15421552 41359CF40FDD89CB00206180 /* DateMath.cpp */,

15791589 933F5CDB126922690049191E /* NullPtr.h */,
15801590 1400067612A6F7830064D123 /* OSAllocator.h */,
15811591 1400069212A6F9E10064D123 /* OSAllocatorPosix.cpp */,
 1592 97941A3F130299DB004A3447 /* OSRandomSource.cpp */,
 1593 97941A40130299DB004A3447 /* OSRandomSource.h */,
15821594 9303F5A409911A5800AD71B8 /* OwnArrayPtr.h */,
15831595 0BDFFAD10FC616EC00D69EF4 /* OwnFastMallocPtr.h */,
15841596 9303F567099118FA00AD71B8 /* OwnPtr.h */,

24442456 E49DC16D12EF295300184A1F /* SourceProviderCacheItem.h in Headers */,
24452457 14C824AD12F7C785008F35E0 /* MarkedBlock.h in Headers */,
24462458 E4D8CEFB12FC439600BC9F5A /* BloomFilter.h in Headers */,
 2459 97941A5A13029ACC004A3447 /* OSRandomSource.h in Headers */,
 2460 97941A7F1302A098004A3447 /* CryptographicallyRandomNumber.h in Headers */,
24472461 );
24482462 runOnlyForDeploymentPostprocessing = 0;
24492463 };

29132927 86704B4212DB8A8100A9FE7B /* YarrSyntaxChecker.cpp in Sources */,
29142928 E49DC16B12EF293E00184A1F /* SourceProviderCache.cpp in Sources */,
29152929 14C824AB12F7C77E008F35E0 /* MarkedBlock.cpp in Sources */,
 2930 97941A5713029AAB004A3447 /* OSRandomSource.cpp in Sources */,
 2931 97941A7E1302A098004A3447 /* CryptographicallyRandomNumber.cpp in Sources */,
29162932 );
29172933 runOnlyForDeploymentPostprocessing = 0;
29182934 };

Source/JavaScriptCore/config.h

5252// We need to define this before the first #include of stdlib.h or it won't contain rand_s.
5353#ifndef _CRT_RAND_S
5454#define _CRT_RAND_S
 55#define WTF_USE_OS_RANDOMNESS 1
5556#endif
5657#endif
5758
5859#endif
5960
 61#if OS(DARWIN) || OS(UNIX)
 62#define WTF_USE_OS_RANDOMNESS 1
 63#endif
 64
6065#if OS(FREEBSD) || OS(OPENBSD)
6166#define HAVE_PTHREAD_NP_H 1
6267#endif

Source/JavaScriptCore/wtf/CMakeLists.txt

@@SET(WTF_HEADERS
99 ByteArray.h
1010 Complex.h
1111 CrossThreadRefCounted.h
 12 CryptographicallyRandomNumber.h
1213 CurrentTime.h
1314 DateMath.h
1415 DecimalNumber.h

@@SET(WTF_HEADERS
4142 NotFound.h
4243 NullPtr.h
4344 OSAllocator.h
 45 OSRandomSource.h
4446 OwnArrayPtr.h
4547 OwnFastMallocPtr.h
4648 OwnPtr.h

@@SET(WTF_HEADERS
103105SET(WTF_SOURCES
104106 Assertions.cpp
105107 ByteArray.cpp
 108 CryptographicallyRandomNumber.cpp
106109 CurrentTime.cpp
107110 DecimalNumber.cpp
108111 FastMalloc.cpp
109112 HashTable.cpp
110113 MainThread.cpp
111114 MD5.cpp
 115 OSRandomSource.cpp
112116 RandomNumber.cpp
113117 RefCountedLeakCounter.cpp
114118 StackBounds.cpp

Source/JavaScriptCore/wtf/CryptographicallyRandomNumber.cpp

 1/*
 2 * Copyright (c) 1996, David Mazieres <dm@uun.org>
 3 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
 4 *
 5 * Permission to use, copy, modify, and distribute this software for any
 6 * purpose with or without fee is hereby granted, provided that the above
 7 * copyright notice and this permission notice appear in all copies.
 8 *
 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 16 */
 17
 18/*
 19 * Arc4 random number generator for OpenBSD.
 20 *
 21 * This code is derived from section 17.1 of Applied Cryptography,
 22 * second edition, which describes a stream cipher allegedly
 23 * compatible with RSA Labs "RC4" cipher (the actual description of
 24 * which is a trade secret). The same algorithm is used as a stream
 25 * cipher called "arcfour" in Tatu Ylonen's ssh package.
 26 *
 27 * RC4 is a registered trademark of RSA Laboratories.
 28 */
 29
 30#include "config.h"
 31#include "CryptographicallyRandomNumber.h"
 32
 33#include "MainThread.h"
 34#include "OSRandomSource.h"
 35#include "StdLibExtras.h"
 36#include "ThreadingPrimitives.h"
 37
 38namespace WTF {
 39
 40#if USE(OS_RANDOMNESS)
 41
 42namespace {
 43
 44struct ARC4Stream {
 45 uint8_t i;
 46 uint8_t j;
 47 uint8_t s[256];
 48};
 49
 50class ARC4RandomNumberGenerator {
 51public:
 52 ARC4RandomNumberGenerator();
 53
 54 uint32_t randomNumber();
 55 void randomValues(void* buffer, size_t length);
 56
 57private:
 58 inline void addRandomData(unsigned char *data, int length);
 59 void stir();
 60 void stirIfNeeded();
 61 inline uint8_t getByte();
 62 inline uint32_t getWord();
 63
 64 bool m_initialized;
 65 ARC4Stream m_stream;
 66 int m_count;
 67 Mutex m_mutex;
 68};
 69
 70
 71ARC4RandomNumberGenerator::ARC4RandomNumberGenerator()
 72 : m_initialized(false)
 73 , m_count(0)
 74{
 75 for (int n = 0; n < 256; n++)
 76 m_stream.s[n] = n;
 77 m_stream.i = 0;
 78 m_stream.j = 0;
 79}
 80
 81void ARC4RandomNumberGenerator::addRandomData(unsigned char *data, int length)
 82{
 83 m_stream.i--;
 84 for (int n = 0; n < 256; n++) {
 85 m_stream.i = (m_stream.i + 1);
 86 uint8_t si = m_stream.s[m_stream.i];
 87 m_stream.j = (m_stream.j + si + data[n % length]);
 88 m_stream.s[m_stream.i] = m_stream.s[m_stream.j];
 89 m_stream.s[m_stream.j] = si;
 90 }
 91 m_stream.j = m_stream.i;
 92}
 93
 94void ARC4RandomNumberGenerator::stir()
 95{
 96 unsigned char randomness[128];
 97 size_t length = sizeof(randomness);
 98 randomValuesFromOS(randomness, length);
 99 addRandomData(randomness, length);
 100
 101 // Discard early keystream, as per recommendations in:
 102 // http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
 103 for (int i = 0; i < 256; i++)
 104 getByte();
 105 m_count = 1600000;
 106}
 107
 108void ARC4RandomNumberGenerator::stirIfNeeded()
 109{
 110 if (m_count <= 0 || !m_initialized)
 111 stir();
 112}
 113
 114uint8_t ARC4RandomNumberGenerator::getByte()
 115{
 116 m_stream.i = (m_stream.i + 1);
 117 uint8_t si = m_stream.s[m_stream.i];
 118 m_stream.j = (m_stream.j + si);
 119 uint8_t sj = m_stream.s[m_stream.j];
 120 m_stream.s[m_stream.i] = sj;
 121 m_stream.s[m_stream.j] = si;
 122 return (m_stream.s[(si + sj) & 0xff]);
 123}
 124
 125uint32_t ARC4RandomNumberGenerator::getWord()
 126{
 127 uint32_t val;
 128 val = getByte() << 24;
 129 val |= getByte() << 16;
 130 val |= getByte() << 8;
 131 val |= getByte();
 132 return val;
 133}
 134
 135uint32_t ARC4RandomNumberGenerator::randomNumber()
 136{
 137#if !ENABLE(JSC_MULTIPLE_THREADS)
 138 MutexLocker locker(m_mutex);
 139#else
 140 ASSERT(isMainThread());
 141#endif
 142
 143 m_count -= 4;
 144 stirIfNeeded();
 145 return getWord();
 146}
 147
 148void ARC4RandomNumberGenerator::randomValues(void* buffer, size_t length)
 149{
 150#if !ENABLE(JSC_MULTIPLE_THREADS)
 151 MutexLocker locker(m_mutex);
 152#else
 153 ASSERT(isMainThread());
 154#endif
 155
 156 unsigned char* result = reinterpret_cast<unsigned char*>(buffer);
 157 stirIfNeeded();
 158 while (length--) {
 159 if (--m_count <= 0)
 160 stir();
 161 result[length] = getByte();
 162 }
 163}
 164
 165ARC4RandomNumberGenerator& sharedRandomNumberGenerator()
 166{
 167 DEFINE_STATIC_LOCAL(ARC4RandomNumberGenerator, randomNumberGenerator, ());
 168 return randomNumberGenerator;
 169}
 170
 171}
 172
 173uint32_t cryptographicallyRandomNumber()
 174{
 175 return sharedRandomNumberGenerator().randomNumber();
 176}
 177
 178void cryptographicallyRandomValues(void* buffer, size_t length)
 179{
 180 sharedRandomNumberGenerator().randomValues(buffer, length);
 181}
 182
 183#endif
 184
 185}

Source/JavaScriptCore/wtf/CryptographicallyRandomNumber.h

 1/*
 2 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
 3 *
 4 * Redistribution and use in source and binary forms, with or without
 5 * modification, are permitted provided that the following conditions
 6 * are met:
 7 * 1. Redistributions of source code must retain the above copyright
 8 * notice, this list of conditions and the following disclaimer.
 9 * 2. Redistributions in binary form must reproduce the above copyright
 10 * notice, this list of conditions and the following disclaimer in the
 11 * documentation and/or other materials provided with the distribution.
 12 *
 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 24 */
 25
 26#ifndef WTF_CryptographicallyRandomNumber_h
 27#define WTF_CryptographicallyRandomNumber_h
 28
 29#include <stdint.h>
 30
 31namespace WTF {
 32
 33#if USE(OS_RANDOMNESS)
 34uint32_t cryptographicallyRandomNumber();
 35void cryptographicallyRandomValues(void* buffer, size_t length);
 36#endif
 37
 38}
 39
 40#if USE(OS_RANDOMNESS)
 41using WTF::cryptographicallyRandomNumber;
 42using WTF::cryptographicallyRandomValues;
 43#endif
 44
 45#endif

Source/JavaScriptCore/wtf/OSRandomSource.cpp

 1/*
 2 * Copyright (C) 2011 Google Inc. All rights reserved.
 3 *
 4 * Redistribution and use in source and binary forms, with or without
 5 * modification, are permitted provided that the following conditions
 6 * are met:
 7 * 1. Redistributions of source code must retain the above copyright
 8 * notice, this list of conditions and the following disclaimer.
 9 * 2. Redistributions in binary form must reproduce the above copyright
 10 * notice, this list of conditions and the following disclaimer in the
 11 * documentation and/or other materials provided with the distribution.
 12 *
 13 * THIS SOFTWARE IS PROVIDED BY GOOGLE, INC. ``AS IS'' AND ANY
 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 24 */
 25
 26#include "config.h"
 27#include "OSRandomSource.h"
 28
 29#include <stdint.h>
 30#include <stdlib.h>
 31
 32#if OS(UNIX)
 33#include <fcntl.h>
 34#include <unistd.h>
 35#endif
 36
 37namespace WTF {
 38
 39#if USE(OS_RANDOMNESS)
 40void randomValuesFromOS(unsigned char* buffer, size_t length)
 41{
 42#if COMPILER(MSVC) && defined(_CRT_RAND_S)
 43 for (size_t i = 0; i < length; i++) {
 44 uint32_t bits;
 45 rand_s(&bits);
 46 buffer[i] = static_cast<unsigned char>(bits);
 47 }
 48#elif OS(DARWIN)
 49 for (size_t i = 0; i < length; i++) {
 50 uint32_t bits;
 51 bits = arc4random();
 52 buffer[i] = static_cast<unsigned char>(bits);
 53 }
 54#elif OS(UNIX)
 55 int fd = open("/dev/urandom", O_RDONLY, 0);
 56 if (fd < 0)
 57 CRASH(); // We need /dev/urandom for this API to work...
 58
 59 read(fd, buffer, length);
 60 close(fd);
 61#else
 62 #error This configuration doesn't have a strong source of randomness.
 63 // WARNING: When adding new sources of OS randomness, the randomness must
 64 // be of cryptographic quality!
 65#endif
 66}
 67#endif
 68
 69}

Source/JavaScriptCore/wtf/OSRandomSource.h

 1/*
 2 * Copyright (C) Google, Inc. All rights reserved.
 3 *
 4 * Redistribution and use in source and binary forms, with or without
 5 * modification, are permitted provided that the following conditions
 6 * are met:
 7 * 1. Redistributions of source code must retain the above copyright
 8 * notice, this list of conditions and the following disclaimer.
 9 * 2. Redistributions in binary form must reproduce the above copyright
 10 * notice, this list of conditions and the following disclaimer in the
 11 * documentation and/or other materials provided with the distribution.
 12 *
 13 * THIS SOFTWARE IS PROVIDED BY GOOGLE, INC. ``AS IS'' AND ANY
 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 24 */
 25
 26#ifndef WTF_OSRandomSource_h
 27#define WTF_OSRandomSource_h
 28
 29namespace WTF {
 30
 31#if USE(OS_RANDOMNESS)
 32// This function attempts to fill buffer with randomness from the operating
 33// system. If insufficient randomness is available, the buffer will be
 34// partially filled. Rather than calling this function directly, consider
 35// calling cryptographicallyRandomNumber or cryptographicallyRandomValues.
 36void randomValuesFromOS(unsigned char* buffer, size_t length);
 37#endif
 38
 39}
 40
 41#endif

Source/JavaScriptCore/wtf/wtf.pri

33SOURCES += \
44 wtf/Assertions.cpp \
55 wtf/ByteArray.cpp \
 6 wtf/CryptographicallyRandomNumber.cpp \
67 wtf/CurrentTime.cpp \
78 wtf/DateMath.cpp \
89 wtf/dtoa.cpp \

@@SOURCES += \
1415 wtf/MD5.cpp \
1516 wtf/MainThread.cpp \
1617 wtf/NullPtr.cpp \
 18 wtf/OSRandomSource.cpp \
1719 wtf/qt/MainThreadQt.cpp \
1820 wtf/qt/StringQt.cpp \
1921 wtf/qt/ThreadingQt.cpp \