mirror of https://github.com/PurpleI2P/i2pd.git
orignal
4 years ago
committed by
GitHub
47 changed files with 3415 additions and 1775 deletions
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
name: Build on Windows |
||||
|
||||
on: [push, pull_request] |
||||
|
||||
defaults: |
||||
run: |
||||
shell: msys2 {0} |
||||
|
||||
jobs: |
||||
build: |
||||
name: Building for ${{ matrix.arch }} |
||||
runs-on: windows-latest |
||||
strategy: |
||||
fail-fast: true |
||||
matrix: |
||||
include: [ |
||||
{ msystem: MINGW64, arch: x86_64 }, |
||||
{ msystem: MINGW32, arch: i686 } |
||||
] |
||||
steps: |
||||
- uses: actions/checkout@v2 |
||||
- name: Setup MSYS2 |
||||
uses: msys2/setup-msys2@v2 |
||||
with: |
||||
msystem: ${{ matrix.msystem }} |
||||
install: base-devel mingw-w64-${{ matrix.arch }}-gcc mingw-w64-${{ matrix.arch }}-boost mingw-w64-${{ matrix.arch }}-openssl mingw-w64-${{ matrix.arch }}-miniupnpc |
||||
update: true |
||||
- name: build application |
||||
run: make USE_UPNP=yes DEBUG=no -j3 |
@ -0,0 +1,21 @@
@@ -0,0 +1,21 @@
|
||||
name: Build on Ubuntu with make |
||||
|
||||
on: [push, pull_request] |
||||
|
||||
jobs: |
||||
build: |
||||
name: Building with USE_UPNP=${{ matrix.with_upnp }} flag |
||||
runs-on: ubuntu-16.04 |
||||
strategy: |
||||
fail-fast: true |
||||
matrix: |
||||
with_upnp: ['yes', 'no'] |
||||
steps: |
||||
- uses: actions/checkout@v2 |
||||
- name: install packages |
||||
run: | |
||||
sudo add-apt-repository ppa:mhier/libboost-latest |
||||
sudo apt-get update |
||||
sudo apt-get install build-essential libboost1.74-dev libminiupnpc-dev libssl-dev zlib1g-dev |
||||
- name: build application |
||||
run: make USE_AVX=no USE_AESNI=no USE_UPNP=${{ matrix.with_upnp }} -j3 |
@ -1,10 +0,0 @@
@@ -1,10 +0,0 @@
|
||||
set -e -x |
||||
|
||||
base_url='http://repo.msys2.org/msys/x86_64/' |
||||
packages="libzstd-1.4.4-2-x86_64.pkg.tar.xz pacman-5.2.1-6-x86_64.pkg.tar.xz zstd-1.4.4-2-x86_64.pkg.tar.xz" |
||||
for p in $packages |
||||
do |
||||
curl "${base_url}$p" -o "$p" |
||||
done |
||||
pacman -U --noconfirm $packages |
||||
rm -f $packages |
@ -1,88 +0,0 @@
@@ -1,88 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2020, The PurpleI2P Project |
||||
* |
||||
* This file is part of Purple i2pd project and licensed under BSD3 |
||||
* |
||||
* See full license text in LICENSE file at top of project tree |
||||
*/ |
||||
|
||||
#ifndef CRYPTO_WORKER_H_ |
||||
#define CRYPTO_WORKER_H_ |
||||
|
||||
#include <condition_variable> |
||||
#include <mutex> |
||||
#include <deque> |
||||
#include <thread> |
||||
#include <vector> |
||||
#include <memory> |
||||
|
||||
namespace i2p |
||||
{ |
||||
namespace worker |
||||
{ |
||||
template<typename Caller> |
||||
struct ThreadPool |
||||
{ |
||||
typedef std::function<void(void)> ResultFunc; |
||||
typedef std::function<ResultFunc(void)> WorkFunc; |
||||
typedef std::pair<std::shared_ptr<Caller>, WorkFunc> Job; |
||||
typedef std::mutex mtx_t; |
||||
typedef std::unique_lock<mtx_t> lock_t; |
||||
typedef std::condition_variable cond_t; |
||||
ThreadPool(int workers) |
||||
{ |
||||
stop = false; |
||||
if(workers > 0) |
||||
{ |
||||
while(workers--) |
||||
{ |
||||
threads.emplace_back([this] { |
||||
for (;;) |
||||
{ |
||||
Job job; |
||||
{ |
||||
lock_t lock(this->queue_mutex); |
||||
this->condition.wait( |
||||
lock, [this] { return this->stop || !this->jobs.empty(); }); |
||||
if (this->stop && this->jobs.empty()) return; |
||||
job = std::move(this->jobs.front()); |
||||
this->jobs.pop_front(); |
||||
} |
||||
ResultFunc result = job.second(); |
||||
job.first->GetService().post(result); |
||||
} |
||||
}); |
||||
} |
||||
} |
||||
}; |
||||
|
||||
void Offer(const Job & job) |
||||
{ |
||||
{ |
||||
lock_t lock(queue_mutex); |
||||
if (stop) return; |
||||
jobs.emplace_back(job); |
||||
} |
||||
condition.notify_one(); |
||||
} |
||||
|
||||
~ThreadPool() |
||||
{ |
||||
{ |
||||
lock_t lock(queue_mutex); |
||||
stop = true; |
||||
} |
||||
condition.notify_all(); |
||||
for(auto &t: threads) t.join(); |
||||
} |
||||
|
||||
std::vector<std::thread> threads; |
||||
std::deque<Job> jobs; |
||||
mtx_t queue_mutex; |
||||
cond_t condition; |
||||
bool stop; |
||||
}; |
||||
} |
||||
} |
||||
|
||||
#endif |
@ -0,0 +1,166 @@
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2020, The PurpleI2P Project |
||||
* |
||||
* This file is part of Purple i2pd project and licensed under BSD3 |
||||
* |
||||
* See full license text in LICENSE file at top of project tree |
||||
* |
||||
*/ |
||||
|
||||
#include <memory> |
||||
#include <openssl/rand.h> |
||||
#include <openssl/sha.h> |
||||
#include "Crypto.h" |
||||
#include "Log.h" |
||||
#include "Transports.h" |
||||
#include "Timestamp.h" |
||||
#include "I2PEndian.h" |
||||
#include "I2NPProtocol.h" |
||||
#include "TunnelConfig.h" |
||||
|
||||
namespace i2p |
||||
{ |
||||
namespace tunnel |
||||
{ |
||||
TunnelHopConfig::TunnelHopConfig (std::shared_ptr<const i2p::data::IdentityEx> r) |
||||
{ |
||||
RAND_bytes (layerKey, 32); |
||||
RAND_bytes (ivKey, 32); |
||||
RAND_bytes (replyKey, 32); |
||||
RAND_bytes (replyIV, 16); |
||||
RAND_bytes ((uint8_t *)&tunnelID, 4); |
||||
if (!tunnelID) tunnelID = 1; // tunnelID can't be zero
|
||||
isGateway = true; |
||||
isEndpoint = true; |
||||
ident = r; |
||||
//nextRouter = nullptr;
|
||||
nextTunnelID = 0; |
||||
|
||||
next = nullptr; |
||||
prev = nullptr; |
||||
} |
||||
|
||||
void TunnelHopConfig::SetNextIdent (const i2p::data::IdentHash& ident) |
||||
{ |
||||
nextIdent = ident; |
||||
isEndpoint = false; |
||||
RAND_bytes ((uint8_t *)&nextTunnelID, 4); |
||||
if (!nextTunnelID) nextTunnelID = 1; // tunnelID can't be zero
|
||||
} |
||||
|
||||
void TunnelHopConfig::SetReplyHop (uint32_t replyTunnelID, const i2p::data::IdentHash& replyIdent) |
||||
{ |
||||
nextIdent = replyIdent; |
||||
nextTunnelID = replyTunnelID; |
||||
isEndpoint = true; |
||||
} |
||||
|
||||
void TunnelHopConfig::SetNext (TunnelHopConfig * n) |
||||
{ |
||||
next = n; |
||||
if (next) |
||||
{ |
||||
next->prev = this; |
||||
next->isGateway = false; |
||||
isEndpoint = false; |
||||
nextIdent = next->ident->GetIdentHash (); |
||||
nextTunnelID = next->tunnelID; |
||||
} |
||||
} |
||||
|
||||
void TunnelHopConfig::SetPrev (TunnelHopConfig * p) |
||||
{ |
||||
prev = p; |
||||
if (prev) |
||||
{ |
||||
prev->next = this; |
||||
prev->isEndpoint = false; |
||||
isGateway = false; |
||||
} |
||||
} |
||||
|
||||
void TunnelHopConfig::CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID, BN_CTX * ctx) |
||||
{ |
||||
uint8_t flag = 0; |
||||
if (isGateway) flag |= 0x80; |
||||
if (isEndpoint) flag |= 0x40; |
||||
auto encryptor = ident->CreateEncryptor (nullptr); |
||||
if (IsECIES ()) |
||||
{ |
||||
uint8_t clearText[ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE]; |
||||
htobe32buf (clearText + ECIES_BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET, tunnelID); |
||||
htobe32buf (clearText + ECIES_BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET, nextTunnelID); |
||||
memcpy (clearText + ECIES_BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET, nextIdent, 32); |
||||
memcpy (clearText + ECIES_BUILD_REQUEST_RECORD_LAYER_KEY_OFFSET, layerKey, 32); |
||||
memcpy (clearText + ECIES_BUILD_REQUEST_RECORD_IV_KEY_OFFSET, ivKey, 32); |
||||
memcpy (clearText + ECIES_BUILD_REQUEST_RECORD_REPLY_KEY_OFFSET, replyKey, 32); |
||||
memcpy (clearText + ECIES_BUILD_REQUEST_RECORD_REPLY_IV_OFFSET, replyIV, 16); |
||||
clearText[ECIES_BUILD_REQUEST_RECORD_FLAG_OFFSET] = flag; |
||||
memset (clearText + ECIES_BUILD_REQUEST_RECORD_MORE_FLAGS_OFFSET, 0, 3); // set to 0 for compatibility
|
||||
htobe32buf (clearText + ECIES_BUILD_REQUEST_RECORD_REQUEST_TIME_OFFSET, i2p::util::GetMinutesSinceEpoch ()); |
||||
htobe32buf (clearText + ECIES_BUILD_REQUEST_RECORD_REQUEST_EXPIRATION_OFFSET, 600); // +10 minutes
|
||||
htobe32buf (clearText + ECIES_BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET, replyMsgID); |
||||
memset (clearText + ECIES_BUILD_REQUEST_RECORD_PADDING_OFFSET, 0, ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE - ECIES_BUILD_REQUEST_RECORD_PADDING_OFFSET); |
||||
if (encryptor) |
||||
EncryptECIES (encryptor, clearText, record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET, ctx); |
||||
} |
||||
else |
||||
{ |
||||
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE]; |
||||
htobe32buf (clearText + BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET, tunnelID); |
||||
memcpy (clearText + BUILD_REQUEST_RECORD_OUR_IDENT_OFFSET, ident->GetIdentHash (), 32); |
||||
htobe32buf (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET, nextTunnelID); |
||||
memcpy (clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET, nextIdent, 32); |
||||
memcpy (clearText + BUILD_REQUEST_RECORD_LAYER_KEY_OFFSET, layerKey, 32); |
||||
memcpy (clearText + BUILD_REQUEST_RECORD_IV_KEY_OFFSET, ivKey, 32); |
||||
memcpy (clearText + BUILD_REQUEST_RECORD_REPLY_KEY_OFFSET, replyKey, 32); |
||||
memcpy (clearText + BUILD_REQUEST_RECORD_REPLY_IV_OFFSET, replyIV, 16); |
||||
clearText[BUILD_REQUEST_RECORD_FLAG_OFFSET] = flag; |
||||
htobe32buf (clearText + BUILD_REQUEST_RECORD_REQUEST_TIME_OFFSET, i2p::util::GetHoursSinceEpoch ()); |
||||
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); |
||||
if (encryptor) |
||||
encryptor->Encrypt (clearText, record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET, ctx, false); |
||||
} |
||||
memcpy (record + BUILD_REQUEST_RECORD_TO_PEER_OFFSET, (const uint8_t *)ident->GetIdentHash (), 16); |
||||
} |
||||
|
||||
void TunnelHopConfig::EncryptECIES (std::shared_ptr<i2p::crypto::CryptoKeyEncryptor>& encryptor, |
||||
const uint8_t * plainText, uint8_t * encrypted, BN_CTX * ctx) |
||||
{ |
||||
static const char protocolName[] = "Noise_N_25519_ChaChaPoly_SHA256"; // 31 chars
|
||||
memcpy (ck, protocolName, 32); // ck = h = protocol_name || 0
|
||||
SHA256 (ck, 32, h); // h = SHA256(h);
|
||||
uint8_t hepk[32]; |
||||
encryptor->Encrypt (nullptr, hepk, nullptr, false); |
||||
MixHash (hepk, 32); // h = SHA256(h || hepk)
|
||||
auto ephemeralKeys = i2p::transport::transports.GetNextX25519KeysPair (); |
||||
memcpy (encrypted, ephemeralKeys->GetPublicKey (), 32); |
||||
MixHash (encrypted, 32); // h = SHA256(h || sepk)
|
||||
encrypted += 32; |
||||
uint8_t sharedSecret[32]; |
||||
ephemeralKeys->Agree (hepk, sharedSecret); // x25519(sesk, hepk)
|
||||
uint8_t keydata[64]; |
||||
i2p::crypto::HKDF (ck, sharedSecret, 32, "", keydata); |
||||
memcpy (ck, keydata, 32); |
||||
uint8_t nonce[12]; |
||||
memset (nonce, 0, 12); |
||||
if (!i2p::crypto::AEADChaCha20Poly1305 (plainText, ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE, h, 32, |
||||
keydata + 32, nonce, encrypted, ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE + 16, true)) // encrypt
|
||||
{ |
||||
LogPrint (eLogWarning, "Tunnel: Plaintext AEAD encryption failed"); |
||||
return; |
||||
} |
||||
MixHash (encrypted, ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE + 16); // h = SHA256(h || ciphertext)
|
||||
} |
||||
|
||||
void TunnelHopConfig::MixHash (const uint8_t * buf, size_t len) |
||||
{ |
||||
SHA256_CTX ctx; |
||||
SHA256_Init (&ctx); |
||||
SHA256_Update (&ctx, h, 32); |
||||
SHA256_Update (&ctx, buf, len); |
||||
SHA256_Final (h, &ctx); |
||||
} |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue