From 980c0aa1d78fb8e78def4d1b1c721817fd9eea7d Mon Sep 17 00:00:00 2001
From: mpc <mpc>
Date: Wed, 23 Jun 2004 11:56:53 +0000
Subject: [PATCH] Added PRNG code

---
 apps/enclave/Makefile          |  1 +
 apps/enclave/src/logger.cpp    |  3 +-
 apps/enclave/src/main.cpp      |  9 ++---
 apps/enclave/src/platform.hpp  |  8 +++--
 apps/enclave/src/random.cpp    | 65 ++++++++++++++++++++++++++++++++++
 apps/enclave/src/random.hpp    | 45 +++++++++++++++++++++++
 apps/enclave/src/rpc.hpp       |  1 +
 apps/enclave/src/sam.hpp       |  1 +
 apps/enclave/src/sam_error.hpp |  1 +
 apps/enclave/src/sha1.hpp      |  1 +
 10 files changed, 126 insertions(+), 9 deletions(-)
 create mode 100644 apps/enclave/src/random.cpp
 create mode 100644 apps/enclave/src/random.hpp

diff --git a/apps/enclave/Makefile b/apps/enclave/Makefile
index ee91d6348b..499868fcc1 100644
--- a/apps/enclave/Makefile
+++ b/apps/enclave/Makefile
@@ -44,6 +44,7 @@ OBJS =	$(OBJDIR)/bigint.o \
 		$(OBJDIR)/logger.o \
 		$(OBJDIR)/main.o \
 		$(OBJDIR)/peers.o \
+		$(OBJDIR)/random.o \
 		$(OBJDIR)/rpc.o \
 		$(OBJDIR)/sam.o \
 		$(OBJDIR)/sha1.o
diff --git a/apps/enclave/src/logger.cpp b/apps/enclave/src/logger.cpp
index ba0a10c3d5..9ef448a35f 100644
--- a/apps/enclave/src/logger.cpp
+++ b/apps/enclave/src/logger.cpp
@@ -31,9 +31,8 @@
 #include "platform.hpp"
 #include "logger.hpp"
 
