Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Refactor src/core/tunnel. See #58.
Browse files Browse the repository at this point in the history
* Moved TunnelCrypto.{cpp,h} to src/core/crypto, renamed to Tunnel.{cpp,h}.
  • Loading branch information
anonimal committed Jan 3, 2016
1 parent 90b38bc commit 956ca96
Show file tree
Hide file tree
Showing 17 changed files with 2,932 additions and 2,548 deletions.
2 changes: 1 addition & 1 deletion src/core/CMakeLists.txt
Expand Up @@ -13,6 +13,7 @@ set(CORE_SRC
"crypto/CryptoConst.cpp"
"crypto/EdDSA25519.cpp"
"crypto/Signature.cpp"
"crypto/Tunnel.cpp"
"transport/NTCP.cpp"
"transport/NTCPSession.cpp"
"transport/SSU.cpp"
Expand All @@ -22,7 +23,6 @@ set(CORE_SRC
"transport/UPnP.cpp"
"tunnel/TransitTunnel.cpp"
"tunnel/Tunnel.cpp"
"tunnel/TunnelCrypto.cpp"
"tunnel/TunnelEndpoint.cpp"
"tunnel/TunnelGateway.cpp"
"tunnel/TunnelPool.cpp"
Expand Down
133 changes: 133 additions & 0 deletions src/core/crypto/Tunnel.cpp
@@ -0,0 +1,133 @@
/**
* Copyright (c) 2015-2016, The Kovri I2P Router Project
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "AESNIMacros.h"
#include "crypto/Tunnel.h"
#include "tunnel/TunnelBase.h"

namespace i2p {
namespace crypto {

void TunnelEncryption::SetKeys(
const AESKey& layerKey,
const AESKey& ivKey) {
m_LayerEncryption.SetKey(layerKey);
m_IVEncryption.SetKey(ivKey);
}

void TunnelEncryption::Encrypt(
const uint8_t* in,
uint8_t* out) {
#ifdef AESNI
__asm__(
// encrypt IV
"movups (%[in]), %%xmm0 \n"
EncryptAES256(sched_iv)
"movaps %%xmm0, %%xmm1 \n"
// double IV encryption
EncryptAES256(sched_iv)
"movups %%xmm0, (%[out]) \n"
// encrypt data, IV is xmm1
"1: \n"
"add $16, %[in] \n"
"add $16, %[out] \n"
"movups (%[in]), %%xmm0 \n"
"pxor %%xmm1, %%xmm0 \n"
EncryptAES256(sched_l)
"movaps %%xmm0, %%xmm1 \n"
"movups %%xmm0, (%[out]) \n"
"dec %[num] \n"
"jnz 1b \n"
:
: [sched_iv]"r"(m_IVEncryption.GetKeySchedule()), [sched_l]"r"(m_LayerEncryption.GetKeySchedule()),
[in]"r"(in), [out]"r"(out), [num]"r"(63) // 63 blocks = 1008 bytes
: "%xmm0", "%xmm1", "cc", "memory"
);
#else
m_IVEncryption.Encrypt( // iv
(const CipherBlock *)in,
reinterpret_cast<CipherBlock *>(out));
m_LayerEncryption.SetIV(out);
m_LayerEncryption.Encrypt( // data
in + 16,
i2p::tunnel::TUNNEL_DATA_ENCRYPTED_SIZE,
out + 16);
m_IVEncryption.Encrypt( // double iv
reinterpret_cast<CipherBlock *>(out),
reinterpret_cast<CipherBlock *>(out));
#endif
}

void TunnelDecryption::Decrypt(
const uint8_t* in,
uint8_t* out) {
#ifdef AESNI
__asm__(
// decrypt IV
"movups (%[in]), %%xmm0 \n"
DecryptAES256(sched_iv)
"movaps %%xmm0, %%xmm1 \n"
// double IV encryption
DecryptAES256(sched_iv)
"movups %%xmm0, (%[out]) \n"
// decrypt data, IV is xmm1
"1: \n"
"add $16, %[in] \n"
"add $16, %[out] \n"
"movups (%[in]), %%xmm0 \n"
"movaps %%xmm0, %%xmm2 \n"
DecryptAES256(sched_l)
"pxor %%xmm1, %%xmm0 \n"
"movups %%xmm0, (%[out]) \n"
"movaps %%xmm2, %%xmm1 \n"
"dec %[num] \n"
"jnz 1b \n"
:
: [sched_iv]"r"(m_IVDecryption.GetKeySchedule()), [sched_l]"r"(m_LayerDecryption.GetKeySchedule()),
[in]"r"(in), [out]"r"(out), [num]"r"(63) // 63 blocks = 1008 bytes
: "%xmm0", "%xmm1", "%xmm2", "cc", "memory"
);
#else
m_IVDecryption.Decrypt(
(const CipherBlock *)in,
reinterpret_cast<CipherBlock *>(out)); // iv
m_LayerDecryption.SetIV(out);
m_LayerDecryption.Decrypt( // data
in + 16,
i2p::tunnel::TUNNEL_DATA_ENCRYPTED_SIZE,
out + 16);
m_IVDecryption.Decrypt( // double iv
reinterpret_cast<CipherBlock *>(out),
reinterpret_cast<CipherBlock *>(out));
#endif
}

} // namespace crypto
} // namespace i2p
62 changes: 33 additions & 29 deletions src/core/tunnel/TunnelCrypto.h → src/core/crypto/Tunnel.h
Expand Up @@ -28,52 +28,56 @@
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef TUNNEL_CRYPTO_H__
#define TUNNEL_CRYPTO_H__
#ifndef SRC_CORE_CRYPTO_TUNNEL_H_
#define SRC_CORE_CRYPTO_TUNNEL_H_

#include "crypto/AES.h"
#include "AES.h"

namespace i2p {
namespace crypto {

class TunnelEncryption { // with double IV encryption
public:
void SetKeys (const AESKey& layerKey, const AESKey& ivKey);
class TunnelEncryption { // with double IV encryption
public:
void SetKeys(
const AESKey& layerKey,
const AESKey& ivKey);

void Encrypt (const uint8_t * in, uint8_t * out); // 1024 bytes (16 IV + 1008 data)
void Encrypt(
const uint8_t* in,
uint8_t* out); // 1024 bytes (16 IV + 1008 data)

private:

ECBEncryption m_IVEncryption;
private:
ECBEncryption m_IVEncryption;
#ifdef AESNI
ECBEncryption m_LayerEncryption;
ECBEncryption m_LayerEncryption;
#else
CBCEncryption m_LayerEncryption;
CBCEncryption m_LayerEncryption;
#endif
};

class TunnelDecryption { // with double IV encryption
public:

void SetKeys (const AESKey& layerKey, const AESKey& ivKey)
{
m_LayerDecryption.SetKey (layerKey);
m_IVDecryption.SetKey (ivKey);
}
class TunnelDecryption { // with double IV encryption
public:
void SetKeys(
const AESKey& layerKey,
const AESKey& ivKey) {
m_LayerDecryption.SetKey(layerKey);
m_IVDecryption.SetKey(ivKey);
}

void Decrypt (const uint8_t * in, uint8_t * out); // 1024 bytes (16 IV + 1008 data)
void Decrypt(
const uint8_t* in,
uint8_t* out); // 1024 bytes (16 IV + 1008 data)

private:

ECBDecryption m_IVDecryption;
private:
ECBDecryption m_IVDecryption;
#ifdef AESNI
ECBDecryption m_LayerDecryption;
ECBDecryption m_LayerDecryption;
#else
CBCDecryption m_LayerDecryption;
CBCDecryption m_LayerDecryption;
#endif
};

} // crypto
} // i2p
} // namespace crypto
} // namespace i2p

#endif
#endif // SRC_CORE_CRYPTO_TUNNEL_H_
2 changes: 1 addition & 1 deletion src/core/transport/NTCP.h
Expand Up @@ -98,7 +98,7 @@ class NTCPServer {
std::thread* m_Thread;
boost::asio::io_service m_Service;
boost::asio::io_service::work m_Work;
boost::asio::ip::tcp::acceptor* m_NTCPAcceptor,l
boost::asio::ip::tcp::acceptor* m_NTCPAcceptor,
*m_NTCPV6Acceptor;
std::mutex m_NTCPSessionsMutex;
std::map<i2p::data::IdentHash, std::shared_ptr<NTCPSession> > m_NTCPSessions;
Expand Down

0 comments on commit 956ca96

Please sign in to comment.