Browse Source

use ElGamalEncrypt

pull/750/head
orignal 8 years ago
parent
commit
9da984b866
  1. 24
      Crypto.cpp
  2. 16
      Crypto.h
  3. 3
      Garlic.cpp
  4. 3
      TunnelConfig.h

24
Crypto.cpp

@ -272,10 +272,9 @@ namespace crypto
} }
// ElGamal // ElGamal
void ElGamalEncrypt (const uint8_t * key, const uint8_t * data, uint8_t * encrypted, bool zeroPadding)
ElGamalEncryption::ElGamalEncryption (const uint8_t * key)
{ {
ctx = BN_CTX_new (); BN_CTX * ctx = BN_CTX_new ();
// select random k // select random k
BIGNUM * k = BN_new (); BIGNUM * k = BN_new ();
#if defined(__x86_64__) #if defined(__x86_64__)
@ -284,6 +283,7 @@ namespace crypto
BN_rand (k, ELGAMAL_SHORT_EXPONENT_NUM_BITS, -1, 1); // short exponent of 226 bits BN_rand (k, ELGAMAL_SHORT_EXPONENT_NUM_BITS, -1, 1); // short exponent of 226 bits
#endif #endif
// calculate a // calculate a
BIGNUM * a;
if (g_ElggTable) if (g_ElggTable)
a = ElggPow (k, g_ElggTable, ctx); a = ElggPow (k, g_ElggTable, ctx);
else else
@ -295,21 +295,10 @@ namespace crypto
BIGNUM * y = BN_new (); BIGNUM * y = BN_new ();
BN_bin2bn (key, 256, y); BN_bin2bn (key, 256, y);
// calculate b1 // calculate b1
b1 = BN_new (); BIGNUM * b1 = BN_new ();
BN_mod_exp (b1, y, k, elgp, ctx); BN_mod_exp (b1, y, k, elgp, ctx);
BN_free (y); BN_free (y);
BN_free (k); BN_free (k);
}
ElGamalEncryption::~ElGamalEncryption ()
{
BN_CTX_free (ctx);
BN_free (a);
BN_free (b1);
}
void ElGamalEncryption::Encrypt (const uint8_t * data, uint8_t * encrypted, bool zeroPadding) const
{
// create m // create m
uint8_t m[255]; uint8_t m[255];
m[0] = 0xFF; m[0] = 0xFF;
@ -319,6 +308,7 @@ namespace crypto
BIGNUM * b = BN_new (); BIGNUM * b = BN_new ();
BN_bin2bn (m, 255, b); BN_bin2bn (m, 255, b);
BN_mod_mul (b, b1, b, elgp, ctx); BN_mod_mul (b, b1, b, elgp, ctx);
BN_free (b1);
// copy a and b // copy a and b
if (zeroPadding) if (zeroPadding)
{ {
@ -333,8 +323,10 @@ namespace crypto
bn2buf (b, encrypted + 256, 256); bn2buf (b, encrypted + 256, 256);
} }
BN_free (b); BN_free (b);
BN_free (a);
BN_CTX_free (ctx);
} }
bool ElGamalDecrypt (const uint8_t * key, const uint8_t * encrypted, bool ElGamalDecrypt (const uint8_t * key, const uint8_t * encrypted,
uint8_t * data, bool zeroPadding) uint8_t * data, bool zeroPadding)
{ {

16
Crypto.h

@ -47,21 +47,7 @@ namespace crypto
}; };
// ElGamal // ElGamal
class ElGamalEncryption void ElGamalEncrypt (const uint8_t * key, const uint8_t * data, uint8_t * encrypted, bool zeroPadding = false);
{
public:
ElGamalEncryption (const uint8_t * key);
~ElGamalEncryption ();
void Encrypt (const uint8_t * data, uint8_t * encrypted, bool zeroPadding = false) const;
private:
BN_CTX * ctx;
BIGNUM * a, * b1;
};
bool ElGamalDecrypt (const uint8_t * key, const uint8_t * encrypted, uint8_t * data, bool zeroPadding = false); bool ElGamalDecrypt (const uint8_t * key, const uint8_t * encrypted, uint8_t * data, bool zeroPadding = false);
void GenerateElGamalKeyPair (uint8_t * priv, uint8_t * pub); void GenerateElGamalKeyPair (uint8_t * priv, uint8_t * pub);

3
Garlic.cpp

@ -187,8 +187,7 @@ namespace garlic
RAND_bytes (elGamal.preIV, 32); // Pre-IV RAND_bytes (elGamal.preIV, 32); // Pre-IV
uint8_t iv[32]; // IV is first 16 bytes uint8_t iv[32]; // IV is first 16 bytes
SHA256(elGamal.preIV, 32, iv); SHA256(elGamal.preIV, 32, iv);
i2p::crypto::ElGamalEncryption elGamalEncryption (m_Destination->GetEncryptionPublicKey ()); i2p::crypto::ElGamalEncrypt (m_Destination->GetEncryptionPublicKey (), (uint8_t *)&elGamal, buf, true);
elGamalEncryption.Encrypt ((uint8_t *)&elGamal, buf, true);
m_Encryption.SetIV (iv); m_Encryption.SetIV (iv);
buf += 514; buf += 514;
len += 514; len += 514;

3
TunnelConfig.h

@ -101,8 +101,7 @@ namespace tunnel
htobe32buf (clearText + BUILD_REQUEST_RECORD_REQUEST_TIME_OFFSET, i2p::util::GetHoursSinceEpoch ()); htobe32buf (clearText + BUILD_REQUEST_RECORD_REQUEST_TIME_OFFSET, i2p::util::GetHoursSinceEpoch ());
htobe32buf (clearText + BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET, replyMsgID); htobe32buf (clearText + BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET, replyMsgID);
RAND_bytes (clearText + BUILD_REQUEST_RECORD_PADDING_OFFSET, BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE - BUILD_REQUEST_RECORD_PADDING_OFFSET); RAND_bytes (clearText + BUILD_REQUEST_RECORD_PADDING_OFFSET, BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE - BUILD_REQUEST_RECORD_PADDING_OFFSET);
i2p::crypto::ElGamalEncryption elGamalEncryption (ident->GetEncryptionPublicKey ()); i2p::crypto::ElGamalEncrypt (ident->GetEncryptionPublicKey (), clearText, record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET);
elGamalEncryption.Encrypt (clearText, record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET);
memcpy (record + BUILD_REQUEST_RECORD_TO_PEER_OFFSET, (const uint8_t *)ident->GetIdentHash (), 16); memcpy (record + BUILD_REQUEST_RECORD_TO_PEER_OFFSET, (const uint8_t *)ident->GetIdentHash (), 16);
} }
}; };

Loading…
Cancel
Save