@browsercore/quic 0.2.1 → 0.2.3

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.
Files changed (61) hide show
  1. package/README.md +19 -1
  2. package/dist/connection.d.ts +149 -23
  3. package/dist/connection.d.ts.map +1 -1
  4. package/dist/connection.js +543 -66
  5. package/dist/connection.js.map +1 -1
  6. package/dist/crypto/hash-length.d.ts +11 -0
  7. package/dist/crypto/hash-length.d.ts.map +1 -0
  8. package/dist/crypto/hash-length.js +20 -0
  9. package/dist/crypto/hash-length.js.map +1 -0
  10. package/dist/crypto/index.d.ts +8 -0
  11. package/dist/crypto/index.d.ts.map +1 -0
  12. package/dist/crypto/index.js +8 -0
  13. package/dist/crypto/index.js.map +1 -0
  14. package/dist/crypto/key-derivation.d.ts +87 -0
  15. package/dist/crypto/key-derivation.d.ts.map +1 -0
  16. package/dist/crypto/key-derivation.js +123 -0
  17. package/dist/crypto/key-derivation.js.map +1 -0
  18. package/dist/errors.d.ts +27 -0
  19. package/dist/errors.d.ts.map +1 -1
  20. package/dist/errors.js +36 -1
  21. package/dist/errors.js.map +1 -1
  22. package/dist/frame/frame.d.ts.map +1 -1
  23. package/dist/frame/frame.js +2 -2
  24. package/dist/frame/frame.js.map +1 -1
  25. package/dist/handshake/index.d.ts +10 -0
  26. package/dist/handshake/index.d.ts.map +1 -0
  27. package/dist/handshake/index.js +9 -0
  28. package/dist/handshake/index.js.map +1 -0
  29. package/dist/handshake/quic-handshake.d.ts +119 -0
  30. package/dist/handshake/quic-handshake.d.ts.map +1 -0
  31. package/dist/handshake/quic-handshake.js +213 -0
  32. package/dist/handshake/quic-handshake.js.map +1 -0
  33. package/dist/handshake/quic-transport-adapter.d.ts +86 -0
  34. package/dist/handshake/quic-transport-adapter.d.ts.map +1 -0
  35. package/dist/handshake/quic-transport-adapter.js +166 -0
  36. package/dist/handshake/quic-transport-adapter.js.map +1 -0
  37. package/dist/index.d.ts +8 -5
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +10 -4
  40. package/dist/index.js.map +1 -1
  41. package/dist/packet/packet-protection.d.ts +158 -0
  42. package/dist/packet/packet-protection.d.ts.map +1 -0
  43. package/dist/packet/packet-protection.js +312 -0
  44. package/dist/packet/packet-protection.js.map +1 -0
  45. package/dist/packet/packet.d.ts +2 -2
  46. package/dist/packet/packet.d.ts.map +1 -1
  47. package/dist/packet/packet.js +4 -4
  48. package/dist/packet/packet.js.map +1 -1
  49. package/dist/stream/stream.d.ts +7 -11
  50. package/dist/stream/stream.d.ts.map +1 -1
  51. package/dist/stream/stream.js +16 -28
  52. package/dist/stream/stream.js.map +1 -1
  53. package/dist/transport-params.d.ts +4 -0
  54. package/dist/transport-params.d.ts.map +1 -1
  55. package/dist/transport-params.js +8 -1
  56. package/dist/transport-params.js.map +1 -1
  57. package/dist/types.d.ts +129 -41
  58. package/dist/types.d.ts.map +1 -1
  59. package/dist/types.js +8 -20
  60. package/dist/types.js.map +1 -1
  61. package/package.json +7 -5
package/dist/index.d.ts CHANGED
@@ -3,15 +3,15 @@
3
3
  *
4
4
  * QUIC transport (RFC 9000) over a datagram (UDP) transport. No knowledge of
5
5
  * HTTP/3, TLS handshake semantics, or sockets — it composes exclusively over an
6
- * injected {@link DatagramTransport} and `@browsercore/crypto`. Higher layers
7
- * (http3) compose through {@link QuicConnection}.
6
+ * injected {@link DatagramTransport} and an injected `CryptoProvider`. Higher
7
+ * layers (http3) compose through {@link QuicConnection}.
8
8
  */
9
9
  export { connectQuic, QuicConnectionImpl } from "./connection.js";
10
- export { ConnectionClosedError, FlowControlError, FrameParseError, HandshakeTimeoutError, PacketParseError, QuicError, ResetStreamError, StopSendingError, TransportParameterError, } from "./errors.js";
10
+ export { ConnectionClosedError, ConnectionClosingError, FlowControlError, FrameParseError, HandshakeTimeoutError, PacketParseError, PacketProtectionError, QuicError, ResetStreamError, StopSendingError, TransportParameterError, TlsHandshakeError, } from "./errors.js";
11
11
  export { LongPacketType, QuicFrameType, TransportParameter, type LongPacketTypeValue, type QuicFrame, type QuicFrameTypeValue, } from "./types.js";
12
- export { EMPTY_CONNECTION_ID, HEADER_FORM_LONG, HEADER_FORM_SHORT, MIN_MAX_UDP_PAYLOAD_SIZE, STREAM_FIN_BIT, STREAM_LEN_BIT, STREAM_OFF_BIT, firstStreamId, makeStreamId, nextStreamId, streamIdIsBidirectional, streamIdIsClientInitiated, } from "./types.js";
12
+ export { EMPTY_CONNECTION_ID, HEADER_FORM_LONG, HEADER_FORM_SHORT, MIN_MAX_UDP_PAYLOAD_SIZE, STREAM_FIN_BIT, STREAM_LEN_BIT, STREAM_OFF_BIT, firstStreamId, makeConnectionId, makeStreamId, nextStreamId, streamIdIsBidirectional, streamIdIsClientInitiated, systemClock, } from "./types.js";
13
13
  export { VARINT_MAX } from "./frame/varint.js";
14
- export { type BaseQuicFrame, type ConnectionId, type DatagramCloseReason, type DatagramTransport, type QuicConnection, type QuicOptions, type QuicStream, type QuicTransportParameters, type StreamId, type StreamState, type StreamCloseReason, type UdpAddress, } from "./types.js";
14
+ export { type BaseQuicFrame, type Clock, type ConnectionId, type DatagramCloseReason, type DatagramTransport, type QuicConnection, type QuicOptions, type QuicStream, type QuicTransportParameters, type StreamId, type StreamState, type StreamCloseReason, type UdpAddress, type ClientHelloConfigLike, type ProtocolVersionLike, } from "./types.js";
15
15
  export { decodeVarint, encodeVarint, encodeVarintInto, getVarintEncodedLength, prefixMask, } from "./frame/varint.js";
