Split stuff from util to PlatformMacros and RandomNumbers

This commit is contained in:
Vinnie Falco
2013-05-26 10:46:53 -07:00
parent b0e56d170b
commit b40f11288b
9 changed files with 166 additions and 45 deletions

View File

@@ -32,6 +32,15 @@
#include <boost/asio.hpp> // for stupid parseIpPort #include <boost/asio.hpp> // for stupid parseIpPort
#include <boost/regex.hpp> #include <boost/regex.hpp>
// VFALCO: TODO, Replace OpenSSL randomness with a dependency-free implementation
// Perhaps Schneier's Yarrow or a variant. Abstract the collection of
// entropy and provide OS-specific implementation. We can re-use the
// BearShare source code for this.
//
// Add Random number generation to the new VFLib
//
#include <openssl/rand.h> // Because of ripple_RandomNumbers.cpp
// VFALCO: TODO, fix these warnings! // VFALCO: TODO, fix these warnings!
#ifdef _MSC_VER #ifdef _MSC_VER
//#pragma warning (push) // Causes spurious C4503 "decorated name exceeds maximum length" //#pragma warning (push) // Causes spurious C4503 "decorated name exceeds maximum length"
@@ -52,6 +61,8 @@
#include "memory/ripple_ByteOrder.cpp" #include "memory/ripple_ByteOrder.cpp"
#include "memory/ripple_StringUtilities.cpp" #include "memory/ripple_StringUtilities.cpp"
#include "system/ripple_RandomNumbers.cpp"
#ifdef _MSC_VER #ifdef _MSC_VER
//#pragma warning (pop) //#pragma warning (pop)
#endif #endif

View File

