hpke 1.1.4 → 1.1.6
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/README.md +3 -3
- package/index.d.ts +24 -6
- package/index.d.ts.map +1 -1
- package/index.js +263 -301
- package/index.ts +297 -335
- package/package.json +6 -4
package/index.ts
CHANGED
|
@@ -1519,22 +1519,23 @@ export function encode(string: string): Uint8Array {
|
|
|
1519
1519
|
// Each is a freshly-allocated Uint8Array held as a constant to avoid repeating
|
|
1520
1520
|
// the per-call `encode()` validation+allocation loop. These are only ever read
|
|
1521
1521
|
// (and copied into concat() outputs), never mutated.
|
|
1522
|
-
const L_HPKE_v1 = encode('HPKE-v1')
|
|
1523
|
-
const L_HPKE = encode('HPKE')
|
|
1524
|
-
const L_KEM = encode('KEM')
|
|
1525
|
-
const L_sec = encode('sec')
|
|
1526
|
-
const L_secret = encode('secret')
|
|
1527
|
-
const L_key = encode('key')
|
|
1528
|
-
const L_base_nonce = encode('base_nonce')
|
|
1529
|
-
const L_exp = encode('exp')
|
|
1530
|
-
const L_psk_id_hash = encode('psk_id_hash')
|
|
1531
|
-
const L_info_hash = encode('info_hash')
|
|
1532
|
-
const L_dkp_prk = encode('dkp_prk')
|
|
1533
|
-
const L_candidate = encode('candidate')
|
|
1534
|
-
const L_eae_prk = encode('eae_prk')
|
|
1535
|
-
const L_shared_secret = encode('shared_secret')
|
|
1536
|
-
const L_sk = encode('sk')
|
|
1537
|
-
const
|
|
1522
|
+
const L_HPKE_v1 = /* @__PURE__ */ encode('HPKE-v1')
|
|
1523
|
+
const L_HPKE = /* @__PURE__ */ encode('HPKE')
|
|
1524
|
+
const L_KEM = /* @__PURE__ */ encode('KEM')
|
|
1525
|
+
const L_sec = /* @__PURE__ */ encode('sec')
|
|
1526
|
+
const L_secret = /* @__PURE__ */ encode('secret')
|
|
1527
|
+
const L_key = /* @__PURE__ */ encode('key')
|
|
1528
|
+
const L_base_nonce = /* @__PURE__ */ encode('base_nonce')
|
|
1529
|
+
const L_exp = /* @__PURE__ */ encode('exp')
|
|
1530
|
+
const L_psk_id_hash = /* @__PURE__ */ encode('psk_id_hash')
|
|
1531
|
+
const L_info_hash = /* @__PURE__ */ encode('info_hash')
|
|
1532
|
+
const L_dkp_prk = /* @__PURE__ */ encode('dkp_prk')
|
|
1533
|
+
const L_candidate = /* @__PURE__ */ encode('candidate')
|
|
1534
|
+
const L_eae_prk = /* @__PURE__ */ encode('eae_prk')
|
|
1535
|
+
const L_shared_secret = /* @__PURE__ */ encode('shared_secret')
|
|
1536
|
+
const L_sk = /* @__PURE__ */ encode('sk')
|
|
1537
|
+
const L_public_key_recovery = /* @__PURE__ */ encode('HPKE public key recovery')
|
|
1538
|
+
const L_DeriveKeyPair = /* @__PURE__ */ encode('DeriveKeyPair')
|
|
1538
1539
|
|
|
1539
1540
|
function lengthPrefixed(x: Uint8Array): Uint8Array {
|
|
1540
1541
|
return concat(I2OSP(x.byteLength, 2), x)
|
|
@@ -2976,148 +2977,116 @@ async function CurveKeyFromD(
|
|
|
2976
2977
|
// KEM (Key Encapsulation Mechanism) - DHKEM NIST Curve Implementations
|
|
2977
2978
|
// ============================================================================
|
|
2978
2979
|
|
|
2979
|
-
interface ECPoint {
|
|
2980
|
-
x: bigint
|
|
2981
|
-
y: bigint
|
|
2982
|
-
}
|
|
2983
|
-
|
|
2984
|
-
// Jacobian projective coordinates: (X, Y, Z) represents affine (X/Z², Y/Z³)
|
|
2985
|
-
// Uses wNAF scalar multiplication - only one modular inverse at the very end.
|
|
2986
|
-
type JP = [bigint, bigint, bigint] // [X, Y, Z]
|
|
2987
|
-
|
|
2988
|
-
// Non-negative modular reduction
|
|
2989
|
-
function mod(a: bigint, p: bigint): bigint {
|
|
2990
|
-
const r = a % p
|
|
2991
|
-
return r < 0n ? r + p : r
|
|
2992
|
-
}
|
|
2993
|
-
|
|
2994
|
-
// Modular inverse using Extended Euclidean Algorithm
|
|
2995
|
-
function modInverse(a: bigint, m: bigint): bigint {
|
|
2996
|
-
a = ((a % m) + m) % m
|
|
2997
|
-
let [t, newT] = [0n, 1n]
|
|
2998
|
-
let [r, newR] = [m, a]
|
|
2999
|
-
|
|
3000
|
-
while (newR !== 0n) {
|
|
3001
|
-
const quotient = r / newR
|
|
3002
|
-
;[t, newT] = [newT, t - quotient * newT]
|
|
3003
|
-
;[r, newR] = [newR, r - quotient * newR]
|
|
3004
|
-
}
|
|
3005
|
-
|
|
3006
|
-
if (r > 1n) throw new Error('a is not invertible')
|
|
3007
|
-
if (t < 0n) t = t + m
|
|
3008
|
-
return t
|
|
3009
|
-
}
|
|
3010
|
-
|
|
3011
|
-
function jDouble(p: JP, P: bigint, a: bigint): JP {
|
|
3012
|
-
const [X, Y, Z] = p
|
|
3013
|
-
if (Y === 0n) return [1n, 1n, 0n]
|
|
3014
|
-
const Y2 = mod(Y * Y, P)
|
|
3015
|
-
const S = mod(4n * X * Y2, P)
|
|
3016
|
-
const Z2 = mod(Z * Z, P)
|
|
3017
|
-
const M = mod(3n * X * X + a * Z2 * Z2, P)
|
|
3018
|
-
const X3 = mod(M * M - 2n * S, P)
|
|
3019
|
-
return [X3, mod(M * (S - X3) - 8n * Y2 * Y2, P), mod(2n * Y * Z, P)]
|
|
3020
|
-
}
|
|
3021
|
-
|
|
3022
|
-
function jAdd(p: JP, q: JP, P: bigint, a: bigint): JP {
|
|
3023
|
-
if (p[2] === 0n) return q
|
|
3024
|
-
if (q[2] === 0n) return p
|
|
3025
|
-
const pZ2 = mod(p[2] * p[2], P)
|
|
3026
|
-
const qZ2 = mod(q[2] * q[2], P)
|
|
3027
|
-
const U1 = mod(p[0] * qZ2, P)
|
|
3028
|
-
const U2 = mod(q[0] * pZ2, P)
|
|
3029
|
-
const S1 = mod(p[1] * qZ2 * q[2], P)
|
|
3030
|
-
const S2 = mod(q[1] * pZ2 * p[2], P)
|
|
3031
|
-
if (U1 === U2) return S1 === S2 ? jDouble(p, P, a) : [1n, 1n, 0n]
|
|
3032
|
-
const H = mod(U2 - U1, P)
|
|
3033
|
-
const R = mod(S2 - S1, P)
|
|
3034
|
-
const H2 = mod(H * H, P)
|
|
3035
|
-
const H3 = mod(H * H2, P)
|
|
3036
|
-
const U1H2 = mod(U1 * H2, P)
|
|
3037
|
-
const X3 = mod(R * R - H3 - 2n * U1H2, P)
|
|
3038
|
-
return [X3, mod(R * (U1H2 - X3) - S1 * H3, P), mod(H * p[2] * q[2], P)]
|
|
3039
|
-
}
|
|
3040
|
-
|
|
3041
|
-
// Scalar multiplication using wNAF with Jacobian coordinates: k * G
|
|
3042
|
-
//
|
|
3043
|
-
// This is used to compute the public key from a private scalar for NIST curves so that
|
|
3044
|
-
// the private key can be imported via JWK. Importing via PKCS8 (which would avoid the need
|
|
3045
|
-
// for computing the public key) is not viable cross-browser:
|
|
3046
|
-
// - WebKit: https://bugs.webkit.org/show_bug.cgi?id=302707
|
|
3047
|
-
// - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=2000795
|
|
3048
|
-
function scalarMult(k: bigint, G: ECPoint, prime: bigint, a: bigint, order: bigint): ECPoint {
|
|
3049
|
-
if (k === 0n || k >= order) {
|
|
3050
|
-
throw new Error('Invalid scalar')
|
|
3051
|
-
}
|
|
3052
|
-
|
|
3053
|
-
// Precompute odd multiples: 1G, 3G, 5G, 7G, 9G, 11G, 13G, 15G
|
|
3054
|
-
const precomp: JP[] = new Array(8)
|
|
3055
|
-
const Gj: JP = [G.x, G.y, 1n]
|
|
3056
|
-
const G2 = jDouble(Gj, prime, a)
|
|
3057
|
-
precomp[0] = Gj
|
|
3058
|
-
for (let i = 1; i < 8; i++) precomp[i] = jAdd(precomp[i - 1]!, G2, prime, a)
|
|
3059
|
-
|
|
3060
|
-
// wNAF encoding (w=4)
|
|
3061
|
-
const naf: number[] = []
|
|
3062
|
-
let s = k
|
|
3063
|
-
while (s > 0n) {
|
|
3064
|
-
if (s & 1n) {
|
|
3065
|
-
let d = Number(s & 15n)
|
|
3066
|
-
if (d >= 8) d -= 16
|
|
3067
|
-
naf.push(d)
|
|
3068
|
-
s -= BigInt(d)
|
|
3069
|
-
} else {
|
|
3070
|
-
naf.push(0)
|
|
3071
|
-
}
|
|
3072
|
-
s >>= 1n
|
|
3073
|
-
}
|
|
3074
|
-
|
|
3075
|
-
let r: JP = [1n, 1n, 0n]
|
|
3076
|
-
for (let i = naf.length - 1; i >= 0; i--) {
|
|
3077
|
-
r = jDouble(r, prime, a)
|
|
3078
|
-
const d = naf[i]!
|
|
3079
|
-
if (d > 0) r = jAdd(r, precomp[(d - 1) >> 1]!, prime, a)
|
|
3080
|
-
else if (d < 0) {
|
|
3081
|
-
const t = precomp[(-d - 1) >> 1]!
|
|
3082
|
-
r = jAdd(r, [t[0], mod(-t[1], prime), t[2]], prime, a)
|
|
3083
|
-
}
|
|
3084
|
-
}
|
|
3085
|
-
|
|
3086
|
-
const zI = modInverse(r[2], prime)
|
|
3087
|
-
const zI2 = mod(zI * zI, prime)
|
|
3088
|
-
return { x: mod(r[0] * zI2, prime), y: mod(r[1] * zI2 * zI, prime) }
|
|
3089
|
-
}
|
|
3090
|
-
|
|
3091
2980
|
interface NistCurveConfig {
|
|
3092
2981
|
order: bigint
|
|
3093
2982
|
bitmask: number
|
|
3094
|
-
prime: bigint
|
|
3095
2983
|
Gx: bigint
|
|
3096
2984
|
Gy: bigint
|
|
2985
|
+
pkcs8: Uint8Array
|
|
2986
|
+
spki: Uint8Array
|
|
3097
2987
|
algorithm: EcKeyAlgorithm
|
|
3098
2988
|
Npk: number
|
|
3099
2989
|
Nsk: number
|
|
3100
2990
|
}
|
|
3101
2991
|
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
2992
|
+
async function importKeyNist(
|
|
2993
|
+
curve: NistCurveConfig,
|
|
2994
|
+
key: Uint8Array,
|
|
2995
|
+
name: string,
|
|
2996
|
+
extractable: boolean,
|
|
2997
|
+
includePublicKey: boolean,
|
|
2998
|
+
): Promise<{ privateKey: CryptoKey; publicKey?: CryptoKey }> {
|
|
2999
|
+
const d = OS2IP(key)
|
|
3000
|
+
if (d === 0n || d >= curve.order) {
|
|
3001
|
+
throw new Error('Invalid scalar')
|
|
3002
|
+
}
|
|
3107
3003
|
|
|
3108
|
-
|
|
3109
|
-
const
|
|
3110
|
-
|
|
3111
|
-
|
|
3004
|
+
// Copy before awaiting, including when key is a Node.js Buffer.
|
|
3005
|
+
const pkcs8 = concat(curve.pkcs8, key)
|
|
3006
|
+
return await subtle(async (c) => {
|
|
3007
|
+
async function fromJwk(jwk: JsonWebKey, privateKey?: CryptoKey) {
|
|
3008
|
+
privateKey ??= await c.importKey('jwk', jwk, curve.algorithm, extractable, ['deriveBits'])
|
|
3009
|
+
delete jwk.d
|
|
3010
|
+
return {
|
|
3011
|
+
privateKey,
|
|
3012
|
+
publicKey: includePublicKey
|
|
3013
|
+
? await c.importKey('jwk', jwk, curve.algorithm, true, [])
|
|
3014
|
+
: undefined,
|
|
3015
|
+
}
|
|
3016
|
+
}
|
|
3112
3017
|
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3018
|
+
let privateKey: CryptoKey | undefined
|
|
3019
|
+
// @ts-expect-error
|
|
3020
|
+
const nativePublicKey = typeof c.getPublicKey === 'function'
|
|
3021
|
+
try {
|
|
3022
|
+
privateKey = await c.importKey(
|
|
3023
|
+
'pkcs8',
|
|
3024
|
+
pkcs8 as BufferSource,
|
|
3025
|
+
curve.algorithm,
|
|
3026
|
+
nativePublicKey ? extractable : true,
|
|
3027
|
+
['deriveBits'],
|
|
3028
|
+
)
|
|
3029
|
+
if (nativePublicKey) {
|
|
3030
|
+
// Also establish that scalar-only import recovered a usable public point.
|
|
3031
|
+
// @ts-expect-error
|
|
3032
|
+
const recovered = await c.getPublicKey(privateKey, [])
|
|
3033
|
+
return { privateKey, publicKey: includePublicKey ? recovered : undefined }
|
|
3034
|
+
}
|
|
3035
|
+
const { x, y, d } = await c.exportKey('jwk', privateKey)
|
|
3036
|
+
return fromJwk(
|
|
3037
|
+
{ kty: 'EC', crv: curve.algorithm.namedCurve, x, y, d },
|
|
3038
|
+
extractable ? privateKey : undefined,
|
|
3039
|
+
)
|
|
3040
|
+
} catch (cause) {
|
|
3041
|
+
// WebKit requires public coordinates on import; Firefox cannot export them
|
|
3042
|
+
// from a scalar-only PKCS8 key. Other errors must not trigger recovery.
|
|
3043
|
+
// https://bugs.webkit.org/show_bug.cgi?id=302707
|
|
3044
|
+
// https://bugzilla.mozilla.org/show_bug.cgi?id=2000795
|
|
3045
|
+
if (
|
|
3046
|
+
!(cause instanceof DOMException) ||
|
|
3047
|
+
(cause.name !== 'DataError' && cause.name !== 'OperationError')
|
|
3048
|
+
) {
|
|
3049
|
+
throw cause
|
|
3050
|
+
}
|
|
3051
|
+
}
|
|
3052
|
+
|
|
3053
|
+
const generator: JsonWebKey = {
|
|
3054
|
+
kty: 'EC',
|
|
3055
|
+
crv: curve.algorithm.namedCurve,
|
|
3056
|
+
x: toB64u(bigIntToUint8Array(curve.Gx, curve.Nsk)),
|
|
3057
|
+
y: toB64u(bigIntToUint8Array(curve.Gy, curve.Nsk)),
|
|
3058
|
+
}
|
|
3059
|
+
const jwk = { ...generator, d: toB64u(slice(pkcs8, curve.pkcs8.length)) }
|
|
3060
|
+
const signingAlgorithm = { name: 'ECDSA', namedCurve: curve.algorithm.namedCurve }
|
|
3061
|
+
const signingKey = privateKey
|
|
3062
|
+
? await c.importKey('pkcs8', pkcs8 as BufferSource, signingAlgorithm, false, ['sign'])
|
|
3063
|
+
: await c.importKey('jwk', jwk, signingAlgorithm, false, ['sign'])
|
|
3064
|
+
privateKey ??= await c.importKey('jwk', jwk, curve.algorithm, false, ['deriveBits'])
|
|
3065
|
+
|
|
3066
|
+
// ECDH(d, G) gives x(dG). WebKit preserves the supplied generator coordinates
|
|
3067
|
+
// on export, so the temporary private keys must never escape this function.
|
|
3068
|
+
const generatorKey = await c.importKey('jwk', generator, curve.algorithm, true, [])
|
|
3069
|
+
const x = new Uint8Array(
|
|
3070
|
+
await c.deriveBits({ name: 'ECDH', public: generatorKey }, privateKey, curve.Nsk << 3),
|
|
3071
|
+
)
|
|
3072
|
+
|
|
3073
|
+
// Compressed SPKI import recovers each possible y. An ECDSA signature selects
|
|
3074
|
+
// the point corresponding to d, without performing EC arithmetic in JavaScript.
|
|
3075
|
+
const message = L_public_key_recovery as BufferSource
|
|
3076
|
+
const signatureAlgorithm = { name: 'ECDSA', hash: 'SHA-256' }
|
|
3077
|
+
const signature = await c.sign(signatureAlgorithm, signingKey, message)
|
|
3078
|
+
for (const prefix of [0x02, 0x03]) {
|
|
3079
|
+
const spki = concat(curve.spki, Uint8Array.of(prefix), x)
|
|
3080
|
+
const candidate = await c.importKey('spki', spki as BufferSource, signingAlgorithm, true, [
|
|
3081
|
+
'verify',
|
|
3082
|
+
])
|
|
3083
|
+
if (await c.verify(signatureAlgorithm, candidate, signature, message)) {
|
|
3084
|
+
const { x, y } = await c.exportKey('jwk', candidate)
|
|
3085
|
+
return fromJwk({ kty: 'EC', crv: generator.crv, d: jwk.d, x, y })
|
|
3086
|
+
}
|
|
3087
|
+
}
|
|
3088
|
+
throw new Error('Public key recovery failed')
|
|
3089
|
+
}, name)
|
|
3121
3090
|
}
|
|
3122
3091
|
|
|
3123
3092
|
async function DeserializePrivateKeyNist(
|
|
@@ -3125,15 +3094,7 @@ async function DeserializePrivateKeyNist(
|
|
|
3125
3094
|
key: Uint8Array,
|
|
3126
3095
|
extractable: boolean,
|
|
3127
3096
|
) {
|
|
3128
|
-
|
|
3129
|
-
const jwk = getPrivateJwkNist(this, d)
|
|
3130
|
-
|
|
3131
|
-
const privateKey = await subtle(
|
|
3132
|
-
(c) => c.importKey('jwk', jwk, this.algorithm, extractable, ['deriveBits']),
|
|
3133
|
-
this.name,
|
|
3134
|
-
)
|
|
3135
|
-
|
|
3136
|
-
return privateKey
|
|
3097
|
+
return (await importKeyNist(this, key, this.name, extractable, false)).privateKey
|
|
3137
3098
|
}
|
|
3138
3099
|
|
|
3139
3100
|
/** @see [DeriveKeyPair](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.1.3) */
|
|
@@ -3164,20 +3125,7 @@ async function GetKeyPairNist(
|
|
|
3164
3125
|
extractable: boolean,
|
|
3165
3126
|
name: string,
|
|
3166
3127
|
) {
|
|
3167
|
-
|
|
3168
|
-
|
|
3169
|
-
const privateKey = await subtle(
|
|
3170
|
-
(c) => c.importKey('jwk', jwk, curveConfig.algorithm, extractable, ['deriveBits']),
|
|
3171
|
-
name,
|
|
3172
|
-
)
|
|
3173
|
-
|
|
3174
|
-
delete jwk.d
|
|
3175
|
-
const publicKey = await subtle(
|
|
3176
|
-
(c) => c.importKey('jwk', jwk, curveConfig.algorithm, true, []),
|
|
3177
|
-
name,
|
|
3178
|
-
)
|
|
3179
|
-
|
|
3180
|
-
return { privateKey, publicKey }
|
|
3128
|
+
return (await importKeyNist(curveConfig, sk, name, extractable, true)) as CryptoKeyPair
|
|
3181
3129
|
}
|
|
3182
3130
|
|
|
3183
3131
|
// ============================================================================
|
|
@@ -3200,7 +3148,8 @@ const P256: NistCurveConfig = {
|
|
|
3200
3148
|
Nsk: 32,
|
|
3201
3149
|
order: 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n,
|
|
3202
3150
|
bitmask: 0xff,
|
|
3203
|
-
|
|
3151
|
+
pkcs8: /* @__PURE__ */ Uint8Array.of(0x30, 0x41, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04, 0x27, 0x30, 0x25, 0x02, 0x01, 0x01, 0x04, 0x20), // prettier-ignore
|
|
3152
|
+
spki: /* @__PURE__ */ Uint8Array.of(0x30, 0x39, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x22, 0x00), // prettier-ignore
|
|
3204
3153
|
Gx: 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296n,
|
|
3205
3154
|
Gy: 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5n,
|
|
3206
3155
|
}
|
|
@@ -3255,8 +3204,8 @@ const P384: NistCurveConfig = {
|
|
|
3255
3204
|
order:
|
|
3256
3205
|
0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973n,
|
|
3257
3206
|
bitmask: 0xff,
|
|
3258
|
-
|
|
3259
|
-
|
|
3207
|
+
pkcs8: /* @__PURE__ */ Uint8Array.of(0x30, 0x4e, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22, 0x04, 0x37, 0x30, 0x35, 0x02, 0x01, 0x01, 0x04, 0x30), // prettier-ignore
|
|
3208
|
+
spki: /* @__PURE__ */ Uint8Array.of(0x30, 0x46, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22, 0x03, 0x32, 0x00), // prettier-ignore
|
|
3260
3209
|
Gx: 0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7n,
|
|
3261
3210
|
Gy: 0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5fn,
|
|
3262
3211
|
}
|
|
@@ -3311,8 +3260,8 @@ const P521: NistCurveConfig = {
|
|
|
3311
3260
|
order:
|
|
3312
3261
|
0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409n,
|
|
3313
3262
|
bitmask: 0x01,
|
|
3314
|
-
|
|
3315
|
-
|
|
3263
|
+
pkcs8: /* @__PURE__ */ Uint8Array.of(0x30, 0x60, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23, 0x04, 0x49, 0x30, 0x47, 0x02, 0x01, 0x01, 0x04, 0x42), // prettier-ignore
|
|
3264
|
+
spki: /* @__PURE__ */ Uint8Array.of(0x30, 0x58, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23, 0x03, 0x44, 0x00), // prettier-ignore
|
|
3316
3265
|
Gx: 0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66n,
|
|
3317
3266
|
Gy: 0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650n,
|
|
3318
3267
|
}
|
|
@@ -3463,18 +3412,20 @@ export const KEM_DHKEM_X448_HKDF_SHA512: KEMFactory<CryptoKey> = function (): DH
|
|
|
3463
3412
|
}
|
|
3464
3413
|
|
|
3465
3414
|
// ============================================================================
|
|
3466
|
-
// KEM (Key Encapsulation Mechanism) -
|
|
3415
|
+
// KEM (Key Encapsulation Mechanism) - Direct KEM Types and Implementation
|
|
3467
3416
|
// ============================================================================
|
|
3468
3417
|
|
|
3469
|
-
|
|
3418
|
+
type KEMParams = Pick<KEM, 'id' | 'name' | 'Nsecret' | 'Nenc' | 'Npk' | 'Nsk'>
|
|
3419
|
+
|
|
3420
|
+
interface DirectKEM extends KEM<CryptoKey> {
|
|
3470
3421
|
readonly suite_id: Uint8Array
|
|
3471
3422
|
readonly algorithm: Readonly<KeyAlgorithm>
|
|
3472
3423
|
readonly kdf: KDF
|
|
3473
3424
|
}
|
|
3474
3425
|
|
|
3475
|
-
function
|
|
3426
|
+
function DIRECT_KEM_SHARED(): KEM_BASE<CryptoKey> {
|
|
3476
3427
|
return {
|
|
3477
|
-
async DeriveKeyPair(this:
|
|
3428
|
+
async DeriveKeyPair(this: DirectKEM, ikm, extractable) {
|
|
3478
3429
|
const dk = await LabeledDerive(
|
|
3479
3430
|
this.kdf,
|
|
3480
3431
|
this.suite_id,
|
|
@@ -3491,7 +3442,7 @@ function MLKEM_SHARED(): KEM_BASE<CryptoKey> {
|
|
|
3491
3442
|
|
|
3492
3443
|
return { privateKey, publicKey }
|
|
3493
3444
|
},
|
|
3494
|
-
async GenerateKeyPair(this:
|
|
3445
|
+
async GenerateKeyPair(this: DirectKEM, extractable) {
|
|
3495
3446
|
// @ts-expect-error
|
|
3496
3447
|
const usages: KeyUsage[] = ['encapsulateBits', 'decapsulateBits']
|
|
3497
3448
|
return (await subtle(
|
|
@@ -3499,14 +3450,14 @@ function MLKEM_SHARED(): KEM_BASE<CryptoKey> {
|
|
|
3499
3450
|
this.name,
|
|
3500
3451
|
)) as CryptoKeyPair
|
|
3501
3452
|
},
|
|
3502
|
-
async SerializePublicKey(this:
|
|
3453
|
+
async SerializePublicKey(this: DirectKEM, key) {
|
|
3503
3454
|
assertKeyAlgorithm(key, this.algorithm)
|
|
3504
3455
|
assertCryptoKey(key)
|
|
3505
3456
|
// @ts-expect-error
|
|
3506
3457
|
const format: Exclude<KeyFormat, 'jwk'> = 'raw-public'
|
|
3507
3458
|
return new Uint8Array(await subtle((c) => c.exportKey(format, key), this.name))
|
|
3508
3459
|
},
|
|
3509
|
-
async DeserializePublicKey(this:
|
|
3460
|
+
async DeserializePublicKey(this: DirectKEM, key) {
|
|
3510
3461
|
// @ts-expect-error
|
|
3511
3462
|
const format: Exclude<KeyFormat, 'jwk'> = 'raw-public'
|
|
3512
3463
|
// @ts-expect-error
|
|
@@ -3516,14 +3467,14 @@ function MLKEM_SHARED(): KEM_BASE<CryptoKey> {
|
|
|
3516
3467
|
this.name,
|
|
3517
3468
|
)
|
|
3518
3469
|
},
|
|
3519
|
-
async SerializePrivateKey(this:
|
|
3470
|
+
async SerializePrivateKey(this: DirectKEM, key) {
|
|
3520
3471
|
assertKeyAlgorithm(key, this.algorithm)
|
|
3521
3472
|
assertCryptoKey(key)
|
|
3522
3473
|
// @ts-expect-error
|
|
3523
3474
|
const format: Exclude<KeyFormat, 'jwk'> = 'raw-seed'
|
|
3524
3475
|
return new Uint8Array(await subtle((c) => c.exportKey(format, key), this.name))
|
|
3525
3476
|
},
|
|
3526
|
-
async DeserializePrivateKey(this:
|
|
3477
|
+
async DeserializePrivateKey(this: DirectKEM, key, extractable) {
|
|
3527
3478
|
// @ts-expect-error
|
|
3528
3479
|
const format: Exclude<KeyFormat, 'jwk'> = 'raw-seed'
|
|
3529
3480
|
// @ts-expect-error
|
|
@@ -3533,7 +3484,7 @@ function MLKEM_SHARED(): KEM_BASE<CryptoKey> {
|
|
|
3533
3484
|
this.name,
|
|
3534
3485
|
)
|
|
3535
3486
|
},
|
|
3536
|
-
async Encap(this:
|
|
3487
|
+
async Encap(this: DirectKEM, pkR) {
|
|
3537
3488
|
assertKeyAlgorithm(pkR, this.algorithm)
|
|
3538
3489
|
|
|
3539
3490
|
const { sharedKey, ciphertext } = (await subtle(
|
|
@@ -3544,7 +3495,7 @@ function MLKEM_SHARED(): KEM_BASE<CryptoKey> {
|
|
|
3544
3495
|
|
|
3545
3496
|
return { shared_secret: new Uint8Array(sharedKey), enc: new Uint8Array(ciphertext) }
|
|
3546
3497
|
},
|
|
3547
|
-
async Decap(this:
|
|
3498
|
+
async Decap(this: DirectKEM, enc, skR, _pkR) {
|
|
3548
3499
|
assertKeyAlgorithm(skR, this.algorithm)
|
|
3549
3500
|
return new Uint8Array(
|
|
3550
3501
|
await subtle(
|
|
@@ -3557,6 +3508,25 @@ function MLKEM_SHARED(): KEM_BASE<CryptoKey> {
|
|
|
3557
3508
|
}
|
|
3558
3509
|
}
|
|
3559
3510
|
|
|
3511
|
+
function createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk }: KEMParams): DirectKEM {
|
|
3512
|
+
const kdf = KDF_SHAKE256()
|
|
3513
|
+
// @ts-expect-error: so that NotSupportedError messages from kdf's subtle() are accurate
|
|
3514
|
+
kdf.name = name
|
|
3515
|
+
return {
|
|
3516
|
+
id,
|
|
3517
|
+
suite_id: concat(L_KEM, I2OSP(id, 2)),
|
|
3518
|
+
type: 'KEM',
|
|
3519
|
+
name,
|
|
3520
|
+
Nsecret,
|
|
3521
|
+
Nenc,
|
|
3522
|
+
Npk,
|
|
3523
|
+
Nsk,
|
|
3524
|
+
algorithm: { name },
|
|
3525
|
+
kdf,
|
|
3526
|
+
...DIRECT_KEM_SHARED(),
|
|
3527
|
+
}
|
|
3528
|
+
}
|
|
3529
|
+
|
|
3560
3530
|
// ============================================================================
|
|
3561
3531
|
// KEM (Key Encapsulation Mechanism) - ML-KEM Suite Exports
|
|
3562
3532
|
// ============================================================================
|
|
@@ -3584,25 +3554,14 @@ function MLKEM_SHARED(): KEM_BASE<CryptoKey> {
|
|
|
3584
3554
|
* @group KEM Algorithms
|
|
3585
3555
|
* @see [HPKE-PQ KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-3)
|
|
3586
3556
|
*/
|
|
3587
|
-
export const KEM_ML_KEM_512: KEMFactory<CryptoKey> = function ():
|
|
3557
|
+
export const KEM_ML_KEM_512: KEMFactory<CryptoKey> = function (): DirectKEM {
|
|
3588
3558
|
const id = 0x0040
|
|
3589
3559
|
const name = 'ML-KEM-512'
|
|
3590
|
-
const
|
|
3591
|
-
|
|
3592
|
-
|
|
3593
|
-
|
|
3594
|
-
|
|
3595
|
-
suite_id: concat(L_KEM, I2OSP(id, 2)),
|
|
3596
|
-
type: 'KEM',
|
|
3597
|
-
name,
|
|
3598
|
-
Nsecret: 32,
|
|
3599
|
-
Nenc: 768,
|
|
3600
|
-
Npk: 800,
|
|
3601
|
-
Nsk: 64,
|
|
3602
|
-
algorithm: { name: 'ML-KEM-512' },
|
|
3603
|
-
kdf,
|
|
3604
|
-
...MLKEM_SHARED(),
|
|
3605
|
-
}
|
|
3560
|
+
const Nsecret = 32
|
|
3561
|
+
const Nenc = 768
|
|
3562
|
+
const Npk = 800
|
|
3563
|
+
const Nsk = 64
|
|
3564
|
+
return createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk })
|
|
3606
3565
|
}
|
|
3607
3566
|
|
|
3608
3567
|
/**
|
|
@@ -3624,25 +3583,14 @@ export const KEM_ML_KEM_512: KEMFactory<CryptoKey> = function (): MLKEM {
|
|
|
3624
3583
|
* @group KEM Algorithms
|
|
3625
3584
|
* @see [HPKE-PQ KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-3)
|
|
3626
3585
|
*/
|
|
3627
|
-
export const KEM_ML_KEM_768: KEMFactory<CryptoKey> = function ():
|
|
3586
|
+
export const KEM_ML_KEM_768: KEMFactory<CryptoKey> = function (): DirectKEM {
|
|
3628
3587
|
const id = 0x0041
|
|
3629
3588
|
const name = 'ML-KEM-768'
|
|
3630
|
-
const
|
|
3631
|
-
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
|
|
3635
|
-
suite_id: concat(L_KEM, I2OSP(id, 2)),
|
|
3636
|
-
type: 'KEM',
|
|
3637
|
-
name,
|
|
3638
|
-
Nsecret: 32,
|
|
3639
|
-
Nenc: 1088,
|
|
3640
|
-
Npk: 1184,
|
|
3641
|
-
Nsk: 64,
|
|
3642
|
-
algorithm: { name: 'ML-KEM-768' },
|
|
3643
|
-
kdf,
|
|
3644
|
-
...MLKEM_SHARED(),
|
|
3645
|
-
}
|
|
3589
|
+
const Nsecret = 32
|
|
3590
|
+
const Nenc = 1088
|
|
3591
|
+
const Npk = 1184
|
|
3592
|
+
const Nsk = 64
|
|
3593
|
+
return createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk })
|
|
3646
3594
|
}
|
|
3647
3595
|
|
|
3648
3596
|
/**
|
|
@@ -3664,25 +3612,14 @@ export const KEM_ML_KEM_768: KEMFactory<CryptoKey> = function (): MLKEM {
|
|
|
3664
3612
|
* @group KEM Algorithms
|
|
3665
3613
|
* @see [HPKE-PQ KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-3)
|
|
3666
3614
|
*/
|
|
3667
|
-
export const KEM_ML_KEM_1024: KEMFactory<CryptoKey> = function ():
|
|
3615
|
+
export const KEM_ML_KEM_1024: KEMFactory<CryptoKey> = function (): DirectKEM {
|
|
3668
3616
|
const id = 0x0042
|
|
3669
3617
|
const name = 'ML-KEM-1024'
|
|
3670
|
-
const
|
|
3671
|
-
|
|
3672
|
-
|
|
3673
|
-
|
|
3674
|
-
|
|
3675
|
-
suite_id: concat(L_KEM, I2OSP(id, 2)),
|
|
3676
|
-
type: 'KEM',
|
|
3677
|
-
name,
|
|
3678
|
-
Nsecret: 32,
|
|
3679
|
-
Nenc: 1568,
|
|
3680
|
-
Npk: 1568,
|
|
3681
|
-
Nsk: 64,
|
|
3682
|
-
algorithm: { name: 'ML-KEM-1024' },
|
|
3683
|
-
kdf,
|
|
3684
|
-
...MLKEM_SHARED(),
|
|
3685
|
-
}
|
|
3618
|
+
const Nsecret = 32
|
|
3619
|
+
const Nenc = 1568
|
|
3620
|
+
const Npk = 1568
|
|
3621
|
+
const Nsk = 64
|
|
3622
|
+
return createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk })
|
|
3686
3623
|
}
|
|
3687
3624
|
|
|
3688
3625
|
interface WebCryptoAEAD extends AEAD {
|
|
@@ -3856,7 +3793,7 @@ export const AEAD_ChaCha20Poly1305: AEADFactory = function AEAD_ChaCha20Poly1305
|
|
|
3856
3793
|
}
|
|
3857
3794
|
|
|
3858
3795
|
// ============================================================================
|
|
3859
|
-
// KEM (Key Encapsulation Mechanism) - Hybrid KEM Types and Implementation
|
|
3796
|
+
// KEM (Key Encapsulation Mechanism) - Composite Hybrid KEM Types and Implementation
|
|
3860
3797
|
// ============================================================================
|
|
3861
3798
|
|
|
3862
3799
|
/* c8 ignore next 5 */
|
|
@@ -3865,7 +3802,7 @@ const InvalidInvocation = (_: typeof priv) => {
|
|
|
3865
3802
|
throw new Error('invalid invocation')
|
|
3866
3803
|
}
|
|
3867
3804
|
}
|
|
3868
|
-
const priv = Symbol()
|
|
3805
|
+
const priv = /* @__PURE__ */ Symbol()
|
|
3869
3806
|
class HybridKey implements Key {
|
|
3870
3807
|
#algorithm: KeyAlgorithm
|
|
3871
3808
|
#type: 'public' | 'private'
|
|
@@ -3956,7 +3893,7 @@ function split(N1: number, N2: number, x: Uint8Array): [Uint8Array, Uint8Array]
|
|
|
3956
3893
|
return [x1, x2]
|
|
3957
3894
|
}
|
|
3958
3895
|
|
|
3959
|
-
function RandomScalarNist(t:
|
|
3896
|
+
function RandomScalarNist(t: CompositeHybridKEM['t'], seed: Uint8Array): Uint8Array {
|
|
3960
3897
|
let sk_bigint = 0n
|
|
3961
3898
|
let start = 0
|
|
3962
3899
|
let end = t.Nscalar!
|
|
@@ -3974,69 +3911,69 @@ function RandomScalarNist(t: HybridKEM['t'], seed: Uint8Array): Uint8Array {
|
|
|
3974
3911
|
}
|
|
3975
3912
|
|
|
3976
3913
|
/** @see [expandDecapsKeyG](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hybrid-kems-12.html#section-5.1.1) */
|
|
3977
|
-
async function expandDecapsKeyG(
|
|
3978
|
-
const Nout =
|
|
3914
|
+
async function expandDecapsKeyG(kem: CompositeHybridKEM, seed: Uint8Array) {
|
|
3915
|
+
const Nout = kem.pq.Nseed + kem.t.Nseed
|
|
3979
3916
|
const bits = Nout << 3
|
|
3980
3917
|
// @ts-expect-error
|
|
3981
3918
|
const algorithm: CShakeParams = { name: 'cSHAKE256', length: bits, outputLength: bits }
|
|
3982
|
-
const seed_full = await subtle((c) => c.digest(algorithm, seed as BufferSource),
|
|
3919
|
+
const seed_full = await subtle((c) => c.digest(algorithm, seed as BufferSource), kem.name)
|
|
3983
3920
|
|
|
3984
|
-
const [seed_PQ, seed_T] = split(
|
|
3921
|
+
const [seed_PQ, seed_T] = split(kem.pq.Nseed, kem.t.Nseed, new Uint8Array(seed_full))
|
|
3985
3922
|
|
|
3986
3923
|
// @ts-expect-error
|
|
3987
3924
|
const format: Exclude<KeyFormat, 'jwk'> = 'raw-seed'
|
|
3988
3925
|
// @ts-expect-error
|
|
3989
3926
|
const usages: [KeyUsage, KeyUsage] = ['decapsulateBits', 'encapsulateBits']
|
|
3990
3927
|
const dk_PQ = await subtle(
|
|
3991
|
-
(c) => c.importKey(format, seed_PQ as BufferSource,
|
|
3992
|
-
|
|
3928
|
+
(c) => c.importKey(format, seed_PQ as BufferSource, kem.pq.algorithm, true, [usages[0]]),
|
|
3929
|
+
kem.name,
|
|
3993
3930
|
)
|
|
3994
|
-
const ek_PQ = await getPublicKey(
|
|
3931
|
+
const ek_PQ = await getPublicKey(kem.name, dk_PQ, [usages[1]])
|
|
3995
3932
|
|
|
3996
|
-
const sk =
|
|
3997
|
-
const { privateKey: dk_T, publicKey: ek_T } = await
|
|
3933
|
+
const sk = kem.t.RandomScalar?.(seed_T) ?? seed_T
|
|
3934
|
+
const { privateKey: dk_T, publicKey: ek_T } = await kem.t.GetKeyPair(sk)
|
|
3998
3935
|
|
|
3999
3936
|
return { ek_PQ, ek_T, dk_PQ, dk_T }
|
|
4000
3937
|
}
|
|
4001
3938
|
|
|
4002
3939
|
/** @see [C2PRICombiner](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hybrid-kems-12.html#section-5.1.3) */
|
|
4003
3940
|
async function C2PRICombiner(
|
|
4004
|
-
|
|
3941
|
+
kem: CompositeHybridKEM,
|
|
4005
3942
|
ss_PQ: Uint8Array,
|
|
4006
3943
|
ss_T: Uint8Array,
|
|
4007
3944
|
ct_T: Uint8Array,
|
|
4008
3945
|
_ek_T: CryptoKey,
|
|
4009
3946
|
label: Uint8Array,
|
|
4010
3947
|
): Promise<Uint8Array> {
|
|
4011
|
-
const ek_T = new Uint8Array(await subtle((c) => c.exportKey('raw', _ek_T),
|
|
3948
|
+
const ek_T = new Uint8Array(await subtle((c) => c.exportKey('raw', _ek_T), kem.name))
|
|
4012
3949
|
const data = concat(ss_PQ, ss_T, ct_T, ek_T, label) as BufferSource
|
|
4013
|
-
return new Uint8Array(await subtle((c) => c.digest('SHA3-256', data),
|
|
3950
|
+
return new Uint8Array(await subtle((c) => c.digest('SHA3-256', data), kem.name))
|
|
4014
3951
|
}
|
|
4015
3952
|
|
|
4016
3953
|
/** @see [prepareEncapsG](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hybrid-kems-12.html#section-5.1.1) */
|
|
4017
3954
|
async function prepareEncapsG(
|
|
4018
|
-
|
|
3955
|
+
kem: CompositeHybridKEM,
|
|
4019
3956
|
ek_PQ: CryptoKey,
|
|
4020
3957
|
ek_T: CryptoKey,
|
|
4021
3958
|
): Promise<[Uint8Array, Uint8Array, Uint8Array, Uint8Array]> {
|
|
4022
3959
|
const res = (await subtle(
|
|
4023
3960
|
// @ts-expect-error
|
|
4024
|
-
(c) => c.encapsulateBits(
|
|
4025
|
-
|
|
3961
|
+
(c) => c.encapsulateBits(kem.pq.algorithm, ek_PQ),
|
|
3962
|
+
kem.name,
|
|
4026
3963
|
)) as { sharedKey: ArrayBuffer; ciphertext: ArrayBuffer }
|
|
4027
3964
|
const ss_PQ = new Uint8Array(res.sharedKey)
|
|
4028
3965
|
const ct_PQ = new Uint8Array(res.ciphertext)
|
|
4029
3966
|
|
|
4030
3967
|
const { privateKey: sk_E, publicKey } = (await subtle(
|
|
4031
|
-
(c) => c.generateKey(
|
|
4032
|
-
|
|
3968
|
+
(c) => c.generateKey(kem.t.algorithm, true, ['deriveBits']),
|
|
3969
|
+
kem.name,
|
|
4033
3970
|
)) as CryptoKeyPair
|
|
4034
|
-
const ct_T = new Uint8Array(await subtle((c) => c.exportKey('raw', publicKey),
|
|
3971
|
+
const ct_T = new Uint8Array(await subtle((c) => c.exportKey('raw', publicKey), kem.name))
|
|
4035
3972
|
|
|
4036
3973
|
const ss_T = new Uint8Array(
|
|
4037
3974
|
await subtle(
|
|
4038
|
-
(c) => c.deriveBits({ name:
|
|
4039
|
-
|
|
3975
|
+
(c) => c.deriveBits({ name: kem.t.algorithm.name, public: ek_T }, sk_E, kem.t.Nss << 3),
|
|
3976
|
+
kem.name,
|
|
4040
3977
|
),
|
|
4041
3978
|
)
|
|
4042
3979
|
checkNotAllZeros(ss_T)
|
|
@@ -4046,7 +3983,7 @@ async function prepareEncapsG(
|
|
|
4046
3983
|
|
|
4047
3984
|
/** @see [prepareDecapsG](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hybrid-kems-12.html#section-5.1.1) */
|
|
4048
3985
|
async function prepareDecapsG(
|
|
4049
|
-
|
|
3986
|
+
kem: CompositeHybridKEM,
|
|
4050
3987
|
dk_PQ: CryptoKey,
|
|
4051
3988
|
dk_T: CryptoKey,
|
|
4052
3989
|
ct_PQ: Uint8Array,
|
|
@@ -4055,20 +3992,20 @@ async function prepareDecapsG(
|
|
|
4055
3992
|
const ss_PQ = new Uint8Array(
|
|
4056
3993
|
await subtle(
|
|
4057
3994
|
// @ts-expect-error
|
|
4058
|
-
(c) => c.decapsulateBits(
|
|
4059
|
-
|
|
3995
|
+
(c) => c.decapsulateBits(kem.pq.algorithm, dk_PQ, ct_PQ),
|
|
3996
|
+
kem.name,
|
|
4060
3997
|
),
|
|
4061
3998
|
)
|
|
4062
3999
|
|
|
4063
4000
|
const pub = await subtle(
|
|
4064
|
-
(c) => c.importKey('raw', ct_T as BufferSource,
|
|
4065
|
-
|
|
4001
|
+
(c) => c.importKey('raw', ct_T as BufferSource, kem.t.algorithm, true, []),
|
|
4002
|
+
kem.name,
|
|
4066
4003
|
)
|
|
4067
4004
|
|
|
4068
4005
|
const ss_T = new Uint8Array(
|
|
4069
4006
|
await subtle(
|
|
4070
|
-
(c) => c.deriveBits({ name:
|
|
4071
|
-
|
|
4007
|
+
(c) => c.deriveBits({ name: kem.t.algorithm.name, public: pub }, dk_T, kem.t.Nss << 3),
|
|
4008
|
+
kem.name,
|
|
4072
4009
|
),
|
|
4073
4010
|
)
|
|
4074
4011
|
checkNotAllZeros(ss_T)
|
|
@@ -4076,7 +4013,7 @@ async function prepareDecapsG(
|
|
|
4076
4013
|
return [ss_PQ, ss_T]
|
|
4077
4014
|
}
|
|
4078
4015
|
|
|
4079
|
-
interface
|
|
4016
|
+
interface CompositeHybridKEM extends KEM<HybridKey> {
|
|
4080
4017
|
readonly suite_id: Uint8Array
|
|
4081
4018
|
readonly kdf: KDF
|
|
4082
4019
|
readonly algorithm: KeyAlgorithm
|
|
@@ -4096,18 +4033,20 @@ interface HybridKEM extends KEM<HybridKey> {
|
|
|
4096
4033
|
readonly Nsk: number
|
|
4097
4034
|
readonly Nscalar?: number
|
|
4098
4035
|
readonly GetKeyPair: (
|
|
4099
|
-
this:
|
|
4036
|
+
this: CompositeHybridKEM['t'],
|
|
4100
4037
|
sk: Uint8Array,
|
|
4101
4038
|
) => Promise<{ privateKey: CryptoKey; publicKey: CryptoKey }>
|
|
4102
4039
|
readonly order?: bigint
|
|
4103
|
-
readonly RandomScalar?: (this:
|
|
4040
|
+
readonly RandomScalar?: (this: CompositeHybridKEM['t'], seed: Uint8Array) => Uint8Array
|
|
4104
4041
|
}
|
|
4105
4042
|
}
|
|
4106
4043
|
|
|
4107
|
-
|
|
4044
|
+
type CompositeHybridKEMParams = KEMParams & Pick<CompositeHybridKEM, 'pq' | 't' | 'label'>
|
|
4045
|
+
|
|
4046
|
+
function COMPOSITE_HYBRID_KEM_SHARED(): KEM_BASE<HybridKey> {
|
|
4108
4047
|
Object.freeze(HybridKey.prototype)
|
|
4109
4048
|
return {
|
|
4110
|
-
async DeriveKeyPair(this:
|
|
4049
|
+
async DeriveKeyPair(this: CompositeHybridKEM, ikm: Uint8Array, extractable) {
|
|
4111
4050
|
const seed = await LabeledDerive(
|
|
4112
4051
|
this.kdf,
|
|
4113
4052
|
this.suite_id,
|
|
@@ -4133,10 +4072,10 @@ function PQTKEM_SHARED(): KEM_BASE<HybridKey> {
|
|
|
4133
4072
|
|
|
4134
4073
|
return { privateKey, publicKey }
|
|
4135
4074
|
},
|
|
4136
|
-
async GenerateKeyPair(this:
|
|
4075
|
+
async GenerateKeyPair(this: CompositeHybridKEM, extractable) {
|
|
4137
4076
|
return await this.DeriveKeyPair(crypto.getRandomValues(new Uint8Array(32)), extractable)
|
|
4138
4077
|
},
|
|
4139
|
-
async SerializePublicKey(this:
|
|
4078
|
+
async SerializePublicKey(this: CompositeHybridKEM, key) {
|
|
4140
4079
|
assertKeyAlgorithm(key, this.algorithm)
|
|
4141
4080
|
HybridKey.validate(key, true)
|
|
4142
4081
|
// @ts-expect-error
|
|
@@ -4150,7 +4089,7 @@ function PQTKEM_SHARED(): KEM_BASE<HybridKey> {
|
|
|
4150
4089
|
|
|
4151
4090
|
return concat(ek_PQ, ek_T)
|
|
4152
4091
|
},
|
|
4153
|
-
async DeserializePublicKey(this:
|
|
4092
|
+
async DeserializePublicKey(this: CompositeHybridKEM, key) {
|
|
4154
4093
|
// @ts-expect-error
|
|
4155
4094
|
const format: Exclude<KeyFormat, 'jwk'> = 'raw-public'
|
|
4156
4095
|
// @ts-expect-error
|
|
@@ -4164,13 +4103,13 @@ function PQTKEM_SHARED(): KEM_BASE<HybridKey> {
|
|
|
4164
4103
|
|
|
4165
4104
|
return new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
4166
4105
|
},
|
|
4167
|
-
async SerializePrivateKey(this:
|
|
4106
|
+
async SerializePrivateKey(this: CompositeHybridKEM, key) {
|
|
4168
4107
|
assertKeyAlgorithm(key, this.algorithm)
|
|
4169
4108
|
HybridKey.validate(key, true)
|
|
4170
4109
|
|
|
4171
4110
|
return key.getSeed(priv)
|
|
4172
4111
|
},
|
|
4173
|
-
async DeserializePrivateKey(this:
|
|
4112
|
+
async DeserializePrivateKey(this: CompositeHybridKEM, key, extractable) {
|
|
4174
4113
|
const { ek_PQ, ek_T, dk_PQ, dk_T } = await expandDecapsKeyG(this, key)
|
|
4175
4114
|
const publicKey = new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
4176
4115
|
const privateKey = new HybridKey(
|
|
@@ -4186,7 +4125,7 @@ function PQTKEM_SHARED(): KEM_BASE<HybridKey> {
|
|
|
4186
4125
|
|
|
4187
4126
|
return privateKey
|
|
4188
4127
|
},
|
|
4189
|
-
async Encap(this:
|
|
4128
|
+
async Encap(this: CompositeHybridKEM, pkR) {
|
|
4190
4129
|
assertKeyAlgorithm(pkR, this.algorithm)
|
|
4191
4130
|
HybridKey.validate(pkR)
|
|
4192
4131
|
|
|
@@ -4198,7 +4137,7 @@ function PQTKEM_SHARED(): KEM_BASE<HybridKey> {
|
|
|
4198
4137
|
|
|
4199
4138
|
return { shared_secret: ss_H, enc: ct_H }
|
|
4200
4139
|
},
|
|
4201
|
-
async Decap(this:
|
|
4140
|
+
async Decap(this: CompositeHybridKEM, enc, skR, pkR) {
|
|
4202
4141
|
assertKeyAlgorithm(skR, this.algorithm)
|
|
4203
4142
|
HybridKey.validate(skR)
|
|
4204
4143
|
|
|
@@ -4220,6 +4159,20 @@ function PQTKEM_SHARED(): KEM_BASE<HybridKey> {
|
|
|
4220
4159
|
}
|
|
4221
4160
|
}
|
|
4222
4161
|
|
|
4162
|
+
function createCompositeHybridKEM(parameters: CompositeHybridKEMParams): CompositeHybridKEM {
|
|
4163
|
+
const kdf = KDF_SHAKE256()
|
|
4164
|
+
// @ts-expect-error: so that NotSupportedError messages from kdf's subtle() are accurate
|
|
4165
|
+
kdf.name = parameters.name
|
|
4166
|
+
return {
|
|
4167
|
+
...parameters,
|
|
4168
|
+
suite_id: concat(L_KEM, I2OSP(parameters.id, 2)),
|
|
4169
|
+
type: 'KEM',
|
|
4170
|
+
algorithm: { name: parameters.name },
|
|
4171
|
+
kdf,
|
|
4172
|
+
...COMPOSITE_HYBRID_KEM_SHARED(),
|
|
4173
|
+
}
|
|
4174
|
+
}
|
|
4175
|
+
|
|
4223
4176
|
// ============================================================================
|
|
4224
4177
|
// KEM (Key Encapsulation Mechanism) - Hybrid KEM Suite Exports
|
|
4225
4178
|
// ============================================================================
|
|
@@ -4227,12 +4180,18 @@ function PQTKEM_SHARED(): KEM_BASE<HybridKey> {
|
|
|
4227
4180
|
/**
|
|
4228
4181
|
* Hybrid KEM combining ML-KEM-768 with X25519 (MLKEM768-X25519).
|
|
4229
4182
|
*
|
|
4230
|
-
* Depends on the following Web Cryptography
|
|
4183
|
+
* Depends on one of the following Web Cryptography algorithm sets being supported in the runtime:
|
|
4184
|
+
*
|
|
4185
|
+
* - MLKEM768-X25519 key encapsulation
|
|
4186
|
+
* - SHAKE256 (cSHAKE256 without any parameters) digest on the recipient side for key derivation
|
|
4187
|
+
*
|
|
4188
|
+
* Or:
|
|
4231
4189
|
*
|
|
4232
4190
|
* - ML-KEM-768 key encapsulation
|
|
4233
4191
|
* - X25519 key agreement
|
|
4234
4192
|
* - SHA3-256 digest
|
|
4235
|
-
* - SHAKE256 (cSHAKE256 without any parameters) digest on the recipient side for
|
|
4193
|
+
* - SHAKE256 (cSHAKE256 without any parameters) digest on the recipient side for key derivation and
|
|
4194
|
+
* composite-key seed expansion
|
|
4236
4195
|
*
|
|
4237
4196
|
* This is a factory function that must be passed to the {@link CipherSuite} constructor.
|
|
4238
4197
|
*
|
|
@@ -4243,24 +4202,22 @@ function PQTKEM_SHARED(): KEM_BASE<HybridKey> {
|
|
|
4243
4202
|
* @group KEM Algorithms
|
|
4244
4203
|
* @see [HPKE-PQ Hybrid KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-4)
|
|
4245
4204
|
*/
|
|
4246
|
-
export const KEM_MLKEM768_X25519: KEMFactory = function ():
|
|
4205
|
+
export const KEM_MLKEM768_X25519: KEMFactory = function (): CompositeHybridKEM | DirectKEM {
|
|
4247
4206
|
const id = 0x647a
|
|
4248
4207
|
const name = 'MLKEM768-X25519'
|
|
4249
|
-
const
|
|
4208
|
+
const Nsecret = 32
|
|
4209
|
+
const Nenc = 1120
|
|
4210
|
+
const Npk = 1216
|
|
4211
|
+
const Nsk = 32
|
|
4212
|
+
const kem = { id, name, Nsecret, Nenc, Npk, Nsk }
|
|
4213
|
+
// @ts-expect-error
|
|
4214
|
+
if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
|
|
4215
|
+
return createDirectKEM(kem)
|
|
4216
|
+
}
|
|
4217
|
+
|
|
4250
4218
|
const pkcs8 = Uint8Array.of(0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20) // prettier-ignore
|
|
4251
|
-
|
|
4252
|
-
|
|
4253
|
-
return {
|
|
4254
|
-
id,
|
|
4255
|
-
kdf,
|
|
4256
|
-
suite_id: concat(L_KEM, I2OSP(id, 2)),
|
|
4257
|
-
type: 'KEM',
|
|
4258
|
-
name,
|
|
4259
|
-
Nsecret: 32,
|
|
4260
|
-
Nenc: 1120,
|
|
4261
|
-
Npk: 1216,
|
|
4262
|
-
Nsk: 32,
|
|
4263
|
-
algorithm: { name: 'MLKEM768-X25519' },
|
|
4219
|
+
return createCompositeHybridKEM({
|
|
4220
|
+
...kem,
|
|
4264
4221
|
pq: { algorithm: { name: 'ML-KEM-768' }, Nseed: 64, Npk: 1184, Nct: 1088 },
|
|
4265
4222
|
t: {
|
|
4266
4223
|
algorithm: { name: 'X25519' },
|
|
@@ -4277,19 +4234,24 @@ export const KEM_MLKEM768_X25519: KEMFactory = function (): HybridKEM {
|
|
|
4277
4234
|
},
|
|
4278
4235
|
},
|
|
4279
4236
|
label: Uint8Array.of(0x5c, 0x2e, 0x2f, 0x2f, 0x5e, 0x5c), // prettier-ignore
|
|
4280
|
-
|
|
4281
|
-
}
|
|
4237
|
+
})
|
|
4282
4238
|
}
|
|
4283
4239
|
|
|
4284
4240
|
/**
|
|
4285
4241
|
* Hybrid KEM combining ML-KEM-768 with P-256 (MLKEM768-P256).
|
|
4286
4242
|
*
|
|
4287
|
-
* Depends on the following Web Cryptography
|
|
4243
|
+
* Depends on one of the following Web Cryptography algorithm sets being supported in the runtime:
|
|
4244
|
+
*
|
|
4245
|
+
* - MLKEM768-P256 key encapsulation
|
|
4246
|
+
* - SHAKE256 (cSHAKE256 without any parameters) digest on the recipient side for key derivation
|
|
4247
|
+
*
|
|
4248
|
+
* Or:
|
|
4288
4249
|
*
|
|
4289
4250
|
* - ML-KEM-768 key encapsulation
|
|
4290
4251
|
* - ECDH with P-256 curve
|
|
4291
4252
|
* - SHA3-256 digest
|
|
4292
|
-
* - SHAKE256 (cSHAKE256 without any parameters) digest on the recipient side for
|
|
4253
|
+
* - SHAKE256 (cSHAKE256 without any parameters) digest on the recipient side for key derivation and
|
|
4254
|
+
* composite-key seed expansion
|
|
4293
4255
|
*
|
|
4294
4256
|
* This is a factory function that must be passed to the {@link CipherSuite} constructor.
|
|
4295
4257
|
*
|
|
@@ -4300,23 +4262,21 @@ export const KEM_MLKEM768_X25519: KEMFactory = function (): HybridKEM {
|
|
|
4300
4262
|
* @group KEM Algorithms
|
|
4301
4263
|
* @see [HPKE-PQ Hybrid KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-4)
|
|
4302
4264
|
*/
|
|
4303
|
-
export const KEM_MLKEM768_P256: KEMFactory = function ():
|
|
4265
|
+
export const KEM_MLKEM768_P256: KEMFactory = function (): CompositeHybridKEM | DirectKEM {
|
|
4304
4266
|
const id = 0x0050
|
|
4305
4267
|
const name = 'MLKEM768-P256'
|
|
4306
|
-
const
|
|
4307
|
-
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
4312
|
-
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
|
|
4317
|
-
|
|
4318
|
-
Nsk: 32,
|
|
4319
|
-
algorithm: { name: 'MLKEM768-P256' },
|
|
4268
|
+
const Nsecret = 32
|
|
4269
|
+
const Nenc = 1153
|
|
4270
|
+
const Npk = 1249
|
|
4271
|
+
const Nsk = 32
|
|
4272
|
+
const kem = { id, name, Nsecret, Nenc, Npk, Nsk }
|
|
4273
|
+
// @ts-expect-error
|
|
4274
|
+
if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
|
|
4275
|
+
return createDirectKEM(kem)
|
|
4276
|
+
}
|
|
4277
|
+
|
|
4278
|
+
return createCompositeHybridKEM({
|
|
4279
|
+
...kem,
|
|
4320
4280
|
pq: { algorithm: { name: 'ML-KEM-768' }, Nseed: 64, Npk: 1184, Nct: 1088 },
|
|
4321
4281
|
t: {
|
|
4322
4282
|
...P256,
|
|
@@ -4333,19 +4293,24 @@ export const KEM_MLKEM768_P256: KEMFactory = function (): HybridKEM {
|
|
|
4333
4293
|
},
|
|
4334
4294
|
},
|
|
4335
4295
|
label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x37, 0x36, 0x38, 0x2d, 0x50, 0x32, 0x35, 0x36), // prettier-ignore
|
|
4336
|
-
|
|
4337
|
-
}
|
|
4296
|
+
})
|
|
4338
4297
|
}
|
|
4339
4298
|
|
|
4340
4299
|
/**
|
|
4341
4300
|
* Hybrid KEM combining ML-KEM-1024 with P-384 (MLKEM1024-P384).
|
|
4342
4301
|
*
|
|
4343
|
-
* Depends on the following Web Cryptography
|
|
4302
|
+
* Depends on one of the following Web Cryptography algorithm sets being supported in the runtime:
|
|
4303
|
+
*
|
|
4304
|
+
* - MLKEM1024-P384 key encapsulation
|
|
4305
|
+
* - SHAKE256 (cSHAKE256 without any parameters) digest on the recipient side for key derivation
|
|
4306
|
+
*
|
|
4307
|
+
* Or:
|
|
4344
4308
|
*
|
|
4345
4309
|
* - ML-KEM-1024 key encapsulation
|
|
4346
4310
|
* - ECDH with P-384 curve
|
|
4347
4311
|
* - SHA3-256 digest
|
|
4348
|
-
* - SHAKE256 (cSHAKE256 without any parameters) digest on the recipient side for
|
|
4312
|
+
* - SHAKE256 (cSHAKE256 without any parameters) digest on the recipient side for key derivation and
|
|
4313
|
+
* composite-key seed expansion
|
|
4349
4314
|
*
|
|
4350
4315
|
* This is a factory function that must be passed to the {@link CipherSuite} constructor.
|
|
4351
4316
|
*
|
|
@@ -4356,23 +4321,21 @@ export const KEM_MLKEM768_P256: KEMFactory = function (): HybridKEM {
|
|
|
4356
4321
|
* @group KEM Algorithms
|
|
4357
4322
|
* @see [HPKE-PQ Hybrid KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-4)
|
|
4358
4323
|
*/
|
|
4359
|
-
export const KEM_MLKEM1024_P384: KEMFactory = function ():
|
|
4324
|
+
export const KEM_MLKEM1024_P384: KEMFactory = function (): CompositeHybridKEM | DirectKEM {
|
|
4360
4325
|
const id = 0x0051
|
|
4361
4326
|
const name = 'MLKEM1024-P384'
|
|
4362
|
-
const
|
|
4363
|
-
|
|
4364
|
-
|
|
4365
|
-
|
|
4366
|
-
|
|
4367
|
-
|
|
4368
|
-
|
|
4369
|
-
|
|
4370
|
-
|
|
4371
|
-
|
|
4372
|
-
|
|
4373
|
-
|
|
4374
|
-
Nsk: 32,
|
|
4375
|
-
algorithm: { name: 'MLKEM1024-P384' },
|
|
4327
|
+
const Nsecret = 32
|
|
4328
|
+
const Nenc = 1665
|
|
4329
|
+
const Npk = 1665
|
|
4330
|
+
const Nsk = 32
|
|
4331
|
+
const kem = { id, name, Nsecret, Nenc, Npk, Nsk }
|
|
4332
|
+
// @ts-expect-error
|
|
4333
|
+
if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
|
|
4334
|
+
return createDirectKEM(kem)
|
|
4335
|
+
}
|
|
4336
|
+
|
|
4337
|
+
return createCompositeHybridKEM({
|
|
4338
|
+
...kem,
|
|
4376
4339
|
pq: { algorithm: { name: 'ML-KEM-1024' }, Nseed: 64, Npk: 1568, Nct: 1568 },
|
|
4377
4340
|
t: {
|
|
4378
4341
|
...P384,
|
|
@@ -4390,6 +4353,5 @@ export const KEM_MLKEM1024_P384: KEMFactory = function (): HybridKEM {
|
|
|
4390
4353
|
},
|
|
4391
4354
|
},
|
|
4392
4355
|
label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x31, 0x30, 0x32, 0x34, 0x2d, 0x50, 0x33, 0x38, 0x34), // prettier-ignore
|
|
4393
|
-
|
|
4394
|
-
}
|
|
4356
|
+
})
|
|
4395
4357
|
}
|