From 04dc34260fe803baf6fdd65c94dab8b47461b1b1 Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 26 Feb 2017 15:05:14 -0500 Subject: [PATCH 01/39] replaced stringstream by a list of buffers --- Streaming.cpp | 98 +++++++++++++++++++++++++++++++++------------------ Streaming.h | 50 ++++++++++++++++++++++---- 2 files changed, 107 insertions(+), 41 deletions(-) diff --git a/Streaming.cpp b/Streaming.cpp index 357a3373..0045b10d 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -11,6 +11,50 @@ namespace i2p { namespace stream { + void SendBufferQueue::Add (const uint8_t * buf, size_t len, SendHandler handler) + { + m_Buffers.push_back (std::make_shared(buf, len, handler)); + m_Size += len; + } + + size_t SendBufferQueue::Get (uint8_t * buf, size_t len) + { + size_t offset = 0; + while (!m_Buffers.empty () && offset < len) + { + auto nextBuffer = m_Buffers.front (); + auto rem = nextBuffer->GetRemainingSize (); + if (offset + rem <= len) + { + // whole buffer + memcpy (buf + offset, nextBuffer->GetRemaningBuffer (), rem); + offset += rem; + m_Buffers.pop_front (); // delete it + } + else + { + // partially + rem = len - offset; + memcpy (buf + offset, nextBuffer->GetRemaningBuffer (), len - offset); + nextBuffer->offset += (len - offset); + offset = len; // break + } + } + m_Size -= offset; + return offset; + } + + void SendBufferQueue::CleanUp () + { + if (!m_Buffers.empty ()) + { + for (auto it: m_Buffers) + it->Cancel (); + m_Buffers.clear (); + m_Size = 0; + } + } + Stream::Stream (boost::asio::io_service& service, StreamingDestination& local, std::shared_ptr remote, int port): m_Service (service), m_SendStreamID (0), m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1), @@ -45,19 +89,16 @@ namespace stream m_AckSendTimer.cancel (); m_ReceiveTimer.cancel (); m_ResendTimer.cancel (); - if (m_SendHandler) - { - auto handler = m_SendHandler; - m_SendHandler = nullptr; - handler (boost::asio::error::make_error_code (boost::asio::error::operation_aborted)); - } //CleanUp (); /* Need to recheck - broke working on windows */ m_LocalDestination.DeleteStream (shared_from_this ()); } void Stream::CleanUp () { - m_SendBuffer.str (""); + { + std::unique_lock l(m_SendBufferMutex); + m_SendBuffer.CleanUp (); + } while (!m_ReceiveQueue.empty ()) { auto packet = m_ReceiveQueue.front (); @@ -321,23 +362,21 @@ namespace stream size_t Stream::Send (const uint8_t * buf, size_t len) { - if (len > 0 && buf) - { - std::unique_lock l(m_SendBufferMutex); - m_SendBuffer.clear (); - m_SendBuffer.write ((const char *)buf, len); - } - m_Service.post (std::bind (&Stream::SendBuffer, shared_from_this ())); + // TODO: check max buffer size + AsyncSend (buf, len, nullptr); return len; } void Stream::AsyncSend (const uint8_t * buf, size_t len, SendHandler handler) { - if (m_SendHandler) - handler (boost::asio::error::make_error_code (boost::asio::error::in_progress)); - else - m_SendHandler = handler; - Send (buf, len); + if (len > 0 && buf) + { + std::unique_lock l(m_SendBufferMutex); + m_SendBuffer.Add (buf, len, handler); + } + else if (handler) + handler(boost::system::error_code ()); + m_Service.post (std::bind (&Stream::SendBuffer, shared_from_this ())); } void Stream::SendBuffer () @@ -349,7 +388,7 @@ namespace stream std::vector packets; { std::unique_lock l(m_SendBufferMutex); - while ((m_Status == eStreamStatusNew) || (IsEstablished () && !m_SendBuffer.eof () && numMsgs > 0)) + while ((m_Status == eStreamStatusNew) || (IsEstablished () && !m_SendBuffer.IsEmpty () && numMsgs > 0)) { Packet * p = m_LocalDestination.NewPacket (); uint8_t * packet = p->GetBuffer (); @@ -390,8 +429,7 @@ namespace stream uint8_t * signature = packet + size; // set it later memset (signature, 0, signatureLen); // zeroes for now size += signatureLen; // signature - m_SendBuffer.read ((char *)(packet + size), STREAMING_MTU - size); - size += m_SendBuffer.gcount (); // payload + size += m_SendBuffer.Get (packet + size, STREAMING_MTU - size); // payload m_LocalDestination.GetOwner ()->Sign (packet, size, signature); } else @@ -401,22 +439,12 @@ namespace stream size += 2; // flags htobuf16 (packet + size, 0); // no options size += 2; // options size - m_SendBuffer.read((char *)(packet + size), STREAMING_MTU - size); - size += m_SendBuffer.gcount (); // payload + size += m_SendBuffer.Get(packet + size, STREAMING_MTU - size); // payload } p->len = size; packets.push_back (p); numMsgs--; } - if (m_SendBuffer.eof ()) - { - m_SendBuffer.str(""); // clean up buffer - if (m_SendHandler) - { - m_SendHandler (boost::system::error_code ()); - m_SendHandler = nullptr; - } - } } if (packets.size () > 0) { @@ -433,7 +461,7 @@ namespace stream m_SentPackets.insert (it); } SendPackets (packets); - if (m_Status == eStreamStatusClosing && m_SendBuffer.eof ()) + if (m_Status == eStreamStatusClosing && m_SendBuffer.IsEmpty ()) SendClose (); if (isEmpty) ScheduleResend (); @@ -525,7 +553,7 @@ namespace stream Terminate (); break; case eStreamStatusClosing: - if (m_SentPackets.empty () && m_SendBuffer.eof ()) // nothing to send + if (m_SentPackets.empty () && m_SendBuffer.IsEmpty ()) // nothing to send { m_Status = eStreamStatusClosed; SendClose (); diff --git a/Streaming.h b/Streaming.h index bfea74ed..be8f3f1a 100644 --- a/Streaming.h +++ b/Streaming.h @@ -3,7 +3,6 @@ #include #include -#include #include #include #include @@ -104,6 +103,48 @@ namespace stream }; }; + typedef std::function SendHandler; + struct SendBuffer + { + uint8_t * buf; + size_t len, offset; + SendHandler handler; + + SendBuffer (const uint8_t * b, size_t l, SendHandler h): + len(l), offset (0), handler(h) + { + buf = new uint8_t[len]; + memcpy (buf, b, len); + } + ~SendBuffer () + { + delete[] buf; + if (handler) handler(boost::system::error_code ()); + } + size_t GetRemainingSize () const { return len - offset; }; + const uint8_t * GetRemaningBuffer () const { return buf + offset; }; + void Cancel () { if (handler) handler (boost::asio::error::make_error_code (boost::asio::error::operation_aborted)); handler = nullptr; }; + }; + + class SendBufferQueue + { + public: + + SendBufferQueue (): m_Size (0) {}; + ~SendBufferQueue () { CleanUp (); }; + + void Add (const uint8_t * buf, size_t len, SendHandler handler); + size_t Get (uint8_t * buf, size_t len); + size_t GetSize () const { return m_Size; }; + bool IsEmpty () const { return m_Buffers.empty (); }; + void CleanUp (); + + private: + + std::list > m_Buffers; + size_t m_Size; + }; + enum StreamStatus { eStreamStatusNew = 0, @@ -118,8 +159,6 @@ namespace stream { public: - typedef std::function SendHandler; - Stream (boost::asio::io_service& service, StreamingDestination& local, std::shared_ptr remote, int port = 0); // outgoing Stream (boost::asio::io_service& service, StreamingDestination& local); // incoming @@ -149,7 +188,7 @@ namespace stream size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; }; size_t GetSendQueueSize () const { return m_SentPackets.size (); }; size_t GetReceiveQueueSize () const { return m_ReceiveQueue.size (); }; - size_t GetSendBufferSize () const { return m_SendBuffer.rdbuf ()->in_avail (); }; + size_t GetSendBufferSize () const { return m_SendBuffer.GetSize (); }; int GetWindowSize () const { return m_WindowSize; }; int GetRTT () const { return m_RTT; }; @@ -202,11 +241,10 @@ namespace stream uint16_t m_Port; std::mutex m_SendBufferMutex; - std::stringstream m_SendBuffer; + SendBufferQueue m_SendBuffer; int m_WindowSize, m_RTT, m_RTO; uint64_t m_LastWindowSizeIncreaseTime; int m_NumResendAttempts; - SendHandler m_SendHandler; }; class StreamingDestination: public std::enable_shared_from_this From 319f72ae2af77d6e8c48771008e51563e0f9e1ad Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 28 Feb 2017 15:19:49 -0500 Subject: [PATCH 02/39] 0.9.29 --- version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.h b/version.h index 3e9fca8b..440ca0e8 100644 --- a/version.h +++ b/version.h @@ -21,7 +21,7 @@ #define I2P_VERSION_MAJOR 0 #define I2P_VERSION_MINOR 9 -#define I2P_VERSION_MICRO 28 +#define I2P_VERSION_MICRO 29 #define I2P_VERSION_PATCH 0 #define I2P_VERSION MAKE_VERSION(I2P_VERSION_MAJOR, I2P_VERSION_MINOR, I2P_VERSION_MICRO) From 60ed43c11bece260c197fcc12f3f01bffbb7e216 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 28 Feb 2017 15:58:53 -0500 Subject: [PATCH 03/39] #818. Exploratory config --- Config.cpp | 8 ++++++++ Tunnel.cpp | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Config.cpp b/Config.cpp index 7f8f2f53..10c55e8f 100644 --- a/Config.cpp +++ b/Config.cpp @@ -210,6 +210,13 @@ namespace config { ("websockets.address", value()->default_value("127.0.0.1"), "address to bind websocket server on") ("websockets.port", value()->default_value(7666), "port to bind websocket server on"); + options_description exploratory("Exploratory Options"); + exploratory.add_options() + ("exploratory.inbound.length", value()->default_value(2), "Exploratory inbound tunnel length") + ("exploratory.outbound.length", value()->default_value(2), "Exploratory outbound tunnel length") + ("exploratory.inbound.quantity", value()->default_value(3), "Exploratory inbound tunnels quantity") + ("exploratory.outbound.quantity", value()->default_value(3), "Exploratory outbound tunnels quantity"); + m_OptionsDesc .add(general) .add(limits) @@ -226,6 +233,7 @@ namespace config { .add(addressbook) .add(trust) .add(websocket) + .add(exploratory) ; } diff --git a/Tunnel.cpp b/Tunnel.cpp index 6f9346d2..390c64ac 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -10,6 +10,7 @@ #include "I2NPProtocol.h" #include "Transports.h" #include "NetDb.h" +#include "Config.h" #include "Tunnel.h" #include "TunnelPool.h" #ifdef WITH_EVENTS @@ -741,7 +742,11 @@ namespace tunnel CreateZeroHopsOutboundTunnel (); if (!m_ExploratoryPool) { - m_ExploratoryPool = CreateTunnelPool (2, 2, 3, 3); // 2-hop exploratory, 3 tunnels + int ibLen; i2p::config::GetOption("exploratory.inbound.length", ibLen); + int obLen; i2p::config::GetOption("exploratory.outbound.length", obLen); + int ibNum; i2p::config::GetOption("exploratory.inbound.quantity", ibNum); + int obNum; i2p::config::GetOption("exploratory.outbound.quantity", obNum); + m_ExploratoryPool = CreateTunnelPool (ibLen, obLen, ibNum, obNum); m_ExploratoryPool->SetLocalDestination (i2p::context.GetSharedDestination ()); } return; From 92fc736cfa2d4cf77842a508983c9232c2ed3dde Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 5 Mar 2017 17:08:20 -0500 Subject: [PATCH 04/39] recreate SSU session again if session key is invalid --- SSUData.cpp | 3 +++ SSUSession.cpp | 19 ++++++++++++++++--- SSUSession.h | 4 +++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/SSUData.cpp b/SSUData.cpp index 32e66e22..78f93270 100644 --- a/SSUData.cpp +++ b/SSUData.cpp @@ -48,6 +48,9 @@ namespace transport { m_ResendTimer.cancel (); m_IncompleteMessagesCleanupTimer.cancel (); + m_IncompleteMessages.clear (); + m_SentMessages.clear (); + m_ReceivedMessages.clear (); } void SSUData::AdjustPacketSize (std::shared_ptr remoteRouter) diff --git a/SSUSession.cpp b/SSUSession.cpp index d54a1137..49757772 100644 --- a/SSUSession.cpp +++ b/SSUSession.cpp @@ -104,6 +104,7 @@ namespace transport DecryptSessionKey (buf, len); else { + if (m_State == eSessionStateEstablished) Reset (); // new session key required // try intro key depending on side if (Validate (buf, len, m_IntroKey)) Decrypt (buf, len, m_IntroKey); @@ -869,14 +870,26 @@ namespace transport void SSUSession::Close () { + SendSessionDestroyed (); + Reset (); m_State = eSessionStateClosed; - SendSesionDestroyed (); + } + + void SSUSession::Reset () + { + m_State = eSessionStateUnknown; transports.PeerDisconnected (shared_from_this ()); m_Data.Stop (); m_ConnectTimer.cancel (); if (m_SentRelayTag) + { m_Server.RemoveRelay (m_SentRelayTag); // relay tag is not valid anymore - } + m_SentRelayTag = 0; + } + m_DHKeysPair = nullptr; + m_SignedData = nullptr; + m_IsSessionKey = false; + } void SSUSession::Done () { @@ -1140,7 +1153,7 @@ namespace transport } } - void SSUSession::SendSesionDestroyed () + void SSUSession::SendSessionDestroyed () { if (m_IsSessionKey) { diff --git a/SSUSession.h b/SSUSession.h index b25beeae..af13c645 100644 --- a/SSUSession.h +++ b/SSUSession.h @@ -121,7 +121,7 @@ namespace transport void ProcessPeerTest (const uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint); void SendPeerTest (uint32_t nonce, const boost::asio::ip::address& address, uint16_t port, const uint8_t * introKey, bool toAddress = true, bool sendAddress = true); void ProcessData (uint8_t * buf, size_t len); - void SendSesionDestroyed (); + void SendSessionDestroyed (); void Send (uint8_t type, const uint8_t * payload, size_t len); // with session key void Send (const uint8_t * buf, size_t size); @@ -132,6 +132,8 @@ namespace transport void DecryptSessionKey (uint8_t * buf, size_t len); bool Validate (uint8_t * buf, size_t len, const i2p::crypto::MACKey& macKey); + void Reset (); + private: friend class SSUData; // TODO: change in later From 0e8d80e055e4d72bbf2d57b989c4dfb0803a480b Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 7 Mar 2017 14:24:19 -0500 Subject: [PATCH 05/39] eliminate some BIGNUM allocation overhead --- Crypto.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Crypto.cpp b/Crypto.cpp index 4e8673d2..a110c38f 100644 --- a/Crypto.cpp +++ b/Crypto.cpp @@ -275,8 +275,13 @@ namespace crypto void ElGamalEncrypt (const uint8_t * key, const uint8_t * data, uint8_t * encrypted, bool zeroPadding) { BN_CTX * ctx = BN_CTX_new (); + BN_CTX_start (ctx); + // everything, but a, because a might come from table + BIGNUM * k = BN_CTX_get (ctx); + BIGNUM * y = BN_CTX_get (ctx); + BIGNUM * b1 = BN_CTX_get (ctx); + BIGNUM * b = BN_CTX_get (ctx); // select random k - BIGNUM * k = BN_new (); #if defined(__x86_64__) BN_rand (k, ELGAMAL_FULL_EXPONENT_NUM_BITS, -1, 1); // full exponent for x64 #else @@ -292,23 +297,18 @@ namespace crypto BN_mod_exp (a, elgg, k, elgp, ctx); } - BIGNUM * y = BN_new (); + // restore y from key BN_bin2bn (key, 256, y); // calculate b1 - BIGNUM * b1 = BN_new (); BN_mod_exp (b1, y, k, elgp, ctx); - BN_free (y); - BN_free (k); // create m uint8_t m[255]; m[0] = 0xFF; memcpy (m+33, data, 222); SHA256 (m+33, 222, m+1); // calculate b = b1*m mod p - BIGNUM * b = BN_new (); BN_bin2bn (m, 255, b); BN_mod_mul (b, b1, b, elgp, ctx); - BN_free (b1); // copy a and b if (zeroPadding) { @@ -322,8 +322,8 @@ namespace crypto bn2buf (a, encrypted, 256); bn2buf (b, encrypted + 256, 256); } - BN_free (b); BN_free (a); + BN_CTX_end (ctx); BN_CTX_free (ctx); } From 955b46534d83a157efd74b240cdc287e9044dc3f Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 7 Mar 2017 14:51:05 -0500 Subject: [PATCH 06/39] eliminate some BIGNUM allocation overhead --- Crypto.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Crypto.cpp b/Crypto.cpp index a110c38f..9ca5928c 100644 --- a/Crypto.cpp +++ b/Crypto.cpp @@ -331,7 +331,8 @@ namespace crypto uint8_t * data, bool zeroPadding) { BN_CTX * ctx = BN_CTX_new (); - BIGNUM * x = BN_new (), * a = BN_new (), * b = BN_new (); + BN_CTX_start (ctx); + BIGNUM * x = BN_CTX_get (ctx), * a = BN_CTX_get (ctx), * b = BN_CTX_get (ctx); BN_bin2bn (key, 256, x); BN_sub (x, elgp, x); BN_sub_word (x, 1); // x = elgp - x- 1 BN_bin2bn (zeroPadding ? encrypted + 1 : encrypted, 256, a); @@ -341,7 +342,7 @@ namespace crypto BN_mod_mul (b, b, x, elgp, ctx); uint8_t m[255]; bn2buf (b, m, 255); - BN_free (x); BN_free (a); BN_free (b); + BN_CTX_end (ctx); BN_CTX_free (ctx); uint8_t hash[32]; SHA256 (m + 33, 222, hash); From 044d6a220738310888439793c17f2dc01a0beef8 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 7 Mar 2017 15:02:01 -0500 Subject: [PATCH 07/39] eliminate some BIGNUM allocation overhead --- Signature.cpp | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/Signature.cpp b/Signature.cpp index c38b1333..7571760a 100644 --- a/Signature.cpp +++ b/Signature.cpp @@ -192,7 +192,8 @@ namespace crypto BN_one (z3); // D = 1 } - BIGNUM * E = BN_new (), * F = BN_new (), * G = BN_new (), * H = BN_new (); + BN_CTX_start (ctx); + BIGNUM * E = BN_CTX_get (ctx), * F = BN_CTX_get (ctx), * G = BN_CTX_get (ctx), * H = BN_CTX_get (ctx); BN_add (E, p1.x, p1.y); BN_add (F, p2.x, p2.y); BN_mul (E, E, F, ctx); // (x1 + y1)*(x2 + y2) @@ -207,7 +208,7 @@ namespace crypto BN_mod_mul (z3, F, G, q, ctx); // z3 = F*G BN_mod_mul (t3, E, H, q, ctx); // t3 = E*H - BN_free (E); BN_free (F); BN_free (G); BN_free (H); + BN_CTX_end (ctx); return EDDSAPoint {x3, y3, z3, t3}; } @@ -231,7 +232,8 @@ namespace crypto else BN_one (z2); // z2 = 1 - BIGNUM * E = BN_new (), * F = BN_new (), * G = BN_new (), * H = BN_new (); + BN_CTX_start (ctx); + BIGNUM * E = BN_CTX_get (ctx), * F = BN_CTX_get (ctx), * G = BN_CTX_get (ctx), * H = BN_CTX_get (ctx); // E = (x+y)*(x+y)-A-B = x^2+y^2+2xy-A-B = 2xy BN_mul (E, p.x, p.y, ctx); BN_lshift1 (E, E); // E =2*x*y @@ -244,7 +246,7 @@ namespace crypto BN_mod_mul (z2, F, G, q, ctx); // z2 = F*G BN_mod_mul (t2, E, H, q, ctx); // t2 = E*H - BN_free (E); BN_free (F); BN_free (G); BN_free (H); + BN_CTX_end (ctx); return EDDSAPoint {x2, y2, z2, t2}; } @@ -316,12 +318,11 @@ namespace crypto bool IsOnCurve (const EDDSAPoint& p, BN_CTX * ctx) const { - BIGNUM * x2 = BN_new (); + BN_CTX_start (ctx); + BIGNUM * x2 = BN_CTX_get (ctx), * y2 = BN_CTX_get (ctx), * tmp = BN_CTX_get (ctx); BN_sqr (x2, p.x, ctx); // x^2 - BIGNUM * y2 = BN_new (); BN_sqr (y2, p.y, ctx); // y^2 - // y^2 - x^2 - 1 - d*x^2*y^2 - BIGNUM * tmp = BN_new (); + // y^2 - x^2 - 1 - d*x^2*y^2 BN_mul (tmp, d, x2, ctx); BN_mul (tmp, tmp, y2, ctx); BN_sub (tmp, y2, tmp); @@ -329,18 +330,16 @@ namespace crypto BN_sub_word (tmp, 1); BN_mod (tmp, tmp, q, ctx); // % q bool ret = BN_is_zero (tmp); - BN_free (x2); - BN_free (y2); - BN_free (tmp); + BN_CTX_end (ctx); return ret; } BIGNUM * RecoverX (const BIGNUM * y, BN_CTX * ctx) const { - BIGNUM * y2 = BN_new (); + BN_CTX_start (ctx); + BIGNUM * y2 = BN_CTX_get (ctx), * xx = BN_CTX_get (ctx); BN_sqr (y2, y, ctx); // y^2 // xx = (y^2 -1)*inv(d*y^2 +1) - BIGNUM * xx = BN_new (); BN_mul (xx, d, y2, ctx); BN_add_word (xx, 1); BN_mod_inverse (xx, xx, q, ctx); @@ -356,8 +355,6 @@ namespace crypto BN_mod_mul (x, x, I, q, ctx); if (BN_is_odd (x)) BN_sub (x, q, x); - BN_free (y2); - BN_free (xx); return x; } From 5dfb7cb938e84471462b28e1b085ab21789bcc8e Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 7 Mar 2017 16:36:17 -0500 Subject: [PATCH 08/39] GOST R 34.10 curve added --- Signature.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Signature.cpp b/Signature.cpp index 7571760a..72343761 100644 --- a/Signature.cpp +++ b/Signature.cpp @@ -492,6 +492,36 @@ namespace crypto { GetEd25519 ()->Sign (m_ExpandedPrivateKey, m_PublicKeyEncoded, buf, len, signature); } + +//---------------------------------------------- +// GOST + + class GOSTR3410 + { + public: + + GOSTR3410 (BIGNUM * a, BIGNUM * b, BIGNUM * p, BIGNUM * q, BIGNUM * x, BIGNUM * y) + { + BN_CTX * ctx = BN_CTX_new (); + m_Curve = EC_GROUP_new_curve_GFp (p, a, b, ctx); + EC_POINT * P = EC_POINT_new (m_Curve); + EC_POINT_set_affine_coordinates_GFp (m_Curve, P, x, y, ctx); + EC_GROUP_set_generator (m_Curve, P, q, nullptr); + EC_GROUP_set_curve_name (m_Curve, NID_id_GostR3410_2001); + EC_POINT_free(P); + BN_CTX_free (ctx); + } + + ~GOSTR3410 () + { + EC_GROUP_free (m_Curve); + } + + private: + + EC_GROUP * m_Curve; + }; + } } From 831c835106cb89dfdbe5c0a913f85be405968458 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 8 Mar 2017 19:35:51 -0500 Subject: [PATCH 09/39] GOST R 34.10 param sets --- Signature.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++------- Signature.h | 12 ++++++++ 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/Signature.cpp b/Signature.cpp index 72343761..24f84c20 100644 --- a/Signature.cpp +++ b/Signature.cpp @@ -1,4 +1,5 @@ #include +#include #include "Log.h" #include "Signature.h" @@ -496,32 +497,89 @@ namespace crypto //---------------------------------------------- // GOST - class GOSTR3410 + class GOSTR3410Curve { public: - GOSTR3410 (BIGNUM * a, BIGNUM * b, BIGNUM * p, BIGNUM * q, BIGNUM * x, BIGNUM * y) + GOSTR3410Curve (BIGNUM * a, BIGNUM * b, BIGNUM * p, BIGNUM * q, BIGNUM * x, BIGNUM * y) { BN_CTX * ctx = BN_CTX_new (); - m_Curve = EC_GROUP_new_curve_GFp (p, a, b, ctx); - EC_POINT * P = EC_POINT_new (m_Curve); - EC_POINT_set_affine_coordinates_GFp (m_Curve, P, x, y, ctx); - EC_GROUP_set_generator (m_Curve, P, q, nullptr); - EC_GROUP_set_curve_name (m_Curve, NID_id_GostR3410_2001); + m_Group = EC_GROUP_new_curve_GFp (p, a, b, ctx); + EC_POINT * P = EC_POINT_new (m_Group); + EC_POINT_set_affine_coordinates_GFp (m_Group, P, x, y, ctx); + EC_GROUP_set_generator (m_Group, P, q, nullptr); + EC_GROUP_set_curve_name (m_Group, NID_id_GostR3410_2001); EC_POINT_free(P); BN_CTX_free (ctx); } - ~GOSTR3410 () + ~GOSTR3410Curve () { - EC_GROUP_free (m_Curve); + EC_GROUP_free (m_Group); } private: - EC_GROUP * m_Curve; + EC_GROUP * m_Group; }; + GOSTR3410Curve * CreateGOSTR3410Curve (GOSTR3410ParamSet paramSet) + { + // a, b, p, q, x, y + static const char * params[eGOSTR3410NumParamSets][6] = + { + { + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD94", + "A6", + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD97", + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF6C611070995AD10045841B09B761B893", + "1", + "8D91E471E0989CDA27DF505A453F2B7635294F2DDF23E3B122ACC99C9E9F1E14" + }, // A + { + "8000000000000000000000000000000000000000000000000000000000000C96", + "3E1AF419A269A5F866A7D3C25C3DF80AE979259373FF2B182F49D4CE7E1BBC8B", + "8000000000000000000000000000000000000000000000000000000000000C99", + "800000000000000000000000000000015F700CFFF1A624E5E497161BCC8A198F", + "1", + "3FA8124359F96680B83D1C3EB2C070E5C545C9858D03ECFB744BF8D717717EFC" + }, // B + { + "9B9F605F5A858107AB1EC85E6B41C8AACF846E86789051D37998F7B9022D7598", + "805A", + "9B9F605F5A858107AB1EC85E6B41C8AACF846E86789051D37998F7B9022D759B", + "9B9F605F5A858107AB1EC85E6B41C8AA582CA3511EDDFB74F02F3A6598980BB9", + "0", + "41ECE55743711A8C3CBF3783CD08C0EE4D4DC440D4641A8F366E550DFDB3BB67" + } // C + }; + + BIGNUM * a = nullptr, * b = nullptr, * p = nullptr, * q =nullptr, * x = nullptr, * y = nullptr; + BN_hex2bn(&a, params[paramSet][0]); + BN_hex2bn(&b, params[paramSet][1]); + BN_hex2bn(&p, params[paramSet][2]); + BN_hex2bn(&q, params[paramSet][3]); + BN_hex2bn(&x, params[paramSet][4]); + BN_hex2bn(&y, params[paramSet][5]); + auto curve = new GOSTR3410Curve (a, b, p, q, x, y); + BN_free (a); BN_free (b); BN_free (p); BN_free (q); BN_free (x); BN_free (y); + return curve; + } + + static std::array, eGOSTR3410NumParamSets> g_GOSTR3410Curves; + std::unique_ptr& GetGOSTR3410Curve (GOSTR3410ParamSet paramSet) + { + if (!g_GOSTR3410Curves[paramSet]) + { + auto c = CreateGOSTR3410Curve (paramSet); + if (!g_GOSTR3410Curves[paramSet]) // make sure it was not created already + g_GOSTR3410Curves[paramSet].reset (c); + else + delete c; + } + return g_GOSTR3410Curves[paramSet]; + } + } } diff --git a/Signature.h b/Signature.h index d8c04814..6c0ca76e 100644 --- a/Signature.h +++ b/Signature.h @@ -443,6 +443,18 @@ namespace crypto } // ГОСТ Р 34.10-2001 + + enum GOSTR3410ParamSet + { + eGOSTR3410CryptoProA = 0, // 1.2.643.2.2.35.1 + eGOSTR3410CryptoProB, // 1.2.643.2.2.35.2 + eGOSTR3410CryptoProC, // 1.2.643.2.2.35.3 + //eGOSTR3410CryptoProXchA, // 1.2.643.2.2.36.0 + //eGOSTR3410CryptoProXchB, // 1.2.643.2.2.36.1 + // XchA = A, XchB = C + eGOSTR3410NumParamSets + }; + const size_t GOSTR3410_PUBLIC_KEY_LENGTH = 64; const size_t GOSTR3410_SIGNATURE_LENGTH = 64; From 7f71d5dbd880a36d1756f12882dbb6c74ac0d5c3 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 9 Mar 2017 18:46:28 -0500 Subject: [PATCH 10/39] generate GOST R 34.10 keys with param set --- Signature.cpp | 32 ++++++++++++++++++++++++++++++-- Signature.h | 16 ++-------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/Signature.cpp b/Signature.cpp index 24f84c20..2e83e91c 100644 --- a/Signature.cpp +++ b/Signature.cpp @@ -515,9 +515,23 @@ namespace crypto ~GOSTR3410Curve () { - EC_GROUP_free (m_Group); + EC_GROUP_free (m_Group); } + EC_POINT * MulP (const BIGNUM * n) const + { + BN_CTX * ctx = BN_CTX_new (); + auto p = EC_POINT_new (m_Group); + EC_POINT_mul (m_Group, p, n, nullptr, nullptr, ctx); + BN_CTX_free (ctx); + return p; + } + + bool GetXY (const EC_POINT * p, BIGNUM * x, BIGNUM * y) const + { + return EC_POINT_get_affine_coordinates_GFp (m_Group, p, x, y, nullptr); + } + private: EC_GROUP * m_Group; @@ -579,7 +593,21 @@ namespace crypto } return g_GOSTR3410Curves[paramSet]; } - + + void CreateGOSTR3410RandomKeys (GOSTR3410ParamSet paramSet, uint8_t * signingPrivateKey, uint8_t * signingPublicKey) + { + RAND_bytes (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); + BIGNUM * priv = BN_bin2bn (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, nullptr); + const auto& curve = GetGOSTR3410Curve (paramSet); + auto pub = curve->MulP (priv); + BN_free (priv); + BIGNUM * x = BN_new (), * y = BN_new (); + curve->GetXY (pub, x, y); + EC_POINT_free (pub); + bn2buf (x, signingPublicKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); + bn2buf (y, signingPublicKey + GOSTR3410_PUBLIC_KEY_LENGTH/2, GOSTR3410_PUBLIC_KEY_LENGTH/2); + BN_free (x); BN_free (y); + } } } diff --git a/Signature.h b/Signature.h index 6c0ca76e..204102e8 100644 --- a/Signature.h +++ b/Signature.h @@ -524,22 +524,10 @@ namespace crypto EVP_PKEY * m_PrivateKey; }; + void CreateGOSTR3410RandomKeys (GOSTR3410ParamSet paramSet, uint8_t * signingPrivateKey, uint8_t * signingPublicKey); inline void CreateGOSTR3410RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey) { - auto ctx = EVP_PKEY_CTX_new_id(NID_id_GostR3410_2001, nullptr); - EVP_PKEY_keygen_init (ctx); - EVP_PKEY_CTX_ctrl_str (ctx, "paramset", "A"); // TODO should be in one place - EVP_PKEY* pkey = nullptr; - EVP_PKEY_keygen (ctx, &pkey); - const EC_KEY* ecKey = (const EC_KEY*) EVP_PKEY_get0(pkey); - bn2buf (EC_KEY_get0_private_key (ecKey), signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); - BIGNUM * x = BN_new(), * y = BN_new(); - EC_POINT_get_affine_coordinates_GFp (EC_KEY_get0_group(ecKey), EC_KEY_get0_public_key (ecKey), x, y, NULL); - bn2buf (x, signingPublicKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); - bn2buf (y, signingPublicKey + GOSTR3410_PUBLIC_KEY_LENGTH/2, GOSTR3410_PUBLIC_KEY_LENGTH/2); - BN_free (x); BN_free (y); - EVP_PKEY_CTX_free (ctx); - EVP_PKEY_free (pkey); + CreateGOSTR3410RandomKeys (eGOSTR3410CryptoProA, signingPrivateKey, signingPublicKey); // A by default } } } From 439c2d445ce0672460f8c22696bf2fa86dffead2 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 9 Mar 2017 20:13:21 -0500 Subject: [PATCH 11/39] GOST R 34.10 sign --- Signature.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Signature.cpp b/Signature.cpp index 2e83e91c..72826aa9 100644 --- a/Signature.cpp +++ b/Signature.cpp @@ -531,6 +531,26 @@ namespace crypto { return EC_POINT_get_affine_coordinates_GFp (m_Group, p, x, y, nullptr); } + + void Sign (const BIGNUM * priv, const BIGNUM * digest, BIGNUM * r, BIGNUM * s) + { + BN_CTX * ctx = BN_CTX_new (); + BN_CTX_start (ctx); + BIGNUM * q = BN_CTX_get (ctx); + EC_GROUP_get_order(m_Group, q, ctx); + BIGNUM * k = BN_CTX_get (ctx); + BN_rand_range (k, q); // 0 < k < q + EC_POINT * C = MulP (k); // C = k*P + GetXY (C, r, nullptr); // r = Cx + EC_POINT_free (C); + BN_mod_mul (s, r, priv, q, ctx); // (r*priv)%q + BIGNUM * tmp = BN_CTX_get (ctx); + BN_mod_mul (tmp, k, digest, q, ctx); // (k*digest)%q + BN_mod_add (s, s, tmp, q, ctx); // (r*priv+k*digest)%q + BN_CTX_end (ctx); + BN_CTX_free (ctx); + } + private: From cd860bfbf8b0427dd9633601382f087cade9ef21 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 9 Mar 2017 21:46:34 -0500 Subject: [PATCH 12/39] correct param set for GOST R 34.10 signing --- Identity.cpp | 4 ++-- Signature.cpp | 13 +++++++++++++ Signature.h | 29 +++++++---------------------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/Identity.cpp b/Identity.cpp index aecb11cc..f76bc62b 100644 --- a/Identity.cpp +++ b/Identity.cpp @@ -525,7 +525,7 @@ namespace data m_Signer.reset (new i2p::crypto::EDDSA25519Signer (m_SigningPrivateKey, m_Public->GetStandardIdentity ().certificate - i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH)); break; case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: - m_Signer.reset (new i2p::crypto::GOSTR3410Signer (m_SigningPrivateKey)); + m_Signer.reset (new i2p::crypto::GOSTR3410Signer (i2p::crypto::eGOSTR3410CryptoProA, m_SigningPrivateKey)); break; default: LogPrint (eLogError, "Identity: Signing key type ", (int)m_Public->GetSigningKeyType (), " is not supported"); @@ -563,7 +563,7 @@ namespace data i2p::crypto::CreateEDDSA25519RandomKeys (keys.m_SigningPrivateKey, signingPublicKey); break; case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: - i2p::crypto::CreateGOSTR3410RandomKeys (keys.m_SigningPrivateKey, signingPublicKey); + i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410CryptoProA, keys.m_SigningPrivateKey, signingPublicKey); break; default: LogPrint (eLogError, "Identity: Signing key type ", (int)type, " is not supported. Create DSA-SHA1"); diff --git a/Signature.cpp b/Signature.cpp index 72826aa9..30e4fc03 100644 --- a/Signature.cpp +++ b/Signature.cpp @@ -614,6 +614,19 @@ namespace crypto return g_GOSTR3410Curves[paramSet]; } + void GOSTR3410Signer::Sign (const uint8_t * buf, int len, uint8_t * signature) const + { + uint8_t digest[32]; + GOSTR3411 (buf, len, digest); + BIGNUM * d = BN_bin2bn (digest, 32, nullptr); + BIGNUM * r = BN_new (), * s = BN_new (); + const auto& curve = GetGOSTR3410Curve (m_ParamSet); + curve->Sign (m_PrivateKey, d, r, s); + bn2buf (r, signature, GOSTR3410_SIGNATURE_LENGTH/2); + bn2buf (s, signature + GOSTR3410_SIGNATURE_LENGTH/2, GOSTR3410_SIGNATURE_LENGTH/2); + BN_free (d); BN_free (r); BN_free (s); + } + void CreateGOSTR3410RandomKeys (GOSTR3410ParamSet paramSet, uint8_t * signingPrivateKey, uint8_t * signingPublicKey) { RAND_bytes (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); diff --git a/Signature.h b/Signature.h index 204102e8..c505524c 100644 --- a/Signature.h +++ b/Signature.h @@ -498,37 +498,22 @@ namespace crypto { public: - GOSTR3410Signer (const uint8_t * signingPrivateKey) + GOSTR3410Signer (GOSTR3410ParamSet paramSet, const uint8_t * signingPrivateKey): + m_ParamSet (paramSet) { - m_PrivateKey = EVP_PKEY_new (); - EC_KEY * ecKey = EC_KEY_new (); - EVP_PKEY_assign (m_PrivateKey, NID_id_GostR3410_2001, ecKey); - EVP_PKEY_copy_parameters (m_PrivateKey, GetGostPKEY ()); - EC_KEY_set_private_key (ecKey, BN_bin2bn (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, NULL)); + m_PrivateKey = BN_bin2bn (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, nullptr); } - ~GOSTR3410Signer () { EVP_PKEY_free (m_PrivateKey); } + ~GOSTR3410Signer () { BN_free (m_PrivateKey); } - void Sign (const uint8_t * buf, int len, uint8_t * signature) const - { - uint8_t digest[32]; - GOSTR3411 (buf, len, digest); - EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new (m_PrivateKey, nullptr); - EVP_PKEY_sign_init (ctx); - size_t l = GOSTR3410_SIGNATURE_LENGTH; - EVP_PKEY_sign (ctx, signature, &l, digest, 32); - EVP_PKEY_CTX_free (ctx); - } + void Sign (const uint8_t * buf, int len, uint8_t * signature) const; private: - EVP_PKEY * m_PrivateKey; + GOSTR3410ParamSet m_ParamSet; + BIGNUM * m_PrivateKey; }; void CreateGOSTR3410RandomKeys (GOSTR3410ParamSet paramSet, uint8_t * signingPrivateKey, uint8_t * signingPublicKey); - inline void CreateGOSTR3410RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey) - { - CreateGOSTR3410RandomKeys (eGOSTR3410CryptoProA, signingPrivateKey, signingPublicKey); // A by default - } } } From 1bed3f393671d91a986643cf7df44568fe7f8d84 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 10 Mar 2017 11:53:05 -0500 Subject: [PATCH 13/39] ZEC added --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a2c7d23f..6bfb1b78 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ Donations --------- BTC: 1K7Ds6KUeR8ya287UC4rYTjvC96vXyZbDY +ZEC: t1cTckLuXsr1dwVrK4NDzfhehss4NvMadAJ DASH: Xw8YUrQpYzP9tZBmbjqxS3M97Q7v3vJKUF LTC: LKQirrYrDeTuAPnpYq5y7LVKtywfkkHi59 ANC: AQJYweYYUqM1nVfLqfoSMpUMfzxvS4Xd7z From 084e48d6dd8b5c28bf807006e8017790125942c2 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 10 Mar 2017 11:53:49 -0500 Subject: [PATCH 14/39] ZEC added --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6bfb1b78..68b397a4 100644 --- a/README.md +++ b/README.md @@ -61,12 +61,12 @@ See [documentation](https://i2pd.readthedocs.io/en/latest/user-guide/run/) and Donations --------- -BTC: 1K7Ds6KUeR8ya287UC4rYTjvC96vXyZbDY -ZEC: t1cTckLuXsr1dwVrK4NDzfhehss4NvMadAJ -DASH: Xw8YUrQpYzP9tZBmbjqxS3M97Q7v3vJKUF +BTC: 1K7Ds6KUeR8ya287UC4rYTjvC96vXyZbDY +ZEC: t1cTckLuXsr1dwVrK4NDzfhehss4NvMadAJ +DASH: Xw8YUrQpYzP9tZBmbjqxS3M97Q7v3vJKUF LTC: LKQirrYrDeTuAPnpYq5y7LVKtywfkkHi59 ANC: AQJYweYYUqM1nVfLqfoSMpUMfzxvS4Xd7z -DOGE: DNXLQKziRPAsD9H3DFNjk4fLQrdaSX893Y +DOGE: DNXLQKziRPAsD9H3DFNjk4fLQrdaSX893Y License ------- From ba89c60b6deaa2eb5a64cd5051919e1b0997b8b3 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 10 Mar 2017 11:55:30 -0500 Subject: [PATCH 15/39] correct param set for GOST R 34.10 verification --- Identity.cpp | 6 +++--- Signature.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++-- Signature.h | 28 +++++--------------------- 3 files changed, 62 insertions(+), 28 deletions(-) diff --git a/Identity.cpp b/Identity.cpp index f76bc62b..fb45764d 100644 --- a/Identity.cpp +++ b/Identity.cpp @@ -97,14 +97,14 @@ namespace data } case SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519: { - size_t padding = 128 - i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH; // 96 = 128 - 32 + size_t padding = 128 - i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH; // 96 = 128 - 32 RAND_bytes (m_StandardIdentity.signingKey, padding); memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH); break; } case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: { - size_t padding = 128 - i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 + size_t padding = 128 - i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 RAND_bytes (m_StandardIdentity.signingKey, padding); memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH); break; @@ -380,7 +380,7 @@ namespace data case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: { size_t padding = 128 - i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 - UpdateVerifier (new i2p::crypto::GOSTR3410Verifier (m_StandardIdentity.signingKey + padding)); + UpdateVerifier (new i2p::crypto::GOSTR3410Verifier (i2p::crypto::eGOSTR3410CryptoProA, m_StandardIdentity.signingKey + padding)); break; } default: diff --git a/Signature.cpp b/Signature.cpp index 30e4fc03..15d606dc 100644 --- a/Signature.cpp +++ b/Signature.cpp @@ -532,6 +532,13 @@ namespace crypto return EC_POINT_get_affine_coordinates_GFp (m_Group, p, x, y, nullptr); } + EC_POINT * CreatePoint (const BIGNUM * x, const BIGNUM * y) const + { + EC_POINT * p = EC_POINT_new (m_Group); + EC_POINT_set_affine_coordinates_GFp (m_Group, p, x, y, nullptr); + return p; + } + void Sign (const BIGNUM * priv, const BIGNUM * digest, BIGNUM * r, BIGNUM * s) { BN_CTX * ctx = BN_CTX_new (); @@ -551,6 +558,31 @@ namespace crypto BN_CTX_free (ctx); } + bool Verify (const EC_POINT * pub, const BIGNUM * digest, const BIGNUM * r, const BIGNUM * s) + { + BN_CTX * ctx = BN_CTX_new (); + BN_CTX_start (ctx); + BIGNUM * q = BN_CTX_get (ctx); + EC_GROUP_get_order(m_Group, q, ctx); + BIGNUM * h = BN_CTX_get (ctx); + BN_mod (h, digest, q, ctx); // h = digest % q + BN_mod_inverse (h, h, q, ctx); // 1/h mod q + BIGNUM * z1 = BN_CTX_get (ctx); + BN_mod_mul (z1, s, h, q, ctx); // z1 = s/h + BIGNUM * z2 = BN_CTX_get (ctx); + BN_sub (z2, q, r); // z2 = -r + BN_mod_mul (z2, z2, h, q, ctx); // z2 = -r/h + EC_POINT * C = EC_POINT_new (m_Group); + EC_POINT_mul (m_Group, C, z1, pub, z2, ctx); // z1*P + z2*pub + BIGNUM * x = BN_CTX_get (ctx); + GetXY (C, x, nullptr); // Cx + BN_mod (x, x, q, ctx); // Cx % q + bool ret = !BN_cmp (x, r); // Cx = r ? + EC_POINT_free (C); + BN_CTX_end (ctx); + BN_CTX_free (ctx); + return ret; + } private: @@ -614,14 +646,34 @@ namespace crypto return g_GOSTR3410Curves[paramSet]; } + GOSTR3410Verifier::GOSTR3410Verifier (GOSTR3410ParamSet paramSet, const uint8_t * signingKey): + m_ParamSet (paramSet) + { + BIGNUM * x = BN_bin2bn (signingKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, NULL); + BIGNUM * y = BN_bin2bn (signingKey + GOSTR3410_PUBLIC_KEY_LENGTH/2, GOSTR3410_PUBLIC_KEY_LENGTH/2, NULL); + m_PublicKey = GetGOSTR3410Curve (m_ParamSet)->CreatePoint (x, y); + BN_free (x); BN_free (y); + } + + bool GOSTR3410Verifier::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const + { + uint8_t digest[32]; + GOSTR3411 (buf, len, digest); + BIGNUM * d = BN_bin2bn (digest, 32, nullptr); + BIGNUM * r = BN_bin2bn (signature, GetSignatureLen ()/2, NULL); + BIGNUM * s = BN_bin2bn (signature + GetSignatureLen ()/2, GetSignatureLen ()/2, NULL); + bool ret = GetGOSTR3410Curve (m_ParamSet)->Verify (m_PublicKey, d, r, s); + BN_free (d); BN_free (r); BN_free (s); + return ret; + } + void GOSTR3410Signer::Sign (const uint8_t * buf, int len, uint8_t * signature) const { uint8_t digest[32]; GOSTR3411 (buf, len, digest); BIGNUM * d = BN_bin2bn (digest, 32, nullptr); BIGNUM * r = BN_new (), * s = BN_new (); - const auto& curve = GetGOSTR3410Curve (m_ParamSet); - curve->Sign (m_PrivateKey, d, r, s); + GetGOSTR3410Curve (m_ParamSet)->Sign (m_PrivateKey, d, r, s); bn2buf (r, signature, GOSTR3410_SIGNATURE_LENGTH/2); bn2buf (s, signature + GOSTR3410_SIGNATURE_LENGTH/2, GOSTR3410_SIGNATURE_LENGTH/2); BN_free (d); BN_free (r); BN_free (s); diff --git a/Signature.h b/Signature.h index c505524c..0ccb99ea 100644 --- a/Signature.h +++ b/Signature.h @@ -462,36 +462,18 @@ namespace crypto { public: - GOSTR3410Verifier (const uint8_t * signingKey) - { - m_PublicKey = EVP_PKEY_new (); - EC_KEY * ecKey = EC_KEY_new (); - EVP_PKEY_assign (m_PublicKey, NID_id_GostR3410_2001, ecKey); - EVP_PKEY_copy_parameters (m_PublicKey, GetGostPKEY ()); - BIGNUM * x = BN_bin2bn (signingKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, NULL); - BIGNUM * y = BN_bin2bn (signingKey + GOSTR3410_PUBLIC_KEY_LENGTH/2, GOSTR3410_PUBLIC_KEY_LENGTH/2, NULL); - EC_KEY_set_public_key_affine_coordinates (ecKey, x, y); - BN_free (x); BN_free (y); - } - ~GOSTR3410Verifier () { EVP_PKEY_free (m_PublicKey); } + GOSTR3410Verifier (GOSTR3410ParamSet paramSet, const uint8_t * signingKey); + ~GOSTR3410Verifier () { EC_POINT_free (m_PublicKey); } - bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const - { - uint8_t digest[32]; - GOSTR3411 (buf, len, digest); - EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new (m_PublicKey, nullptr); - EVP_PKEY_verify_init (ctx); - int ret = EVP_PKEY_verify (ctx, signature, GOSTR3410_SIGNATURE_LENGTH, digest, 32); - EVP_PKEY_CTX_free (ctx); - return ret == 1; - } + bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const; size_t GetPublicKeyLen () const { return GOSTR3410_PUBLIC_KEY_LENGTH; } size_t GetSignatureLen () const { return GOSTR3410_SIGNATURE_LENGTH; } private: - EVP_PKEY * m_PublicKey; + GOSTR3410ParamSet m_ParamSet; + EC_POINT * m_PublicKey; }; class GOSTR3410Signer: public Signer From 7caf3ea7d0c716b7e123f3734ddca77196fd84a6 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 10 Mar 2017 13:21:22 -0500 Subject: [PATCH 16/39] eliminate dependancy from GOST engine for GOST R 34.10 --- Crypto.cpp | 61 ++------------------------------------ Crypto.h | 3 -- Daemon.cpp | 2 -- android/jni/Android.mk | 7 ----- android/jni/Application.mk | 1 - 5 files changed, 3 insertions(+), 71 deletions(-) diff --git a/Crypto.cpp b/Crypto.cpp index 9ca5928c..791968d3 100644 --- a/Crypto.cpp +++ b/Crypto.cpp @@ -802,70 +802,15 @@ namespace crypto } }*/ - static ENGINE * g_GostEngine = nullptr; - static const EVP_MD * g_Gost3411 = nullptr; - static EVP_PKEY * g_GostPKEY = nullptr; - - const EVP_PKEY * GetGostPKEY () - { - return g_GostPKEY; - } uint8_t * GOSTR3411 (const uint8_t * buf, size_t len, uint8_t * digest) { - if (!g_Gost3411) return nullptr; - auto ctx = EVP_MD_CTX_new (); - EVP_DigestInit_ex (ctx, g_Gost3411, g_GostEngine); - EVP_DigestUpdate (ctx, buf, len); - EVP_DigestFinal_ex (ctx, digest, nullptr); - EVP_MD_CTX_free (ctx); + // TODO: implement actual GOST R 34.11 + // SHA-256 is used for testing only + SHA256 (buf, len, digest); return digest; } - bool InitGost () - { -#ifndef OPENSSL_NO_ENGINE -#if (OPENSSL_VERSION_NUMBER < 0x010100000) || defined(LIBRESSL_VERSION_NUMBER) - ENGINE_load_builtin_engines (); - ENGINE_load_dynamic (); -#else - OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN, NULL); -#endif - g_GostEngine = ENGINE_by_id ("gost"); - if (!g_GostEngine) return false; - - ENGINE_init (g_GostEngine); - ENGINE_set_default (g_GostEngine, ENGINE_METHOD_ALL); - g_Gost3411 = ENGINE_get_digest(g_GostEngine, NID_id_GostR3411_94); - - auto ctx = EVP_PKEY_CTX_new_id(NID_id_GostR3410_2001, g_GostEngine); - if (!ctx) return false; - EVP_PKEY_keygen_init (ctx); - EVP_PKEY_CTX_ctrl_str (ctx, "paramset", "A"); // possible values 'A', 'B', 'C', 'XA', 'XB' - EVP_PKEY_keygen (ctx, &g_GostPKEY); // it seems only way to fill with correct params - EVP_PKEY_CTX_free (ctx); - return true; -#else - LogPrint (eLogError, "Can't initialize GOST. Engines are not supported"); - return false; -#endif - } - - void TerminateGost () - { - if (g_GostPKEY) - EVP_PKEY_free (g_GostPKEY); -#ifndef OPENSSL_NO_ENGINE - if (g_GostEngine) - { - ENGINE_finish (g_GostEngine); - ENGINE_free (g_GostEngine); -#if (OPENSSL_VERSION_NUMBER < 0x010100000) || defined(LIBRESSL_VERSION_NUMBER) - ENGINE_cleanup(); -#endif - } -#endif - } void InitCrypto (bool precomputation, bool withGost) { diff --git a/Crypto.h b/Crypto.h index fca7b073..748a7f48 100644 --- a/Crypto.h +++ b/Crypto.h @@ -280,9 +280,6 @@ namespace crypto }; // GOST - bool InitGost (); - void TerminateGost (); - const EVP_PKEY * GetGostPKEY (); uint8_t * GOSTR3411 (const uint8_t * buf, size_t len, uint8_t * digest); // hash void InitCrypto (bool precomputation, bool withGost = false); diff --git a/Daemon.cpp b/Daemon.cpp index 74d3f859..c7aaa279 100644 --- a/Daemon.cpp +++ b/Daemon.cpp @@ -122,7 +122,6 @@ namespace i2p i2p::crypto::InitCrypto (precomputation); int netID; i2p::config::GetOption("netid", netID); - if (netID != 2) i2p::crypto::InitGost (); // init GOST for own darknet i2p::context.SetNetID (netID); i2p::context.Init (); @@ -350,7 +349,6 @@ namespace i2p d.m_WebsocketServer = nullptr; } #endif - if (i2p::context.GetNetID () != 2) i2p::crypto::TerminateGost (); i2p::crypto::TerminateCrypto (); i2p::log::Logger().Stop(); diff --git a/android/jni/Android.mk b/android/jni/Android.mk index ad2592ee..1bba69f0 100755 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -109,13 +109,6 @@ LOCAL_EXPORT_C_INCLUDES := $(OPENSSL_PATH)/openssl-1.1.0e/include LOCAL_STATIC_LIBRARIES := crypto include $(PREBUILT_STATIC_LIBRARY) -LOCAL_PATH := $(call my-dir) -include $(CLEAR_VARS) -LOCAL_MODULE := gost_engine -LOCAL_SRC_FILES := $(GOST_PATH)/gost-engine/$(TARGET_ARCH_ABI)/lib/libgost.a -LOCAL_EXPORT_C_INCLUDES := $(GOST_PATH)/gost-engine/include -include $(PREBUILT_STATIC_LIBRARY) - LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := miniupnpc diff --git a/android/jni/Application.mk b/android/jni/Application.mk index ee031efc..e8a51add 100755 --- a/android/jni/Application.mk +++ b/android/jni/Application.mk @@ -30,4 +30,3 @@ BOOST_PATH = $(I2PD_LIBS_PATH)/Boost-for-Android-Prebuilt OPENSSL_PATH = $(I2PD_LIBS_PATH)/OpenSSL-for-Android-Prebuilt MINIUPNP_PATH = $(I2PD_LIBS_PATH)/MiniUPnP-for-Android-Prebuilt IFADDRS_PATH = $(I2PD_LIBS_PATH)/android-ifaddrs -GOST_PATH = $(I2PD_LIBS_PATH)/GOST-Engine-for-Android-Prebuilt From bd8cdd345ad4a40cc8964e6b52e5f5281f9cad77 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 10 Mar 2017 16:57:56 -0500 Subject: [PATCH 17/39] A,B,B param sets for GOST R 34.10 --- Identity.cpp | 28 +++++++++++++++++++++++++++- Identity.h | 2 ++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Identity.cpp b/Identity.cpp index fb45764d..e6bf34bf 100644 --- a/Identity.cpp +++ b/Identity.cpp @@ -103,6 +103,8 @@ namespace data break; } case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: + case SIGNING_KEY_TYPE_GOSTR3410_B_GOSTR3411: + case SIGNING_KEY_TYPE_GOSTR3410_C_GOSTR3411: { size_t padding = 128 - i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 RAND_bytes (m_StandardIdentity.signingKey, padding); @@ -382,7 +384,19 @@ namespace data size_t padding = 128 - i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 UpdateVerifier (new i2p::crypto::GOSTR3410Verifier (i2p::crypto::eGOSTR3410CryptoProA, m_StandardIdentity.signingKey + padding)); break; - } + } + case SIGNING_KEY_TYPE_GOSTR3410_B_GOSTR3411: + { + size_t padding = 128 - i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 + UpdateVerifier (new i2p::crypto::GOSTR3410Verifier (i2p::crypto::eGOSTR3410CryptoProB, m_StandardIdentity.signingKey + padding)); + break; + } + case SIGNING_KEY_TYPE_GOSTR3410_C_GOSTR3411: + { + size_t padding = 128 - i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 + UpdateVerifier (new i2p::crypto::GOSTR3410Verifier (i2p::crypto::eGOSTR3410CryptoProC, m_StandardIdentity.signingKey + padding)); + break; + } default: LogPrint (eLogError, "Identity: Signing key type ", (int)keyType, " is not supported"); } @@ -527,6 +541,12 @@ namespace data case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: m_Signer.reset (new i2p::crypto::GOSTR3410Signer (i2p::crypto::eGOSTR3410CryptoProA, m_SigningPrivateKey)); break; + case SIGNING_KEY_TYPE_GOSTR3410_B_GOSTR3411: + m_Signer.reset (new i2p::crypto::GOSTR3410Signer (i2p::crypto::eGOSTR3410CryptoProB, m_SigningPrivateKey)); + break; + case SIGNING_KEY_TYPE_GOSTR3410_C_GOSTR3411: + m_Signer.reset (new i2p::crypto::GOSTR3410Signer (i2p::crypto::eGOSTR3410CryptoProC, m_SigningPrivateKey)); + break; default: LogPrint (eLogError, "Identity: Signing key type ", (int)m_Public->GetSigningKeyType (), " is not supported"); } @@ -565,6 +585,12 @@ namespace data case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410CryptoProA, keys.m_SigningPrivateKey, signingPublicKey); break; + case SIGNING_KEY_TYPE_GOSTR3410_B_GOSTR3411: + i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410CryptoProB, keys.m_SigningPrivateKey, signingPublicKey); + break; + case SIGNING_KEY_TYPE_GOSTR3410_C_GOSTR3411: + i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410CryptoProC, keys.m_SigningPrivateKey, signingPublicKey); + break; default: LogPrint (eLogError, "Identity: Signing key type ", (int)type, " is not supported. Create DSA-SHA1"); return PrivateKeys (i2p::data::CreateRandomKeys ()); // DSA-SHA1 diff --git a/Identity.h b/Identity.h index 5fbf1675..479fd5d1 100644 --- a/Identity.h +++ b/Identity.h @@ -62,6 +62,8 @@ namespace data const uint16_t SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519 = 7; // following signature type should never appear in netid=2 const uint16_t SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411 = 65280; // approved by FSB + const uint16_t SIGNING_KEY_TYPE_GOSTR3410_B_GOSTR3411 = 65281; + const uint16_t SIGNING_KEY_TYPE_GOSTR3410_C_GOSTR3411 = 65282; typedef uint16_t SigningKeyType; typedef uint16_t CryptoKeyType; From 2eec205e3190cd674191bc15d90e57219495563e Mon Sep 17 00:00:00 2001 From: Anatolii Vorona Date: Sat, 11 Mar 2017 00:31:00 +0100 Subject: [PATCH 18/39] i2pd.spec: changed Release format, upgrade Version --- contrib/rpm/i2pd.spec | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/contrib/rpm/i2pd.spec b/contrib/rpm/i2pd.spec index bb58d28d..a286349c 100644 --- a/contrib/rpm/i2pd.spec +++ b/contrib/rpm/i2pd.spec @@ -1,6 +1,8 @@ +%define build_timestamp %(date +"%Y%m%d") + Name: i2pd -Version: 2.10.0 -Release: 3%{?dist} +Version: 2.12.0 +Release: %{build_timestamp}git%{?dist} Summary: I2P router written in C++ License: BSD @@ -101,6 +103,23 @@ getent passwd i2pd >/dev/null || \ %changelog +* Tue Feb 14 2017 orignal - 2.12.0 +- Additional HTTP and SOCKS proxy tunnels +- Reseed from ZIP archive +- 'X' bandwidth code +- Reduced memory and file descriptors usage + +* Mon Dec 19 2016 orignal - 2.11.0 +- Full support of zero-hops tunnels +- Tunnel configuration for HTTP and SOCKS proxy +- Websockets support +- Multiple acceptors for SAM destination +- Routing path for UDP tunnels +- Reseed through a floodfill +- Use AVX instructions for DHT and HMAC if applicable +- Fixed UPnP discovery bug, producing excessive CPU usage +- Handle multiple lookups of the same LeaseSet correctly + * Tue Oct 20 2016 Anatolii Vorona - 2.10.0-3 - add support C7 - move rpm-related files to contrib folder From c0c157ecef9f4035039bb4399c99e0818e639771 Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 11 Mar 2017 16:48:54 -0500 Subject: [PATCH 19/39] use common context for ElGamal encrypt/decrypt --- Crypto.cpp | 8 ++------ Crypto.h | 4 ++-- Garlic.cpp | 13 ++++++++++--- Garlic.h | 8 +++++--- I2NPProtocol.cpp | 5 +++-- Tunnel.cpp | 4 +++- TunnelConfig.h | 4 ++-- 7 files changed, 27 insertions(+), 19 deletions(-) diff --git a/Crypto.cpp b/Crypto.cpp index 791968d3..04126cb6 100644 --- a/Crypto.cpp +++ b/Crypto.cpp @@ -272,9 +272,8 @@ namespace crypto } // ElGamal - void ElGamalEncrypt (const uint8_t * key, const uint8_t * data, uint8_t * encrypted, bool zeroPadding) + void ElGamalEncrypt (const uint8_t * key, const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx, bool zeroPadding) { - BN_CTX * ctx = BN_CTX_new (); BN_CTX_start (ctx); // everything, but a, because a might come from table BIGNUM * k = BN_CTX_get (ctx); @@ -324,13 +323,11 @@ namespace crypto } BN_free (a); BN_CTX_end (ctx); - BN_CTX_free (ctx); } bool ElGamalDecrypt (const uint8_t * key, const uint8_t * encrypted, - uint8_t * data, bool zeroPadding) + uint8_t * data, BN_CTX * ctx, bool zeroPadding) { - BN_CTX * ctx = BN_CTX_new (); BN_CTX_start (ctx); BIGNUM * x = BN_CTX_get (ctx), * a = BN_CTX_get (ctx), * b = BN_CTX_get (ctx); BN_bin2bn (key, 256, x); @@ -343,7 +340,6 @@ namespace crypto uint8_t m[255]; bn2buf (b, m, 255); BN_CTX_end (ctx); - BN_CTX_free (ctx); uint8_t hash[32]; SHA256 (m + 33, 222, hash); if (memcmp (m + 1, hash, 32)) diff --git a/Crypto.h b/Crypto.h index 748a7f48..691bb716 100644 --- a/Crypto.h +++ b/Crypto.h @@ -48,8 +48,8 @@ namespace crypto }; // ElGamal - void ElGamalEncrypt (const uint8_t * key, const uint8_t * data, uint8_t * encrypted, bool zeroPadding = false); - bool ElGamalDecrypt (const uint8_t * key, const uint8_t * encrypted, uint8_t * data, bool zeroPadding = false); + void ElGamalEncrypt (const uint8_t * key, const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx, bool zeroPadding = false); + bool ElGamalDecrypt (const uint8_t * key, const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx, bool zeroPadding = false); void GenerateElGamalKeyPair (uint8_t * priv, uint8_t * pub); // HMAC diff --git a/Garlic.cpp b/Garlic.cpp index 51fa6c3b..5b5fd71b 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -187,7 +187,8 @@ namespace garlic RAND_bytes (elGamal.preIV, 32); // Pre-IV uint8_t iv[32]; // IV is first 16 bytes SHA256(elGamal.preIV, 32, iv); - i2p::crypto::ElGamalEncrypt (m_Destination->GetEncryptionPublicKey (), (uint8_t *)&elGamal, buf, true); + i2p::crypto::ElGamalEncrypt (m_Destination->GetEncryptionPublicKey (), + (uint8_t *)&elGamal, buf, m_Owner->GetBNContext (), true); m_Encryption.SetIV (iv); buf += 514; len += 514; @@ -388,9 +389,15 @@ namespace garlic return size; } - + + GarlicDestination::GarlicDestination (): m_NumTags (32) // 32 tags by default + { + m_Ctx = BN_CTX_new (); + } + GarlicDestination::~GarlicDestination () { + BN_CTX_free (m_Ctx); } void GarlicDestination::CleanUp () @@ -446,7 +453,7 @@ namespace garlic { // tag not found. Use ElGamal ElGamalBlock elGamal; - if (length >= 514 && i2p::crypto::ElGamalDecrypt (GetEncryptionPrivateKey (), buf, (uint8_t *)&elGamal, true)) + if (length >= 514 && i2p::crypto::ElGamalDecrypt (GetEncryptionPrivateKey (), buf, (uint8_t *)&elGamal, m_Ctx, true)) { auto decryption = std::make_shared(); decryption->SetKey (elGamal.sessionKey); diff --git a/Garlic.h b/Garlic.h index b5dcd1eb..6b05df42 100644 --- a/Garlic.h +++ b/Garlic.h @@ -153,11 +153,12 @@ namespace garlic { public: - GarlicDestination (): m_NumTags (32) {}; // 32 tags by default + GarlicDestination (); ~GarlicDestination (); void CleanUp (); - void SetNumTags (int numTags) { m_NumTags = numTags; }; + void SetNumTags (int numTags) { m_NumTags = numTags; }; + BN_CTX * GetBNContext () const { return m_Ctx; }; std::shared_ptr GetRoutingSession (std::shared_ptr destination, bool attachLeaseSet); void CleanupExpiredTags (); void RemoveDeliveryStatusSession (uint32_t msgID); @@ -188,7 +189,8 @@ namespace garlic void HandleGarlicPayload (uint8_t * buf, size_t len, std::shared_ptr from); private: - + + BN_CTX * m_Ctx; // outgoing sessions int m_NumTags; std::mutex m_SessionsMutex; diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index 0a5031e7..4e4ca7c3 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -326,8 +326,9 @@ namespace i2p if (!memcmp (record + BUILD_REQUEST_RECORD_TO_PEER_OFFSET, (const uint8_t *)i2p::context.GetRouterInfo ().GetIdentHash (), 16)) { LogPrint (eLogDebug, "I2NP: Build request record ", i, " is ours"); - - i2p::crypto::ElGamalDecrypt (i2p::context.GetEncryptionPrivateKey (), record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET, clearText); + BN_CTX * ctx = BN_CTX_new (); + i2p::crypto::ElGamalDecrypt (i2p::context.GetEncryptionPrivateKey (), record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET, clearText, ctx); + BN_CTX_free (ctx); // replace record to reply if (i2p::context.AcceptsTunnels () && i2p::tunnel::tunnels.GetTransitTunnels ().size () <= g_MaxNumTransitTunnels && diff --git a/Tunnel.cpp b/Tunnel.cpp index 390c64ac..798d7198 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -51,6 +51,7 @@ namespace tunnel uint8_t * records = msg->GetPayload () + 1; TunnelHopConfig * hop = m_Config->GetFirstHop (); int i = 0; + BN_CTX * ctx = BN_CTX_new (); while (hop) { uint32_t msgID; @@ -59,7 +60,7 @@ namespace tunnel else msgID = replyMsgID; int idx = recordIndicies[i]; - hop->CreateBuildRequestRecord (records + idx*TUNNEL_BUILD_RECORD_SIZE, msgID); + hop->CreateBuildRequestRecord (records + idx*TUNNEL_BUILD_RECORD_SIZE, msgID, ctx); hop->recordIndex = idx; i++; #ifdef WITH_EVENTS @@ -67,6 +68,7 @@ namespace tunnel #endif hop = hop->next; } + BN_CTX_free (ctx); #ifdef WITH_EVENTS EmitTunnelEvent("tunnel.build", this, peers); #endif diff --git a/TunnelConfig.h b/TunnelConfig.h index c131059c..10e980ad 100644 --- a/TunnelConfig.h +++ b/TunnelConfig.h @@ -83,7 +83,7 @@ namespace tunnel } } - void CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID) const + void CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID, BN_CTX * ctx) const { uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE]; htobe32buf (clearText + BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET, tunnelID); @@ -101,7 +101,7 @@ namespace tunnel 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); - i2p::crypto::ElGamalEncrypt (ident->GetEncryptionPublicKey (), clearText, record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET); + i2p::crypto::ElGamalEncrypt (ident->GetEncryptionPublicKey (), clearText, record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET, ctx); memcpy (record + BUILD_REQUEST_RECORD_TO_PEER_OFFSET, (const uint8_t *)ident->GetIdentHash (), 16); } }; From f0d6145fa677ea4f7ec651ee6a6cd92991ba8661 Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 11 Mar 2017 21:17:19 -0500 Subject: [PATCH 20/39] terminate incoming connection by timeout --- NTCPSession.cpp | 26 +++++++++++++++++++++++++- NTCPSession.h | 4 +++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/NTCPSession.cpp b/NTCPSession.cpp index 8dbe4fe7..e26265a3 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -23,7 +23,7 @@ namespace i2p namespace transport { NTCPSession::NTCPSession (NTCPServer& server, std::shared_ptr in_RemoteRouter): - TransportSession (in_RemoteRouter, NTCP_TERMINATION_TIMEOUT), + TransportSession (in_RemoteRouter, NTCP_CONNECT_TIMEOUT), m_Server (server), m_Socket (m_Server.GetService ()), m_IsEstablished (false), m_IsTerminated (false), m_ReceiveBufferOffset (0), m_NextMessage (nullptr), m_IsSending (false) @@ -98,6 +98,7 @@ namespace transport m_DHKeysPair = nullptr; + SetTerminationTimeout (NTCP_TERMINATION_TIMEOUT); SendTimeSyncMessage (); transports.PeerConnected (shared_from_this ()); } @@ -120,6 +121,7 @@ namespace transport void NTCPSession::ServerLogin () { + m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch (); // receive Phase1 boost::asio::async_read (m_Socket, boost::asio::buffer(&m_Establisher->phase1, sizeof (NTCPPhase1)), boost::asio::transfer_all (), std::bind(&NTCPSession::HandlePhase1Received, shared_from_this (), @@ -858,6 +860,8 @@ namespace transport auto ntcpSessions = m_NTCPSessions; for (auto& it: ntcpSessions) it.second->Terminate (); + for (auto& it: m_PendingIncomingSessions) + it->Terminate (); } m_NTCPSessions.clear (); @@ -940,7 +944,10 @@ namespace transport { LogPrint (eLogDebug, "NTCP: Connected from ", ep); if (conn) + { conn->ServerLogin (); + m_PendingIncomingSessions.push_back (conn); + } } else LogPrint (eLogError, "NTCP: Connected from error ", ec.message ()); @@ -965,7 +972,10 @@ namespace transport { LogPrint (eLogDebug, "NTCP: Connected from ", ep); if (conn) + { conn->ServerLogin (); + m_PendingIncomingSessions.push_back (conn); + } } else LogPrint (eLogError, "NTCP: Connected from error ", ec.message ()); @@ -1033,16 +1043,30 @@ namespace transport if (ecode != boost::asio::error::operation_aborted) { auto ts = i2p::util::GetSecondsSinceEpoch (); + // established for (auto& it: m_NTCPSessions) if (it.second->IsTerminationTimeoutExpired (ts)) { auto session = it.second; + // Termniate modifies m_NTCPSession, so we postpone it m_Service.post ([session] { LogPrint (eLogDebug, "NTCP: No activity for ", session->GetTerminationTimeout (), " seconds"); session->Terminate (); }); } + // pending + for (auto it = m_PendingIncomingSessions.begin (); it != m_PendingIncomingSessions.end ();) + { + if ((*it)->IsEstablished () || (*it)->IsTerminated ()) + it = m_PendingIncomingSessions.erase (it); // established or terminated + else if ((*it)->IsTerminationTimeoutExpired (ts)) + { + (*it)->Terminate (); + it = m_PendingIncomingSessions.erase (it); // expired + } + } + ScheduleTermination (); } } diff --git a/NTCPSession.h b/NTCPSession.h index d9acd5ce..3891dd62 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -55,7 +55,8 @@ namespace transport boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; }; bool IsEstablished () const { return m_IsEstablished; }; - + bool IsTerminated () const { return m_IsTerminated; }; + void ClientLogin (); void ServerLogin (); void SendI2NPMessages (const std::vector >& msgs); @@ -166,6 +167,7 @@ namespace transport boost::asio::deadline_timer m_TerminationTimer; boost::asio::ip::tcp::acceptor * m_NTCPAcceptor, * m_NTCPV6Acceptor; std::map > m_NTCPSessions; // access from m_Thread only + std::list > m_PendingIncomingSessions; public: From 1317b80fca8c66ca0e9d12454f80d9d01a189c1a Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 11 Mar 2017 21:32:35 -0500 Subject: [PATCH 21/39] terminate incoming connection by timeout --- NTCPSession.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NTCPSession.cpp b/NTCPSession.cpp index e26265a3..b66713c9 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -1065,6 +1065,8 @@ namespace transport (*it)->Terminate (); it = m_PendingIncomingSessions.erase (it); // expired } + else + it++; } ScheduleTermination (); From 65ed57aff425bde8286b6fd4e85e9d82ab4d7409 Mon Sep 17 00:00:00 2001 From: r4sas Date: Sun, 12 Mar 2017 18:28:25 +0300 Subject: [PATCH 22/39] fix year --- debian/changelog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debian/changelog b/debian/changelog index 22842831..be3a9897 100644 --- a/debian/changelog +++ b/debian/changelog @@ -2,7 +2,7 @@ i2pd (2.12.0-1) unstable; urgency=low * updated to version 2.12.0/0.9.28 - -- orignal Tue, 14 Feb 2016 17:59:30 +0000 + -- orignal Sun, 14 Feb 2017 17:59:30 +0000 i2pd (2.11.0-1) unstable; urgency=low From ce0bf0f4b4205e9a5fe39861805f0411134f6d14 Mon Sep 17 00:00:00 2001 From: r4sas Date: Sun, 12 Mar 2017 18:29:57 +0300 Subject: [PATCH 23/39] re-fix year --- debian/changelog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debian/changelog b/debian/changelog index be3a9897..5f745efa 100644 --- a/debian/changelog +++ b/debian/changelog @@ -2,7 +2,7 @@ i2pd (2.12.0-1) unstable; urgency=low * updated to version 2.12.0/0.9.28 - -- orignal Sun, 14 Feb 2017 17:59:30 +0000 + -- orignal Tue, 14 Feb 2017 17:59:30 +0000 i2pd (2.11.0-1) unstable; urgency=low From 5803a84bd71733e01f64847c32bdbe2ba4dfbeeb Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 12 Mar 2017 14:48:11 -0400 Subject: [PATCH 24/39] change establish timeout to 10 seconds --- NTCPSession.cpp | 2 +- NTCPSession.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/NTCPSession.cpp b/NTCPSession.cpp index b66713c9..a2aec85c 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -23,7 +23,7 @@ namespace i2p namespace transport { NTCPSession::NTCPSession (NTCPServer& server, std::shared_ptr in_RemoteRouter): - TransportSession (in_RemoteRouter, NTCP_CONNECT_TIMEOUT), + TransportSession (in_RemoteRouter, NTCP_ESTABLISH_TIMEOUT), m_Server (server), m_Socket (m_Server.GetService ()), m_IsEstablished (false), m_IsTerminated (false), m_ReceiveBufferOffset (0), m_NextMessage (nullptr), m_IsSending (false) diff --git a/NTCPSession.h b/NTCPSession.h index 3891dd62..9de75d02 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -37,6 +37,7 @@ namespace transport const size_t NTCP_MAX_MESSAGE_SIZE = 16384; const size_t NTCP_BUFFER_SIZE = 1028; // fits 1 tunnel data message const int NTCP_CONNECT_TIMEOUT = 5; // 5 seconds + const int NTCP_ESTABLISH_TIMEOUT = 10; // 10 seconds const int NTCP_TERMINATION_TIMEOUT = 120; // 2 minutes const int NTCP_TERMINATION_CHECK_TIMEOUT = 30; // 30 seconds const size_t NTCP_DEFAULT_PHASE3_SIZE = 2/*size*/ + i2p::data::DEFAULT_IDENTITY_SIZE/*387*/ + 4/*ts*/ + 15/*padding*/ + 40/*signature*/; // 448 From 6ed7f1967378dffb5bae2aad48ab4c9dc6bc8dcd Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 13 Mar 2017 20:28:36 -0400 Subject: [PATCH 25/39] moved GOST to separate file --- Gost.cpp | 150 +++++++++++++++++++++++++++++++++++++++++ Gost.h | 47 +++++++++++++ Signature.cpp | 150 ----------------------------------------- Signature.h | 12 +--- android/jni/Android.mk | 1 + filelist.mk | 2 +- qt/i2pd_qt/i2pd_qt.pro | 4 +- 7 files changed, 202 insertions(+), 164 deletions(-) create mode 100644 Gost.cpp create mode 100644 Gost.h diff --git a/Gost.cpp b/Gost.cpp new file mode 100644 index 00000000..7d9db0f8 --- /dev/null +++ b/Gost.cpp @@ -0,0 +1,150 @@ +#include +#include +#include "Gost.h" + +namespace i2p +{ +namespace crypto +{ + GOSTR3410Curve::GOSTR3410Curve (BIGNUM * a, BIGNUM * b, BIGNUM * p, BIGNUM * q, BIGNUM * x, BIGNUM * y) + { + BN_CTX * ctx = BN_CTX_new (); + m_Group = EC_GROUP_new_curve_GFp (p, a, b, ctx); + EC_POINT * P = EC_POINT_new (m_Group); + EC_POINT_set_affine_coordinates_GFp (m_Group, P, x, y, ctx); + EC_GROUP_set_generator (m_Group, P, q, nullptr); + EC_GROUP_set_curve_name (m_Group, NID_id_GostR3410_2001); + EC_POINT_free(P); + BN_CTX_free (ctx); + } + + GOSTR3410Curve::~GOSTR3410Curve () + { + EC_GROUP_free (m_Group); + } + + EC_POINT * GOSTR3410Curve::MulP (const BIGNUM * n) const + { + BN_CTX * ctx = BN_CTX_new (); + auto p = EC_POINT_new (m_Group); + EC_POINT_mul (m_Group, p, n, nullptr, nullptr, ctx); + BN_CTX_free (ctx); + return p; + } + + bool GOSTR3410Curve::GetXY (const EC_POINT * p, BIGNUM * x, BIGNUM * y) const + { + return EC_POINT_get_affine_coordinates_GFp (m_Group, p, x, y, nullptr); + } + + EC_POINT * GOSTR3410Curve::CreatePoint (const BIGNUM * x, const BIGNUM * y) const + { + EC_POINT * p = EC_POINT_new (m_Group); + EC_POINT_set_affine_coordinates_GFp (m_Group, p, x, y, nullptr); + return p; + } + + void GOSTR3410Curve::Sign (const BIGNUM * priv, const BIGNUM * digest, BIGNUM * r, BIGNUM * s) + { + BN_CTX * ctx = BN_CTX_new (); + BN_CTX_start (ctx); + BIGNUM * q = BN_CTX_get (ctx); + EC_GROUP_get_order(m_Group, q, ctx); + BIGNUM * k = BN_CTX_get (ctx); + BN_rand_range (k, q); // 0 < k < q + EC_POINT * C = MulP (k); // C = k*P + GetXY (C, r, nullptr); // r = Cx + EC_POINT_free (C); + BN_mod_mul (s, r, priv, q, ctx); // (r*priv)%q + BIGNUM * tmp = BN_CTX_get (ctx); + BN_mod_mul (tmp, k, digest, q, ctx); // (k*digest)%q + BN_mod_add (s, s, tmp, q, ctx); // (r*priv+k*digest)%q + BN_CTX_end (ctx); + BN_CTX_free (ctx); + } + + bool GOSTR3410Curve::Verify (const EC_POINT * pub, const BIGNUM * digest, const BIGNUM * r, const BIGNUM * s) + { + BN_CTX * ctx = BN_CTX_new (); + BN_CTX_start (ctx); + BIGNUM * q = BN_CTX_get (ctx); + EC_GROUP_get_order(m_Group, q, ctx); + BIGNUM * h = BN_CTX_get (ctx); + BN_mod (h, digest, q, ctx); // h = digest % q + BN_mod_inverse (h, h, q, ctx); // 1/h mod q + BIGNUM * z1 = BN_CTX_get (ctx); + BN_mod_mul (z1, s, h, q, ctx); // z1 = s/h + BIGNUM * z2 = BN_CTX_get (ctx); + BN_sub (z2, q, r); // z2 = -r + BN_mod_mul (z2, z2, h, q, ctx); // z2 = -r/h + EC_POINT * C = EC_POINT_new (m_Group); + EC_POINT_mul (m_Group, C, z1, pub, z2, ctx); // z1*P + z2*pub + BIGNUM * x = BN_CTX_get (ctx); + GetXY (C, x, nullptr); // Cx + BN_mod (x, x, q, ctx); // Cx % q + bool ret = !BN_cmp (x, r); // Cx = r ? + EC_POINT_free (C); + BN_CTX_end (ctx); + BN_CTX_free (ctx); + return ret; + } + + static GOSTR3410Curve * CreateGOSTR3410Curve (GOSTR3410ParamSet paramSet) + { + // a, b, p, q, x, y + static const char * params[eGOSTR3410NumParamSets][6] = + { + { + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD94", + "A6", + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD97", + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF6C611070995AD10045841B09B761B893", + "1", + "8D91E471E0989CDA27DF505A453F2B7635294F2DDF23E3B122ACC99C9E9F1E14" + }, // A + { + "8000000000000000000000000000000000000000000000000000000000000C96", + "3E1AF419A269A5F866A7D3C25C3DF80AE979259373FF2B182F49D4CE7E1BBC8B", + "8000000000000000000000000000000000000000000000000000000000000C99", + "800000000000000000000000000000015F700CFFF1A624E5E497161BCC8A198F", + "1", + "3FA8124359F96680B83D1C3EB2C070E5C545C9858D03ECFB744BF8D717717EFC" + }, // B + { + "9B9F605F5A858107AB1EC85E6B41C8AACF846E86789051D37998F7B9022D7598", + "805A", + "9B9F605F5A858107AB1EC85E6B41C8AACF846E86789051D37998F7B9022D759B", + "9B9F605F5A858107AB1EC85E6B41C8AA582CA3511EDDFB74F02F3A6598980BB9", + "0", + "41ECE55743711A8C3CBF3783CD08C0EE4D4DC440D4641A8F366E550DFDB3BB67" + } // C + }; + + BIGNUM * a = nullptr, * b = nullptr, * p = nullptr, * q =nullptr, * x = nullptr, * y = nullptr; + BN_hex2bn(&a, params[paramSet][0]); + BN_hex2bn(&b, params[paramSet][1]); + BN_hex2bn(&p, params[paramSet][2]); + BN_hex2bn(&q, params[paramSet][3]); + BN_hex2bn(&x, params[paramSet][4]); + BN_hex2bn(&y, params[paramSet][5]); + auto curve = new GOSTR3410Curve (a, b, p, q, x, y); + BN_free (a); BN_free (b); BN_free (p); BN_free (q); BN_free (x); BN_free (y); + return curve; + } + + static std::array, eGOSTR3410NumParamSets> g_GOSTR3410Curves; + std::unique_ptr& GetGOSTR3410Curve (GOSTR3410ParamSet paramSet) + { + if (!g_GOSTR3410Curves[paramSet]) + { + auto c = CreateGOSTR3410Curve (paramSet); + if (!g_GOSTR3410Curves[paramSet]) // make sure it was not created already + g_GOSTR3410Curves[paramSet].reset (c); + else + delete c; + } + return g_GOSTR3410Curves[paramSet]; + } + +} +} diff --git a/Gost.h b/Gost.h new file mode 100644 index 00000000..f287219f --- /dev/null +++ b/Gost.h @@ -0,0 +1,47 @@ +#ifndef GOST_H__ +#define GOST_H__ + +#include +#include + +namespace i2p +{ +namespace crypto +{ + + // ГОСТ Р 34.10 + + enum GOSTR3410ParamSet + { + eGOSTR3410CryptoProA = 0, // 1.2.643.2.2.35.1 + eGOSTR3410CryptoProB, // 1.2.643.2.2.35.2 + eGOSTR3410CryptoProC, // 1.2.643.2.2.35.3 + //eGOSTR3410CryptoProXchA, // 1.2.643.2.2.36.0 + //eGOSTR3410CryptoProXchB, // 1.2.643.2.2.36.1 + // XchA = A, XchB = C + eGOSTR3410NumParamSets + }; + + class GOSTR3410Curve + { + public: + + GOSTR3410Curve (BIGNUM * a, BIGNUM * b, BIGNUM * p, BIGNUM * q, BIGNUM * x, BIGNUM * y); + ~GOSTR3410Curve (); + + EC_POINT * MulP (const BIGNUM * n) const; + bool GetXY (const EC_POINT * p, BIGNUM * x, BIGNUM * y) const; + EC_POINT * CreatePoint (const BIGNUM * x, const BIGNUM * y) const; + void Sign (const BIGNUM * priv, const BIGNUM * digest, BIGNUM * r, BIGNUM * s); + bool Verify (const EC_POINT * pub, const BIGNUM * digest, const BIGNUM * r, const BIGNUM * s); + + private: + + EC_GROUP * m_Group; + }; + + std::unique_ptr& GetGOSTR3410Curve (GOSTR3410ParamSet paramSet); +} +} + +#endif \ No newline at end of file diff --git a/Signature.cpp b/Signature.cpp index 15d606dc..bcb451f2 100644 --- a/Signature.cpp +++ b/Signature.cpp @@ -1,5 +1,4 @@ #include -#include #include "Log.h" #include "Signature.h" @@ -497,155 +496,6 @@ namespace crypto //---------------------------------------------- // GOST - class GOSTR3410Curve - { - public: - - GOSTR3410Curve (BIGNUM * a, BIGNUM * b, BIGNUM * p, BIGNUM * q, BIGNUM * x, BIGNUM * y) - { - BN_CTX * ctx = BN_CTX_new (); - m_Group = EC_GROUP_new_curve_GFp (p, a, b, ctx); - EC_POINT * P = EC_POINT_new (m_Group); - EC_POINT_set_affine_coordinates_GFp (m_Group, P, x, y, ctx); - EC_GROUP_set_generator (m_Group, P, q, nullptr); - EC_GROUP_set_curve_name (m_Group, NID_id_GostR3410_2001); - EC_POINT_free(P); - BN_CTX_free (ctx); - } - - ~GOSTR3410Curve () - { - EC_GROUP_free (m_Group); - } - - EC_POINT * MulP (const BIGNUM * n) const - { - BN_CTX * ctx = BN_CTX_new (); - auto p = EC_POINT_new (m_Group); - EC_POINT_mul (m_Group, p, n, nullptr, nullptr, ctx); - BN_CTX_free (ctx); - return p; - } - - bool GetXY (const EC_POINT * p, BIGNUM * x, BIGNUM * y) const - { - return EC_POINT_get_affine_coordinates_GFp (m_Group, p, x, y, nullptr); - } - - EC_POINT * CreatePoint (const BIGNUM * x, const BIGNUM * y) const - { - EC_POINT * p = EC_POINT_new (m_Group); - EC_POINT_set_affine_coordinates_GFp (m_Group, p, x, y, nullptr); - return p; - } - - void Sign (const BIGNUM * priv, const BIGNUM * digest, BIGNUM * r, BIGNUM * s) - { - BN_CTX * ctx = BN_CTX_new (); - BN_CTX_start (ctx); - BIGNUM * q = BN_CTX_get (ctx); - EC_GROUP_get_order(m_Group, q, ctx); - BIGNUM * k = BN_CTX_get (ctx); - BN_rand_range (k, q); // 0 < k < q - EC_POINT * C = MulP (k); // C = k*P - GetXY (C, r, nullptr); // r = Cx - EC_POINT_free (C); - BN_mod_mul (s, r, priv, q, ctx); // (r*priv)%q - BIGNUM * tmp = BN_CTX_get (ctx); - BN_mod_mul (tmp, k, digest, q, ctx); // (k*digest)%q - BN_mod_add (s, s, tmp, q, ctx); // (r*priv+k*digest)%q - BN_CTX_end (ctx); - BN_CTX_free (ctx); - } - - bool Verify (const EC_POINT * pub, const BIGNUM * digest, const BIGNUM * r, const BIGNUM * s) - { - BN_CTX * ctx = BN_CTX_new (); - BN_CTX_start (ctx); - BIGNUM * q = BN_CTX_get (ctx); - EC_GROUP_get_order(m_Group, q, ctx); - BIGNUM * h = BN_CTX_get (ctx); - BN_mod (h, digest, q, ctx); // h = digest % q - BN_mod_inverse (h, h, q, ctx); // 1/h mod q - BIGNUM * z1 = BN_CTX_get (ctx); - BN_mod_mul (z1, s, h, q, ctx); // z1 = s/h - BIGNUM * z2 = BN_CTX_get (ctx); - BN_sub (z2, q, r); // z2 = -r - BN_mod_mul (z2, z2, h, q, ctx); // z2 = -r/h - EC_POINT * C = EC_POINT_new (m_Group); - EC_POINT_mul (m_Group, C, z1, pub, z2, ctx); // z1*P + z2*pub - BIGNUM * x = BN_CTX_get (ctx); - GetXY (C, x, nullptr); // Cx - BN_mod (x, x, q, ctx); // Cx % q - bool ret = !BN_cmp (x, r); // Cx = r ? - EC_POINT_free (C); - BN_CTX_end (ctx); - BN_CTX_free (ctx); - return ret; - } - - private: - - EC_GROUP * m_Group; - }; - - GOSTR3410Curve * CreateGOSTR3410Curve (GOSTR3410ParamSet paramSet) - { - // a, b, p, q, x, y - static const char * params[eGOSTR3410NumParamSets][6] = - { - { - "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD94", - "A6", - "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD97", - "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF6C611070995AD10045841B09B761B893", - "1", - "8D91E471E0989CDA27DF505A453F2B7635294F2DDF23E3B122ACC99C9E9F1E14" - }, // A - { - "8000000000000000000000000000000000000000000000000000000000000C96", - "3E1AF419A269A5F866A7D3C25C3DF80AE979259373FF2B182F49D4CE7E1BBC8B", - "8000000000000000000000000000000000000000000000000000000000000C99", - "800000000000000000000000000000015F700CFFF1A624E5E497161BCC8A198F", - "1", - "3FA8124359F96680B83D1C3EB2C070E5C545C9858D03ECFB744BF8D717717EFC" - }, // B - { - "9B9F605F5A858107AB1EC85E6B41C8AACF846E86789051D37998F7B9022D7598", - "805A", - "9B9F605F5A858107AB1EC85E6B41C8AACF846E86789051D37998F7B9022D759B", - "9B9F605F5A858107AB1EC85E6B41C8AA582CA3511EDDFB74F02F3A6598980BB9", - "0", - "41ECE55743711A8C3CBF3783CD08C0EE4D4DC440D4641A8F366E550DFDB3BB67" - } // C - }; - - BIGNUM * a = nullptr, * b = nullptr, * p = nullptr, * q =nullptr, * x = nullptr, * y = nullptr; - BN_hex2bn(&a, params[paramSet][0]); - BN_hex2bn(&b, params[paramSet][1]); - BN_hex2bn(&p, params[paramSet][2]); - BN_hex2bn(&q, params[paramSet][3]); - BN_hex2bn(&x, params[paramSet][4]); - BN_hex2bn(&y, params[paramSet][5]); - auto curve = new GOSTR3410Curve (a, b, p, q, x, y); - BN_free (a); BN_free (b); BN_free (p); BN_free (q); BN_free (x); BN_free (y); - return curve; - } - - static std::array, eGOSTR3410NumParamSets> g_GOSTR3410Curves; - std::unique_ptr& GetGOSTR3410Curve (GOSTR3410ParamSet paramSet) - { - if (!g_GOSTR3410Curves[paramSet]) - { - auto c = CreateGOSTR3410Curve (paramSet); - if (!g_GOSTR3410Curves[paramSet]) // make sure it was not created already - g_GOSTR3410Curves[paramSet].reset (c); - else - delete c; - } - return g_GOSTR3410Curves[paramSet]; - } - GOSTR3410Verifier::GOSTR3410Verifier (GOSTR3410ParamSet paramSet, const uint8_t * signingKey): m_ParamSet (paramSet) { diff --git a/Signature.h b/Signature.h index 0ccb99ea..91f0e66b 100644 --- a/Signature.h +++ b/Signature.h @@ -9,6 +9,7 @@ #include #include #include "Crypto.h" +#include "Gost.h" namespace i2p { @@ -443,17 +444,6 @@ namespace crypto } // ГОСТ Р 34.10-2001 - - enum GOSTR3410ParamSet - { - eGOSTR3410CryptoProA = 0, // 1.2.643.2.2.35.1 - eGOSTR3410CryptoProB, // 1.2.643.2.2.35.2 - eGOSTR3410CryptoProC, // 1.2.643.2.2.35.3 - //eGOSTR3410CryptoProXchA, // 1.2.643.2.2.36.0 - //eGOSTR3410CryptoProXchB, // 1.2.643.2.2.36.1 - // XchA = A, XchB = C - eGOSTR3410NumParamSets - }; const size_t GOSTR3410_PUBLIC_KEY_LENGTH = 64; const size_t GOSTR3410_SIGNATURE_LENGTH = 64; diff --git a/android/jni/Android.mk b/android/jni/Android.mk index 1bba69f0..46e00896 100755 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -59,6 +59,7 @@ LOCAL_SRC_FILES := DaemonAndroid.cpp i2pd_android.cpp \ ../../TunnelPool.cpp \ ../../Timestamp.cpp \ ../../Event.cpp \ + ../../Gost.cpp \ ../../WebSocks.cpp \ ../../BloomFilter.cpp \ ../../util.cpp \ diff --git a/filelist.mk b/filelist.mk index d5d703a6..9149eefa 100644 --- a/filelist.mk +++ b/filelist.mk @@ -5,7 +5,7 @@ LIB_SRC = \ SSUSession.cpp SSUData.cpp Streaming.cpp Identity.cpp TransitTunnel.cpp \ Transports.cpp Tunnel.cpp TunnelEndpoint.cpp TunnelPool.cpp TunnelGateway.cpp \ Destination.cpp Base.cpp I2PEndian.cpp FS.cpp Config.cpp Family.cpp \ - Config.cpp HTTP.cpp Timestamp.cpp util.cpp api.cpp Event.cpp + Config.cpp HTTP.cpp Timestamp.cpp util.cpp api.cpp Event.cpp Gost.cpp LIB_CLIENT_SRC = \ AddressBook.cpp BOB.cpp ClientContext.cpp I2PTunnel.cpp I2PService.cpp \ diff --git a/qt/i2pd_qt/i2pd_qt.pro b/qt/i2pd_qt/i2pd_qt.pro index 36883399..dd3987d9 100644 --- a/qt/i2pd_qt/i2pd_qt.pro +++ b/qt/i2pd_qt/i2pd_qt.pro @@ -36,7 +36,7 @@ SOURCES += DaemonQT.cpp mainwindow.cpp \ ../../SSUData.cpp ../../SSUSession.cpp ../../Streaming.cpp ../../TransitTunnel.cpp \ ../../Transports.cpp ../../Tunnel.cpp ../../TunnelEndpoint.cpp ../../TunnelGateway.cpp \ ../../TunnelPool.cpp ../../UPnP.cpp ../../Gzip.cpp ../../Timestamp.cpp ../../util.cpp \ - ../../Event.cpp ../../BloomFilter.cpp ../../WebSocks.cpp ../../i2pd.cpp + ../../Event.cpp ../../BloomFiler.cpp ../../Gost.cpp ../../i2pd.cpp HEADERS += DaemonQT.h mainwindow.h \ ../../HTTPServer.h ../../I2PControl.h ../../UPnP.h ../../Daemon.h ../../Config.h \ @@ -51,7 +51,7 @@ HEADERS += DaemonQT.h mainwindow.h \ ../../TransportSession.h ../../Tunnel.h ../../TunnelBase.h ../../TunnelConfig.h \ ../../TunnelEndpoint.h ../../TunnelGateway.h ../../TunnelPool.h ../../UPnP.h \ ../../util.h ../../version.h ../../Gzip.h ../../Tag.h \ - ../../BloomFilter.h ../../Event.h ../../WebSocks.h + ../../BloomFiler.h ../../Event.h ../../Gost.h FORMS += mainwindow.ui From a52064463edae53ad715871608c2c203f644eef4 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 14 Mar 2017 12:03:51 -0400 Subject: [PATCH 26/39] fixed race condition --- Garlic.cpp | 5 +++-- Garlic.h | 3 +-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Garlic.cpp b/Garlic.cpp index 5b5fd71b..b2f64000 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -187,8 +187,9 @@ namespace garlic RAND_bytes (elGamal.preIV, 32); // Pre-IV uint8_t iv[32]; // IV is first 16 bytes SHA256(elGamal.preIV, 32, iv); - i2p::crypto::ElGamalEncrypt (m_Destination->GetEncryptionPublicKey (), - (uint8_t *)&elGamal, buf, m_Owner->GetBNContext (), true); + BN_CTX * ctx = BN_CTX_new (); + i2p::crypto::ElGamalEncrypt (m_Destination->GetEncryptionPublicKey (), (uint8_t *)&elGamal, buf, ctx, true); + BN_CTX_free (ctx); m_Encryption.SetIV (iv); buf += 514; len += 514; diff --git a/Garlic.h b/Garlic.h index 6b05df42..6cc37a7d 100644 --- a/Garlic.h +++ b/Garlic.h @@ -158,7 +158,6 @@ namespace garlic void CleanUp (); void SetNumTags (int numTags) { m_NumTags = numTags; }; - BN_CTX * GetBNContext () const { return m_Ctx; }; std::shared_ptr GetRoutingSession (std::shared_ptr destination, bool attachLeaseSet); void CleanupExpiredTags (); void RemoveDeliveryStatusSession (uint32_t msgID); @@ -190,7 +189,7 @@ namespace garlic private: - BN_CTX * m_Ctx; + BN_CTX * m_Ctx; // incoming // outgoing sessions int m_NumTags; std::mutex m_SessionsMutex; From 5bcc5ff873e5cf9f5a4d93e4f4187844e31acc16 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 14 Mar 2017 16:02:16 -0400 Subject: [PATCH 27/39] initial support of GOST R 34.10-2012 --- Crypto.cpp | 9 --------- Crypto.h | 3 --- Gost.cpp | 23 +++++++++++++++++++++-- Gost.h | 8 ++++++-- Signature.cpp | 51 --------------------------------------------------- Signature.h | 47 +++++++++++++++++++++++++++++++++++++++++++---- 6 files changed, 70 insertions(+), 71 deletions(-) diff --git a/Crypto.cpp b/Crypto.cpp index 04126cb6..9e328fd0 100644 --- a/Crypto.cpp +++ b/Crypto.cpp @@ -797,15 +797,6 @@ namespace crypto m_OpenSSLMutexes[type]->unlock (); } }*/ - - - uint8_t * GOSTR3411 (const uint8_t * buf, size_t len, uint8_t * digest) - { - // TODO: implement actual GOST R 34.11 - // SHA-256 is used for testing only - SHA256 (buf, len, digest); - return digest; - } void InitCrypto (bool precomputation, bool withGost) diff --git a/Crypto.h b/Crypto.h index 691bb716..f55e1bd7 100644 --- a/Crypto.h +++ b/Crypto.h @@ -278,9 +278,6 @@ namespace crypto CBCDecryption m_LayerDecryption; #endif }; - -// GOST - uint8_t * GOSTR3411 (const uint8_t * buf, size_t len, uint8_t * digest); // hash void InitCrypto (bool precomputation, bool withGost = false); void TerminateCrypto (); diff --git a/Gost.cpp b/Gost.cpp index 7d9db0f8..63625baa 100644 --- a/Gost.cpp +++ b/Gost.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "Gost.h" @@ -6,6 +7,9 @@ namespace i2p { namespace crypto { + +// GOST R 34.10 + GOSTR3410Curve::GOSTR3410Curve (BIGNUM * a, BIGNUM * b, BIGNUM * p, BIGNUM * q, BIGNUM * x, BIGNUM * y) { BN_CTX * ctx = BN_CTX_new (); @@ -117,7 +121,23 @@ namespace crypto "9B9F605F5A858107AB1EC85E6B41C8AA582CA3511EDDFB74F02F3A6598980BB9", "0", "41ECE55743711A8C3CBF3783CD08C0EE4D4DC440D4641A8F366E550DFDB3BB67" - } // C + }, // C + { + "C2173F1513981673AF4892C23035A27CE25E2013BF95AA33B22C656F277E7335", + "295F9BAE7428ED9CCC20E7C359A9D41A22FCCD9108E17BF7BA9337A6F8AE9513", + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD97", + "400000000000000000000000000000000FD8CDDFC87B6635C115AF556C360C67", + "91E38443A5E82C0D880923425712B2BB658B9196932E02C78B2582FE742DAA28", + "32879423AB1A0375895786C4BB46E9565FDE0B5344766740AF268ADB32322E5C" + }, // tc26-2012-paramSetA-256 + { + "DC9203E514A721875485A529D2C722FB187BC8980EB866644DE41C68E143064546E861C0E2C9EDD92ADE71F46FCF50FF2AD97F951FDA9F2A2EB6546F39689BD3", + "B4C4EE28CEBC6C2C8AC12952CF37F16AC7EFB6A9F69F4B57FFDA2E4F0DE5ADE038CBC2FFF719D2C18DE0284B8BFEF3B52B8CC7A5F5BF0A3C8D2319A5312557E1", + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC7", + "3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC98CDBA46506AB004C33A9FF5147502CC8EDA9E7A769A12694623CEF47F023ED", + "E2E31EDFC23DE7BDEBE241CE593EF5DE2295B7A9CBAEF021D385F7074CEA043AA27272A7AE602BF2A7B9033DB9ED3610C6FB85487EAE97AAC5BC7928C1950148", + "F5CE40D95B5EB899ABBCCFF5911CB8577939804D6527378B8C108C3D2090FF9BE18E2D33E3021ED27EF32D85822423B6304F726AA854BAE07D0396E9A9ADDC40F" + } // tc26-2012-paramSetC-256 }; BIGNUM * a = nullptr, * b = nullptr, * p = nullptr, * q =nullptr, * x = nullptr, * y = nullptr; @@ -145,6 +165,5 @@ namespace crypto } return g_GOSTR3410Curves[paramSet]; } - } } diff --git a/Gost.h b/Gost.h index f287219f..62a8aea1 100644 --- a/Gost.h +++ b/Gost.h @@ -13,12 +13,16 @@ namespace crypto enum GOSTR3410ParamSet { + // GOST R 34.10-2001 eGOSTR3410CryptoProA = 0, // 1.2.643.2.2.35.1 eGOSTR3410CryptoProB, // 1.2.643.2.2.35.2 eGOSTR3410CryptoProC, // 1.2.643.2.2.35.3 + // XchA = A, XchB = C //eGOSTR3410CryptoProXchA, // 1.2.643.2.2.36.0 //eGOSTR3410CryptoProXchB, // 1.2.643.2.2.36.1 - // XchA = A, XchB = C + // GOST R 34.10-2012 + eGOSTR3410tc26paramSetA256, // 1.2.643.7.1.2.1.1.1 + eGOSTR3410tc26paramSetC512, // 1.2.643.7.1.2.1.2.3 eGOSTR3410NumParamSets }; @@ -44,4 +48,4 @@ namespace crypto } } -#endif \ No newline at end of file +#endif diff --git a/Signature.cpp b/Signature.cpp index bcb451f2..7571760a 100644 --- a/Signature.cpp +++ b/Signature.cpp @@ -492,57 +492,6 @@ namespace crypto { GetEd25519 ()->Sign (m_ExpandedPrivateKey, m_PublicKeyEncoded, buf, len, signature); } - -//---------------------------------------------- -// GOST - - GOSTR3410Verifier::GOSTR3410Verifier (GOSTR3410ParamSet paramSet, const uint8_t * signingKey): - m_ParamSet (paramSet) - { - BIGNUM * x = BN_bin2bn (signingKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, NULL); - BIGNUM * y = BN_bin2bn (signingKey + GOSTR3410_PUBLIC_KEY_LENGTH/2, GOSTR3410_PUBLIC_KEY_LENGTH/2, NULL); - m_PublicKey = GetGOSTR3410Curve (m_ParamSet)->CreatePoint (x, y); - BN_free (x); BN_free (y); - } - - bool GOSTR3410Verifier::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const - { - uint8_t digest[32]; - GOSTR3411 (buf, len, digest); - BIGNUM * d = BN_bin2bn (digest, 32, nullptr); - BIGNUM * r = BN_bin2bn (signature, GetSignatureLen ()/2, NULL); - BIGNUM * s = BN_bin2bn (signature + GetSignatureLen ()/2, GetSignatureLen ()/2, NULL); - bool ret = GetGOSTR3410Curve (m_ParamSet)->Verify (m_PublicKey, d, r, s); - BN_free (d); BN_free (r); BN_free (s); - return ret; - } - - void GOSTR3410Signer::Sign (const uint8_t * buf, int len, uint8_t * signature) const - { - uint8_t digest[32]; - GOSTR3411 (buf, len, digest); - BIGNUM * d = BN_bin2bn (digest, 32, nullptr); - BIGNUM * r = BN_new (), * s = BN_new (); - GetGOSTR3410Curve (m_ParamSet)->Sign (m_PrivateKey, d, r, s); - bn2buf (r, signature, GOSTR3410_SIGNATURE_LENGTH/2); - bn2buf (s, signature + GOSTR3410_SIGNATURE_LENGTH/2, GOSTR3410_SIGNATURE_LENGTH/2); - BN_free (d); BN_free (r); BN_free (s); - } - - void CreateGOSTR3410RandomKeys (GOSTR3410ParamSet paramSet, uint8_t * signingPrivateKey, uint8_t * signingPublicKey) - { - RAND_bytes (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); - BIGNUM * priv = BN_bin2bn (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, nullptr); - const auto& curve = GetGOSTR3410Curve (paramSet); - auto pub = curve->MulP (priv); - BN_free (priv); - BIGNUM * x = BN_new (), * y = BN_new (); - curve->GetXY (pub, x, y); - EC_POINT_free (pub); - bn2buf (x, signingPublicKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); - bn2buf (y, signingPublicKey + GOSTR3410_PUBLIC_KEY_LENGTH/2, GOSTR3410_PUBLIC_KEY_LENGTH/2); - BN_free (x); BN_free (y); - } } } diff --git a/Signature.h b/Signature.h index 91f0e66b..62a36cd3 100644 --- a/Signature.h +++ b/Signature.h @@ -452,10 +452,26 @@ namespace crypto { public: - GOSTR3410Verifier (GOSTR3410ParamSet paramSet, const uint8_t * signingKey); + GOSTR3410Verifier (GOSTR3410ParamSet paramSet, const uint8_t * signingKey) + { + BIGNUM * x = BN_bin2bn (signingKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, NULL); + BIGNUM * y = BN_bin2bn (signingKey + GOSTR3410_PUBLIC_KEY_LENGTH/2, GOSTR3410_PUBLIC_KEY_LENGTH/2, NULL); + m_PublicKey = GetGOSTR3410Curve (m_ParamSet)->CreatePoint (x, y); + BN_free (x); BN_free (y); + } ~GOSTR3410Verifier () { EC_POINT_free (m_PublicKey); } - bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const; + bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const + { + uint8_t digest[32]; + SHA256 (buf, len, digest); // TODO: use GOST 34.11 + BIGNUM * d = BN_bin2bn (digest, 32, nullptr); + BIGNUM * r = BN_bin2bn (signature, GetSignatureLen ()/2, NULL); + BIGNUM * s = BN_bin2bn (signature + GetSignatureLen ()/2, GetSignatureLen ()/2, NULL); + bool ret = GetGOSTR3410Curve (m_ParamSet)->Verify (m_PublicKey, d, r, s); + BN_free (d); BN_free (r); BN_free (s); + return ret; + } size_t GetPublicKeyLen () const { return GOSTR3410_PUBLIC_KEY_LENGTH; } size_t GetSignatureLen () const { return GOSTR3410_SIGNATURE_LENGTH; } @@ -477,7 +493,17 @@ namespace crypto } ~GOSTR3410Signer () { BN_free (m_PrivateKey); } - void Sign (const uint8_t * buf, int len, uint8_t * signature) const; + void Sign (const uint8_t * buf, int len, uint8_t * signature) const + { + uint8_t digest[32]; + SHA256 (buf, len, digest); // TODO: use GOST 34.11 + BIGNUM * d = BN_bin2bn (digest, 32, nullptr); + BIGNUM * r = BN_new (), * s = BN_new (); + GetGOSTR3410Curve (m_ParamSet)->Sign (m_PrivateKey, d, r, s); + bn2buf (r, signature, GOSTR3410_SIGNATURE_LENGTH/2); + bn2buf (s, signature + GOSTR3410_SIGNATURE_LENGTH/2, GOSTR3410_SIGNATURE_LENGTH/2); + BN_free (d); BN_free (r); BN_free (s); + } private: @@ -485,7 +511,20 @@ namespace crypto BIGNUM * m_PrivateKey; }; - void CreateGOSTR3410RandomKeys (GOSTR3410ParamSet paramSet, uint8_t * signingPrivateKey, uint8_t * signingPublicKey); + inline void CreateGOSTR3410RandomKeys (GOSTR3410ParamSet paramSet, uint8_t * signingPrivateKey, uint8_t * signingPublicKey) + { + RAND_bytes (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); + BIGNUM * priv = BN_bin2bn (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, nullptr); + const auto& curve = GetGOSTR3410Curve (paramSet); + auto pub = curve->MulP (priv); + BN_free (priv); + BIGNUM * x = BN_new (), * y = BN_new (); + curve->GetXY (pub, x, y); + EC_POINT_free (pub); + bn2buf (x, signingPublicKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); + bn2buf (y, signingPublicKey + GOSTR3410_PUBLIC_KEY_LENGTH/2, GOSTR3410_PUBLIC_KEY_LENGTH/2); + BN_free (x); BN_free (y); + } } } From e87724703268edee2d6095c9db65a725defe7966 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 14 Mar 2017 16:41:06 -0400 Subject: [PATCH 28/39] support of GOST 34.10-2012 512 bits --- Gost.cpp | 1 + Gost.h | 2 ++ Identity.cpp | 12 +++++------ Signature.h | 58 +++++++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 57 insertions(+), 16 deletions(-) diff --git a/Gost.cpp b/Gost.cpp index 63625baa..6c5d7f24 100644 --- a/Gost.cpp +++ b/Gost.cpp @@ -12,6 +12,7 @@ namespace crypto GOSTR3410Curve::GOSTR3410Curve (BIGNUM * a, BIGNUM * b, BIGNUM * p, BIGNUM * q, BIGNUM * x, BIGNUM * y) { + m_KeyLen = BN_num_bytes (p); BN_CTX * ctx = BN_CTX_new (); m_Group = EC_GROUP_new_curve_GFp (p, a, b, ctx); EC_POINT * P = EC_POINT_new (m_Group); diff --git a/Gost.h b/Gost.h index 62a8aea1..d0f63ed7 100644 --- a/Gost.h +++ b/Gost.h @@ -33,6 +33,7 @@ namespace crypto GOSTR3410Curve (BIGNUM * a, BIGNUM * b, BIGNUM * p, BIGNUM * q, BIGNUM * x, BIGNUM * y); ~GOSTR3410Curve (); + size_t GetKeyLen () const { return m_KeyLen; }; EC_POINT * MulP (const BIGNUM * n) const; bool GetXY (const EC_POINT * p, BIGNUM * x, BIGNUM * y) const; EC_POINT * CreatePoint (const BIGNUM * x, const BIGNUM * y) const; @@ -42,6 +43,7 @@ namespace crypto private: EC_GROUP * m_Group; + size_t m_KeyLen; // in bytes }; std::unique_ptr& GetGOSTR3410Curve (GOSTR3410ParamSet paramSet); diff --git a/Identity.cpp b/Identity.cpp index e6bf34bf..89eb43f6 100644 --- a/Identity.cpp +++ b/Identity.cpp @@ -382,19 +382,19 @@ namespace data case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: { size_t padding = 128 - i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 - UpdateVerifier (new i2p::crypto::GOSTR3410Verifier (i2p::crypto::eGOSTR3410CryptoProA, m_StandardIdentity.signingKey + padding)); + UpdateVerifier (new i2p::crypto::GOSTR3410_2001_Verifier (i2p::crypto::eGOSTR3410CryptoProA, m_StandardIdentity.signingKey + padding)); break; } case SIGNING_KEY_TYPE_GOSTR3410_B_GOSTR3411: { size_t padding = 128 - i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 - UpdateVerifier (new i2p::crypto::GOSTR3410Verifier (i2p::crypto::eGOSTR3410CryptoProB, m_StandardIdentity.signingKey + padding)); + UpdateVerifier (new i2p::crypto::GOSTR3410_2001_Verifier (i2p::crypto::eGOSTR3410CryptoProB, m_StandardIdentity.signingKey + padding)); break; } case SIGNING_KEY_TYPE_GOSTR3410_C_GOSTR3411: { size_t padding = 128 - i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 - UpdateVerifier (new i2p::crypto::GOSTR3410Verifier (i2p::crypto::eGOSTR3410CryptoProC, m_StandardIdentity.signingKey + padding)); + UpdateVerifier (new i2p::crypto::GOSTR3410_2001_Verifier (i2p::crypto::eGOSTR3410CryptoProC, m_StandardIdentity.signingKey + padding)); break; } default: @@ -539,13 +539,13 @@ namespace data m_Signer.reset (new i2p::crypto::EDDSA25519Signer (m_SigningPrivateKey, m_Public->GetStandardIdentity ().certificate - i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH)); break; case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: - m_Signer.reset (new i2p::crypto::GOSTR3410Signer (i2p::crypto::eGOSTR3410CryptoProA, m_SigningPrivateKey)); + m_Signer.reset (new i2p::crypto::GOSTR3410_2001_Signer (i2p::crypto::eGOSTR3410CryptoProA, m_SigningPrivateKey)); break; case SIGNING_KEY_TYPE_GOSTR3410_B_GOSTR3411: - m_Signer.reset (new i2p::crypto::GOSTR3410Signer (i2p::crypto::eGOSTR3410CryptoProB, m_SigningPrivateKey)); + m_Signer.reset (new i2p::crypto::GOSTR3410_2001_Signer (i2p::crypto::eGOSTR3410CryptoProB, m_SigningPrivateKey)); break; case SIGNING_KEY_TYPE_GOSTR3410_C_GOSTR3411: - m_Signer.reset (new i2p::crypto::GOSTR3410Signer (i2p::crypto::eGOSTR3410CryptoProC, m_SigningPrivateKey)); + m_Signer.reset (new i2p::crypto::GOSTR3410_2001_Signer (i2p::crypto::eGOSTR3410CryptoProC, m_SigningPrivateKey)); break; default: LogPrint (eLogError, "Identity: Signing key type ", (int)m_Public->GetSigningKeyType (), " is not supported"); diff --git a/Signature.h b/Signature.h index 62a36cd3..c76d15a3 100644 --- a/Signature.h +++ b/Signature.h @@ -443,11 +443,43 @@ namespace crypto memcpy (signingPublicKey, signer.GetPublicKey (), EDDSA25519_PUBLIC_KEY_LENGTH); } - // ГОСТ Р 34.10-2001 - + + // ГОСТ Р 34.11 + struct GOSTR3411_2001_Hash + { + static void CalculateHash (const uint8_t * buf, size_t len, uint8_t * digest) + { + SHA256 (buf, len, digest); // TODO: implement GOST R 34.11 - 2001 + } + + enum { hashLen = 32 }; + }; + + struct GOSTR3411_2012_256_Hash + { + static void CalculateHash (const uint8_t * buf, size_t len, uint8_t * digest) + { + SHA256 (buf, len, digest); // TODO: implement GOST R 34.11 - 2012 + } + + enum { hashLen = 32 }; + }; + + struct GOSTR3411_2012_512_Hash + { + static void CalculateHash (const uint8_t * buf, size_t len, uint8_t * digest) + { + SHA512 (buf, len, digest); // TODO: implement GOST R 34.11 - 2012 + } + + enum { hashLen = 32 }; + }; + + // ГОСТ Р 34.10 const size_t GOSTR3410_PUBLIC_KEY_LENGTH = 64; const size_t GOSTR3410_SIGNATURE_LENGTH = 64; + template class GOSTR3410Verifier: public Verifier { public: @@ -463,8 +495,8 @@ namespace crypto bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const { - uint8_t digest[32]; - SHA256 (buf, len, digest); // TODO: use GOST 34.11 + uint8_t digest[Hash::hashLen]; + Hash::CalculateHash (buf, len, digest); BIGNUM * d = BN_bin2bn (digest, 32, nullptr); BIGNUM * r = BN_bin2bn (signature, GetSignatureLen ()/2, NULL); BIGNUM * s = BN_bin2bn (signature + GetSignatureLen ()/2, GetSignatureLen ()/2, NULL); @@ -482,6 +514,7 @@ namespace crypto EC_POINT * m_PublicKey; }; + template class GOSTR3410Signer: public Signer { public: @@ -495,8 +528,8 @@ namespace crypto void Sign (const uint8_t * buf, int len, uint8_t * signature) const { - uint8_t digest[32]; - SHA256 (buf, len, digest); // TODO: use GOST 34.11 + uint8_t digest[Hash::hashLen]; + Hash::CalculateHash (buf, len, digest); BIGNUM * d = BN_bin2bn (digest, 32, nullptr); BIGNUM * r = BN_new (), * s = BN_new (); GetGOSTR3410Curve (m_ParamSet)->Sign (m_PrivateKey, d, r, s); @@ -513,18 +546,23 @@ namespace crypto inline void CreateGOSTR3410RandomKeys (GOSTR3410ParamSet paramSet, uint8_t * signingPrivateKey, uint8_t * signingPublicKey) { - RAND_bytes (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); - BIGNUM * priv = BN_bin2bn (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, nullptr); const auto& curve = GetGOSTR3410Curve (paramSet); + auto keyLen = curve->GetKeyLen (); + RAND_bytes (signingPrivateKey, keyLen); + BIGNUM * priv = BN_bin2bn (signingPrivateKey, keyLen, nullptr); + auto pub = curve->MulP (priv); BN_free (priv); BIGNUM * x = BN_new (), * y = BN_new (); curve->GetXY (pub, x, y); EC_POINT_free (pub); - bn2buf (x, signingPublicKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); - bn2buf (y, signingPublicKey + GOSTR3410_PUBLIC_KEY_LENGTH/2, GOSTR3410_PUBLIC_KEY_LENGTH/2); + bn2buf (x, signingPublicKey, keyLen); + bn2buf (y, signingPublicKey + keyLen, keyLen); BN_free (x); BN_free (y); } + + typedef GOSTR3410Verifier GOSTR3410_2001_Verifier; + typedef GOSTR3410Signer GOSTR3410_2001_Signer; } } From 6369a900da9314d25da0fe94336814ebed33d5bc Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 14 Mar 2017 20:03:44 -0400 Subject: [PATCH 29/39] added TC26 param sets for GOST R 34.10-2012 --- Gost.h | 4 ++-- Identity.cpp | 53 +++++++++++++++++++++++++++++----------------------- Identity.h | 6 +++--- Signature.h | 35 +++++++++++++++++++++------------- 4 files changed, 57 insertions(+), 41 deletions(-) diff --git a/Gost.h b/Gost.h index d0f63ed7..eb8e79f6 100644 --- a/Gost.h +++ b/Gost.h @@ -21,8 +21,8 @@ namespace crypto //eGOSTR3410CryptoProXchA, // 1.2.643.2.2.36.0 //eGOSTR3410CryptoProXchB, // 1.2.643.2.2.36.1 // GOST R 34.10-2012 - eGOSTR3410tc26paramSetA256, // 1.2.643.7.1.2.1.1.1 - eGOSTR3410tc26paramSetC512, // 1.2.643.7.1.2.1.2.3 + eGOSTR3410TC26A256, // 1.2.643.7.1.2.1.1.1 + eGOSTR3410TC26C512, // 1.2.643.7.1.2.1.2.3 eGOSTR3410NumParamSets }; diff --git a/Identity.cpp b/Identity.cpp index 89eb43f6..4c05be24 100644 --- a/Identity.cpp +++ b/Identity.cpp @@ -102,13 +102,20 @@ namespace data memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH); break; } - case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: - case SIGNING_KEY_TYPE_GOSTR3410_B_GOSTR3411: - case SIGNING_KEY_TYPE_GOSTR3410_C_GOSTR3411: + case SIGNING_KEY_TYPE_GOSTR3410_2001_CRYPTO_PRO_A_GOSTR3411: + case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_A_256_GOSTR3411: { - size_t padding = 128 - i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 + // 256 + size_t padding = 128 - i2p::crypto::GOSTR3410_256_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 RAND_bytes (m_StandardIdentity.signingKey, padding); - memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH); + memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::GOSTR3410_256_PUBLIC_KEY_LENGTH); + break; + } + case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_C_512_GOSTR3411: + { + // 512 + // no padding, key length is 128 + memcpy (m_StandardIdentity.signingKey, signingKey, i2p::crypto::GOSTR3410_512_PUBLIC_KEY_LENGTH); break; } default: @@ -379,22 +386,22 @@ namespace data UpdateVerifier (new i2p::crypto::EDDSA25519Verifier (m_StandardIdentity.signingKey + padding)); break; } - case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: + case SIGNING_KEY_TYPE_GOSTR3410_2001_CRYPTO_PRO_A_GOSTR3411: { - size_t padding = 128 - i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 + size_t padding = 128 - i2p::crypto::GOSTR3410_256_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 UpdateVerifier (new i2p::crypto::GOSTR3410_2001_Verifier (i2p::crypto::eGOSTR3410CryptoProA, m_StandardIdentity.signingKey + padding)); break; } - case SIGNING_KEY_TYPE_GOSTR3410_B_GOSTR3411: + case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_A_256_GOSTR3411: { - size_t padding = 128 - i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 - UpdateVerifier (new i2p::crypto::GOSTR3410_2001_Verifier (i2p::crypto::eGOSTR3410CryptoProB, m_StandardIdentity.signingKey + padding)); + size_t padding = 128 - i2p::crypto::GOSTR3410_256_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 + UpdateVerifier (new i2p::crypto::GOSTR3410_2012_256_Verifier (i2p::crypto::eGOSTR3410TC26A256, m_StandardIdentity.signingKey + padding)); break; } - case SIGNING_KEY_TYPE_GOSTR3410_C_GOSTR3411: + case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_C_512_GOSTR3411: { - size_t padding = 128 - i2p::crypto::GOSTR3410_PUBLIC_KEY_LENGTH; // 64 = 128 - 64 - UpdateVerifier (new i2p::crypto::GOSTR3410_2001_Verifier (i2p::crypto::eGOSTR3410CryptoProC, m_StandardIdentity.signingKey + padding)); + // zero padding + UpdateVerifier (new i2p::crypto::GOSTR3410_2012_512_Verifier (i2p::crypto::eGOSTR3410TC26C512, m_StandardIdentity.signingKey)); break; } default: @@ -538,14 +545,14 @@ namespace data case SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519: m_Signer.reset (new i2p::crypto::EDDSA25519Signer (m_SigningPrivateKey, m_Public->GetStandardIdentity ().certificate - i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH)); break; - case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: + case SIGNING_KEY_TYPE_GOSTR3410_2001_CRYPTO_PRO_A_GOSTR3411: m_Signer.reset (new i2p::crypto::GOSTR3410_2001_Signer (i2p::crypto::eGOSTR3410CryptoProA, m_SigningPrivateKey)); break; - case SIGNING_KEY_TYPE_GOSTR3410_B_GOSTR3411: - m_Signer.reset (new i2p::crypto::GOSTR3410_2001_Signer (i2p::crypto::eGOSTR3410CryptoProB, m_SigningPrivateKey)); + case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_A_256_GOSTR3411: + m_Signer.reset (new i2p::crypto::GOSTR3410_2012_256_Signer (i2p::crypto::eGOSTR3410TC26A256, m_SigningPrivateKey)); break; - case SIGNING_KEY_TYPE_GOSTR3410_C_GOSTR3411: - m_Signer.reset (new i2p::crypto::GOSTR3410_2001_Signer (i2p::crypto::eGOSTR3410CryptoProC, m_SigningPrivateKey)); + case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_C_512_GOSTR3411: + m_Signer.reset (new i2p::crypto::GOSTR3410_2012_512_Signer (i2p::crypto::eGOSTR3410TC26C512, m_SigningPrivateKey)); break; default: LogPrint (eLogError, "Identity: Signing key type ", (int)m_Public->GetSigningKeyType (), " is not supported"); @@ -582,14 +589,14 @@ namespace data case SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519: i2p::crypto::CreateEDDSA25519RandomKeys (keys.m_SigningPrivateKey, signingPublicKey); break; - case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: + case SIGNING_KEY_TYPE_GOSTR3410_2001_CRYPTO_PRO_A_GOSTR3411: i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410CryptoProA, keys.m_SigningPrivateKey, signingPublicKey); break; - case SIGNING_KEY_TYPE_GOSTR3410_B_GOSTR3411: - i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410CryptoProB, keys.m_SigningPrivateKey, signingPublicKey); + case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_A_256_GOSTR3411: + i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410TC26A256, keys.m_SigningPrivateKey, signingPublicKey); break; - case SIGNING_KEY_TYPE_GOSTR3410_C_GOSTR3411: - i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410CryptoProC, keys.m_SigningPrivateKey, signingPublicKey); + case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_C_512_GOSTR3411: + i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410TC26C512, keys.m_SigningPrivateKey, signingPublicKey); break; default: LogPrint (eLogError, "Identity: Signing key type ", (int)type, " is not supported. Create DSA-SHA1"); diff --git a/Identity.h b/Identity.h index 479fd5d1..5751d567 100644 --- a/Identity.h +++ b/Identity.h @@ -61,9 +61,9 @@ namespace data const uint16_t SIGNING_KEY_TYPE_RSA_SHA512_4096 = 6; const uint16_t SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519 = 7; // following signature type should never appear in netid=2 - const uint16_t SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411 = 65280; // approved by FSB - const uint16_t SIGNING_KEY_TYPE_GOSTR3410_B_GOSTR3411 = 65281; - const uint16_t SIGNING_KEY_TYPE_GOSTR3410_C_GOSTR3411 = 65282; + const uint16_t SIGNING_KEY_TYPE_GOSTR3410_2001_CRYPTO_PRO_A_GOSTR3411 = 65280; // approved by FSB + const uint16_t SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_A_256_GOSTR3411 = 65281; + const uint16_t SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_C_512_GOSTR3411 = 65282; typedef uint16_t SigningKeyType; typedef uint16_t CryptoKeyType; diff --git a/Signature.h b/Signature.h index c76d15a3..9a8bbb06 100644 --- a/Signature.h +++ b/Signature.h @@ -472,22 +472,25 @@ namespace crypto SHA512 (buf, len, digest); // TODO: implement GOST R 34.11 - 2012 } - enum { hashLen = 32 }; + enum { hashLen = 64 }; }; // ГОСТ Р 34.10 - const size_t GOSTR3410_PUBLIC_KEY_LENGTH = 64; - const size_t GOSTR3410_SIGNATURE_LENGTH = 64; + const size_t GOSTR3410_256_PUBLIC_KEY_LENGTH = 64; + const size_t GOSTR3410_512_PUBLIC_KEY_LENGTH = 128; template class GOSTR3410Verifier: public Verifier { public: - GOSTR3410Verifier (GOSTR3410ParamSet paramSet, const uint8_t * signingKey) + enum { keyLen = Hash::hashLen }; + + GOSTR3410Verifier (GOSTR3410ParamSet paramSet, const uint8_t * signingKey): + m_ParamSet (paramSet) { - BIGNUM * x = BN_bin2bn (signingKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, NULL); - BIGNUM * y = BN_bin2bn (signingKey + GOSTR3410_PUBLIC_KEY_LENGTH/2, GOSTR3410_PUBLIC_KEY_LENGTH/2, NULL); + BIGNUM * x = BN_bin2bn (signingKey, GetPublicKeyLen ()/2, NULL); + BIGNUM * y = BN_bin2bn (signingKey + GetPublicKeyLen ()/2, GetPublicKeyLen ()/2, NULL); m_PublicKey = GetGOSTR3410Curve (m_ParamSet)->CreatePoint (x, y); BN_free (x); BN_free (y); } @@ -497,16 +500,16 @@ namespace crypto { uint8_t digest[Hash::hashLen]; Hash::CalculateHash (buf, len, digest); - BIGNUM * d = BN_bin2bn (digest, 32, nullptr); + BIGNUM * d = BN_bin2bn (digest, Hash::hashLen, nullptr); BIGNUM * r = BN_bin2bn (signature, GetSignatureLen ()/2, NULL); BIGNUM * s = BN_bin2bn (signature + GetSignatureLen ()/2, GetSignatureLen ()/2, NULL); bool ret = GetGOSTR3410Curve (m_ParamSet)->Verify (m_PublicKey, d, r, s); BN_free (d); BN_free (r); BN_free (s); return ret; } - - size_t GetPublicKeyLen () const { return GOSTR3410_PUBLIC_KEY_LENGTH; } - size_t GetSignatureLen () const { return GOSTR3410_SIGNATURE_LENGTH; } + + size_t GetPublicKeyLen () const { return keyLen*2; } + size_t GetSignatureLen () const { return keyLen*2; } private: @@ -519,10 +522,12 @@ namespace crypto { public: + enum { keyLen = Hash::hashLen }; + GOSTR3410Signer (GOSTR3410ParamSet paramSet, const uint8_t * signingPrivateKey): m_ParamSet (paramSet) { - m_PrivateKey = BN_bin2bn (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, nullptr); + m_PrivateKey = BN_bin2bn (signingPrivateKey, keyLen, nullptr); } ~GOSTR3410Signer () { BN_free (m_PrivateKey); } @@ -533,8 +538,8 @@ namespace crypto BIGNUM * d = BN_bin2bn (digest, 32, nullptr); BIGNUM * r = BN_new (), * s = BN_new (); GetGOSTR3410Curve (m_ParamSet)->Sign (m_PrivateKey, d, r, s); - bn2buf (r, signature, GOSTR3410_SIGNATURE_LENGTH/2); - bn2buf (s, signature + GOSTR3410_SIGNATURE_LENGTH/2, GOSTR3410_SIGNATURE_LENGTH/2); + bn2buf (r, signature, keyLen); + bn2buf (s, signature + keyLen, keyLen); BN_free (d); BN_free (r); BN_free (s); } @@ -563,6 +568,10 @@ namespace crypto typedef GOSTR3410Verifier GOSTR3410_2001_Verifier; typedef GOSTR3410Signer GOSTR3410_2001_Signer; + typedef GOSTR3410Verifier GOSTR3410_2012_256_Verifier; + typedef GOSTR3410Signer GOSTR3410_2012_256_Signer; + typedef GOSTR3410Verifier GOSTR3410_2012_512_Verifier; + typedef GOSTR3410Signer GOSTR3410_2012_512_Signer; } } From af2a3f3a65cf1decc35eca56238ad7c73d0cdcd4 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 14 Mar 2017 21:14:23 -0400 Subject: [PATCH 30/39] Gost.cpp added --- build/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/build/CMakeLists.txt b/build/CMakeLists.txt index 2baa02aa..32aff387 100644 --- a/build/CMakeLists.txt +++ b/build/CMakeLists.txt @@ -62,6 +62,7 @@ set (LIBI2PD_SRC "${CMAKE_SOURCE_DIR}/Timestamp.cpp" "${CMAKE_SOURCE_DIR}/api.cpp" "${CMAKE_SOURCE_DIR}/Event.cpp" + "${CMAKE_SOURCE_DIR}/Gost.cpp" ) if (WITH_WEBSOCKETS) From da2c49ab66528cb8a1c0a85cfacc42b0aa964a2a Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 14 Mar 2017 21:15:17 -0400 Subject: [PATCH 31/39] tc26 paramSetA 512 for GOST 34.1002012 --- Gost.cpp | 8 ++++++++ Gost.h | 1 + Signature.h | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Gost.cpp b/Gost.cpp index 6c5d7f24..b142dbb3 100644 --- a/Gost.cpp +++ b/Gost.cpp @@ -131,6 +131,14 @@ namespace crypto "91E38443A5E82C0D880923425712B2BB658B9196932E02C78B2582FE742DAA28", "32879423AB1A0375895786C4BB46E9565FDE0B5344766740AF268ADB32322E5C" }, // tc26-2012-paramSetA-256 + { + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC4", + "E8C2505DEDFC86DDC1BD0B2B6667F1DA34B82574761CB0E879BD081CFD0B6265EE3CB090F30D27614CB4574010DA90DD862EF9D4EBEE4761503190785A71C760", + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC7", + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF27E69532F48D89116FF22B8D4E0560609B4B38ABFAD2B85DCACDB1411F10B275", + "3", + "7503CFE87A836AE3A61B8816E25450E6CE5E1C93ACF1ABC1778064FDCBEFA921DF1626BE4FD036E93D75E6A50E3A41E98028FE5FC235F5B889A589CB5215F2A" + }, // tc26-2012-paramSetA-512 { "DC9203E514A721875485A529D2C722FB187BC8980EB866644DE41C68E143064546E861C0E2C9EDD92ADE71F46FCF50FF2AD97F951FDA9F2A2EB6546F39689BD3", "B4C4EE28CEBC6C2C8AC12952CF37F16AC7EFB6A9F69F4B57FFDA2E4F0DE5ADE038CBC2FFF719D2C18DE0284B8BFEF3B52B8CC7A5F5BF0A3C8D2319A5312557E1", diff --git a/Gost.h b/Gost.h index eb8e79f6..39a57bb2 100644 --- a/Gost.h +++ b/Gost.h @@ -22,6 +22,7 @@ namespace crypto //eGOSTR3410CryptoProXchB, // 1.2.643.2.2.36.1 // GOST R 34.10-2012 eGOSTR3410TC26A256, // 1.2.643.7.1.2.1.1.1 + eGOSTR3410TC26A512, // 1.2.643.7.1.2.1.2.1 eGOSTR3410TC26C512, // 1.2.643.7.1.2.1.2.3 eGOSTR3410NumParamSets }; diff --git a/Signature.h b/Signature.h index 9a8bbb06..aee8c487 100644 --- a/Signature.h +++ b/Signature.h @@ -535,7 +535,7 @@ namespace crypto { uint8_t digest[Hash::hashLen]; Hash::CalculateHash (buf, len, digest); - BIGNUM * d = BN_bin2bn (digest, 32, nullptr); + BIGNUM * d = BN_bin2bn (digest, Hash::hashLen, nullptr); BIGNUM * r = BN_new (), * s = BN_new (); GetGOSTR3410Curve (m_ParamSet)->Sign (m_PrivateKey, d, r, s); bn2buf (r, signature, keyLen); From 6b9a2705064d2c5d330b3387f6f3eea6dd2f47db Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 15 Mar 2017 12:41:02 -0400 Subject: [PATCH 32/39] enable TC26 paramSet A 512 for GOST 34.10-2012 --- Gost.cpp | 28 ++-------------------------- Gost.h | 3 --- Identity.cpp | 14 +++++++------- Identity.h | 2 +- 4 files changed, 10 insertions(+), 37 deletions(-) diff --git a/Gost.cpp b/Gost.cpp index b142dbb3..b68e2397 100644 --- a/Gost.cpp +++ b/Gost.cpp @@ -107,22 +107,6 @@ namespace crypto "1", "8D91E471E0989CDA27DF505A453F2B7635294F2DDF23E3B122ACC99C9E9F1E14" }, // A - { - "8000000000000000000000000000000000000000000000000000000000000C96", - "3E1AF419A269A5F866A7D3C25C3DF80AE979259373FF2B182F49D4CE7E1BBC8B", - "8000000000000000000000000000000000000000000000000000000000000C99", - "800000000000000000000000000000015F700CFFF1A624E5E497161BCC8A198F", - "1", - "3FA8124359F96680B83D1C3EB2C070E5C545C9858D03ECFB744BF8D717717EFC" - }, // B - { - "9B9F605F5A858107AB1EC85E6B41C8AACF846E86789051D37998F7B9022D7598", - "805A", - "9B9F605F5A858107AB1EC85E6B41C8AACF846E86789051D37998F7B9022D759B", - "9B9F605F5A858107AB1EC85E6B41C8AA582CA3511EDDFB74F02F3A6598980BB9", - "0", - "41ECE55743711A8C3CBF3783CD08C0EE4D4DC440D4641A8F366E550DFDB3BB67" - }, // C { "C2173F1513981673AF4892C23035A27CE25E2013BF95AA33B22C656F277E7335", "295F9BAE7428ED9CCC20E7C359A9D41A22FCCD9108E17BF7BA9337A6F8AE9513", @@ -137,16 +121,8 @@ namespace crypto "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC7", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF27E69532F48D89116FF22B8D4E0560609B4B38ABFAD2B85DCACDB1411F10B275", "3", - "7503CFE87A836AE3A61B8816E25450E6CE5E1C93ACF1ABC1778064FDCBEFA921DF1626BE4FD036E93D75E6A50E3A41E98028FE5FC235F5B889A589CB5215F2A" - }, // tc26-2012-paramSetA-512 - { - "DC9203E514A721875485A529D2C722FB187BC8980EB866644DE41C68E143064546E861C0E2C9EDD92ADE71F46FCF50FF2AD97F951FDA9F2A2EB6546F39689BD3", - "B4C4EE28CEBC6C2C8AC12952CF37F16AC7EFB6A9F69F4B57FFDA2E4F0DE5ADE038CBC2FFF719D2C18DE0284B8BFEF3B52B8CC7A5F5BF0A3C8D2319A5312557E1", - "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC7", - "3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC98CDBA46506AB004C33A9FF5147502CC8EDA9E7A769A12694623CEF47F023ED", - "E2E31EDFC23DE7BDEBE241CE593EF5DE2295B7A9CBAEF021D385F7074CEA043AA27272A7AE602BF2A7B9033DB9ED3610C6FB85487EAE97AAC5BC7928C1950148", - "F5CE40D95B5EB899ABBCCFF5911CB8577939804D6527378B8C108C3D2090FF9BE18E2D33E3021ED27EF32D85822423B6304F726AA854BAE07D0396E9A9ADDC40F" - } // tc26-2012-paramSetC-256 + "7503CFE87A836AE3A61B8816E25450E6CE5E1C93ACF1ABC1778064FDCBEFA921DF1626BE4FD036E93D75E6A50E3A41E98028FE5FC235F5B889A589CB5215F2A4" + } // tc26-2012-paramSetA-512 }; BIGNUM * a = nullptr, * b = nullptr, * p = nullptr, * q =nullptr, * x = nullptr, * y = nullptr; diff --git a/Gost.h b/Gost.h index 39a57bb2..84ac82d1 100644 --- a/Gost.h +++ b/Gost.h @@ -15,15 +15,12 @@ namespace crypto { // GOST R 34.10-2001 eGOSTR3410CryptoProA = 0, // 1.2.643.2.2.35.1 - eGOSTR3410CryptoProB, // 1.2.643.2.2.35.2 - eGOSTR3410CryptoProC, // 1.2.643.2.2.35.3 // XchA = A, XchB = C //eGOSTR3410CryptoProXchA, // 1.2.643.2.2.36.0 //eGOSTR3410CryptoProXchB, // 1.2.643.2.2.36.1 // GOST R 34.10-2012 eGOSTR3410TC26A256, // 1.2.643.7.1.2.1.1.1 eGOSTR3410TC26A512, // 1.2.643.7.1.2.1.2.1 - eGOSTR3410TC26C512, // 1.2.643.7.1.2.1.2.3 eGOSTR3410NumParamSets }; diff --git a/Identity.cpp b/Identity.cpp index 4c05be24..b5b72c43 100644 --- a/Identity.cpp +++ b/Identity.cpp @@ -111,7 +111,7 @@ namespace data memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::GOSTR3410_256_PUBLIC_KEY_LENGTH); break; } - case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_C_512_GOSTR3411: + case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_A_512_GOSTR3411: { // 512 // no padding, key length is 128 @@ -398,10 +398,10 @@ namespace data UpdateVerifier (new i2p::crypto::GOSTR3410_2012_256_Verifier (i2p::crypto::eGOSTR3410TC26A256, m_StandardIdentity.signingKey + padding)); break; } - case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_C_512_GOSTR3411: + case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_A_512_GOSTR3411: { // zero padding - UpdateVerifier (new i2p::crypto::GOSTR3410_2012_512_Verifier (i2p::crypto::eGOSTR3410TC26C512, m_StandardIdentity.signingKey)); + UpdateVerifier (new i2p::crypto::GOSTR3410_2012_512_Verifier (i2p::crypto::eGOSTR3410TC26A512, m_StandardIdentity.signingKey)); break; } default: @@ -551,8 +551,8 @@ namespace data case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_A_256_GOSTR3411: m_Signer.reset (new i2p::crypto::GOSTR3410_2012_256_Signer (i2p::crypto::eGOSTR3410TC26A256, m_SigningPrivateKey)); break; - case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_C_512_GOSTR3411: - m_Signer.reset (new i2p::crypto::GOSTR3410_2012_512_Signer (i2p::crypto::eGOSTR3410TC26C512, m_SigningPrivateKey)); + case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_A_512_GOSTR3411: + m_Signer.reset (new i2p::crypto::GOSTR3410_2012_512_Signer (i2p::crypto::eGOSTR3410TC26A512, m_SigningPrivateKey)); break; default: LogPrint (eLogError, "Identity: Signing key type ", (int)m_Public->GetSigningKeyType (), " is not supported"); @@ -595,8 +595,8 @@ namespace data case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_A_256_GOSTR3411: i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410TC26A256, keys.m_SigningPrivateKey, signingPublicKey); break; - case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_C_512_GOSTR3411: - i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410TC26C512, keys.m_SigningPrivateKey, signingPublicKey); + case SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_A_512_GOSTR3411: + i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410TC26A512, keys.m_SigningPrivateKey, signingPublicKey); break; default: LogPrint (eLogError, "Identity: Signing key type ", (int)type, " is not supported. Create DSA-SHA1"); diff --git a/Identity.h b/Identity.h index 5751d567..1c3c8c18 100644 --- a/Identity.h +++ b/Identity.h @@ -63,7 +63,7 @@ namespace data // following signature type should never appear in netid=2 const uint16_t SIGNING_KEY_TYPE_GOSTR3410_2001_CRYPTO_PRO_A_GOSTR3411 = 65280; // approved by FSB const uint16_t SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_A_256_GOSTR3411 = 65281; - const uint16_t SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_C_512_GOSTR3411 = 65282; + const uint16_t SIGNING_KEY_TYPE_GOSTR3410_2012_TC26_A_512_GOSTR3411 = 65282; typedef uint16_t SigningKeyType; typedef uint16_t CryptoKeyType; From 4b76c76712d6c91146bc59162b15f45953c56881 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 15 Mar 2017 14:59:17 -0400 Subject: [PATCH 33/39] correct x for tc26 paramSetA 256 --- Gost.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gost.cpp b/Gost.cpp index b68e2397..027d63f1 100644 --- a/Gost.cpp +++ b/Gost.cpp @@ -112,7 +112,7 @@ namespace crypto "295F9BAE7428ED9CCC20E7C359A9D41A22FCCD9108E17BF7BA9337A6F8AE9513", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD97", "400000000000000000000000000000000FD8CDDFC87B6635C115AF556C360C67", - "91E38443A5E82C0D880923425712B2BB658B9196932E02C78B2582FE742DAA28", + "0D", "32879423AB1A0375895786C4BB46E9565FDE0B5344766740AF268ADB32322E5C" }, // tc26-2012-paramSetA-256 { From 8de8de1b1e1cb2dcbf75703bba56ed951feba3d1 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 15 Mar 2017 20:46:27 -0400 Subject: [PATCH 34/39] GOST 34.11-2012 functions --- Gost.cpp | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 201 insertions(+), 1 deletion(-) diff --git a/Gost.cpp b/Gost.cpp index 027d63f1..67d625c6 100644 --- a/Gost.cpp +++ b/Gost.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -149,6 +150,205 @@ namespace crypto delete c; } return g_GOSTR3410Curves[paramSet]; - } + } + +// ГОСТ 34.11-2012 + + static const uint8_t sbox_[256] = + { + 0xFC, 0xEE, 0xDD, 0x11, 0xCF, 0x6E, 0x31, 0x16, 0xFB, 0xC4, 0xFA, 0xDA, 0x23, 0xC5, 0x04, 0x4D, + 0xE9, 0x77, 0xF0, 0xDB, 0x93, 0x2E, 0x99, 0xBA, 0x17, 0x36, 0xF1, 0xBB, 0x14, 0xCD, 0x5F, 0xC1, + 0xF9, 0x18, 0x65, 0x5A, 0xE2, 0x5C, 0xEF, 0x21, 0x81, 0x1C, 0x3C, 0x42, 0x8B, 0x01, 0x8E, 0x4F, + 0x05, 0x84, 0x02, 0xAE, 0xE3, 0x6A, 0x8F, 0xA0, 0x06, 0x0B, 0xED, 0x98, 0x7F, 0xD4, 0xD3, 0x1F, + 0xEB, 0x34, 0x2C, 0x51, 0xEA, 0xC8, 0x48, 0xAB, 0xF2, 0x2A, 0x68, 0xA2, 0xFD, 0x3A, 0xCE, 0xCC, + 0xB5, 0x70, 0x0E, 0x56, 0x08, 0x0C, 0x76, 0x12, 0xBF, 0x72, 0x13, 0x47, 0x9C, 0xB7, 0x5D, 0x87, + 0x15, 0xA1, 0x96, 0x29, 0x10, 0x7B, 0x9A, 0xC7, 0xF3, 0x91, 0x78, 0x6F, 0x9D, 0x9E, 0xB2, 0xB1, + 0x32, 0x75, 0x19, 0x3D, 0xFF, 0x35, 0x8A, 0x7E, 0x6D, 0x54, 0xC6, 0x80, 0xC3, 0xBD, 0x0D, 0x57, + 0xDF, 0xF5, 0x24, 0xA9, 0x3E, 0xA8, 0x43, 0xC9, 0xD7, 0x79, 0xD6, 0xF6, 0x7C, 0x22, 0xB9, 0x03, + 0xE0, 0x0F, 0xEC, 0xDE, 0x7A, 0x94, 0xB0, 0xBC, 0xDC, 0xE8, 0x28, 0x50, 0x4E, 0x33, 0x0A, 0x4A, + 0xA7, 0x97, 0x60, 0x73, 0x1E, 0x00, 0x62, 0x44, 0x1A, 0xB8, 0x38, 0x82, 0x64, 0x9F, 0x26, 0x41, + 0xAD, 0x45, 0x46, 0x92, 0x27, 0x5E, 0x55, 0x2F, 0x8C, 0xA3, 0xA5, 0x7D, 0x69, 0xD5, 0x95, 0x3B, + 0x07, 0x58, 0xB3, 0x40, 0x86, 0xAC, 0x1D, 0xF7, 0x30, 0x37, 0x6B, 0xE4, 0x88, 0xD9, 0xE7, 0x89, + 0xE1, 0x1B, 0x83, 0x49, 0x4C, 0x3F, 0xF8, 0xFE, 0x8D, 0x53, 0xAA, 0x90, 0xCA, 0xD8, 0x85, 0x61, + 0x20, 0x71, 0x67, 0xA4, 0x2D, 0x2B, 0x09, 0x5B, 0xCB, 0x9B, 0x25, 0xD0, 0xBE, 0xE5, 0x6C, 0x52, + 0x59, 0xA6, 0x74, 0xD2, 0xE6, 0xF4, 0xB4, 0xC0, 0xD1, 0x66, 0xAF, 0xC2, 0x39, 0x4B, 0x63, 0xB6 + }; + + static const uint64_t A_[64] = + { + 0x8e20faa72ba0b470, 0x47107ddd9b505a38, 0xad08b0e0c3282d1c, 0xd8045870ef14980e, + 0x6c022c38f90a4c07, 0x3601161cf205268d, 0x1b8e0b0e798c13c8, 0x83478b07b2468764, + 0xa011d380818e8f40, 0x5086e740ce47c920, 0x2843fd2067adea10, 0x14aff010bdd87508, + 0x0ad97808d06cb404, 0x05e23c0468365a02, 0x8c711e02341b2d01, 0x46b60f011a83988e, + 0x90dab52a387ae76f, 0x486dd4151c3dfdb9, 0x24b86a840e90f0d2, 0x125c354207487869, + 0x092e94218d243cba, 0x8a174a9ec8121e5d, 0x4585254f64090fa0, 0xaccc9ca9328a8950, + 0x9d4df05d5f661451, 0xc0a878a0a1330aa6, 0x60543c50de970553, 0x302a1e286fc58ca7, + 0x18150f14b9ec46dd, 0x0c84890ad27623e0, 0x0642ca05693b9f70, 0x0321658cba93c138, + 0x86275df09ce8aaa8, 0x439da0784e745554, 0xafc0503c273aa42a, 0xd960281e9d1d5215, + 0xe230140fc0802984, 0x71180a8960409a42, 0xb60c05ca30204d21, 0x5b068c651810a89e, + 0x456c34887a3805b9, 0xac361a443d1c8cd2, 0x561b0d22900e4669, 0x2b838811480723ba, + 0x9bcf4486248d9f5d, 0xc3e9224312c8c1a0, 0xeffa11af0964ee50, 0xf97d86d98a327728, + 0xe4fa2054a80b329c, 0x727d102a548b194e, 0x39b008152acb8227, 0x9258048415eb419d, + 0x492c024284fbaec0, 0xaa16012142f35760, 0x550b8e9e21f7a530, 0xa48b474f9ef5dc18, + 0x70a6a56e2440598e, 0x3853dc371220a247, 0x1ca76e95091051ad, 0x0edd37c48a08a6d8, + 0x07e095624504536c, 0x8d70c431ac02a736, 0xc83862965601dd1b, 0x641c314b2b8ee083 + }; + + static const uint8_t C_[12][64] = + { + { + 0xb1,0x08,0x5b,0xda,0x1e,0xca,0xda,0xe9,0xeb,0xcb,0x2f,0x81,0xc0,0x65,0x7c,0x1f, + 0x2f,0x6a,0x76,0x43,0x2e,0x45,0xd0,0x16,0x71,0x4e,0xb8,0x8d,0x75,0x85,0xc4,0xfc, + 0x4b,0x7c,0xe0,0x91,0x92,0x67,0x69,0x01,0xa2,0x42,0x2a,0x08,0xa4,0x60,0xd3,0x15, + 0x05,0x76,0x74,0x36,0xcc,0x74,0x4d,0x23,0xdd,0x80,0x65,0x59,0xf2,0xa6,0x45,0x07 + }, + { + 0x6f,0xa3,0xb5,0x8a,0xa9,0x9d,0x2f,0x1a,0x4f,0xe3,0x9d,0x46,0x0f,0x70,0xb5,0xd7, + 0xf3,0xfe,0xea,0x72,0x0a,0x23,0x2b,0x98,0x61,0xd5,0x5e,0x0f,0x16,0xb5,0x01,0x31, + 0x9a,0xb5,0x17,0x6b,0x12,0xd6,0x99,0x58,0x5c,0xb5,0x61,0xc2,0xdb,0x0a,0xa7,0xca, + 0x55,0xdd,0xa2,0x1b,0xd7,0xcb,0xcd,0x56,0xe6,0x79,0x04,0x70,0x21,0xb1,0x9b,0xb7 + }, + { + 0xf5,0x74,0xdc,0xac,0x2b,0xce,0x2f,0xc7,0x0a,0x39,0xfc,0x28,0x6a,0x3d,0x84,0x35, + 0x06,0xf1,0x5e,0x5f,0x52,0x9c,0x1f,0x8b,0xf2,0xea,0x75,0x14,0xb1,0x29,0x7b,0x7b, + 0xd3,0xe2,0x0f,0xe4,0x90,0x35,0x9e,0xb1,0xc1,0xc9,0x3a,0x37,0x60,0x62,0xdb,0x09, + 0xc2,0xb6,0xf4,0x43,0x86,0x7a,0xdb,0x31,0x99,0x1e,0x96,0xf5,0x0a,0xba,0x0a,0xb2 + }, + { + 0xef,0x1f,0xdf,0xb3,0xe8,0x15,0x66,0xd2,0xf9,0x48,0xe1,0xa0,0x5d,0x71,0xe4,0xdd, + 0x48,0x8e,0x85,0x7e,0x33,0x5c,0x3c,0x7d,0x9d,0x72,0x1c,0xad,0x68,0x5e,0x35,0x3f, + 0xa9,0xd7,0x2c,0x82,0xed,0x03,0xd6,0x75,0xd8,0xb7,0x13,0x33,0x93,0x52,0x03,0xbe, + 0x34,0x53,0xea,0xa1,0x93,0xe8,0x37,0xf1,0x22,0x0c,0xbe,0xbc,0x84,0xe3,0xd1,0x2e + }, + { + 0x4b,0xea,0x6b,0xac,0xad,0x47,0x47,0x99,0x9a,0x3f,0x41,0x0c,0x6c,0xa9,0x23,0x63, + 0x7f,0x15,0x1c,0x1f,0x16,0x86,0x10,0x4a,0x35,0x9e,0x35,0xd7,0x80,0x0f,0xff,0xbd, + 0xbf,0xcd,0x17,0x47,0x25,0x3a,0xf5,0xa3,0xdf,0xff,0x00,0xb7,0x23,0x27,0x1a,0x16, + 0x7a,0x56,0xa2,0x7e,0xa9,0xea,0x63,0xf5,0x60,0x17,0x58,0xfd,0x7c,0x6c,0xfe,0x57 + }, + { + 0xae,0x4f,0xae,0xae,0x1d,0x3a,0xd3,0xd9,0x6f,0xa4,0xc3,0x3b,0x7a,0x30,0x39,0xc0, + 0x2d,0x66,0xc4,0xf9,0x51,0x42,0xa4,0x6c,0x18,0x7f,0x9a,0xb4,0x9a,0xf0,0x8e,0xc6, + 0xcf,0xfa,0xa6,0xb7,0x1c,0x9a,0xb7,0xb4,0x0a,0xf2,0x1f,0x66,0xc2,0xbe,0xc6,0xb6, + 0xbf,0x71,0xc5,0x72,0x36,0x90,0x4f,0x35,0xfa,0x68,0x40,0x7a,0x46,0x64,0x7d,0x6e + }, + { + 0xf4,0xc7,0x0e,0x16,0xee,0xaa,0xc5,0xec,0x51,0xac,0x86,0xfe,0xbf,0x24,0x09,0x54, + 0x39,0x9e,0xc6,0xc7,0xe6,0xbf,0x87,0xc9,0xd3,0x47,0x3e,0x33,0x19,0x7a,0x93,0xc9, + 0x09,0x92,0xab,0xc5,0x2d,0x82,0x2c,0x37,0x06,0x47,0x69,0x83,0x28,0x4a,0x05,0x04, + 0x35,0x17,0x45,0x4c,0xa2,0x3c,0x4a,0xf3,0x88,0x86,0x56,0x4d,0x3a,0x14,0xd4,0x93 + }, + { + 0x9b,0x1f,0x5b,0x42,0x4d,0x93,0xc9,0xa7,0x03,0xe7,0xaa,0x02,0x0c,0x6e,0x41,0x41, + 0x4e,0xb7,0xf8,0x71,0x9c,0x36,0xde,0x1e,0x89,0xb4,0x44,0x3b,0x4d,0xdb,0xc4,0x9a, + 0xf4,0x89,0x2b,0xcb,0x92,0x9b,0x06,0x90,0x69,0xd1,0x8d,0x2b,0xd1,0xa5,0xc4,0x2f, + 0x36,0xac,0xc2,0x35,0x59,0x51,0xa8,0xd9,0xa4,0x7f,0x0d,0xd4,0xbf,0x02,0xe7,0x1e + }, + { + 0x37,0x8f,0x5a,0x54,0x16,0x31,0x22,0x9b,0x94,0x4c,0x9a,0xd8,0xec,0x16,0x5f,0xde, + 0x3a,0x7d,0x3a,0x1b,0x25,0x89,0x42,0x24,0x3c,0xd9,0x55,0xb7,0xe0,0x0d,0x09,0x84, + 0x80,0x0a,0x44,0x0b,0xdb,0xb2,0xce,0xb1,0x7b,0x2b,0x8a,0x9a,0xa6,0x07,0x9c,0x54, + 0x0e,0x38,0xdc,0x92,0xcb,0x1f,0x2a,0x60,0x72,0x61,0x44,0x51,0x83,0x23,0x5a,0xdb + }, + { + 0xab,0xbe,0xde,0xa6,0x80,0x05,0x6f,0x52,0x38,0x2a,0xe5,0x48,0xb2,0xe4,0xf3,0xf3, + 0x89,0x41,0xe7,0x1c,0xff,0x8a,0x78,0xdb,0x1f,0xff,0xe1,0x8a,0x1b,0x33,0x61,0x03, + 0x9f,0xe7,0x67,0x02,0xaf,0x69,0x33,0x4b,0x7a,0x1e,0x6c,0x30,0x3b,0x76,0x52,0xf4, + 0x36,0x98,0xfa,0xd1,0x15,0x3b,0xb6,0xc3,0x74,0xb4,0xc7,0xfb,0x98,0x45,0x9c,0xed + }, + { + 0x7b,0xcd,0x9e,0xd0,0xef,0xc8,0x89,0xfb,0x30,0x02,0xc6,0xcd,0x63,0x5a,0xfe,0x94, + 0xd8,0xfa,0x6b,0xbb,0xeb,0xab,0x07,0x61,0x20,0x01,0x80,0x21,0x14,0x84,0x66,0x79, + 0x8a,0x1d,0x71,0xef,0xea,0x48,0xb9,0xca,0xef,0xba,0xcd,0x1d,0x7d,0x47,0x6e,0x98, + 0xde,0xa2,0x59,0x4a,0xc0,0x6f,0xd8,0x5d,0x6b,0xca,0xa4,0xcd,0x81,0xf3,0x2d,0x1b + }, + { + 0x37,0x8e,0xe7,0x67,0xf1,0x16,0x31,0xba,0xd2,0x13,0x80,0xb0,0x04,0x49,0xb1,0x7a, + 0xcd,0xa4,0x3c,0x32,0xbc,0xdf,0x1d,0x77,0xf8,0x20,0x12,0xd4,0x30,0x21,0x9f,0x9b, + 0x5d,0x80,0xef,0x9d,0x18,0x91,0xcc,0x86,0xe7,0x1d,0xa4,0xaa,0x88,0xe1,0x28,0x52, + 0xfa,0xf4,0x17,0xd5,0xd9,0xb2,0x1b,0x99,0x48,0xbc,0x92,0x4a,0xf1,0x1b,0xd7,0x20 + } + }; + + union GOST3411Block // 8 bytes aligned + { + uint8_t buf[64]; + uint64_t ll[8]; + + GOST3411Block operator^(const GOST3411Block& other) + { + GOST3411Block ret; + for (int i = 0; i < 8; i++) + ret.ll[i] = ll[i]^other.ll[i]; + return ret; + } + + GOST3411Block operator^(const uint8_t * other) + { + GOST3411Block ret; + for (int i = 0; i < 64; i++) + ret.buf[i] = buf[i]^other[i]; + return ret; + } + + GOST3411Block operator+(const GOST3411Block& other) + { + GOST3411Block ret; + uint8_t carry = 0; + for (int i = 63; i >= 0; i--) + { + uint16_t sum = buf[i] + other.buf[i] + carry; + ret.buf[i] = sum & 0xFF; + carry = sum >> 8; + } + return ret; + } + + void S () + { + for (int i = 0; i < 64; i++) + buf[i] = sbox_[buf[i]]; + } + + void LP () + { + uint8_t tmp[64]; + memcpy (tmp, buf, 64); + for (int i = 0; i < 8; i++) + { + uint64_t c = 0; + for (int j = 0; j < 8; j++) + { + uint8_t bit = 0x80; + for (int k = 0; k < 8; k++) + { + + if (tmp[j*8+i] & bit) c ^= A_[j*8+k]; + bit >>= 1; + } + } + for (int j = 0; j < 8; j++) + buf[i*8+j] = c >> ((7 - j) << 3); + } + } + + GOST3411Block E (const GOST3411Block& m) + { + GOST3411Block k = *this; + GOST3411Block res = k^m; + for (int i = 0; i < 12; i++) + { + res.S (); + res.LP (); + k = k^C_[i]; + k.S (); + k.LP (); + res = k^res; + } + return res; + } + }; + + } } From 8b40354786f4041fbeedfa6a2b61f4f3bb206e13 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 16 Mar 2017 13:31:25 -0400 Subject: [PATCH 35/39] GOST R 34.11-2012 implementation --- Gost.cpp | 113 ++++++++++++++++++++++++++++++++++++++++++++++++------- Gost.h | 3 ++ 2 files changed, 103 insertions(+), 13 deletions(-) diff --git a/Gost.cpp b/Gost.cpp index 67d625c6..d3c56eb2 100644 --- a/Gost.cpp +++ b/Gost.cpp @@ -194,6 +194,19 @@ namespace crypto 0x07e095624504536c, 0x8d70c431ac02a736, 0xc83862965601dd1b, 0x641c314b2b8ee083 }; + static const uint8_t T_[64]= + { + 0, 8, 16, 24, 32, 40, 48, 56, + 1, 9, 17, 25, 33, 41, 49, 57, + 2, 10, 18, 26, 34, 42, 50, 58, + 3, 11, 19, 27, 35, 43, 51, 59, + 4, 12, 20, 28, 36, 44, 52, 60, + 5, 13, 21, 29, 37, 45, 53, 61, + 6, 14, 22, 30, 38, 46, 54, 62, + 7, 15, 23, 31, 39, 47, 55, 63 + }; + + static const uint8_t C_[12][64] = { { @@ -275,7 +288,7 @@ namespace crypto uint8_t buf[64]; uint64_t ll[8]; - GOST3411Block operator^(const GOST3411Block& other) + GOST3411Block operator^(const GOST3411Block& other) const { GOST3411Block ret; for (int i = 0; i < 8; i++) @@ -283,7 +296,7 @@ namespace crypto return ret; } - GOST3411Block operator^(const uint8_t * other) + GOST3411Block operator^(const uint8_t * other) const { GOST3411Block ret; for (int i = 0; i < 64; i++) @@ -291,7 +304,7 @@ namespace crypto return ret; } - GOST3411Block operator+(const GOST3411Block& other) + GOST3411Block operator+(const GOST3411Block& other) const { GOST3411Block ret; uint8_t carry = 0; @@ -303,17 +316,25 @@ namespace crypto } return ret; } - + + void Add (uint32_t c) + { + for (int i = 63; i >= 0; i--) + { + c += buf[i]; + buf[i] = c; + c >>= 8; + } + } + void S () { for (int i = 0; i < 64; i++) buf[i] = sbox_[buf[i]]; } - void LP () + void L () { - uint8_t tmp[64]; - memcpy (tmp, buf, 64); for (int i = 0; i < 8; i++) { uint64_t c = 0; @@ -322,16 +343,22 @@ namespace crypto uint8_t bit = 0x80; for (int k = 0; k < 8; k++) { - - if (tmp[j*8+i] & bit) c ^= A_[j*8+k]; + if (buf[i*8+j] & bit) c ^= A_[j*8+k]; bit >>= 1; } } - for (int j = 0; j < 8; j++) - buf[i*8+j] = c >> ((7 - j) << 3); + ll[i] = c; } } + void P () + { + uint8_t t[64]; + for (int i = 0; i < 64; i++) + t[i] = buf[T_[i]]; + memcpy (buf, t, 64); + } + GOST3411Block E (const GOST3411Block& m) { GOST3411Block k = *this; @@ -339,16 +366,76 @@ namespace crypto for (int i = 0; i < 12; i++) { res.S (); - res.LP (); + res.P (); + res.L (); k = k^C_[i]; k.S (); - k.LP (); + k.P (); + k.L (); res = k^res; } return res; } }; + static GOST3411Block gN (const GOST3411Block& N, const GOST3411Block& h, const GOST3411Block& m) + { + GOST3411Block res = N ^ h; + res.S (); + res.P (); + res.L (); + res = res.E (m); + res = res ^ h; + res = res ^ m; + return res; + } + + static void H (const uint8_t * iv, const uint8_t * buf, size_t len, uint8_t * digest) + { + // stage 1 + GOST3411Block h, N, s, m; + memcpy (h.buf, iv, 64); + memset (N.buf, 0, 64); + memset (s.buf, 0, 64); + size_t l = len; + // stage 2 + while (l >= 64) + { + memcpy (m.buf, buf + l - 64, 64); // TODO + h= gN (N, h, m); + N.Add (512); + s = m + s; + len -= 64; + } + memset (m.buf, 0, 64); + memcpy (m.buf + 64, buf, l); + // stage 3 + h = gN (N, h, m); + N.Add (l*8); + s = m + s; + GOST3411Block N0; + memset (N0.buf, 0, 64); + h = gN (N0, h, N); + h = gN (N0, h, s); + + memcpy (digest, h.buf, 64); + } + + void GOSTR3411_2012_256 (const uint8_t * buf, size_t len, uint8_t * digest) + { + uint8_t iv[64]; + memset (iv, 1, 64); + uint8_t h[64]; + H (iv, buf, len, h); + memcpy (digest, h, 32); // first half + } + + void GOSTR3411_2012_512 (const uint8_t * buf, size_t len, uint8_t * digest) + { + uint8_t iv[64]; + memset (iv, 0, 64); + H (iv, buf, len, digest); + } } } diff --git a/Gost.h b/Gost.h index 84ac82d1..9b65fcf1 100644 --- a/Gost.h +++ b/Gost.h @@ -45,6 +45,9 @@ namespace crypto }; std::unique_ptr& GetGOSTR3410Curve (GOSTR3410ParamSet paramSet); + + void GOSTR3411_2012_256 (const uint8_t * buf, size_t len, uint8_t * digest); + void GOSTR3411_2012_512 (const uint8_t * buf, size_t len, uint8_t * digest); } } From 289e9c809f62b7c77b947c501e6278359753444c Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 16 Mar 2017 16:30:32 -0400 Subject: [PATCH 36/39] correct padding for GOST 34.11 --- Gost.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Gost.cpp b/Gost.cpp index d3c56eb2..41f52b7c 100644 --- a/Gost.cpp +++ b/Gost.cpp @@ -407,9 +407,15 @@ namespace crypto s = m + s; len -= 64; } - memset (m.buf, 0, 64); - memcpy (m.buf + 64, buf, l); // stage 3 + size_t padding = 64 - l; + if (padding) + { + memset (m.buf, 0, padding - 1); + m.buf[padding - 1] = 1; + } + memcpy (m.buf + padding, buf, l); + h = gN (N, h, m); N.Add (l*8); s = m + s; From 465366e6443b35f03fdaf8453e4c2cbebd5e7873 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 16 Mar 2017 20:55:05 -0400 Subject: [PATCH 37/39] new ressed --- Config.cpp | 3 +- .../reseed/randomrng_at_mail.i2p.crt | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 contrib/certificates/reseed/randomrng_at_mail.i2p.crt diff --git a/Config.cpp b/Config.cpp index 10c55e8f..963ac755 100644 --- a/Config.cpp +++ b/Config.cpp @@ -185,7 +185,8 @@ namespace config { "https://reseed.atomike.ninja/," "https://reseed.memcpy.io/," "https://reseed.onion.im/," - "https://itoopie.atomike.ninja/" + "https://itoopie.atomike.ninja/," + "https://randomrng.ddns.net/" ), "Reseed URLs, separated by comma") ; diff --git a/contrib/certificates/reseed/randomrng_at_mail.i2p.crt b/contrib/certificates/reseed/randomrng_at_mail.i2p.crt new file mode 100644 index 00000000..aa3330ad --- /dev/null +++ b/contrib/certificates/reseed/randomrng_at_mail.i2p.crt @@ -0,0 +1,34 @@ +-----BEGIN CERTIFICATE----- +MIIFzTCCA7WgAwIBAgIQAkW0dwrp8UmNi9MTzU4/QjANBgkqhkiG9w0BAQsFADBy +MQswCQYDVQQGEwJYWDELMAkGA1UEBxMCWFgxCzAJBgNVBAkTAlhYMR4wHAYDVQQK +ExVJMlAgQW5vbnltb3VzIE5ldHdvcmsxDDAKBgNVBAsTA0kyUDEbMBkGA1UEAwwS +cmFuZG9tcm5nQG1haWwuaTJwMB4XDTE3MDIyNTE2MTgyM1oXDTI3MDIyNTE2MTgy +M1owcjELMAkGA1UEBhMCWFgxCzAJBgNVBAcTAlhYMQswCQYDVQQJEwJYWDEeMBwG +A1UEChMVSTJQIEFub255bW91cyBOZXR3b3JrMQwwCgYDVQQLEwNJMlAxGzAZBgNV +BAMMEnJhbmRvbXJuZ0BtYWlsLmkycDCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCC +AgoCggIBAMSUiFKmGKb5sDkuB1mEGfXikfOJA5ATgMROXyY0XkwLWedBCC6pa1/X +cVOfPDmDdmkPO8Brep7D2CLq21BBb4lAH7LrKHrABNjf1kfAwRAYMon9HsW3Yn+O +yIAvGbVTXqQlWpeL1ZRGFhU/5h/D5UtEpcIyG0lkBYRfZ52wFKP2WP52TBcGVpj8 +wBQnXfGmAhRUQfKDmJVCB5GLzNSxrmbhbdyBzZMoeOLTaTfMfb7jSIakYzH4f0TZ +1VE5+z+1BkJ53qVRH7IV1UBtSIBGD/L84wkqM5mIGKnZXiOyZxfKvS/sGr7WZuMu +NK3ugCFfTZnxYtb0dDPaqeXrdB3OKE/d5ghAOcIyBWrfsRQJlaHSIwvpqz7Hr7vA +3xSklkvvJoGwCIy2/+bFGP+Z6ietzvF9/mr1NcwxXGH32zjVmDSto+yaDjsMGFu1 +y4L4wUsOQOVsgNYPECC2HZOisUm/iYdw1+Y/PbgZS0sG3KzBZ1HYsvvFiTLZiyZR ++ZFTLmBoI3DPMfmTf0lRWXXWgUnWXDkxqBAV/fvmAc3XQfpI4HrkAYOllmAIStr9 +7OVsBAJiMcYWzx0UIZGBG+PE9uxHnGxVX64n2lKYLoXLWFURVoFJIYn7AJaxTv0N +r0IduERKqoQ0wyCjZ6RJOtz26GFUe1bPa7rc/VgfbZwUbF17lzAVAgMBAAGjXzBd +MA4GA1UdDwEB/wQEAwIChDAdBgNVHSUEFjAUBggrBgEFBQcDAgYIKwYBBQUHAwEw +DwYDVR0TAQH/BAUwAwEB/zAbBgNVHQ4EFAQScmFuZG9tcm5nQG1haWwuaTJwMA0G +CSqGSIb3DQEBCwUAA4ICAQAi4OOKzy7TcaFZccl2lmKp1wxDmaSaf/dhaqpaSKMS +zzvgO9XIq6CLEY/YqSm5AjU8PsclaC8+g20THCSUHntL3Jxu2dw1m/H30Mg0I1uJ +G7pyIVYwuwkdbalGQOaS0u3grzWnbCGMzuqeMBi8EBsZ5jsT5LGjgy1GE+BXl2tv +9EEWhy8dSVh3cy1iaAM6ZhCyj4rSw4odQqH2NWDOFt52cycHe/rpvKKD1AlrmFHQ +w18WnfUhr43JAyTWKxg/6uwdxb+cBTX0cad8lbOtQLiqNwOxQvEi/4uRrkdKtbRf +Z+MUI0XIopH2XV5lLExtxXStAaB4mZTgAbFPCnBC0wKQh/sgdXmUWsEEk610NShC +26jtXuPb43cDyCewUNCZ+jtzbc9pl6/SyatY/i2gAHamvGmeVJFzQkHe7YHRdzeR +RIqnWGlwSh0roPPRCU2dNdBZ0uH9lYbkG0IzGmEtks+KQqG+1H0yZkSCmArarLfj +aU5UslG+Zf1imqXtz5kFD/UMMuaQW05Sa/0YO6gW/hLtChHI5Jpd/Qb/KqLkPAM3 +PEVs4H5ZMVa6mLUsLIw9Ra2QozdB9lqoZBMRa0jqgJKxnAgNcWpYtTyJ2PtfA9oE +xmjE6O3FlNSee4aKxZ2Kz7cTufd/+ndsSSeNuRLQVihXKNqkrQIuow+H/KDw930c +Cw== +-----END CERTIFICATE----- From eb12d43800cdb20efcec0dfc0ef3fbb76ec3bfb4 Mon Sep 17 00:00:00 2001 From: R4SAS Date: Fri, 17 Mar 2017 20:59:10 +0300 Subject: [PATCH 38/39] update windows build scripts --- contrib/build_mingw.cmd | 31 +++++++++++++++++-------------- contrib/build_mingw.sh | 12 ++++++++---- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/contrib/build_mingw.cmd b/contrib/build_mingw.cmd index b5ac0d69..b104d79c 100644 --- a/contrib/build_mingw.cmd +++ b/contrib/build_mingw.cmd @@ -1,22 +1,25 @@ @echo off -title ઠ i2pd +setlocal -set "WD=C:\msys64" +title Building i2pd + +set "WD=C:\msys64\usr\bin\" +set MSYS2_PATH_TYPE=inherit set CHERE_INVOKING=enabled_from_arguments -set MSYSCON=mintty.exe +set MSYSCON= -echo ઠ i2pd win32. Enter ᫥ 砭 樨... -set "MSYSTEM=MINGW32" -set "CONTITLE=MinGW x32" -start "%CONTITLE%" /WAIT C:\msys64\usr\bin\mintty.exe -i /msys2.ico /usr/bin/bash --login build_mingw.sh -pause +echo Building i2pd for win32. Press Enter after the end of compilation... +set MSYSTEM=MINGW32 +set CONTITLE=MinGW x32 +start "%CONTITLE%" /WAIT "%WD%bash" --login build_mingw.sh +pause > nul -echo ઠ i2pd win64. Enter ᫥ 砭 樨... -set "MSYSTEM=MINGW64" -set "CONTITLE=MinGW x64" -start "%CONTITLE%" /WAIT C:\msys64\usr\bin\mintty.exe -i /msys2.ico /usr/bin/bash --login build_mingw.sh -pause +echo Building i2pd for win64. Press Enter after the end of compilation... +set MSYSTEM=MINGW64 +set CONTITLE=MinGW x64 +start "%CONTITLE%" /WAIT "%WD%bash" --login build_mingw.sh +pause > nul -echo ઠ 襭... +echo Build complete... pause exit /b 0 \ No newline at end of file diff --git a/contrib/build_mingw.sh b/contrib/build_mingw.sh index ec4b975b..ecc388c1 100644 --- a/contrib/build_mingw.sh +++ b/contrib/build_mingw.sh @@ -48,7 +48,8 @@ fi; echo "Собираем i2pd ${version} (коммит ${commit}) для ${arch}."; # Собираем приложение с разными параметрами, и архивируем в zip архивы. -make USE_UPNP=yes USE_AVX=1 USE_AESNI=1 -j ${threads} > ${contrib}/build_avx_aesni.log 2>&1 +echo "Сборка AVX+AESNI"; +make USE_UPNP=yes USE_AVX=1 USE_AESNI=1 -j ${threads} > ${contrib}/build_${arch}_avx_aesni.log 2>&1 if [ "$?" != 0 ]; then echo "Сборка не удалась. Смотрите в build_avx_aesni.log"; exit 1; @@ -56,7 +57,8 @@ fi; zip -9 ${contrib}/i2pd_${version}_${commit}_${arch}_mingw_avx_aesni.zip i2pd.exe >> /dev/null make clean >> /dev/null -make USE_UPNP=yes USE_AVX=1 -j ${threads} > ${contrib}/build_avx.log 2>&1 +echo "Сборка AVX"; +make USE_UPNP=yes USE_AVX=1 -j ${threads} > ${contrib}/build_${arch}_avx.log 2>&1 if [ "$?" != 0 ]; then echo "Сборка не удалась. Смотрите в build_avx.log."; exit 1; @@ -64,7 +66,8 @@ fi; zip -9 ${contrib}/i2pd_${version}_${commit}_${arch}_mingw_avx.zip i2pd.exe >> /dev/null make clean >> /dev/null -make USE_UPNP=yes USE_AESNI=1 -j ${threads} > ${contrib}/build_aesni.log 2>&1 +echo "Сборка AESNI"; +make USE_UPNP=yes USE_AESNI=1 -j ${threads} > ${contrib}/build_${arch}_aesni.log 2>&1 if [ "$?" != 0 ]; then echo "Сборка не удалась. Смотрите в build_aesni.log"; exit 1; @@ -72,7 +75,8 @@ fi; zip -9 ${contrib}/i2pd_${version}_${commit}_${arch}_mingw_aesni.zip i2pd.exe >> /dev/null make clean >> /dev/null -make USE_UPNP=yes -j ${threads} > ${contrib}/build.log 2>&1 +echo "Сборка без дополнительных инструкций"; +make USE_UPNP=yes -j ${threads} > ${contrib}/build_${arch}.log 2>&1 if [ "$?" != 0 ]; then echo "Сборка не удалась. Смотрите в build.log"; exit 1; From 6c848a57b67d6d99dabfdf03af318ac6528dd127 Mon Sep 17 00:00:00 2001 From: R4SAS Date: Sat, 18 Mar 2017 02:40:39 +0300 Subject: [PATCH 39/39] recreated appveyor config --- appveyor.yml | 191 ++++++--------------------------------------------- 1 file changed, 21 insertions(+), 170 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index be16e890..0018d85a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -version: 1.0.{build} +version: 2.12.{build} pull_requests: do_not_increment_build_number: true branches: @@ -8,190 +8,41 @@ skip_tags: true os: Visual Studio 2015 shallow_clone: true clone_depth: 1 -init: -- cmd: >- - mkdir \projects\instdir - - rem Appveyor has win32 openssl pre-installed that is picked up erroneously even for 64-bit. Cleaning the mess... Should happen before restoring cache. - - rem Might consider passing OPENSSL_ROOT_DIR - - if exist \OpenSSL-Win32 rmdir /S /Q \OpenSSL-Win32 - if exist \OpenSSL-Win64 rmdir /S /Q \OpenSSL-Win64 - - if exist \OpenSSL rmdir /S /Q \OpenSSL environment: - BOOST_ROOT: C:\Libraries\boost_1_59_0 - MINIUPNPC: miniupnpc-1.9.20151026 - OPENSSL: OpenSSL_1_0_2e - ZLIB: zlib-1.2.8 + MSYS2_PATH_TYPE: inherit + CHERE_INVOKING: enabled_from_arguments matrix: - # - type: static - # msvc: 14 - # x64: 0 - # - type: static - # variant: Release - # # FIXME why is this necessary with Appveyor??? - # cmake: -DSSL_EAY=/mingw32/lib/libssl.a -DLIB_EAY=/mingw32/lib/libcrypto.a - - type: shared - variant: Release - - type: static - msvc: 12 - x64: 1 - variant: RelWithDebInfo - - type: static - msvc: 14 - variant: RelWithDebInfo - cmake: -DWITH_PCH=ON - # - type: static - # msvc: 12 - # - type: shared - # msvc: 14 - # variant: Debug - # - type: shared - # variant: Release - # cmake: -DWITH_PCH=ON - # x64: 1 + - MSYSTEM: MINGW64 + - MSYSTEM: MINGW32 + install: -- if not exist \projects\miniupnpc\ ( - mkdir \projects\miniupnpc - && curl -sL http://miniupnp.free.fr/files/download.php?file=%MINIUPNPC%.tar.gz -o \projects\miniupnpc\%MINIUPNPC%.tar.gz - ) -- tar --strip-components=1 --directory=\projects\miniupnpc -xzf \projects\miniupnpc\%MINIUPNPC%.tar.gz -- if not exist \projects\zlib\ ( - mkdir \projects\zlib - && cd \projects\zlib - && curl -sLO http://zlib.net/%ZLIB%.tar.gz - ) -- tar --strip-components=1 --directory=\projects\zlib -xzf \projects\zlib\%ZLIB%.tar.gz -- patch -p0 C:/projects/zlib/CMakeLists.txt %APPVEYOR_BUILD_FOLDER%/build/cmake-zlib-static.patch -- patch -p0 C:/projects/zlib/CMakeLists.txt %APPVEYOR_BUILD_FOLDER%/build/cmake-zlib-amd64.patch -- if "%type%" == "static" ( - set "static=ON" - && set "boostlib=lib" +- c:\msys64\usr\bin\bash -lc "pacman --noconfirm -Rns gcc-fortran gcc" +- c:\msys64\usr\bin\bash -lc "pacman --noconfirm -Syuu" +- c:\msys64\usr\bin\bash -lc "pacman --noconfirm -Syuu" + +- if "%MSYSTEM%" == "MINGW64" ( + c:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-x86_64-boost mingw-w64-x86_64-miniupnpc" ) else ( - set "static=OFF" - && set "dll=dll" + c:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-boost mingw-w64-i686-miniupnpc" ) -- if "%x64%"=="1" ( + +- if "%MSYSTEM%" == "MINGW64" ( set "bitness=64" - && set "openssl_target=VC-WIN64A" - && set "zlib_asm=-DAMD64=ON" ) else ( set "bitness=32" - && set "openssl_target=VC-WIN32" - && set "zlib_asm=-DASM686=ON "-DCMAKE_ASM_MASM_FLAGS=/W0 /safeseh"" - ) -- C:\msys64\usr\bin\bash -lc "export PATH=/mingw%bitness%/bin:/usr/bin:. && cd /c/projects/miniupnpc && CC=gcc make -f Makefile.mingw init miniupnpc.dll > c:\projects\instdir\build_miniupnpc.log 2>&1 || cat c:\projects\instdir\build_miniupnpc.log" -- set /a generator=%msvc%+2001 -- if defined msvc ( - ( - if "%x64%" == "1" ( - call "C:\Program Files (x86)\Microsoft Visual Studio %msvc%.0\VC\vcvarsall.bat" amd64 - && set "generator=Visual Studio %msvc% %generator% Win64" - ) else ( - call "C:\Program Files (x86)\Microsoft Visual Studio %msvc%.0\VC\vcvarsall.bat" x86 - && set "generator=Visual Studio %msvc% %generator%" - ) - ) - && set "zlib_root=C:/stage/zlib-Win%bitness%-vc%msvc%-%type%" - && if "%variant%" neq "Debug" ( - set "boost_variant=variant=release" - && set "boostdbg=-gd" - ) - ) else ( - set "generator=Unix Makefiles" - ) -- if defined msvc if not exist %zlib_root% ( - mkdir \projects\zlib-build - && cd \projects\zlib-build - && cmake ../zlib -G "%generator%" %zlib_asm% -DWITH_STATIC=%static% -DCMAKE_INSTALL_PREFIX=%zlib_root% > c:\projects\instdir\build_zlib.log - && cmake --build . --config Release --target INSTALL >> c:\projects\instdir\build_zlib.log - || type c:\projects\instdir\build_zlib.log ) -- cmd: >- - rem cinst nasm - - cd \projects - - if not exist nasm-2.11.08-installer.exe curl --silent --location --remote-name http://www.nasm.us/pub/nasm/releasebuilds/2.11.08/win32/nasm-2.11.08-installer.exe - nasm-2.11.08-installer.exe /S - - set "PATH=%PATH%;C:\Program Files (x86)\nasm" - - if not exist %OPENSSL%.zip curl --silent --location --remote-name https://github.com/openssl/openssl/archive/%OPENSSL%.zip -- cd %BOOST_ROOT% -- if defined msvc if not exist "stage%bitness%\lib\%boostlib%boost_system-vc%msvc%0-mt%boostdbg%*" ( - bootstrap > c:\projects\instdir\build_boost.log - && b2 toolset=msvc-%msvc%.0 %boost_variant% link=%type% runtime-link=%type% address-model=%bitness% --build-type=minimal --with-filesystem --with-program_options --with-date_time --stagedir=stage%bitness% >> c:\projects\instdir\build_boost.log - || type c:\projects\instdir\build_boost.log - ) -- if defined msvc if not exist C:\stage\OpenSSL-Win%bitness%-vc%msvc%-%type%\ ( - cd \projects - && 7z x %OPENSSL%.zip > NUL - && cd openssl-%OPENSSL% - && perl Configure %openssl_target% no-rc2 no-rc4 no-rc5 no-idea no-bf no-cast no-whirlpool no-md2 no-md4 no-ripemd no-mdc2 no-camellia no-seed no-comp no-krb5 no-gmp no-rfc3779 no-ec2m no-ssl2 no-jpake no-srp no-sctp no-srtp --prefix=c:\stage\OpenSSL-Win%bitness%-vc%msvc%-%type% > c:\projects\instdir\build_openssl.log - && ( if "%x64%" == "1" ( ms\do_win64a >> c:\projects\instdir\build_openssl.log ) else ( ms\do_nasm >> c:\projects\instdir\build_openssl.log ) ) - && nmake -f ms\nt%dll%.mak install >> c:\projects\instdir\build_openssl.log 2>&1 - || type c:\projects\instdir\build_openssl.log - ) -- mklink /J \OpenSSL \stage\OpenSSL-Win%bitness%-vc%msvc%-%type% -- rem already there: mingw-w64-i686-openssl mingw-w64-i686-gcc -- if not defined msvc ( - C:\msys64\usr\bin\bash -lc "pacman --needed --noconfirm -Sy bash pacman pacman-mirrors msys2-runtime msys2-runtime-devel cmake" - && if "%x64%" == "1" ( - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-x86_64-openssl mingw-w64-x86_64-boost mingw-w64-x86_64-miniupnpc mingw-w64-x86_64-extra-cmake-modules" - ) else ( - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-openssl mingw-w64-i686-boost mingw-w64-i686-miniupnpc mingw-w64-i686-extra-cmake-modules" - ) - ) -cache: -- C:\projects\%OPENSSL%.zip -- C:\projects\nasm-2.11.08-installer.exe -- C:\projects\miniupnpc\%MINIUPNPC%.tar.gz -- C:\stage -- '%BOOST_ROOT%\stage32' -- '%BOOST_ROOT%\stage64' -- C:\projects\zlib\%ZLIB%.tar.gz build_script: - cmd: >- - mkdir \projects\build - - rem FIXME use fixup_bundle in cmake + cd \projects\i2pd - rem msbuild i2pd.sln /p:Configuration=Release + echo MSYSTEM = %MSYSTEM%, bitness = %bitness% - if defined variant ( set cmake_extra=-DCMAKE_BUILD_TYPE=%variant% && set "cmake_build=--config %variant%" ) +- c:\msys64\usr\bin\bash -lc "make USE_UPNP=yes -j2" +- 7z a -tzip -mx9 -mmt i2pd-mingw-win%bitness%.zip i2pd.exe - echo "bitness=%bitness%; static=%static%; dll=%dll%; type=%type%; generator=%generator%; variant=%variant%; cmake=%cmake%; cmake_extra=%cmake_extra%" -- if not defined msvc ( - C:\msys64\usr\bin\bash -lc "export PATH=/mingw%bitness%/bin:/usr/bin && cd /c/projects/build && CC=/mingw%bitness%/bin/gcc.exe CXX=/mingw%bitness%/bin/g++.exe /usr/bin/cmake /c/projects/i2pd/build -G 'Unix Makefiles' -DWITH_AESNI=ON -DWITH_UPNP=ON %cmake% %cmake_extra% -DWITH_STATIC=%static% -DWITH_HARDENING=ON -DCMAKE_INSTALL_PREFIX:PATH=/c/projects/instdir -DCMAKE_FIND_ROOT_PATH=/mingw%bitness% && make install" - && 7z a -tzip -mx9 -mmt C:\projects\i2pd\i2pd-mingw-win%bitness%-%type%.zip C:\projects\instdir\* C:\msys64\mingw%bitness%\bin\zlib1.dll C:\msys64\mingw%bitness%\bin\*eay32.dll - ) -- rem We are fine with multiple generated configurations in MS solution. Will use later -- if defined msvc ( - cd \projects\build - && cmake ..\i2pd\build -G "%generator%" -DWITH_UPNP=ON %cmake% -DWITH_STATIC=%static% -DZLIB_ROOT=%zlib_root% -DBoost_LIBRARY_DIR:PATH=%BOOST_ROOT%/stage%bitness%/lib -DCMAKE_INSTALL_PREFIX:PATH=c:/projects/instdir - && cmake --build . %cmake_build% --target install - && 7z a -tzip -mx9 -mmt C:\projects\i2pd\i2pd-vc%msvc%-win%bitness%-%type%.zip C:\projects\instdir\* - && cmake --build . %cmake_build% --target package - && xcopy i2pd*win*.exe ..\i2pd\ - ) test: off + artifacts: -- path: i2pd-vc12-win64-static.zip -- path: i2pd-vc12-win32-static.zip -- path: i2pd-vc12-win64-shared.zip -- path: i2pd-vc12-win32-shared.zip -- path: i2pd-vc14-win64-static.zip -- path: i2pd-vc14-win32-static.zip -- path: i2pd-vc14-win64-shared.zip -- path: i2pd-vc14-win32-shared.zip -- path: i2pd-mingw-win64-static.zip -- path: i2pd-mingw-win32-static.zip -- path: i2pd-mingw-win64-shared.zip -- path: i2pd-mingw-win32-shared.zip -- path: i2pd-2.1.0-win64.exe -- path: i2pd-2.1.0-win32.exe +- path: i2pd-mingw-win*.zip