-
Notifications
You must be signed in to change notification settings - Fork 1
/
HandshakeTest.kt
127 lines (111 loc) · 4.77 KB
/
HandshakeTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package nl.sanderdijkhuis.noise
import nl.sanderdijkhuis.noise.cryptography.AssociatedData
import nl.sanderdijkhuis.noise.cryptography.Plaintext
import nl.sanderdijkhuis.noise.data.Data
import org.junit.jupiter.api.Test
class HandshakeTest {
private fun String.toPayload() = Payload(Data(toByteArray()))
@Test
fun testHandshakeXN() {
val aliceStaticKey = JavaCryptography.generateKeyPair()
val pattern = Handshake.Noise_XN_25519_ChaChaPoly_SHA256
val prologue = Data.empty
val alice00 = Handshake.initialize(
JavaCryptography,
pattern,
Role.INITIATOR,
prologue,
localEphemeralKeyPair = JavaCryptography.generateKeyPair(),
localStaticKeyPair = aliceStaticKey
)!!
val bob00 = Handshake.initialize(
JavaCryptography,
pattern,
Role.RESPONDER,
prologue,
localEphemeralKeyPair = JavaCryptography.generateKeyPair(),
trustedStaticKeys = setOf(aliceStaticKey.first)
)!!
val string01 = "Hello"
val string02 = "Hi"
val string03 = "Bye"
val alice01 = alice00.writeMessage(string01.toPayload())!!
val bob01 = bob00.readMessage(alice01.result)!!
assert(String(bob01.result.data.value) == string01)
val bob02 = bob01.state<Handshake>()?.writeMessage(string02.toPayload())!!
val alice02 = alice01.state<Handshake>()?.readMessage(bob02.result)!!
assert(String(alice02.result.data.value) == string02)
val alice03 = alice02.state<Handshake>()?.writeMessage(string03.toPayload())!!
val bob03 = bob02.state<Handshake>()?.readMessage(alice03.result)!!
assert(String(bob03.result.data.value) == string03)
println(bob03)
}
@Test
fun testHandshakeNK() {
val bobStaticKey = JavaCryptography.generateKeyPair()
val pattern = Handshake.Noise_NK_25519_ChaChaPoly_SHA256
val prologue = Data.empty
val alice00 = Handshake.initialize(
JavaCryptography,
pattern,
Role.INITIATOR,
prologue,
localEphemeralKeyPair = JavaCryptography.generateKeyPair(),
remoteStaticKey = bobStaticKey.first,
)!!
val bob00 = Handshake.initialize(
JavaCryptography,
pattern,
Role.RESPONDER,
prologue,
localEphemeralKeyPair = JavaCryptography.generateKeyPair(),
localStaticKeyPair = bobStaticKey
)!!
val string01 = "Hello"
val string02 = "Hi"
val string03 = "Another message"
val alice01 = alice00.writeMessage(string01.toPayload())!!
val bob01 = bob00.readMessage(alice01.result)!!
assert(String(bob01.result.data.value) == string01)
val bob02 = bob01.state<Handshake>()?.writeMessage(string02.toPayload())!!
val alice02 = alice01.state<Handshake>()?.readMessage(bob02.result)!!
assert(String(alice02.result.data.value) == string02)
val bob03 =
bob02.state<Transport>()!!.responderCipherState.encrypt(
AssociatedData(Data.empty),
Plaintext(string03.toPayload().data)
)
val alice03 =
alice02.state<Transport>()!!.responderCipherState.decrypt(AssociatedData(Data.empty), bob03.result)!!
assert(String(alice03.result.data.value) == string03)
println(alice02)
}
/** https://github.com/sander/noise-kotlin/discussions/15 */
@Test
fun testFinalHandshakeHash() {
val aliceStaticKey = JavaCryptography.generateKeyPair()
val alice00 = Handshake.initialize(
JavaCryptography,
Handshake.Noise_XN_25519_ChaChaPoly_SHA256,
Role.INITIATOR,
Data.empty,
localEphemeralKeyPair = JavaCryptography.generateKeyPair(),
localStaticKeyPair = aliceStaticKey
)!!
val bob00 = Handshake.initialize(
JavaCryptography,
Handshake.Noise_XN_25519_ChaChaPoly_SHA256,
Role.RESPONDER,
Data.empty,
localEphemeralKeyPair = JavaCryptography.generateKeyPair(),
trustedStaticKeys = setOf(aliceStaticKey.first)
)!!
val alice01 = alice00.writeMessage("hello".toPayload())!!
val bob01 = bob00.readMessage(alice01.result)!!
val bob02 = bob01.state<Handshake>()?.writeMessage("hi".toPayload())!!
val alice02 = alice01.state<Handshake>()?.readMessage(bob02.result)!!
val alice03 = alice02.state<Handshake>()?.writeMessage("bye".toPayload())!!
val bob03 = bob02.state<Handshake>()?.readMessage(alice03.result)!!
assert((alice03.value as Transport).handshakeHash == (bob03.value as Transport).handshakeHash)
}
}