@@ -101,4 +101,7 @@ namespace boost {
#include "events/ripple_UptimeTimer.h" #include "events/ripple_UptimeTimer.h"
#include "system/ripple_PlatformMacros.h"
#include "system/ripple_RandomNumbers.h"
#endif #endif

View File

@@ -0,0 +1,52 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_PLATFORMMACROS_H
#define RIPPLE_PLATFORMMACROS_H
// VFALCO: TODO Clean this up
#if (!defined(FORCE_NO_C11X) && (__cplusplus > 201100L)) || defined(FORCE_C11X)
#define C11X
#include <functional>
#define UPTR_T std::unique_ptr
#define MOVE_P(p) std::move(p)
#define BIND_TYPE std::bind
#define FUNCTION_TYPE std::function
#define P_1 std::placeholders::_1
#define P_2 std::placeholders::_2
#define P_3 std::placeholders::_3
#define P_4 std::placeholders::_4
#else
#include <boost/bind.hpp>
#include <boost/function.hpp>
#define UPTR_T std::auto_ptr
#define MOVE_P(p) (p)
#define BIND_TYPE boost::bind
#define FUNCTION_TYPE boost::function
#define P_1 _1
#define P_2 _2
#define P_3 _3
#define P_4 _4
#endif
#endif

View File

@@ -0,0 +1,30 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
void getRand(unsigned char *buf, int num)
{
#ifdef PURIFY
memset(buf, 0, num);
#endif
if (RAND_bytes(buf, num) != 1)
{
assert(false);
throw std::runtime_error("Entropy pool not seeded");
}
}

View File

@@ -0,0 +1,46 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_RANDOMNUMBERS_H
#define RIPPLE_RANDOMNUMBERS_H
// Cryptographically secure random number source
// VFALCO: TODO Clean this up, rename stuff
// Seriously...wtf...rename "num" to bytes, or make it work
// using a template so the destination can be a vector of objects.
//
// VFALCO: Should accept void* not unsigned char*
//
extern void getRand (unsigned char *buf, int num);
inline static void getRand (char *buf, int num)
{
return getRand (reinterpret_cast<unsigned char *>(buf), num);
}
// VFALCO: TODO Clean this
// "num" is really bytes this should just be called getRandomBytes()
// This function is unnecessary!
//
inline static void getRand (void *buf, int num)
{
return getRand (reinterpret_cast<unsigned char *>(buf), num);
}
#endif

View File

@@ -210,6 +210,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile> </ClCompile>
<ClCompile Include="modules\ripple_basics\ripple_basics.cpp" /> <ClCompile Include="modules\ripple_basics\ripple_basics.cpp" />
<ClCompile Include="modules\ripple_basics\system\ripple_RandomNumbers.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="modules\ripple_client\ripple_client.cpp" /> <ClCompile Include="modules\ripple_client\ripple_client.cpp" />
<ClCompile Include="modules\ripple_db\ripple_db.cpp" /> <ClCompile Include="modules\ripple_db\ripple_db.cpp" />
<ClCompile Include="modules\ripple_json\json\json_reader.cpp"> <ClCompile Include="modules\ripple_json\json\json_reader.cpp">
@@ -1196,6 +1202,8 @@
<ClInclude Include="modules\ripple_basics\memory\ripple_ByteOrder.h" /> <ClInclude Include="modules\ripple_basics\memory\ripple_ByteOrder.h" />
<ClInclude Include="modules\ripple_basics\memory\ripple_StringUtilities.h" /> <ClInclude Include="modules\ripple_basics\memory\ripple_StringUtilities.h" />
<ClInclude Include="modules\ripple_basics\ripple_basics.h" /> <ClInclude Include="modules\ripple_basics\ripple_basics.h" />
<ClInclude Include="modules\ripple_basics\system\ripple_PlatformMacros.h" />
<ClInclude Include="modules\ripple_basics\system\ripple_RandomNumbers.h" />
<ClInclude Include="modules\ripple_basics\types\ripple_IntegerTypes.h" /> <ClInclude Include="modules\ripple_basics\types\ripple_IntegerTypes.h" />
<ClInclude Include="modules\ripple_client\ripple_client.h" /> <ClInclude Include="modules\ripple_client\ripple_client.h" />
<ClInclude Include="modules\ripple_db\ripple_db.h" /> <ClInclude Include="modules\ripple_db\ripple_db.h" />

View File

@@ -136,6 +136,9 @@
<Filter Include="1. Modules\ripple_basics\memory"> <Filter Include="1. Modules\ripple_basics\memory">
<UniqueIdentifier>{49faa808-02f3-44ba-a17c-43dce213672e}</UniqueIdentifier> <UniqueIdentifier>{49faa808-02f3-44ba-a17c-43dce213672e}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="1. Modules\ripple_basics\system">
<UniqueIdentifier>{c4b8f568-a8ff-40a6-9eae-1d1f41d58e7a}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="src\cpp\database\sqlite3.c"> <ClCompile Include="src\cpp\database\sqlite3.c">
@@ -762,6 +765,9 @@
<ClCompile Include="modules\ripple_basics\memory\ripple_StringUtilities.cpp"> <ClCompile Include="modules\ripple_basics\memory\ripple_StringUtilities.cpp">
<Filter>1. Modules\ripple_basics\memory</Filter> <Filter>1. Modules\ripple_basics\memory</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="modules\ripple_basics\system\ripple_RandomNumbers.cpp">
<Filter>1. Modules\ripple_basics\system</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="database\sqlite3ext.h"> <ClInclude Include="database\sqlite3ext.h">
@@ -1415,6 +1421,12 @@
<ClInclude Include="modules\ripple_basics\memory\ripple_StringUtilities.h"> <ClInclude Include="modules\ripple_basics\memory\ripple_StringUtilities.h">
<Filter>1. Modules\ripple_basics\memory</Filter> <Filter>1. Modules\ripple_basics\memory</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="modules\ripple_basics\system\ripple_PlatformMacros.h">
<Filter>1. Modules\ripple_basics\system</Filter>
</ClInclude>
<ClInclude Include="modules\ripple_basics\system\ripple_RandomNumbers.h">
<Filter>1. Modules\ripple_basics\system</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="SConstruct" /> <None Include="SConstruct" />

View File

@@ -23,23 +23,11 @@
//#include <boost/regex.hpp> //#include <boost/regex.hpp>
//#include <boost/test/unit_test.hpp> //#include <boost/test/unit_test.hpp>
#include <openssl/rand.h> //#include <openssl/rand.h>
#include "utils.h" #include "utils.h"
#include "uint256.h" #include "uint256.h"
void getRand(unsigned char *buf, int num)
{
#ifdef PURIFY
memset(buf, 0, num);
#endif
if (RAND_bytes(buf, num) != 1)
{
assert(false);
throw std::runtime_error("Entropy pool not seeded");
}
}
// //
// Time support // Time support
// We have our own epoch. // We have our own epoch.

View File

@@ -26,10 +26,8 @@ uint64_t utFromSeconds(int iSeconds);
DH* DH_der_load(const std::string& strDer); DH* DH_der_load(const std::string& strDer);
std::string DH_der_gen(int iKeyLength); std::string DH_der_gen(int iKeyLength);
void getRand(unsigned char *buf, int num); // VFALCO: NOTE, these three are unused.
inline static void getRand(char *buf, int num) { return getRand(reinterpret_cast<unsigned char *>(buf), num); } /*
inline static void getRand(void *buf, int num) { return getRand(reinterpret_cast<unsigned char *>(buf), num); }
template<typename T> T range_check(const T& value, const T& minimum, const T& maximum) template<typename T> T range_check(const T& value, const T& minimum, const T& maximum)
{ {
if ((value < minimum) || (value > maximum)) if ((value < minimum) || (value > maximum))
@@ -50,6 +48,7 @@ template<typename T> T range_check_max(const T& value, const T& maximum)
throw std::runtime_error("Value out of range"); throw std::runtime_error("Value out of range");
return value; return value;
} }
*/
template<typename T, typename U> T range_check_cast(const U& value, const T& minimum, const T& maximum) template<typename T, typename U> T range_check_cast(const U& value, const T& minimum, const T& maximum)
{ {
@@ -64,34 +63,6 @@ extern bool HaveSustain();
extern std::string StopSustain(); extern std::string StopSustain();
extern std::string DoSustain(); extern std::string DoSustain();
#if (!defined(FORCE_NO_C11X) && (__cplusplus > 201100L)) || defined(FORCE_C11X)
#define C11X
#include <functional>
#define UPTR_T std::unique_ptr
#define MOVE_P(p) std::move(p)
#define BIND_TYPE std::bind
#define FUNCTION_TYPE std::function
#define P_1 std::placeholders::_1
#define P_2 std::placeholders::_2
#define P_3 std::placeholders::_3
#define P_4 std::placeholders::_4
#else
#include <boost/bind.hpp>
#include <boost/function.hpp>
#define UPTR_T std::auto_ptr
#define MOVE_P(p) (p)
#define BIND_TYPE boost::bind
#define FUNCTION_TYPE boost::function
#define P_1 _1
#define P_2 _2
#define P_3 _3
#define P_4 _4
#endif
#endif #endif
// vim:ts=4 // vim:ts=4