Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix P-521 bit length in Web Crypto #2710

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions packages/crypto/src/keys/ecdh/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import webcrypto from '../../webcrypto/index.js'
import type { Curve } from './index.js'
import type { ECDHKey, ECDHKeyPair, JWKEncodedPrivateKey, JWKEncodedPublicKey } from '../interface.js'

const bits = {
'P-256': 256,
'P-384': 384,
'P-521': 521
const curveLengths = {
'P-256': 32,
'P-384': 48,
'P-521': 66
}

const curveTypes = Object.keys(bits)
const curveTypes = Object.keys(curveLengths)
const names = curveTypes.join(' / ')

export async function generateEphemeralKeyPair (curve: Curve): Promise<ECDHKey> {
Expand Down Expand Up @@ -63,12 +63,10 @@ export async function generateEphemeralKeyPair (curve: Curve): Promise<ECDHKey>
const buffer = await webcrypto.get().subtle.deriveBits(
{
name: 'ECDH',
// @ts-expect-error namedCurve is missing from the types
namedCurve: curve,
public: key
},
privateKey,
bits[curve]
curveLengths[curve] * 8
)

return new Uint8Array(buffer, 0, buffer.byteLength)
Expand All @@ -84,12 +82,6 @@ export async function generateEphemeralKeyPair (curve: Curve): Promise<ECDHKey>
return ecdhKey
}

const curveLengths = {
'P-256': 32,
'P-384': 48,
'P-521': 66
}

// Marshal converts a jwk encoded ECDH public key into the
// form specified in section 4.3.6 of ANSI X9.62. (This is the format
// go-ipfs uses)
Expand Down
Loading