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.js
CHANGED
|
@@ -1519,22 +1519,23 @@ export function encode(string ) {
|
|
|
1519
1519
|
|
|
1520
1520
|
|
|
1521
1521
|
|
|
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 ) {
|
|
1540
1541
|
return concat(I2OSP(x.byteLength, 2), x)
|
|
@@ -2986,138 +2987,106 @@ async function CurveKeyFromD(
|
|
|
2986
2987
|
|
|
2987
2988
|
|
|
2988
2989
|
|
|
2989
|
-
function mod(a , p ) {
|
|
2990
|
-
const r = a % p
|
|
2991
|
-
return r < 0n ? r + p : r
|
|
2992
|
-
}
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
function modInverse(a , m ) {
|
|
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 , P , a ) {
|
|
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 , q , P , a ) {
|
|
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
2990
|
|
|
3042
2991
|
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
|
|
2992
|
+
async function importKeyNist(
|
|
2993
|
+
curve ,
|
|
2994
|
+
key ,
|
|
2995
|
+
name ,
|
|
2996
|
+
extractable ,
|
|
2997
|
+
includePublicKey ,
|
|
2998
|
+
) {
|
|
2999
|
+
const d = OS2IP(key)
|
|
3000
|
+
if (d === 0n || d >= curve.order) {
|
|
3050
3001
|
throw new Error('Invalid scalar')
|
|
3051
3002
|
}
|
|
3052
3003
|
|
|
3053
3004
|
|
|
3054
|
-
const
|
|
3055
|
-
|
|
3056
|
-
|
|
3057
|
-
|
|
3058
|
-
|
|
3059
|
-
|
|
3060
|
-
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
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 = [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)
|
|
3005
|
+
const pkcs8 = concat(curve.pkcs8, key)
|
|
3006
|
+
return await subtle(async (c) => {
|
|
3007
|
+
async function fromJwk(jwk , privateKey ) {
|
|
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
|
+
}
|
|
3083
3016
|
}
|
|
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
|
-
|
|
3092
3017
|
|
|
3018
|
+
let privateKey
|
|
3093
3019
|
|
|
3020
|
+
const nativePublicKey = typeof c.getPublicKey === 'function'
|
|
3021
|
+
try {
|
|
3022
|
+
privateKey = await c.importKey(
|
|
3023
|
+
'pkcs8',
|
|
3024
|
+
pkcs8 ,
|
|
3025
|
+
curve.algorithm,
|
|
3026
|
+
nativePublicKey ? extractable : true,
|
|
3027
|
+
['deriveBits'],
|
|
3028
|
+
)
|
|
3029
|
+
if (nativePublicKey) {
|
|
3094
3030
|
|
|
3095
3031
|
|
|
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) {
|
|
3096
3041
|
|
|
3097
3042
|
|
|
3098
3043
|
|
|
3099
3044
|
|
|
3045
|
+
if (
|
|
3046
|
+
!(cause instanceof DOMException) ||
|
|
3047
|
+
(cause.name !== 'DataError' && cause.name !== 'OperationError')
|
|
3048
|
+
) {
|
|
3049
|
+
throw cause
|
|
3050
|
+
}
|
|
3051
|
+
}
|
|
3100
3052
|
|
|
3053
|
+
const generator = {
|
|
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 , signingAlgorithm, false, ['sign'])
|
|
3063
|
+
: await c.importKey('jwk', jwk, signingAlgorithm, false, ['sign'])
|
|
3064
|
+
privateKey ??= await c.importKey('jwk', jwk, curve.algorithm, false, ['deriveBits'])
|
|
3101
3065
|
|
|
3102
3066
|
|
|
3103
|
-
function getPrivateJwkNist(DHKEM , d ) {
|
|
3104
3067
|
|
|
3105
|
-
|
|
3106
|
-
|
|
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
|
+
)
|
|
3107
3072
|
|
|
3108
|
-
const coordSize = (DHKEM.Npk - 1) / 2
|
|
3109
|
-
const xBytes = bigIntToUint8Array(publicPoint.x, coordSize)
|
|
3110
|
-
const yBytes = bigIntToUint8Array(publicPoint.y, coordSize)
|
|
3111
|
-
const dBytes = bigIntToUint8Array(d, DHKEM.Nsk)
|
|
3112
3073
|
|
|
3113
3074
|
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3075
|
+
const message = L_public_key_recovery
|
|
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 , 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 ,
|
|
3126
3095
|
extractable ,
|
|
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
|
|
|
@@ -3164,20 +3125,7 @@ async function GetKeyPairNist(
|
|
|
3164
3125
|
extractable ,
|
|
3165
3126
|
name ,
|
|
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))
|
|
3181
3129
|
}
|
|
3182
3130
|
|
|
3183
3131
|
|
|
@@ -3200,7 +3148,8 @@ const P256 = {
|
|
|
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),
|
|
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),
|
|
3204
3153
|
Gx: 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296n,
|
|
3205
3154
|
Gy: 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5n,
|
|
3206
3155
|
}
|
|
@@ -3255,8 +3204,8 @@ const P384 = {
|
|
|
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),
|
|
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),
|
|
3260
3209
|
Gx: 0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7n,
|
|
3261
3210
|
Gy: 0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5fn,
|
|
3262
3211
|
}
|
|
@@ -3311,8 +3260,8 @@ const P521 = {
|
|
|
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),
|
|
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),
|
|
3316
3265
|
Gx: 0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66n,
|
|
3317
3266
|
Gy: 0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650n,
|
|
3318
3267
|
}
|
|
@@ -3472,9 +3421,11 @@ export const KEM_DHKEM_X448_HKDF_SHA512 = function ()
|
|
|
3472
3421
|
|
|
3473
3422
|
|
|
3474
3423
|
|
|
3475
|
-
|
|
3424
|
+
|
|
3425
|
+
|
|
3426
|
+
function DIRECT_KEM_SHARED() {
|
|
3476
3427
|
return {
|
|
3477
|
-
async DeriveKeyPair(
|
|
3428
|
+
async DeriveKeyPair( ikm, extractable) {
|
|
3478
3429
|
const dk = await LabeledDerive(
|
|
3479
3430
|
this.kdf,
|
|
3480
3431
|
this.suite_id,
|
|
@@ -3491,7 +3442,7 @@ function MLKEM_SHARED() {
|
|
|
3491
3442
|
|
|
3492
3443
|
return { privateKey, publicKey }
|
|
3493
3444
|
},
|
|
3494
|
-
async GenerateKeyPair(
|
|
3445
|
+
async GenerateKeyPair( extractable) {
|
|
3495
3446
|
|
|
3496
3447
|
const usages = ['encapsulateBits', 'decapsulateBits']
|
|
3497
3448
|
return (await subtle(
|
|
@@ -3499,14 +3450,14 @@ function MLKEM_SHARED() {
|
|
|
3499
3450
|
this.name,
|
|
3500
3451
|
))
|
|
3501
3452
|
},
|
|
3502
|
-
async SerializePublicKey(
|
|
3453
|
+
async SerializePublicKey( key) {
|
|
3503
3454
|
assertKeyAlgorithm(key, this.algorithm)
|
|
3504
3455
|
assertCryptoKey(key)
|
|
3505
3456
|
|
|
3506
3457
|
const format = 'raw-public'
|
|
3507
3458
|
return new Uint8Array(await subtle((c) => c.exportKey(format, key), this.name))
|
|
3508
3459
|
},
|
|
3509
|
-
async DeserializePublicKey(
|
|
3460
|
+
async DeserializePublicKey( key) {
|
|
3510
3461
|
|
|
3511
3462
|
const format = 'raw-public'
|
|
3512
3463
|
|
|
@@ -3516,14 +3467,14 @@ function MLKEM_SHARED() {
|
|
|
3516
3467
|
this.name,
|
|
3517
3468
|
)
|
|
3518
3469
|
},
|
|
3519
|
-
async SerializePrivateKey(
|
|
3470
|
+
async SerializePrivateKey( key) {
|
|
3520
3471
|
assertKeyAlgorithm(key, this.algorithm)
|
|
3521
3472
|
assertCryptoKey(key)
|
|
3522
3473
|
|
|
3523
3474
|
const format = 'raw-seed'
|
|
3524
3475
|
return new Uint8Array(await subtle((c) => c.exportKey(format, key), this.name))
|
|
3525
3476
|
},
|
|
3526
|
-
async DeserializePrivateKey(
|
|
3477
|
+
async DeserializePrivateKey( key, extractable) {
|
|
3527
3478
|
|
|
3528
3479
|
const format = 'raw-seed'
|
|
3529
3480
|
|
|
@@ -3533,7 +3484,7 @@ function MLKEM_SHARED() {
|
|
|
3533
3484
|
this.name,
|
|
3534
3485
|
)
|
|
3535
3486
|
},
|
|
3536
|
-
async Encap(
|
|
3487
|
+
async Encap( pkR) {
|
|
3537
3488
|
assertKeyAlgorithm(pkR, this.algorithm)
|
|
3538
3489
|
|
|
3539
3490
|
const { sharedKey, ciphertext } = (await subtle(
|
|
@@ -3544,7 +3495,7 @@ function MLKEM_SHARED() {
|
|
|
3544
3495
|
|
|
3545
3496
|
return { shared_secret: new Uint8Array(sharedKey), enc: new Uint8Array(ciphertext) }
|
|
3546
3497
|
},
|
|
3547
|
-
async Decap(
|
|
3498
|
+
async Decap( 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() {
|
|
|
3557
3508
|
}
|
|
3558
3509
|
}
|
|
3559
3510
|
|
|
3511
|
+
function createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk } ) {
|
|
3512
|
+
const kdf = KDF_SHAKE256()
|
|
3513
|
+
|
|
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
|
|
|
3562
3532
|
|
|
@@ -3584,25 +3554,14 @@ function MLKEM_SHARED() {
|
|
|
3584
3554
|
|
|
3585
3555
|
|
|
3586
3556
|
|
|
3587
|
-
export const KEM_ML_KEM_512 = function ()
|
|
3557
|
+
export const KEM_ML_KEM_512 = function () {
|
|
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 = function () {
|
|
|
3624
3583
|
|
|
3625
3584
|
|
|
3626
3585
|
|
|
3627
|
-
export const KEM_ML_KEM_768 = function ()
|
|
3586
|
+
export const KEM_ML_KEM_768 = function () {
|
|
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 = function () {
|
|
|
3664
3612
|
|
|
3665
3613
|
|
|
3666
3614
|
|
|
3667
|
-
export const KEM_ML_KEM_1024 = function ()
|
|
3615
|
+
export const KEM_ML_KEM_1024 = function () {
|
|
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
|
|
|
@@ -3865,7 +3802,7 @@ const InvalidInvocation = (_ ) => {
|
|
|
3865
3802
|
throw new Error('invalid invocation')
|
|
3866
3803
|
}
|
|
3867
3804
|
}
|
|
3868
|
-
const priv = Symbol()
|
|
3805
|
+
const priv = /* @__PURE__ */ Symbol()
|
|
3869
3806
|
class HybridKey {
|
|
3870
3807
|
#algorithm
|
|
3871
3808
|
#type
|
|
@@ -3956,7 +3893,7 @@ function split(N1 , N2 , x )
|
|
|
3956
3893
|
return [x1, x2]
|
|
3957
3894
|
}
|
|
3958
3895
|
|
|
3959
|
-
function RandomScalarNist(t
|
|
3896
|
+
function RandomScalarNist(t , seed ) {
|
|
3960
3897
|
let sk_bigint = 0n
|
|
3961
3898
|
let start = 0
|
|
3962
3899
|
let end = t.Nscalar
|
|
@@ -3974,69 +3911,69 @@ function RandomScalarNist(t , seed ) {
|
|
|
3974
3911
|
}
|
|
3975
3912
|
|
|
3976
3913
|
|
|
3977
|
-
async function expandDecapsKeyG(
|
|
3978
|
-
const Nout =
|
|
3914
|
+
async function expandDecapsKeyG(kem , seed ) {
|
|
3915
|
+
const Nout = kem.pq.Nseed + kem.t.Nseed
|
|
3979
3916
|
const bits = Nout << 3
|
|
3980
3917
|
|
|
3981
3918
|
const algorithm = { name: 'cSHAKE256', length: bits, outputLength: bits }
|
|
3982
|
-
const seed_full = await subtle((c) => c.digest(algorithm, seed ),
|
|
3919
|
+
const seed_full = await subtle((c) => c.digest(algorithm, seed ), 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
|
|
|
3987
3924
|
const format = 'raw-seed'
|
|
3988
3925
|
|
|
3989
3926
|
const usages = ['decapsulateBits', 'encapsulateBits']
|
|
3990
3927
|
const dk_PQ = await subtle(
|
|
3991
|
-
(c) => c.importKey(format, seed_PQ ,
|
|
3992
|
-
|
|
3928
|
+
(c) => c.importKey(format, seed_PQ , 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
|
|
|
4003
3940
|
async function C2PRICombiner(
|
|
4004
|
-
|
|
3941
|
+
kem ,
|
|
4005
3942
|
ss_PQ ,
|
|
4006
3943
|
ss_T ,
|
|
4007
3944
|
ct_T ,
|
|
4008
3945
|
_ek_T ,
|
|
4009
3946
|
label ,
|
|
4010
3947
|
) {
|
|
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)
|
|
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
|
|
|
4017
3954
|
async function prepareEncapsG(
|
|
4018
|
-
|
|
3955
|
+
kem ,
|
|
4019
3956
|
ek_PQ ,
|
|
4020
3957
|
ek_T ,
|
|
4021
3958
|
) {
|
|
4022
3959
|
const res = (await subtle(
|
|
4023
3960
|
|
|
4024
|
-
(c) => c.encapsulateBits(
|
|
4025
|
-
|
|
3961
|
+
(c) => c.encapsulateBits(kem.pq.algorithm, ek_PQ),
|
|
3962
|
+
kem.name,
|
|
4026
3963
|
))
|
|
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
|
))
|
|
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
|
|
|
4048
3985
|
async function prepareDecapsG(
|
|
4049
|
-
|
|
3986
|
+
kem ,
|
|
4050
3987
|
dk_PQ ,
|
|
4051
3988
|
dk_T ,
|
|
4052
3989
|
ct_PQ ,
|
|
@@ -4055,20 +3992,20 @@ async function prepareDecapsG(
|
|
|
4055
3992
|
const ss_PQ = new Uint8Array(
|
|
4056
3993
|
await subtle(
|
|
4057
3994
|
|
|
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 ,
|
|
4065
|
-
|
|
4001
|
+
(c) => c.importKey('raw', ct_T , 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)
|
|
@@ -4104,10 +4041,12 @@ async function prepareDecapsG(
|
|
|
4104
4041
|
|
|
4105
4042
|
|
|
4106
4043
|
|
|
4107
|
-
|
|
4044
|
+
|
|
4045
|
+
|
|
4046
|
+
function COMPOSITE_HYBRID_KEM_SHARED() {
|
|
4108
4047
|
Object.freeze(HybridKey.prototype)
|
|
4109
4048
|
return {
|
|
4110
|
-
async DeriveKeyPair(
|
|
4049
|
+
async DeriveKeyPair( ikm , extractable) {
|
|
4111
4050
|
const seed = await LabeledDerive(
|
|
4112
4051
|
this.kdf,
|
|
4113
4052
|
this.suite_id,
|
|
@@ -4133,10 +4072,10 @@ function PQTKEM_SHARED() {
|
|
|
4133
4072
|
|
|
4134
4073
|
return { privateKey, publicKey }
|
|
4135
4074
|
},
|
|
4136
|
-
async GenerateKeyPair(
|
|
4075
|
+
async GenerateKeyPair( extractable) {
|
|
4137
4076
|
return await this.DeriveKeyPair(crypto.getRandomValues(new Uint8Array(32)), extractable)
|
|
4138
4077
|
},
|
|
4139
|
-
async SerializePublicKey(
|
|
4078
|
+
async SerializePublicKey( key) {
|
|
4140
4079
|
assertKeyAlgorithm(key, this.algorithm)
|
|
4141
4080
|
HybridKey.validate(key, true)
|
|
4142
4081
|
|
|
@@ -4150,7 +4089,7 @@ function PQTKEM_SHARED() {
|
|
|
4150
4089
|
|
|
4151
4090
|
return concat(ek_PQ, ek_T)
|
|
4152
4091
|
},
|
|
4153
|
-
async DeserializePublicKey(
|
|
4092
|
+
async DeserializePublicKey( key) {
|
|
4154
4093
|
|
|
4155
4094
|
const format = 'raw-public'
|
|
4156
4095
|
|
|
@@ -4164,13 +4103,13 @@ function PQTKEM_SHARED() {
|
|
|
4164
4103
|
|
|
4165
4104
|
return new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
4166
4105
|
},
|
|
4167
|
-
async SerializePrivateKey(
|
|
4106
|
+
async SerializePrivateKey( 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(
|
|
4112
|
+
async DeserializePrivateKey( 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() {
|
|
|
4186
4125
|
|
|
4187
4126
|
return privateKey
|
|
4188
4127
|
},
|
|
4189
|
-
async Encap(
|
|
4128
|
+
async Encap( pkR) {
|
|
4190
4129
|
assertKeyAlgorithm(pkR, this.algorithm)
|
|
4191
4130
|
HybridKey.validate(pkR)
|
|
4192
4131
|
|
|
@@ -4198,7 +4137,7 @@ function PQTKEM_SHARED() {
|
|
|
4198
4137
|
|
|
4199
4138
|
return { shared_secret: ss_H, enc: ct_H }
|
|
4200
4139
|
},
|
|
4201
|
-
async Decap(
|
|
4140
|
+
async Decap( enc, skR, pkR) {
|
|
4202
4141
|
assertKeyAlgorithm(skR, this.algorithm)
|
|
4203
4142
|
HybridKey.validate(skR)
|
|
4204
4143
|
|
|
@@ -4220,6 +4159,22 @@ function PQTKEM_SHARED() {
|
|
|
4220
4159
|
}
|
|
4221
4160
|
}
|
|
4222
4161
|
|
|
4162
|
+
function createCompositeHybridKEM(parameters ) {
|
|
4163
|
+
const kdf = KDF_SHAKE256()
|
|
4164
|
+
|
|
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
|
+
|
|
4176
|
+
|
|
4177
|
+
|
|
4223
4178
|
|
|
4224
4179
|
|
|
4225
4180
|
|
|
@@ -4243,24 +4198,26 @@ function PQTKEM_SHARED() {
|
|
|
4243
4198
|
|
|
4244
4199
|
|
|
4245
4200
|
|
|
4246
|
-
|
|
4201
|
+
|
|
4202
|
+
|
|
4203
|
+
|
|
4204
|
+
|
|
4205
|
+
export const KEM_MLKEM768_X25519 = function () {
|
|
4247
4206
|
const id = 0x647a
|
|
4248
4207
|
const name = 'MLKEM768-X25519'
|
|
4249
|
-
const
|
|
4250
|
-
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
|
+
|
|
4214
|
+
if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
|
|
4215
|
+
return createDirectKEM(kem)
|
|
4216
|
+
}
|
|
4251
4217
|
|
|
4252
|
-
|
|
4253
|
-
return {
|
|
4254
|
-
|
|
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' },
|
|
4218
|
+
const pkcs8 = Uint8Array.of(0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20)
|
|
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,8 +4234,7 @@ export const KEM_MLKEM768_X25519 = function () {
|
|
|
4277
4234
|
},
|
|
4278
4235
|
},
|
|
4279
4236
|
label: Uint8Array.of(0x5c, 0x2e, 0x2f, 0x2f, 0x5e, 0x5c),
|
|
4280
|
-
|
|
4281
|
-
}
|
|
4237
|
+
})
|
|
4282
4238
|
}
|
|
4283
4239
|
|
|
4284
4240
|
|
|
@@ -4300,23 +4256,27 @@ export const KEM_MLKEM768_X25519 = function () {
|
|
|
4300
4256
|
|
|
4301
4257
|
|
|
4302
4258
|
|
|
4303
|
-
|
|
4259
|
+
|
|
4260
|
+
|
|
4261
|
+
|
|
4262
|
+
|
|
4263
|
+
|
|
4264
|
+
|
|
4265
|
+
export const KEM_MLKEM768_P256 = function () {
|
|
4304
4266
|
const id = 0x0050
|
|
4305
4267
|
const name = 'MLKEM768-P256'
|
|
4306
|
-
const
|
|
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
|
+
|
|
4274
|
+
if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
|
|
4275
|
+
return createDirectKEM(kem)
|
|
4276
|
+
}
|
|
4307
4277
|
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
id,
|
|
4311
|
-
kdf,
|
|
4312
|
-
suite_id: concat(L_KEM, I2OSP(id, 2)),
|
|
4313
|
-
type: 'KEM',
|
|
4314
|
-
name,
|
|
4315
|
-
Nsecret: 32,
|
|
4316
|
-
Nenc: 1153,
|
|
4317
|
-
Npk: 1249,
|
|
4318
|
-
Nsk: 32,
|
|
4319
|
-
algorithm: { name: 'MLKEM768-P256' },
|
|
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,8 +4293,7 @@ export const KEM_MLKEM768_P256 = function () {
|
|
|
4333
4293
|
},
|
|
4334
4294
|
},
|
|
4335
4295
|
label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x37, 0x36, 0x38, 0x2d, 0x50, 0x32, 0x35, 0x36),
|
|
4336
|
-
|
|
4337
|
-
}
|
|
4296
|
+
})
|
|
4338
4297
|
}
|
|
4339
4298
|
|
|
4340
4299
|
|
|
@@ -4356,23 +4315,27 @@ export const KEM_MLKEM768_P256 = function () {
|
|
|
4356
4315
|
|
|
4357
4316
|
|
|
4358
4317
|
|
|
4359
|
-
|
|
4318
|
+
|
|
4319
|
+
|
|
4320
|
+
|
|
4321
|
+
|
|
4322
|
+
|
|
4323
|
+
|
|
4324
|
+
export const KEM_MLKEM1024_P384 = function () {
|
|
4360
4325
|
const id = 0x0051
|
|
4361
4326
|
const name = 'MLKEM1024-P384'
|
|
4362
|
-
const
|
|
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
|
+
|
|
4333
|
+
if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
|
|
4334
|
+
return createDirectKEM(kem)
|
|
4335
|
+
}
|
|
4363
4336
|
|
|
4364
|
-
|
|
4365
|
-
|
|
4366
|
-
id,
|
|
4367
|
-
kdf,
|
|
4368
|
-
suite_id: concat(L_KEM, I2OSP(id, 2)),
|
|
4369
|
-
type: 'KEM',
|
|
4370
|
-
name,
|
|
4371
|
-
Nsecret: 32,
|
|
4372
|
-
Nenc: 1665,
|
|
4373
|
-
Npk: 1665,
|
|
4374
|
-
Nsk: 32,
|
|
4375
|
-
algorithm: { name: 'MLKEM1024-P384' },
|
|
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 = function () {
|
|
|
4390
4353
|
},
|
|
4391
4354
|
},
|
|
4392
4355
|
label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x31, 0x30, 0x32, 0x34, 0x2d, 0x50, 0x33, 0x38, 0x34),
|
|
4393
|
-
|
|
4394
|
-
}
|
|
4356
|
+
})
|
|
4395
4357
|
}
|