|
| 1 | +// META: title=WebCrypto API: supports method tests for algorithms in https://wicg.github.io/webcrypto-modern-algos/ |
| 2 | +// META: script=util/helpers.js |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +const modernAlgorithms = { |
| 7 | + // Asymmetric algorithms |
| 8 | + 'ML-DSA-44': { |
| 9 | + operations: ['generateKey', 'importKey', 'sign', 'verify'], |
| 10 | + }, |
| 11 | + 'ML-DSA-65': { |
| 12 | + operations: ['generateKey', 'importKey', 'sign', 'verify'], |
| 13 | + }, |
| 14 | + 'ML-DSA-87': { |
| 15 | + operations: ['generateKey', 'importKey', 'sign', 'verify'], |
| 16 | + }, |
| 17 | + 'ML-KEM-512': { |
| 18 | + operations: [ |
| 19 | + 'generateKey', 'importKey', 'encapsulateKey', 'encapsulateBits', |
| 20 | + 'decapsulateKey', 'decapsulateBits' |
| 21 | + ], |
| 22 | + }, |
| 23 | + 'ML-KEM-768': { |
| 24 | + operations: [ |
| 25 | + 'generateKey', 'importKey', 'encapsulateKey', 'encapsulateBits', |
| 26 | + 'decapsulateKey', 'decapsulateBits' |
| 27 | + ], |
| 28 | + }, |
| 29 | + 'ML-KEM-1024': { |
| 30 | + operations: [ |
| 31 | + 'generateKey', 'importKey', 'encapsulateKey', 'encapsulateBits', |
| 32 | + 'decapsulateKey', 'decapsulateBits' |
| 33 | + ], |
| 34 | + }, |
| 35 | + |
| 36 | + // Symmetric algorithms |
| 37 | + 'ChaCha20-Poly1305': { |
| 38 | + operations: ['generateKey', 'importKey', 'encrypt', 'decrypt'], |
| 39 | + encryptParams: {name: 'ChaCha20-Poly1305', iv: new Uint8Array(12)}, |
| 40 | + }, |
| 41 | + |
| 42 | +}; |
| 43 | + |
| 44 | +const operations = [ |
| 45 | + 'generateKey', |
| 46 | + 'importKey', |
| 47 | + 'sign', |
| 48 | + 'verify', |
| 49 | + 'encrypt', |
| 50 | + 'decrypt', |
| 51 | + 'deriveBits', |
| 52 | + 'digest', |
| 53 | + 'encapsulateKey', |
| 54 | + 'encapsulateBits', |
| 55 | + 'decapsulateKey', |
| 56 | + 'decapsulateBits', |
| 57 | +]; |
| 58 | + |
| 59 | +// Test that supports method exists and is a static method |
| 60 | +test(() => { |
| 61 | + assert_true( |
| 62 | + typeof SubtleCrypto.supports === 'function', |
| 63 | + 'SubtleCrypto.supports should be a function'); |
| 64 | +}, 'SubtleCrypto.supports method exists'); |
| 65 | + |
| 66 | + |
| 67 | +// Test standard WebCrypto algorithms for requested operations |
| 68 | +for (const [algorithmName, algorithmInfo] of Object.entries(modernAlgorithms)) { |
| 69 | + for (const operation of operations) { |
| 70 | + promise_test(async (t) => { |
| 71 | + const isSupported = algorithmInfo.operations.includes(operation); |
| 72 | + |
| 73 | + // Use appropriate algorithm parameters for each operation |
| 74 | + let algorithm; |
| 75 | + let lengthOrAdditionalAlgorithm; |
| 76 | + switch (operation) { |
| 77 | + case 'generateKey': |
| 78 | + algorithm = algorithmInfo.keyGenParams || algorithmName; |
| 79 | + break; |
| 80 | + case 'importKey': |
| 81 | + algorithm = algorithmInfo.importParams || algorithmName; |
| 82 | + break; |
| 83 | + case 'sign': |
| 84 | + case 'verify': |
| 85 | + algorithm = algorithmInfo.signParams || algorithmName; |
| 86 | + break; |
| 87 | + case 'encrypt': |
| 88 | + case 'decrypt': |
| 89 | + algorithm = algorithmInfo.encryptParams || algorithmName; |
| 90 | + break; |
| 91 | + case 'deriveBits': |
| 92 | + algorithm = algorithmInfo.deriveBitsParams || algorithmName; |
| 93 | + if (algorithm?.public instanceof Promise) { |
| 94 | + algorithm.public = (await algorithm.public).publicKey; |
| 95 | + } |
| 96 | + if (algorithmName === 'PBKDF2' || algorithmName === 'HKDF') { |
| 97 | + lengthOrAdditionalAlgorithm = 256; |
| 98 | + } |
| 99 | + break; |
| 100 | + case 'digest': |
| 101 | + algorithm = algorithmName; |
| 102 | + break; |
| 103 | + case 'encapsulateKey': |
| 104 | + case 'encapsulateBits': |
| 105 | + case 'decapsulateKey': |
| 106 | + case 'decapsulateBits': |
| 107 | + algorithm = algorithmName; |
| 108 | + if (operation === 'encapsulateKey' || operation === 'decapsulateKey') { |
| 109 | + lengthOrAdditionalAlgorithm = { name: 'AES-GCM', length: 256 }; |
| 110 | + } |
| 111 | + break; |
| 112 | + default: |
| 113 | + algorithm = algorithmName; |
| 114 | + } |
| 115 | + |
| 116 | + const result = SubtleCrypto.supports(operation, algorithm, lengthOrAdditionalAlgorithm); |
| 117 | + |
| 118 | + if (isSupported) { |
| 119 | + assert_true(result, `${algorithmName} should support ${operation}`); |
| 120 | + } else { |
| 121 | + assert_false( |
| 122 | + result, `${algorithmName} should not support ${operation}`); |
| 123 | + } |
| 124 | + }, `supports(${operation}, ${algorithmName})`); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// Test some algorithm objects with valid parameters |
| 129 | +test(() => { |
| 130 | + assert_true( |
| 131 | + SubtleCrypto.supports('encrypt', { |
| 132 | + name: 'ChaCha20-Poly1305', |
| 133 | + iv: new Uint8Array(12), |
| 134 | + tagLength: 128, |
| 135 | + }), |
| 136 | + 'ChaCha20-Poly1305 encrypt with valid tagLength'); |
| 137 | +}, 'supports returns true for algorithm objects with valid parameters'); |
| 138 | + |
| 139 | +// Test some algorithm objects with invalid parameters |
| 140 | +test(() => { |
| 141 | + assert_false( |
| 142 | + SubtleCrypto.supports('encrypt', { |
| 143 | + name: 'ChaCha20-Poly1305', |
| 144 | + iv: new Uint8Array(12), |
| 145 | + tagLength: 100, |
| 146 | + }), |
| 147 | + 'ChaCha20-Poly1305 encrypt with invalid tagLength'); |
| 148 | + |
| 149 | + assert_false( |
| 150 | + SubtleCrypto.supports('encrypt', { |
| 151 | + name: 'ChaCha20-Poly1305', |
| 152 | + iv: new Uint8Array(10), |
| 153 | + tagLength: 128, |
| 154 | + }), |
| 155 | + 'ChaCha20-Poly1305 encrypt with invalid iv'); |
| 156 | +}, 'supports returns false for algorithm objects with invalid parameters'); |
| 157 | + |
| 158 | + |
| 159 | +done(); |
0 commit comments