@@ -4,10 +4,10 @@ data class HandshakeState(
44 val role : Role ,
55 val symmetricState : SymmetricState ,
66 val messagePatterns : List <List <Token >>,
7- val s : KeyPair ? = null ,
8- val e : KeyPair ? = null ,
9- val rs : PublicKey ? = null ,
10- val re : PublicKey ? = null ,
7+ val localStaticKeyPair : KeyPair ? = null ,
8+ val localEphemeralKeyPair : KeyPair ? = null ,
9+ val remoteStaticKey : PublicKey ? = null ,
10+ val remoteEphemeralKey : PublicKey ? = null ,
1111 val trustedStaticKeys : Set <PublicKey > = emptySet()
1212) {
1313
@@ -34,17 +34,17 @@ data class HandshakeState(
3434 }
3535 when {
3636 state == null -> null
37- token == Token .E && e != null -> state.run (e .public.data) { it.mixHash(e .public.data) }
38- token == Token .S && s != null -> state.runAndAppendInState {
39- it.encryptAndHash(s .public.plaintext).map { c -> c.data }
37+ token == Token .E && localEphemeralKeyPair != null -> state.run (localEphemeralKeyPair .public.data) { it.mixHash(localEphemeralKeyPair .public.data) }
38+ token == Token .S && localStaticKeyPair != null -> state.runAndAppendInState {
39+ it.encryptAndHash(localStaticKeyPair .public.plaintext).map { c -> c.data }
4040 }
4141
42- token == Token .EE -> mixKey(e, re )
43- token == Token .ES && role == Role .INITIATOR -> mixKey(e, rs )
44- token == Token .ES && role == Role .RESPONDER -> mixKey(s, re )
45- token == Token .SE && role == Role .INITIATOR -> mixKey(s, re )
46- token == Token .SE && role == Role .RESPONDER -> mixKey(e, rs )
47- token == Token .SS -> mixKey(s, rs )
42+ token == Token .EE -> mixKey(localEphemeralKeyPair, remoteEphemeralKey )
43+ token == Token .ES && role == Role .INITIATOR -> mixKey(localEphemeralKeyPair, remoteStaticKey )
44+ token == Token .ES && role == Role .RESPONDER -> mixKey(localStaticKeyPair, remoteEphemeralKey )
45+ token == Token .SE && role == Role .INITIATOR -> mixKey(localStaticKeyPair, remoteEphemeralKey )
46+ token == Token .SE && role == Role .RESPONDER -> mixKey(localEphemeralKeyPair, remoteStaticKey )
47+ token == Token .SS -> mixKey(localStaticKeyPair, remoteStaticKey )
4848 else -> null
4949 }
5050 }?.runAndAppendInState { it.encryptAndHash(payload.plainText).map { c -> c.data } }
@@ -53,7 +53,14 @@ data class HandshakeState(
5353 when {
5454 state == null -> MessageResult .InsufficientKeyMaterial
5555 rest.isEmpty() -> symmetricState.split()
56- .let { MessageResult .FinalHandshakeMessage (it.first, it.second, symmetricState.digest, state.result) }
56+ .let {
57+ MessageResult .FinalHandshakeMessage (
58+ it.first,
59+ it.second,
60+ symmetricState.handshakeHash,
61+ state.result
62+ )
63+ }
5764
5865 else -> MessageResult .IntermediateHandshakeMessage (
5966 state.current.copy(messagePatterns = rest),
@@ -74,38 +81,40 @@ data class HandshakeState(
7481 println (" Token $token " )
7582 when {
7683 state == null -> null
77- token == Token .E && state.current.re == null ->
84+ token == Token .E && state.current.remoteEphemeralKey == null ->
7885 let {
7986 val re =
8087 PublicKey (
81- state.result.value.sliceArray(
82- IntRange (
83- 0 ,
84- KeyAgreementConfiguration .SIZE .value - 1
88+ Data (
89+ state.result.value.sliceArray(
90+ IntRange (
91+ 0 ,
92+ SharedSecret .SIZE .value - 1
93+ )
8594 )
8695 )
8796 )
8897 println (" E: read $re " )
8998 val mixed = state.current.symmetricState.mixHash(re.data)
9099 state.copy(
91- current = state.current.copy(symmetricState = mixed, re = re),
92- result = Data (state.result.value.drop(KeyAgreementConfiguration .SIZE .value).toByteArray())
100+ current = state.current.copy(symmetricState = mixed, remoteEphemeralKey = re),
101+ result = Data (state.result.value.drop(SharedSecret .SIZE .value).toByteArray())
93102 )
94103 }
95104
96- token == Token .S && state.current.rs == null -> let {
105+ token == Token .S && state.current.remoteStaticKey == null -> let {
97106 println (" S" )
98- val splitAt = KeyAgreementConfiguration .SIZE .value + 16
107+ val splitAt = SharedSecret .SIZE .value + 16
99108 val temp =
100109 state.result.value.sliceArray(IntRange (0 , splitAt - 1 ))
101- state.current.symmetricState.decryptAndHash(Ciphertext (temp))?.let {
102- val publicKey = PublicKey (it.result.value )
110+ state.current.symmetricState.decryptAndHash(Ciphertext (Data ( temp) ))?.let {
111+ val publicKey = PublicKey (it.result.data )
103112 println (" Public key $publicKey " )
104113 println (" Trusting $trustedStaticKeys " )
105114 println (" Trusted? ${trustedStaticKeys.contains(publicKey)} " )
106115 if (trustedStaticKeys.contains(publicKey))
107116 state.copy(
108- current = state.current.copy(symmetricState = it.current, rs = publicKey),
117+ current = state.current.copy(symmetricState = it.current, remoteStaticKey = publicKey),
109118 result = Data (
110119 state.result.value.drop(splitAt).toByteArray()
111120 )
@@ -115,26 +124,26 @@ data class HandshakeState(
115124 }
116125
117126 token == Token .EE -> let {
118- println (" EE: Mixing ${state.current.e } ${state.current.re } " )
119- mixKey(state.current.e , state.current.re )
127+ println (" EE: Mixing ${state.current.localEphemeralKeyPair } ${state.current.remoteEphemeralKey } " )
128+ mixKey(state.current.localEphemeralKeyPair , state.current.remoteEphemeralKey )
120129 }
121130
122- token == Token .ES && role == Role .INITIATOR -> mixKey(state.current.e , state.current.rs )
123- token == Token .ES && role == Role .RESPONDER -> mixKey(state.current.s , state.current.re )
131+ token == Token .ES && role == Role .INITIATOR -> mixKey(state.current.localEphemeralKeyPair , state.current.remoteStaticKey )
132+ token == Token .ES && role == Role .RESPONDER -> mixKey(state.current.localStaticKeyPair , state.current.remoteEphemeralKey )
124133 token == Token .SE && role == Role .INITIATOR -> let {
125134 println (" SE" )
126- mixKey(state.current.s , state.current.re )
135+ mixKey(state.current.localStaticKeyPair , state.current.remoteEphemeralKey )
127136 }
128137
129- token == Token .SE && role == Role .RESPONDER -> mixKey(state.current.e , state.current.rs )
130- token == Token .SS -> mixKey(state.current.s , state.current.rs )
138+ token == Token .SE && role == Role .RESPONDER -> mixKey(state.current.localEphemeralKeyPair , state.current.remoteStaticKey )
139+ token == Token .SS -> mixKey(state.current.localStaticKeyPair , state.current.remoteStaticKey )
131140 else -> null
132141 }
133142 }?.let {
134- it.current.symmetricState.decryptAndHash(Ciphertext (it.result.value ))?.let { decrypted ->
143+ it.current.symmetricState.decryptAndHash(Ciphertext (it.result))?.let { decrypted ->
135144 State (
136145 it.current.copy(symmetricState = decrypted.current), Payload (
137- Data ( decrypted.result.value)
146+ decrypted.result.data
138147 )
139148 )
140149 }
@@ -144,7 +153,14 @@ data class HandshakeState(
144153 when {
145154 state == null -> MessageResult .InsufficientKeyMaterial
146155 rest.isEmpty() -> symmetricState.split()
147- .let { MessageResult .FinalHandshakeMessage (it.first, it.second, symmetricState.digest, state.result) }
156+ .let {
157+ MessageResult .FinalHandshakeMessage (
158+ it.first,
159+ it.second,
160+ symmetricState.handshakeHash,
161+ state.result
162+ )
163+ }
148164
149165 else -> MessageResult .IntermediateHandshakeMessage (
150166 state.current.copy(messagePatterns = rest),
0 commit comments