@ -44,29 +44,77 @@ namespace iwp
gen_intro ( void * user )
{
iwp_async_intro * intro = static_cast < iwp_async_intro * > ( user ) ;
char ftmp [ 68 ] = { 0 } ;
const char * hexname = llarp : : HexEncode < llarp : : PubKey , decltype ( ftmp ) > (
intro - > remote_pubkey , ftmp ) ;
llarp : : LogDebug ( " gen_intro remote_pubkey: " , hexname ) ;
llarp : : SharedSecret sharedkey ;
llarp : : ShortHash e_k ;
llarp_crypto * crypto = intro - > iwp - > crypto ;
byte_t tmp [ 64 ] ;
// S = TKE(a.k, b.k, n)
crypto - > transport_dh_client ( sharedkey , intro - > remote_pubkey ,
intro - > secretkey , intro - > nonce ) ;
char ftmpShared [ 68 ] = { 0 } ;
const char * hexShared =
llarp : : HexEncode < llarp : : SharedSecret , decltype ( ftmpShared ) > (
sharedkey , ftmpShared ) ;
llarp : : LogDebug ( " gen_intro sharedkey " , hexShared ) ;
byte_t tmp [ 64 ] ;
auto buf = llarp : : StackBuffer < decltype ( tmp ) > ( tmp ) ;
llarp : : SymmNonce n ;
// copy nonce
memcpy ( n , intro - > nonce , 24 ) ;
char ftmpN [ 68 ] = { 0 } ;
const char * hexN =
llarp : : HexEncode < llarp : : SymmNonce , decltype ( ftmpN ) > ( n , ftmpN ) ;
llarp : : LogDebug ( " gen_intro nonce " , hexN ) ;
// e_k = HS(b.k + n)
llarp : : ShortHash e_k ;
memcpy ( tmp , intro - > remote_pubkey , 32 ) ;
memcpy ( tmp + 32 , intro - > nonce , 32 ) ;
crypto - > shorthash ( e_k , buf ) ;
char ftmpEk [ 68 ] = { 0 } ;
const char * hexEk =
llarp : : HexEncode < llarp : : ShortHash , decltype ( ftmpEk ) > ( e_k , ftmpEk ) ;
llarp : : LogDebug ( " gen_intro e_k " , hexEk , " used " , strlen ( hexEk ) ) ;
// e = SE(a.k, e_k, n[0:24])
memcpy ( intro - > buf + 64 , llarp : : seckey_topublic ( intro - > secretkey ) , 32 ) ;
char ftmpSk [ 68 ] = { 0 } ;
const char * hexSk = llarp : : HexEncode < llarp : : PubKey , decltype ( ftmpSk ) > (
intro - > secretkey , ftmpSk ) ;
llarp : : LogDebug ( " gen_intro SK " , hexSk ) ;
char ftmpSkPub [ 68 ] = { 0 } ;
const char * hexSkPub =
llarp : : HexEncode < llarp : : PubKey , decltype ( ftmpSkPub ) > (
llarp : : seckey_topublic ( intro - > secretkey ) , ftmpSkPub ) ;
llarp : : LogDebug ( " gen_intro SK pub " , hexSkPub ) ;
buf . base = intro - > buf + 64 ;
buf . cur = buf . base ;
buf . sz = 32 ;
crypto - > xchacha20 ( buf , e_k , intro - > nonce ) ;
// h = MDS( n + e + w0, S)
buf . base = intro - > buf + 32 ;
buf . cur = buf . base ;
buf . sz = intro - > sz - 32 ;
crypto - > hmac ( intro - > buf , buf , sharedkey ) ;
char ftmpHmac [ 68 ] = { 0 } ; // probably could be 65
const char * hexHmac = llarp : : HexEncode < llarp : : PubKey , decltype ( ftmpHmac ) > (
intro - > buf , ftmpHmac ) ;
llarp : : LogDebug ( " gen_intro Hmac " , hexHmac ) ;
// inform result
// intro->hook(intro);
llarp_logic_queue_job ( intro - > iwp - > logic , { intro , & inform_intro } ) ;
@ -82,6 +130,12 @@ namespace iwp
llarp : : SharedSecret h ;
byte_t tmp [ 64 ] ;
const auto OurPK = llarp : : seckey_topublic ( intro - > secretkey ) ;
char ftmp [ 68 ] = { 0 } ;
const char * hexPk =
llarp : : HexEncode < llarp : : PubKey , decltype ( ftmp ) > ( OurPK , ftmp ) ;
llarp : : LogDebug ( " intro OurPK " , hexPk ) ;
// e_k = HS(b.k + n)
memcpy ( tmp , OurPK , 32 ) ;
memcpy ( tmp + 32 , intro - > nonce , 32 ) ;
@ -92,11 +146,25 @@ namespace iwp
buf . cur = buf . base ;
buf . sz = 32 ;
memcpy ( intro - > remote_pubkey , intro - > buf + 64 , 32 ) ;
crypto - > xchacha20 ( buf , e_K , intro - > nonce ) ;
llarp : : LogInfo ( " handshake from " , llarp : : RouterID ( intro - > remote_pubkey ) ) ;
char ftmp2 [ 68 ] = { 0 } ;
const char * hexRemotePK = llarp : : HexEncode < llarp : : PubKey , decltype ( ftmp ) > (
intro - > remote_pubkey , ftmp2 ) ;
llarp : : LogDebug ( " intro remote_pubkey " , hexRemotePK ) ;
// S = TKE(a.k, b.k, n)
crypto - > transport_dh_server ( sharedkey , intro - > remote_pubkey ,
intro - > secretkey , intro - > nonce ) ;
char ftmpShared [ 68 ] = { 0 } ;
const char * hexShared =
llarp : : HexEncode < llarp : : PubKey , decltype ( ftmpShared ) > ( sharedkey ,
ftmpShared ) ;
llarp : : LogDebug ( " intro sharedkey " , hexShared ) ;
// h = MDS( n + e + w2, S)
buf . base = intro - > buf + 32 ;
buf . cur = buf . base ;
@ -104,6 +172,7 @@ namespace iwp
crypto - > hmac ( h , buf , sharedkey ) ;
if ( memcmp ( h , intro - > buf , 32 ) )
{
llarp : : LogWarn ( " intro HMAC failure " ) ;
// hmac fail
intro - > buf = nullptr ;
}
@ -375,11 +444,19 @@ iwp_decrypt_frame(struct iwp_async_frame *frame)
crypto - > hmac ( digest , buf , frame - > sessionkey ) ;
// check hmac
frame - > success = memcmp ( digest , hmac , 32 ) = = 0 ;
if ( ! frame - > success )
{
// [", digest, "] vs [", hmac, "]
llarp : : LogWarn ( " crypto_async::iwp_decrypt_frame failed to decrypt " ) ;
//} else {
// llarp::Debug("crypto_async::iwp_decrypt_frame decrypted");
}
// x = SE(S, p, n[0:24])
buf . base = body ;
buf . cur = buf . base ;
buf . sz = frame - > sz - 64 ;
crypto - > xchacha20 ( buf , frame - > sessionkey , nonce ) ;
return frame - > success ;
}