@kumiai/mls 0.7.0 → 0.9.0

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.
@@ -242,8 +242,14 @@ export declare class GroupHandle {
242
242
  * {@link commitInvite}, {@link removeMember}, {@link commitLedgerEntries}) must not
243
243
  * be reused to send — it silently emits at the now-stale epoch. Adopt the commit's
244
244
  * `newGroup` and encrypt on that.
245
+ *
246
+ * `opts.aad` becomes the frame's MLS `authenticated_data` — bound into the AEAD but
247
+ * carried in the clear, so a caller reading it back (or comparing via `decrypt`'s
248
+ * `expectedAAD`) needs no key. Defaults to empty.
245
249
  */
246
- encrypt(plaintext: Uint8Array): Promise<Uint8Array>;
250
+ encrypt(plaintext: Uint8Array, opts?: {
251
+ aad?: Uint8Array;
252
+ }): Promise<Uint8Array>;
247
253
  /**
248
254
  * Open an application message sealed for the group and recover WHO SENT IT — the
249
255
  * counterpart to {@link encrypt}, and the read half of the group's application lane.
@@ -267,10 +273,18 @@ export declare class GroupHandle {
267
273
  *
268
274
  * ts-mls's own `processMessage` returns an application message's plaintext WITHOUT any
269
275
  * sender, which is why this exists rather than delegating.
276
+ *
277
+ * `opts.expectedAAD`, when given, is compared against the frame's CLEARTEXT
278
+ * `authenticatedData` BEFORE `mlsProcessMessage` runs — a mismatch throws having consumed
279
+ * no ratchet generation, so a wrong-topic frame costs nothing to reject. The frame's AAD
280
+ * is always returned, whether or not `expectedAAD` was given.
270
281
  */
