kxco-post-quantum 1.1.11 → 1.2.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.
package/src/ml-kem.js CHANGED
@@ -1,61 +1,61 @@
1
- // ML-KEM-768 helpers (NIST FIPS 203, Kyber768).
2
- //
3
- // Module-lattice key encapsulation. Security Category 3 (≈ AES-192). Public
4
- // key 1184 bytes, ciphertext 1088 bytes, shared secret 32 bytes. Resistant
5
- // to attacks by quantum computers.
6
- //
7
- // Isomorphic: works in Node and modern browsers. Returns Buffer on Node
8
- // (backwards compatible), Uint8Array in browsers.
9
-
10
- import { ml_kem768 } from '@noble/post-quantum/ml-kem'
11
- import { deriveSeed } from './derive.js'
12
-
13
- const HAS_BUFFER = typeof Buffer !== 'undefined'
14
-
15
- function wrap(bytes) {
16
- return HAS_BUFFER ? Buffer.from(bytes) : bytes
17
- }
18
-
19
- /**
20
- * Generate an ML-KEM-768 keypair from a master + domain-separation info.
21
- *
22
- * @returns {{ publicKey: Buffer|Uint8Array, secretKey: Buffer|Uint8Array }}
23
- */
24
- export function keypairFromMaster(master, info = 'ml-kem-768-v1') {
25
- const seed = deriveSeed(master, info, 64)
26
- const seedU8 = seed instanceof Uint8Array ? seed : new Uint8Array(seed)
27
- const k = ml_kem768.keygen(seedU8)
28
- return {
29
- publicKey: wrap(k.publicKey),
30
- secretKey: wrap(k.secretKey),
31
- }
32
- }
33
-
34
- /**
35
- * Encapsulate a shared secret to the recipient's public key.
36
- *
37
- * @param {Buffer|Uint8Array} publicKey
38
- * @returns {{ ciphertext: Buffer|Uint8Array, cipherText: Buffer|Uint8Array, sharedSecret: Buffer|Uint8Array }}
39
- */
40
- export function encapsulate(publicKey) {
41
- const r = ml_kem768.encapsulate(publicKey)
42
- const ct = wrap(r.cipherText ?? r.ciphertext)
43
- return {
44
- ciphertext: ct,
45
- cipherText: ct,
46
- sharedSecret: wrap(r.sharedSecret),
47
- }
48
- }
49
-
50
- /**
51
- * Decapsulate: recover the shared secret from a ciphertext using the secret key.
52
- *
53
- * @param {Buffer|Uint8Array} ciphertext
54
- * @param {Buffer|Uint8Array} secretKey
55
- * @returns {Buffer|Uint8Array} shared secret (32 bytes)
56
- */
57
- export function decapsulate(ciphertext, secretKey) {
58
- return wrap(ml_kem768.decapsulate(ciphertext, secretKey))
59
- }
60
-
61
- export { ml_kem768 }
1
+ // ML-KEM-768 helpers (NIST FIPS 203, Kyber768).
2
+ //
3
+ // Module-lattice key encapsulation. Security Category 3 (≈ AES-192). Public
4
+ // key 1184 bytes, ciphertext 1088 bytes, shared secret 32 bytes. Resistant
5
+ // to attacks by quantum computers.
6
+ //
7
+ // Isomorphic: works in Node and modern browsers. Returns Buffer on Node
8
+ // (backwards compatible), Uint8Array in browsers.
9
+
10
+ import { ml_kem768 } from '@noble/post-quantum/ml-kem.js'
11
+ import { deriveSeed } from './derive.js'
12
+
13
+ const HAS_BUFFER = typeof Buffer !== 'undefined'
14
+
15
+ function wrap(bytes) {
16
+ return HAS_BUFFER ? Buffer.from(bytes) : bytes
17
+ }
18
+
19
+ /**
20
+ * Generate an ML-KEM-768 keypair from a master + domain-separation info.
21
+ *
22
+ * @returns {{ publicKey: Buffer|Uint8Array, secretKey: Buffer|Uint8Array }}
23
+ */
24
+ export function keypairFromMaster(master, info = 'ml-kem-768-v1') {
25
+ const seed = deriveSeed(master, info, 64)
26
+ const seedU8 = seed instanceof Uint8Array ? seed : new Uint8Array(seed)
27
+ const k = ml_kem768.keygen(seedU8)
28
+ return {
29
+ publicKey: wrap(k.publicKey),
30
+ secretKey: wrap(k.secretKey),
31
+ }
32
+ }
33
+
34
+ /**
35
+ * Encapsulate a shared secret to the recipient's public key.
36
+ *
37
+ * @param {Buffer|Uint8Array} publicKey
38
+ * @returns {{ ciphertext: Buffer|Uint8Array, cipherText: Buffer|Uint8Array, sharedSecret: Buffer|Uint8Array }}
39
+ */
40
+ export function encapsulate(publicKey) {
41
+ const r = ml_kem768.encapsulate(publicKey)
42
+ const ct = wrap(r.cipherText ?? r.ciphertext)
43
+ return {
44
+ ciphertext: ct,
45
+ cipherText: ct,
46
+ sharedSecret: wrap(r.sharedSecret),
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Decapsulate: recover the shared secret from a ciphertext using the secret key.
52
+ *
53
+ * @param {Buffer|Uint8Array} ciphertext
54
+ * @param {Buffer|Uint8Array} secretKey
55
+ * @returns {Buffer|Uint8Array} shared secret (32 bytes)
56
+ */
57
+ export function decapsulate(ciphertext, secretKey) {
58
+ return wrap(ml_kem768.decapsulate(ciphertext, secretKey))
59
+ }
60
+
61
+ export { ml_kem768 }
@@ -0,0 +1,46 @@
1
+ /// <reference types="node" />
2
+
3
+ export interface SlhDsaKeypair {
4
+ /** 48-byte public key */
5
+ publicKey: Buffer
6
+ /** 96-byte secret key */
7
+ secretKey: Buffer
8
+ }
9
+
10
+ /**
11
+ * Generate an SLH-DSA-SHA2-192s (NIST FIPS 205) keypair deterministically
12
+ * from a master + domain-separation info string.
13
+ *
14
+ * Same inputs always produce the same keypair — no state, no DB row.
15
+ * Security Category 3, matching ML-DSA-65.
16
+ */
17
+ export function keypairFromMaster(
18
+ master: Buffer | Uint8Array,
19
+ info?: string,
20
+ ): SlhDsaKeypair
21
+
22
+ /**
23
+ * Sign a message under an SLH-DSA-SHA2-192s secret key. Returns the signature
24
+ * as a hex string (16224 bytes = 32448 hex characters).
25
+ */
26
+ export function sign(
27
+ secretKey: Buffer | Uint8Array,
28
+ message: Buffer | string,
29
+ ): string
30
+
31
+ /**
32
+ * Verify a hex-encoded SLH-DSA-SHA2-192s signature against a public key +
33
+ * message. Returns `false` on any error (invalid hex, wrong length, mismatch).
34
+ */
35
+ export function verify(
36
+ publicKey: Buffer | Uint8Array,
37
+ message: Buffer | string,
38
+ sigHex: string,
39
+ ): boolean
40
+
41
+ /**
42
+ * Raw `@noble/post-quantum` SLH-DSA-SHA2-192s primitive, re-exported for
43
+ * callers who want the lower-level API. The wrapper functions above are
44
+ * recommended for production use.
45
+ */
46
+ export const slh_dsa_sha2_192s: typeof import('@noble/post-quantum/slh-dsa.js').slh_dsa_sha2_192s
package/src/slh-dsa.js ADDED
@@ -0,0 +1,88 @@
1
+ // SLH-DSA-SHA2-192s helpers (NIST FIPS 205, SPHINCS+).
2
+ //
3
+ // Stateless hash-based signatures. Security Category 3 (≈ AES-192), matching
4
+ // ML-DSA-65's security level. Public key 48 bytes, secret key 96 bytes,
5
+ // signature 16224 bytes. Security rests only on the SHA-2 hash function — no
6
+ // lattice or number-theoretic assumptions — which makes it the conservative
7
+ // hedge alongside ML-DSA-65.
8
+ //
9
+ // Tradeoff: signatures are ~5x larger than ML-DSA-65 (16224 vs 3309 bytes) and
10
+ // signing is slower. Use ML-DSA-65 as the default; reach for SLH-DSA when you
11
+ // want a signature whose security does not depend on lattice hardness.
12
+ //
13
+ // Isomorphic: works in Node and modern browsers. Returns Buffer on Node
14
+ // (consistent with ml-dsa.js), Uint8Array in browsers.
15
+
16
+ import { slh_dsa_sha2_192s } from '@noble/post-quantum/slh-dsa.js'
17
+ import { deriveSeed } from './derive.js'
18
+
19
+ const HAS_BUFFER = typeof Buffer !== 'undefined'
20
+ const enc = new TextEncoder()
21
+
22
+ function toBytes(input) {
23
+ if (input instanceof Uint8Array) return input
24
+ if (typeof input === 'string') return enc.encode(input)
25
+ throw new Error('expected Uint8Array or string')
26
+ }
27
+ function hexToBytes(hex) {
28
+ if (typeof hex !== 'string' || hex.length % 2) throw new Error('invalid hex')
29
+ const b = new Uint8Array(hex.length / 2)
30
+ for (let i = 0; i < b.length; i++) b[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16)
31
+ return b
32
+ }
33
+ function bytesToHex(bytes) {
34
+ let s = ''
35
+ for (let i = 0; i < bytes.length; i++) s += bytes[i].toString(16).padStart(2, '0')
36
+ return s
37
+ }
38
+ function wrap(bytes) {
39
+ return HAS_BUFFER ? Buffer.from(bytes) : bytes
40
+ }
41
+
42
+ // Seed length for SLH-DSA-SHA2-192s keygen (FIPS 205: SK.seed || SK.prf || PK.seed).
43
+ const SEED_BYTES = slh_dsa_sha2_192s.lengths.seed
44
+
45
+ /**
46
+ * Generate an SLH-DSA-SHA2-192s keypair from a master + domain-separation info.
47
+ *
48
+ * @returns {{ publicKey: Buffer|Uint8Array, secretKey: Buffer|Uint8Array }}
49
+ */
50
+ export function keypairFromMaster(master, info = 'slh-dsa-sha2-192s-v1') {
51
+ const seed = deriveSeed(master, info, SEED_BYTES)
52
+ const seedU8 = seed instanceof Uint8Array ? seed : new Uint8Array(seed)
53
+ const k = slh_dsa_sha2_192s.keygen(seedU8)
54
+ return {
55
+ publicKey: wrap(k.publicKey),
56
+ secretKey: wrap(k.secretKey),
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Sign a message. Returns the signature as a hex string.
62
+ *
63
+ * @param {Buffer|Uint8Array} secretKey
64
+ * @param {Buffer|Uint8Array|string} message
65
+ * @returns {string} hex-encoded signature (32448 chars)
66
+ */
67
+ export function sign(secretKey, message) {
68
+ const sig = slh_dsa_sha2_192s.sign(toBytes(message), secretKey)
69
+ return bytesToHex(sig)
70
+ }
71
+
72
+ /**
73
+ * Verify a hex-encoded signature.
74
+ *
75
+ * @param {Buffer|Uint8Array} publicKey
76
+ * @param {Buffer|Uint8Array|string} message
77
+ * @param {string} sigHex
78
+ * @returns {boolean}
79
+ */
80
+ export function verify(publicKey, message, sigHex) {
81
+ try {
82
+ return slh_dsa_sha2_192s.verify(hexToBytes(sigHex), toBytes(message), publicKey)
83
+ } catch {
84
+ return false
85
+ }
86
+ }
87
+
88
+ export { slh_dsa_sha2_192s }
package/src/webhook.d.ts CHANGED
@@ -1,119 +1,119 @@
1
- /// <reference types="node" />
2
-
3
- /**
4
- * Build the canonical signed envelope: `timestamp + "." + raw_body`.
5
- *
6
- * Receivers MUST construct the envelope from the timestamp header and the
7
- * RAW request body bytes as received. Re-serialising a parsed JSON object
8
- * will not produce the same bytes and signature verification will fail.
9
- */
10
- export function envelope(
11
- timestamp: string | number,
12
- rawBody: string | Buffer,
13
- ): Buffer
14
-
15
- /**
16
- * Compute the hex HMAC-SHA-256 of the envelope using the shared secret.
17
- * Returns the hex value WITHOUT the `sha256=` prefix.
18
- */
19
- export function hmacHex(
20
- secret: string | Buffer,
21
- timestamp: string | number,
22
- rawBody: string | Buffer,
23
- ): string
24
-
25
- /**
26
- * Constant-time verify of the X-KXCO-Signature header.
27
- * Accepts the header value with or without the `sha256=` prefix.
28
- */
29
- export function verifyHmac(
30
- secret: string | Buffer,
31
- timestamp: string | number,
32
- rawBody: string | Buffer,
33
- sigHeader: string,
34
- ): boolean
35
-
36
- /**
37
- * Produce the X-KXCO-PQ-Signature header value: the hex ML-DSA-65
38
- * signature over the envelope, prefixed with `ml-dsa-65=`.
39
- */
40
- export function pqSign(
41
- secretKey: Buffer | Uint8Array,
42
- timestamp: string | number,
43
- rawBody: string | Buffer,
44
- ): string
45
-
46
- /**
47
- * Verify a hex ML-DSA-65 signature header.
48
- * Accepts the value with or without the `ml-dsa-65=` prefix.
49
- */
50
- export function verifyPq(
51
- publicKey: Buffer | Uint8Array,
52
- timestamp: string | number,
53
- rawBody: string | Buffer,
54
- sigHeader: string,
55
- ): boolean
56
-
57
- export interface SignDeliveryArgs {
58
- /** The exact body bytes that will be transmitted */
59
- rawBody: string | Buffer
60
- /** Per-endpoint shared secret for HMAC */
61
- hmacSecret: string | Buffer
62
- /** Raw ML-DSA-65 secret key */
63
- pqSecretKey: Buffer | Uint8Array
64
- /** 16-hex kid fingerprint of the matching public key */
65
- pqKid: string
66
- /** Optional event name (eg. "payment.settled") */
67
- event?: string
68
- /** Optional delivery / idempotency identifier */
69
- deliveryId?: string
70
- }
71
-
72
- export interface SignDeliveryHeaders {
73
- 'Content-Type': 'application/json'
74
- 'X-KXCO-Timestamp': string
75
- 'X-KXCO-Signature': string // sha256=<hex>
76
- 'X-KXCO-PQ-Signature': string // ml-dsa-65=<hex>
77
- 'X-KXCO-PQ-Kid': string
78
- 'X-KXCO-Event'?: string
79
- 'X-KXCO-Delivery'?: string
80
- }
81
-
82
- /**
83
- * Sign a webhook delivery. Returns the full set of headers a sender
84
- * should attach to the HTTP request.
85
- *
86
- * The caller is responsible for sending `rawBody` byte-for-byte
87
- * unchanged. The receiver verifies against the bytes as transmitted.
88
- */
89
- export function signDelivery(args: SignDeliveryArgs): SignDeliveryHeaders
90
-
91
- export interface VerifyDeliveryArgs {
92
- /** HTTP headers with LOWERCASE keys */
93
- headers: Record<string, string | undefined>
94
- /** The EXACT request body bytes as received */
95
- rawBody: string | Buffer
96
- /** Optional: enable HMAC verification by providing the shared secret */
97
- hmacSecret?: string | Buffer
98
- /** Optional: enable PQ verification by providing the platform public key */
99
- pqPublicKey?: Buffer | Uint8Array
100
- /** Required when `pqPublicKey` is provided */
101
- pinnedKid?: string
102
- /** Replay-window in seconds. Default 300 (5 minutes). */
103
- windowSeconds?: number
104
- }
105
-
106
- export interface VerifyDeliveryResult {
107
- hmacOk: boolean
108
- pqOk: boolean
109
- timestampOk: boolean
110
- kidOk: boolean
111
- }
112
-
113
- /**
114
- * Verify a webhook delivery on the receiving side.
115
- *
116
- * Returns a breakdown of which predicates passed. A delivery is
117
- * acceptable when `(hmacOk || pqOk) && timestampOk && kidOk`.
118
- */
119
- export function verifyDelivery(args: VerifyDeliveryArgs): VerifyDeliveryResult
1
+ /// <reference types="node" />
2
+
3
+ /**
4
+ * Build the canonical signed envelope: `timestamp + "." + raw_body`.
5
+ *
6
+ * Receivers MUST construct the envelope from the timestamp header and the
7
+ * RAW request body bytes as received. Re-serialising a parsed JSON object
8
+ * will not produce the same bytes and signature verification will fail.
9
+ */
10
+ export function envelope(
11
+ timestamp: string | number,
12
+ rawBody: string | Buffer,
13
+ ): Buffer
14
+
15
+ /**
16
+ * Compute the hex HMAC-SHA-256 of the envelope using the shared secret.
17
+ * Returns the hex value WITHOUT the `sha256=` prefix.
18
+ */
19
+ export function hmacHex(
20
+ secret: string | Buffer,
21
+ timestamp: string | number,
22
+ rawBody: string | Buffer,
23
+ ): string
24
+
25
+ /**
26
+ * Constant-time verify of the X-KXCO-Signature header.
27
+ * Accepts the header value with or without the `sha256=` prefix.
28
+ */
29
+ export function verifyHmac(
30
+ secret: string | Buffer,
31
+ timestamp: string | number,
32
+ rawBody: string | Buffer,
33
+ sigHeader: string,
34
+ ): boolean
35
+
36
+ /**
37
+ * Produce the X-KXCO-PQ-Signature header value: the hex ML-DSA-65
38
+ * signature over the envelope, prefixed with `ml-dsa-65=`.
39
+ */
40
+ export function pqSign(
41
+ secretKey: Buffer | Uint8Array,
42
+ timestamp: string | number,
43
+ rawBody: string | Buffer,
44
+ ): string
45
+
46
+ /**
47
+ * Verify a hex ML-DSA-65 signature header.
48
+ * Accepts the value with or without the `ml-dsa-65=` prefix.
49
+ */
50
+ export function verifyPq(
51
+ publicKey: Buffer | Uint8Array,
52
+ timestamp: string | number,
53
+ rawBody: string | Buffer,
54
+ sigHeader: string,
55
+ ): boolean
56
+
57
+ export interface SignDeliveryArgs {
58
+ /** The exact body bytes that will be transmitted */
59
+ rawBody: string | Buffer
60
+ /** Per-endpoint shared secret for HMAC */
61
+ hmacSecret: string | Buffer
62
+ /** Raw ML-DSA-65 secret key */
63
+ pqSecretKey: Buffer | Uint8Array
64
+ /** 16-hex kid fingerprint of the matching public key */
65
+ pqKid: string
66
+ /** Optional event name (eg. "payment.settled") */
67
+ event?: string
68
+ /** Optional delivery / idempotency identifier */
69
+ deliveryId?: string
70
+ }
71
+
72
+ export interface SignDeliveryHeaders {
73
+ 'Content-Type': 'application/json'
74
+ 'X-KXCO-Timestamp': string
75
+ 'X-KXCO-Signature': string // sha256=<hex>
76
+ 'X-KXCO-PQ-Signature': string // ml-dsa-65=<hex>
77
+ 'X-KXCO-PQ-Kid': string
78
+ 'X-KXCO-Event'?: string
79
+ 'X-KXCO-Delivery'?: string
80
+ }
81
+
82
+ /**
83
+ * Sign a webhook delivery. Returns the full set of headers a sender
84
+ * should attach to the HTTP request.
85
+ *
86
+ * The caller is responsible for sending `rawBody` byte-for-byte
87
+ * unchanged. The receiver verifies against the bytes as transmitted.
88
+ */
89
+ export function signDelivery(args: SignDeliveryArgs): SignDeliveryHeaders
90
+
91
+ export interface VerifyDeliveryArgs {
92
+ /** HTTP headers with LOWERCASE keys */
93
+ headers: Record<string, string | undefined>
94
+ /** The EXACT request body bytes as received */
95
+ rawBody: string | Buffer
96
+ /** Optional: enable HMAC verification by providing the shared secret */
97
+ hmacSecret?: string | Buffer
98
+ /** Optional: enable PQ verification by providing the platform public key */
99
+ pqPublicKey?: Buffer | Uint8Array
100
+ /** Required when `pqPublicKey` is provided */
101
+ pinnedKid?: string
102
+ /** Replay-window in seconds. Default 300 (5 minutes). */
103
+ windowSeconds?: number
104
+ }
105
+
106
+ export interface VerifyDeliveryResult {
107
+ hmacOk: boolean
108
+ pqOk: boolean
109
+ timestampOk: boolean
110
+ kidOk: boolean
111
+ }
112
+
113
+ /**
114
+ * Verify a webhook delivery on the receiving side.
115
+ *
116
+ * Returns a breakdown of which predicates passed. A delivery is
117
+ * acceptable when `(hmacOk || pqOk) && timestampOk && kidOk`.
118
+ */
119
+ export function verifyDelivery(args: VerifyDeliveryArgs): VerifyDeliveryResult