{"version":3,"file":"algorithm.mjs","names":["z"],"sources":["../../../../src/modules/kms/cose/algorithm.ts"],"sourcesContent":["import { z } from 'zod'\nimport { KeyManagementError } from '../error/KeyManagementError'\nimport { type KnownJwaSignatureAlgorithm, KnownJwaSignatureAlgorithms } from '../jwk/jwa'\n\nexport const KnownCoseSignatureAlgorithms = {\n  // EdDSA algorithms - RFC 9864 Section 2.2\n  Ed25519: -19,\n  Ed448: -53,\n\n  // Deprecated polymorphic EdDSA - RFC 9864 Section 4.1.2\n  EdDSA: -8,\n\n  // Deprecated polymorphic ES\n  ES256: -7,\n\n  // ECDSA algorithms - RFC 9864 Section 2.1\n  // JOSE ES256/ES384/ES512 map to fully-specified COSE ESP256/ESP384/ESP512\n  ESP256: -9, // COSE ESP256 (ECDSA using P-256 curve and SHA-256)\n  ESP384: -51, // COSE ESP384 (ECDSA using P-384 curve and SHA-384)\n  ESP512: -52, // COSE ESP512 (ECDSA using P-521 curve and SHA-512)\n  ES256K: -47, // ECDSA using secp256k1 curve and SHA-256\n\n  // RSA algorithms - RFC 7518\n  RS256: -257, // RSASSA-PKCS1-v1_5 using SHA-256\n  RS384: -258, // RSASSA-PKCS1-v1_5 using SHA-384\n  RS512: -259, // RSASSA-PKCS1-v1_5 using SHA-512\n  PS256: -37, // RSASSA-PSS using SHA-256 and MGF1 with SHA-256\n  PS384: -38, // RSASSA-PSS using SHA-384 and MGF1 with SHA-384\n  PS512: -39, // RSASSA-PSS using SHA-512 and MGF1 with SHA-512\n\n  // Mac\n  HS256: 5,\n  HS384: 6,\n  HS512: 7,\n} as const\n\nexport const zKnownCoseSignatureAlgorithm = z.enum(KnownCoseSignatureAlgorithms)\nexport type KnownCoseSignatureAlgorithm = z.output<typeof zKnownCoseSignatureAlgorithm>\n\nexport function isKnownCoseSignatureAlgorithm(coseAlg: number): coseAlg is KnownCoseSignatureAlgorithm {\n  return Object.values(KnownCoseSignatureAlgorithms).includes(coseAlg as KnownCoseSignatureAlgorithm)\n}\n\nconst coseToJwaSignatureAlgorithmMap: Partial<Record<KnownCoseSignatureAlgorithm, KnownJwaSignatureAlgorithm>> = {\n  [KnownCoseSignatureAlgorithms.Ed25519]: KnownJwaSignatureAlgorithms.Ed25519,\n  [KnownCoseSignatureAlgorithms.ESP256]: KnownJwaSignatureAlgorithms.ES256,\n  [KnownCoseSignatureAlgorithms.ESP384]: KnownJwaSignatureAlgorithms.ES384,\n  [KnownCoseSignatureAlgorithms.ESP512]: KnownJwaSignatureAlgorithms.ES512,\n  [KnownCoseSignatureAlgorithms.ES256K]: KnownJwaSignatureAlgorithms.ES256K,\n  [KnownCoseSignatureAlgorithms.RS256]: KnownJwaSignatureAlgorithms.RS256,\n  [KnownCoseSignatureAlgorithms.RS384]: KnownJwaSignatureAlgorithms.RS384,\n  [KnownCoseSignatureAlgorithms.RS512]: KnownJwaSignatureAlgorithms.RS512,\n  [KnownCoseSignatureAlgorithms.PS256]: KnownJwaSignatureAlgorithms.PS256,\n  [KnownCoseSignatureAlgorithms.PS384]: KnownJwaSignatureAlgorithms.PS384,\n  [KnownCoseSignatureAlgorithms.PS512]: KnownJwaSignatureAlgorithms.PS512,\n  [KnownCoseSignatureAlgorithms.HS256]: KnownJwaSignatureAlgorithms.HS256,\n  [KnownCoseSignatureAlgorithms.HS384]: KnownJwaSignatureAlgorithms.HS384,\n  [KnownCoseSignatureAlgorithms.HS512]: KnownJwaSignatureAlgorithms.HS512,\n\n  // NOTE: we map the deprecated EdDSA COSE algorithm (-8) to JWA algorithm Ed25519, as Credo has never supported other EdDSA variants\n  [KnownCoseSignatureAlgorithms.EdDSA]: KnownJwaSignatureAlgorithms.Ed25519,\n\n  // NOTE: we map the deprecated ES256 COSE algorithm (-7) to JWA algorithm ES256, as Credo has never supported other ES256 variants besides P-256\n  [KnownCoseSignatureAlgorithms.ES256]: KnownJwaSignatureAlgorithms.ES256,\n} as const\n\nexport function knownJwaFromCoseSignatureAlgorithm(coseAlg: KnownCoseSignatureAlgorithm): KnownJwaSignatureAlgorithm {\n  const jwaAlg = coseToJwaSignatureAlgorithmMap[coseAlg]\n  if (!jwaAlg) {\n    throw new KeyManagementError(\n      `Cannot map coseAlg ${coseAlg} (${Object.entries(KnownCoseSignatureAlgorithms).find(([key, possibleCoseAlg]) => coseAlg === possibleCoseAlg)?.[0]}) to JWA signature algorithm, no JWA signature algorithm known for COSE algorithm.`\n    )\n  }\n\n  return jwaAlg\n}\n"],"mappings":";;;;AAIA,MAAa,+BAA+B;CAE1C,SAAS;CACT,OAAO;CAGP,OAAO;CAGP,OAAO;CAIP,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CAGR,OAAO;CACP,OAAO;CACP,OAAO;CACP,OAAO;CACP,OAAO;CACP,OAAO;CAGP,OAAO;CACP,OAAO;CACP,OAAO;AACT;AAE4CA,IAAE,KAAK,4BAA4B;AAG/E,SAAgB,8BAA8B,SAAyD;CACrG,OAAO,OAAO,OAAO,4BAA4B,CAAC,CAAC,SAAS,OAAsC;AACpG;AAEA,MAAM,iCAA2G;EAC9G,6BAA6B,UAAU,4BAA4B;EACnE,6BAA6B,SAAS,4BAA4B;EAClE,6BAA6B,SAAS,4BAA4B;EAClE,6BAA6B,SAAS,4BAA4B;EAClE,6BAA6B,SAAS,4BAA4B;EAClE,6BAA6B,QAAQ,4BAA4B;EACjE,6BAA6B,QAAQ,4BAA4B;EACjE,6BAA6B,QAAQ,4BAA4B;EACjE,6BAA6B,QAAQ,4BAA4B;EACjE,6BAA6B,QAAQ,4BAA4B;EACjE,6BAA6B,QAAQ,4BAA4B;EACjE,6BAA6B,QAAQ,4BAA4B;EACjE,6BAA6B,QAAQ,4BAA4B;EACjE,6BAA6B,QAAQ,4BAA4B;EAGjE,6BAA6B,QAAQ,4BAA4B;EAGjE,6BAA6B,QAAQ,4BAA4B;AACpE;AAEA,SAAgB,mCAAmC,SAAkE;CACnH,MAAM,SAAS,+BAA+B;CAC9C,IAAI,CAAC,QACH,MAAM,IAAI,mBACR,sBAAsB,QAAQ,IAAI,OAAO,QAAQ,4BAA4B,CAAC,CAAC,MAAM,CAAC,KAAK,qBAAqB,YAAY,eAAe,CAAC,GAAG,GAAG,mFACpJ;CAGF,OAAO;AACT"}