({ name, publicKeyPem, privateKeyPem, results })
| 24 | } |
| 25 | |
| 26 | async function testEncapsulateKey({ name, publicKeyPem, privateKeyPem, results }) { |
| 27 | const [ |
| 28 | publicKey, |
| 29 | noEncapsulatePublicKey, |
| 30 | privateKey, |
| 31 | ] = await Promise.all([ |
| 32 | crypto.createPublicKey(publicKeyPem) |
| 33 | .toCryptoKey(name, false, ['encapsulateKey']), |
| 34 | crypto.createPublicKey(publicKeyPem) |
| 35 | .toCryptoKey(name, false, ['encapsulateBits']), |
| 36 | crypto.createPrivateKey(privateKeyPem) |
| 37 | .toCryptoKey(name, false, ['decapsulateKey']), |
| 38 | ]); |
| 39 | |
| 40 | // Test successful encapsulation |
| 41 | const encapsulated = await subtle.encapsulateKey( |
| 42 | { name }, |
| 43 | publicKey, |
| 44 | 'HKDF', |
| 45 | false, |
| 46 | ['deriveBits'] |
| 47 | ); |
| 48 | |
| 49 | assert.strictEqual(Object.getPrototypeOf(encapsulated), Object.prototype); |
| 50 | assert(encapsulated.sharedKey instanceof CryptoKey); |
| 51 | assert(encapsulated.ciphertext instanceof ArrayBuffer); |
| 52 | assert.strictEqual(encapsulated.sharedKey.type, 'secret'); |
| 53 | assert.strictEqual(encapsulated.sharedKey.algorithm.name, 'HKDF'); |
| 54 | assert.strictEqual(encapsulated.sharedKey.extractable, false); |
| 55 | assert.deepStrictEqual(encapsulated.sharedKey.usages, ['deriveBits']); |
| 56 | |
| 57 | // Verify ciphertext length matches expected for algorithm |
| 58 | assert.strictEqual(encapsulated.ciphertext.byteLength, results.ciphertext.byteLength); |
| 59 | |
| 60 | // Test with different shared key algorithm |
| 61 | const encapsulated2 = await subtle.encapsulateKey( |
| 62 | { name }, |
| 63 | publicKey, |
| 64 | { name: 'HMAC', hash: 'SHA-256' }, |
| 65 | false, |
| 66 | ['sign', 'verify'] |
| 67 | ); |
| 68 | |
| 69 | assert.strictEqual(Object.getPrototypeOf(encapsulated2), Object.prototype); |
| 70 | assert(encapsulated2.sharedKey instanceof CryptoKey); |
| 71 | assert.strictEqual(encapsulated2.sharedKey.algorithm.name, 'HMAC'); |
| 72 | assert.strictEqual(encapsulated2.sharedKey.extractable, false); |
| 73 | |
| 74 | const encapsulated3 = await subtle.encapsulateKey( |
| 75 | { name }, |
| 76 | publicKey, |
| 77 | { name: 'HMAC', hash: 'SHA-256', length: 255 }, |
| 78 | false, |
| 79 | ['sign', 'verify'] |
| 80 | ); |
| 81 | |
| 82 | assert.strictEqual(encapsulated3.sharedKey.algorithm.name, 'HMAC'); |
| 83 | assert.strictEqual(encapsulated3.sharedKey.algorithm.length, 255); |
no test coverage detected