16
16
  export { decodeFrame, readFrames, serializeFrame } from "./frame/frame.js";
17
17
  export { parsePacketHeader, serializeShortHeader, serializeLongHeader, decodePacketNumber, encodePacketNumber, } from "./packet/packet.js";
@@ -21,4 +21,7 @@ export type { StreamManager } from "./stream/stream.js";
21
21
  export { assertNever, concat, concatAll, hex } from "./utils.js";
22
22
  export { decodeTransportParameters, encodeTransportParameters, fromWireParameters, toWireParameters, } from "./transport-params.js";
23
23
  export type { TransportParameters } from "./transport-params.js";
24
+ export { constructNonce, encryptPayload, decryptPayload, computeHeaderProtectionMask, applyHeaderProtection, removeHeaderProtection, protectPayload, unprotectPayload, type QuicAead, } from "./packet/packet-protection.js";
25
+ export { quicHkdfExpandLabel, deriveQuicSecrets, deriveInitialSecrets, INITIAL_SALT_V1, QUIC_IV_LENGTH, type QuicProtectionSecrets, type InitialSecrets, } from "./crypto/key-derivation.js";
26
+ export { runQuicHandshake, adaptQuicStreamToTransport, QuicTransportAdapter, type QuicKeyPhase, type QuicPhaseSecrets, type QuicHandshakeResult, } from "./handshake/index.js";
24
27
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAElE,OAAO,EACH,qBAAqB,EACrB,gBAAgB,EAChB,eAAe,EACf,qBAAqB,EACrB,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAChB,uBAAuB,GAC1B,MAAM,aAAa,CAAC;AAErB,OAAO,EACH,cAAc,EACd,aAAa,EACb,kBAAkB,EAClB,KAAK,mBAAmB,EACxB,KAAK,SAAS,EACd,KAAK,kBAAkB,GAC1B,MAAM,YAAY,CAAC;AAEpB,OAAO,EACH,mBAAmB,EACnB,gBAAgB,EAChB,iBAAiB,EACjB,wBAAwB,EACxB,cAAc,EACd,cAAc,EACd,cAAc,EACd,aAAa,EACb,YAAY,EACZ,YAAY,EACZ,uBAAuB,EACvB,yBAAyB,GAC5B,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE/C,OAAO,EACH,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,mBAAmB,EACxB,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACnB,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,uBAAuB,EAC5B,KAAK,QA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,EAClB,gBAAgB,GACnB,MAAM,uBAAuB,CAAC;AAC/B,YAAY,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAGjE,OAAO,EACH,cAAc,EACd,cAAc,EACd,cAAc,EACd,2BAA2B,EAC3B,qBAAqB,EACrB,sBAAsB,EACtB,cAAc,EACd,gBAAgB,EAChB,KAAK,QAAQ,GAChB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,EACH,mBAAmB,EACnB,iBAAiB,EACjB,oBAAoB,EACpB,eAAe,EACf,cAAc,EACd,KAAK,qBAAqB,EAC1B,KAAK,cAAc,GACtB,MAAM,4BAA4B,CAAC;AAGpC,OAAO,EACH,gBAAgB,EAChB,0BAA0B,EAC1B,oBAAoB,EACpB,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,mBAAmB,GAC3B,MAAM,sBAAsB,CAAC"}
package/dist/index.js CHANGED
@@ -3,13 +3,13 @@
3
3
  *
4
4
  * QUIC transport (RFC 9000) over a datagram (UDP) transport. No knowledge of
5
5
  * HTTP/3, TLS handshake semantics, or sockets — it composes exclusively over an
6
- * injected {@link DatagramTransport} and `@browsercore/crypto`. Higher layers
7
- * (http3) compose through {@link QuicConnection}.
6
+ * injected {@link DatagramTransport} and an injected `CryptoProvider`. Higher
7
+ * layers (http3) compose through {@link QuicConnection}.
8
8
  */
9
9
  export { connectQuic, QuicConnectionImpl } from "./connection.js";
10
- export { ConnectionClosedError, FlowControlError, FrameParseError, HandshakeTimeoutError, PacketParseError, QuicError, ResetStreamError, StopSendingError, TransportParameterError, } from "./errors.js";
10
+ export { ConnectionClosedError, ConnectionClosingError, FlowControlError, FrameParseError, HandshakeTimeoutError, PacketParseError, PacketProtectionError, QuicError, ResetStreamError, StopSendingError, TransportParameterError, TlsHandshakeError, } from "./errors.js";
11
11
  export { LongPacketType, QuicFrameType, TransportParameter, } from "./types.js";
12
- export { EMPTY_CONNECTION_ID, HEADER_FORM_LONG, HEADER_FORM_SHORT, MIN_MAX_UDP_PAYLOAD_SIZE, STREAM_FIN_BIT, STREAM_LEN_BIT, STREAM_OFF_BIT, firstStreamId, makeStreamId, nextStreamId, streamIdIsBidirectional, streamIdIsClientInitiated, } from "./types.js";
12
+ export { EMPTY_CONNECTION_ID, HEADER_FORM_LONG, HEADER_FORM_SHORT, MIN_MAX_UDP_PAYLOAD_SIZE, STREAM_FIN_BIT, STREAM_LEN_BIT, STREAM_OFF_BIT, firstStreamId, makeConnectionId, makeStreamId, nextStreamId, streamIdIsBidirectional, streamIdIsClientInitiated, systemClock, } from "./types.js";
13
13
  export { VARINT_MAX } from "./frame/varint.js";
14
14
  export {} from "./types.js";
15
15
  export { decodeVarint, encodeVarint, encodeVarintInto, getVarintEncodedLength, prefixMask, } from "./frame/varint.js";
