{"version":3,"file":"mac0Context.mjs","names":[],"sources":["../../../src/crypto/contexts/mac0Context.ts"],"sourcesContent":["import { type Mac0Context } from '@owf/cose'\nimport type { AgentContext } from '../../agent'\nimport { CredoError } from '../../error'\nimport {\n  KeyManagementApi,\n  type KmsJwkPublicAsymmetric,\n  type KnownCoseSignatureAlgorithm,\n  knownJwaFromCoseSignatureAlgorithm,\n} from '../../modules/kms'\n\nexport const getMac0Context = (agentContext: AgentContext): Mac0Context => {\n  const kms = agentContext.resolve(KeyManagementApi)\n\n  return {\n    authenticate: async (input) => {\n      if (input.key instanceof Uint8Array) {\n        throw new CredoError('For mdoc authentication with mac0 a CoseKey is required, not a Uint8Array')\n      }\n      if (!input.key.keyId) {\n        throw new CredoError('Missing required keyId on CoseKey for signing mdoc')\n      }\n\n      const algorithm = input.algorithm ?? input.key.algorithm\n      if (algorithm === undefined) {\n        throw new CredoError(\n          'Unable to authenticate COSE Mac0 structure. No algorithm provided or defined on the COSE key.'\n        )\n      }\n\n      const { signature } = await kms.sign({\n        data: input.toBeAuthenticated,\n        algorithm: knownJwaFromCoseSignatureAlgorithm(algorithm as unknown as KnownCoseSignatureAlgorithm),\n        keyId: input.key.keyId,\n      })\n\n      return signature\n    },\n    verify: async (input) => {\n      const { tag, toBeAuthenticated, key } = input\n      if (key instanceof Uint8Array) {\n        throw new CredoError('For mdoc authentication verification with mac0 a CoseKey is required, not a Uint8Array')\n      }\n\n      const algorithm = input.algorithm ?? key.algorithm\n      if (algorithm === undefined) {\n        throw new CredoError(\n          'Unable to verify COSE Mac0 structure. No algorithm defined in the protected header or on the COSE key.'\n        )\n      }\n\n      const { verified } = await kms.verify({\n        key: {\n          publicJwk: key.jwk as KmsJwkPublicAsymmetric,\n        },\n        data: toBeAuthenticated,\n        algorithm: knownJwaFromCoseSignatureAlgorithm(algorithm as unknown as KnownCoseSignatureAlgorithm),\n        signature: tag,\n      })\n\n      return verified\n    },\n  } satisfies Mac0Context\n}\n"],"mappings":";;;;;;;AAUA,MAAa,kBAAkB,iBAA4C;CACzE,MAAM,MAAM,aAAa,QAAQ,gBAAgB;CAEjD,OAAO;EACL,cAAc,OAAO,UAAU;GAC7B,IAAI,MAAM,eAAe,YACvB,MAAM,IAAI,WAAW,2EAA2E;GAElG,IAAI,CAAC,MAAM,IAAI,OACb,MAAM,IAAI,WAAW,oDAAoD;GAG3E,MAAM,YAAY,MAAM,aAAa,MAAM,IAAI;GAC/C,IAAI,cAAc,KAAA,GAChB,MAAM,IAAI,WACR,+FACF;GAGF,MAAM,EAAE,cAAc,MAAM,IAAI,KAAK;IACnC,MAAM,MAAM;IACZ,WAAW,mCAAmC,SAAmD;IACjG,OAAO,MAAM,IAAI;GACnB,CAAC;GAED,OAAO;EACT;EACA,QAAQ,OAAO,UAAU;GACvB,MAAM,EAAE,KAAK,mBAAmB,QAAQ;GACxC,IAAI,eAAe,YACjB,MAAM,IAAI,WAAW,wFAAwF;GAG/G,MAAM,YAAY,MAAM,aAAa,IAAI;GACzC,IAAI,cAAc,KAAA,GAChB,MAAM,IAAI,WACR,wGACF;GAGF,MAAM,EAAE,aAAa,MAAM,IAAI,OAAO;IACpC,KAAK,EACH,WAAW,IAAI,IACjB;IACA,MAAM;IACN,WAAW,mCAAmC,SAAmD;IACjG,WAAW;GACb,CAAC;GAED,OAAO;EACT;CACF;AACF"}