diff --git a/apps/enclave/Makefile b/apps/enclave/Makefile index ee91d6348b5621e6b086282992c664f63bc9b616..499868fcc1ec95347437a29e01d550f21e6984d1 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 ba0a10c3d59f3360a09ffa6e1d33a016cb661c9c..9ef448a35f913efbfe7fa2676ac6ba9be73fc5c3 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 1791c6e8dbc7e7f32eddb740513a4b7761926079..07c1b2c19a57bffe8236d8a4a5792d1870fcc7c3 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 3a0a843d570c8749feda96db9c5fd672a10e8818..23a9ee0f85aca7e0326376ea0aca865fb5941a2e 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 0000000000000000000000000000000000000000..1652d050ee24c7b4af18f45bcc1c664110176622 --- /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 0000000000000000000000000000000000000000..279efa065ef7a663a7b14f594de2d4ef72f7ab36 --- /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 6ed019a9bd15439e87355a730460917756448a71..ba069291325f2b649eb86ea7ade7e2ec85607a98 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 57eb8d6faf17a815018c98e16d040b878a34d1d7..a669fb38d8644de1bc5d9067a0f61d3c55481fed 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 700d94737b18e42b3f9aac62cb3cef6022b754cb..af596ad9b9c96b8617ac27edb3dfefebe762f48e 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 c997d1b8f698837e0237796669f70483f33cccdb..32076f9410277c9cc8294b791056e97036b8933a 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;