33 */
44import * as base64url from 'base64url-universal' ;
55import { ml_dsa44 } from '@noble/post-quantum/ml-dsa.js' ;
6- import { ALGORITHM , NIST_SECURITY_LEVEL_2 } from './constants.js' ;
6+ import { ALGORITHM } from './constants.js' ;
77
8- // Maps NIST security level to noble implementation and algorithm name
9- export const NIST_LEVEL_MAP = new Map ( [
10- [ 2 , { impl : ml_dsa44 , name : ALGORITHM . MLDSA44 } ] ,
8+ // Maps algorithm name to noble implementation
9+ export const ALGORITHM_MAP = new Map ( [
10+ [ ALGORITHM . MLDSA44 , { impl : ml_dsa44 } ] ,
1111] ) ;
1212
1313export class CryptoKey {
@@ -34,7 +34,7 @@ export class CryptoKey {
3434 return this . _extractable ;
3535 }
3636
37- // algorithm: KeyAlgorithm object (e.g. {name: "MLDSA", nistSecurityLevel: 2 })
37+ // algorithm: KeyAlgorithm object (e.g. {name: "ML-DSA-44" })
3838 get algorithm ( ) {
3939 return this . _algorithm ;
4040 }
@@ -48,7 +48,7 @@ export class CryptoKey {
4848// Signs data using ML-DSA.
4949export async function mldsaSign ( key , data ) {
5050 assertMldsaKey ( key , 'private' , 'sign' ) ;
51- const { impl} = getImpl ( key . algorithm . nistSecurityLevel ) ;
51+ const { impl} = getImpl ( key . algorithm . name ) ;
5252 const msg = toUint8Array ( data ) ;
5353 const sig = impl . sign ( msg , key . _keyBytes ) ;
5454 return sig . buffer . slice ( sig . byteOffset , sig . byteOffset + sig . byteLength ) ;
@@ -57,7 +57,7 @@ export async function mldsaSign(key, data) {
5757// Verifies a signature over data using ML-DSA.
5858export async function mldsaVerify ( key , signature , data ) {
5959 assertMldsaKey ( key , 'public' , 'verify' ) ;
60- const { impl} = getImpl ( key . algorithm . nistSecurityLevel ) ;
60+ const { impl} = getImpl ( key . algorithm . name ) ;
6161 const msg = toUint8Array ( data ) ;
6262 const sig = toUint8Array ( signature ) ;
6363 try {
@@ -69,12 +69,9 @@ export async function mldsaVerify(key, signature, data) {
6969
7070// Generates a new ML-DSA key pair from a 32-byte seed.
7171export function mldsaGenerateKey ( algorithm , extractable , keyUsages , seed ) {
72- const nistSecurityLevel = resolveNistSecurityLevel ( algorithm ) ;
73- const { impl} = getImpl ( nistSecurityLevel ) ;
74- const keyAlgorithm = {
75- name : NIST_LEVEL_MAP . get ( nistSecurityLevel ) . name ,
76- nistSecurityLevel
77- } ;
72+ const algorithmName = resolveAlgorithmName ( algorithm ) ;
73+ const { impl} = getImpl ( algorithmName ) ;
74+ const keyAlgorithm = { name : algorithmName } ;
7875
7976 const { publicKey : pubBytes , secretKey : secBytes } = impl . keygen ( seed ) ;
8077
@@ -101,12 +98,9 @@ export function mldsaGenerateKey(algorithm, extractable, keyUsages, seed) {
10198// Imports an ML-DSA key from various formats.
10299export function mldsaImportKey ( format , keyData , algorithm , extractable ,
103100 keyUsages ) {
104- const nistSecurityLevel = resolveNistSecurityLevel ( algorithm ) ;
105- const { impl} = getImpl ( nistSecurityLevel ) ;
106- const keyAlgorithm = {
107- name : NIST_LEVEL_MAP . get ( nistSecurityLevel ) . name ,
108- nistSecurityLevel
109- } ;
101+ const algorithmName = resolveAlgorithmName ( algorithm ) ;
102+ const { impl} = getImpl ( algorithmName ) ;
103+ const keyAlgorithm = { name : algorithmName } ;
110104
111105 if ( format === 'spki' || format === 'pkcs8' ) {
112106 throw new DOMException (
@@ -152,7 +146,7 @@ export function mldsaImportKey(format, keyData, algorithm, extractable,
152146 }
153147
154148 if ( format === 'jwk' ) {
155- return importJwk ( { keyData, keyAlgorithm, nistSecurityLevel , impl,
149+ return importJwk ( { keyData, keyAlgorithm, algorithmName , impl,
156150 extractable, keyUsages} ) ;
157151 }
158152
@@ -165,8 +159,7 @@ export function mldsaExportKey(format, key) {
165159 if ( ! key . extractable ) {
166160 throw new DOMException ( 'Key is not extractable.' , 'InvalidAccessError' ) ;
167161 }
168- const { nistSecurityLevel} = key . algorithm ;
169- const { name} = getImpl ( nistSecurityLevel ) ;
162+ const { name} = key . algorithm ;
170163
171164 if ( format === 'spki' || format === 'pkcs8' ) {
172165 throw new DOMException (
@@ -233,27 +226,26 @@ export function mldsaExportKey(format, key) {
233226 'NotSupportedError' ) ;
234227}
235228
236- // Resolves nistSecurityLevel from an algorithm object or integer .
237- export function resolveNistSecurityLevel ( algorithm ) {
238- if ( algorithm ?. nistSecurityLevel ) {
239- return algorithm . nistSecurityLevel ;
229+ // Resolves algorithm name from an algorithm object or string .
230+ export function resolveAlgorithmName ( algorithm ) {
231+ if ( typeof algorithm === 'string' ) {
232+ return algorithm ;
240233 }
241- if ( algorithm . name === ALGORITHM . MLDSA44 ) {
242- return NIST_SECURITY_LEVEL_2 ;
234+ if ( algorithm ? .name ) {
235+ return algorithm . name ;
243236 }
244237 throw new DOMException (
245- `Unknown NIST security level for given algorithm "${ algorithm } ". ` +
246- 'Only ML-DSA-44 (level 2) is supported.' ,
238+ `Unknown algorithm "${ algorithm } ". Only ML-DSA-44 is supported.` ,
247239 'NotSupportedError' ) ;
248240}
249241
250- // Returns the noble implementation and name for a given NIST security level .
251- export function getImpl ( nistSecurityLevel ) {
252- const entry = NIST_LEVEL_MAP . get ( nistSecurityLevel ) ;
242+ // Returns the noble implementation for a given algorithm name .
243+ export function getImpl ( algorithmName ) {
244+ const entry = ALGORITHM_MAP . get ( algorithmName ) ;
253245 if ( ! entry ) {
254246 throw new DOMException (
255- `Unsupported NIST security level "${ nistSecurityLevel } ". ` +
256- 'Only ML-DSA-44 (level 2) is supported.' ,
247+ `Unsupported algorithm "${ algorithmName } ". ` +
248+ 'Only ML-DSA-44 is supported.' ,
257249 'NotSupportedError' ) ;
258250 }
259251 return entry ;
@@ -287,8 +279,8 @@ export function toUint8Array(data) {
287279
288280// Imports a JWK-formatted key per webcryptomldsa spec Section 7.4.4.
289281function importJwk ( {
290- keyData, keyAlgorithm, nistSecurityLevel , impl, extractable, keyUsages} ) {
291- const { name} = getImpl ( nistSecurityLevel ) ;
282+ keyData, keyAlgorithm, algorithmName , impl, extractable, keyUsages} ) {
283+ const { name} = keyAlgorithm ;
292284 const jwk = keyData ;
293285
294286 if ( jwk . kty !== 'AKP' ) {
0 commit comments