Replace custom random with llarp/sodium random

pull/1576/head
Jason Rhinelander 3 years ago committed by Jeff Becker
parent aa0f54fa07
commit 5d897781ef
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -32,7 +32,7 @@ namespace llarp::quic
// - delay_stream_timer
auto connptr =
std::make_shared<Connection>(*this, ConnectionID::random(rng), path, tunnel_port);
std::make_shared<Connection>(*this, ConnectionID::random(), path, tunnel_port);
auto& conn = *connptr;
conns.emplace(conn.base_cid, connptr);

@ -16,6 +16,10 @@
#include <oxenmq/hex.h>
#include <oxenmq/bt_serialize.h>
extern "C" {
#include <sodium/randombytes.h>
}
namespace llarp::quic
{
ConnectionID::ConnectionID(const uint8_t* cid, size_t length)
@ -31,6 +35,13 @@ namespace llarp::quic
return o << oxenmq::to_hex(c.data, c.data + c.datalen);
}
ConnectionID ConnectionID::random(size_t size) {
ConnectionID r;
r.datalen = std::min(size, ConnectionID::max_size());
randombytes_buf(r.data, r.datalen);
return r;
}
namespace
{
#pragma GCC diagnostic push
@ -255,8 +266,7 @@ namespace llarp::quic
[[maybe_unused]] ngtcp2_rand_usage usage)
{
Debug("######################", __func__);
auto& rng = *static_cast<std::mt19937_64*>(rand_ctx->native_handle);
random_bytes(dest, destlen, rng);
randombytes_buf(dest, destlen);
return 0;
}
int
@ -479,8 +489,7 @@ namespace llarp::quic
settings.token = header.token;
// FIXME is this required?
random_bytes(
std::begin(tparams.stateless_reset_token), sizeof(tparams.stateless_reset_token), s.rng);
randombytes_buf(tparams.stateless_reset_token, sizeof(tparams.stateless_reset_token));
tparams.stateless_reset_token_present = 1;
ngtcp2_conn* connptr;
@ -508,7 +517,7 @@ namespace llarp::quic
: tunnel_port{tunnel_port}
, endpoint{c}
, base_cid{scid}
, dest_cid{ConnectionID::random(c.rng)}
, dest_cid{ConnectionID::random()}
, path{path}
{
auto [settings, tparams, cb] = init();

@ -1,7 +1,6 @@
#pragma once
#include "address.hpp"
#include "random.hpp"
#include "stream.hpp"
#include "io_result.hpp"
@ -13,7 +12,10 @@
#include <unordered_set>
#include <map>
extern "C" {
#include <ngtcp2/ngtcp2.h>
#include <sodium/randombytes.h>
}
#include <uvw/async.h>
#include <uvw/poll.h>
#include <uvw/timer.h>
@ -67,15 +69,8 @@ namespace llarp::quic
return !(*this == other);
}
template <typename RNG>
static ConnectionID
random(RNG&& rng, size_t size = ConnectionID::max_size())
{
ConnectionID r;
r.datalen = std::min(size, ConnectionID::max_size());
random_bytes(r.data, r.datalen, rng);
return r;
}
random(size_t size = ConnectionID::max_size());
};
std::ostream&
operator<<(std::ostream& o, const ConnectionID& c);

