mirror of https://github.com/PurpleI2P/i2pd.git
chertov
11 years ago
9 changed files with 256 additions and 27 deletions
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
#include <boost/bind.hpp> |
||||
#include "Log.h" |
||||
#include "hmac.h" |
||||
#include "SSU.h" |
||||
|
||||
namespace i2p |
||||
{ |
||||
namespace ssu |
||||
{ |
||||
SSUServer::SSUServer (boost::asio::io_service& service, int port): |
||||
m_Socket (service, boost::asio::ip::udp::v4 (), port) |
||||
{ |
||||
} |
||||
|
||||
void SSUServer::Start () |
||||
{ |
||||
Receive (); |
||||
} |
||||
|
||||
void SSUServer::Stop () |
||||
{ |
||||
m_Socket.close (); |
||||
} |
||||
|
||||
void SSUServer::Receive () |
||||
{ |
||||
m_Socket.async_receive_from (boost::asio::buffer (m_ReceiveBuffer, SSU_MTU), m_SenderEndpoint, |
||||
boost::bind (&SSUServer::HandleReceivedFrom, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); |
||||
} |
||||
|
||||
void SSUServer::HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred) |
||||
{ |
||||
if (!ecode) |
||||
{ |
||||
LogPrint ("SSU received ", bytes_transferred, " bytes"); |
||||
// Handle
|
||||
Receive (); |
||||
} |
||||
else |
||||
LogPrint ("SSU receive error: ", ecode.message ()); |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
#ifndef SSU_H__ |
||||
#define SSU_H__ |
||||
|
||||
#include <inttypes.h> |
||||
#include <boost/asio.hpp> |
||||
|
||||
namespace i2p |
||||
{ |
||||
namespace ssu |
||||
{ |
||||
const int SSU_MTU = 1484; |
||||
|
||||
class SSUServer |
||||
{ |
||||
public: |
||||
|
||||
SSUServer (boost::asio::io_service& service, int port); |
||||
void Start (); |
||||
void Stop (); |
||||
|
||||
private: |
||||
|
||||
void Receive (); |
||||
void HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred); |
||||
|
||||
private: |
||||
|
||||
boost::asio::ip::udp::socket m_Socket; |
||||
boost::asio::ip::udp::endpoint m_SenderEndpoint; |
||||
uint8_t m_ReceiveBuffer[SSU_MTU]; |
||||
}; |
||||
} |
||||
} |
||||
|
||||
#endif |
||||
|
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
#ifndef HMAC_H__ |
||||
#define HMAC_H__ |
||||
|
||||
#include <inttypes.h> |
||||
#include <string.h> |
||||
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 |
||||
#include <cryptopp/md5.h> |
||||
|
||||
namespace i2p |
||||
{ |
||||
namespace crypto |
||||
{ |
||||
const uint64_t IPAD = 0x3636363636363636; |
||||
const uint64_t OPAD = 0x5C5C5C5C5C5C5C5C; |
||||
|
||||
inline void HMACMD5Digest (uint8_t * msg, size_t len, uint8_t * key, uint8_t * digest) |
||||
// key is 32 bytes
|
||||
// digest is 16 bytes
|
||||
// block size is 64 bytes
|
||||
{ |
||||
size_t totalLen = len + 64 + 32; |
||||
uint8_t * buf = new uint8_t[totalLen]; // TODO: reuse buffers
|
||||
// ikeypad
|
||||
((uint64_t *)buf)[0] = ((uint64_t *)key)[0] ^ IPAD; |
||||
((uint64_t *)buf)[1] = ((uint64_t *)key)[1] ^ IPAD; |
||||
((uint64_t *)buf)[2] = ((uint64_t *)key)[2] ^ IPAD; |
||||
((uint64_t *)buf)[3] = ((uint64_t *)key)[3] ^ IPAD; |
||||
((uint64_t *)buf)[4] = IPAD; |
||||
((uint64_t *)buf)[5] = IPAD; |
||||
((uint64_t *)buf)[6] = IPAD; |
||||
((uint64_t *)buf)[7] = IPAD; |
||||
// concatenate with msg
|
||||
memcpy (buf + 64, msg, len); |
||||
// calculate first hash
|
||||
uint8_t hash[16]; // MD5
|
||||
CryptoPP::Weak1::MD5().CalculateDigest (hash, buf, len + 64); |
||||
|
||||
// okeypad
|
||||
((uint64_t *)buf)[0] = ((uint64_t *)key)[0] ^ OPAD; |
||||
((uint64_t *)buf)[1] = ((uint64_t *)key)[1] ^ OPAD; |
||||
((uint64_t *)buf)[2] = ((uint64_t *)key)[2] ^ OPAD; |
||||
((uint64_t *)buf)[3] = ((uint64_t *)key)[3] ^ OPAD; |
||||
((uint64_t *)buf)[4] = OPAD; |
||||
((uint64_t *)buf)[5] = OPAD; |
||||
((uint64_t *)buf)[6] = OPAD; |
||||
((uint64_t *)buf)[7] = OPAD; |
||||
// copy first hash after okeypad
|
||||
memcpy (buf + 64, hash, 16); |
||||
// fill next 16 bytes with zeros (first hash size assumed 32 bytes in I2P)
|
||||
memset (buf + 72, 0, 16); |
||||
|
||||
// calculate digest
|
||||
CryptoPP::Weak1::MD5().CalculateDigest (digest, buf, totalLen); |
||||
delete[] buf; |
||||
} |
||||
} |
||||
} |
||||
|
||||
#endif |
||||
|
Loading…
Reference in new issue