|
| 1 | +import type { AlgorithmName } from 'pq-oid'; |
| 2 | +import { OID } from 'pq-oid'; |
| 3 | +import { UnknownAlgorithmError } from './errors'; |
| 4 | +import type { |
| 5 | + CoseIdentifier, |
| 6 | + IdentifierRecord, |
| 7 | + IdentifierRecordMap, |
| 8 | + JoseIdentifier, |
| 9 | + X509ParametersPolicy, |
| 10 | +} from './types'; |
| 11 | + |
| 12 | +const DEFAULT_X509_PARAMETERS_POLICY: Readonly<X509ParametersPolicy> = Object.freeze({ |
| 13 | + defaultParametersEncoding: 'absent', |
| 14 | + acceptNull: true, |
| 15 | + acceptAbsent: true, |
| 16 | +}); |
| 17 | + |
| 18 | +const JOSE_IDENTIFIERS: Readonly<Partial<Record<AlgorithmName, JoseIdentifier>>> = Object.freeze({ |
| 19 | + 'ML-DSA-44': 'ML-DSA-44', |
| 20 | + 'ML-DSA-65': 'ML-DSA-65', |
| 21 | + 'ML-DSA-87': 'ML-DSA-87', |
| 22 | +}); |
| 23 | + |
| 24 | +const COSE_IDENTIFIERS: Readonly<Partial<Record<AlgorithmName, CoseIdentifier>>> = Object.freeze({ |
| 25 | + 'ML-DSA-44': -48, |
| 26 | + 'ML-DSA-65': -49, |
| 27 | + 'ML-DSA-87': -50, |
| 28 | +}); |
| 29 | + |
| 30 | +const ALGORITHM_NAMES = [ |
| 31 | + 'ML-KEM-512', |
| 32 | + 'ML-KEM-768', |
| 33 | + 'ML-KEM-1024', |
| 34 | + 'ML-DSA-44', |
| 35 | + 'ML-DSA-65', |
| 36 | + 'ML-DSA-87', |
| 37 | + 'SLH-DSA-SHA2-128s', |
| 38 | + 'SLH-DSA-SHA2-128f', |
| 39 | + 'SLH-DSA-SHA2-192s', |
| 40 | + 'SLH-DSA-SHA2-192f', |
| 41 | + 'SLH-DSA-SHA2-256s', |
| 42 | + 'SLH-DSA-SHA2-256f', |
| 43 | + 'SLH-DSA-SHAKE-128s', |
| 44 | + 'SLH-DSA-SHAKE-128f', |
| 45 | + 'SLH-DSA-SHAKE-192s', |
| 46 | + 'SLH-DSA-SHAKE-192f', |
| 47 | + 'SLH-DSA-SHAKE-256s', |
| 48 | + 'SLH-DSA-SHAKE-256f', |
| 49 | +] as const satisfies ReadonlyArray<AlgorithmName>; |
| 50 | + |
| 51 | +const IDENTIFIER_RECORDS = Object.freeze( |
| 52 | + ALGORITHM_NAMES.map((name) => |
| 53 | + Object.freeze({ |
| 54 | + name, |
| 55 | + jose: JOSE_IDENTIFIERS[name], |
| 56 | + cose: COSE_IDENTIFIERS[name], |
| 57 | + x509: DEFAULT_X509_PARAMETERS_POLICY, |
| 58 | + } satisfies IdentifierRecord), |
| 59 | + ), |
| 60 | +); |
| 61 | + |
| 62 | +const IDENTIFIER_RECORDS_BY_NAME: IdentifierRecordMap = Object.freeze( |
| 63 | + Object.fromEntries( |
| 64 | + IDENTIFIER_RECORDS.map( |
| 65 | + (record) => [record.name, record] satisfies [AlgorithmName, IdentifierRecord], |
| 66 | + ), |
| 67 | + ) as Record<AlgorithmName, IdentifierRecord>, |
| 68 | +); |
| 69 | + |
| 70 | +export function listRegistryAlgorithmNames(): readonly AlgorithmName[] { |
| 71 | + return ALGORITHM_NAMES; |
| 72 | +} |
| 73 | + |
| 74 | +export function listIdentifierRecords(): readonly IdentifierRecord[] { |
| 75 | + return IDENTIFIER_RECORDS; |
| 76 | +} |
| 77 | + |
| 78 | +export function getIdentifierRecord(name: AlgorithmName): IdentifierRecord { |
| 79 | + const record = IDENTIFIER_RECORDS_BY_NAME[name]; |
| 80 | + if (record === undefined) { |
| 81 | + throw new UnknownAlgorithmError(name); |
| 82 | + } |
| 83 | + return record; |
| 84 | +} |
| 85 | + |
| 86 | +export function deriveOidFromName(name: AlgorithmName): string { |
| 87 | + return OID.fromName(name); |
| 88 | +} |
0 commit comments