@@ -7,7 +7,7 @@ use super::registration::process_registration;
77use super :: LpHandlerState ;
88use crate :: error:: GatewayError ;
99use nym_lp:: {
10- keypair:: { Keypair , PublicKey } ,
10+ keypair:: { Keypair , PrivateKey as LpPrivateKey , PublicKey } ,
1111 LpMessage , LpPacket , LpSession ,
1212} ;
1313use nym_metrics:: { add_histogram_obs, inc} ;
@@ -109,10 +109,21 @@ impl LpConnectionHandler {
109109 // 2. Client's public key (will be received during handshake)
110110 // 3. PSK (pre-shared key) - for now use a placeholder
111111
112- // Generate fresh LP keypair (x25519) for this connection
113- // Using Keypair::default() which generates a new random x25519 keypair
114- // This is secure and simple - each connection gets its own keypair
115- let gateway_keypair = Keypair :: default ( ) ;
112+ // Derive LP keypair from gateway's ed25519 identity using proper conversion
113+ // This creates a valid x25519 keypair for ECDH operations in Noise protocol
114+ let x25519_private = self . state . local_identity . private_key ( ) . to_x25519 ( ) ;
115+ let x25519_public = self . state . local_identity . public_key ( ) . to_x25519 ( )
116+ . map_err ( |e| GatewayError :: LpHandshakeError (
117+ format ! ( "Failed to convert ed25519 public key to x25519: {}" , e)
118+ ) ) ?;
119+
120+ let lp_private = LpPrivateKey :: from_bytes ( x25519_private. as_bytes ( ) ) ;
121+ let lp_public = PublicKey :: from_bytes ( x25519_public. as_bytes ( ) )
122+ . map_err ( |e| GatewayError :: LpHandshakeError (
123+ format ! ( "Failed to create LP public key: {}" , e)
124+ ) ) ?;
125+
126+ let gateway_keypair = Keypair :: from_keys ( lp_private, lp_public) ;
116127
117128 // Receive client's public key and salt via ClientHello message
118129 // The client initiates by sending ClientHello as first packet
@@ -289,11 +300,7 @@ impl LpConnectionHandler {
289300 . duration_since( UNIX_EPOCH )
290301 . expect( "System time before UNIX epoch" )
291302 . as_secs( ) ;
292- if now >= timestamp {
293- now - timestamp
294- } else {
295- timestamp - now
296- }
303+ now. abs_diff( timestamp)
297304 } ,
298305 self . state. lp_config. timestamp_tolerance_secs
299306 ) ;
@@ -333,22 +340,20 @@ impl LpConnectionHandler {
333340 ) ) ) ;
334341 }
335342
336- // Extract registration request from LP message
337- match packet. message ( ) {
338- LpMessage :: EncryptedData ( data) => {
339- // Deserialize registration request
340- bincode:: deserialize ( & data) . map_err ( |e| {
341- GatewayError :: LpProtocolError ( format ! (
342- "Failed to deserialize registration request: {}" ,
343- e
344- ) )
345- } )
346- }
347- other => Err ( GatewayError :: LpProtocolError ( format ! (
348- "Expected EncryptedData message, got {:?}" ,
349- other
350- ) ) ) ,
351- }
343+ // Decrypt the packet payload using the established session
344+ let decrypted_bytes = session
345+ . decrypt_data ( packet. message ( ) )
346+ . map_err ( |e| {
347+ GatewayError :: LpProtocolError ( format ! ( "Failed to decrypt registration request: {}" , e) )
348+ } ) ?;
349+
350+ // Deserialize the decrypted bytes into LpRegistrationRequest
351+ bincode:: deserialize ( & decrypted_bytes) . map_err ( |e| {
352+ GatewayError :: LpProtocolError ( format ! (
353+ "Failed to deserialize registration request: {}" ,
354+ e
355+ ) )
356+ } )
352357 }
353358
354359 /// Send registration response after processing
0 commit comments