Skip to content

Commit

Permalink
Refactoring (#19)
Browse files Browse the repository at this point in the history
* fix: Fixed memory leak in IVEC.

* chore: Updated NetworkWorker.

* chore: Fixed import for libi2pd.

* chore: Packet handler optimization.

* chore: Replace va_list with variadic tamplate.

* chore: Added overload for 0 args.

* chore: Optimization of arrays. Fixed a bug with padding.

* chore: Optimization of arrays.

* chore: Optimization of arrays.

* chore: Added a delay in the loop to reduce the frequency of connection retries and not litter the log with errors.

* chore: Added type casting.

* chore: Optimization of arrays. One nesting level removed.

* chore: Fixed typo.

* chore: Optimization of arrays.

* chore: Fixed typo.

* chore: Clean commented code.

* chore: Remove path from version.

* chore: Added logic for processing duplicated data.

* chore: Added logic for correct Index Packet processing.
  • Loading branch information
polistern authored Dec 2, 2021
1 parent 06b28e2 commit af2737f
Show file tree
Hide file tree
Showing 23 changed files with 341 additions and 326 deletions.
2 changes: 1 addition & 1 deletion src/BoteContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "Logging.h"
#include "Packet.h"

#include "../lib/i2pd/libi2pd/Identity.h"
#include "Identity.h"

namespace pbote {

Expand Down
76 changes: 41 additions & 35 deletions src/Cryptography.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
* Copyright (c) 2019-2021 polistern
*/

#include <algorithm>
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
#include <algorithm>

#include "Tag.h"

#include "Cryptography.h"
#include "../lib/i2pd/libi2pd/Tag.h"

namespace pbote {

ECDHP256Encryptor::ECDHP256Encryptor(const byte *pubkey) {
Expand Down Expand Up @@ -37,7 +39,7 @@ std::vector<byte> ECDHP256Encryptor::Encrypt(const byte *data, int len) {
// Get shared point
const EC_POINT* ec_eph_point = EC_KEY_get0_public_key(ec_ephemeral_key);
byte shared_key[EPH_KEY_LEN];
EC_POINT_point2oct(ec_curve, ec_eph_point, POINT_CONVERSION_COMPRESSED, shared_key, 33, nullptr);
EC_POINT_point2oct(ec_curve, ec_eph_point, POINT_CONVERSION_COMPRESSED, shared_key, EPH_KEY_LEN, nullptr);
std::vector<byte> result(shared_key, shared_key + EPH_KEY_LEN);

// Create the shared secret
Expand All @@ -54,37 +56,36 @@ std::vector<byte> ECDHP256Encryptor::Encrypt(const byte *data, int len) {
LogPrint(eLogDebug, "Crypto: Encrypt: secret len: ", secret_len);

// Generate hash of shared secret
byte secret_hash[secret_len];
SHA256(secret, secret_len, secret_hash);
std::vector<byte> secret_hash(secret_len);
SHA256(secret, secret_len, secret_hash.data());
OPENSSL_free(secret);

i2p::data::Tag<32> secret_h(secret_hash);
i2p::data::Tag<32> secret_h(secret_hash.data());
LogPrint(eLogDebug, "Crypto: Encrypt: secret_hash: ", secret_h.ToBase64());

// Encrypt the data using the hash of the shared secret as an AES key
byte *ivec = new byte[AES_BLOCK_SIZE];
byte ivec[AES_BLOCK_SIZE];
std::generate(ivec, ivec + AES_BLOCK_SIZE, std::ref(rbe));
result.insert(result.end(), ivec, ivec + AES_BLOCK_SIZE);

AES_KEY encrypt_key;
AES_set_encrypt_key(secret_hash, 256, &encrypt_key);

LogPrint(eLogDebug, "Crypto: Encrypt: len: ", len, ", pad: ", len % 16);
len += len % 16;

//byte *encrypted = new byte[len];
byte encrypted[len];

LogPrint(eLogDebug, "Crypto: Encrypt: len: ", len);
int key_status = AES_set_encrypt_key(secret_hash.data(), 256, &encrypt_key);
if (key_status == -1) {
LogPrint(eLogError, "Crypto: Encrypt: AES key is null");
return {};
}
if (key_status == -2) {
LogPrint(eLogError, "Crypto: Encrypt: AES unsupported number of bits");
return {};
}

AES_cbc_encrypt(data, encrypted, len, &encrypt_key, ivec, AES_ENCRYPT);
const int padding = len % 16;

LogPrint(eLogDebug, "Crypto: Encrypt: len: ", len);
std::vector<byte> encrypted(len + padding);
AES_cbc_encrypt(data, encrypted.data(), len, &encrypt_key, ivec, AES_ENCRYPT);

std::vector<byte> enc_data(encrypted, encrypted + len);
//std::vector<byte> enc_data(encrypted, encrypted + (sizeof encrypted / sizeof encrypted[0]));
LogPrint(eLogDebug, "Crypto: Encrypt: enc_data.size(): ", enc_data.size());
result.insert(result.end(), enc_data.begin(), enc_data.end());
LogPrint(eLogDebug, "Crypto: Encrypt: encrypted size: ", encrypted.size());
result.insert(result.end(), encrypted.begin(), encrypted.end());

return result;
}
Expand Down Expand Up @@ -142,33 +143,38 @@ std::vector<byte> ECDHP256Decryptor::Decrypt(const byte *encrypted, int elen) {
LogPrint(eLogDebug, "Crypto: Decrypt: secret len: ", secret_len);

// generate hash of shared secret
byte secret_hash[secret_len];
SHA256(secret, secret_len, secret_hash);
std::vector<byte> secret_hash(secret_len);
SHA256(secret, secret_len, secret_hash.data());
OPENSSL_free(secret);

i2p::data::Tag<32> secret_h(secret_hash);
i2p::data::Tag<32> secret_h(secret_hash.data());
LogPrint(eLogDebug, "Crypto: Decrypt: secret_hash: ", secret_h.ToBase64());

// decrypt using the shared secret hash as AES key
byte *ivec = new byte[AES_BLOCK_SIZE];
byte ivec[AES_BLOCK_SIZE];
memcpy(ivec, encrypted + offset, AES_BLOCK_SIZE);
offset += AES_BLOCK_SIZE;

size_t dlen = elen - offset;
LogPrint(eLogDebug, "Crypto: Decrypt: dlen: ", dlen, ", elen: ", elen);
LogPrint(eLogDebug, "Crypto: Decrypt: elen: ", elen, ", dlen: ", dlen);

byte *edata = new byte[dlen];
memcpy(edata, encrypted + offset, dlen);
std::vector<byte> edata(encrypted + offset, encrypted + offset + dlen);

AES_KEY dkey;
AES_set_decrypt_key(secret_hash, 256, &dkey);

byte *decrypted = new byte[dlen];
int key_status = AES_set_decrypt_key(secret_hash.data(), 256, &dkey);
if (key_status == -1) {
LogPrint(eLogError, "Crypto: Decrypt: AES key is null");
return {};
}
if (key_status == -2) {
LogPrint(eLogError, "Crypto: Decrypt: AES unsupported number of bits");
return {};
}

AES_cbc_encrypt(edata, decrypted, dlen, &dkey, ivec, AES_DECRYPT);
std::vector<byte> decrypted(dlen);
AES_cbc_encrypt(edata.data(), decrypted.data(), dlen, &dkey, ivec, AES_DECRYPT);

std::vector<byte> dec_data(decrypted, decrypted + dlen);
return dec_data;
return decrypted;
}
return {};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Cryptography.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace pbote {

static const unsigned int EPH_KEY_LEN = 33;
#define EPH_KEY_LEN 33

typedef uint8_t byte;

Expand Down
Loading

0 comments on commit af2737f

Please sign in to comment.