Skip to content

Commit

Permalink
perf: use KeyObject.prototype asymmetricKeyDetails when available
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Jan 18, 2021
1 parent 808f06c commit ad88ee2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
7 changes: 5 additions & 2 deletions src/runtime/node/check_modulus_length.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ const getModulusLength = (key: KeyObject): number => {
return weakMap.get(key)!
}

const modulusLength =
const modulusLength: number =
// @ts-expect-error
key.asymmetricKeyDetails?.modulusLength ??
(getLengthOfSeqIndex(
key.export({ format: 'der', type: 'pkcs1' }),
key.type === 'private' ? 1 : 0,
) -
1) <<
3
3

weakMap.set(key, modulusLength)
return modulusLength
}
Expand Down
61 changes: 39 additions & 22 deletions src/runtime/node/get_named_curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@ const secp256k1 = Buffer.from([43, 129, 4, 0, 10])

export const weakMap: WeakMap<KeyObject, string> = new WeakMap()

const getNamedCurve = (key: KeyObject): string => {
const namedCurveToJOSE = (namedCurve: string) => {
switch (namedCurve) {
case 'prime256v1':
return 'P-256'
case 'secp384r1':
return 'P-384'
case 'secp521r1':
return 'P-521'
case 'secp256k1':
return namedCurve
default:
throw new JOSENotSupported('unsupported curve for this operation')
}
}

const getNamedCurve = (key: KeyObject, raw?: boolean): string => {
if (key.type === 'secret') {
throw new TypeError('only "private" or "public" key objects can be used for this operation')
}
Expand All @@ -21,31 +36,33 @@ const getNamedCurve = (key: KeyObject): string => {
return `X${key.asymmetricKeyType.substr(1)}`
case 'ec': {
if (weakMap.has(key)) {
return <string>weakMap.get(key)
return weakMap.get(key)!
}

if (key.type === 'private') {
const curve = getNamedCurve(createPublicKey(key))
weakMap.set(key, curve)
return curve
}
// @ts-expect-error
let namedCurve: string = key.asymmetricKeyDetails?.namedCurve

const buf = key.export({ format: 'der', type: 'spki' })
const i = buf[1] < 128 ? 14 : 15
const len = buf[i]
const curveOid = buf.slice(i + 1, i + 1 + len)
let curve: string
if (curveOid.equals(p256)) {
curve = 'P-256'
} else if (curveOid.equals(p384)) {
curve = 'P-384'
} else if (curveOid.equals(p521)) {
curve = 'P-521'
} else if (curveOid.equals(secp256k1)) {
curve = 'secp256k1'
} else {
throw new JOSENotSupported('unsupported curve for this operation')
if (!namedCurve && key.type === 'private') {
namedCurve = getNamedCurve(createPublicKey(key), true)
} else if (!namedCurve) {
const buf = key.export({ format: 'der', type: 'spki' })
const i = buf[1] < 128 ? 14 : 15
const len = buf[i]
const curveOid = buf.slice(i + 1, i + 1 + len)
if (curveOid.equals(p256)) {
namedCurve = 'prime256v1'
} else if (curveOid.equals(p384)) {
namedCurve = 'secp384r1'
} else if (curveOid.equals(p521)) {
namedCurve = 'secp521r1'
} else if (curveOid.equals(secp256k1)) {
namedCurve = 'secp256k1'
}
}

if (raw) return namedCurve

const curve = namedCurveToJOSE(namedCurve)
weakMap.set(key, curve)
return curve
}
Expand Down
6 changes: 3 additions & 3 deletions test/jwk/jwk2key.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ Promise.all([import(`${root}/jwk/parse`), import(`${root}/jwk/from_key_like`)]).
};

t.deepEqual(
await parseJwk(oct, 'HS256'),
Uint8Array.from([
[...(await parseJwk(oct, 'HS256'))],
[
23,
32,
170,
Expand Down Expand Up @@ -106,7 +106,7 @@ Promise.all([import(`${root}/jwk/parse`), import(`${root}/jwk/from_key_like`)]).
17,
146,
114,
]),
],
);
});

Expand Down

0 comments on commit ad88ee2

Please sign in to comment.