{"version":3,"file":"algorithm.mjs","names":[],"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  // Maps to Ed25519 as it's the most common use case (similar to WebAuthn's approach)\n  EdDSA: -19,\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} 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;CAIP,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;CACR;AAED,MAAa,+BAA+B,EAAE,KAAK,6BAA6B;AAGhF,SAAgB,8BAA8B,SAAyD;AACrG,QAAO,OAAO,OAAO,6BAA6B,CAAC,SAAS,QAAuC;;AAGrG,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;CACnE;AAED,SAAgB,mCAAmC,SAAkE;CACnH,MAAM,SAAS,+BAA+B;AAC9C,KAAI,CAAC,OACH,OAAM,IAAI,mBACR,sBAAsB,QAAQ,IAAI,OAAO,QAAQ,6BAA6B,CAAC,MAAM,CAAC,KAAK,qBAAqB,YAAY,gBAAgB,GAAG,GAAG,oFACnJ;AAGH,QAAO"}