-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkx.go
341 lines (303 loc) · 10.6 KB
/
kx.go
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package hydrogen
// #cgo LDFLAGS: -lhydrogen
// #include <stdlib.h>
// #include <hydrogen.h>
import "C"
import (
"unsafe"
)
const (
// n
KxNPacket1Bytes int = C.hydro_kx_N_PACKET1BYTES
// kk
KxKKPacket1Bytes int = C.hydro_kx_KK_PACKET1BYTES
KxKKPacket2Bytes int = C.hydro_kx_KK_PACKET2BYTES
// xx
KxXXPacket1Bytes int = C.hydro_kx_XX_PACKET1BYTES
KxXXPacket2Bytes int = C.hydro_kx_XX_PACKET2BYTES
KxXXPacket3Bytes int = C.hydro_kx_XX_PACKET3BYTES
// keys
KxPublicKeyBytes int = C.hydro_kx_PUBLICKEYBYTES
KxSecretKeyBytes int = C.hydro_kx_SECRETKEYBYTES
KxSessionKeyBytes int = C.hydro_kx_SESSIONKEYBYTES
KxSeedBytes int = C.hydro_kx_SEEDBYTES
KxPskBytes int = C.hydro_kx_PSKBYTES
)
/* -------------------------------- KxKeyPair ------------------------------- */
// Reprentation of hydro_kx_keypair
type KxKeyPair struct {
inner *C.hydro_kx_keypair
}
func (kp *KxKeyPair) Pk() []byte {
return C.GoBytes(unsafe.Pointer(&kp.inner.pk), C.hydro_kx_PUBLICKEYBYTES)
}
func (kp *KxKeyPair) Sk() []byte {
return C.GoBytes(unsafe.Pointer(&kp.inner.sk), C.hydro_kx_SECRETKEYBYTES)
}
/* ---------------------------- KxSessionKeyPair ---------------------------- */
// Reprentation of hydro_kx_session_keypair
type KxSessionKeyPair struct {
inner *C.hydro_kx_session_keypair
}
func (kp *KxSessionKeyPair) Rx() []byte {
return C.GoBytes(unsafe.Pointer(&kp.inner.rx), C.hydro_kx_SESSIONKEYBYTES)
}
func (kp *KxSessionKeyPair) Tx() []byte {
return C.GoBytes(unsafe.Pointer(&kp.inner.tx), C.hydro_kx_SESSIONKEYBYTES)
}
/* --------------------------------- Keygen --------------------------------- */
// void hydro_kx_keygen(hydro_kx_keypair *static_kp);
func KxKeygen() KxKeyPair {
cKxKeypair := new(C.struct_hydro_kx_keypair)
C.hydro_kx_keygen(cKxKeypair)
return KxKeyPair{cKxKeypair}
}
// void hydro_kx_keygen_deterministic(hydro_kx_keypair *static_kp, const uint8_t seed[hydro_kx_SEEDBYTES]);
func KxKeygenDeterministic(seed []byte) KxKeyPair {
CheckSize(seed, KxSeedBytes, "seed")
cKxKeypair := new(C.struct_hydro_kx_keypair)
C.hydro_kx_keygen_deterministic(cKxKeypair, (*C.uint8_t)(&seed[0]))
return KxKeyPair{cKxKeypair}
}
/* ----------------------------------- Kx ----------------------------------- */
// Reprentation of hydro_kx_state
type KxState struct {
inner *C.hydro_kx_state
}
// Create a new KxState object
func NewKxState() KxState {
buf := new(C.hydro_kx_state)
return KxState{buf}
}
/* --------------------------------- Kx (N) --------------------------------- */
// KxN1: Client
// * Computes a key pair using the server's public key
// * Builds a packet packet that has to be sent to the server
// * Returns KxSessionKeyPair, pkt1 slice, 0/-1 (success/error)
// Prototype:
// int hydro_kx_n_1(hydro_kx_session_keypair *kp, uint8_t packet1[hydro_kx_N_PACKET1BYTES], const uint8_t psk[hydro_kx_PSKBYTES], const uint8_t peer_static_pk[hydro_kx_PUBLICKEYBYTES]);
func KxN1(psk []byte, server_pubkey []byte) (KxSessionKeyPair, []byte, int) {
if psk != nil {
CheckSize(psk, KxPskBytes, "psk")
}
CheckSize(server_pubkey, KxPublicKeyBytes, "server_pubkey")
pkt1 := make([]byte, KxNPacket1Bytes)
cSessionKp := new(C.struct_hydro_kx_session_keypair)
var exit int
if psk != nil {
exit = int(C.hydro_kx_n_1(
cSessionKp,
(*C.uint8_t)(&pkt1[0]),
(*C.uint8_t)(&psk[0]),
(*C.uint8_t)(&server_pubkey[0])))
} else {
exit = int(C.hydro_kx_n_1(
cSessionKp,
(*C.uint8_t)(&pkt1[0]),
nil,
(*C.uint8_t)(&server_pubkey[0])))
}
return KxSessionKeyPair{cSessionKp}, pkt1, exit
}
// KxN2: Server
// * Process the initial request from the client (aka packet1)
// * Compute sessions keys
// * Returns KxSessionKeyPair, pkt1 slice, 0/-1 (success/error)
// Prototype:
// int hydro_kx_n_2(hydro_kx_session_keypair *kp, const uint8_t packet1[hydro_kx_N_PACKET1BYTES], const uint8_t psk[hydro_kx_PSKBYTES], const hydro_kx_keypair *static_kp);
func KxN2(pkt1 []byte, psk []byte, server_kp KxKeyPair) (KxSessionKeyPair, int) {
if psk != nil {
CheckSize(psk, KxPskBytes, "psk")
}
CheckSize(pkt1, KxNPacket1Bytes, "n2-pkt1")
cSessionKp := new(C.struct_hydro_kx_session_keypair)
var exit int
if psk != nil {
exit = int(C.hydro_kx_n_2(
cSessionKp,
(*C.uint8_t)(&pkt1[0]),
(*C.uint8_t)(&psk[0]),
server_kp.inner))
} else {
exit = int(C.hydro_kx_n_2(
cSessionKp,
(*C.uint8_t)(&pkt1[0]),
nil,
server_kp.inner))
}
return KxSessionKeyPair{cSessionKp}, exit
}
/* -------------------------------- Kx (KK) --------------------------------- */
// KxKK1: Client -> Server
// * initializes the local state
// * compute ephemeral key pair + pkt1
// * Returns pkt1 slice, 0/-1 (success/error)
// Prototype:
// int hydro_kx_kk_1(hydro_kx_state *state, uint8_t packet1[hydro_kx_KK_PACKET1BYTES], const uint8_t peer_static_pk[hydro_kx_PUBLICKEYBYTES], const hydro_kx_keypair *static_kp);
func KxKK1(st_client KxState, server_pubkey []byte, client_kp KxKeyPair) ([]byte, int) {
CheckSize(server_pubkey, KxPublicKeyBytes, "kk1-server_pubkey")
pkt1 := make([]byte, KxKKPacket1Bytes)
exit := int(C.hydro_kx_kk_1(
st_client.inner,
(*C.uint8_t)(&pkt1[0]),
(*C.uint8_t)(&server_pubkey[0]),
client_kp.inner))
return pkt1, exit
}
// KxKK2: Server -> Client
// * validates the initial request
// * compute ephemeral key pair + pkt2
// * Returns KxSessionKeyPair, pkt2 slice, 0/-1 (success/error)
// Prototype:
// int hydro_kx_kk_2(hydro_kx_session_keypair *kp, uint8_t packet2[hydro_kx_KK_PACKET2BYTES], const uint8_t packet1[hydro_kx_KK_PACKET1BYTES], const uint8_t peer_static_pk[hydro_kx_PUBLICKEYBYTES], const hydro_kx_keypair *static_kp);
func KxKK2(pkt1 []byte, client_pubkey []byte, server_kp KxKeyPair) (KxSessionKeyPair, []byte, int) {
CheckSize(pkt1, KxKKPacket1Bytes, "kk2-pkt1")
CheckSize(client_pubkey, KxPublicKeyBytes, "kk2-client_pubkey")
pkt2 := make([]byte, KxKKPacket2Bytes)
cSessionKp := new(C.struct_hydro_kx_session_keypair)
exit := int(C.hydro_kx_kk_2(
cSessionKp,
(*C.uint8_t)(&pkt2[0]),
(*C.uint8_t)(&pkt1[0]),
(*C.uint8_t)(&client_pubkey[0]),
server_kp.inner))
return KxSessionKeyPair{cSessionKp}, pkt2, exit
}
// KxKK3: Client
// * compute session key pair using server pkt2
// * Returns KxSessionKeyPair, pkt2 slice, 0/-1 (success/error)
// Prototype:
// int hydro_kx_kk_3(hydro_kx_state *state, hydro_kx_session_keypair *kp, const uint8_t packet2[hydro_kx_KK_PACKET2BYTES], const hydro_kx_keypair *static_kp);
func KxKK3(st_client KxState, pkt2 []byte, client_kp KxKeyPair) (KxSessionKeyPair, int) {
CheckSize(pkt2, KxKKPacket1Bytes, "kk3-pkt2 bytes")
cSessionKp := new(C.struct_hydro_kx_session_keypair)
exit := int(C.hydro_kx_kk_3(
st_client.inner,
cSessionKp,
(*C.uint8_t)(&pkt2[0]),
client_kp.inner))
return KxSessionKeyPair{cSessionKp}, exit
}
type KxHelperKK struct {
state KxState
context string
}
/* -------------------------------- Kx (XX) --------------------------------- */
// KxXX1: Client -> Server
// * Returns pkt1 slice, 0/-1 (success/error)
// Prototype:
// int hydro_kx_xx_1(hydro_kx_state *state, uint8_t packet1[hydro_kx_XX_PACKET1BYTES], const uint8_t psk[hydro_kx_PSKBYTES]);
func KxXX1(st_client KxState, psk []byte) ([]byte, int) {
if psk != nil {
CheckSize(psk, KxPskBytes, "psk")
}
pkt1 := make([]byte, KxXXPacket1Bytes)
var exit int
if psk != nil {
exit = int(C.hydro_kx_xx_1(
st_client.inner,
(*C.uint8_t)(&pkt1[0]),
(*C.uint8_t)(&psk[0])))
} else {
exit = int(C.hydro_kx_xx_1(
st_client.inner,
(*C.uint8_t)(&pkt1[0]),
nil))
}
return pkt1, exit
}
// KxXX2: Server -> Client
// * Returns pkt2 slice, 0/-1 (success/error)
// Prototype:
// int hydro_kx_xx_2(hydro_kx_state *state, uint8_t packet2[hydro_kx_XX_PACKET2BYTES], const uint8_t packet1[hydro_kx_XX_PACKET1BYTES], const uint8_t psk[hydro_kx_PSKBYTES], const hydro_kx_keypair *static_kp);
func KxXX2(st_server KxState, pkt1 []byte, server_kp KxKeyPair, psk []byte) ([]byte, int) {
if psk != nil {
CheckSize(psk, KxPskBytes, "psk")
}
CheckSize(pkt1, KxXXPacket1Bytes, "xx2-pkt1")
pkt2 := make([]byte, KxXXPacket2Bytes)
var exit int
if psk != nil {
exit = int(C.hydro_kx_xx_2(
st_server.inner,
(*C.uint8_t)(&pkt2[0]),
(*C.uint8_t)(&pkt1[0]),
(*C.uint8_t)(&psk[0]),
server_kp.inner))
} else {
exit = int(C.hydro_kx_xx_2(
st_server.inner,
(*C.uint8_t)(&pkt2[0]),
(*C.uint8_t)(&pkt1[0]),
nil,
server_kp.inner))
}
return pkt2, exit
}
// KxXX3: Client -> Server
// * Returns client session pair, pkt3 slice, peer publickey, 0/-1 (success/error)
// Prototype:
// int hydro_kx_xx_3(hydro_kx_state *state, hydro_kx_session_keypair *kp, uint8_t packet3[hydro_kx_XX_PACKET3BYTES], uint8_t peer_static_pk[hydro_kx_PUBLICKEYBYTES], const uint8_t packet2[hydro_kx_XX_PACKET2BYTES], const uint8_t psk[hydro_kx_PSKBYTES], const hydro_kx_keypair *static_kp);
func KxXX3(st_client KxState, pkt2 []byte, client_kp KxKeyPair, psk []byte) (KxSessionKeyPair, []byte, []byte, int) {
if psk != nil {
CheckSize(psk, KxPskBytes, "psk")
}
CheckSize(pkt2, KxXXPacket2Bytes, "xx3-pkt2")
cSessionKpClient := new(C.struct_hydro_kx_session_keypair)
pkt3 := make([]byte, KxXXPacket3Bytes)
peer_pk := make([]byte, KxPublicKeyBytes)
var exit int
if psk != nil {
exit = int(C.hydro_kx_xx_3(
st_client.inner,
cSessionKpClient,
(*C.uint8_t)(&pkt3[0]),
(*C.uint8_t)(&peer_pk[0]),
(*C.uint8_t)(&pkt2[0]),
(*C.uint8_t)(&psk[0]),
client_kp.inner))
} else {
exit = int(C.hydro_kx_xx_3(
st_client.inner,
cSessionKpClient,
(*C.uint8_t)(&pkt3[0]),
(*C.uint8_t)(&peer_pk[0]),
(*C.uint8_t)(&pkt2[0]),
nil,
client_kp.inner))
}
return KxSessionKeyPair{cSessionKpClient}, pkt3, peer_pk, exit
}
// KxXX4: Server
// * Returns server session pair, peer publickey, 0/-1 (success/error)
// Prototype:
// int hydro_kx_xx_4(hydro_kx_state *state, hydro_kx_session_keypair *kp, uint8_t peer_static_pk[hydro_kx_PUBLICKEYBYTES], const uint8_t packet3[hydro_kx_XX_PACKET3BYTES], const uint8_t psk[hydro_kx_PSKBYTES]);
func KxXX4(st_server KxState, pkt3 []byte, psk []byte) (KxSessionKeyPair, []byte, int) {
if psk != nil {
CheckSize(psk, KxPskBytes, "xx4-psk")
}
CheckSize(pkt3, KxXXPacket3Bytes, "xx4-pkt3")
cSessionKpServer := new(C.struct_hydro_kx_session_keypair)
peer_pk := make([]byte, KxPublicKeyBytes)
var exit int
if psk != nil {
exit = int(C.hydro_kx_xx_4(
st_server.inner,
cSessionKpServer,
(*C.uint8_t)(&peer_pk[0]),
(*C.uint8_t)(&pkt3[0]),
(*C.uint8_t)(&psk[0])))
} else {
exit = int(C.hydro_kx_xx_4(
st_server.inner,
cSessionKpServer,
(*C.uint8_t)(&peer_pk[0]),
(*C.uint8_t)(&pkt3[0]),
nil))
}
return KxSessionKeyPair{cSessionKpServer}, peer_pk, exit
}
type KxHelperXX struct {
state KxState
context string
}