2020-05-22 16:18:41 +03:00
/*
* Copyright ( c ) 2013 - 2020 , The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
2020-01-15 23:13:43 +03:00
# include <string.h>
# include <openssl/sha.h>
# include "Log.h"
# include "Crypto.h"
# include "Elligator.h"
# include "Tag.h"
# include "I2PEndian.h"
2020-01-23 22:26:40 +03:00
# include "Timestamp.h"
2020-03-09 01:13:41 +03:00
# include "Tunnel.h"
# include "TunnelPool.h"
2020-01-15 23:13:43 +03:00
# include "ECIESX25519AEADRatchetSession.h"
namespace i2p
{
namespace garlic
{
2020-03-01 13:25:50 +03:00
void RatchetTagSet : : DHInitialize ( const uint8_t * rootKey , const uint8_t * k )
{
// DH_INITIALIZE(rootKey, k)
uint8_t keydata [ 64 ] ;
i2p : : crypto : : HKDF ( rootKey , k , 32 , " KDFDHRatchetStep " , keydata ) ; // keydata = HKDF(rootKey, k, "KDFDHRatchetStep", 64)
memcpy ( m_NextRootKey , keydata , 32 ) ; // nextRootKey = keydata[0:31]
i2p : : crypto : : HKDF ( keydata + 32 , nullptr , 0 , " TagAndKeyGenKeys " , m_KeyData . buf ) ;
// [sessTag_ck, symmKey_ck] = HKDF(keydata[32:63], ZEROLEN, "TagAndKeyGenKeys", 64)
2020-02-09 05:51:02 +03:00
memcpy ( m_SymmKeyCK , m_KeyData . buf + 32 , 32 ) ;
m_NextSymmKeyIndex = 0 ;
2020-03-01 13:25:50 +03:00
}
2020-01-20 23:17:38 +03:00
2020-03-01 13:25:50 +03:00
void RatchetTagSet : : NextSessionTagRatchet ( )
{
i2p : : crypto : : HKDF ( m_KeyData . GetSessTagCK ( ) , nullptr , 0 , " STInitialization " , m_KeyData . buf ) ; // [sessTag_ck, sesstag_constant] = HKDF(sessTag_ck, ZEROLEN, "STInitialization", 64)
memcpy ( m_SessTagConstant , m_KeyData . GetSessTagConstant ( ) , 32 ) ;
2020-02-05 23:48:51 +03:00
m_NextIndex = 0 ;
2020-03-01 13:25:50 +03:00
}
2020-01-20 23:17:38 +03:00
2020-03-01 13:25:50 +03:00
uint64_t RatchetTagSet : : GetNextSessionTag ( )
{
i2p : : crypto : : HKDF ( m_KeyData . GetSessTagCK ( ) , m_SessTagConstant , 32 , " SessionTagKeyGen " , m_KeyData . buf ) ; // [sessTag_ck, tag] = HKDF(sessTag_chainkey, SESSTAG_CONSTANT, "SessionTagKeyGen", 64)
m_NextIndex + + ;
if ( m_NextIndex > = 65535 )
2020-04-27 02:27:31 +03:00
{
LogPrint ( eLogError , " Garlic: Tagset " , GetTagSetID ( ) , " is empty " ) ;
return 0 ;
2020-03-01 13:25:50 +03:00
}
return m_KeyData . GetTag ( ) ;
}
2020-01-20 23:17:38 +03:00
2020-03-08 02:46:40 +03:00
void RatchetTagSet : : GetSymmKey ( int index , uint8_t * key )
2020-02-09 05:51:02 +03:00
{
2020-04-17 04:30:18 +03:00
if ( index > = m_NextSymmKeyIndex )
2020-03-01 13:25:50 +03:00
{
2020-03-08 02:46:40 +03:00
auto num = index + 1 - m_NextSymmKeyIndex ;
2020-04-17 04:30:18 +03:00
if ( ! m_NextSymmKeyIndex )
{
i2p : : crypto : : HKDF ( m_SymmKeyCK , nullptr , 0 , " SymmetricRatchet " , m_CurrentSymmKeyCK ) ; // keydata_0 = HKDF(symmKey_ck, SYMMKEY_CONSTANT, "SymmetricRatchet", 64)
m_NextSymmKeyIndex = 1 ;
num - - ;
2020-03-01 13:25:50 +03:00
}
2020-03-08 02:46:40 +03:00
for ( int i = 0 ; i < num ; i + + )
2020-03-01 13:25:50 +03:00
{
2020-03-08 02:46:40 +03:00
i2p : : crypto : : HKDF ( m_CurrentSymmKeyCK , nullptr , 0 , " SymmetricRatchet " , m_CurrentSymmKeyCK ) ;
2020-04-17 04:30:18 +03:00
if ( i < num - 1 )
m_ItermediateSymmKeys . emplace ( m_NextSymmKeyIndex + i , m_CurrentSymmKeyCK + 32 ) ;
2020-03-01 13:25:50 +03:00
}
2020-03-08 02:46:40 +03:00
m_NextSymmKeyIndex + = num ;
memcpy ( key , m_CurrentSymmKeyCK + 32 , 32 ) ;
2020-02-09 05:51:02 +03:00
}
else
2020-04-17 04:30:18 +03:00
{
auto it = m_ItermediateSymmKeys . find ( index ) ;
if ( it ! = m_ItermediateSymmKeys . end ( ) )
2020-03-01 13:25:50 +03:00
{
2020-04-17 04:30:18 +03:00
memcpy ( key , it - > second , 32 ) ;
m_ItermediateSymmKeys . erase ( it ) ;
2020-03-01 13:25:50 +03:00
}
2020-04-17 04:30:18 +03:00
else
LogPrint ( eLogError , " Garlic: Missing symmetric key for index " , index ) ;
2020-03-01 13:25:50 +03:00
}
}
2020-05-01 04:27:35 +03:00
void RatchetTagSet : : Expire ( )
{
if ( ! m_ExpirationTimestamp )
m_ExpirationTimestamp = i2p : : util : : GetSecondsSinceEpoch ( ) + ECIESX25519_PREVIOUS_TAGSET_EXPIRATION_TIMEOUT ;
2020-03-01 13:25:50 +03:00
}
ECIESX25519AEADRatchetSession : : ECIESX25519AEADRatchetSession ( GarlicDestination * owner , bool attachLeaseSet ) :
GarlicRoutingSession ( owner , attachLeaseSet )
{
ResetKeys ( ) ;
}
ECIESX25519AEADRatchetSession : : ~ ECIESX25519AEADRatchetSession ( )
{
}
2020-01-15 23:13:43 +03:00
2020-01-23 05:42:30 +03:00
void ECIESX25519AEADRatchetSession : : ResetKeys ( )
{
2020-05-03 20:23:08 +03:00
static const uint8_t protocolNameHash [ 32 ] =
{
0x4c , 0xaf , 0x11 , 0xef , 0x2c , 0x8e , 0x36 , 0x56 , 0x4c , 0x53 , 0xe8 , 0x88 , 0x85 , 0x06 , 0x4d , 0xba ,
0xac , 0xbe , 0x00 , 0x54 , 0xad , 0x17 , 0x8f , 0x80 , 0x79 , 0xa6 , 0x46 , 0x82 , 0x7e , 0x6e , 0xe4 , 0x0c
} ; // SHA256("Noise_IKelg2+hs2_25519_ChaChaPoly_SHA256"), 40 bytes
static const uint8_t hh [ 32 ] =
{
0x9c , 0xcf , 0x85 , 0x2c , 0xc9 , 0x3b , 0xb9 , 0x50 , 0x44 , 0x41 , 0xe9 , 0x50 , 0xe0 , 0x1d , 0x52 , 0x32 ,
0x2e , 0x0d , 0x47 , 0xad , 0xd1 , 0xe9 , 0xa5 , 0x55 , 0xf7 , 0x55 , 0xb5 , 0x69 , 0xae , 0x18 , 0x3b , 0x5c
} ; // SHA256 (protocolNameHash)
memcpy ( m_CK , protocolNameHash , 32 ) ;
memcpy ( m_H , hh , 32 ) ;
2020-03-01 13:25:50 +03:00
}
void ECIESX25519AEADRatchetSession : : MixHash ( const uint8_t * buf , size_t len )
{
SHA256_CTX ctx ;
2020-01-15 23:13:43 +03:00
SHA256_Init ( & ctx ) ;
SHA256_Update ( & ctx , m_H , 32 ) ;
SHA256_Update ( & ctx , buf , len ) ;
SHA256_Final ( m_H , & ctx ) ;
2020-03-01 13:25:50 +03:00
}
2020-02-05 23:48:51 +03:00
void ECIESX25519AEADRatchetSession : : CreateNonce ( uint64_t seqn , uint8_t * nonce )
{
2020-03-01 13:25:50 +03:00
memset ( nonce , 0 , 4 ) ;
htole64buf ( nonce + 4 , seqn ) ;
}
bool ECIESX25519AEADRatchetSession : : GenerateEphemeralKeysAndEncode ( uint8_t * buf )
{
for ( int i = 0 ; i < 10 ; i + + )
{
m_EphemeralKeys . GenerateKeys ( ) ;
if ( i2p : : crypto : : GetElligator ( ) - > Encode ( m_EphemeralKeys . GetPublicKey ( ) , buf ) )
return true ; // success
}
return false ;
2020-02-05 23:48:51 +03:00
}
2020-03-01 13:25:50 +03:00
std : : shared_ptr < RatchetTagSet > ECIESX25519AEADRatchetSession : : CreateNewSessionTagset ( )
{
uint8_t tagsetKey [ 32 ] ;
i2p : : crypto : : HKDF ( m_CK , nullptr , 0 , " SessionReplyTags " , tagsetKey , 32 ) ; // tagsetKey = HKDF(chainKey, ZEROLEN, "SessionReplyTags", 32)
// Session Tag Ratchet
2020-06-05 01:19:38 +03:00
auto tagsetNsr = ( m_State = = eSessionStateNewSessionReceived ) ? std : : make_shared < RatchetTagSet > ( shared_from_this ( ) ) :
std : : make_shared < NSRatchetTagSet > ( shared_from_this ( ) ) ;
2020-03-01 13:25:50 +03:00
tagsetNsr - > DHInitialize ( m_CK , tagsetKey ) ; // tagset_nsr = DH_INITIALIZE(chainKey, tagsetKey)
tagsetNsr - > NextSessionTagRatchet ( ) ;
return tagsetNsr ;
}
bool ECIESX25519AEADRatchetSession : : HandleNewIncomingSession ( const uint8_t * buf , size_t len )
{
if ( ! GetOwner ( ) ) return false ;
// we are Bob
// KDF1
MixHash ( GetOwner ( ) - > GetEncryptionPublicKey ( i2p : : data : : CRYPTO_KEY_TYPE_ECIES_X25519_AEAD_RATCHET ) , 32 ) ; // h = SHA256(h || bpk)
2020-01-30 05:57:10 +03:00
if ( ! i2p : : crypto : : GetElligator ( ) - > Decode ( buf , m_Aepk ) )
2020-03-01 13:25:50 +03:00
{
2020-01-15 23:13:43 +03:00
LogPrint ( eLogError , " Garlic: Can't decode elligator " ) ;
2020-03-01 13:25:50 +03:00
return false ;
2020-01-15 23:13:43 +03:00
}
2020-03-01 13:25:50 +03:00
buf + = 32 ; len - = 32 ;
MixHash ( m_Aepk , 32 ) ; // h = SHA256(h || aepk)
uint8_t sharedSecret [ 32 ] ;
2020-04-14 03:00:19 +03:00
GetOwner ( ) - > Decrypt ( m_Aepk , sharedSecret , nullptr , i2p : : data : : CRYPTO_KEY_TYPE_ECIES_X25519_AEAD_RATCHET ) ; // x25519(bsk, aepk)
2020-01-17 03:33:00 +03:00
i2p : : crypto : : HKDF ( m_CK , sharedSecret , 32 , " " , m_CK ) ; // [chainKey, key] = HKDF(chainKey, sharedSecret, "", 64)
2020-03-01 13:25:50 +03:00
// decrypt flags/static
2020-01-15 23:13:43 +03:00
uint8_t nonce [ 12 ] , fs [ 32 ] ;
2020-02-05 23:48:51 +03:00
CreateNonce ( 0 , nonce ) ;
2020-01-17 03:33:00 +03:00
if ( ! i2p : : crypto : : AEADChaCha20Poly1305 ( buf , 32 , m_H , 32 , m_CK + 32 , nonce , fs , 32 , false ) ) // decrypt
2020-01-15 23:13:43 +03:00
{
LogPrint ( eLogWarning , " Garlic: Flags/static section AEAD verification failed " ) ;
return false ;
}
MixHash ( buf , 48 ) ; // h = SHA256(h || ciphertext)
buf + = 48 ; len - = 48 ; // 32 data + 16 poly
// KDF2 for payload
2020-03-01 13:25:50 +03:00
bool isStatic = ! i2p : : data : : Tag < 32 > ( fs ) . IsZero ( ) ;
2020-01-15 23:13:43 +03:00
if ( isStatic )
{
// static key, fs is apk
2020-03-01 13:25:50 +03:00
memcpy ( m_RemoteStaticKey , fs , 32 ) ;
2020-04-14 03:00:19 +03:00
GetOwner ( ) - > Decrypt ( fs , sharedSecret , nullptr , i2p : : data : : CRYPTO_KEY_TYPE_ECIES_X25519_AEAD_RATCHET ) ; // x25519(bsk, apk)
2020-01-17 03:33:00 +03:00
i2p : : crypto : : HKDF ( m_CK , sharedSecret , 32 , " " , m_CK ) ; // [chainKey, key] = HKDF(chainKey, sharedSecret, "", 64)
2020-01-15 23:13:43 +03:00
}
else // all zeros flags
2020-02-05 23:48:51 +03:00
CreateNonce ( 1 , nonce ) ;
2020-03-01 13:25:50 +03:00
2020-05-13 01:30:04 +03:00
// decrypt payload
std : : vector < uint8_t > payload ( len - 16 ) ; // we must save original ciphertext
2020-01-17 03:33:00 +03:00
if ( ! i2p : : crypto : : AEADChaCha20Poly1305 ( buf , len - 16 , m_H , 32 , m_CK + 32 , nonce , payload . data ( ) , len - 16 , false ) ) // decrypt
2020-01-15 23:13:43 +03:00
{
LogPrint ( eLogWarning , " Garlic: Payload section AEAD verification failed " ) ;
return false ;
}
if ( isStatic ) MixHash ( buf , len ) ; // h = SHA256(h || ciphertext)
2020-03-01 13:25:50 +03:00
m_State = eSessionStateNewSessionReceived ;
2020-01-29 20:54:26 +03:00
GetOwner ( ) - > AddECIESx25519Session ( m_RemoteStaticKey , shared_from_this ( ) ) ;
2020-01-17 19:21:41 +03:00
2020-03-01 13:25:50 +03:00
HandlePayload ( payload . data ( ) , len - 16 , nullptr , 0 ) ;
2020-01-15 23:13:43 +03:00
2020-03-01 13:25:50 +03:00
return true ;
}
2020-01-15 23:13:43 +03:00
2020-03-01 13:25:50 +03:00
void ECIESX25519AEADRatchetSession : : HandlePayload ( const uint8_t * buf , size_t len , const std : : shared_ptr < RatchetTagSet > & receiveTagset , int index )
{
size_t offset = 0 ;
2020-01-15 23:13:43 +03:00
while ( offset < len )
{
uint8_t blk = buf [ offset ] ;
offset + + ;
auto size = bufbe16toh ( buf + offset ) ;
offset + = 2 ;
LogPrint ( eLogDebug , " Garlic: Block type " , ( int ) blk , " of size " , size ) ;
if ( size > len )
{
LogPrint ( eLogError , " Garlic: Unexpected block length " , size ) ;
break ;
}
switch ( blk )
{
case eECIESx25519BlkGalicClove :
2020-05-05 20:01:23 +03:00
if ( GetOwner ( ) )
GetOwner ( ) - > HandleECIESx25519GarlicClove ( buf + offset , size ) ;
2020-01-15 23:13:43 +03:00
break ;
2020-04-25 04:36:08 +03:00
case eECIESx25519BlkNextKey :
2020-03-01 13:25:50 +03:00
LogPrint ( eLogDebug , " Garlic: next key " ) ;
2020-04-25 04:36:08 +03:00
HandleNextKey ( buf + offset , size , receiveTagset ) ;
break ;
2020-04-19 22:00:51 +03:00
case eECIESx25519BlkAck :
2020-03-01 13:25:50 +03:00
{
2020-04-19 22:00:51 +03:00
LogPrint ( eLogDebug , " Garlic: ack " ) ;
int numAcks = size > > 2 ; // /4
2020-03-01 13:25:50 +03:00
auto offset1 = offset ;
2020-04-19 22:00:51 +03:00
for ( auto i = 0 ; i < numAcks ; i + + )
2020-03-01 13:25:50 +03:00
{
2020-04-19 22:00:51 +03:00
offset1 + = 2 ; // tagsetid
MessageConfirmed ( bufbe16toh ( buf + offset1 ) ) ; offset1 + = 2 ; // N
}
break ;
2020-03-01 13:25:50 +03:00
}
2020-03-27 02:03:38 +03:00
case eECIESx25519BlkAckRequest :
2020-03-01 13:25:50 +03:00
{
2020-03-27 02:03:38 +03:00
LogPrint ( eLogDebug , " Garlic: ack request " ) ;
2020-03-01 13:25:50 +03:00
m_AckRequests . push_back ( { receiveTagset - > GetTagSetID ( ) , index } ) ;
break ;
}
2020-05-05 20:01:23 +03:00
case eECIESx25519BlkTermination :
LogPrint ( eLogDebug , " Garlic: termination " ) ;
if ( GetOwner ( ) )
GetOwner ( ) - > RemoveECIESx25519Session ( m_RemoteStaticKey ) ;
if ( receiveTagset ) receiveTagset - > Expire ( ) ;
2020-03-01 13:25:50 +03:00
break ;
2020-05-05 20:01:23 +03:00
case eECIESx25519BlkDateTime :
LogPrint ( eLogDebug , " Garlic: datetime " ) ;
2020-03-01 13:25:50 +03:00
break ;
2020-05-05 20:01:23 +03:00
case eECIESx25519BlkOptions :
LogPrint ( eLogDebug , " Garlic: options " ) ;
break ;
case eECIESx25519BlkPadding :
LogPrint ( eLogDebug , " Garlic: padding " ) ;
2020-03-01 13:25:50 +03:00
break ;
2020-01-15 23:13:43 +03:00
default :
LogPrint ( eLogWarning , " Garlic: Unknown block type " , ( int ) blk ) ;
}
offset + = size ;
}
2020-03-01 13:25:50 +03:00
}
2020-01-16 22:59:19 +03:00
2020-04-25 04:36:08 +03:00
void ECIESX25519AEADRatchetSession : : HandleNextKey ( const uint8_t * buf , size_t len , const std : : shared_ptr < RatchetTagSet > & receiveTagset )
{
uint8_t flag = buf [ 0 ] ; buf + + ; // flag
if ( flag & ECIESX25519_NEXT_KEY_REVERSE_KEY_FLAG )
{
2020-04-27 16:35:02 +03:00
if ( ! m_SendForwardKey | | ! m_NextSendRatchet ) return ;
2020-04-27 02:27:31 +03:00
uint16_t keyID = bufbe16toh ( buf ) ; buf + = 2 ; // keyID
if ( ( ( ! m_NextSendRatchet - > newKey | | ! m_NextSendRatchet - > keyID ) & & keyID = = m_NextSendRatchet - > keyID ) | |
2020-03-01 13:25:50 +03:00
( m_NextSendRatchet - > newKey & & keyID = = m_NextSendRatchet - > keyID - 1 ) )
2020-04-27 02:27:31 +03:00
{
2020-04-27 16:35:02 +03:00
if ( flag & ECIESX25519_NEXT_KEY_KEY_PRESENT_FLAG )
2020-04-27 02:27:31 +03:00
memcpy ( m_NextSendRatchet - > remote , buf , 32 ) ;
uint8_t sharedSecret [ 32 ] , tagsetKey [ 32 ] ;
m_NextSendRatchet - > key . Agree ( m_NextSendRatchet - > remote , sharedSecret ) ;
i2p : : crypto : : HKDF ( sharedSecret , nullptr , 0 , " XDHRatchetTagSet " , tagsetKey , 32 ) ; // tagsetKey = HKDF(sharedSecret, ZEROLEN, "XDHRatchetTagSet", 32)
auto newTagset = std : : make_shared < RatchetTagSet > ( shared_from_this ( ) ) ;
newTagset - > SetTagSetID ( 1 + m_NextSendRatchet - > keyID + keyID ) ;
2020-03-01 13:25:50 +03:00
newTagset - > DHInitialize ( m_SendTagset - > GetNextRootKey ( ) , tagsetKey ) ;
2020-04-27 02:27:31 +03:00
newTagset - > NextSessionTagRatchet ( ) ;
2020-03-01 13:25:50 +03:00
m_SendTagset = newTagset ;
2020-04-27 02:27:31 +03:00
m_SendForwardKey = false ;
LogPrint ( eLogDebug , " Garlic: next send tagset " , newTagset - > GetTagSetID ( ) , " created " ) ;
}
else
LogPrint ( eLogDebug , " Garlic: Unexpected next key " , keyID ) ;
2020-03-01 13:25:50 +03:00
}
2020-04-25 04:36:08 +03:00
else
{
uint16_t keyID = bufbe16toh ( buf ) ; buf + = 2 ; // keyID
2020-04-26 04:09:03 +03:00
bool newKey = flag & ECIESX25519_NEXT_KEY_REQUEST_REVERSE_KEY_FLAG ;
m_SendReverseKey = true ;
if ( ! m_NextReceiveRatchet )
m_NextReceiveRatchet . reset ( new DHRatchet ( ) ) ;
else
2020-03-01 13:25:50 +03:00
{
if ( keyID = = m_NextReceiveRatchet - > keyID & & newKey = = m_NextReceiveRatchet - > newKey )
2020-04-25 21:45:53 +03:00
{
2020-04-26 04:09:03 +03:00
LogPrint ( eLogDebug , " Garlic: Duplicate " , newKey ? " new " : " old " , " key " , keyID , " received " ) ;
return ;
2020-03-01 13:25:50 +03:00
}
2020-04-26 04:09:03 +03:00
m_NextReceiveRatchet - > keyID = keyID ;
2020-04-25 04:36:08 +03:00
}
2020-04-26 04:09:03 +03:00
int tagsetID = 2 * keyID ;
if ( newKey )
2020-03-01 13:25:50 +03:00
{
2020-04-26 04:09:03 +03:00
m_NextReceiveRatchet - > key . GenerateKeys ( ) ;
m_NextReceiveRatchet - > newKey = true ;
tagsetID + + ;
2020-03-01 13:25:50 +03:00
}
2020-04-25 04:36:08 +03:00
else
2020-04-26 04:09:03 +03:00
m_NextReceiveRatchet - > newKey = false ;
if ( flag & ECIESX25519_NEXT_KEY_KEY_PRESENT_FLAG )
memcpy ( m_NextReceiveRatchet - > remote , buf , 32 ) ;
2020-03-01 13:25:50 +03:00
2020-04-26 04:09:03 +03:00
uint8_t sharedSecret [ 32 ] , tagsetKey [ 32 ] ;
m_NextReceiveRatchet - > key . Agree ( m_NextReceiveRatchet - > remote , sharedSecret ) ;
i2p : : crypto : : HKDF ( sharedSecret , nullptr , 0 , " XDHRatchetTagSet " , tagsetKey , 32 ) ; // tagsetKey = HKDF(sharedSecret, ZEROLEN, "XDHRatchetTagSet", 32)
2020-03-01 13:25:50 +03:00
auto newTagset = std : : make_shared < RatchetTagSet > ( shared_from_this ( ) ) ;
newTagset - > SetTagSetID ( tagsetID ) ;
newTagset - > DHInitialize ( receiveTagset - > GetNextRootKey ( ) , tagsetKey ) ;
2020-04-26 04:09:03 +03:00
newTagset - > NextSessionTagRatchet ( ) ;
2020-03-01 13:25:50 +03:00
GenerateMoreReceiveTags ( newTagset , ECIESX25519_MAX_NUM_GENERATED_TAGS ) ;
2020-05-01 04:27:35 +03:00
receiveTagset - > Expire ( ) ;
2020-04-26 04:09:03 +03:00
LogPrint ( eLogDebug , " Garlic: next receive tagset " , tagsetID , " created " ) ;
2020-03-01 13:25:50 +03:00
}
}
2020-04-27 02:27:31 +03:00
void ECIESX25519AEADRatchetSession : : NewNextSendRatchet ( )
{
if ( m_NextSendRatchet )
{
2020-04-27 16:35:02 +03:00
if ( ! m_NextSendRatchet - > newKey | | ! m_NextSendRatchet - > keyID )
2020-04-27 02:27:31 +03:00
{
2020-04-27 16:35:02 +03:00
m_NextSendRatchet - > keyID + + ;
m_NextSendRatchet - > newKey = true ;
2020-03-01 13:25:50 +03:00
}
2020-04-27 02:27:31 +03:00
else
2020-04-27 16:35:02 +03:00
m_NextSendRatchet - > newKey = false ;
2020-03-01 13:25:50 +03:00
}
2020-04-27 16:35:02 +03:00
else
m_NextSendRatchet . reset ( new DHRatchet ( ) ) ;
if ( m_NextSendRatchet - > newKey )
m_NextSendRatchet - > key . GenerateKeys ( ) ;
2020-03-01 13:25:50 +03:00
2020-04-27 02:27:31 +03:00
m_SendForwardKey = true ;
2020-04-27 16:35:02 +03:00
LogPrint ( eLogDebug , " Garlic: new send ratchet " , m_NextSendRatchet - > newKey ? " new " : " old " , " key " , m_NextSendRatchet - > keyID , " created " ) ;
2020-03-01 13:25:50 +03:00
}
bool ECIESX25519AEADRatchetSession : : NewOutgoingSessionMessage ( const uint8_t * payload , size_t len , uint8_t * out , size_t outLen )
{
2020-01-23 05:42:30 +03:00
ResetKeys ( ) ;
2020-03-01 13:25:50 +03:00
// we are Alice, bpk is m_RemoteStaticKey
size_t offset = 0 ;
if ( ! GenerateEphemeralKeysAndEncode ( out + offset ) )
{
2020-01-17 00:34:13 +03:00
LogPrint ( eLogError , " Garlic: Can't encode elligator " ) ;
2020-03-01 13:25:50 +03:00
return false ;
}
offset + = 32 ;
// KDF1
MixHash ( m_RemoteStaticKey , 32 ) ; // h = SHA256(h || bpk)
MixHash ( m_EphemeralKeys . GetPublicKey ( ) , 32 ) ; // h = SHA256(h || aepk)
uint8_t sharedSecret [ 32 ] ;
2020-01-22 03:13:23 +03:00
m_EphemeralKeys . Agree ( m_RemoteStaticKey , sharedSecret ) ; // x25519(aesk, bpk)
2020-03-01 13:25:50 +03:00
i2p : : crypto : : HKDF ( m_CK , sharedSecret , 32 , " " , m_CK ) ; // [chainKey, key] = HKDF(chainKey, sharedSecret, "", 64)
// encrypt static key section
uint8_t nonce [ 12 ] ;
2020-02-05 23:48:51 +03:00
CreateNonce ( 0 , nonce ) ;
2020-04-14 03:00:19 +03:00
if ( ! i2p : : crypto : : AEADChaCha20Poly1305 ( GetOwner ( ) - > GetEncryptionPublicKey ( i2p : : data : : CRYPTO_KEY_TYPE_ECIES_X25519_AEAD_RATCHET ) , 32 , m_H , 32 , m_CK + 32 , nonce , out + offset , 48 , true ) ) // encrypt
2020-01-17 00:34:13 +03:00
{
LogPrint ( eLogWarning , " Garlic: Static section AEAD encryption failed " ) ;
return false ;
}
2020-03-01 13:25:50 +03:00
MixHash ( out + offset , 48 ) ; // h = SHA256(h || ciphertext)
offset + = 48 ;
// KDF2
GetOwner ( ) - > Decrypt ( m_RemoteStaticKey , sharedSecret , nullptr , i2p : : data : : CRYPTO_KEY_TYPE_ECIES_X25519_AEAD_RATCHET ) ; // x25519 (ask, bpk)
2020-01-17 03:33:00 +03:00
i2p : : crypto : : HKDF ( m_CK , sharedSecret , 32 , " " , m_CK ) ; // [chainKey, key] = HKDF(chainKey, sharedSecret, "", 64)
// encrypt payload
if ( ! i2p : : crypto : : AEADChaCha20Poly1305 ( payload , len , m_H , 32 , m_CK + 32 , nonce , out + offset , len + 16 , true ) ) // encrypt
{
LogPrint ( eLogWarning , " Garlic: Payload section AEAD encryption failed " ) ;
return false ;
}
2020-01-30 05:57:10 +03:00
MixHash ( out + offset , len + 16 ) ; // h = SHA256(h || ciphertext)
2020-01-21 22:40:23 +03:00
2020-01-29 23:54:11 +03:00
m_State = eSessionStateNewSessionSent ;
2020-03-01 13:25:50 +03:00
if ( GetOwner ( ) )
2020-04-28 21:47:53 +03:00
GenerateMoreReceiveTags ( CreateNewSessionTagset ( ) , ECIESX25519_NSR_NUM_GENERATED_TAGS ) ;
2020-01-21 22:40:23 +03:00
2020-03-01 13:25:50 +03:00
return true ;
}
2020-01-17 00:34:13 +03:00
2020-03-01 13:25:50 +03:00
bool ECIESX25519AEADRatchetSession : : NewSessionReplyMessage ( const uint8_t * payload , size_t len , uint8_t * out , size_t outLen )
{
// we are Bob
2020-06-05 01:19:38 +03:00
m_NSRSendTagset = CreateNewSessionTagset ( ) ;
uint64_t tag = m_NSRSendTagset - > GetNextSessionTag ( ) ;
2020-03-01 13:25:50 +03:00
size_t offset = 0 ;
memcpy ( out + offset , & tag , 8 ) ;
offset + = 8 ;
if ( ! GenerateEphemeralKeysAndEncode ( out + offset ) ) // bepk
{
2020-01-17 22:11:15 +03:00
LogPrint ( eLogError , " Garlic: Can't encode elligator " ) ;
2020-03-01 13:25:50 +03:00
return false ;
2020-01-22 19:27:47 +03:00
}
2020-04-29 01:23:13 +03:00
memcpy ( m_NSREncodedKey , out + offset , 56 ) ; // for possible next NSR
memcpy ( m_NSRH , m_H , 32 ) ;
2020-03-01 13:25:50 +03:00
offset + = 32 ;
// KDF for Reply Key Section
MixHash ( ( const uint8_t * ) & tag , 8 ) ; // h = SHA256(h || tag)
MixHash ( m_EphemeralKeys . GetPublicKey ( ) , 32 ) ; // h = SHA256(h || bepk)
uint8_t sharedSecret [ 32 ] ;
m_EphemeralKeys . Agree ( m_Aepk , sharedSecret ) ; // sharedSecret = x25519(besk, aepk)
i2p : : crypto : : HKDF ( m_CK , sharedSecret , 32 , " " , m_CK , 32 ) ; // chainKey = HKDF(chainKey, sharedSecret, "", 32)
m_EphemeralKeys . Agree ( m_RemoteStaticKey , sharedSecret ) ; // sharedSecret = x25519(besk, apk)
i2p : : crypto : : HKDF ( m_CK , sharedSecret , 32 , " " , m_CK ) ; // [chainKey, key] = HKDF(chainKey, sharedSecret, "", 64)
2020-01-31 03:30:30 +03:00
uint8_t nonce [ 12 ] ;
2020-02-05 23:48:51 +03:00
CreateNonce ( 0 , nonce ) ;
2020-03-01 13:25:50 +03:00
// calculate hash for zero length
2020-04-29 01:23:13 +03:00
if ( ! i2p : : crypto : : AEADChaCha20Poly1305 ( nonce /* can be anything */ , 0 , m_H , 32 , m_CK + 32 , nonce , out + offset , 16 , true ) ) // encrypt, ciphertext = ENCRYPT(k, n, ZEROLEN, ad)
2020-01-17 22:11:15 +03:00
{
LogPrint ( eLogWarning , " Garlic: Reply key section AEAD encryption failed " ) ;
return false ;
}
2020-03-01 13:25:50 +03:00
MixHash ( out + offset , 16 ) ; // h = SHA256(h || ciphertext)
offset + = 16 ;
// KDF for payload
uint8_t keydata [ 64 ] ;
i2p : : crypto : : HKDF ( m_CK , nullptr , 0 , " " , keydata ) ; // keydata = HKDF(chainKey, ZEROLEN, "", 64)
2020-02-09 05:51:02 +03:00
// k_ab = keydata[0:31], k_ba = keydata[32:63]
2020-04-25 04:36:08 +03:00
auto receiveTagset = std : : make_shared < RatchetTagSet > ( shared_from_this ( ) ) ;
2020-03-01 13:25:50 +03:00
receiveTagset - > DHInitialize ( m_CK , keydata ) ; // tagset_ab = DH_INITIALIZE(chainKey, k_ab)
2020-04-25 04:36:08 +03:00
receiveTagset - > NextSessionTagRatchet ( ) ;
2020-04-24 22:46:02 +03:00
m_SendTagset = std : : make_shared < RatchetTagSet > ( shared_from_this ( ) ) ;
2020-03-01 13:25:50 +03:00
m_SendTagset - > DHInitialize ( m_CK , keydata + 32 ) ; // tagset_ba = DH_INITIALIZE(chainKey, k_ba)
m_SendTagset - > NextSessionTagRatchet ( ) ;
2020-04-28 01:53:02 +03:00
GenerateMoreReceiveTags ( receiveTagset , ECIESX25519_MIN_NUM_GENERATED_TAGS ) ;
2020-03-01 13:25:50 +03:00
i2p : : crypto : : HKDF ( keydata + 32 , nullptr , 0 , " AttachPayloadKDF " , m_NSRKey , 32 ) ; // k = HKDF(k_ba, ZEROLEN, "AttachPayloadKDF", 32)
// encrypt payload
if ( ! i2p : : crypto : : AEADChaCha20Poly1305 ( payload , len , m_H , 32 , m_NSRKey , nonce , out + offset , len + 16 , true ) ) // encrypt
2020-01-17 22:11:15 +03:00
{
2020-04-03 04:48:39 +03:00
LogPrint ( eLogWarning , " Garlic: NSR payload section AEAD encryption failed " ) ;
2020-01-17 22:11:15 +03:00
return false ;
}
2020-04-03 04:48:39 +03:00
m_State = eSessionStateNewSessionReplySent ;
2020-05-23 17:20:22 +03:00
m_SessionCreatedTimestamp = i2p : : util : : GetSecondsSinceEpoch ( ) ;
2020-03-01 13:25:50 +03:00
return true ;
}
2020-01-17 22:11:15 +03:00
2020-04-03 04:48:39 +03:00
bool ECIESX25519AEADRatchetSession : : NextNewSessionReplyMessage ( const uint8_t * payload , size_t len , uint8_t * out , size_t outLen )
2020-03-01 13:25:50 +03:00
{
// we are Bob and sent NSR already
2020-06-05 01:19:38 +03:00
uint64_t tag = m_NSRSendTagset - > GetNextSessionTag ( ) ; // next tag
2020-03-01 13:25:50 +03:00
memcpy ( out , & tag , 8 ) ;
2020-04-29 01:23:13 +03:00
memcpy ( out + 8 , m_NSREncodedKey , 32 ) ;
2020-03-01 13:25:50 +03:00
// recalculate h with new tag
2020-04-29 01:23:13 +03:00
memcpy ( m_H , m_NSRH , 32 ) ;
MixHash ( ( const uint8_t * ) & tag , 8 ) ; // h = SHA256(h || tag)
2020-03-01 13:25:50 +03:00
MixHash ( m_EphemeralKeys . GetPublicKey ( ) , 32 ) ; // h = SHA256(h || bepk)
2020-04-03 04:48:39 +03:00
uint8_t nonce [ 12 ] ;
2020-03-01 13:25:50 +03:00
CreateNonce ( 0 , nonce ) ;
2020-04-29 01:23:13 +03:00
if ( ! i2p : : crypto : : AEADChaCha20Poly1305 ( nonce /* can be anything */ , 0 , m_H , 32 , m_CK + 32 , nonce , out + 40 , 16 , true ) ) // encrypt, ciphertext = ENCRYPT(k, n, ZEROLEN, ad)
{
LogPrint ( eLogWarning , " Garlic: Reply key section AEAD encryption failed " ) ;
return false ;
}
2020-03-01 13:25:50 +03:00
MixHash ( out + 40 , 16 ) ; // h = SHA256(h || ciphertext)
2020-04-03 04:48:39 +03:00
// encrypt payload
2020-03-01 13:25:50 +03:00
if ( ! i2p : : crypto : : AEADChaCha20Poly1305 ( payload , len , m_H , 32 , m_NSRKey , nonce , out + 56 , len + 16 , true ) ) // encrypt
2020-04-03 04:48:39 +03:00
{
LogPrint ( eLogWarning , " Garlic: Next NSR payload section AEAD encryption failed " ) ;
return false ;
}
return true ;
2020-03-01 13:25:50 +03:00
}
bool ECIESX25519AEADRatchetSession : : HandleNewOutgoingSessionReply ( uint8_t * buf , size_t len )
{
2020-01-29 20:54:26 +03:00
// we are Alice
2020-01-23 05:42:30 +03:00
LogPrint ( eLogDebug , " Garlic: reply received " ) ;
2020-01-29 20:54:26 +03:00
const uint8_t * tag = buf ;
buf + = 8 ; len - = 8 ; // tag
2020-03-01 13:25:50 +03:00
uint8_t bepk [ 32 ] ; // Bob's ephemeral key
2020-01-29 20:54:26 +03:00
if ( ! i2p : : crypto : : GetElligator ( ) - > Decode ( buf , bepk ) )
2020-03-01 13:25:50 +03:00
{
2020-01-29 20:54:26 +03:00
LogPrint ( eLogError , " Garlic: Can't decode elligator " ) ;
2020-03-01 13:25:50 +03:00
return false ;
}
2020-01-29 20:54:26 +03:00
buf + = 32 ; len - = 32 ;
2020-03-01 13:25:50 +03:00
// KDF for Reply Key Section
2020-04-29 05:03:13 +03:00
uint8_t h [ 32 ] ; memcpy ( h , m_H , 32 ) ; // save m_H
2020-03-01 13:25:50 +03:00
MixHash ( tag , 8 ) ; // h = SHA256(h || tag)
MixHash ( bepk , 32 ) ; // h = SHA256(h || bepk)
uint8_t sharedSecret [ 32 ] ;
2020-04-29 05:03:13 +03:00
if ( m_State = = eSessionStateNewSessionSent )
2020-03-01 13:25:50 +03:00
{
2020-04-29 05:03:13 +03:00
// only fist time, we assume ephemeral keys the same
2020-03-01 13:25:50 +03:00
m_EphemeralKeys . Agree ( bepk , sharedSecret ) ; // sharedSecret = x25519(aesk, bepk)
i2p : : crypto : : HKDF ( m_CK , sharedSecret , 32 , " " , m_CK , 32 ) ; // chainKey = HKDF(chainKey, sharedSecret, "", 32)
2020-04-29 05:03:13 +03:00
GetOwner ( ) - > Decrypt ( bepk , sharedSecret , nullptr , i2p : : data : : CRYPTO_KEY_TYPE_ECIES_X25519_AEAD_RATCHET ) ; // x25519 (ask, bepk)
i2p : : crypto : : HKDF ( m_CK , sharedSecret , 32 , " " , m_CK ) ; // [chainKey, key] = HKDF(chainKey, sharedSecret, "", 64)
2020-03-01 13:25:50 +03:00
}
2020-01-31 03:30:30 +03:00
uint8_t nonce [ 12 ] ;
2020-02-05 23:48:51 +03:00
CreateNonce ( 0 , nonce ) ;
2020-03-01 13:25:50 +03:00
// calculate hash for zero length
if ( ! i2p : : crypto : : AEADChaCha20Poly1305 ( buf , 0 , m_H , 32 , m_CK + 32 , nonce , sharedSecret /* can be anything */ , 0 , false ) ) // decrypt, DECRYPT(k, n, ZEROLEN, ad) verification only
2020-01-29 20:54:26 +03:00
{
LogPrint ( eLogWarning , " Garlic: Reply key section AEAD decryption failed " ) ;
return false ;
}
2020-03-01 13:25:50 +03:00
MixHash ( buf , 16 ) ; // h = SHA256(h || ciphertext)
2020-01-29 20:54:26 +03:00
buf + = 16 ; len - = 16 ;
// KDF for payload
2020-03-01 13:25:50 +03:00
uint8_t keydata [ 64 ] ;
i2p : : crypto : : HKDF ( m_CK , nullptr , 0 , " " , keydata ) ; // keydata = HKDF(chainKey, ZEROLEN, "", 64)
2020-04-29 05:03:13 +03:00
if ( m_State = = eSessionStateNewSessionSent )
2020-03-01 13:25:50 +03:00
{
// k_ab = keydata[0:31], k_ba = keydata[32:63]
2020-04-29 05:03:13 +03:00
m_SendTagset = std : : make_shared < RatchetTagSet > ( shared_from_this ( ) ) ;
2020-03-01 13:25:50 +03:00
m_SendTagset - > DHInitialize ( m_CK , keydata ) ; // tagset_ab = DH_INITIALIZE(chainKey, k_ab)
2020-04-29 05:03:13 +03:00
m_SendTagset - > NextSessionTagRatchet ( ) ;
auto receiveTagset = std : : make_shared < RatchetTagSet > ( shared_from_this ( ) ) ;
2020-03-01 13:25:50 +03:00
receiveTagset - > DHInitialize ( m_CK , keydata + 32 ) ; // tagset_ba = DH_INITIALIZE(chainKey, k_ba)
2020-04-29 05:03:13 +03:00
receiveTagset - > NextSessionTagRatchet ( ) ;
GenerateMoreReceiveTags ( receiveTagset , ECIESX25519_MIN_NUM_GENERATED_TAGS ) ;
2020-03-01 13:25:50 +03:00
}
i2p : : crypto : : HKDF ( keydata + 32 , nullptr , 0 , " AttachPayloadKDF " , keydata , 32 ) ; // k = HKDF(k_ba, ZEROLEN, "AttachPayloadKDF", 32)
2020-01-29 20:54:26 +03:00
// decrypt payload
2020-03-01 13:25:50 +03:00
if ( ! i2p : : crypto : : AEADChaCha20Poly1305 ( buf , len - 16 , m_H , 32 , keydata , nonce , buf , len - 16 , false ) ) // decrypt
2020-01-29 20:54:26 +03:00
{
LogPrint ( eLogWarning , " Garlic: Payload section AEAD decryption failed " ) ;
return false ;
}
2020-04-29 05:03:13 +03:00
if ( m_State = = eSessionStateNewSessionSent )
2020-03-01 13:25:50 +03:00
{
2020-04-29 05:03:13 +03:00
m_State = eSessionStateEstablished ;
2020-05-23 17:20:22 +03:00
m_SessionCreatedTimestamp = i2p : : util : : GetSecondsSinceEpoch ( ) ;
2020-04-29 05:03:13 +03:00
GetOwner ( ) - > AddECIESx25519Session ( m_RemoteStaticKey , shared_from_this ( ) ) ;
}
2020-03-01 13:25:50 +03:00
memcpy ( m_H , h , 32 ) ; // restore m_H
HandlePayload ( buf , len - 16 , nullptr , 0 ) ;
2020-01-29 20:54:26 +03:00
2020-04-19 22:00:51 +03:00
// we have received reply to NS with LeaseSet in it
SetLeaseSetUpdateStatus ( eLeaseSetUpToDate ) ;
SetLeaseSetUpdateMsgID ( 0 ) ;
2020-03-01 13:25:50 +03:00
return true ;
}
2020-01-21 22:40:23 +03:00
2020-02-05 23:48:51 +03:00
bool ECIESX25519AEADRatchetSession : : NewExistingSessionMessage ( const uint8_t * payload , size_t len , uint8_t * out , size_t outLen )
2020-02-04 00:21:07 +03:00
{
uint8_t nonce [ 12 ] ;
2020-04-24 22:46:02 +03:00
auto index = m_SendTagset - > GetNextIndex ( ) ;
2020-02-09 05:51:02 +03:00
CreateNonce ( index , nonce ) ; // tag's index
2020-04-24 22:46:02 +03:00
uint64_t tag = m_SendTagset - > GetNextSessionTag ( ) ;
2020-02-05 23:48:51 +03:00
memcpy ( out , & tag , 8 ) ;
2020-02-04 00:21:07 +03:00
// ad = The session tag, 8 bytes
// ciphertext = ENCRYPT(k, n, payload, ad)
2020-03-08 02:46:40 +03:00
uint8_t key [ 32 ] ;
2020-04-24 22:46:02 +03:00
m_SendTagset - > GetSymmKey ( index , key ) ;
2020-03-08 02:46:40 +03:00
if ( ! i2p : : crypto : : AEADChaCha20Poly1305 ( payload , len , out , 8 , key , nonce , out + 8 , outLen - 8 , true ) ) // encrypt
2020-02-05 23:48:51 +03:00
{
LogPrint ( eLogWarning , " Garlic: Payload section AEAD encryption failed " ) ;
return false ;
2020-03-01 13:25:50 +03:00
}
2020-04-27 02:27:31 +03:00
if ( index > = ECIESX25519_TAGSET_MAX_NUM_TAGS & & ! m_SendForwardKey )
2020-03-01 13:25:50 +03:00
NewNextSendRatchet ( ) ;
2020-02-05 23:48:51 +03:00
return true ;
}
2020-03-01 13:25:50 +03:00
bool ECIESX25519AEADRatchetSession : : HandleExistingSessionMessage ( uint8_t * buf , size_t len ,
2020-04-25 04:36:08 +03:00
std : : shared_ptr < RatchetTagSet > receiveTagset , int index )
2020-02-05 23:48:51 +03:00
{
uint8_t nonce [ 12 ] ;
CreateNonce ( index , nonce ) ; // tag's index
2020-03-01 13:25:50 +03:00
len - = 8 ; // tag
2020-05-13 01:30:04 +03:00
uint8_t * payload = buf + 8 ;
2020-03-08 02:46:40 +03:00
uint8_t key [ 32 ] ;
2020-04-25 04:36:08 +03:00
receiveTagset - > GetSymmKey ( index , key ) ;
2020-05-13 01:30:04 +03:00
if ( ! i2p : : crypto : : AEADChaCha20Poly1305 ( payload , len - 16 , buf , 8 , key , nonce , payload , len - 16 , false ) ) // decrypt
2020-02-04 00:21:07 +03:00
{
LogPrint ( eLogWarning , " Garlic: Payload section AEAD decryption failed " ) ;
return false ;
2020-03-01 13:25:50 +03:00
}
HandlePayload ( payload , len - 16 , receiveTagset , index ) ;
2020-04-28 01:53:02 +03:00
int moreTags = ECIESX25519_MIN_NUM_GENERATED_TAGS + ( index > > 2 ) ; // N/4
if ( moreTags > ECIESX25519_MAX_NUM_GENERATED_TAGS ) moreTags = ECIESX25519_MAX_NUM_GENERATED_TAGS ;
2020-03-01 13:25:50 +03:00
moreTags - = ( receiveTagset - > GetNextIndex ( ) - index ) ;
2020-05-26 23:47:45 +03:00
if ( moreTags > 0 & & GetOwner ( ) )
2020-03-01 13:25:50 +03:00
GenerateMoreReceiveTags ( receiveTagset , moreTags ) ;
2020-02-04 00:21:07 +03:00
return true ;
}
2020-03-01 13:25:50 +03:00
bool ECIESX25519AEADRatchetSession : : HandleNextMessage ( uint8_t * buf , size_t len ,
2020-04-25 04:36:08 +03:00
std : : shared_ptr < RatchetTagSet > receiveTagset , int index )
2020-01-29 23:54:11 +03:00
{
2020-02-20 23:44:09 +03:00
m_LastActivityTimestamp = i2p : : util : : GetSecondsSinceEpoch ( ) ;
2020-01-29 23:54:11 +03:00
switch ( m_State )
{
2020-04-03 04:48:39 +03:00
case eSessionStateNewSessionReplySent :
m_State = eSessionStateEstablished ;
2020-06-05 01:19:38 +03:00
m_NSRSendTagset = nullptr ;
2020-04-03 04:48:39 +03:00
# if (__cplusplus >= 201703L) // C++ 17 or higher
2020-03-01 13:25:50 +03:00
[[fallthrough]] ;
# endif
2020-02-04 00:21:07 +03:00
case eSessionStateEstablished :
2020-04-29 05:03:13 +03:00
if ( HandleExistingSessionMessage ( buf , len , receiveTagset , index ) ) return true ;
2020-04-30 22:38:15 +03:00
// check NSR just in case
LogPrint ( eLogDebug , " Garlic: check for out of order NSR with index " , index ) ;
if ( receiveTagset - > GetNextIndex ( ) - index < ECIESX25519_NSR_NUM_GENERATED_TAGS / 2 )
GenerateMoreReceiveTags ( receiveTagset , ECIESX25519_NSR_NUM_GENERATED_TAGS ) ;
return HandleNewOutgoingSessionReply ( buf , len ) ;
2020-01-29 23:54:11 +03:00
case eSessionStateNew :
2020-02-04 00:21:07 +03:00
return HandleNewIncomingSession ( buf , len ) ;
2020-01-29 23:54:11 +03:00
case eSessionStateNewSessionSent :
2020-05-01 04:27:35 +03:00
receiveTagset - > Expire ( ) ; // NSR tagset
2020-02-04 00:21:07 +03:00
return HandleNewOutgoingSessionReply ( buf , len ) ;
2020-01-29 23:54:11 +03:00
default :
return false ;
}
return true ;
}
2020-03-01 13:25:50 +03:00
std : : shared_ptr < I2NPMessage > ECIESX25519AEADRatchetSession : : WrapSingleMessage ( std : : shared_ptr < const I2NPMessage > msg )
{
auto payload = CreatePayload ( msg , m_State ! = eSessionStateEstablished ) ;
size_t len = payload . size ( ) ;
2020-05-17 02:10:17 +03:00
if ( ! len ) return nullptr ;
2020-04-30 22:38:15 +03:00
auto m = NewI2NPMessage ( len + 100 ) ; // 96 + 4
m - > Align ( 12 ) ; // in order to get buf aligned to 16 (12 + 4)
uint8_t * buf = m - > GetPayload ( ) + 4 ; // 4 bytes for length
2020-03-01 13:25:50 +03:00
switch ( m_State )
{
2020-02-05 23:48:51 +03:00
case eSessionStateEstablished :
if ( ! NewExistingSessionMessage ( payload . data ( ) , payload . size ( ) , buf , m - > maxLen ) )
return nullptr ;
len + = 24 ;
2020-03-01 13:25:50 +03:00
break ;
case eSessionStateNew :
if ( ! NewOutgoingSessionMessage ( payload . data ( ) , payload . size ( ) , buf , m - > maxLen ) )
return nullptr ;
len + = 96 ;
break ;
case eSessionStateNewSessionReceived :
if ( ! NewSessionReplyMessage ( payload . data ( ) , payload . size ( ) , buf , m - > maxLen ) )
return nullptr ;
len + = 72 ;
break ;
2020-04-03 04:48:39 +03:00
case eSessionStateNewSessionReplySent :
if ( ! NextNewSessionReplyMessage ( payload . data ( ) , payload . size ( ) , buf , m - > maxLen ) )
2020-03-01 13:25:50 +03:00
return nullptr ;
len + = 72 ;
break ;
default :
return nullptr ;
}
htobe32buf ( m - > GetPayload ( ) , len ) ;
2020-01-17 19:21:41 +03:00
m - > len + = len + 4 ;
m - > FillI2NPMessageHeader ( eI2NPGarlic ) ;
return m ;
2020-03-01 13:25:50 +03:00
}
2020-01-17 19:21:41 +03:00
2020-03-01 13:25:50 +03:00
std : : vector < uint8_t > ECIESX25519AEADRatchetSession : : CreatePayload ( std : : shared_ptr < const I2NPMessage > msg , bool first )
{
2020-03-09 01:13:41 +03:00
uint64_t ts = i2p : : util : : GetMillisecondsSinceEpoch ( ) ;
2020-03-01 13:25:50 +03:00
size_t payloadLen = 0 ;
if ( first ) payloadLen + = 7 ; // datatime
if ( msg & & m_Destination )
payloadLen + = msg - > GetPayloadLength ( ) + 13 + 32 ;
2020-05-19 05:31:36 +03:00
auto leaseSet = ( GetLeaseSetUpdateStatus ( ) = = eLeaseSetUpdated | |
2020-03-01 13:25:50 +03:00
( GetLeaseSetUpdateStatus ( ) = = eLeaseSetSubmitted & &
ts > GetLeaseSetSubmissionTime ( ) + LEASET_CONFIRMATION_TIMEOUT ) ) ?
2020-05-19 05:31:36 +03:00
GetOwner ( ) - > GetLeaseSet ( ) : nullptr ;
2020-03-09 01:13:41 +03:00
if ( leaseSet )
2020-03-01 13:25:50 +03:00
{
payloadLen + = leaseSet - > GetBufferLen ( ) + DATABASE_STORE_HEADER_SIZE + 13 ;
if ( ! first )
2020-04-19 22:00:51 +03:00
{
// ack request
SetLeaseSetUpdateStatus ( eLeaseSetSubmitted ) ;
2020-04-24 22:46:02 +03:00
SetLeaseSetUpdateMsgID ( m_SendTagset - > GetNextIndex ( ) ) ;
2020-04-19 22:00:51 +03:00
SetLeaseSetSubmissionTime ( ts ) ;
2020-03-01 13:25:50 +03:00
payloadLen + = 4 ;
}
}
2020-03-27 02:03:38 +03:00
if ( m_AckRequests . size ( ) > 0 )
payloadLen + = m_AckRequests . size ( ) * 4 + 3 ;
2020-04-26 04:09:03 +03:00
if ( m_SendReverseKey )
2020-03-01 13:25:50 +03:00
{
2020-04-26 04:09:03 +03:00
payloadLen + = 6 ;
if ( m_NextReceiveRatchet - > newKey ) payloadLen + = 32 ;
2020-03-01 13:25:50 +03:00
}
2020-04-27 02:27:31 +03:00
if ( m_SendForwardKey )
{
payloadLen + = 6 ;
if ( m_NextSendRatchet - > newKey ) payloadLen + = 32 ;
2020-05-17 02:10:17 +03:00
}
uint8_t paddingSize = 0 ;
if ( payloadLen )
2020-03-01 13:25:50 +03:00
{
2020-05-20 02:03:12 +03:00
int delta = ( int ) ECIESX25519_OPTIMAL_PAYLOAD_SIZE - ( int ) payloadLen ;
if ( delta < 0 | | delta > 3 ) // don't create padding if we are close to optimal size
{
2020-05-18 23:42:06 +03:00
RAND_bytes ( & paddingSize , 1 ) ;
2020-05-20 02:03:12 +03:00
paddingSize & = 0x0F ; // 0 - 15
if ( delta > 3 )
2020-03-01 13:25:50 +03:00
{
2020-05-20 02:03:12 +03:00
delta - = 3 ;
2020-03-01 13:25:50 +03:00
if ( paddingSize > = delta ) paddingSize % = delta ;
}
2020-05-20 02:03:12 +03:00
paddingSize + + ;
2020-03-01 13:25:50 +03:00
payloadLen + = paddingSize + 3 ;
}
}
std : : vector < uint8_t > v ( payloadLen ) ;
size_t offset = 0 ;
// DateTime
2020-04-19 22:00:51 +03:00
if ( first )
2020-03-01 13:25:50 +03:00
{
v [ offset ] = eECIESx25519BlkDateTime ; offset + + ;
htobe16buf ( v . data ( ) + offset , 4 ) ; offset + = 2 ;
htobe32buf ( v . data ( ) + offset , ts / 1000 ) ; offset + = 4 ; // in seconds
}
// LeaseSet
if ( leaseSet )
{
offset + = CreateLeaseSetClove ( leaseSet , ts , v . data ( ) + offset , payloadLen - offset ) ;
2020-04-19 22:00:51 +03:00
if ( ! first )
2020-03-01 13:25:50 +03:00
{
2020-04-19 22:00:51 +03:00
// ack request
v [ offset ] = eECIESx25519BlkAckRequest ; offset + + ;
htobe16buf ( v . data ( ) + offset , 1 ) ; offset + = 2 ;
v [ offset ] = 0 ; offset + + ; // flags
2020-03-01 13:25:50 +03:00
}
}
// msg
if ( msg & & m_Destination )
offset + = CreateGarlicClove ( msg , v . data ( ) + offset , payloadLen - offset , true ) ;
2020-03-27 02:03:38 +03:00
// ack
if ( m_AckRequests . size ( ) > 0 )
{
v [ offset ] = eECIESx25519BlkAck ; offset + + ;
2020-03-01 13:25:50 +03:00
htobe16buf ( v . data ( ) + offset , m_AckRequests . size ( ) * 4 ) ; offset + = 2 ;
2020-03-27 02:03:38 +03:00
for ( auto & it : m_AckRequests )
{
htobe16buf ( v . data ( ) + offset , it . first ) ; offset + = 2 ;
htobe16buf ( v . data ( ) + offset , it . second ) ; offset + = 2 ;
2020-03-01 13:25:50 +03:00
}
2020-03-27 02:03:38 +03:00
m_AckRequests . clear ( ) ;
2020-03-01 13:25:50 +03:00
}
2020-04-25 04:36:08 +03:00
// next keys
2020-04-26 04:09:03 +03:00
if ( m_SendReverseKey )
2020-04-25 04:36:08 +03:00
{
2020-04-25 21:45:53 +03:00
v [ offset ] = eECIESx25519BlkNextKey ; offset + + ;
2020-04-26 04:09:03 +03:00
htobe16buf ( v . data ( ) + offset , m_NextReceiveRatchet - > newKey ? 35 : 3 ) ; offset + = 2 ;
2020-03-01 13:25:50 +03:00
v [ offset ] = ECIESX25519_NEXT_KEY_REVERSE_KEY_FLAG ;
2020-04-26 04:09:03 +03:00
int keyID = m_NextReceiveRatchet - > keyID - 1 ;
2020-03-01 13:25:50 +03:00
if ( m_NextReceiveRatchet - > newKey )
{
2020-04-26 04:09:03 +03:00
v [ offset ] | = ECIESX25519_NEXT_KEY_KEY_PRESENT_FLAG ;
keyID + + ;
2020-03-01 13:25:50 +03:00
}
2020-04-26 04:09:03 +03:00
offset + + ; // flag
htobe16buf ( v . data ( ) + offset , keyID ) ; offset + = 2 ; // keyid
if ( m_NextReceiveRatchet - > newKey )
2020-03-01 13:25:50 +03:00
{
memcpy ( v . data ( ) + offset , m_NextReceiveRatchet - > key . GetPublicKey ( ) , 32 ) ;
2020-04-26 04:09:03 +03:00
offset + = 32 ; // public key
2020-03-01 13:25:50 +03:00
}
2020-04-26 04:09:03 +03:00
m_SendReverseKey = false ;
2020-03-01 13:25:50 +03:00
}
2020-04-27 02:27:31 +03:00
if ( m_SendForwardKey )
{
v [ offset ] = eECIESx25519BlkNextKey ; offset + + ;
htobe16buf ( v . data ( ) + offset , m_NextSendRatchet - > newKey ? 35 : 3 ) ; offset + = 2 ;
2020-03-01 13:25:50 +03:00
v [ offset ] = m_NextSendRatchet - > newKey ? ECIESX25519_NEXT_KEY_KEY_PRESENT_FLAG : ECIESX25519_NEXT_KEY_REQUEST_REVERSE_KEY_FLAG ;
2020-04-27 02:27:31 +03:00
if ( ! m_NextSendRatchet - > keyID ) v [ offset ] | = ECIESX25519_NEXT_KEY_REQUEST_REVERSE_KEY_FLAG ; // for first key only
offset + + ; // flag
htobe16buf ( v . data ( ) + offset , m_NextSendRatchet - > keyID ) ; offset + = 2 ; // keyid
if ( m_NextSendRatchet - > newKey )
2020-03-01 13:25:50 +03:00
{
memcpy ( v . data ( ) + offset , m_NextSendRatchet - > key . GetPublicKey ( ) , 32 ) ;
2020-04-27 02:27:31 +03:00
offset + = 32 ; // public key
2020-03-01 13:25:50 +03:00
}
}
// padding
2020-05-17 02:10:17 +03:00
if ( paddingSize )
2020-03-01 13:25:50 +03:00
{
v [ offset ] = eECIESx25519BlkPadding ; offset + + ;
htobe16buf ( v . data ( ) + offset , paddingSize ) ; offset + = 2 ;
memset ( v . data ( ) + offset , 0 , paddingSize ) ; offset + = paddingSize ;
}
return v ;
}
size_t ECIESX25519AEADRatchetSession : : CreateGarlicClove ( std : : shared_ptr < const I2NPMessage > msg , uint8_t * buf , size_t len , bool isDestination )
{
if ( ! msg ) return 0 ;
uint16_t cloveSize = msg - > GetPayloadLength ( ) + 9 + 1 ;
2020-01-30 19:48:32 +03:00
if ( isDestination ) cloveSize + = 32 ;
2020-03-01 13:25:50 +03:00
if ( ( int ) len < cloveSize + 3 ) return 0 ;
buf [ 0 ] = eECIESx25519BlkGalicClove ; // clove type
htobe16buf ( buf + 1 , cloveSize ) ; // size
2020-01-30 19:48:32 +03:00
buf + = 3 ;
if ( isDestination )
{
* buf = ( eGarlicDeliveryTypeDestination < < 5 ) ;
memcpy ( buf + 1 , * m_Destination , 32 ) ; buf + = 32 ;
}
else
2020-03-01 13:25:50 +03:00
* buf = 0 ;
2020-01-30 19:48:32 +03:00
buf + + ; // flag and delivery instructions
2020-03-01 13:25:50 +03:00
* buf = msg - > GetTypeID ( ) ; // I2NP msg type
htobe32buf ( buf + 1 , msg - > GetMsgID ( ) ) ; // msgID
htobe32buf ( buf + 5 , msg - > GetExpiration ( ) / 1000 ) ; // expiration in seconds
memcpy ( buf + 9 , msg - > GetPayload ( ) , msg - > GetPayloadLength ( ) ) ;
return cloveSize + 3 ;
}
2020-02-10 01:19:42 +03:00
2020-05-14 01:09:26 +03:00
size_t ECIESX25519AEADRatchetSession : : CreateLeaseSetClove ( std : : shared_ptr < const i2p : : data : : LocalLeaseSet > ls , uint64_t ts , uint8_t * buf , size_t len )
2020-03-01 13:25:50 +03:00
{
2020-05-14 01:09:26 +03:00
if ( ! ls | | ls - > GetStoreType ( ) ! = i2p : : data : : NETDB_STORE_TYPE_STANDARD_LEASESET2 )
{
LogPrint ( eLogError , " Garlic: Incorrect LeasetSet type to send " ) ;
return 0 ;
2020-03-01 13:25:50 +03:00
}
2020-05-14 01:09:26 +03:00
uint16_t cloveSize = 1 + 9 + DATABASE_STORE_HEADER_SIZE + ls - > GetBufferLen ( ) ; // to local
2020-03-09 01:13:41 +03:00
if ( ( int ) len < cloveSize + 3 ) return 0 ;
buf [ 0 ] = eECIESx25519BlkGalicClove ; // clove type
2020-03-01 13:25:50 +03:00
htobe16buf ( buf + 1 , cloveSize ) ; // size
2020-03-09 01:13:41 +03:00
buf + = 3 ;
2020-05-14 01:09:26 +03:00
* buf = 0 ; buf + + ; // flag and delivery instructions
* buf = eI2NPDatabaseStore ; buf + + ; // I2NP msg type
RAND_bytes ( buf , 4 ) ; buf + = 4 ; // msgID
2020-03-01 13:25:50 +03:00
htobe32buf ( buf , ( ts + I2NP_MESSAGE_EXPIRATION_TIMEOUT ) / 1000 ) ; buf + = 4 ; // expiration
2020-05-14 01:09:26 +03:00
// payload
memcpy ( buf + DATABASE_STORE_KEY_OFFSET , ls - > GetStoreHash ( ) , 32 ) ;
buf [ DATABASE_STORE_TYPE_OFFSET ] = i2p : : data : : NETDB_STORE_TYPE_STANDARD_LEASESET2 ;
2020-03-01 13:25:50 +03:00
memset ( buf + DATABASE_STORE_REPLY_TOKEN_OFFSET , 0 , 4 ) ; // replyToken = 0
2020-05-14 01:09:26 +03:00
buf + = DATABASE_STORE_HEADER_SIZE ;
memcpy ( buf , ls - > GetBuffer ( ) , ls - > GetBufferLen ( ) ) ;
2020-03-09 01:13:41 +03:00
return cloveSize + 3 ;
2020-05-14 01:09:26 +03:00
}
2020-03-01 13:25:50 +03:00
2020-04-25 04:36:08 +03:00
void ECIESX25519AEADRatchetSession : : GenerateMoreReceiveTags ( std : : shared_ptr < RatchetTagSet > receiveTagset , int numTags )
2020-02-10 01:19:42 +03:00
{
for ( int i = 0 ; i < numTags ; i + + )
2020-04-25 04:36:08 +03:00
GetOwner ( ) - > AddECIESx25519SessionNextTag ( receiveTagset ) ;
2020-03-01 13:25:50 +03:00
}
2020-03-14 23:35:34 +03:00
bool ECIESX25519AEADRatchetSession : : CheckExpired ( uint64_t ts )
2020-03-01 13:25:50 +03:00
{
2020-03-14 23:35:34 +03:00
CleanupUnconfirmedLeaseSet ( ts ) ;
2020-03-01 13:25:50 +03:00
return ts > m_LastActivityTimestamp + ECIESX25519_EXPIRATION_TIMEOUT ;
}
2020-04-07 18:40:18 +03:00
std : : shared_ptr < I2NPMessage > WrapECIESX25519AEADRatchetMessage ( std : : shared_ptr < const I2NPMessage > msg , const uint8_t * key , uint64_t tag )
{
auto m = NewI2NPMessage ( ) ;
m - > Align ( 12 ) ; // in order to get buf aligned to 16 (12 + 4)
uint8_t * buf = m - > GetPayload ( ) + 4 ; // 4 bytes for length
uint8_t nonce [ 12 ] ;
2020-03-01 13:25:50 +03:00
memset ( nonce , 0 , 12 ) ; // n = 0
2020-04-07 18:40:18 +03:00
size_t offset = 0 ;
memcpy ( buf + offset , & tag , 8 ) ; offset + = 8 ;
auto payload = buf + offset ;
uint16_t cloveSize = msg - > GetPayloadLength ( ) + 9 + 1 ;
size_t len = cloveSize + 3 ;
2020-03-01 13:25:50 +03:00
payload [ 0 ] = eECIESx25519BlkGalicClove ; // clove type
htobe16buf ( payload + 1 , cloveSize ) ; // size
2020-04-07 18:40:18 +03:00
payload + = 3 ;
2020-03-01 13:25:50 +03:00
* payload = 0 ; payload + + ; // flag and delivery instructions
* payload = msg - > GetTypeID ( ) ; // I2NP msg type
htobe32buf ( payload + 1 , msg - > GetMsgID ( ) ) ; // msgID
htobe32buf ( payload + 5 , msg - > GetExpiration ( ) / 1000 ) ; // expiration in seconds
memcpy ( payload + 9 , msg - > GetPayload ( ) , msg - > GetPayloadLength ( ) ) ;
2020-04-07 18:40:18 +03:00
if ( ! i2p : : crypto : : AEADChaCha20Poly1305 ( buf + offset , len , buf , 8 , key , nonce , buf + offset , len + 16 , true ) ) // encrypt
{
LogPrint ( eLogWarning , " Garlic: Payload section AEAD encryption failed " ) ;
return nullptr ;
2020-03-01 13:25:50 +03:00
}
2020-04-07 18:40:18 +03:00
offset + = len + 16 ;
2020-03-01 13:25:50 +03:00
2020-04-07 18:40:18 +03:00
htobe32buf ( m - > GetPayload ( ) , offset ) ;
m - > len + = offset + 4 ;
m - > FillI2NPMessageHeader ( eI2NPGarlic ) ;
return m ;
}
2020-03-01 13:25:50 +03:00
2020-01-15 23:13:43 +03:00
}
}