Skip to content

Commit f677e26

Browse files
committed
fix: correct handshake hash upon last read
1 parent 088b62e commit f677e26

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/main/kotlin/Handshake.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ data class Handshake(
102102
}?.let { s ->
103103
val rest = messagePatterns.drop(1)
104104
if (rest.isEmpty()) s.value.symmetry.split()
105-
.let { State(Transport(it.first, it.second, symmetry.handshakeHash.digest), s.result) }
105+
.let { State(Transport(it.first, it.second, s.value.symmetry.handshakeHash.digest), s.result) }
106106
else State(s.value.copy(messagePatterns = rest), s.result)
107107
}
108108

src/test/kotlin/HandshakeTest.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,33 @@ class HandshakeTest {
9595

9696
println(alice02)
9797
}
98+
99+
/** https://github.com/sander/noise-kotlin/discussions/15 */
100+
@Test
101+
fun testFinalHandshakeHash() {
102+
val aliceStaticKey = JavaCryptography.generateKeyPair()
103+
val alice00 = Handshake.initialize(
104+
JavaCryptography,
105+
Handshake.Noise_XN_25519_ChaChaPoly_SHA256,
106+
Role.INITIATOR,
107+
Data.empty,
108+
localEphemeralKeyPair = JavaCryptography.generateKeyPair(),
109+
localStaticKeyPair = aliceStaticKey
110+
)!!
111+
val bob00 = Handshake.initialize(
112+
JavaCryptography,
113+
Handshake.Noise_XN_25519_ChaChaPoly_SHA256,
114+
Role.RESPONDER,
115+
Data.empty,
116+
localEphemeralKeyPair = JavaCryptography.generateKeyPair(),
117+
trustedStaticKeys = setOf(aliceStaticKey.first)
118+
)!!
119+
val alice01 = alice00.writeMessage("hello".toPayload())!!
120+
val bob01 = bob00.readMessage(alice01.result)!!
121+
val bob02 = bob01.state<Handshake>()?.writeMessage("hi".toPayload())!!
122+
val alice02 = alice01.state<Handshake>()?.readMessage(bob02.result)!!
123+
val alice03 = alice02.state<Handshake>()?.writeMessage("bye".toPayload())!!
124+
val bob03 = bob02.state<Handshake>()?.readMessage(alice03.result)!!
125+
assert((alice03.value as Transport).handshakeHash == (bob03.value as Transport).handshakeHash)
126+
}
98127
}

0 commit comments

Comments
 (0)