-Logger::Logger(const string& file)
+Logger::Logger(const string& file): file(file)
 {
-	this->file = file;
 	loglevel = priority = debug;
 	logf.open(file.c_str(), ios::app);
 	if (!logf) {
diff --git a/apps/enclave/src/main.cpp b/apps/enclave/src/main.cpp
index 1791c6e8db..07c1b2c19a 100644
--- a/apps/enclave/src/main.cpp
+++ b/apps/enclave/src/main.cpp
@@ -31,8 +31,9 @@
 #include "platform.hpp"
 #include "main.hpp"
 
-Logger logger(LOG_FILE);
-Sam *sam;
+Logger logger(LOG_FILE);  // Logging mechanism
+Random prng;  // Random number generator
+Sam *sam;  // SAM connection
 
 int main(int argc, char* argv[])
 {
@@ -52,7 +53,7 @@ int main(int argc, char* argv[])
 		cerr << "SAM error: " << x.what() << '\n';
 		if (x.code() == SAM_SOCKET_ERROR) {
 			LERROR << "Check whether you have specified the correct SAM host " \
-				"and port number, and that\nI2P is running.\n";
+				"and port number, and that I2P is running.\n";
 			cerr << "Check whether you have specified the correct SAM host " \
 				"and port number, and that\nI2P is running.\n";
 		}
@@ -63,7 +64,7 @@ int main(int argc, char* argv[])
 		sam->read_buffer();  // wait until we get our own dest back from lookup
 
 	sam->peers->advertise_self();
-	
+
 	while (true)
 		sam->read_buffer();
 
diff --git a/apps/enclave/src/platform.hpp b/apps/enclave/src/platform.hpp
index 3a0a843d57..23a9ee0f85 100644
--- a/apps/enclave/src/platform.hpp
+++ b/apps/enclave/src/platform.hpp
@@ -76,7 +76,7 @@ using namespace std;
 /*
  * Local includes
  */
-#include "logger.hpp"
+#include "logger.hpp"  // Logger
 #include "sam_error.hpp"  // for sam.hpp
 #include "bigint.hpp"  // for sha1.hpp
 #include "sha1.hpp"  // for peers.hpp
@@ -84,11 +84,13 @@ using namespace std;
 #include "near_peer.hpp"  // for peers.hpp
 #include "peers.hpp" // for sam.hpp
 #include "sam.hpp"  // SAM
+#include "random.hpp"  // Random
 
 /*
  * Global variables
  */
-extern Logger logger;
-extern Sam *sam;
+extern Logger logger;  // Logging mechanism
+extern Random prng;  // Random number generator
+extern Sam *sam;  // Sam connection
 
 #endif  // PLATFORM_HPP
diff --git a/apps/enclave/src/random.cpp b/apps/enclave/src/random.cpp
new file mode 100644
index 0000000000..1652d050ee
--- /dev/null
+++ b/apps/enclave/src/random.cpp
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2004, Matthew P. Cashdollar <mpc@innographx.com>
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * 
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *     * Neither the name of the author nor the names of any contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "platform.hpp"
+#include "random.hpp"
+
+/*
+ * Prepares the Yarrow PRNG for use
+ */
+Random::Random(void)
+{
+	LINFO << "Initalising PRNG\n";// it could take a bit of time on some systems
+
+	int rc = yarrow_start(&prng);
+	assert(rc == CRYPT_OK);
+
+	uchar_t entropy[ENTROPY_SIZE];
+	size_t sz = rng_get_bytes(entropy, ENTROPY_SIZE, NULL);
+	assert(sz == ENTROPY_SIZE);
+
+	rc = yarrow_add_entropy(entropy, ENTROPY_SIZE, &prng);
+	assert(rc == CRYPT_OK);
+
+	rc = yarrow_ready(&prng);
+	assert(rc == CRYPT_OK);
+}
+
+/*
+ * Gets `size' random bytes from the PRNG
+ *
+ * random - space to fill with random bytes
+ * size - size of `random'
+ */
+void Random::get_bytes(uchar_t* random, size_t size)
+{
+	size_t sz = yarrow_read(random, size, &prng);
+	assert(sz == size);
+}
diff --git a/apps/enclave/src/random.hpp b/apps/enclave/src/random.hpp
new file mode 100644
index 0000000000..279efa065e
--- /dev/null
+++ b/apps/enclave/src/random.hpp
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2004, Matthew P. Cashdollar <mpc@innographx.com>
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * 
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *     * Neither the name of the author nor the names of any contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef RANDOM_HPP
+#define RANDOM_HPP
+
+class Random {
+	public:
+		Random(void);
+
+		void get_bytes(uchar_t* random, size_t size);
+
+	private:
+		static const size_t ENTROPY_SIZE = 32;
+		prng_state prng;
+};
+
+#endif  // RNG_HPP
diff --git a/apps/enclave/src/rpc.hpp b/apps/enclave/src/rpc.hpp
index 6ed019a9bd..ba06929132 100644
--- a/apps/enclave/src/rpc.hpp
+++ b/apps/enclave/src/rpc.hpp
@@ -48,6 +48,7 @@ class Rpc {
 
 		Rpc(Peer* peer)
 			: peer(peer) {};
+
 		void find_peers(const Sha1& sha1);
 		void parse(const void* data, size_t size);
 		void ping(void);
diff --git a/apps/enclave/src/sam.hpp b/apps/enclave/src/sam.hpp
index 57eb8d6faf..a669fb38d8 100644
--- a/apps/enclave/src/sam.hpp
+++ b/apps/enclave/src/sam.hpp
@@ -36,6 +36,7 @@ class Sam {
 		Sam(const char* samhost, uint16_t samport, const char* destname,
 			uint_t tunneldepth);
 		~Sam(void);
+
 		const string& get_my_dest(void) const { return my_dest; }
 		const Sha1& get_my_sha1(void) const { return my_sha1; }
 		void naming_lookup(const string& name = "ME") const;
diff --git a/apps/enclave/src/sam_error.hpp b/apps/enclave/src/sam_error.hpp
index 700d94737b..af596ad9b9 100644
--- a/apps/enclave/src/sam_error.hpp
+++ b/apps/enclave/src/sam_error.hpp
@@ -35,6 +35,7 @@ class Sam_error {
 	public:
 		Sam_error(samerr_t error)
 			: errcode(error) {}
+
 		samerr_t code(void) const { return errcode; }
 		const char* what(void) const { return sam_strerror(errcode); }
 
diff --git a/apps/enclave/src/sha1.hpp b/apps/enclave/src/sha1.hpp
index c997d1b8f6..32076f9410 100644
--- a/apps/enclave/src/sha1.hpp
+++ b/apps/enclave/src/sha1.hpp
@@ -38,6 +38,7 @@ class Sha1 {
 		Sha1(void);
 		Sha1(const string& data);
 		Sha1(const uchar_t binary[SHA1BIN_LEN]);
+
 		const string& b64hash(void) const { return b64hashed; }
 		const uchar_t* binhash(void) const { return binhashed; }
 		bool operator<(const Sha1& rhs) const;
-- 
GitLab