@ -2,21 +2,19 @@
#include "client.hpp"
#include "log.hpp"
#include "server.hpp"
#include <llarp/crypto/crypto.hpp>
#include <iostream>
#include <random>
#include <variant>
#include <oxenmq/hex.h>
#include <oxenmq/variant.h>
#include <uvw/timer.h>
#include <oxenmq/variant.h>
#include <sodium/crypto_generichash.h>
// DEBUG:
extern "C"
{
#include "../ngtcp2_conn.h"
#include <sodium/crypto_generichash.h>
#include <sodium/randombytes.h>
}
namespace llarp::quic
@ -24,7 +22,7 @@ namespace llarp::quic
Endpoint::Endpoint(std::optional<Address> addr, std::shared_ptr<uvw::Loop> loop_)
: loop{std::move(loop_)}
{
random_bytes(static_secret.data(), static_secret.size(), rng);
randombytes_buf(static_secret.data(), static_secret.size());
// Create and bind the UDP socket. We can't use libuv's UDP socket here because it doesn't
// give us the ability to set up the ECN field as QUIC requires.
@ -252,10 +250,8 @@ namespace llarp::quic
Endpoint::read_packet(const Packet& p, Connection& conn)
{
Debug("Reading packet from ", p.path);
Debug("Conn state before reading: ", conn.conn->state);
auto rv =
ngtcp2_conn_read_pkt(conn, p.path, &p.info, u8data(p.data), p.data.size(), get_timestamp());
Debug("Conn state after reading: ", conn.conn->state);
if (rv == 0)
conn.io_ready();
@ -339,6 +335,7 @@ namespace llarp::quic
// we're supposed to send some 0x?a?a?a?a version to trigger version negotiation
versions[0] = 0x1a2a3a4au;
CSRNG rng{};
auto nwrote = ngtcp2_pkt_write_version_negotiation(
u8data(buf),
buf.size(),
@ -506,7 +503,7 @@ namespace llarp::quic
ConnectionID cid;
for (bool inserted = false; !inserted;)
{
cid = ConnectionID::random(rng, cid_length);
cid = ConnectionID::random(cid_length);
inserted = conns.emplace(cid, conn.weak_from_this()).second;
}
Debug("Created cid ", cid, " alias for ", conn.base_cid);

@ -11,7 +11,6 @@
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <unordered_map>
#include <variant>
#include <vector>
@ -63,8 +62,6 @@ namespace llarp::quic
static constexpr size_t max_pkt_size_v4 = NGTCP2_MAX_PKTLEN_IPV4;
static constexpr size_t max_pkt_size_v6 = NGTCP2_MAX_PKTLEN_IPV6;
std::mt19937_64 rng = seeded<std::mt19937_64>();
using primary_conn_ptr = std::shared_ptr<Connection>;
using alias_conn_ptr = std::weak_ptr<Connection>;

@ -1,37 +0,0 @@
#pragma once
// TODO: replace with llarp
#include <algorithm>
#include <random>
#include <cstring>
#include <cstddef>
template <typename Gen>
void
random_bytes(void* dest, size_t length, Gen&& rng)
{
using RNG = std::remove_reference_t<Gen>;
using UInt = typename RNG::result_type;
static_assert(std::is_same_v<UInt, uint32_t> || std::is_same_v<UInt, uint64_t>);
static_assert(RNG::min() == 0 && RNG::max() == std::numeric_limits<UInt>::max());
auto* d = reinterpret_cast<std::byte*>(dest);
for (size_t o = 0; o < length; o += sizeof(UInt))
{
UInt x = rng();
std::memcpy(d + o, &x, std::min(sizeof(UInt), length - o));
}
}
// Returns an RNG with a fully seeded state from std::random_device
template <typename RNG>
RNG
seeded()
{
constexpr size_t rd_draws =
((RNG::state_size * sizeof(typename RNG::result_type) - 1) / sizeof(unsigned int) + 1);
std::array<unsigned int, rd_draws> seed_data;
std::generate(seed_data.begin(), seed_data.end(), std::random_device{});
std::seed_seq seed(seed_data.begin(), seed_data.end());
return RNG{seed};
}

@ -104,7 +104,7 @@ namespace llarp::quic
// create and store Connection
for (;;)
{
if (auto [it, ins] = conns.emplace(ConnectionID::random(rng), primary_conn_ptr{}); ins)
if (auto [it, ins] = conns.emplace(ConnectionID::random(), primary_conn_ptr{}); ins)
{
auto connptr = std::make_shared<Connection>(*this, it->first, hd, p.path);
it->second = connptr;

Loading…
Cancel
Save