diff --git a/app/src/main/java/com/bitchat/android/mesh/BluetoothMeshService.kt b/app/src/main/java/com/bitchat/android/mesh/BluetoothMeshService.kt index 73d06df3..b54691dc 100644 --- a/app/src/main/java/com/bitchat/android/mesh/BluetoothMeshService.kt +++ b/app/src/main/java/com/bitchat/android/mesh/BluetoothMeshService.kt @@ -61,7 +61,7 @@ class BluetoothMeshService(private val context: Context) { init { setupDelegates() - startPeriodicDebugLogging() +// startPeriodicDebugLogging() } /** diff --git a/app/src/main/java/com/bitchat/android/noise/NoiseSession.kt b/app/src/main/java/com/bitchat/android/noise/NoiseSession.kt index add0cf40..26ee3b03 100644 --- a/app/src/main/java/com/bitchat/android/noise/NoiseSession.kt +++ b/app/src/main/java/com/bitchat/android/noise/NoiseSession.kt @@ -2,6 +2,7 @@ package com.bitchat.android.noise import android.util.Log import com.bitchat.android.noise.southernstorm.protocol.* +import com.bitchat.android.util.toHexString import java.security.SecureRandom @@ -148,7 +149,7 @@ class NoiseSession( Log.d(TAG, "Persistent identity public key: ${localStaticPublicKey.joinToString("") { "%02x".format(it) }}") Log.d(TAG, "Set public key: ${verifyPublic.joinToString("") { "%02x".format(it) }}") - + } else { throw IllegalStateException("HandshakeState returned null for local key pair") } @@ -156,10 +157,10 @@ class NoiseSession( } else { Log.d(TAG, "Local static key pair not needed for this handshake pattern/role") } - + handshakeState?.setPrologue(PROTOCOL_NAME.toByteArray(), 0, PROTOCOL_NAME.toByteArray().size) handshakeState?.start() Log.d(TAG, "Handshake state started successfully with persistent identity keys") - + } catch (e: Exception) { Log.e(TAG, "Exception during handshake initialization: ${e.message}", e) throw e @@ -193,7 +194,7 @@ class NoiseSession( val messageBuffer = ByteArray(XX_MESSAGE_1_SIZE) val handshakeStateLocal = handshakeState ?: throw IllegalStateException("Handshake state is null") - val messageLength = handshakeStateLocal.writeMessage(messageBuffer, 0, ByteArray(0), 0, 0) + val messageLength = handshakeStateLocal.writeMessage(messageBuffer, 0, null, 0, 0) val firstMessage = messageBuffer.copyOf(messageLength) // Validate message size matches XX pattern expectations @@ -223,7 +224,7 @@ class NoiseSession( if (state == NoiseSessionState.Uninitialized && !isInitiator) { initializeNoiseHandshake(HandshakeState.RESPONDER) state = NoiseSessionState.Handshaking - Log.d(TAG, "Initialized as responder for real XX handshake with $peerID") + Log.d(TAG, "Initialized as RESPONDER for XX handshake with $peerID") } if (state != NoiseSessionState.Handshaking) { @@ -246,8 +247,8 @@ class NoiseSession( return when (action) { HandshakeState.WRITE_MESSAGE -> { // Noise library says we need to send a response - val responseBuffer = ByteArray(MAX_PAYLOAD_SIZE) // Large buffer for any response - val responseLength = handshakeStateLocal.writeMessage(responseBuffer, 0, ByteArray(0), 0, 0) + val responseBuffer = ByteArray(XX_MESSAGE_2_SIZE + MAX_PAYLOAD_SIZE) // Large buffer for any response + val responseLength = handshakeStateLocal.writeMessage(responseBuffer, 0, null, 0, 0) val response = responseBuffer.copyOf(responseLength) Log.d(TAG, "Generated handshake response: ${response.size} bytes, action still: ${handshakeStateLocal.getAction()}") diff --git a/app/src/main/java/com/bitchat/android/noise/NoiseSessionManager.kt b/app/src/main/java/com/bitchat/android/noise/NoiseSessionManager.kt index a4b5e8dc..eb9eb790 100644 --- a/app/src/main/java/com/bitchat/android/noise/NoiseSessionManager.kt +++ b/app/src/main/java/com/bitchat/android/noise/NoiseSessionManager.kt @@ -111,7 +111,7 @@ class NoiseSessionManager( val shouldInitiate = resolveTieBreaker(peerID) if (!shouldInitiate) { Log.d(TAG, "Tie-breaker: Waiting for $peerID to initiate handshake") - throw IllegalStateException("Tie-breaker: Should not initiate with $peerID") +// throw IllegalStateException("Tie-breaker: Should not initiate with $peerID") } // Remove any existing non-established session diff --git a/app/src/main/java/com/bitchat/android/noise/southernstorm/protocol/HandshakeState.java b/app/src/main/java/com/bitchat/android/noise/southernstorm/protocol/HandshakeState.java index 8e275476..31e746c5 100644 --- a/app/src/main/java/com/bitchat/android/noise/southernstorm/protocol/HandshakeState.java +++ b/app/src/main/java/com/bitchat/android/noise/southernstorm/protocol/HandshakeState.java @@ -22,6 +22,8 @@ package com.bitchat.android.noise.southernstorm.protocol; +import android.util.Log; + import java.security.NoSuchAlgorithmException; import java.util.Arrays; @@ -545,7 +547,18 @@ public class HandshakeState implements Destroyable { if ((requirements & LOCAL_PREMSG) != 0) symmetric.mixPublicKey(localKeyPair); } - + // Log the symmetric.hash in hex: + byte[] currentHandshakeHash = symmetric.h; + if (currentHandshakeHash != null) { + StringBuilder hexString = new StringBuilder(currentHandshakeHash.length * 2); + for (byte b : currentHandshakeHash) { + hexString.append(String.format("%02X", b)); + } + Log.d("TAG", "Initial Handshake Hash (h): " + hexString.toString()); + } else { + Log.d("TAG", "Initial Handshake Hash (h): null"); + } + // The handshake has officially started - set the first action. if (isInitiator) action = WRITE_MESSAGE;