From b2542a57df06525966d2f5550daf0abef5d75599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Thu, 22 Nov 2018 15:37:22 -0500 Subject: [PATCH] securedht: cache value signature check/decryption results --- include/opendht/value.h | 10 ++++++++++ src/securedht.cpp | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/include/opendht/value.h b/include/opendht/value.h index 63795caf..a4a3348a 100644 --- a/include/opendht/value.h +++ b/include/opendht/value.h @@ -122,6 +122,8 @@ private: std::map<ValueType::Id, ValueType> types {}; }; +struct CryptoValueCache; + /** * A "value" is data potentially stored on the Dht, with some metadata. * @@ -588,6 +590,14 @@ struct OPENDHT_PUBLIC Value * Hold encrypted version of the data. */ Blob cypher {}; + +private: + friend class SecureDht; + /* Cache for crypto ops */ + bool signatureChecked {false}; + bool signatureValid {false}; + bool decrypted {false}; + Sp<Value> decryptedValue {}; }; using ValuesExport = std::pair<InfoHash, Blob>; diff --git a/src/securedht.cpp b/src/securedht.cpp index 4d173170..a0d5a5a2 100644 --- a/src/securedht.cpp +++ b/src/securedht.cpp @@ -235,12 +235,17 @@ SecureDht::checkValue(const Sp<Value>& v) #endif return {}; } + if (v->decrypted) { + return v->decryptedValue; + } + v->decrypted = true; try { Value decrypted_val (decrypt(*v)); if (decrypted_val.recipient == getId()) { if (decrypted_val.owner) nodesPubKeys_[decrypted_val.owner->getId()] = decrypted_val.owner; - return std::make_shared<Value>(std::move(decrypted_val)); + v->decryptedValue = std::make_shared<Value>(std::move(decrypted_val)); + return v->decryptedValue; } // Ignore values belonging to other people } catch (const std::exception& e) { @@ -249,7 +254,12 @@ SecureDht::checkValue(const Sp<Value>& v) } // Check signed values else if (v->isSigned()) { + if (v->signatureChecked) { + return v->signatureValid ? v : Sp<Value>{}; + } + v->signatureChecked = true; if (v->owner and v->owner->checkSignature(v->getToSign(), v->signature)) { + v->signatureValid = true; nodesPubKeys_[v->owner->getId()] = v->owner; return v; } @@ -355,7 +365,7 @@ SecureDht::putSigned(const InfoHash& hash, Sp<Value> val, DoneCallback callback, void SecureDht::putEncrypted(const InfoHash& hash, const InfoHash& to, Sp<Value> val, DoneCallback callback, bool permanent) { - findPublicKey(to, [=](const Sp<const crypto::PublicKey> pk) { + findPublicKey(to, [=](const Sp<const crypto::PublicKey>& pk) { if(!pk || !*pk) { if (callback) callback(false, {}); -- GitLab