Skip to content

Commit

Permalink
chore: temp code for hmac debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderPostma committed Dec 11, 2024
1 parent 1dccd80 commit 153c1f4
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions packages/jwt-service/src/functions/JWE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,32 +388,26 @@ export function toWebCryptoCiphertext(ciphertext: string, tag: string): Uint8Arr
}


async function deriveEcdhKey(publicKey: CryptoKey): Promise<Uint8Array> {
// Extract public key coordinates
const jwk = await crypto.subtle.exportKey('jwk', publicKey)
if (!jwk.x || !jwk.y) {
async function deriveEcdhKey(publicKey: jose.KeyLike): Promise<Uint8Array> {
const keyData = (publicKey as any)._import.keyData
if (!keyData.x || !keyData.y) {
throw new Error('Invalid public key format')
}

// Convert base64url coordinates to bytes
const xBytes = base64ToBytes(jwk.x)
const yBytes = base64ToBytes(jwk.y)
const xBytes = base64ToBytes(keyData.x)
const yBytes = base64ToBytes(keyData.y)

// Create point from coordinates
const point = p256.ProjectivePoint.fromAffine({
x: BigInt('0x' + u8a.toString(xBytes, 'hex')),
y: BigInt('0x' + u8a.toString(yBytes, 'hex'))
})

// Generate ephemeral key pair
const ephemeralPrivate = p256.utils.randomPrivateKey()
//const ephemeralPoint = p256.getPublicKey(ephemeralPrivate)

// Compute shared secret
const sharedPoint = point.multiply(BigInt('0x' + u8a.toString(ephemeralPrivate, 'hex')))
const sharedSecret = u8a.fromString(sharedPoint.x.toString(16), 'hex')

// Derive key using concat KDF as per JOSE spec
// Convert x coordinate to bytes using encode method
const sharedSecret = p256.CURVE.Fp.toBytes(sharedPoint.x)

const derivedKey = await crypto.subtle.digest('SHA-256', sharedSecret)

return new Uint8Array(derivedKey)
Expand Down

0 comments on commit 153c1f4

Please sign in to comment.