diff --git a/ElGamal.h b/ElGamal.h index 745c3840..b3f0e44f 100644 --- a/ElGamal.h +++ b/ElGamal.h @@ -17,14 +17,13 @@ namespace crypto { public: - ElGamalEncryption (const uint8_t * key, bool zeroPadding = false): + ElGamalEncryption (const uint8_t * key): y (key, 256), k (rnd, CryptoPP::Integer::One(), elgp-1), - a (a_exp_b_mod_c (elgg, k, elgp)), b1 (a_exp_b_mod_c (y, k, elgp)), - m_ZeroPadding (zeroPadding) + a (a_exp_b_mod_c (elgg, k, elgp)), b1 (a_exp_b_mod_c (y, k, elgp)) { } - void Encrypt (const uint8_t * data, int len, uint8_t * encrypted) + void Encrypt (const uint8_t * data, int len, uint8_t * encrypted, bool zeroPadding = false) { // calculate b = b1*m mod p uint8_t m[255]; @@ -34,7 +33,7 @@ namespace crypto CryptoPP::Integer b (a_times_b_mod_c (b1, CryptoPP::Integer (m, 255), elgp)); // copy a and b - if (m_ZeroPadding) + if (zeroPadding) { encrypted[0] = 0; a.Encode (encrypted + 1, 256); @@ -72,29 +71,6 @@ namespace crypto memcpy (data, m + 33, 222); return true; } - - -// deprecated - - inline void ElGamalEncrypt (const uint8_t * key, const uint8_t * data, int len, - uint8_t * encrypted, bool zeroPadding = false) // 514 with padding and 512 without - { - CryptoPP::AutoSeededRandomPool rnd; - CryptoPP::Integer y(key, 256), k(rnd, CryptoPP::Integer::One(), elgp-1); - - if (zeroPadding) - { - encrypted[0] = 0; - encrypted[257] = 0; - } - a_exp_b_mod_c (elgg, k, elgp).Encode (zeroPadding ? encrypted + 1 : encrypted, 256); - uint8_t m[255]; - m[0] = 0xFF; - memcpy (m+33, data, len); - CryptoPP::SHA256().CalculateDigest(m+1, m+33, 222); - a_times_b_mod_c (a_exp_b_mod_c (y, k, elgp), - CryptoPP::Integer (m, 255), elgp).Encode (zeroPadding ? encrypted + 258 : encrypted + 256, 256); - } } } diff --git a/Garlic.cpp b/Garlic.cpp index 80a267c7..e73a6f02 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -15,8 +15,7 @@ namespace garlic { GarlicRoutingSession::GarlicRoutingSession (const i2p::data::RoutingDestination& destination, int numTags): m_Destination (destination), m_FirstMsgID (0), m_IsAcknowledged (false), - m_NumTags (numTags), m_NextTag (-1), m_SessionTags (0), - m_ElGamalEncryption (m_Destination.GetEncryptionPublicKey (), true) + m_NumTags (numTags), m_NextTag (-1), m_SessionTags (0) { // create new session tags and session key m_Rnd.GenerateBlock (m_SessionKey, 32); @@ -56,7 +55,7 @@ namespace garlic m_Rnd.GenerateBlock (elGamal.preIV, 32); // Pre-IV uint8_t iv[32]; // IV is first 16 bytes CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32); - m_ElGamalEncryption.Encrypt ((uint8_t *)&elGamal, sizeof(elGamal), buf); + m_Destination.GetElGamalEncryption ()->Encrypt ((uint8_t *)&elGamal, sizeof(elGamal), buf, true); m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv); buf += 514; len += 514; diff --git a/Garlic.h b/Garlic.h index ca6bae64..0383b030 100644 --- a/Garlic.h +++ b/Garlic.h @@ -9,7 +9,6 @@ #include #include "I2NPProtocol.h" #include "LeaseSet.h" -#include "ElGamal.h" namespace i2p { @@ -66,7 +65,6 @@ namespace garlic uint8_t * m_SessionTags; // m_NumTags*32 bytes CryptoPP::CBC_Mode::Encryption m_Encryption; - i2p::crypto::ElGamalEncryption m_ElGamalEncryption; CryptoPP::AutoSeededRandomPool m_Rnd; }; diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index f42d9f9d..25549a09 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -212,7 +212,7 @@ namespace i2p const I2NPBuildRequestRecordClearText& clearText, I2NPBuildRequestRecordElGamalEncrypted& record) { - i2p::crypto::ElGamalEncrypt (router.GetRouterIdentity ().publicKey, (uint8_t *)&clearText, sizeof(clearText), record.encrypted); + router.GetElGamalEncryption ()->Encrypt ((uint8_t *)&clearText, sizeof(clearText), record.encrypted); memcpy (record.toPeer, (const uint8_t *)router.GetIdentHash (), 16); } diff --git a/Identity.h b/Identity.h index e17b27be..10af227c 100644 --- a/Identity.h +++ b/Identity.h @@ -3,6 +3,7 @@ #include #include +#include "ElGamal.h" namespace i2p { @@ -84,9 +85,24 @@ namespace data class RoutingDestination { public: + + RoutingDestination (): m_ElGamalEncryption (nullptr) {}; + virtual ~RoutingDestination () { delete m_ElGamalEncryption; }; + virtual const IdentHash& GetIdentHash () const = 0; virtual const uint8_t * GetEncryptionPublicKey () const = 0; virtual bool IsDestination () const = 0; // for garlic + + i2p::crypto::ElGamalEncryption * GetElGamalEncryption () const + { + if (!m_ElGamalEncryption) + m_ElGamalEncryption = new i2p::crypto::ElGamalEncryption (GetEncryptionPublicKey ()); + return m_ElGamalEncryption; + } + + private: + + mutable i2p::crypto::ElGamalEncryption * m_ElGamalEncryption; // use lazy initialization }; } }