Browse Source

replaced ElGamalEncrypt to ElGamalEncryption

pull/46/head
orignal 11 years ago
parent
commit
3c8f859169
  1. 32
      ElGamal.h
  2. 5
      Garlic.cpp
  3. 2
      Garlic.h
  4. 2
      I2NPProtocol.cpp
  5. 16
      Identity.h

32
ElGamal.h

@ -17,14 +17,13 @@ namespace crypto
{ {
public: public:
ElGamalEncryption (const uint8_t * key, bool zeroPadding = false): ElGamalEncryption (const uint8_t * key):
y (key, 256), k (rnd, CryptoPP::Integer::One(), elgp-1), 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)), a (a_exp_b_mod_c (elgg, k, elgp)), b1 (a_exp_b_mod_c (y, k, elgp))
m_ZeroPadding (zeroPadding)
{ {
} }
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 // calculate b = b1*m mod p
uint8_t m[255]; uint8_t m[255];
@ -34,7 +33,7 @@ namespace crypto
CryptoPP::Integer b (a_times_b_mod_c (b1, CryptoPP::Integer (m, 255), elgp)); CryptoPP::Integer b (a_times_b_mod_c (b1, CryptoPP::Integer (m, 255), elgp));
// copy a and b // copy a and b
if (m_ZeroPadding) if (zeroPadding)
{ {
encrypted[0] = 0; encrypted[0] = 0;
a.Encode (encrypted + 1, 256); a.Encode (encrypted + 1, 256);
@ -72,29 +71,6 @@ namespace crypto
memcpy (data, m + 33, 222); memcpy (data, m + 33, 222);
return true; 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);
}
} }
} }

5
Garlic.cpp

@ -15,8 +15,7 @@ namespace garlic
{ {
GarlicRoutingSession::GarlicRoutingSession (const i2p::data::RoutingDestination& destination, int numTags): GarlicRoutingSession::GarlicRoutingSession (const i2p::data::RoutingDestination& destination, int numTags):
m_Destination (destination), m_FirstMsgID (0), m_IsAcknowledged (false), m_Destination (destination), m_FirstMsgID (0), m_IsAcknowledged (false),
m_NumTags (numTags), m_NextTag (-1), m_SessionTags (0), m_NumTags (numTags), m_NextTag (-1), m_SessionTags (0)
m_ElGamalEncryption (m_Destination.GetEncryptionPublicKey (), true)
{ {
// create new session tags and session key // create new session tags and session key
m_Rnd.GenerateBlock (m_SessionKey, 32); m_Rnd.GenerateBlock (m_SessionKey, 32);
@ -56,7 +55,7 @@ namespace garlic
m_Rnd.GenerateBlock (elGamal.preIV, 32); // Pre-IV m_Rnd.GenerateBlock (elGamal.preIV, 32); // Pre-IV
uint8_t iv[32]; // IV is first 16 bytes uint8_t iv[32]; // IV is first 16 bytes
CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32); 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); m_Encryption.SetKeyWithIV (m_SessionKey, 32, iv);
buf += 514; buf += 514;
len += 514; len += 514;

2
Garlic.h

@ -9,7 +9,6 @@
#include <cryptopp/osrng.h> #include <cryptopp/osrng.h>
#include "I2NPProtocol.h" #include "I2NPProtocol.h"
#include "LeaseSet.h" #include "LeaseSet.h"
#include "ElGamal.h"
namespace i2p namespace i2p
{ {
@ -66,7 +65,6 @@ namespace garlic
uint8_t * m_SessionTags; // m_NumTags*32 bytes uint8_t * m_SessionTags; // m_NumTags*32 bytes
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption m_Encryption; CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption m_Encryption;
i2p::crypto::ElGamalEncryption m_ElGamalEncryption;
CryptoPP::AutoSeededRandomPool m_Rnd; CryptoPP::AutoSeededRandomPool m_Rnd;
}; };

2
I2NPProtocol.cpp

@ -212,7 +212,7 @@ namespace i2p
const I2NPBuildRequestRecordClearText& clearText, const I2NPBuildRequestRecordClearText& clearText,
I2NPBuildRequestRecordElGamalEncrypted& record) 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); memcpy (record.toPeer, (const uint8_t *)router.GetIdentHash (), 16);
} }

16
Identity.h

@ -3,6 +3,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <string.h> #include <string.h>
#include "ElGamal.h"
namespace i2p namespace i2p
{ {
@ -84,9 +85,24 @@ namespace data
class RoutingDestination class RoutingDestination
{ {
public: public:
RoutingDestination (): m_ElGamalEncryption (nullptr) {};
virtual ~RoutingDestination () { delete m_ElGamalEncryption; };
virtual const IdentHash& GetIdentHash () const = 0; virtual const IdentHash& GetIdentHash () const = 0;
virtual const uint8_t * GetEncryptionPublicKey () const = 0; virtual const uint8_t * GetEncryptionPublicKey () const = 0;
virtual bool IsDestination () const = 0; // for garlic 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
}; };
} }
} }

Loading…
Cancel
Save