@@ -4,10 +4,10 @@ data class HandshakeState(
4
4
val role : Role ,
5
5
val symmetricState : SymmetricState ,
6
6
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 ,
11
11
val trustedStaticKeys : Set <PublicKey > = emptySet()
12
12
) {
13
13
@@ -34,17 +34,17 @@ data class HandshakeState(
34
34
}
35
35
when {
36
36
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 }
40
40
}
41
41
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 )
48
48
else -> null
49
49
}
50
50
}?.runAndAppendInState { it.encryptAndHash(payload.plainText).map { c -> c.data } }
@@ -53,7 +53,14 @@ data class HandshakeState(
53
53
when {
54
54
state == null -> MessageResult .InsufficientKeyMaterial
55
55
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
+ }
57
64
58
65
else -> MessageResult .IntermediateHandshakeMessage (
59
66
state.current.copy(messagePatterns = rest),
@@ -74,38 +81,40 @@ data class HandshakeState(
74
81
println (" Token $token " )
75
82
when {
76
83
state == null -> null
77
- token == Token .E && state.current.re == null ->
84
+ token == Token .E && state.current.remoteEphemeralKey == null ->
78
85
let {
79
86
val re =
80
87
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
+ )
85
94
)
86
95
)
87
96
)
88
97
println (" E: read $re " )
89
98
val mixed = state.current.symmetricState.mixHash(re.data)
90
99
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())
93
102
)
94
103
}
95
104
96
- token == Token .S && state.current.rs == null -> let {
105
+ token == Token .S && state.current.remoteStaticKey == null -> let {
97
106
println (" S" )
98
- val splitAt = KeyAgreementConfiguration .SIZE .value + 16
107
+ val splitAt = SharedSecret .SIZE .value + 16
99
108
val temp =
100
109
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 )
103
112
println (" Public key $publicKey " )
104
113
println (" Trusting $trustedStaticKeys " )
105
114
println (" Trusted? ${trustedStaticKeys.contains(publicKey)} " )
106
115
if (trustedStaticKeys.contains(publicKey))
107
116
state.copy(
108
- current = state.current.copy(symmetricState = it.current, rs = publicKey),
117
+ current = state.current.copy(symmetricState = it.current, remoteStaticKey = publicKey),
109
118
result = Data (
110
119
state.result.value.drop(splitAt).toByteArray()
111
120
)
@@ -115,26 +124,26 @@ data class HandshakeState(
115
124
}
116
125
117
126
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 )
120
129
}
121
130
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 )
124
133
token == Token .SE && role == Role .INITIATOR -> let {
125
134
println (" SE" )
126
- mixKey(state.current.s , state.current.re )
135
+ mixKey(state.current.localStaticKeyPair , state.current.remoteEphemeralKey )
127
136
}
128
137
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 )
131
140
else -> null
132
141
}
133
142
}?.let {
134
- it.current.symmetricState.decryptAndHash(Ciphertext (it.result.value ))?.let { decrypted ->
143
+ it.current.symmetricState.decryptAndHash(Ciphertext (it.result))?.let { decrypted ->
135
144
State (
136
145
it.current.copy(symmetricState = decrypted.current), Payload (
137
- Data ( decrypted.result.value)
146
+ decrypted.result.data
138
147
)
139
148
)
140
149
}
@@ -144,7 +153,14 @@ data class HandshakeState(
144
153
when {
145
154
state == null -> MessageResult .InsufficientKeyMaterial
146
155
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
+ }
148
164
149
165
else -> MessageResult .IntermediateHandshakeMessage (
150
166
state.current.copy(messagePatterns = rest),
0 commit comments