Added PRNG code
This commit is contained in:
@ -44,6 +44,7 @@ OBJS = $(OBJDIR)/bigint.o \
|
|||||||
$(OBJDIR)/logger.o \
|
$(OBJDIR)/logger.o \
|
||||||
$(OBJDIR)/main.o \
|
$(OBJDIR)/main.o \
|
||||||
$(OBJDIR)/peers.o \
|
$(OBJDIR)/peers.o \
|
||||||
|
$(OBJDIR)/random.o \
|
||||||
$(OBJDIR)/rpc.o \
|
$(OBJDIR)/rpc.o \
|
||||||
$(OBJDIR)/sam.o \
|
$(OBJDIR)/sam.o \
|
||||||
$(OBJDIR)/sha1.o
|
$(OBJDIR)/sha1.o
|
||||||
|
@ -31,9 +31,8 @@
|
|||||||
#include "platform.hpp"
|
#include "platform.hpp"
|
||||||
#include "logger.hpp"
|
#include "logger.hpp"
|
||||||
|
|
||||||
Logger::Logger(const string& file)
|
Logger::Logger(const string& file): file(file)
|
||||||
{
|
{
|
||||||
this->file = file;
|
|
||||||
loglevel = priority = debug;
|
loglevel = priority = debug;
|
||||||
logf.open(file.c_str(), ios::app);
|
logf.open(file.c_str(), ios::app);
|
||||||
if (!logf) {
|
if (!logf) {
|
||||||
|
@ -31,8 +31,9 @@
|
|||||||
#include "platform.hpp"
|
#include "platform.hpp"
|
||||||
#include "main.hpp"
|
#include "main.hpp"
|
||||||
|
|
||||||
Logger logger(LOG_FILE);
|
Logger logger(LOG_FILE); // Logging mechanism
|
||||||
Sam *sam;
|
Random prng; // Random number generator
|
||||||
|
Sam *sam; // SAM connection
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
@ -52,7 +53,7 @@ int main(int argc, char* argv[])
|
|||||||
cerr << "SAM error: " << x.what() << '\n';
|
cerr << "SAM error: " << x.what() << '\n';
|
||||||
if (x.code() == SAM_SOCKET_ERROR) {
|
if (x.code() == SAM_SOCKET_ERROR) {
|
||||||
LERROR << "Check whether you have specified the correct SAM host " \
|
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 " \
|
cerr << "Check whether you have specified the correct SAM host " \
|
||||||
"and port number, and that\nI2P is running.\n";
|
"and port number, and that\nI2P is running.\n";
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ using namespace std;
|
|||||||
/*
|
/*
|
||||||
* Local includes
|
* Local includes
|
||||||
*/
|
*/
|
||||||
#include "logger.hpp"
|
#include "logger.hpp" // Logger
|
||||||
#include "sam_error.hpp" // for sam.hpp
|
#include "sam_error.hpp" // for sam.hpp
|
||||||
#include "bigint.hpp" // for sha1.hpp
|
#include "bigint.hpp" // for sha1.hpp
|
||||||
#include "sha1.hpp" // for peers.hpp
|
#include "sha1.hpp" // for peers.hpp
|
||||||
@ -84,11 +84,13 @@ using namespace std;
|
|||||||
#include "near_peer.hpp" // for peers.hpp
|
#include "near_peer.hpp" // for peers.hpp
|
||||||
#include "peers.hpp" // for sam.hpp
|
#include "peers.hpp" // for sam.hpp
|
||||||
#include "sam.hpp" // SAM
|
#include "sam.hpp" // SAM
|
||||||
|
#include "random.hpp" // Random
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Global variables
|
* Global variables
|
||||||
*/
|
*/
|
||||||
extern Logger logger;
|
extern Logger logger; // Logging mechanism
|
||||||
extern Sam *sam;
|
extern Random prng; // Random number generator
|
||||||
|
extern Sam *sam; // Sam connection
|
||||||
|
|
||||||
#endif // PLATFORM_HPP
|
#endif // PLATFORM_HPP
|
||||||
|
65
apps/enclave/src/random.cpp
Normal file
65
apps/enclave/src/random.cpp
Normal file
@ -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);
|
||||||
|
}
|
45
apps/enclave/src/random.hpp
Normal file
45
apps/enclave/src/random.hpp
Normal file
@ -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
|
@ -48,6 +48,7 @@ class Rpc {
|
|||||||
|
|
||||||
Rpc(Peer* peer)
|
Rpc(Peer* peer)
|
||||||
: peer(peer) {};
|
: peer(peer) {};
|
||||||
|
|
||||||
void find_peers(const Sha1& sha1);
|
void find_peers(const Sha1& sha1);
|
||||||
void parse(const void* data, size_t size);
|
void parse(const void* data, size_t size);
|
||||||
void ping(void);
|
void ping(void);
|
||||||
|
@ -36,6 +36,7 @@ class Sam {
|
|||||||
Sam(const char* samhost, uint16_t samport, const char* destname,
|
Sam(const char* samhost, uint16_t samport, const char* destname,
|
||||||
uint_t tunneldepth);
|
uint_t tunneldepth);
|
||||||
~Sam(void);
|
~Sam(void);
|
||||||
|
|
||||||
const string& get_my_dest(void) const { return my_dest; }
|
const string& get_my_dest(void) const { return my_dest; }
|
||||||
const Sha1& get_my_sha1(void) const { return my_sha1; }
|
const Sha1& get_my_sha1(void) const { return my_sha1; }
|
||||||
void naming_lookup(const string& name = "ME") const;
|
void naming_lookup(const string& name = "ME") const;
|
||||||
|
@ -35,6 +35,7 @@ class Sam_error {
|
|||||||
public:
|
public:
|
||||||
Sam_error(samerr_t error)
|
Sam_error(samerr_t error)
|
||||||
: errcode(error) {}
|
: errcode(error) {}
|
||||||
|
|
||||||
samerr_t code(void) const { return errcode; }
|
samerr_t code(void) const { return errcode; }
|
||||||
const char* what(void) const { return sam_strerror(errcode); }
|
const char* what(void) const { return sam_strerror(errcode); }
|
||||||
|
|
||||||
|
@ -38,6 +38,7 @@ class Sha1 {
|
|||||||
Sha1(void);
|
Sha1(void);
|
||||||
Sha1(const string& data);
|
Sha1(const string& data);
|
||||||
Sha1(const uchar_t binary[SHA1BIN_LEN]);
|
Sha1(const uchar_t binary[SHA1BIN_LEN]);
|
||||||
|
|
||||||
const string& b64hash(void) const { return b64hashed; }
|
const string& b64hash(void) const { return b64hashed; }
|
||||||
const uchar_t* binhash(void) const { return binhashed; }
|
const uchar_t* binhash(void) const { return binhashed; }
|
||||||
bool operator<(const Sha1& rhs) const;
|
bool operator<(const Sha1& rhs) const;
|
||||||
|
Reference in New Issue
Block a user