{"version":3,"file":"algs.mjs","names":["CEK_LENGTH: Record<string, number>","SENDER: Record<string, SenderFn>","RECIPIENT: Record<string, RecipientFn>","ecdhEsSender: SenderFn","ecdhEsRecipient: RecipientFn"],"sources":["../../src/jwe/algs.ts"],"sourcesContent":["/**\n * JWE algorithm registry. Two-tier dispatch on `alg` (key management)\n * and `enc` (content encryption). `ECDH-ES` + `A256GCM` are wired at\n * module load. pq-hybrid registers via its standalone module so the\n * core has no post-quantum import.\n */\n\nimport { x25519 } from '@noble/curves/ed25519'\n\nimport { concatKdf } from './concatKdf'\nimport {\n  ALG_ECDH_ES,\n  ENC_A256GCM,\n  JweAlgUnsupportedError,\n  type KeyAgreementResult,\n  type RecipientFn,\n  type SenderFn,\n} from './types'\nimport { b64uDecode, b64uEncode } from './b64url'\n\nconst CEK_LENGTH: Record<string, number> = {\n  [ENC_A256GCM]: 32,\n}\n\nconst SENDER: Record<string, SenderFn> = {}\nconst RECIPIENT: Record<string, RecipientFn> = {}\n\n/** Register a `(sender, recipient)` pair for an alg. Idempotent. */\nexport function register(alg: string, sender: SenderFn, recipient: RecipientFn): void {\n  SENDER[alg] = sender\n  RECIPIENT[alg] = recipient\n}\n\nexport function resolveAlg(alg: string): { sender: SenderFn; recipient: RecipientFn } {\n  if (!SENDER[alg] || !RECIPIENT[alg]) {\n    throw new JweAlgUnsupportedError(\n      `JWE alg ${JSON.stringify(alg)} not registered (have: ${Object.keys(SENDER).sort().join(', ')})`\n    )\n  }\n  return { sender: SENDER[alg], recipient: RECIPIENT[alg] }\n}\n\nexport function cekLengthForEnc(enc: string): number {\n  if (!(enc in CEK_LENGTH)) {\n    throw new JweAlgUnsupportedError(\n      `JWE enc ${JSON.stringify(enc)} not supported (have: ${Object.keys(CEK_LENGTH).join(', ')})`\n    )\n  }\n  return CEK_LENGTH[enc]\n}\n\n// ─────────────────────── built-in: ECDH-ES (X25519) ──────────────────\n\nconst ecdhEsSender: SenderFn = ({ recipientPublicKey, enc, apu, apv }) => {\n  if (recipientPublicKey.length !== 32) {\n    throw new JweAlgUnsupportedError(\n      `ECDH-ES recipient pub must be 32 bytes (got ${recipientPublicKey.length})`\n    )\n  }\n  // Ephemeral X25519. @noble/curves x25519.utils.randomPrivateKey()\n  // returns secure-random bytes.\n  const ephemeralPriv = x25519.utils.randomSecretKey()\n  const ephemeralPub = x25519.getPublicKey(ephemeralPriv)\n  const sharedSecret = x25519.getSharedSecret(ephemeralPriv, recipientPublicKey)\n\n  const cek = concatKdf({\n    sharedSecret,\n    algorithmId: enc,\n    partyUInfo: apu,\n    partyVInfo: apv,\n    keyDataLen: cekLengthForEnc(enc) * 8,\n  })\n\n  const result: KeyAgreementResult = {\n    cek,\n    encryptedKey: new Uint8Array(0),\n    headerExtra: {\n      epk: {\n        kty: 'OKP',\n        crv: 'X25519',\n        x: b64uEncode(ephemeralPub),\n      },\n    },\n  }\n  return result\n}\n\nconst ecdhEsRecipient: RecipientFn = ({ privateKey, recipientHeader, enc, apu, apv }) => {\n  if (privateKey.length !== 32) {\n    throw new JweAlgUnsupportedError(\n      `ECDH-ES recipient priv must be 32 bytes (got ${privateKey.length})`\n    )\n  }\n  const epk = recipientHeader['epk'] as { kty?: string; crv?: string; x?: string } | undefined\n  if (!epk || epk.kty !== 'OKP' || epk.crv !== 'X25519' || typeof epk.x !== 'string') {\n    throw new JweAlgUnsupportedError('ECDH-ES recipient header missing X25519 epk')\n  }\n  const ephemeralPub = b64uDecode(epk.x)\n  const sharedSecret = x25519.getSharedSecret(privateKey, ephemeralPub)\n  return concatKdf({\n    sharedSecret,\n    algorithmId: enc,\n    partyUInfo: apu,\n    partyVInfo: apv,\n    keyDataLen: cekLengthForEnc(enc) * 8,\n  })\n}\n\nregister(ALG_ECDH_ES, ecdhEsSender, ecdhEsRecipient)\n"],"mappings":";;;;;;;;;;;;AAoBA,MAAMA,aAAqC,GACxC,cAAc,IAChB;AAED,MAAMC,SAAmC,EAAE;AAC3C,MAAMC,YAAyC,EAAE;;AAGjD,SAAgB,S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