From b40f11288b98d23c273e666a5e3942507a042793 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sun, 26 May 2013 10:46:53 -0700 Subject: [PATCH] Split stuff from util to PlatformMacros and RandomNumbers --- modules/ripple_basics/ripple_basics.cpp | 11 ++++ modules/ripple_basics/ripple_basics.h | 3 ++ .../system/ripple_PlatformMacros.h | 52 +++++++++++++++++++ .../system/ripple_RandomNumbers.cpp | 30 +++++++++++ .../system/ripple_RandomNumbers.h | 46 ++++++++++++++++ newcoin.vcxproj | 8 +++ newcoin.vcxproj.filters | 12 +++++ src/cpp/ripple/utils.cpp | 14 +---- src/cpp/ripple/utils.h | 35 ++----------- 9 files changed, 166 insertions(+), 45 deletions(-) create mode 100644 modules/ripple_basics/system/ripple_PlatformMacros.h create mode 100644 modules/ripple_basics/system/ripple_RandomNumbers.cpp create mode 100644 modules/ripple_basics/system/ripple_RandomNumbers.h diff --git a/modules/ripple_basics/ripple_basics.cpp b/modules/ripple_basics/ripple_basics.cpp index b0706c7d43..cdd6d5552a 100644 --- a/modules/ripple_basics/ripple_basics.cpp +++ b/modules/ripple_basics/ripple_basics.cpp @@ -32,6 +32,15 @@ #include // for stupid parseIpPort #include +// 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 // Because of ripple_RandomNumbers.cpp + // VFALCO: TODO, fix these warnings! #ifdef _MSC_VER //#pragma warning (push) // Causes spurious C4503 "decorated name exceeds maximum length" @@ -52,6 +61,8 @@ #include "memory/ripple_ByteOrder.cpp" #include "memory/ripple_StringUtilities.cpp" +#include "system/ripple_RandomNumbers.cpp" + #ifdef _MSC_VER //#pragma warning (pop) #endif diff --git a/modules/ripple_basics/ripple_basics.h b/modules/ripple_basics/ripple_basics.h index a93f423589..949e09cb15 100644 --- a/modules/ripple_basics/ripple_basics.h +++ b/modules/ripple_basics/ripple_basics.h @@ -101,4 +101,7 @@ namespace boost { #include "events/ripple_UptimeTimer.h" +#include "system/ripple_PlatformMacros.h" +#include "system/ripple_RandomNumbers.h" + #endif diff --git a/modules/ripple_basics/system/ripple_PlatformMacros.h b/modules/ripple_basics/system/ripple_PlatformMacros.h new file mode 100644 index 0000000000..4bd6101a96 --- /dev/null +++ b/modules/ripple_basics/system/ripple_PlatformMacros.h @@ -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 +#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 +#include +#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 diff --git a/modules/ripple_basics/system/ripple_RandomNumbers.cpp b/modules/ripple_basics/system/ripple_RandomNumbers.cpp new file mode 100644 index 0000000000..2699b8ccc9 --- /dev/null +++ b/modules/ripple_basics/system/ripple_RandomNumbers.cpp @@ -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"); + } +} + diff --git a/modules/ripple_basics/system/ripple_RandomNumbers.h b/modules/ripple_basics/system/ripple_RandomNumbers.h new file mode 100644 index 0000000000..058b642e9c --- /dev/null +++ b/modules/ripple_basics/system/ripple_RandomNumbers.h @@ -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(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(buf), num); +} + +#endif diff --git a/newcoin.vcxproj b/newcoin.vcxproj index eccd7ece33..5d36c45ce4 100644 --- a/newcoin.vcxproj +++ b/newcoin.vcxproj @@ -210,6 +210,12 @@ true + + true + true + true + true + @@ -1196,6 +1202,8 @@ + + diff --git a/newcoin.vcxproj.filters b/newcoin.vcxproj.filters index 4cf26a8e8d..2e09adc5c8 100644 --- a/newcoin.vcxproj.filters +++ b/newcoin.vcxproj.filters @@ -136,6 +136,9 @@ {49faa808-02f3-44ba-a17c-43dce213672e} + + {c4b8f568-a8ff-40a6-9eae-1d1f41d58e7a} + @@ -762,6 +765,9 @@ 1. Modules\ripple_basics\memory + + 1. Modules\ripple_basics\system + @@ -1415,6 +1421,12 @@ 1. Modules\ripple_basics\memory + + 1. Modules\ripple_basics\system + + + 1. Modules\ripple_basics\system + diff --git a/src/cpp/ripple/utils.cpp b/src/cpp/ripple/utils.cpp index fc6737f597..71abdb662d 100644 --- a/src/cpp/ripple/utils.cpp +++ b/src/cpp/ripple/utils.cpp @@ -23,23 +23,11 @@ //#include //#include -#include +//#include #include "utils.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 // We have our own epoch. diff --git a/src/cpp/ripple/utils.h b/src/cpp/ripple/utils.h index ed97374c23..0aeea1f76c 100644 --- a/src/cpp/ripple/utils.h +++ b/src/cpp/ripple/utils.h @@ -26,10 +26,8 @@ uint64_t utFromSeconds(int iSeconds); DH* DH_der_load(const std::string& strDer); std::string DH_der_gen(int iKeyLength); -void getRand(unsigned char *buf, int num); -inline static void getRand(char *buf, int num) { return getRand(reinterpret_cast(buf), num); } -inline static void getRand(void *buf, int num) { return getRand(reinterpret_cast(buf), num); } - +// VFALCO: NOTE, these three are unused. +/* template T range_check(const T& value, const T& minimum, const T& maximum) { if ((value < minimum) || (value > maximum)) @@ -50,6 +48,7 @@ template T range_check_max(const T& value, const T& maximum) throw std::runtime_error("Value out of range"); return value; } +*/ template 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 DoSustain(); -#if (!defined(FORCE_NO_C11X) && (__cplusplus > 201100L)) || defined(FORCE_C11X) - -#define C11X -#include -#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 -#include -#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 // vim:ts=4