kxco-post-quantum 1.3.0 → 1.4.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/CHANGELOG.md +374 -292
- package/CONFORMANCE.md +180 -0
- package/LICENSE +202 -202
- package/MIGRATION.md +143 -0
- package/README.md +215 -178
- package/SECURITY.md +41 -41
- package/THREAT-MODEL.md +194 -0
- package/package.json +124 -106
- package/src/derive.d.ts +20 -20
- package/src/derive.js +47 -47
- package/src/index.d.ts +13 -8
- package/src/index.js +24 -17
- package/src/kid.d.ts +21 -21
- package/src/kid.js +53 -53
- package/src/ml-dsa-87.d.ts +81 -0
- package/src/ml-dsa-87.js +127 -0
- package/src/ml-dsa.d.ts +78 -78
- package/src/ml-dsa.js +102 -102
- package/src/ml-kem-1024.d.ts +59 -0
- package/src/ml-kem-1024.js +90 -0
- package/src/ml-kem.d.ts +49 -49
- package/src/ml-kem.js +61 -61
- package/src/slh-dsa.d.ts +72 -72
- package/src/slh-dsa.js +106 -106
- package/src/webhook.d.ts +119 -119
- package/src/webhook.js +135 -135
package/src/webhook.js
CHANGED
|
@@ -1,135 +1,135 @@
|
|
|
1
|
-
// Hybrid HMAC + ML-DSA-65 webhook signing — the production pattern used by
|
|
2
|
-
// KXCO Bank, KnightsVault, and every product on the KXCO platform.
|
|
3
|
-
//
|
|
4
|
-
// Why hybrid?
|
|
5
|
-
// - HMAC-SHA-256 is symmetric and post-quantum secure as a MAC. Receivers
|
|
6
|
-
// who share the secret can verify offline with no library dependencies.
|
|
7
|
-
// - ML-DSA-65 adds NON-REPUDIATION: a receiver who only verifies the PQ
|
|
8
|
-
// signature can prove the message came from the holder of the platform
|
|
9
|
-
// private key — even if the HMAC secret has been leaked to a third party.
|
|
10
|
-
//
|
|
11
|
-
// Both signatures cover EXACTLY the same envelope: `${timestamp}.${rawBody}`.
|
|
12
|
-
//
|
|
13
|
-
// Isomorphic: HMAC and SHA-256 come from @noble/hashes, constant-time
|
|
14
|
-
// compare is a portable byte loop. No node:crypto dependency. Runs in Node
|
|
15
|
-
// and modern browsers.
|
|
16
|
-
|
|
17
|
-
import { hmac } from '@noble/hashes/hmac.js'
|
|
18
|
-
import { sha256 } from '@noble/hashes/sha2.js'
|
|
19
|
-
import { sign as mlDsaSign, verify as mlDsaVerify } from './ml-dsa.js'
|
|
20
|
-
|
|
21
|
-
const HAS_BUFFER = typeof Buffer !== 'undefined'
|
|
22
|
-
const enc = new TextEncoder()
|
|
23
|
-
|
|
24
|
-
function toBytes(input) {
|
|
25
|
-
if (input instanceof Uint8Array) return input
|
|
26
|
-
if (typeof input === 'string') return enc.encode(input)
|
|
27
|
-
throw new Error('expected Uint8Array or string')
|
|
28
|
-
}
|
|
29
|
-
function bytesToHex(bytes) {
|
|
30
|
-
let s = ''
|
|
31
|
-
for (let i = 0; i < bytes.length; i++) s += bytes[i].toString(16).padStart(2, '0')
|
|
32
|
-
return s
|
|
33
|
-
}
|
|
34
|
-
function wrap(bytes) {
|
|
35
|
-
return HAS_BUFFER ? Buffer.from(bytes) : bytes
|
|
36
|
-
}
|
|
37
|
-
// Portable constant-time string compare (Node + browser).
|
|
38
|
-
function constTimeEqualStrings(a, b) {
|
|
39
|
-
if (a.length !== b.length) return false
|
|
40
|
-
let diff = 0
|
|
41
|
-
for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i)
|
|
42
|
-
return diff === 0
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Build the canonical signed envelope: timestamp + "." + raw body string.
|
|
47
|
-
*/
|
|
48
|
-
export function envelope(timestamp, rawBody) {
|
|
49
|
-
const bodyBytes = toBytes(rawBody)
|
|
50
|
-
const prefixBytes = enc.encode(`${timestamp}.`)
|
|
51
|
-
const out = new Uint8Array(prefixBytes.length + bodyBytes.length)
|
|
52
|
-
out.set(prefixBytes, 0)
|
|
53
|
-
out.set(bodyBytes, prefixBytes.length)
|
|
54
|
-
return wrap(out)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Compute the hex HMAC-SHA-256 of the envelope using a shared secret.
|
|
59
|
-
*/
|
|
60
|
-
export function hmacHex(secret, timestamp, rawBody) {
|
|
61
|
-
const env = envelope(timestamp, rawBody)
|
|
62
|
-
const envBytes = env instanceof Uint8Array ? env : new Uint8Array(env)
|
|
63
|
-
const key = toBytes(secret)
|
|
64
|
-
return bytesToHex(hmac(sha256, key, envBytes))
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Verify the HMAC signature in constant time.
|
|
69
|
-
*/
|
|
70
|
-
export function verifyHmac(secret, timestamp, rawBody, sigHeader) {
|
|
71
|
-
const expected = 'sha256=' + hmacHex(secret, timestamp, rawBody)
|
|
72
|
-
const given = sigHeader.startsWith('sha256=') ? sigHeader : `sha256=${sigHeader}`
|
|
73
|
-
return constTimeEqualStrings(expected, given)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Produce the X-KXCO-PQ-Signature header value.
|
|
78
|
-
*/
|
|
79
|
-
export function pqSign(secretKey, timestamp, rawBody) {
|
|
80
|
-
const sig = mlDsaSign(secretKey, envelope(timestamp, rawBody))
|
|
81
|
-
return `ml-dsa-65=${sig}`
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Verify a hex ML-DSA-65 signature header.
|
|
86
|
-
*/
|
|
87
|
-
export function verifyPq(publicKey, timestamp, rawBody, sigHeader) {
|
|
88
|
-
const hex = sigHeader.startsWith('ml-dsa-65=')
|
|
89
|
-
? sigHeader.slice('ml-dsa-65='.length)
|
|
90
|
-
: sigHeader
|
|
91
|
-
return mlDsaVerify(publicKey, envelope(timestamp, rawBody), hex)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Sign a webhook delivery. Returns the full set of headers a sender should
|
|
96
|
-
* attach to the HTTP request.
|
|
97
|
-
*/
|
|
98
|
-
export function signDelivery({ rawBody, hmacSecret, pqSecretKey, pqKid, event, deliveryId }) {
|
|
99
|
-
const ts = Math.floor(Date.now() / 1000).toString()
|
|
100
|
-
const headers = {
|
|
101
|
-
'Content-Type': 'application/json',
|
|
102
|
-
'X-KXCO-Timestamp': ts,
|
|
103
|
-
'X-KXCO-Signature': 'sha256=' + hmacHex(hmacSecret, ts, rawBody),
|
|
104
|
-
'X-KXCO-PQ-Signature': pqSign(pqSecretKey, ts, rawBody),
|
|
105
|
-
'X-KXCO-PQ-Kid': pqKid,
|
|
106
|
-
}
|
|
107
|
-
if (event) headers['X-KXCO-Event'] = event
|
|
108
|
-
if (deliveryId) headers['X-KXCO-Delivery'] = deliveryId
|
|
109
|
-
return headers
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Verify a webhook delivery on the receiving side.
|
|
114
|
-
*/
|
|
115
|
-
export function verifyDelivery({ headers, rawBody, hmacSecret, pqPublicKey, pinnedKid, windowSeconds = 300 }) {
|
|
116
|
-
const ts = headers['x-kxco-timestamp']
|
|
117
|
-
const sigHmac = headers['x-kxco-signature']
|
|
118
|
-
const sigPq = headers['x-kxco-pq-signature']
|
|
119
|
-
const kid = headers['x-kxco-pq-kid']
|
|
120
|
-
|
|
121
|
-
const tsNum = parseInt(ts, 10)
|
|
122
|
-
const timestampOk = Number.isFinite(tsNum) &&
|
|
123
|
-
Math.abs(Date.now() / 1000 - tsNum) <= windowSeconds
|
|
124
|
-
|
|
125
|
-
const hmacOk = (hmacSecret && sigHmac && timestampOk)
|
|
126
|
-
? verifyHmac(hmacSecret, ts, rawBody, sigHmac)
|
|
127
|
-
: false
|
|
128
|
-
|
|
129
|
-
const kidOk = pinnedKid ? kid === pinnedKid : true
|
|
130
|
-
const pqOk = (pqPublicKey && sigPq && timestampOk && kidOk)
|
|
131
|
-
? verifyPq(pqPublicKey, ts, rawBody, sigPq)
|
|
132
|
-
: false
|
|
133
|
-
|
|
134
|
-
return { hmacOk, pqOk, timestampOk, kidOk }
|
|
135
|
-
}
|
|
1
|
+
// Hybrid HMAC + ML-DSA-65 webhook signing — the production pattern used by
|
|
2
|
+
// KXCO Bank, KnightsVault, and every product on the KXCO platform.
|
|
3
|
+
//
|
|
4
|
+
// Why hybrid?
|
|
5
|
+
// - HMAC-SHA-256 is symmetric and post-quantum secure as a MAC. Receivers
|
|
6
|
+
// who share the secret can verify offline with no library dependencies.
|
|
7
|
+
// - ML-DSA-65 adds NON-REPUDIATION: a receiver who only verifies the PQ
|
|
8
|
+
// signature can prove the message came from the holder of the platform
|
|
9
|
+
// private key — even if the HMAC secret has been leaked to a third party.
|
|
10
|
+
//
|
|
11
|
+
// Both signatures cover EXACTLY the same envelope: `${timestamp}.${rawBody}`.
|
|
12
|
+
//
|
|
13
|
+
// Isomorphic: HMAC and SHA-256 come from @noble/hashes, constant-time
|
|
14
|
+
// compare is a portable byte loop. No node:crypto dependency. Runs in Node
|
|
15
|
+
// and modern browsers.
|
|
16
|
+
|
|
17
|
+
import { hmac } from '@noble/hashes/hmac.js'
|
|
18
|
+
import { sha256 } from '@noble/hashes/sha2.js'
|
|
19
|
+
import { sign as mlDsaSign, verify as mlDsaVerify } from './ml-dsa.js'
|
|
20
|
+
|
|
21
|
+
const HAS_BUFFER = typeof Buffer !== 'undefined'
|
|
22
|
+
const enc = new TextEncoder()
|
|
23
|
+
|
|
24
|
+
function toBytes(input) {
|
|
25
|
+
if (input instanceof Uint8Array) return input
|
|
26
|
+
if (typeof input === 'string') return enc.encode(input)
|
|
27
|
+
throw new Error('expected Uint8Array or string')
|
|
28
|
+
}
|
|
29
|
+
function bytesToHex(bytes) {
|
|
30
|
+
let s = ''
|
|
31
|
+
for (let i = 0; i < bytes.length; i++) s += bytes[i].toString(16).padStart(2, '0')
|
|
32
|
+
return s
|
|
33
|
+
}
|
|
34
|
+
function wrap(bytes) {
|
|
35
|
+
return HAS_BUFFER ? Buffer.from(bytes) : bytes
|
|
36
|
+
}
|
|
37
|
+
// Portable constant-time string compare (Node + browser).
|
|
38
|
+
function constTimeEqualStrings(a, b) {
|
|
39
|
+
if (a.length !== b.length) return false
|
|
40
|
+
let diff = 0
|
|
41
|
+
for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i)
|
|
42
|
+
return diff === 0
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Build the canonical signed envelope: timestamp + "." + raw body string.
|
|
47
|
+
*/
|
|
48
|
+
export function envelope(timestamp, rawBody) {
|
|
49
|
+
const bodyBytes = toBytes(rawBody)
|
|
50
|
+
const prefixBytes = enc.encode(`${timestamp}.`)
|
|
51
|
+
const out = new Uint8Array(prefixBytes.length + bodyBytes.length)
|
|
52
|
+
out.set(prefixBytes, 0)
|
|
53
|
+
out.set(bodyBytes, prefixBytes.length)
|
|
54
|
+
return wrap(out)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Compute the hex HMAC-SHA-256 of the envelope using a shared secret.
|
|
59
|
+
*/
|
|
60
|
+
export function hmacHex(secret, timestamp, rawBody) {
|
|
61
|
+
const env = envelope(timestamp, rawBody)
|
|
62
|
+
const envBytes = env instanceof Uint8Array ? env : new Uint8Array(env)
|
|
63
|
+
const key = toBytes(secret)
|
|
64
|
+
return bytesToHex(hmac(sha256, key, envBytes))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Verify the HMAC signature in constant time.
|
|
69
|
+
*/
|
|
70
|
+
export function verifyHmac(secret, timestamp, rawBody, sigHeader) {
|
|
71
|
+
const expected = 'sha256=' + hmacHex(secret, timestamp, rawBody)
|
|
72
|
+
const given = sigHeader.startsWith('sha256=') ? sigHeader : `sha256=${sigHeader}`
|
|
73
|
+
return constTimeEqualStrings(expected, given)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Produce the X-KXCO-PQ-Signature header value.
|
|
78
|
+
*/
|
|
79
|
+
export function pqSign(secretKey, timestamp, rawBody) {
|
|
80
|
+
const sig = mlDsaSign(secretKey, envelope(timestamp, rawBody))
|
|
81
|
+
return `ml-dsa-65=${sig}`
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Verify a hex ML-DSA-65 signature header.
|
|
86
|
+
*/
|
|
87
|
+
export function verifyPq(publicKey, timestamp, rawBody, sigHeader) {
|
|
88
|
+
const hex = sigHeader.startsWith('ml-dsa-65=')
|
|
89
|
+
? sigHeader.slice('ml-dsa-65='.length)
|
|
90
|
+
: sigHeader
|
|
91
|
+
return mlDsaVerify(publicKey, envelope(timestamp, rawBody), hex)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Sign a webhook delivery. Returns the full set of headers a sender should
|
|
96
|
+
* attach to the HTTP request.
|
|
97
|
+
*/
|
|
98
|
+
export function signDelivery({ rawBody, hmacSecret, pqSecretKey, pqKid, event, deliveryId }) {
|
|
99
|
+
const ts = Math.floor(Date.now() / 1000).toString()
|
|
100
|
+
const headers = {
|
|
101
|
+
'Content-Type': 'application/json',
|
|
102
|
+
'X-KXCO-Timestamp': ts,
|
|
103
|
+
'X-KXCO-Signature': 'sha256=' + hmacHex(hmacSecret, ts, rawBody),
|
|
104
|
+
'X-KXCO-PQ-Signature': pqSign(pqSecretKey, ts, rawBody),
|
|
105
|
+
'X-KXCO-PQ-Kid': pqKid,
|
|
106
|
+
}
|
|
107
|
+
if (event) headers['X-KXCO-Event'] = event
|
|
108
|
+
if (deliveryId) headers['X-KXCO-Delivery'] = deliveryId
|
|
109
|
+
return headers
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Verify a webhook delivery on the receiving side.
|
|
114
|
+
*/
|
|
115
|
+
export function verifyDelivery({ headers, rawBody, hmacSecret, pqPublicKey, pinnedKid, windowSeconds = 300 }) {
|
|
116
|
+
const ts = headers['x-kxco-timestamp']
|
|
117
|
+
const sigHmac = headers['x-kxco-signature']
|
|
118
|
+
const sigPq = headers['x-kxco-pq-signature']
|
|
119
|
+
const kid = headers['x-kxco-pq-kid']
|
|
120
|
+
|
|
121
|
+
const tsNum = parseInt(ts, 10)
|
|
122
|
+
const timestampOk = Number.isFinite(tsNum) &&
|
|
123
|
+
Math.abs(Date.now() / 1000 - tsNum) <= windowSeconds
|
|
124
|
+
|
|
125
|
+
const hmacOk = (hmacSecret && sigHmac && timestampOk)
|
|
126
|
+
? verifyHmac(hmacSecret, ts, rawBody, sigHmac)
|
|
127
|
+
: false
|
|
128
|
+
|
|
129
|
+
const kidOk = pinnedKid ? kid === pinnedKid : true
|
|
130
|
+
const pqOk = (pqPublicKey && sigPq && timestampOk && kidOk)
|
|
131
|
+
? verifyPq(pqPublicKey, ts, rawBody, sigPq)
|
|
132
|
+
: false
|
|
133
|
+
|
|
134
|
+
return { hmacOk, pqOk, timestampOk, kidOk }
|
|
135
|
+
}
|