Compare commits

..

No commits in common. "7397a0d02b190887d2e45e3eb2c225af72e46dd3" and "dd6551ed27591cc8e3eabea2064798fb934eae8f" have entirely different histories.

4 changed files with 47 additions and 72 deletions

21
LICENSE
View File

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2025 Erik Scholz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <solanaceae/contact/fwd.hpp> #include <solanaceae/contact/contact_model3.hpp>
#include <solanaceae/util/event_provider.hpp> #include <solanaceae/util/event_provider.hpp>
#include <solanaceae/util/span.hpp> #include <solanaceae/util/span.hpp>
@ -98,7 +98,7 @@ namespace P2PRNG::Events {
// fired when a secret does not match the hmac // fired when a secret does not match the hmac
struct ValError { struct ValError {
const ByteSpan id; const ByteSpan id;
Contact4 c; Contact3 c;
// TODO: more info? // TODO: more info?
}; };
@ -136,9 +136,9 @@ struct P2PRNGI : public P2PRNGEventProviderI {
// returns unique id, you can then use when listen to events // returns unique id, you can then use when listen to events
// chooses peers depending on C, if C is a group it (tries?) to use everyone? // chooses peers depending on C, if C is a group it (tries?) to use everyone?
virtual std::vector<uint8_t> newGernation(ContactHandle4 c, const ByteSpan initial_state_user_data) = 0; virtual std::vector<uint8_t> newGernation(Contact3Handle c, const ByteSpan initial_state_user_data) = 0;
// manually tell it which peers to use // manually tell it which peers to use
virtual std::vector<uint8_t> newGernationPeers(const std::vector<ContactHandle4>& c_vec, const ByteSpan initial_state_user_data) = 0; virtual std::vector<uint8_t> newGernationPeers(const std::vector<Contact3Handle>& c_vec, const ByteSpan initial_state_user_data) = 0;
// TODO: do we really need this, or are event enough?? // TODO: do we really need this, or are event enough??

View File

@ -5,9 +5,6 @@
#include <solanaceae/toxcore/tox_interface.hpp> #include <solanaceae/toxcore/tox_interface.hpp>
#include <solanaceae/util/utils.hpp> #include <solanaceae/util/utils.hpp>
#include <entt/entity/registry.hpp>
#include <entt/entity/handle.hpp>
#include <sodium.h> #include <sodium.h>
#include <iostream> #include <iostream>
@ -41,7 +38,7 @@
#define TOX_PKG_ID_FRIEND 0xB1 #define TOX_PKG_ID_FRIEND 0xB1
#define TOX_PKG_ID_GROUP 0xa6 #define TOX_PKG_ID_GROUP 0xa6
ContactHandle4 ToxP2PRNG::RngState::getSelf(void) const { Contact3Handle ToxP2PRNG::RngState::getSelf(void) const {
for (auto c : contacts) { for (auto c : contacts) {
if (c.all_of<Contact::Components::TagSelfStrong>()) { if (c.all_of<Contact::Components::TagSelfStrong>()) {
return c; return c;
@ -171,7 +168,7 @@ void ToxP2PRNG::checkHaveAllHMACs(RngState* rng_state, const ByteSpan id) {
// we should also validate any secret that already is in storage // we should also validate any secret that already is in storage
// validate existing (self should be good) // validate existing (self should be good)
std::vector<Contact4> bad_secrets; std::vector<Contact3> bad_secrets;
for (const auto& [pre_c, secret] : rng_state->secrets) { for (const auto& [pre_c, secret] : rng_state->secrets) {
const auto& pre_hmac = rng_state->hmacs.at(pre_c); const auto& pre_hmac = rng_state->hmacs.at(pre_c);
if (p2prng_auth_verify(secret.data()+P2PRNG_LEN, pre_hmac.data(), secret.data(), P2PRNG_LEN) != 0) { if (p2prng_auth_verify(secret.data()+P2PRNG_LEN, pre_hmac.data(), secret.data(), P2PRNG_LEN) != 0) {
@ -197,7 +194,7 @@ void ToxP2PRNG::checkHaveAllHMACs(RngState* rng_state, const ByteSpan id) {
} }
// find self contact // find self contact
ContactHandle4 self = rng_state->getSelf(); Contact3Handle self = rng_state->getSelf();
if (!static_cast<bool>(self)) { if (!static_cast<bool>(self)) {
std::cerr << "TP2PRNG error: failed to look up self\n"; std::cerr << "TP2PRNG error: failed to look up self\n";
return; return;
@ -273,13 +270,13 @@ ToxP2PRNG::ToxP2PRNG(
ToxP2PRNG::~ToxP2PRNG(void) { ToxP2PRNG::~ToxP2PRNG(void) {
} }
std::vector<uint8_t> ToxP2PRNG::newGernation(ContactHandle4 c, const ByteSpan initial_state_user_data) { std::vector<uint8_t> ToxP2PRNG::newGernation(Contact3Handle c, const ByteSpan initial_state_user_data) {
(void)c; (void)c;
(void)initial_state_user_data; (void)initial_state_user_data;
return {}; return {};
} }
std::vector<uint8_t> ToxP2PRNG::newGernationPeers(const std::vector<ContactHandle4>& c_vec, const ByteSpan initial_state_user_data) { std::vector<uint8_t> ToxP2PRNG::newGernationPeers(const std::vector<Contact3Handle>& c_vec, const ByteSpan initial_state_user_data) {
if (initial_state_user_data.empty()) { if (initial_state_user_data.empty()) {
return {}; return {};
} }
@ -405,7 +402,7 @@ ByteSpan ToxP2PRNG::getResult(const ByteSpan id_bytes) {
} }
bool ToxP2PRNG::handlePacket( bool ToxP2PRNG::handlePacket(
ContactHandle4 c, Contact3Handle c,
PKG pkg_type, PKG pkg_type,
ByteSpan data ByteSpan data
) { ) {
@ -489,7 +486,7 @@ bool ToxP2PRNG::handleGroupPacket(
#define _DATA_HAVE(x, error) if ((data.size - curser) < (x)) { error; } #define _DATA_HAVE(x, error) if ((data.size - curser) < (x)) { error; }
bool ToxP2PRNG::handle_init_with_hmac(ContactHandle4 c, const ByteSpan id, ByteSpan data) { bool ToxP2PRNG::handle_init_with_hmac(Contact3Handle c, const ByteSpan id, ByteSpan data) {
std::cerr << "TP2PRNG: got packet INIT_WITH_HMAC\n"; std::cerr << "TP2PRNG: got packet INIT_WITH_HMAC\n";
if (data.size < sizeof(uint16_t) + ToxKey{}.size() + 1) { if (data.size < sizeof(uint16_t) + ToxKey{}.size() + 1) {
@ -556,7 +553,7 @@ bool ToxP2PRNG::handle_init_with_hmac(ContactHandle4 c, const ByteSpan id, ByteS
// else, its new // else, its new
// first resolve peer keys to contacts // first resolve peer keys to contacts
std::vector<ContactHandle4> peer_contacts; std::vector<Contact3Handle> peer_contacts;
if (c.all_of<Contact::Components::ToxFriendEphemeral>()) { if (c.all_of<Contact::Components::ToxFriendEphemeral>()) {
// assuming a 1to1 can only have 2 peers // assuming a 1to1 can only have 2 peers
assert(peers.size() == 2); assert(peers.size() == 2);
@ -564,7 +561,7 @@ bool ToxP2PRNG::handle_init_with_hmac(ContactHandle4 c, const ByteSpan id, ByteS
// TODO: accel lookup // TODO: accel lookup
for (const auto& [find_c, tfp] : c.registry()->view<Contact::Components::ToxFriendPersistent>().each()) { for (const auto& [find_c, tfp] : c.registry()->view<Contact::Components::ToxFriendPersistent>().each()) {
if (tfp.key == peer_key) { if (tfp.key == peer_key) {
peer_contacts.push_back(ContactHandle4{*c.registry(), find_c}); peer_contacts.push_back(Contact3Handle{*c.registry(), find_c});
break; break;
} }
} }
@ -578,7 +575,7 @@ bool ToxP2PRNG::handle_init_with_hmac(ContactHandle4 c, const ByteSpan id, ByteS
// TODO: accel lookup // TODO: accel lookup
for (const auto& [find_c, tgpp] : c.registry()->view<Contact::Components::ToxGroupPeerPersistent>().each()) { for (const auto& [find_c, tgpp] : c.registry()->view<Contact::Components::ToxGroupPeerPersistent>().each()) {
if (tgpp.peer_key == peer_key) { if (tgpp.peer_key == peer_key) {
peer_contacts.push_back(ContactHandle4{*c.registry(), find_c}); peer_contacts.push_back(Contact3Handle{*c.registry(), find_c});
break; break;
} }
} }
@ -669,7 +666,7 @@ bool ToxP2PRNG::handle_init_with_hmac(ContactHandle4 c, const ByteSpan id, ByteS
return true; return true;
} }
bool ToxP2PRNG::handle_hmac(ContactHandle4 c, const ByteSpan id, ByteSpan data) { bool ToxP2PRNG::handle_hmac(Contact3Handle c, const ByteSpan id, ByteSpan data) {
std::cerr << "TP2PRNG: got packet HMAC\n"; std::cerr << "TP2PRNG: got packet HMAC\n";
if (data.size < P2PRNG_MAC_LEN) { if (data.size < P2PRNG_MAC_LEN) {
@ -722,7 +719,7 @@ bool ToxP2PRNG::handle_hmac(ContactHandle4 c, const ByteSpan id, ByteSpan data)
return true; return true;
} }
bool ToxP2PRNG::handle_hmac_request(ContactHandle4 c, const ByteSpan id, ByteSpan data) { bool ToxP2PRNG::handle_hmac_request(Contact3Handle c, const ByteSpan id, ByteSpan data) {
std::cerr << "TP2PRNG: got packet HMAC_REQUEST\n"; std::cerr << "TP2PRNG: got packet HMAC_REQUEST\n";
if (!data.empty()) { if (!data.empty()) {
@ -737,7 +734,7 @@ bool ToxP2PRNG::handle_hmac_request(ContactHandle4 c, const ByteSpan id, ByteSpa
// no state check necessary // no state check necessary
// find self contact // find self contact
ContactHandle4 self = rng_state->getSelf(); Contact3Handle self = rng_state->getSelf();
if (!static_cast<bool>(self)) { if (!static_cast<bool>(self)) {
std::cerr << "TP2PRNG error: failed to look up self\n"; std::cerr << "TP2PRNG error: failed to look up self\n";
@ -757,7 +754,7 @@ bool ToxP2PRNG::handle_hmac_request(ContactHandle4 c, const ByteSpan id, ByteSpa
return false; return false;
} }
bool ToxP2PRNG::handle_secret(ContactHandle4 c, const ByteSpan id, ByteSpan data) { bool ToxP2PRNG::handle_secret(Contact3Handle c, const ByteSpan id, ByteSpan data) {
std::cerr << "TP2PRNG: got packet SECRET\n"; std::cerr << "TP2PRNG: got packet SECRET\n";
if (data.size < P2PRNG_LEN + P2PRNG_MAC_KEY_LEN) { if (data.size < P2PRNG_LEN + P2PRNG_MAC_KEY_LEN) {
@ -838,7 +835,7 @@ bool ToxP2PRNG::handle_secret(ContactHandle4 c, const ByteSpan id, ByteSpan data
return true; return true;
} }
bool ToxP2PRNG::handle_secret_request(ContactHandle4 c, const ByteSpan id, ByteSpan data) { bool ToxP2PRNG::handle_secret_request(Contact3Handle c, const ByteSpan id, ByteSpan data) {
std::cerr << "TP2PRNG: got packet SECRET_REQUEST\n"; std::cerr << "TP2PRNG: got packet SECRET_REQUEST\n";
if (!data.empty()) { if (!data.empty()) {
@ -856,7 +853,7 @@ bool ToxP2PRNG::handle_secret_request(ContactHandle4 c, const ByteSpan id, ByteS
} }
// find self contact // find self contact
ContactHandle4 self = rng_state->getSelf(); Contact3Handle self = rng_state->getSelf();
if (!static_cast<bool>(self)) { if (!static_cast<bool>(self)) {
std::cerr << "TP2PRNG error: failed to look up self\n"; std::cerr << "TP2PRNG error: failed to look up self\n";
@ -878,7 +875,7 @@ bool ToxP2PRNG::handle_secret_request(ContactHandle4 c, const ByteSpan id, ByteS
return true; return true;
} }
static std::tuple<std::vector<uint8_t>, const Contact::Components::ToxFriendEphemeral*, const Contact::Components::ToxGroupPeerEphemeral*> prepSendPkgWithID(ContactHandle4 c, ToxP2PRNG::PKG pkg_type, ByteSpan id) { static std::tuple<std::vector<uint8_t>, const Contact::Components::ToxFriendEphemeral*, const Contact::Components::ToxGroupPeerEphemeral*> prepSendPkgWithID(Contact3Handle c, ToxP2PRNG::PKG pkg_type, ByteSpan id) {
std::vector<uint8_t> pkg; std::vector<uint8_t> pkg;
// determine friend or group (meh) // determine friend or group (meh)
@ -928,9 +925,9 @@ static bool sendToxPrivatePacket(
} }
bool ToxP2PRNG::send_init_with_hmac( bool ToxP2PRNG::send_init_with_hmac(
ContactHandle4 c, Contact3Handle c,
const ByteSpan id, const ByteSpan id,
const std::vector<ContactHandle4>& peers, const std::vector<Contact3Handle>& peers,
const ByteSpan initial_state, const ByteSpan initial_state,
const ByteSpan hmac const ByteSpan hmac
) { ) {
@ -975,7 +972,7 @@ bool ToxP2PRNG::send_init_with_hmac(
return sendToxPrivatePacket(_t, tfe, tgpe, pkg); return sendToxPrivatePacket(_t, tfe, tgpe, pkg);
} }
bool ToxP2PRNG::send_hmac(ContactHandle4 c, ByteSpan id, const ByteSpan hmac) { bool ToxP2PRNG::send_hmac(Contact3Handle c, ByteSpan id, const ByteSpan hmac) {
auto [pkg, tfe, tgpe] = prepSendPkgWithID(c, PKG::HMAC, id); auto [pkg, tfe, tgpe] = prepSendPkgWithID(c, PKG::HMAC, id);
if (pkg.empty()) { if (pkg.empty()) {
return false; return false;
@ -989,7 +986,7 @@ bool ToxP2PRNG::send_hmac(ContactHandle4 c, ByteSpan id, const ByteSpan hmac) {
return sendToxPrivatePacket(_t, tfe, tgpe, pkg); return sendToxPrivatePacket(_t, tfe, tgpe, pkg);
} }
bool ToxP2PRNG::send_hmac_request(ContactHandle4 c, ByteSpan id) { bool ToxP2PRNG::send_hmac_request(Contact3Handle c, ByteSpan id) {
auto [pkg, tfe, tgpe] = prepSendPkgWithID(c, PKG::HMAC_REQUEST, id); auto [pkg, tfe, tgpe] = prepSendPkgWithID(c, PKG::HMAC_REQUEST, id);
if (pkg.empty()) { if (pkg.empty()) {
return false; return false;
@ -1000,7 +997,7 @@ bool ToxP2PRNG::send_hmac_request(ContactHandle4 c, ByteSpan id) {
return sendToxPrivatePacket(_t, tfe, tgpe, pkg); return sendToxPrivatePacket(_t, tfe, tgpe, pkg);
} }
bool ToxP2PRNG::send_secret(ContactHandle4 c, ByteSpan id, const ByteSpan secret) { bool ToxP2PRNG::send_secret(Contact3Handle c, ByteSpan id, const ByteSpan secret) {
auto [pkg, tfe, tgpe] = prepSendPkgWithID(c, PKG::SECRET, id); auto [pkg, tfe, tgpe] = prepSendPkgWithID(c, PKG::SECRET, id);
if (pkg.empty()) { if (pkg.empty()) {
return false; return false;
@ -1014,7 +1011,7 @@ bool ToxP2PRNG::send_secret(ContactHandle4 c, ByteSpan id, const ByteSpan secret
return sendToxPrivatePacket(_t, tfe, tgpe, pkg); return sendToxPrivatePacket(_t, tfe, tgpe, pkg);
} }
bool ToxP2PRNG::send_secret_request(ContactHandle4 c, ByteSpan id) { bool ToxP2PRNG::send_secret_request(Contact3Handle c, ByteSpan id) {
auto [pkg, tfe, tgpe] = prepSendPkgWithID(c, PKG::SECRET_REQUEST, id); auto [pkg, tfe, tgpe] = prepSendPkgWithID(c, PKG::SECRET_REQUEST, id);
if (pkg.empty()) { if (pkg.empty()) {
return false; return false;
@ -1025,7 +1022,7 @@ bool ToxP2PRNG::send_secret_request(ContactHandle4 c, ByteSpan id) {
return sendToxPrivatePacket(_t, tfe, tgpe, pkg); return sendToxPrivatePacket(_t, tfe, tgpe, pkg);
} }
ToxP2PRNG::RngState* ToxP2PRNG::getRngSate(ContactHandle4 c, ByteSpan id_bytes) { ToxP2PRNG::RngState* ToxP2PRNG::getRngSate(Contact3Handle c, ByteSpan id_bytes) {
if (id_bytes.size != ID{}.size()) { if (id_bytes.size != ID{}.size()) {
return nullptr; return nullptr;
} }

View File

@ -36,8 +36,8 @@ class ToxP2PRNG : public P2PRNGI, public ToxEventI {
private: private:
struct RngState { struct RngState {
// all contacts participating, including self // all contacts participating, including self
std::vector<ContactHandle4> contacts; std::vector<Contact3Handle> contacts;
ContactHandle4 getSelf(void) const; Contact3Handle getSelf(void) const;
// app given // app given
std::vector<uint8_t> initial_state; std::vector<uint8_t> initial_state;
@ -49,8 +49,8 @@ class ToxP2PRNG : public P2PRNGI, public ToxEventI {
void fillInitalStatePreamble(const ByteSpan id); void fillInitalStatePreamble(const ByteSpan id);
// use contacts instead? // use contacts instead?
entt::dense_map<Contact4, std::array<uint8_t, P2PRNG_MAC_LEN>> hmacs; entt::dense_map<Contact3, std::array<uint8_t, P2PRNG_MAC_LEN>> hmacs;
entt::dense_map<Contact4, std::array<uint8_t, P2PRNG_LEN + P2PRNG_MAC_KEY_LEN>> secrets; entt::dense_map<Contact3, std::array<uint8_t, P2PRNG_LEN + P2PRNG_MAC_KEY_LEN>> secrets;
void genFinalResult(void); void genFinalResult(void);
@ -72,15 +72,15 @@ class ToxP2PRNG : public P2PRNGI, public ToxEventI {
~ToxP2PRNG(void); ~ToxP2PRNG(void);
public: // p2prng public: // p2prng
std::vector<uint8_t> newGernation(ContactHandle4 c, const ByteSpan initial_state_user_data) override; std::vector<uint8_t> newGernation(Contact3Handle c, const ByteSpan initial_state_user_data) override;
std::vector<uint8_t> newGernationPeers(const std::vector<ContactHandle4>& c_vec, const ByteSpan initial_state_user_data) override; std::vector<uint8_t> newGernationPeers(const std::vector<Contact3Handle>& c_vec, const ByteSpan initial_state_user_data) override;
P2PRNG::State getSate(const ByteSpan id) override; P2PRNG::State getSate(const ByteSpan id) override;
ByteSpan getResult(const ByteSpan id) override; ByteSpan getResult(const ByteSpan id) override;
protected: protected:
bool handlePacket( bool handlePacket(
ContactHandle4 c, Contact3Handle c,
PKG pkg_type, PKG pkg_type,
ByteSpan data ByteSpan data
); );
@ -96,43 +96,42 @@ class ToxP2PRNG : public P2PRNGI, public ToxEventI {
const bool _private const bool _private
); );
bool handle_init_with_hmac(ContactHandle4 c, const ByteSpan id, ByteSpan data); bool handle_init_with_hmac(Contact3Handle c, const ByteSpan id, ByteSpan data);
bool handle_hmac(ContactHandle4 c, const ByteSpan id, ByteSpan data); bool handle_hmac(Contact3Handle c, const ByteSpan id, ByteSpan data);
bool handle_hmac_request(ContactHandle4 c, const ByteSpan id, ByteSpan data); bool handle_hmac_request(Contact3Handle c, const ByteSpan id, ByteSpan data);
bool handle_secret(ContactHandle4 c, const ByteSpan id, ByteSpan data); bool handle_secret(Contact3Handle c, const ByteSpan id, ByteSpan data);
bool handle_secret_request(ContactHandle4 c, const ByteSpan id, ByteSpan data); bool handle_secret_request(Contact3Handle c, const ByteSpan id, ByteSpan data);
bool send_init_with_hmac( bool send_init_with_hmac(
ContactHandle4 c, Contact3Handle c,
const ByteSpan id, const ByteSpan id,
const std::vector<ContactHandle4>& peers, const std::vector<Contact3Handle>& peers,
const ByteSpan initial_state, const ByteSpan initial_state,
const ByteSpan hmac const ByteSpan hmac
); );
bool send_hmac( bool send_hmac(
ContactHandle4 c, Contact3Handle c,
const ByteSpan id, const ByteSpan id,
const ByteSpan hmac const ByteSpan hmac
); );
bool send_hmac_request( bool send_hmac_request(
ContactHandle4 c, Contact3Handle c,
const ByteSpan id const ByteSpan id
); );
bool send_secret( bool send_secret(
ContactHandle4 c, Contact3Handle c,
const ByteSpan id, const ByteSpan id,
const ByteSpan secret const ByteSpan secret
); );
bool send_secret_request( bool send_secret_request(
ContactHandle4 c, Contact3Handle c,
const ByteSpan id const ByteSpan id
); );
RngState* getRngSate(ContactHandle4 c, ByteSpan id); RngState* getRngSate(Contact3Handle c, ByteSpan id);
protected: protected:
bool onToxEvent(const Tox_Event_Friend_Lossless_Packet* e) override; bool onToxEvent(const Tox_Event_Friend_Lossless_Packet* e) override;
bool onToxEvent(const Tox_Event_Group_Custom_Packet* e) override; bool onToxEvent(const Tox_Event_Group_Custom_Packet* e) override;
bool onToxEvent(const Tox_Event_Group_Custom_Private_Packet* e) override; bool onToxEvent(const Tox_Event_Group_Custom_Private_Packet* e) override;
}; };