@@ -18,4 +18,10 @@ export { parsePacketHeader, serializeShortHeader, serializeLongHeader, decodePac
18
18
  export { createStreamManager } from "./stream/stream.js";
19
19
  export { assertNever, concat, concatAll, hex } from "./utils.js";
20
20
  export { decodeTransportParameters, encodeTransportParameters, fromWireParameters, toWireParameters, } from "./transport-params.js";
21
+ // --- QUIC packet protection (RFC 9001 §5) ----------------------------------
22
+ export { constructNonce, encryptPayload, decryptPayload, computeHeaderProtectionMask, applyHeaderProtection, removeHeaderProtection, protectPayload, unprotectPayload, } from "./packet/packet-protection.js";
23
+ // --- QUIC key derivation (RFC 9001 §5) -------------------------------------
24
+ export { quicHkdfExpandLabel, deriveQuicSecrets, deriveInitialSecrets, INITIAL_SALT_V1, QUIC_IV_LENGTH, } from "./crypto/key-derivation.js";
25
+ // --- QUIC TLS handshake (RFC 9001 §4, §8) ----------------------------------
26
+ export { runQuicHandshake, adaptQuicStreamToTransport, QuicTransportAdapter, } from "./handshake/index.js";
21
27
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,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GAEnB,MAAM,+BAA+B,CAAC;AAEvC,8EAA8E;AAC9E,OAAO,EACH,mBAAmB,EACnB,iBAAiB,EACjB,oBAAoB,EACpB,eAAe,EACf,cAAc,GAGjB,MAAM,4BAA4B,CAAC;AAEpC,8EAA8E;AAC9E,OAAO,EACH,gBAAgB,EAChB,0BAA0B,EAC1B,oBAAoB,GAIvB,MAAM,sBAAsB,CAAC"}
@@ -0,0 +1,158 @@
1
+ /**
2
+ * QUIC packet protection (RFC 9001 §5).
3
+ *
4
+ * Applies and removes QUIC's two-layer packet protection:
5
+ *
6
+ * 1. Payload encryption (RFC 9001 §5.3): AEAD with quic_key + quic_iv.
7
+ * The per-record nonce is the XOR of the (zero-padded, big-endian)
8
+ * packet number into the static IV. The AAD is the unprotected header
9
+ * (everything up to and including the unencrypted packet number).
10
+ *
11
+ * 2. Header protection (RFC 9001 §5.4): the low bits of the first byte
12
+ * and the packet number are masked with a mask derived from
13
+ * AES-ECB(quic_hp, sample). The sample is 16 bytes of ciphertext
14
+ * starting 4 bytes into the protected payload.
15
+ *
16
+ * This module is pure wire-format logic — no I/O, no key management. It
17
+ * operates over @browsercore/crypto for the AEAD and AES-ECB primitives.
18
+ *
19
+ * Honest limitations:
20
+ * - Only AES-128-GCM, AES-256-GCM, and ChaCha20-Poly1305 are wired for
21
+ * payload AEAD. AES-128-CCM is defined by TLS 1.3 but never negotiated
22
+ * by QUIC v1, so it is rejected here.
23
+ * - 0-RTT, KeyUpdate, and key-phase-bit handling are out of scope for now
24
+ * — the connection layer passes the correct keys for the packet's phase.
25
+ */
26
+ import type { CryptoProvider } from "@browsercore/contracts";
27
+ import { type QuicProtectionSecrets } from "../crypto/key-derivation.js";
28
+ /**
29
+ * AEAD algorithm identifiers (subset of TLS AEAD algorithms used by QUIC v1).
30
+ * Mirrors @browsercore/tls's AeadAlgorithm naming ("CHACHA20_POLY1305" with
31
+ * underscores). The aeadAlgorithmId helper maps to @browsercore/crypto's
32
+ * "ChaCha20-Poly1305" (hyphens) when calling the crypto provider.
33
+ */
34
+ export type QuicAead = "AES-128-GCM" | "AES-256-GCM" | "CHACHA20-POLY1305";
35
+ /**
36
+ * Construct the per-packet AEAD nonce from the static IV and the packet number.
37
+ *
38
+ * The packet number is encoded as a zero-padded big-endian integer in an
39
+ * 8-byte buffer, then XOR'd into the IV's low 8 bytes (RFC 9001 §5.3).
40
+ *
41
+ * @param iv Static IV (12 bytes) derived from the traffic secret.
42
+ * @param pn Packet number (0..2^62-1).
43
+ */
44
+ export declare function constructNonce(iv: Uint8Array, pn: bigint): Uint8Array;
45
+ /**
46
+ * AEAD-encrypt a payload with the given key and nonce.
47
+ *
48
+ * @param aead AEAD algorithm (AES-128/256-GCM or ChaCha20-Poly1305).
49
+ * @param key AEAD key (16 bytes for AES-128, 32 bytes for AES-256 / ChaCha20).
50
+ * @param nonce Per-packet nonce (12 bytes), computed via {@link constructNonce}.
51
+ * @param plaintext The unprotected payload bytes.
52
+ * @param aad The unprotected header (everything up to and including the
53
+ * unencrypted packet number).
54
+ * @param provider Cryptographic provider.
55
+ * @returns Ciphertext with the 16-byte authentication tag appended.
56
+ */
57
+ export declare function encryptPayload(aead: QuicAead, key: Uint8Array, nonce: Uint8Array, plaintext: Uint8Array, aad: Uint8Array, provider: CryptoProvider): Uint8Array;
58
+ /**
59
+ * AEAD-decrypt a payload with the given key and nonce.
60
+ *
61
+ * @param aead AEAD algorithm.
62
+ * @param key AEAD key.
63
+ * @param nonce Per-packet nonce (12 bytes), computed via {@link constructNonce}.
64
+ * @param ciphertext Ciphertext with the 16-byte authentication tag appended.
65
+ * @param aad The unprotected header (everything up to and including the
66
+ * packet number after header protection removal).
67
+ * @param provider Cryptographic provider.
68
+ * @returns Decrypted plaintext.
69
+ * @throws Error on authentication failure.
70
+ */
71
+ export declare function decryptPayload(aead: QuicAead, key: Uint8Array, nonce: Uint8Array, ciphertext: Uint8Array, aad: Uint8Array, provider: CryptoProvider): Uint8Array;
72
+ /**
73
+ * Derive the header-protection mask for a given sample (RFC 9001 §5.4.1).
74
+ *
75
+ * mask = AES-ECB(hp_key, sample)
76
+ *
77
+ * The sample is the 16 bytes of (protected) payload starting 4 bytes past
78
+ * the start of the packet number. For short headers (1-RTT), the first byte
79
+ * of the mask is used to protect the low 5 bits of the first header byte
80
+ * (after header protection is removed, these encode the header form + key
81
+ * phase + reserved bits). For long headers, the first byte protects the low
82
+ * 4 bits (header form + fixed + long packet type). Bytes 1–5 of the mask
83
+ * protect the encoded packet number.
84
+ *
85
+ * @param hpKey Header-protection key (16 bytes for AES-128, 32 bytes for AES-256).
86
+ * @param sample 16-byte sample from the protected payload.
87
+ * @param provider Cryptographic provider.
88
+ * @returns A 16-byte mask; the low 5 (short header) or 4 (long header) bits of
89
+ * byte 0 protect the first header byte, bytes 1–5 protect the packet
90
+ * number.
91
+ */
92
+ export declare function computeHeaderProtectionMask(hpKey: Uint8Array, sample: Uint8Array, provider: CryptoProvider): Uint8Array;
93
+ /**
94
+ * Apply header protection to the first byte and packet number (RFC 9001 §5.4).
95
+ *
96
+ * @param firstByte The first header byte (already serialized).
97
+ * @param packetNumber The packet number to protect.
98
+ * @param pnLength Number of bytes the packet number occupies on the wire (1–4).
99
+ * @param mask Header-protection mask (from {@link computeHeaderProtectionMask}).
100
+ * @param isLongHeader Whether this is a long header (affects which low bits are masked).
101
+ * @returns The masked first byte and masked packet-number bytes.
102
+ */
103
+ export declare function applyHeaderProtection(firstByte: number, packetNumber: bigint, pnLength: number, mask: Uint8Array, isLongHeader: boolean): {
104
+ firstByte: number;
105
+ packetNumberBytes: Uint8Array;
106
+ };
107
+ /**
108
+ * Remove header protection from the first byte and packet number (RFC 9001 §5.4).
109
+ *
110
+ * @param firstByte The first header byte (as received, still masked).
111
+ * @param pnBytes The masked packet-number bytes (as received).
112
+ * @param pnLength Number of bytes the packet number occupies (1–4).
113
+ * @param mask Header-protection mask (from {@link computeHeaderProtectionMask}).
114
+ * @param isLongHeader Whether this is a long header.
115
+ * @returns The unmasked first byte and packet number.
116
+ */
117
+ export declare function removeHeaderProtection(firstByte: number, pnBytes: Uint8Array, pnLength: number, mask: Uint8Array, isLongHeader: boolean): {
118
+ firstByte: number;
119
+ packetNumber: bigint;
120
+ };
121
+ /**
122
+ * Protect a QUIC packet payload: AEAD-encrypt the payload and apply header
123
+ * protection to the first byte + packet number (RFC 9001 §5.3, §5.4).
124
+ *
125
+ * @param payload The unprotected frame payload.
126
+ * @param packetNumber The packet number to assign.
127
+ * @param pnLength Number of bytes the packet number occupies on the wire (1–4).
128
+ * @param firstByte The first header byte (already serialized, unprotected).
129
+ * @param aead AEAD algorithm.
130
+ * @param secrets QUIC protection secrets (key, iv, hp) for this direction + phase.
131
+ * @param isLongHeader Whether this is a long header.
132
+ * @param provider Cryptographic provider.
133
+ * @returns The AEAD-protected payload (with tag appended) — the caller prepends
134
+ * the masked first byte and masked packet number to form the full packet.
135
+ */
136
+ export declare function protectPayload(payload: Uint8Array, packetNumber: bigint, pnLength: number, firstByte: number, aead: QuicAead, secrets: QuicProtectionSecrets, isLongHeader: boolean, provider: CryptoProvider): {
137
+ protectedPayload: Uint8Array;
138
+ maskedFirstByte: number;
139
+ maskedPacketNumber: Uint8Array;
140
+ };
141
+ /**
142
+ * Unprotect a QUIC packet payload: remove header protection, then AEAD-decrypt.
143
+ *
144
+ * @param firstByte The first header byte (as received, still masked).
145
+ * @param pnBytes The masked packet-number bytes (as received).
146
+ * @param pnLength Number of bytes the packet number occupies (1–4).
147
+ * @param protectedPayload The AEAD-protected payload (with tag appended).
148
+ * @param isLongHeader Whether this is a long header.
149
+ * @param aead AEAD algorithm.
150
+ * @param secrets QUIC protection secrets (key, iv, hp) for this direction + phase.
151
+ * @param provider Cryptographic provider.
152
+ * @returns The decrypted frame payload and the decoded packet number.
153
+ */
154
+ export declare function unprotectPayload(firstByte: number, pnBytes: Uint8Array, pnLength: number, protectedPayload: Uint8Array, isLongHeader: boolean, aead: QuicAead, secrets: QuicProtectionSecrets, provider: CryptoProvider): {
155
+ payload: Uint8Array;
156
+ packetNumber: bigint;
157
+ };
158
+ //# sourceMappingURL=packet-protection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"packet-protection.d.ts","sourceRoot":"","sources":["../../src/packet/packet-protection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,EAAkB,KAAK,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AAGzF;;;;;GAKG;AACH,MAAM,MAAM,QAAQ,GAAG,aAAa,GAAG,aAAa,GAAG,mBAAmB,CAAC;AAoB3E;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,GAAG,UAAU,CAgBrE;AAoBD;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAC1B,IAAI,EAAE,QAAQ,EACd,GAAG,EAAE,UAAU,EACf,KAAK,EAAE,UAAU,EACjB,SAAS,EAAE,UAAU,EACrB,GAAG,EAAE,UAAU,EACf,QAAQ,EAAE,cAAc,GACzB,UAAU,CAEZ;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAC1B,IAAI,EAAE,QAAQ,EACd,GAAG,EAAE,UAAU,EACf,KAAK,EAAE,UAAU,EACjB,UAAU,EAAE,UAAU,EACtB,GAAG,EAAE,UAAU,EACf,QAAQ,EAAE,cAAc,GACzB,UAAU,CAEZ;AA8CD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,2BAA2B,CACvC,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,UAAU,EAClB,QAAQ,EAAE,cAAc,GACzB,UAAU,CAQZ;AAED;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CACjC,SAAS,EAAE,MAAM,EACjB,YAAY,EAAE,MAAM,EACpB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,UAAU,EAChB,YAAY,EAAE,OAAO,GACtB;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,iBAAiB,EAAE,UAAU,CAAA;CAAE,CAwBtD;AAED;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CAClC,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,UAAU,EACnB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,UAAU,EAChB,YAAY,EAAE,OAAO,GACtB;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,CAoB7C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAC1B,OAAO,EAAE,UAAU,EACnB,YAAY,EAAE,MAAM,EACpB,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,QAAQ,EACd,OAAO,EAAE,qBAAqB,EAC9B,YAAY,EAAE,OAAO,EACrB,QAAQ,EAAE,cAAc,GACzB;IAAE,gBAAgB,EAAE,UAAU,CAAC;IAAC,eAAe,EAAE,MAAM,CAAC;IAAC,kBAAkB,EAAE,UAAU,CAAA;CAAE,CA8B3F;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAC5B,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,UAAU,EACnB,QAAQ,EAAE,MAAM,EAChB,gBAAgB,EAAE,UAAU,EAC5B,YAAY,EAAE,OAAO,EACrB,IAAI,EAAE,QAAQ,EACd,OAAO,EAAE,qBAAqB,EAC9B,QAAQ,EAAE,cAAc,GACzB;IAAE,OAAO,EAAE,UAAU,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,CA4B/C"}
@@ -0,0 +1,312 @@
1
+ /**
2
+ * QUIC packet protection (RFC 9001 §5).
3
+ *
4
+ * Applies and removes QUIC's two-layer packet protection:
5
+ *
6
+ * 1. Payload encryption (RFC 9001 §5.3): AEAD with quic_key + quic_iv.
7
+ * The per-record nonce is the XOR of the (zero-padded, big-endian)
8
+ * packet number into the static IV. The AAD is the unprotected header
9
+ * (everything up to and including the unencrypted packet number).
10
+ *
11
+ * 2. Header protection (RFC 9001 §5.4): the low bits of the first byte
12
+ * and the packet number are masked with a mask derived from
13
+ * AES-ECB(quic_hp, sample). The sample is 16 bytes of ciphertext
14
+ * starting 4 bytes into the protected payload.
15
+ *
16
+ * This module is pure wire-format logic — no I/O, no key management. It
17
+ * operates over @browsercore/crypto for the AEAD and AES-ECB primitives.
18
+ *
19
+ * Honest limitations:
20
+ * - Only AES-128-GCM, AES-256-GCM, and ChaCha20-Poly1305 are wired for
21
+ * payload AEAD. AES-128-CCM is defined by TLS 1.3 but never negotiated
22
+ * by QUIC v1, so it is rejected here.
23
+ * - 0-RTT, KeyUpdate, and key-phase-bit handling are out of scope for now
24
+ * — the connection layer passes the correct keys for the packet's phase.
25
+ */
26
+ import { QUIC_IV_LENGTH } from "../crypto/key-derivation.js";
27
+ import { concat, assertNever } from "../utils.js";
28
+ /** AEAD authentication tag length for every cipher QUIC v1 uses (bytes). */
29
+ const AEAD_TAG_LENGTH = 16;
30
+ /** Header-protection sample length (bytes) per RFC 9001 §5.4.1. */
31
+ const HP_SAMPLE_LENGTH = 16;
32
+ /** Offset into the protected payload where the sample starts (RFC 9001 §5.4.1). */
33
+ const HP_SAMPLE_OFFSET = 4;
34
+ /** The byte offsets into the AES-ECB output used for the header-protection mask. */
35
+ const HP_MASK_FIRST_BYTE_LEN = 1; // 5 bits (low 5 bits of the first mask byte)
36
+ const HP_MASK_PN_OFFSET = 0; // packet number mask starts at byte 0
37
+ /** Mask for the low 5 bits of the first header byte (header form, type, reserved, key-phase). */
38
+ const FIRST_BYTE_LOW5_MASK = 0x1f;
39
+ /** Mask for the low 4 bits of the first header byte (long header: form + fixed + type + pn-len). */
40
+ const FIRST_BYTE_LOW4_MASK = 0x0f;
41
+ /**
42
+ * Construct the per-packet AEAD nonce from the static IV and the packet number.
43
+ *
44
+ * The packet number is encoded as a zero-padded big-endian integer in an
45
+ * 8-byte buffer, then XOR'd into the IV's low 8 bytes (RFC 9001 §5.3).
46
+ *
47
+ * @param iv Static IV (12 bytes) derived from the traffic secret.
48
+ * @param pn Packet number (0..2^62-1).
49
+ */
50
+ export function constructNonce(iv, pn) {
51
+ if (iv.length !== QUIC_IV_LENGTH) {
52
+ throw new RangeError(`QUIC IV must be ${QUIC_IV_LENGTH} bytes, got ${iv.length}`);
53
+ }
54
+ const nonce = Uint8Array.from(iv);
55
+ // XOR the packet number (big-endian, low 8 bytes) into the IV's low bytes.
56
+ let remaining = pn;
57
+ for (let i = nonce.length - 1; i >= nonce.length - 8 && remaining > 0n; i--) {
58
+ const byte = nonce[i];
59
+ if (byte === undefined) {
60
+ throw new RangeError(`nonce index ${i} out of bounds (iv length ${iv.length})`);
61
+ }
62
+ nonce[i] = byte ^ Number(remaining & 0xffn);
63
+ remaining >>= 8n;
64
+ }
65
+ return nonce;
66
+ }
67
+ /**
68
+ * Map a QUIC AEAD algorithm name to the @browsercore/crypto symmetric cipher id.
69
+ * QUIC uses "CHACHA20_POLY1305" (underscores, matching @browsercore/tls);
70
+ * @browsercore/crypto expects "ChaCha20-Poly1305" (hyphens).
71
+ */
72
+ function aeadAlgorithmId(aead) {
73
+ switch (aead) {
74
+ case "AES-128-GCM":
75
+ return "AES-128-GCM";
76
+ case "AES-256-GCM":
77
+ return "AES-256-GCM";
78
+ case "CHACHA20-POLY1305":
79
+ return "ChaCha20-Poly1305";
80
+ default:
81
+ return assertNever(aead);
82
+ }
83
+ }
84
+ /**
85
+ * AEAD-encrypt a payload with the given key and nonce.
86
+ *
87
+ * @param aead AEAD algorithm (AES-128/256-GCM or ChaCha20-Poly1305).
88
+ * @param key AEAD key (16 bytes for AES-128, 32 bytes for AES-256 / ChaCha20).
89
+ * @param nonce Per-packet nonce (12 bytes), computed via {@link constructNonce}.
90
+ * @param plaintext The unprotected payload bytes.
91
+ * @param aad The unprotected header (everything up to and including the
92
+ * unencrypted packet number).
93
+ * @param provider Cryptographic provider.
94
+ * @returns Ciphertext with the 16-byte authentication tag appended.
95
+ */
96
+ export function encryptPayload(aead, key, nonce, plaintext, aad, provider) {
97
+ return aeadEncrypt(aead, key, nonce, plaintext, aad, provider);
98
+ }
99
+ /**
100
+ * AEAD-decrypt a payload with the given key and nonce.
101
+ *
102
+ * @param aead AEAD algorithm.
103
+ * @param key AEAD key.
104
+ * @param nonce Per-packet nonce (12 bytes), computed via {@link constructNonce}.
105
+ * @param ciphertext Ciphertext with the 16-byte authentication tag appended.
106
+ * @param aad The unprotected header (everything up to and including the
107
+ * packet number after header protection removal).
108
+ * @param provider Cryptographic provider.
109
+ * @returns Decrypted plaintext.
110
+ * @throws Error on authentication failure.
111
+ */
112
+ export function decryptPayload(aead, key, nonce, ciphertext, aad, provider) {
113
+ return aeadDecrypt(aead, key, nonce, ciphertext, aad, provider);
114
+ }
115
+ /** AEAD-encrypt with a concrete @browsercore/crypto cipher id. */
116
+ function aeadEncrypt(algorithm, key, nonce, plaintext, aad, provider) {
117
+ const id = aeadAlgorithmId(algorithm);
118
+ switch (id) {
119
+ case "AES-128-GCM":
120
+ return provider.aes128GcmEncrypt(key, nonce, plaintext, aad);
121
+ case "AES-256-GCM":
122
+ return provider.aes256GcmEncrypt(key, nonce, plaintext, aad);
123
+ case "ChaCha20-Poly1305":
124
+ return provider.chacha20Poly1305Encrypt(key, nonce, plaintext, aad);
125
+ default:
126
+ return assertNever(id);
127
+ }
128
+ }
129
+ /** AEAD-decrypt with a concrete @browsercore/crypto cipher id. */
130
+ function aeadDecrypt(algorithm, key, nonce, ciphertext, aad, provider) {
131
+ const id = aeadAlgorithmId(algorithm);
132
+ switch (id) {
133
+ case "AES-128-GCM":
134
+ return provider.aes128GcmDecrypt(key, nonce, ciphertext, aad);
135
+ case "AES-256-GCM":
136
+ return provider.aes256GcmDecrypt(key, nonce, ciphertext, aad);
137
+ case "ChaCha20-Poly1305":
138
+ return provider.chacha20Poly1305Decrypt(key, nonce, ciphertext, aad);
139
+ default:
140
+ return assertNever(id);
141
+ }
142
+ }
143
+ /**
144
+ * Derive the header-protection mask for a given sample (RFC 9001 §5.4.1).
145
+ *
146
+ * mask = AES-ECB(hp_key, sample)
147
+ *
148
+ * The sample is the 16 bytes of (protected) payload starting 4 bytes past
149
+ * the start of the packet number. For short headers (1-RTT), the first byte
150
+ * of the mask is used to protect the low 5 bits of the first header byte
151
+ * (after header protection is removed, these encode the header form + key
152
+ * phase + reserved bits). For long headers, the first byte protects the low
153
+ * 4 bits (header form + fixed + long packet type). Bytes 1–5 of the mask
154
+ * protect the encoded packet number.
155
+ *
156
+ * @param hpKey Header-protection key (16 bytes for AES-128, 32 bytes for AES-256).
157
+ * @param sample 16-byte sample from the protected payload.
158
+ * @param provider Cryptographic provider.
159
+ * @returns A 16-byte mask; the low 5 (short header) or 4 (long header) bits of
160
+ * byte 0 protect the first header byte, bytes 1–5 protect the packet
161
+ * number.
162
+ */
163
+ export function computeHeaderProtectionMask(hpKey, sample, provider) {
164
+ if (sample.length < HP_SAMPLE_LENGTH) {
165
+ throw new RangeError(`Header-protection sample must be at least ${HP_SAMPLE_LENGTH} bytes, got ${sample.length}`);
166
+ }
167
+ const block = sample.subarray(0, HP_SAMPLE_LENGTH);
168
+ return provider.aesEcbEncrypt(hpKey, block);
169
+ }
170
+ /**
171
+ * Apply header protection to the first byte and packet number (RFC 9001 §5.4).
172
+ *
173
+ * @param firstByte The first header byte (already serialized).
174
+ * @param packetNumber The packet number to protect.
175
+ * @param pnLength Number of bytes the packet number occupies on the wire (1–4).
176
+ * @param mask Header-protection mask (from {@link computeHeaderProtectionMask}).
177
+ * @param isLongHeader Whether this is a long header (affects which low bits are masked).
178
+ * @returns The masked first byte and masked packet-number bytes.
179
+ */
180
+ export function applyHeaderProtection(firstByte, packetNumber, pnLength, mask, isLongHeader) {
181
+ const firstMaskByte = mask[HP_MASK_PN_OFFSET];
182
+ if (firstMaskByte === undefined) {
183
+ throw new RangeError("header-protection mask is empty");
184
+ }
185
+ const firstByteMask = isLongHeader ? FIRST_BYTE_LOW4_MASK : FIRST_BYTE_LOW5_MASK;
186
+ const maskedFirstByte = firstByte ^ (firstMaskByte & firstByteMask);
187
+ // The packet number is masked with the low (pnLength * 8) bits of mask bytes 1..5.
188
+ const pnBytes = encodeBigInt(packetNumber, pnLength);
189
+ const maskedPnBytes = new Uint8Array(pnLength);
190
+ for (let i = 0; i < pnLength; i++) {
191
+ const maskByte = mask[HP_MASK_FIRST_BYTE_LEN + i];
192
+ if (maskByte === undefined) {
193
+ throw new RangeError(`header-protection mask too short for ${pnLength}-byte packet number`);
194
+ }
195
+ const pnByte = pnBytes[i];
196
+ if (pnByte === undefined) {
197
+ throw new RangeError(`packet number byte ${i} out of bounds`);
198
+ }
199
+ maskedPnBytes[i] = pnByte ^ maskByte;
200
+ }
201
+ return { firstByte: maskedFirstByte, packetNumberBytes: maskedPnBytes };
202
+ }
203
+ /**
204
+ * Remove header protection from the first byte and packet number (RFC 9001 §5.4).
205
+ *
206
+ * @param firstByte The first header byte (as received, still masked).
207
+ * @param pnBytes The masked packet-number bytes (as received).
208
+ * @param pnLength Number of bytes the packet number occupies (1–4).
209
+ * @param mask Header-protection mask (from {@link computeHeaderProtectionMask}).
210
+ * @param isLongHeader Whether this is a long header.
211
+ * @returns The unmasked first byte and packet number.
212
+ */
213
+ export function removeHeaderProtection(firstByte, pnBytes, pnLength, mask, isLongHeader) {
214
+ const firstMaskByte = mask[HP_MASK_PN_OFFSET];
215
+ if (firstMaskByte === undefined) {
216
+ throw new RangeError("header-protection mask is empty");
217
+ }
218
+ const firstByteMask = isLongHeader ? FIRST_BYTE_LOW4_MASK : FIRST_BYTE_LOW5_MASK;
219
+ const unmaskedFirstByte = firstByte ^ (firstMaskByte & firstByteMask);
220
+ const pnLengthActual = Math.min(pnLength, pnBytes.length);
221
+ let packetNumber = 0n;
222
+ for (let i = 0; i < pnLengthActual; i++) {
223
+ const maskByte = mask[HP_MASK_FIRST_BYTE_LEN + i];
224
+ const pnByte = pnBytes[i];
225
+ if (maskByte === undefined || pnByte === undefined) {
226
+ throw new RangeError(`header-protection mask / pn byte ${i} out of bounds`);
227
+ }
228
+ packetNumber = (packetNumber << 8n) | BigInt(pnByte ^ maskByte);
229
+ }
230
+ return { firstByte: unmaskedFirstByte, packetNumber };
231
+ }
232
+ /**
233
+ * Protect a QUIC packet payload: AEAD-encrypt the payload and apply header
234
+ * protection to the first byte + packet number (RFC 9001 §5.3, §5.4).
235
+ *
236
+ * @param payload The unprotected frame payload.
237
+ * @param packetNumber The packet number to assign.
238
+ * @param pnLength Number of bytes the packet number occupies on the wire (1–4).
239
+ * @param firstByte The first header byte (already serialized, unprotected).
240
+ * @param aead AEAD algorithm.
241
+ * @param secrets QUIC protection secrets (key, iv, hp) for this direction + phase.
242
+ * @param isLongHeader Whether this is a long header.
243
+ * @param provider Cryptographic provider.
244
+ * @returns The AEAD-protected payload (with tag appended) — the caller prepends
245
+ * the masked first byte and masked packet number to form the full packet.
246
+ */
247
+ export function protectPayload(payload, packetNumber, pnLength, firstByte, aead, secrets, isLongHeader, provider) {
248
+ // 1. AEAD-encrypt the payload with the header as AAD.
249
+ const nonce = constructNonce(secrets.iv, packetNumber);
250
+ // The AAD is the first byte + packet number bytes (before header protection).
251
+ // Header protection is applied AFTER encryption, so the AAD is the raw header.
252
+ const rawPnBytes = encodeBigInt(packetNumber, pnLength);
253
+ const aad = concat(new Uint8Array([firstByte]), rawPnBytes);
254
+ const protectedPayload = encryptPayload(aead, secrets.key, nonce, payload, aad, provider);
255
+ // 2. Sample the protected payload to derive the header-protection mask.
256
+ const sampleStart = HP_SAMPLE_OFFSET;
257
+ const sampleEnd = sampleStart + HP_SAMPLE_LENGTH;
258
+ if (protectedPayload.length < sampleEnd) {
259
+ throw new RangeError(`Protected payload too short for header-protection sample (need ${sampleEnd}, got ${protectedPayload.length})`);
260
+ }
261
+ const sample = protectedPayload.subarray(sampleStart, sampleEnd);
262
+ const mask = computeHeaderProtectionMask(secrets.hp, sample, provider);
263
+ // 3. Apply header protection to the first byte + packet number.
264
+ const { firstByte: maskedFirstByte, packetNumberBytes: maskedPacketNumber } = applyHeaderProtection(firstByte, packetNumber, pnLength, mask, isLongHeader);
265
+ return { protectedPayload, maskedFirstByte, maskedPacketNumber };
266
+ }
267
+ /**
268
+ * Unprotect a QUIC packet payload: remove header protection, then AEAD-decrypt.
269
+ *
270
+ * @param firstByte The first header byte (as received, still masked).
271
+ * @param pnBytes The masked packet-number bytes (as received).
272
+ * @param pnLength Number of bytes the packet number occupies (1–4).
273
+ * @param protectedPayload The AEAD-protected payload (with tag appended).
274
+ * @param isLongHeader Whether this is a long header.
275
+ * @param aead AEAD algorithm.
276
+ * @param secrets QUIC protection secrets (key, iv, hp) for this direction + phase.
277
+ * @param provider Cryptographic provider.
278
+ * @returns The decrypted frame payload and the decoded packet number.
279
+ */
280
+ export function unprotectPayload(firstByte, pnBytes, pnLength, protectedPayload, isLongHeader, aead, secrets, provider) {
281
+ // 1. Sample the protected payload to derive the header-protection mask.
282
+ const sampleStart = HP_SAMPLE_OFFSET;
283
+ const sampleEnd = sampleStart + HP_SAMPLE_LENGTH;
284
+ if (protectedPayload.length < sampleEnd) {
285
+ throw new RangeError(`Protected payload too short for header-protection sample (need ${sampleEnd}, got ${protectedPayload.length})`);
286
+ }
287
+ const sample = protectedPayload.subarray(sampleStart, sampleEnd);
288
+ const mask = computeHeaderProtectionMask(secrets.hp, sample, provider);
289
+ // 2. Remove header protection to recover the first byte + packet number.
290
+ const { firstByte: unmaskedFirstByte, packetNumber } = removeHeaderProtection(firstByte, pnBytes, pnLength, mask, isLongHeader);
291
+ // 3. AEAD-decrypt the payload with the unprotected header as AAD.
292
+ const nonce = constructNonce(secrets.iv, packetNumber);
293
+ const rawPnBytes = encodeBigInt(packetNumber, pnLength);
294
+ const aad = concat(new Uint8Array([unmaskedFirstByte]), rawPnBytes);
295
+ const payload = decryptPayload(aead, secrets.key, nonce, protectedPayload, aad, provider);
296
+ return { payload, packetNumber };
297
+ }
298
+ void AEAD_TAG_LENGTH;
299
+ /** Encode a non-negative BigInt as a big-endian byte array of the given length. */
300
+ function encodeBigInt(value, length) {
301
+ if (value < 0n) {
302
+ throw new RangeError(`Cannot encode negative value as unsigned: ${value}`);
303
+ }
304
+ const bytes = new Uint8Array(length);
305
+ let remaining = value;
306
+ for (let i = length - 1; i >= 0; i--) {
307
+ bytes[i] = Number(remaining & 0xffn);
308
+ remaining >>= 8n;
309
+ }
310
+ return bytes;
311
+ }
312
+ //# sourceMappingURL=packet-protection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"packet-protection.js","sourceRoot":"","sources":["../../src/packet/packet-protection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAGH,OAAO,EAAE,cAAc,EAA8B,MAAM,6BAA6B,CAAC;AACzF,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAUlD,4EAA4E;AAC5E,MAAM,eAAe,GAAG,EAAE,CAAC;AAE3B,mEAAmE;AACnE,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAE5B,mFAAmF;AACnF,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAE3B,oFAAoF;AACpF,MAAM,sBAAsB,GAAG,CAAC,CAAC,CAAC,6CAA6C;AAC/E,MAAM,iBAAiB,GAAG,CAAC,CAAC,CAAC,sCAAsC;AAEnE,iGAAiG;AACjG,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAClC,oGAAoG;AACpG,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAElC;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAAC,EAAc,EAAE,EAAU;IACrD,IAAI,EAAE,CAAC,MAAM,KAAK,cAAc,EAAE,CAAC;QAC/B,MAAM,IAAI,UAAU,CAAC,mBAAmB,cAAc,eAAe,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC;IACtF,CAAC;IACD,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,CAAC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AAC,YAAY,EAAE,QAAQ,CAAC,CAAC;IACxD,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,UAAU,CAAC,CAAC,iBAAiB,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC;IACpE,MAAM,OAAO,GAAG,cAAc,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,EAAE,KAAK,EAAE,gBAAgB,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IAE1F,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,CAAC;AACrC,CAAC;AAED,KAAK,eAAe,CAAC;AAErB,mFAAmF;AACnF,SAAS,YAAY,CAAC,KAAa,EAAE,MAAc;IAC/C,IAAI,KAAK,GAAG,EAAE,EAAE,CAAC;QACb,MAAM,IAAI,UAAU,CAAC,6CAA6C,KAAK,EAAE,CAAC,CAAC;IAC/E,CAAC;IACD,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IACrC,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,KAAK,IAAI,CAAC,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QACnC,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,SAAS,GAAG,KAAK,CAAC,CAAC;QACrC,SAAS,KAAK,EAAE,CAAC;IACrB,CAAC;IACD,OAAO,KAAK,CAAC;AACjB,CAAC"}
@@ -43,9 +43,9 @@ export interface ShortHeader {
43
43
  }
44
44
  export type PacketHeader = LongHeader | ShortHeader;
45
45
  /** Serialize a long header (without payload / packet number — caller appends those). */
46
- export declare function serializeLongHeader(type: LongPacketTypeValue, version: number, dcid: ConnectionId, scid: ConnectionId, packetNumberLength: number, extra?: Uint8Array): Uint8Array;
46
+ export declare function serializeLongHeader(type: LongPacketTypeValue, version: number, dcid: Uint8Array, scid: Uint8Array, packetNumberLength: number, extra?: Uint8Array): Uint8Array;
47
47
  /** Serialize a short (1-RTT) header (without payload / packet number). */
48
- export declare function serializeShortHeader(dcid: ConnectionId, packetNumberLength: number, spinBit: boolean, keyPhase: boolean): Uint8Array;
48
+ export declare function serializeShortHeader(dcid: Uint8Array, packetNumberLength: number, spinBit: boolean, keyPhase: boolean): Uint8Array;
49
49
  /**
50
50
  * Parse a packet header from the start of a buffer. Returns the header and the
51
51
  * number of header bytes consumed. Throws PacketParseError on malformed input.
@@ -1 +1 @@
1
- {"version":3,"file":"packet.d.ts","sourceRoot":"","sources":["../../src/packet/packet.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EACH,gBAAgB,EAChB,iBAAiB,EAEjB,KAAK,YAAY,EACjB,KAAK,mBAAmB,EAC3B,MAAM,aAAa,CAAC;AAIrB,OAAO,EACH,cAAc,EACd,KAAK,mBAAmB,GAC3B,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,kDAAkD;AAClD,MAAM,WAAW,UAAU;IACvB,QAAQ,CAAC,IAAI,EAAE,OAAO,gBAAgB,CAAC;IACvC,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;IACnC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,wDAAwD;IACxD,QAAQ,CAAC,kBAAkB,EAAE,MAAM,CAAC;IACpC,wEAAwE;IACxE,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;CACjC;AAED,2DAA2D;AAC3D,MAAM,WAAW,WAAW;IACxB,QAAQ,CAAC,IAAI,EAAE,OAAO,iBAAiB,CAAC;IACxC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,wDAAwD;IACxD,QAAQ,CAA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AAC,GAAG,EAAE,UAAU,GAAG,YAAY,CAc/D;AAwDD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAC9B,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,MAAM,GACpB,MAAM,CAcR;AAED,kFAAkF;AAClF,wBAAgB,kBAAkB,CAAC,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,CAG1E;AAED,6EAA6E;AAC7E,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAUxF"}
@@ -16,7 +16,7 @@
16
16
  * first byte (long) or the byte after the DCID (short) and tells the caller how
17
17
  * many bytes of packet number follow.
18
18
  */
19
- import { HEADER_FORM_LONG, HEADER_FORM_SHORT, LONG_PACKET_TYPE_MASK, } from "../types.js";
19
+ import { HEADER_FORM_LONG, HEADER_FORM_SHORT, LONG_PACKET_TYPE_MASK, makeConnectionId, } from "../types.js";
20
20
  import { decodeVarint, encodeVarint } from "../frame/varint.js";
21
21
  import { concatAll } from "../utils.js";
22
22
  export { LongPacketType, } from "../types.js";
@@ -86,7 +86,7 @@ function parseLongHeader(first, buf) {
86
86
  if (buf.length < 6 + dcidLen + 1) {
87
87
  throw new RangeError("Buffer too short for DCID");
88
88
  }
89
- const dcid = buf.subarray(6, 6 + dcidLen);
89
+ const dcid = makeConnectionId(buf.subarray(6, 6 + dcidLen));
90
90
  const scidLen = buf[6 + dcidLen];
91
91
  if (scidLen === undefined) {
92
92
  throw new RangeError("Buffer too short for SCID");
@@ -94,7 +94,7 @@ function parseLongHeader(first, buf) {
94
94
  if (buf.length < 6 + dcidLen + 1 + scidLen) {
95
95
  throw new RangeError("Buffer too short for SCID");
96
96
  }
97
- const scid = buf.subarray(6 + dcidLen + 1, 6 + dcidLen + 1 + scidLen);
97
+ const scid = makeConnectionId(buf.subarray(6 + dcidLen + 1, 6 + dcidLen + 1 + scidLen));
98
98
  const headerLength = 6 + dcidLen + 1 + scidLen;
99
99
  return {
100
100
  form: HEADER_FORM_LONG,
@@ -117,7 +117,7 @@ function parseShortHeader(first, _buf) {
117
117
  form: HEADER_FORM_SHORT,
118
118
  spinBit,
119
119
  keyPhase,
120
- dcid: new Uint8Array(0),
120
+ dcid: makeConnectionId(new Uint8Array(0)),
121
121
  packetNumberLength,
122
122
  headerLength: 1,
123
123
  };