271
- decrypt(message: Uint8Array): Promise<{
282
+ decrypt(message: Uint8Array, opts?: {
283
+ expectedAAD?: Uint8Array;
284
+ }): Promise<{
272
285
  payload: Uint8Array;
273
286
  senderDID?: string;
287
+ aad: Uint8Array;
274
288
  }>;
275
289
  /**
276
290
  * The device-proof binding at the leaf naming `did`: its bound controller (if any), the embedded
@@ -37,6 +37,11 @@ export function mutexFor(handle) {
37
37
  * material does not linger in the heap after the state that used it is replaced. */ function zeroAll(buffers) {
38
38
  for (const buffer of buffers)buffer.fill(0);
39
39
  }
40
+ /** AAD is not secret, so a length/byte compare is enough — no constant-time need. */ function bytesEqual(a, b) {
41
+ if (a.length !== b.length) return false;
42
+ for(let i = 0; i < a.length; i++)if (a[i] !== b[i]) return false;
43
+ return true;
44
+ }
40
45
  /** Thrown by GroupHandle.processMessage when the commit policy rejects an incoming
41
46
  * commit. The handle is left at its pre-commit epoch. */ export class CommitRejectedError extends Error {
42
47
  #proposals;
@@ -100,7 +105,7 @@ export function mutexFor(handle) {
100
105
  if (frame.wireformat !== wireformats.mls_private_message) return undefined;
101
106
  const pm = frame.privateMessage;
102
107
  if (pm == null || pm.contentType !== contentType) return undefined;
103
- if (!(pm.groupId instanceof Uint8Array) || typeof pm.epoch !== 'bigint' || !(pm.encryptedSenderData instanceof Uint8Array) || !(pm.ciphertext instanceof Uint8Array)) {
108
+ if (!(pm.groupId instanceof Uint8Array) || typeof pm.epoch !== 'bigint' || !(pm.encryptedSenderData instanceof Uint8Array) || !(pm.ciphertext instanceof Uint8Array) || !(pm.authenticatedData instanceof Uint8Array)) {
104
109
  return undefined;
105
110
  }
106
111
  return {
@@ -108,7 +113,8 @@ export function mutexFor(handle) {
108
113
  epoch: pm.epoch,
109
114
  contentType: pm.contentType,
110
115
  encryptedSenderData: pm.encryptedSenderData,
111
- ciphertext: pm.ciphertext
116
+ ciphertext: pm.ciphertext,
117
+ authenticatedData: pm.authenticatedData
112
118
  };
113
119
  }
114
120
  /**
@@ -579,12 +585,17 @@ export function mutexFor(handle) {
579
585
  * {@link commitInvite}, {@link removeMember}, {@link commitLedgerEntries}) must not
580
586
  * be reused to send — it silently emits at the now-stale epoch. Adopt the commit's
581
587
  * `newGroup` and encrypt on that.
582
- */ async encrypt(plaintext) {
588
+ *
589
+ * `opts.aad` becomes the frame's MLS `authenticated_data` — bound into the AEAD but
590
+ * carried in the clear, so a caller reading it back (or comparing via `decrypt`'s
591
+ * `expectedAAD`) needs no key. Defaults to empty.
592
+ */ async encrypt(plaintext, opts) {
583
593
  return mutexFor(this).run(async ()=>{
584
594
  const { newState, message, consumed } = await createApplicationMessage({
585
595
  context: this.#context,
586
596
  state: this.#state,
587
- message: plaintext
597
+ message: plaintext,
598
+ authenticatedData: opts?.aad ?? new Uint8Array()
588
599
  });
589
600
  this.#state = newState;
590
601
  zeroAll(consumed);
@@ -614,12 +625,22 @@ export function mutexFor(handle) {
614
625
  *
615
626
  * ts-mls's own `processMessage` returns an application message's plaintext WITHOUT any
616
627
  * sender, which is why this exists rather than delegating.
617
- */ async decrypt(message) {
628
+ *
629
+ * `opts.expectedAAD`, when given, is compared against the frame's CLEARTEXT
630
+ * `authenticatedData` BEFORE `mlsProcessMessage` runs — a mismatch throws having consumed
631
+ * no ratchet generation, so a wrong-topic frame costs nothing to reject. The frame's AAD
632
+ * is always returned, whether or not `expectedAAD` was given.
633
+ */ async decrypt(message, opts) {
618
634
  const decoded = decode(mlsMessageDecoder, message);
619
635
  if (decoded == null) throw new Error('decrypt: failed to decode MLSMessage');
620
636
  return mutexFor(this).run(async ()=>{
621
637
  const pm = readPrivateFrame(decoded, contentTypes.application);
622
638
  if (pm == null) throw new Error('decrypt: not a PrivateMessage application frame');
639
+ // Pre-open: reject a wrong-topic frame from its CLEARTEXT authenticatedData before
640
+ // mlsProcessMessage consumes a ratchet generation. Same lane as the sender-leaf read below.
641
+ if (opts?.expectedAAD != null && !bytesEqual(pm.authenticatedData, opts.expectedAAD)) {
642
+ throw new Error('decrypt: frame authenticated data does not match expected AAD');
643
+ }
623
644
  // Read the sender BEFORE opening: the sender-data secret is epoch-level and this
624
645
  // consumes no ratchet key, so a frame that then fails to open has cost nothing.
625
646
  const leafIndex = await readSenderLeafIndex(this.#context, this.#state.keySchedule.senderDataSecret, pm);
@@ -636,6 +657,7 @@ export function mutexFor(handle) {
636
657
  const senderDID = leafIndex == null ? undefined : this.#didOfLeaf(leafIndex);
637
658
  return {
638
659
  payload: result.message,
660
+ aad: result.aad,
639
661
  ...senderDID != null && {
640
662
  senderDID
641
663
  }
@@ -15,6 +15,7 @@ export type PrivateCommitFrame = {
15
15
  contentType: number;
16
16
  encryptedSenderData: Uint8Array;
17
17
  ciphertext: Uint8Array;
18
+ authenticatedData: Uint8Array;
18
19
  };
19
20
  /**
20
21
  * Decrypt a PrivateMessage's sender-data and return the committer's leaf index, or `null`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kumiai/mls",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "E2EE group identity and membership for Enkaku using MLS",
5
5
  "keywords": [
6
6
  "mls",
@@ -28,9 +28,9 @@
28
28
  "@kokuin/capability": "^0.3.0",
29
29
  "@kokuin/controller": "^0.1.0",
30
30
  "@kokuin/token": "^0.5.0",
31
- "@noble/ciphers": "^2.3.0",
32
- "@noble/curves": "^2.3.0",
33
- "@noble/hashes": "^2.3.0",
31
+ "@noble/ciphers": "^2.4.0",
32
+ "@noble/curves": "^2.4.0",
33
+ "@noble/hashes": "^2.4.0",
34
34
  "@sozai/codec": "^0.4.0",
35
35
  "@sozai/event": "^0.1.3",
36
36
  "@sozai/runtime": "^0.1.0",