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
|
"""
MeshBay — Sender Keys protocol for group messaging.
Signal Groups approach: each member maintains their own sending chain.
Advantages over shared Double Ratchet:
- O(N) state per group (one chain per member) vs O(N^2) pairwise
- Single encrypt per message (not N encryptions)
- No key/nonce reuse — each sender has an independent chain
Key components:
- Chain key ratchet: HKDF per message, provides forward secrecy
- Message key derivation: separate HKDF from chain key
- Ed25519 signing: each sender signs their ciphertext
- AES-256-GCM encryption: browser-compatible symmetric cipher
Key distribution:
- On join: admin wraps each sender's SenderKeyDistribution with GEK
- On leave: all remaining members rotate their chain keys
"""
import os
import struct
from dataclasses import dataclass, field
from cryptography.hazmat.primitives.asymmetric.ed25519 import (
Ed25519PrivateKey,
Ed25519PublicKey,
)
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives import hashes, serialization
CHAIN_INFO = b"meshbay:sk:chain:v1"
MSG_KEY_INFO = b"meshbay:sk:msg:v1"
CHAIN_KEY_LEN = 32
MSG_KEY_LEN = 32
MAX_SKIP = 256
def _hkdf(ikm: bytes, info: bytes, length: int = 32) -> bytes:
return HKDF(
algorithm=hashes.SHA256(), length=length, salt=None, info=info,
).derive(ikm)
def _ratchet_chain(chain_key: bytes) -> tuple[bytes, bytes]:
"""Advance chain key → (new_chain_key, message_key)."""
new_ck = _hkdf(chain_key, CHAIN_INFO, CHAIN_KEY_LEN)
mk = _hkdf(chain_key, MSG_KEY_INFO, MSG_KEY_LEN)
return new_ck, mk
# ── Data structures ──────────────────────────────────────────────────────────
@dataclass
class SenderKeyDistribution:
"""Sent to group members when a sender joins or rotates."""
sender_id: str
chain_key: bytes # 32-byte initial chain key
iteration: int # current message counter
signing_pk: bytes # 32-byte raw Ed25519 public key
def serialize(self) -> bytes:
sender_bytes = self.sender_id.encode()
return (
struct.pack(">H", len(sender_bytes))
+ sender_bytes
+ self.chain_key
+ struct.pack(">I", self.iteration)
+ self.signing_pk
)
@classmethod
def deserialize(cls, data: bytes) -> "SenderKeyDistribution":
sender_len = struct.unpack(">H", data[:2])[0]
offset = 2
sender_id = data[offset:offset + sender_len].decode()
offset += sender_len
chain_key = data[offset:offset + 32]
offset += 32
iteration = struct.unpack(">I", data[offset:offset + 4])[0]
offset += 4
signing_pk = data[offset:offset + 32]
return cls(sender_id=sender_id, chain_key=chain_key,
iteration=iteration, signing_pk=signing_pk)
@dataclass
class SenderKeyState:
"""One sender's chain state as seen by any group member."""
sender_id: str
chain_key: bytes
iteration: int
signing_key: Ed25519PublicKey
_skipped_keys: dict[int, bytes] = field(default_factory=dict)
@classmethod
def from_distribution(cls, dist: SenderKeyDistribution) -> "SenderKeyState":
pk = Ed25519PublicKey.from_public_bytes(dist.signing_pk)
return cls(
sender_id=dist.sender_id,
chain_key=dist.chain_key,
iteration=dist.iteration,
signing_key=pk,
)
def advance_to(self, target: int) -> bytes:
"""Advance chain to target iteration, caching skipped keys. Returns message key."""
if target < self.iteration:
mk = self._skipped_keys.pop(target, None)
if mk is None:
raise ValueError(f"Message key {target} already consumed or too old")
return mk
skip_count = target - self.iteration
if skip_count > MAX_SKIP:
raise ValueError(f"Too many skipped messages: {skip_count}")
for i in range(skip_count):
new_ck, mk = _ratchet_chain(self.chain_key)
self._skipped_keys[self.iteration] = mk
self.chain_key = new_ck
self.iteration += 1
new_ck, mk = _ratchet_chain(self.chain_key)
self.chain_key = new_ck
self.iteration += 1
return mk
@dataclass
class SenderKeyRecord:
"""Sender's own key state (includes signing private key)."""
sender_id: str
chain_key: bytes
iteration: int
signing_sk: Ed25519PrivateKey
@classmethod
def create(cls, sender_id: str) -> "SenderKeyRecord":
return cls(
sender_id=sender_id,
chain_key=os.urandom(CHAIN_KEY_LEN),
iteration=0,
signing_sk=Ed25519PrivateKey.generate(),
)
def distribution(self) -> SenderKeyDistribution:
pk_raw = self.signing_sk.public_key().public_bytes(
serialization.Encoding.Raw, serialization.PublicFormat.Raw)
return SenderKeyDistribution(
sender_id=self.sender_id,
chain_key=self.chain_key,
iteration=self.iteration,
signing_pk=pk_raw,
)
def rotate(self) -> "SenderKeyRecord":
"""Create a new record with fresh chain key (call on member removal)."""
return SenderKeyRecord(
sender_id=self.sender_id,
chain_key=os.urandom(CHAIN_KEY_LEN),
iteration=0,
signing_sk=Ed25519PrivateKey.generate(),
)
# ── Group store ──────────────────────────────────────────────────────────────
class GroupSenderKeyStore:
"""All sender key states for one group, held by one member."""
def __init__(self, group_id: str):
self.group_id = group_id
self._states: dict[str, SenderKeyState] = {}
def add_sender(self, dist: SenderKeyDistribution) -> None:
self._states[dist.sender_id] = SenderKeyState.from_distribution(dist)
def remove_sender(self, sender_id: str) -> None:
self._states.pop(sender_id, None)
def get_state(self, sender_id: str) -> SenderKeyState | None:
return self._states.get(sender_id)
@property
def sender_count(self) -> int:
return len(self._states)
# ── Encrypt / Decrypt ────────────────────────────────────────────────────────
@dataclass
class SenderKeyMessage:
"""Wire format for a Sender Keys encrypted message."""
sender_id: str
iteration: int
ciphertext: bytes
nonce: bytes
signature: bytes
def serialize(self) -> bytes:
sender_bytes = self.sender_id.encode()
return (
struct.pack(">H", len(sender_bytes))
+ sender_bytes
+ struct.pack(">I", self.iteration)
+ struct.pack(">I", len(self.ciphertext))
+ self.ciphertext
+ self.nonce
+ self.signature
)
@classmethod
def deserialize(cls, data: bytes) -> "SenderKeyMessage":
offset = 0
sender_len = struct.unpack(">H", data[offset:offset + 2])[0]
offset += 2
sender_id = data[offset:offset + sender_len].decode()
offset += sender_len
iteration = struct.unpack(">I", data[offset:offset + 4])[0]
offset += 4
ct_len = struct.unpack(">I", data[offset:offset + 4])[0]
offset += 4
ciphertext = data[offset:offset + ct_len]
offset += ct_len
nonce = data[offset:offset + 12]
offset += 12
signature = data[offset:offset + 64]
return cls(sender_id=sender_id, iteration=iteration,
ciphertext=ciphertext, nonce=nonce, signature=signature)
def encrypt_message(
record: SenderKeyRecord,
plaintext: bytes,
aad: bytes = b"",
) -> tuple[SenderKeyMessage, SenderKeyRecord]:
"""
Encrypt a message with the sender's chain key.
Returns (message, updated_record).
"""
new_ck, mk = _ratchet_chain(record.chain_key)
iteration = record.iteration
nonce = os.urandom(12)
ct = AESGCM(mk).encrypt(nonce, plaintext, aad or None)
sig_payload = struct.pack(">I", iteration) + nonce + ct
signature = record.signing_sk.sign(sig_payload)
msg = SenderKeyMessage(
sender_id=record.sender_id,
iteration=iteration,
ciphertext=ct,
nonce=nonce,
signature=signature,
)
updated = SenderKeyRecord(
sender_id=record.sender_id,
chain_key=new_ck,
iteration=iteration + 1,
signing_sk=record.signing_sk,
)
return msg, updated
def decrypt_message(
store: GroupSenderKeyStore,
msg: SenderKeyMessage,
aad: bytes = b"",
) -> bytes:
"""
Decrypt and verify a Sender Keys message.
Advances the sender's chain state in the store.
"""
state = store.get_state(msg.sender_id)
if state is None:
raise ValueError(f"Unknown sender: {msg.sender_id}")
sig_payload = struct.pack(">I", msg.iteration) + msg.nonce + msg.ciphertext
state.signing_key.verify(msg.signature, sig_payload)
mk = state.advance_to(msg.iteration)
return AESGCM(mk).decrypt(msg.nonce, msg.ciphertext, aad or None)
|