wirekx 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wirekx/__init__.py +880 -0
- wirekx-0.1.0.dist-info/METADATA +202 -0
- wirekx-0.1.0.dist-info/RECORD +6 -0
- wirekx-0.1.0.dist-info/WHEEL +4 -0
- wirekx-0.1.0.dist-info/licenses/LICENSE +185 -0
- wirekx-0.1.0.dist-info/licenses/NOTICE +7 -0
wirekx/__init__.py
ADDED
|
@@ -0,0 +1,880 @@
|
|
|
1
|
+
"""Experimental wirekx v1 anonymous key exchange library."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import hmac
|
|
7
|
+
import secrets
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from enum import Enum, IntEnum
|
|
10
|
+
from typing import Optional
|
|
11
|
+
|
|
12
|
+
from cryptography.hazmat.primitives import hashes, serialization
|
|
13
|
+
from cryptography.hazmat.primitives.asymmetric import x25519
|
|
14
|
+
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
__version__ = "0.1.0"
|
|
18
|
+
|
|
19
|
+
VERSION = 0x01
|
|
20
|
+
HEADER_LEN = 4
|
|
21
|
+
PUBLIC_KEY_LEN = 32
|
|
22
|
+
NONCE_LEN = 32
|
|
23
|
+
VERIFY_DATA_LEN = 32
|
|
24
|
+
SYMMETRIC_KEY_LEN = 32
|
|
25
|
+
MAX_PAYLOAD_LEN = 0xFFFF
|
|
26
|
+
HELLO_PAYLOAD_LEN = PUBLIC_KEY_LEN + NONCE_LEN
|
|
27
|
+
CONFIRM_PAYLOAD_LEN = VERIFY_DATA_LEN
|
|
28
|
+
|
|
29
|
+
SESSION_INFO = b"wirekx v1 session key"
|
|
30
|
+
INITIATOR_CONFIRM_LABEL = b"wirekx v1 initiator confirm"
|
|
31
|
+
RESPONDER_CONFIRM_LABEL = b"wirekx v1 responder confirm"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class MessageType(IntEnum):
|
|
35
|
+
"""Wire message type codes."""
|
|
36
|
+
|
|
37
|
+
HELLO = 0x01
|
|
38
|
+
HELLO_BACK = 0x02
|
|
39
|
+
CONFIRM = 0x03
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class Role(IntEnum):
|
|
43
|
+
"""Local role in the anonymous handshake."""
|
|
44
|
+
|
|
45
|
+
INITIATOR = 1
|
|
46
|
+
RESPONDER = 2
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class ProtectionLevel(str, Enum):
|
|
50
|
+
"""Peer-authentication strength of a completed handshake."""
|
|
51
|
+
|
|
52
|
+
OPPORTUNISTIC = "opportunistic"
|
|
53
|
+
AUTHENTICATED = "authenticated"
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class WireKXError(Exception):
|
|
57
|
+
"""Base error for handshake failures."""
|
|
58
|
+
|
|
59
|
+
def __init__(self, message: str, *, reason: str | None = None) -> None:
|
|
60
|
+
super().__init__(message)
|
|
61
|
+
self.message = message
|
|
62
|
+
self.reason = reason
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class MalformedMessage(WireKXError):
|
|
66
|
+
"""Raised when bytes cannot be parsed according to the wire format."""
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class UnexpectedMessage(WireKXError):
|
|
70
|
+
"""Raised when a valid message arrives in the wrong handshake state."""
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class VerificationFailed(WireKXError):
|
|
74
|
+
"""Raised when confirm verify_data does not match."""
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@dataclass(frozen=True)
|
|
78
|
+
class Envelope:
|
|
79
|
+
"""Decoded envelope header plus raw payload."""
|
|
80
|
+
|
|
81
|
+
msg_type: MessageType
|
|
82
|
+
payload: bytes
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@dataclass(frozen=True)
|
|
86
|
+
class HelloPayload:
|
|
87
|
+
"""Payload shared by HELLO and HELLO_BACK."""
|
|
88
|
+
|
|
89
|
+
eph_pub: bytes
|
|
90
|
+
nonce: bytes
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@dataclass(frozen=True)
|
|
94
|
+
class ConfirmPayload:
|
|
95
|
+
"""Payload for CONFIRM."""
|
|
96
|
+
|
|
97
|
+
verify_data: bytes
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@dataclass(frozen=True)
|
|
101
|
+
class HandshakeResult:
|
|
102
|
+
"""Returned only after both CONFIRM messages verify."""
|
|
103
|
+
|
|
104
|
+
symmetric_key: bytes
|
|
105
|
+
transcript_hash: bytes
|
|
106
|
+
protection_level: ProtectionLevel = ProtectionLevel.OPPORTUNISTIC
|
|
107
|
+
peer_identity: Optional[bytes] = None
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@dataclass
|
|
111
|
+
class HandshakeState:
|
|
112
|
+
"""
|
|
113
|
+
Mutable per-handshake state.
|
|
114
|
+
|
|
115
|
+
Keep ephemeral private keys in this object only while needed, clear
|
|
116
|
+
all references after deriving the symmetric key.
|
|
117
|
+
"""
|
|
118
|
+
|
|
119
|
+
role: Role
|
|
120
|
+
own_private_key: object | None = None
|
|
121
|
+
own_public_key: bytes | None = None
|
|
122
|
+
peer_public_key: bytes | None = None
|
|
123
|
+
own_nonce: bytes | None = None
|
|
124
|
+
peer_nonce: bytes | None = None
|
|
125
|
+
hello_bytes: bytes | None = None
|
|
126
|
+
hello_back_bytes: bytes | None = None
|
|
127
|
+
symmetric_key: bytes | None = None
|
|
128
|
+
transcript_hash: bytes | None = None
|
|
129
|
+
peer_confirm_verified: bool = False
|
|
130
|
+
own_confirm_sent: bool = False
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def encode_envelope(msg_type: MessageType, payload: bytes) -> bytes:
|
|
134
|
+
"""
|
|
135
|
+
Build VERSION || msg_type || payload_len_be || payload.
|
|
136
|
+
|
|
137
|
+
Pseudocode:
|
|
138
|
+
- Reject payloads longer than MAX_PAYLOAD_LEN.
|
|
139
|
+
- Convert len(payload) to 2 big-endian bytes.
|
|
140
|
+
- Return bytes([VERSION, msg_type]) + length_bytes + payload.
|
|
141
|
+
"""
|
|
142
|
+
|
|
143
|
+
if not isinstance(msg_type, MessageType):
|
|
144
|
+
try:
|
|
145
|
+
msg_type = MessageType(msg_type)
|
|
146
|
+
except ValueError:
|
|
147
|
+
raise MalformedMessage(
|
|
148
|
+
"unknown message type",
|
|
149
|
+
reason=f"message type {msg_type} is not allowed",
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
if len(payload) > MAX_PAYLOAD_LEN:
|
|
153
|
+
raise MalformedMessage(
|
|
154
|
+
"payload too large",
|
|
155
|
+
reason="payload length must fit in a 2-byte unsigned integer",
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
length_bytes = len(payload).to_bytes(2, "big")
|
|
159
|
+
return bytes([VERSION, msg_type]) + length_bytes + payload
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def decode_envelope(message: bytes) -> Envelope:
|
|
164
|
+
"""
|
|
165
|
+
Parse and validate the 4-byte envelope header.
|
|
166
|
+
|
|
167
|
+
Pseudocode:
|
|
168
|
+
- Require at least HEADER_LEN bytes.
|
|
169
|
+
- Read version, msg_type, and 2-byte big-endian payload length.
|
|
170
|
+
- Require version == VERSION.
|
|
171
|
+
- Require msg_type is one of MessageType.
|
|
172
|
+
- Require remaining bytes exactly match payload_len.
|
|
173
|
+
- Return Envelope(msg_type=MessageType(msg_type), payload=payload).
|
|
174
|
+
"""
|
|
175
|
+
|
|
176
|
+
if len(message) < HEADER_LEN:
|
|
177
|
+
raise MalformedMessage(
|
|
178
|
+
"Payload malformed",
|
|
179
|
+
reason="Payload less than minimum length"
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
version = message[0]
|
|
183
|
+
msg_type_raw = message[1]
|
|
184
|
+
payload_len = int.from_bytes(message[2:4], "big")
|
|
185
|
+
payload = message[4:]
|
|
186
|
+
|
|
187
|
+
if version != VERSION:
|
|
188
|
+
raise MalformedMessage(
|
|
189
|
+
"unsupported version",
|
|
190
|
+
reason=f"expected version {VERSION}, got {version}"
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
try:
|
|
194
|
+
msg_type = MessageType(msg_type_raw)
|
|
195
|
+
except ValueError:
|
|
196
|
+
raise MalformedMessage(
|
|
197
|
+
"unknown message type",
|
|
198
|
+
reason=f"message type {msg_type_raw} is not allowed"
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
if len(payload) != payload_len:
|
|
202
|
+
raise MalformedMessage(
|
|
203
|
+
"payload length mismatch",
|
|
204
|
+
reason=f"header says {payload_len} bytes, got {len(payload)} bytes"
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
return Envelope(msg_type=msg_type, payload=payload)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def encode_hello_payload(eph_pub: bytes, nonce: bytes) -> bytes:
|
|
211
|
+
"""
|
|
212
|
+
Build eph_pub || nonce for HELLO or HELLO_BACK.
|
|
213
|
+
|
|
214
|
+
Pseudocode:
|
|
215
|
+
- Require eph_pub is 32 bytes.
|
|
216
|
+
- Require nonce is 32 bytes.
|
|
217
|
+
- Return eph_pub + nonce.
|
|
218
|
+
"""
|
|
219
|
+
|
|
220
|
+
if len(eph_pub) != PUBLIC_KEY_LEN:
|
|
221
|
+
raise MalformedMessage(
|
|
222
|
+
"public key length mismatch",
|
|
223
|
+
reason=f"expected {PUBLIC_KEY_LEN} bytes, got {len(eph_pub)} bytes",
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
if len(nonce) != NONCE_LEN:
|
|
227
|
+
raise MalformedMessage(
|
|
228
|
+
"nonce length mismatch",
|
|
229
|
+
reason=f"expected {NONCE_LEN} bytes, got {len(nonce)} bytes",
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
return eph_pub + nonce
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def decode_hello_payload(payload: bytes) -> HelloPayload:
|
|
236
|
+
"""
|
|
237
|
+
Parse eph_pub || nonce.
|
|
238
|
+
|
|
239
|
+
Pseudocode:
|
|
240
|
+
- Require payload length is PUBLIC_KEY_LEN + NONCE_LEN.
|
|
241
|
+
- Split first 32 bytes as eph_pub.
|
|
242
|
+
- Split next 32 bytes as nonce.
|
|
243
|
+
- Return HelloPayload(eph_pub, nonce).
|
|
244
|
+
"""
|
|
245
|
+
|
|
246
|
+
if len(payload) != HELLO_PAYLOAD_LEN:
|
|
247
|
+
raise MalformedMessage(
|
|
248
|
+
"invalid HELLO payload length",
|
|
249
|
+
reason=f"expected {HELLO_PAYLOAD_LEN} bytes, got {len(payload)} bytes",
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
eph_pub = payload[:PUBLIC_KEY_LEN]
|
|
253
|
+
nonce = payload[PUBLIC_KEY_LEN:]
|
|
254
|
+
|
|
255
|
+
return HelloPayload(eph_pub=eph_pub, nonce=nonce)
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def encode_confirm_payload(verify_data: bytes) -> bytes:
|
|
259
|
+
"""
|
|
260
|
+
Build the 32-byte CONFIRM payload.
|
|
261
|
+
|
|
262
|
+
Pseudocode:
|
|
263
|
+
- Require verify_data is VERIFY_DATA_LEN bytes.
|
|
264
|
+
- Return verify_data unchanged.
|
|
265
|
+
"""
|
|
266
|
+
|
|
267
|
+
if len(verify_data) != VERIFY_DATA_LEN:
|
|
268
|
+
raise MalformedMessage(
|
|
269
|
+
"invalid verify_data length",
|
|
270
|
+
reason=f"expected {VERIFY_DATA_LEN} bytes, got {len(verify_data)} bytes",
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
return verify_data
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def decode_confirm_payload(payload: bytes) -> ConfirmPayload:
|
|
277
|
+
"""
|
|
278
|
+
Parse the 32-byte CONFIRM payload.
|
|
279
|
+
|
|
280
|
+
Pseudocode:
|
|
281
|
+
- Require payload length is VERIFY_DATA_LEN.
|
|
282
|
+
- Return ConfirmPayload(verify_data=payload).
|
|
283
|
+
"""
|
|
284
|
+
|
|
285
|
+
if len(payload) != CONFIRM_PAYLOAD_LEN:
|
|
286
|
+
raise MalformedMessage(
|
|
287
|
+
"invalid CONFIRM payload length",
|
|
288
|
+
reason=f"expected {CONFIRM_PAYLOAD_LEN} bytes, got {len(payload)} bytes",
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
return ConfirmPayload(verify_data=payload)
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def generate_ephemeral_keypair() -> tuple[x25519.X25519PrivateKey, bytes]:
|
|
295
|
+
"""
|
|
296
|
+
Generate a fresh X25519 private key and raw 32-byte public key.
|
|
297
|
+
|
|
298
|
+
Pseudocode:
|
|
299
|
+
- private_key = x25519.X25519PrivateKey.generate()
|
|
300
|
+
- public_key = private_key.public_key()
|
|
301
|
+
- public_bytes = public_key.public_bytes(Encoding.Raw, PublicFormat.Raw)
|
|
302
|
+
- Return private_key, public_bytes.
|
|
303
|
+
"""
|
|
304
|
+
|
|
305
|
+
private_key = x25519.X25519PrivateKey.generate()
|
|
306
|
+
public_bytes = private_key.public_key().public_bytes(
|
|
307
|
+
encoding=serialization.Encoding.Raw,
|
|
308
|
+
format=serialization.PublicFormat.Raw,
|
|
309
|
+
)
|
|
310
|
+
return private_key, public_bytes
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def generate_nonce() -> bytes:
|
|
314
|
+
"""
|
|
315
|
+
Generate 32 random bytes.
|
|
316
|
+
|
|
317
|
+
Pseudocode:
|
|
318
|
+
- Return secrets.token_bytes(NONCE_LEN).
|
|
319
|
+
"""
|
|
320
|
+
|
|
321
|
+
return secrets.token_bytes(NONCE_LEN)
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def load_peer_public_key(raw_public_key: bytes) -> x25519.X25519PublicKey:
|
|
325
|
+
"""
|
|
326
|
+
Convert a raw 32-byte X25519 public key into a cryptography key object.
|
|
327
|
+
|
|
328
|
+
Pseudocode:
|
|
329
|
+
- Require raw_public_key length is PUBLIC_KEY_LEN.
|
|
330
|
+
- Return x25519.X25519PublicKey.from_public_bytes(raw_public_key).
|
|
331
|
+
"""
|
|
332
|
+
|
|
333
|
+
if len(raw_public_key) != PUBLIC_KEY_LEN:
|
|
334
|
+
raise MalformedMessage(
|
|
335
|
+
"invalid public key length",
|
|
336
|
+
reason=f"expected {PUBLIC_KEY_LEN} bytes, got {len(raw_public_key)} bytes",
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
return x25519.X25519PublicKey.from_public_bytes(raw_public_key)
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
def derive_shared_secret(
|
|
343
|
+
own_private_key: x25519.X25519PrivateKey,
|
|
344
|
+
peer_public_key_bytes: bytes,
|
|
345
|
+
) -> bytes:
|
|
346
|
+
"""
|
|
347
|
+
Run X25519(own_eph_priv, peer_eph_pub).
|
|
348
|
+
|
|
349
|
+
Pseudocode:
|
|
350
|
+
- peer_key = load_peer_public_key(peer_public_key_bytes)
|
|
351
|
+
- Return own_private_key.exchange(peer_key).
|
|
352
|
+
"""
|
|
353
|
+
|
|
354
|
+
peer_key = load_peer_public_key(peer_public_key_bytes)
|
|
355
|
+
return own_private_key.exchange(peer_key)
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def derive_symmetric_key(
|
|
359
|
+
shared_secret: bytes,
|
|
360
|
+
nonce_a: bytes,
|
|
361
|
+
nonce_b: bytes,
|
|
362
|
+
) -> bytes:
|
|
363
|
+
"""
|
|
364
|
+
Derive the 32-byte session key with HKDF-SHA256.
|
|
365
|
+
|
|
366
|
+
Pseudocode:
|
|
367
|
+
- salt = nonce_a + nonce_b, always initiator nonce first.
|
|
368
|
+
- Use HKDF(algorithm=SHA256, length=32, salt=salt, info=SESSION_INFO).
|
|
369
|
+
- Return hkdf.derive(shared_secret).
|
|
370
|
+
"""
|
|
371
|
+
|
|
372
|
+
if len(shared_secret) != SYMMETRIC_KEY_LEN:
|
|
373
|
+
raise MalformedMessage(
|
|
374
|
+
"invalid shared secret length",
|
|
375
|
+
reason=f"expected {SYMMETRIC_KEY_LEN} bytes, got {len(shared_secret)} bytes",
|
|
376
|
+
)
|
|
377
|
+
|
|
378
|
+
if len(nonce_a) != NONCE_LEN:
|
|
379
|
+
raise MalformedMessage(
|
|
380
|
+
"invalid initiator nonce length",
|
|
381
|
+
reason=f"expected {NONCE_LEN} bytes, got {len(nonce_a)} bytes",
|
|
382
|
+
)
|
|
383
|
+
|
|
384
|
+
if len(nonce_b) != NONCE_LEN:
|
|
385
|
+
raise MalformedMessage(
|
|
386
|
+
"invalid responder nonce length",
|
|
387
|
+
reason=f"expected {NONCE_LEN} bytes, got {len(nonce_b)} bytes",
|
|
388
|
+
)
|
|
389
|
+
|
|
390
|
+
hkdf = HKDF(
|
|
391
|
+
algorithm=hashes.SHA256(),
|
|
392
|
+
length=SYMMETRIC_KEY_LEN,
|
|
393
|
+
salt=nonce_a + nonce_b,
|
|
394
|
+
info=SESSION_INFO,
|
|
395
|
+
)
|
|
396
|
+
return hkdf.derive(shared_secret)
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def compute_transcript_hash(hello_bytes: bytes, hello_back_bytes: bytes) -> bytes:
|
|
400
|
+
"""
|
|
401
|
+
Compute SHA-256(HELLO_bytes || HELLO_BACK_bytes).
|
|
402
|
+
|
|
403
|
+
Pseudocode:
|
|
404
|
+
- Hash the exact encoded envelope bytes sent on the wire.
|
|
405
|
+
- Return 32-byte digest.
|
|
406
|
+
"""
|
|
407
|
+
|
|
408
|
+
return hashlib.sha256(hello_bytes + hello_back_bytes).digest()
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
def confirm_label_for(role: Role) -> bytes:
|
|
412
|
+
"""
|
|
413
|
+
Select the HMAC label for the sender of a CONFIRM message.
|
|
414
|
+
|
|
415
|
+
Pseudocode:
|
|
416
|
+
- Role.INITIATOR -> INITIATOR_CONFIRM_LABEL.
|
|
417
|
+
- Role.RESPONDER -> RESPONDER_CONFIRM_LABEL.
|
|
418
|
+
"""
|
|
419
|
+
|
|
420
|
+
if role == Role.INITIATOR:
|
|
421
|
+
return INITIATOR_CONFIRM_LABEL
|
|
422
|
+
if role == Role.RESPONDER:
|
|
423
|
+
return RESPONDER_CONFIRM_LABEL
|
|
424
|
+
|
|
425
|
+
raise UnexpectedMessage(
|
|
426
|
+
"unknown role",
|
|
427
|
+
reason=f"role {role} is not allowed",
|
|
428
|
+
)
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
def compute_verify_data(
|
|
432
|
+
symmetric_key: bytes,
|
|
433
|
+
sender_role: Role,
|
|
434
|
+
transcript_hash: bytes,
|
|
435
|
+
) -> bytes:
|
|
436
|
+
"""
|
|
437
|
+
Compute HMAC-SHA256(key=symmetric_key, data=label || transcript_hash).
|
|
438
|
+
|
|
439
|
+
Pseudocode:
|
|
440
|
+
- label = confirm_label_for(sender_role)
|
|
441
|
+
- data = label + transcript_hash
|
|
442
|
+
- Return HMAC-SHA256(symmetric_key, data).
|
|
443
|
+
"""
|
|
444
|
+
|
|
445
|
+
if len(symmetric_key) != SYMMETRIC_KEY_LEN:
|
|
446
|
+
raise MalformedMessage(
|
|
447
|
+
"invalid symmetric key length",
|
|
448
|
+
reason=f"expected {SYMMETRIC_KEY_LEN} bytes, got {len(symmetric_key)} bytes",
|
|
449
|
+
)
|
|
450
|
+
|
|
451
|
+
if len(transcript_hash) != SYMMETRIC_KEY_LEN:
|
|
452
|
+
raise MalformedMessage(
|
|
453
|
+
"invalid transcript hash length",
|
|
454
|
+
reason=f"expected {SYMMETRIC_KEY_LEN} bytes, got {len(transcript_hash)} bytes",
|
|
455
|
+
)
|
|
456
|
+
|
|
457
|
+
label = confirm_label_for(sender_role)
|
|
458
|
+
return hmac.new(
|
|
459
|
+
symmetric_key,
|
|
460
|
+
label + transcript_hash,
|
|
461
|
+
hashlib.sha256,
|
|
462
|
+
).digest()
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
def verify_confirm_data(
|
|
466
|
+
symmetric_key: bytes,
|
|
467
|
+
sender_role: Role,
|
|
468
|
+
transcript_hash: bytes,
|
|
469
|
+
received_verify_data: bytes,
|
|
470
|
+
) -> None:
|
|
471
|
+
"""
|
|
472
|
+
Validate received verify_data with constant-time comparison.
|
|
473
|
+
|
|
474
|
+
Pseudocode:
|
|
475
|
+
- expected = compute_verify_data(symmetric_key, sender_role, transcript_hash)
|
|
476
|
+
- Use hmac.compare_digest(expected, received_verify_data).
|
|
477
|
+
- If comparison fails, raise VerificationFailed.
|
|
478
|
+
"""
|
|
479
|
+
|
|
480
|
+
expected = compute_verify_data(
|
|
481
|
+
symmetric_key=symmetric_key,
|
|
482
|
+
sender_role=sender_role,
|
|
483
|
+
transcript_hash=transcript_hash,
|
|
484
|
+
)
|
|
485
|
+
|
|
486
|
+
if not hmac.compare_digest(expected, received_verify_data):
|
|
487
|
+
raise VerificationFailed(
|
|
488
|
+
"confirm verification failed",
|
|
489
|
+
reason="received verify_data does not match expected HMAC",
|
|
490
|
+
)
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
def clear_sensitive_state(state: HandshakeState) -> None:
|
|
494
|
+
"""
|
|
495
|
+
Drop references to ephemeral private key material after use or on abort.
|
|
496
|
+
|
|
497
|
+
Pseudocode:
|
|
498
|
+
- Set state.own_private_key = None.
|
|
499
|
+
- Optionally clear symmetric_key if aborting before success.
|
|
500
|
+
- Note: immutable Python bytes cannot be reliably zeroized in place.
|
|
501
|
+
"""
|
|
502
|
+
|
|
503
|
+
state.own_private_key = None
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
class InitiatorHandshake:
|
|
507
|
+
"""
|
|
508
|
+
Initiator-side state machine for anonymous wirekx v1.
|
|
509
|
+
|
|
510
|
+
Expected call order:
|
|
511
|
+
1. create_hello()
|
|
512
|
+
2. receive_hello_back(hello_back_bytes)
|
|
513
|
+
3. create_confirm()
|
|
514
|
+
4. receive_confirm(confirm_bytes)
|
|
515
|
+
5. result()
|
|
516
|
+
"""
|
|
517
|
+
|
|
518
|
+
def __init__(self) -> None:
|
|
519
|
+
"""
|
|
520
|
+
Pseudocode:
|
|
521
|
+
- Create HandshakeState(role=Role.INITIATOR).
|
|
522
|
+
- Do not generate keys until create_hello(), unless you prefer eager init.
|
|
523
|
+
"""
|
|
524
|
+
|
|
525
|
+
self.state = HandshakeState(role=Role.INITIATOR)
|
|
526
|
+
|
|
527
|
+
def create_hello(self) -> bytes:
|
|
528
|
+
"""
|
|
529
|
+
Build the first message.
|
|
530
|
+
|
|
531
|
+
Pseudocode:
|
|
532
|
+
- Generate fresh X25519 keypair.
|
|
533
|
+
- Generate nonce_a.
|
|
534
|
+
- payload = encode_hello_payload(eph_pub_a, nonce_a).
|
|
535
|
+
- hello_bytes = encode_envelope(MessageType.HELLO, payload).
|
|
536
|
+
- Store own key, own public key, nonce_a, and hello_bytes.
|
|
537
|
+
- Return hello_bytes.
|
|
538
|
+
"""
|
|
539
|
+
|
|
540
|
+
own_private_key, own_public_key = generate_ephemeral_keypair()
|
|
541
|
+
own_nonce = generate_nonce()
|
|
542
|
+
payload = encode_hello_payload(own_public_key, own_nonce)
|
|
543
|
+
hello_bytes = encode_envelope(MessageType.HELLO, payload)
|
|
544
|
+
|
|
545
|
+
self.state.own_private_key = own_private_key
|
|
546
|
+
self.state.own_public_key = own_public_key
|
|
547
|
+
self.state.own_nonce = own_nonce
|
|
548
|
+
self.state.hello_bytes = hello_bytes
|
|
549
|
+
|
|
550
|
+
return hello_bytes
|
|
551
|
+
|
|
552
|
+
def receive_hello_back(self, message: bytes) -> None:
|
|
553
|
+
"""
|
|
554
|
+
Process responder HELLO_BACK and derive key material.
|
|
555
|
+
|
|
556
|
+
Pseudocode:
|
|
557
|
+
- Decode envelope and require MessageType.HELLO_BACK.
|
|
558
|
+
- Parse eph_pub_b and nonce_b.
|
|
559
|
+
- Store exact hello_back bytes for transcript.
|
|
560
|
+
- shared_secret = derive_shared_secret(own_private_key, eph_pub_b).
|
|
561
|
+
- symmetric_key = derive_symmetric_key(shared_secret, nonce_a, nonce_b).
|
|
562
|
+
- transcript_hash = compute_transcript_hash(hello_bytes, hello_back_bytes).
|
|
563
|
+
- Clear own_private_key reference after deriving.
|
|
564
|
+
"""
|
|
565
|
+
|
|
566
|
+
if self.state.own_private_key is None:
|
|
567
|
+
raise UnexpectedMessage(
|
|
568
|
+
"HELLO has not been created",
|
|
569
|
+
reason="call create_hello() before receive_hello_back()",
|
|
570
|
+
)
|
|
571
|
+
if self.state.own_nonce is None or self.state.hello_bytes is None:
|
|
572
|
+
raise UnexpectedMessage(
|
|
573
|
+
"initiator state is incomplete",
|
|
574
|
+
reason="missing nonce or HELLO transcript bytes",
|
|
575
|
+
)
|
|
576
|
+
|
|
577
|
+
envelope = decode_envelope(message)
|
|
578
|
+
if envelope.msg_type != MessageType.HELLO_BACK:
|
|
579
|
+
raise UnexpectedMessage(
|
|
580
|
+
"unexpected message type",
|
|
581
|
+
reason=f"expected HELLO_BACK, got {envelope.msg_type.name}",
|
|
582
|
+
)
|
|
583
|
+
|
|
584
|
+
hello_back = decode_hello_payload(envelope.payload)
|
|
585
|
+
shared_secret = derive_shared_secret(
|
|
586
|
+
self.state.own_private_key,
|
|
587
|
+
hello_back.eph_pub,
|
|
588
|
+
)
|
|
589
|
+
symmetric_key = derive_symmetric_key(
|
|
590
|
+
shared_secret,
|
|
591
|
+
self.state.own_nonce,
|
|
592
|
+
hello_back.nonce,
|
|
593
|
+
)
|
|
594
|
+
transcript_hash = compute_transcript_hash(self.state.hello_bytes, message)
|
|
595
|
+
|
|
596
|
+
self.state.peer_public_key = hello_back.eph_pub
|
|
597
|
+
self.state.peer_nonce = hello_back.nonce
|
|
598
|
+
self.state.hello_back_bytes = message
|
|
599
|
+
self.state.symmetric_key = symmetric_key
|
|
600
|
+
self.state.transcript_hash = transcript_hash
|
|
601
|
+
clear_sensitive_state(self.state)
|
|
602
|
+
|
|
603
|
+
def create_confirm(self) -> bytes:
|
|
604
|
+
"""
|
|
605
|
+
Build initiator CONFIRM.
|
|
606
|
+
|
|
607
|
+
Pseudocode:
|
|
608
|
+
- Require symmetric_key and transcript_hash exist.
|
|
609
|
+
- verify_data = compute_verify_data(key, Role.INITIATOR, transcript_hash).
|
|
610
|
+
- payload = encode_confirm_payload(verify_data).
|
|
611
|
+
- confirm_bytes = encode_envelope(MessageType.CONFIRM, payload).
|
|
612
|
+
- Mark own_confirm_sent.
|
|
613
|
+
- Return confirm_bytes.
|
|
614
|
+
"""
|
|
615
|
+
|
|
616
|
+
if self.state.symmetric_key is None or self.state.transcript_hash is None:
|
|
617
|
+
raise UnexpectedMessage(
|
|
618
|
+
"key material has not been derived",
|
|
619
|
+
reason="call receive_hello_back() before create_confirm()",
|
|
620
|
+
)
|
|
621
|
+
|
|
622
|
+
verify_data = compute_verify_data(
|
|
623
|
+
self.state.symmetric_key,
|
|
624
|
+
Role.INITIATOR,
|
|
625
|
+
self.state.transcript_hash,
|
|
626
|
+
)
|
|
627
|
+
payload = encode_confirm_payload(verify_data)
|
|
628
|
+
confirm_bytes = encode_envelope(MessageType.CONFIRM, payload)
|
|
629
|
+
self.state.own_confirm_sent = True
|
|
630
|
+
return confirm_bytes
|
|
631
|
+
|
|
632
|
+
def receive_confirm(self, message: bytes) -> None:
|
|
633
|
+
"""
|
|
634
|
+
Verify responder CONFIRM.
|
|
635
|
+
|
|
636
|
+
Pseudocode:
|
|
637
|
+
- Decode envelope and require MessageType.CONFIRM.
|
|
638
|
+
- Parse verify_data.
|
|
639
|
+
- verify_confirm_data(key, Role.RESPONDER, transcript_hash, verify_data).
|
|
640
|
+
- Mark peer_confirm_verified.
|
|
641
|
+
"""
|
|
642
|
+
|
|
643
|
+
if self.state.symmetric_key is None or self.state.transcript_hash is None:
|
|
644
|
+
raise UnexpectedMessage(
|
|
645
|
+
"key material has not been derived",
|
|
646
|
+
reason="call receive_hello_back() before receive_confirm()",
|
|
647
|
+
)
|
|
648
|
+
|
|
649
|
+
envelope = decode_envelope(message)
|
|
650
|
+
if envelope.msg_type != MessageType.CONFIRM:
|
|
651
|
+
raise UnexpectedMessage(
|
|
652
|
+
"unexpected message type",
|
|
653
|
+
reason=f"expected CONFIRM, got {envelope.msg_type.name}",
|
|
654
|
+
)
|
|
655
|
+
|
|
656
|
+
confirm = decode_confirm_payload(envelope.payload)
|
|
657
|
+
verify_confirm_data(
|
|
658
|
+
self.state.symmetric_key,
|
|
659
|
+
Role.RESPONDER,
|
|
660
|
+
self.state.transcript_hash,
|
|
661
|
+
confirm.verify_data,
|
|
662
|
+
)
|
|
663
|
+
self.state.peer_confirm_verified = True
|
|
664
|
+
|
|
665
|
+
def result(self) -> HandshakeResult:
|
|
666
|
+
"""
|
|
667
|
+
Return final output only after both CONFIRM messages are complete.
|
|
668
|
+
|
|
669
|
+
Pseudocode:
|
|
670
|
+
- Require own_confirm_sent and peer_confirm_verified.
|
|
671
|
+
- Require symmetric_key and transcript_hash exist.
|
|
672
|
+
- Return HandshakeResult(symmetric_key, transcript_hash).
|
|
673
|
+
"""
|
|
674
|
+
|
|
675
|
+
if not self.state.own_confirm_sent or not self.state.peer_confirm_verified:
|
|
676
|
+
raise UnexpectedMessage(
|
|
677
|
+
"handshake is not complete",
|
|
678
|
+
reason="both local and peer CONFIRM messages must complete first",
|
|
679
|
+
)
|
|
680
|
+
if self.state.symmetric_key is None or self.state.transcript_hash is None:
|
|
681
|
+
raise UnexpectedMessage(
|
|
682
|
+
"handshake result is unavailable",
|
|
683
|
+
reason="missing symmetric key or transcript hash",
|
|
684
|
+
)
|
|
685
|
+
|
|
686
|
+
return HandshakeResult(
|
|
687
|
+
symmetric_key=self.state.symmetric_key,
|
|
688
|
+
transcript_hash=self.state.transcript_hash,
|
|
689
|
+
)
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
class ResponderHandshake:
|
|
693
|
+
"""
|
|
694
|
+
Responder-side state machine for anonymous wirekx v1.
|
|
695
|
+
|
|
696
|
+
Expected call order:
|
|
697
|
+
1. receive_hello(hello_bytes)
|
|
698
|
+
2. create_hello_back()
|
|
699
|
+
3. receive_confirm(confirm_bytes)
|
|
700
|
+
4. create_confirm()
|
|
701
|
+
5. result()
|
|
702
|
+
"""
|
|
703
|
+
|
|
704
|
+
def __init__(self) -> None:
|
|
705
|
+
"""
|
|
706
|
+
Pseudocode:
|
|
707
|
+
- Create HandshakeState(role=Role.RESPONDER).
|
|
708
|
+
- Do not generate keys until receive_hello() or create_hello_back().
|
|
709
|
+
"""
|
|
710
|
+
|
|
711
|
+
self.state = HandshakeState(role=Role.RESPONDER)
|
|
712
|
+
|
|
713
|
+
def receive_hello(self, message: bytes) -> None:
|
|
714
|
+
"""
|
|
715
|
+
Process initiator HELLO.
|
|
716
|
+
|
|
717
|
+
Pseudocode:
|
|
718
|
+
- Decode envelope and require MessageType.HELLO.
|
|
719
|
+
- Parse eph_pub_a and nonce_a.
|
|
720
|
+
- Store exact hello bytes for transcript.
|
|
721
|
+
- Store peer public key and peer nonce.
|
|
722
|
+
"""
|
|
723
|
+
|
|
724
|
+
envelope = decode_envelope(message)
|
|
725
|
+
if envelope.msg_type != MessageType.HELLO:
|
|
726
|
+
raise UnexpectedMessage(
|
|
727
|
+
"unexpected message type",
|
|
728
|
+
reason=f"expected HELLO, got {envelope.msg_type.name}",
|
|
729
|
+
)
|
|
730
|
+
|
|
731
|
+
hello = decode_hello_payload(envelope.payload)
|
|
732
|
+
self.state.peer_public_key = hello.eph_pub
|
|
733
|
+
self.state.peer_nonce = hello.nonce
|
|
734
|
+
self.state.hello_bytes = message
|
|
735
|
+
|
|
736
|
+
def create_hello_back(self) -> bytes:
|
|
737
|
+
"""
|
|
738
|
+
Build responder HELLO_BACK and derive key material.
|
|
739
|
+
|
|
740
|
+
Pseudocode:
|
|
741
|
+
- Require initiator HELLO has been received.
|
|
742
|
+
- Generate fresh X25519 keypair.
|
|
743
|
+
- Generate nonce_b.
|
|
744
|
+
- payload = encode_hello_payload(eph_pub_b, nonce_b).
|
|
745
|
+
- hello_back_bytes = encode_envelope(MessageType.HELLO_BACK, payload).
|
|
746
|
+
- shared_secret = derive_shared_secret(own_private_key, eph_pub_a).
|
|
747
|
+
- symmetric_key = derive_symmetric_key(shared_secret, nonce_a, nonce_b).
|
|
748
|
+
- transcript_hash = compute_transcript_hash(hello_bytes, hello_back_bytes).
|
|
749
|
+
- Clear own_private_key reference after deriving.
|
|
750
|
+
- Return hello_back_bytes.
|
|
751
|
+
"""
|
|
752
|
+
|
|
753
|
+
if self.state.peer_public_key is None:
|
|
754
|
+
raise UnexpectedMessage(
|
|
755
|
+
"HELLO has not been received",
|
|
756
|
+
reason="call receive_hello() before create_hello_back()",
|
|
757
|
+
)
|
|
758
|
+
if self.state.peer_nonce is None or self.state.hello_bytes is None:
|
|
759
|
+
raise UnexpectedMessage(
|
|
760
|
+
"responder state is incomplete",
|
|
761
|
+
reason="missing peer nonce or HELLO transcript bytes",
|
|
762
|
+
)
|
|
763
|
+
|
|
764
|
+
own_private_key, own_public_key = generate_ephemeral_keypair()
|
|
765
|
+
own_nonce = generate_nonce()
|
|
766
|
+
payload = encode_hello_payload(own_public_key, own_nonce)
|
|
767
|
+
hello_back_bytes = encode_envelope(MessageType.HELLO_BACK, payload)
|
|
768
|
+
shared_secret = derive_shared_secret(own_private_key, self.state.peer_public_key)
|
|
769
|
+
symmetric_key = derive_symmetric_key(
|
|
770
|
+
shared_secret,
|
|
771
|
+
self.state.peer_nonce,
|
|
772
|
+
own_nonce,
|
|
773
|
+
)
|
|
774
|
+
transcript_hash = compute_transcript_hash(
|
|
775
|
+
self.state.hello_bytes,
|
|
776
|
+
hello_back_bytes,
|
|
777
|
+
)
|
|
778
|
+
|
|
779
|
+
self.state.own_private_key = own_private_key
|
|
780
|
+
self.state.own_public_key = own_public_key
|
|
781
|
+
self.state.own_nonce = own_nonce
|
|
782
|
+
self.state.hello_back_bytes = hello_back_bytes
|
|
783
|
+
self.state.symmetric_key = symmetric_key
|
|
784
|
+
self.state.transcript_hash = transcript_hash
|
|
785
|
+
clear_sensitive_state(self.state)
|
|
786
|
+
|
|
787
|
+
return hello_back_bytes
|
|
788
|
+
|
|
789
|
+
def receive_confirm(self, message: bytes) -> None:
|
|
790
|
+
"""
|
|
791
|
+
Verify initiator CONFIRM.
|
|
792
|
+
|
|
793
|
+
Pseudocode:
|
|
794
|
+
- Decode envelope and require MessageType.CONFIRM.
|
|
795
|
+
- Parse verify_data.
|
|
796
|
+
- verify_confirm_data(key, Role.INITIATOR, transcript_hash, verify_data).
|
|
797
|
+
- Mark peer_confirm_verified.
|
|
798
|
+
"""
|
|
799
|
+
|
|
800
|
+
if self.state.symmetric_key is None or self.state.transcript_hash is None:
|
|
801
|
+
raise UnexpectedMessage(
|
|
802
|
+
"key material has not been derived",
|
|
803
|
+
reason="call create_hello_back() before receive_confirm()",
|
|
804
|
+
)
|
|
805
|
+
|
|
806
|
+
envelope = decode_envelope(message)
|
|
807
|
+
if envelope.msg_type != MessageType.CONFIRM:
|
|
808
|
+
raise UnexpectedMessage(
|
|
809
|
+
"unexpected message type",
|
|
810
|
+
reason=f"expected CONFIRM, got {envelope.msg_type.name}",
|
|
811
|
+
)
|
|
812
|
+
|
|
813
|
+
confirm = decode_confirm_payload(envelope.payload)
|
|
814
|
+
verify_confirm_data(
|
|
815
|
+
self.state.symmetric_key,
|
|
816
|
+
Role.INITIATOR,
|
|
817
|
+
self.state.transcript_hash,
|
|
818
|
+
confirm.verify_data,
|
|
819
|
+
)
|
|
820
|
+
self.state.peer_confirm_verified = True
|
|
821
|
+
|
|
822
|
+
def create_confirm(self) -> bytes:
|
|
823
|
+
"""
|
|
824
|
+
Build responder CONFIRM.
|
|
825
|
+
|
|
826
|
+
Pseudocode:
|
|
827
|
+
- Require initiator CONFIRM has verified.
|
|
828
|
+
- verify_data = compute_verify_data(key, Role.RESPONDER, transcript_hash).
|
|
829
|
+
- payload = encode_confirm_payload(verify_data).
|
|
830
|
+
- confirm_bytes = encode_envelope(MessageType.CONFIRM, payload).
|
|
831
|
+
- Mark own_confirm_sent.
|
|
832
|
+
- Return confirm_bytes.
|
|
833
|
+
"""
|
|
834
|
+
|
|
835
|
+
if not self.state.peer_confirm_verified:
|
|
836
|
+
raise UnexpectedMessage(
|
|
837
|
+
"peer CONFIRM has not been verified",
|
|
838
|
+
reason="call receive_confirm() before create_confirm()",
|
|
839
|
+
)
|
|
840
|
+
if self.state.symmetric_key is None or self.state.transcript_hash is None:
|
|
841
|
+
raise UnexpectedMessage(
|
|
842
|
+
"key material has not been derived",
|
|
843
|
+
reason="call create_hello_back() before create_confirm()",
|
|
844
|
+
)
|
|
845
|
+
|
|
846
|
+
verify_data = compute_verify_data(
|
|
847
|
+
self.state.symmetric_key,
|
|
848
|
+
Role.RESPONDER,
|
|
849
|
+
self.state.transcript_hash,
|
|
850
|
+
)
|
|
851
|
+
payload = encode_confirm_payload(verify_data)
|
|
852
|
+
confirm_bytes = encode_envelope(MessageType.CONFIRM, payload)
|
|
853
|
+
self.state.own_confirm_sent = True
|
|
854
|
+
return confirm_bytes
|
|
855
|
+
|
|
856
|
+
def result(self) -> HandshakeResult:
|
|
857
|
+
"""
|
|
858
|
+
Return final output only after both CONFIRM messages are complete.
|
|
859
|
+
|
|
860
|
+
Pseudocode:
|
|
861
|
+
- Require own_confirm_sent and peer_confirm_verified.
|
|
862
|
+
- Require symmetric_key and transcript_hash exist.
|
|
863
|
+
- Return HandshakeResult(symmetric_key, transcript_hash).
|
|
864
|
+
"""
|
|
865
|
+
|
|
866
|
+
if not self.state.own_confirm_sent or not self.state.peer_confirm_verified:
|
|
867
|
+
raise UnexpectedMessage(
|
|
868
|
+
"handshake is not complete",
|
|
869
|
+
reason="both local and peer CONFIRM messages must complete first",
|
|
870
|
+
)
|
|
871
|
+
if self.state.symmetric_key is None or self.state.transcript_hash is None:
|
|
872
|
+
raise UnexpectedMessage(
|
|
873
|
+
"handshake result is unavailable",
|
|
874
|
+
reason="missing symmetric key or transcript hash",
|
|
875
|
+
)
|
|
876
|
+
|
|
877
|
+
return HandshakeResult(
|
|
878
|
+
symmetric_key=self.state.symmetric_key,
|
|
879
|
+
transcript_hash=self.state.transcript_hash,
|
|
880
|
+
)
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: wirekx
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Experimental anonymous X25519 key exchange wire format.
|
|
5
|
+
Project-URL: Homepage, https://github.com/wirekx/wirekx
|
|
6
|
+
Project-URL: Repository, https://github.com/wirekx/wirekx
|
|
7
|
+
Project-URL: Issues, https://github.com/wirekx/wirekx/issues
|
|
8
|
+
Author: wirekx contributors
|
|
9
|
+
License: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
License-File: NOTICE
|
|
12
|
+
Keywords: cryptography,experimental,key-exchange,x25519
|
|
13
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Security :: Cryptography
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Requires-Dist: cryptography>=49.0.0
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: pytest>=8.0.0; extra == 'dev'
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
```text
|
|
27
|
+
* * __ __ ___ ____ _____ _ __ __ __
|
|
28
|
+
\ / \ \ / / |_ _|| _ \ | ____|| |/ / \ \/ /
|
|
29
|
+
^/ | \^ \ \ /\ / / | | | |_) || _| | ' / \ /
|
|
30
|
+
| o:1 | \ V V / | | | _ < | |___ | . \ / \
|
|
31
|
+
-| 1:o |- \_/\_/ |___||_| \_\|_____||_|\_\ /_/\_\
|
|
32
|
+
/ \_|_/ \
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Python library and wire format for anonymous X25519 key exchange.
|
|
36
|
+
|
|
37
|
+
> **wirekx v1 performs the following:**
|
|
38
|
+
>
|
|
39
|
+
> - Opportunistic key agreement for anonymous handshake
|
|
40
|
+
>
|
|
41
|
+
> **V1 caveat:** Since this is an anonymous handshake, MITM is undetectable.
|
|
42
|
+
>
|
|
43
|
+
> **Use cases:**
|
|
44
|
+
>
|
|
45
|
+
> - Derive symmetric key for payload encryption between trusted services
|
|
46
|
+
>
|
|
47
|
+
> **Future releases:**
|
|
48
|
+
>
|
|
49
|
+
> - publicCA validation for anonymous handshake
|
|
50
|
+
> - Pre-shared fingerprint validation
|
|
51
|
+
> - Quantum-safe encryption
|
|
52
|
+
|
|
53
|
+
## Install for development
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
python3 -m venv .venv
|
|
57
|
+
.venv/bin/python -m pip install -e ".[dev]"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Run the manual example:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
.venv/bin/python testrun.py
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Run tests:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
.venv/bin/python -m pytest
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Use as a library:
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from wirekx import InitiatorHandshake, ResponderHandshake
|
|
76
|
+
|
|
77
|
+
initiator = InitiatorHandshake()
|
|
78
|
+
responder = ResponderHandshake()
|
|
79
|
+
|
|
80
|
+
hello = initiator.create_hello()
|
|
81
|
+
responder.receive_hello(hello)
|
|
82
|
+
|
|
83
|
+
hello_back = responder.create_hello_back()
|
|
84
|
+
initiator.receive_hello_back(hello_back)
|
|
85
|
+
|
|
86
|
+
initiator_confirm = initiator.create_confirm()
|
|
87
|
+
responder.receive_confirm(initiator_confirm)
|
|
88
|
+
|
|
89
|
+
responder_confirm = responder.create_confirm()
|
|
90
|
+
initiator.receive_confirm(responder_confirm)
|
|
91
|
+
|
|
92
|
+
result = initiator.result()
|
|
93
|
+
print(result.symmetric_key.hex())
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Open source contribution flow
|
|
97
|
+
|
|
98
|
+
1. Fork the repository on GitHub.
|
|
99
|
+
2. Create a branch for your change.
|
|
100
|
+
3. Install development dependencies with `.venv/bin/python -m pip install -e ".[dev]"`.
|
|
101
|
+
4. Add or update tests in `tests/`.
|
|
102
|
+
5. Run `.venv/bin/python -m pytest`.
|
|
103
|
+
6. Open a pull request.
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
wirekx is licensed under the Apache License, Version 2.0. See `LICENSE`.
|
|
108
|
+
|
|
109
|
+
Redistributions should preserve the attribution notices in `NOTICE`. If you use
|
|
110
|
+
wirekx in a public project, a README or documentation mention is appreciated.
|
|
111
|
+
|
|
112
|
+
## wirekx wire format (v1, anonymous mode)
|
|
113
|
+
|
|
114
|
+
Two parties run a handshake and end up holding the same 32-byte symmetric key.
|
|
115
|
+
This document specifies the bytes that go on the wire.
|
|
116
|
+
|
|
117
|
+
## Envelope
|
|
118
|
+
|
|
119
|
+
Every message starts with a 4-byte header followed by its payload.
|
|
120
|
+
|
|
121
|
+
```
|
|
122
|
+
┌─────────┬──────────┬─────────────┬──────────────┐
|
|
123
|
+
│ version │ msg_type │ payload_len │ payload │
|
|
124
|
+
│ 1 byte │ 1 byte │ 2 bytes BE │ payload_len │
|
|
125
|
+
└─────────┴──────────┴─────────────┴──────────────┘
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
`version` = `0x01`. Multi-byte integers are big-endian.
|
|
129
|
+
|
|
130
|
+
## Messages
|
|
131
|
+
|
|
132
|
+
| Code | Name | From | Payload |
|
|
133
|
+
|--------|--------------|-----------|-----------------------------------|
|
|
134
|
+
| `0x01` | `HELLO` | initiator | `eph_pub_a` (32) + `nonce_a` (32) |
|
|
135
|
+
| `0x02` | `HELLO_BACK` | responder | `eph_pub_b` (32) + `nonce_b` (32) |
|
|
136
|
+
| `0x03` | `CONFIRM` | both | `verify_data` (32) |
|
|
137
|
+
|
|
138
|
+
`eph_pub_*` is an X25519 public key. `nonce_*` is 32 random bytes.
|
|
139
|
+
|
|
140
|
+
## Cryptography
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
shared_secret = X25519(own_eph_priv, peer_eph_pub)
|
|
144
|
+
|
|
145
|
+
symmetric_key = HKDF-SHA256(
|
|
146
|
+
ikm = shared_secret,
|
|
147
|
+
salt = nonce_a || nonce_b,
|
|
148
|
+
info = "wirekx v1 session key",
|
|
149
|
+
length = 32)
|
|
150
|
+
|
|
151
|
+
transcript = SHA-256(HELLO_bytes || HELLO_BACK_bytes)
|
|
152
|
+
|
|
153
|
+
verify_data = HMAC-SHA256(
|
|
154
|
+
key = symmetric_key,
|
|
155
|
+
data = "wirekx v1 <role> confirm" || transcript)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
`<role>` is `initiator` or `responder` depending on who sent the `CONFIRM`.
|
|
159
|
+
Compare received `verify_data` with constant-time equality. Mismatch = abort.
|
|
160
|
+
|
|
161
|
+
## Flow
|
|
162
|
+
|
|
163
|
+
```
|
|
164
|
+
initiator responder
|
|
165
|
+
│ │
|
|
166
|
+
│ HELLO (eph_pub_a, nonce_a) │
|
|
167
|
+
│ ────────────────────────────────────► │
|
|
168
|
+
│ │
|
|
169
|
+
│ HELLO_BACK (eph_pub_b, nonce_b) │
|
|
170
|
+
│ ◄──────────────────────────────────── │
|
|
171
|
+
│ │
|
|
172
|
+
│ derive symmetric_key, transcript │
|
|
173
|
+
│ CONFIRM (verify_data_initiator) │
|
|
174
|
+
│ ────────────────────────────────────► │
|
|
175
|
+
│ │
|
|
176
|
+
│ │ verify, then:
|
|
177
|
+
│ CONFIRM (verify_data_responder) │
|
|
178
|
+
│ ◄──────────────────────────────────── │
|
|
179
|
+
│ │
|
|
180
|
+
│ verify │
|
|
181
|
+
│ COMPLETE │ COMPLETE
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
On any malformed message, unexpected type, or verification mismatch: abort,
|
|
185
|
+
discard state, do not return the key.
|
|
186
|
+
|
|
187
|
+
## Output
|
|
188
|
+
|
|
189
|
+
After both `CONFIRM` messages verify, return to the caller:
|
|
190
|
+
|
|
191
|
+
- `symmetric_key` — 32 bytes
|
|
192
|
+
- `transcript_hash` — 32 bytes
|
|
193
|
+
- `protection_level` = `"opportunistic"`
|
|
194
|
+
- `peer_identity` = `null`
|
|
195
|
+
|
|
196
|
+
## Notes
|
|
197
|
+
|
|
198
|
+
- `transcript_hash` is unique per handshake.
|
|
199
|
+
- Possible values for `protection_level` are `"opportunistic"` and `"authenticated"`. Opportunistic means anonymous players, active MITM is undetectable. Authenticated means you have verified peer's identity by exchanging certificate via an external channel.
|
|
200
|
+
- Ephemeral keys are fresh per handshake and discarded after use.
|
|
201
|
+
- No version negotiation. Different versions cannot interoperate.
|
|
202
|
+
- Authenticated modes (`fingerprint`, `shared`, `publicCA`) will be built in future.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
wirekx/__init__.py,sha256=DttXcmYAxQ4OF_Ln5PvUqIMsdpXiMoSlzz2G-Z11Fz8,27462
|
|
2
|
+
wirekx-0.1.0.dist-info/METADATA,sha256=AeKYW2VCKJSflDHBdaGAFCHAXzfxwkfxBJP8jqW9VFA,6918
|
|
3
|
+
wirekx-0.1.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
4
|
+
wirekx-0.1.0.dist-info/licenses/LICENSE,sha256=JE3Cn08i5iAJ6crI446FQJt83avkor0twT8LkYZuNFU,10417
|
|
5
|
+
wirekx-0.1.0.dist-info/licenses/NOTICE,sha256=-GtiOtdPGq0CEcKEb8sH_MrYTl-79-yykd_hFzH3XAA,174
|
|
6
|
+
wirekx-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
34
|
+
made available under the License, as indicated by a copyright notice that is
|
|
35
|
+
included in or attached to the work (an example is provided in the Appendix
|
|
36
|
+
below).
|
|
37
|
+
|
|
38
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
39
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
40
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
41
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
42
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
43
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
|
44
|
+
|
|
45
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
46
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
|
47
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
|
48
|
+
by the copyright owner or by an individual or Legal Entity authorized to submit
|
|
49
|
+
on behalf of the copyright owner. For the purposes of this definition,
|
|
50
|
+
"submitted" means any form of electronic, verbal, or written communication sent
|
|
51
|
+
to the Licensor or its representatives, including but not limited to
|
|
52
|
+
communication on electronic mailing lists, source code control systems, and
|
|
53
|
+
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
|
54
|
+
the purpose of discussing and improving the Work, but excluding communication
|
|
55
|
+
that is conspicuously marked or otherwise designated in writing by the copyright
|
|
56
|
+
owner as "Not a Contribution."
|
|
57
|
+
|
|
58
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
59
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
60
|
+
incorporated within the Work.
|
|
61
|
+
|
|
62
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
63
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
64
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
65
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
66
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
67
|
+
Object form.
|
|
68
|
+
|
|
69
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License,
|
|
70
|
+
each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
71
|
+
no-charge, royalty-free, irrevocable (except as stated in this section) patent
|
|
72
|
+
license to make, have made, use, offer to sell, sell, import, and otherwise
|
|
73
|
+
transfer the Work, where such license applies only to those patent claims
|
|
74
|
+
licensable by such Contributor that are necessarily infringed by their
|
|
75
|
+
Contribution(s) alone or by combination of their Contribution(s) with the Work
|
|
76
|
+
to which such Contribution(s) was submitted. If You institute patent litigation
|
|
77
|
+
against any entity (including a cross-claim or counterclaim in a lawsuit)
|
|
78
|
+
alleging that the Work or a Contribution incorporated within the Work
|
|
79
|
+
constitutes direct or contributory patent infringement, then any patent licenses
|
|
80
|
+
granted to You under this License for that Work shall terminate as of the date
|
|
81
|
+
such litigation is filed.
|
|
82
|
+
|
|
83
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
84
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
85
|
+
Source or Object form, provided that You meet the following conditions:
|
|
86
|
+
|
|
87
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy
|
|
88
|
+
of this License; and
|
|
89
|
+
|
|
90
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
91
|
+
You changed the files; and
|
|
92
|
+
|
|
93
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
94
|
+
distribute, all copyright, patent, trademark, and attribution notices
|
|
95
|
+
from the Source form of the Work, excluding those notices that do not
|
|
96
|
+
pertain to any part of the Derivative Works; and
|
|
97
|
+
|
|
98
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution,
|
|
99
|
+
then any Derivative Works that You distribute must include a readable copy
|
|
100
|
+
of the attribution notices contained within such NOTICE file, excluding
|
|
101
|
+
those notices that do not pertain to any part of the Derivative Works, in
|
|
102
|
+
at least one of the following places: within a NOTICE text file
|
|
103
|
+
distributed as part of the Derivative Works; within the Source form or
|
|
104
|
+
documentation, if provided along with the Derivative Works; or, within a
|
|
105
|
+
display generated by the Derivative Works, if and wherever such
|
|
106
|
+
third-party notices normally appear. The contents of the NOTICE file are
|
|
107
|
+
for informational purposes only and do not modify the License. You may add
|
|
108
|
+
Your own attribution notices within Derivative Works that You distribute,
|
|
109
|
+
alongside or as an addendum to the NOTICE text from the Work, provided
|
|
110
|
+
that such additional attribution notices cannot be construed as modifying
|
|
111
|
+
the License.
|
|
112
|
+
|
|
113
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
114
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
115
|
+
distribution of Your modifications, or for any such Derivative Works as a whole,
|
|
116
|
+
provided Your use, reproduction, and distribution of the Work otherwise complies
|
|
117
|
+
with the conditions stated in this License.
|
|
118
|
+
|
|
119
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
120
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
121
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
122
|
+
additional terms or conditions. Notwithstanding the above, nothing herein shall
|
|
123
|
+
supersede or modify the terms of any separate license agreement you may have
|
|
124
|
+
executed with Licensor regarding such Contributions.
|
|
125
|
+
|
|
126
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
127
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
|
128
|
+
for reasonable and customary use in describing the origin of the Work and
|
|
129
|
+
reproducing the content of the NOTICE file.
|
|
130
|
+
|
|
131
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
132
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
133
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
134
|
+
KIND, either express or implied, including, without limitation, any warranties or
|
|
135
|
+
conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
136
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
137
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
138
|
+
associated with Your exercise of permissions under this License.
|
|
139
|
+
|
|
140
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
141
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
142
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
143
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
144
|
+
direct, indirect, special, incidental, or consequential damages of any character
|
|
145
|
+
arising as a result of this License or out of the use or inability to use the
|
|
146
|
+
Work (including but not limited to damages for loss of goodwill, work stoppage,
|
|
147
|
+
computer failure or malfunction, or any and all other commercial damages or
|
|
148
|
+
losses), even if such Contributor has been advised of the possibility of such
|
|
149
|
+
damages.
|
|
150
|
+
|
|
151
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
152
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
153
|
+
acceptance of support, warranty, indemnity, or other liability obligations
|
|
154
|
+
and/or rights consistent with this License. However, in accepting such
|
|
155
|
+
obligations, You may act only on Your own behalf and on Your sole
|
|
156
|
+
responsibility, not on behalf of any other Contributor, and only if You agree to
|
|
157
|
+
indemnify, defend, and hold each Contributor harmless for any liability incurred
|
|
158
|
+
by, or claims asserted against, such Contributor by reason of your accepting any
|
|
159
|
+
such warranty or additional liability.
|
|
160
|
+
|
|
161
|
+
END OF TERMS AND CONDITIONS
|
|
162
|
+
|
|
163
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
164
|
+
|
|
165
|
+
To apply the Apache License to your work, attach the following boilerplate
|
|
166
|
+
notice, with the fields enclosed by brackets "[]" replaced with your own
|
|
167
|
+
identifying information. Do not include the brackets. The text should be
|
|
168
|
+
enclosed in the appropriate comment syntax for the file format. We also
|
|
169
|
+
recommend that a file or class name and description of purpose be included on
|
|
170
|
+
the same "printed page" as the copyright notice for easier identification
|
|
171
|
+
within third-party archives.
|
|
172
|
+
|
|
173
|
+
Copyright 2026 wirekx contributors
|
|
174
|
+
|
|
175
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
176
|
+
you may not use this file except in compliance with the License.
|
|
177
|
+
You may obtain a copy of the License at
|
|
178
|
+
|
|
179
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
180
|
+
|
|
181
|
+
Unless required by applicable law or agreed to in writing, software
|
|
182
|
+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
183
|
+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
184
|
+
License for the specific language governing permissions and limitations under
|
|
185
|
+
the License.
|