1'use strict'; 2 3const common = require('../common'); 4 5if (!common.hasCrypto) 6 common.skip('missing crypto'); 7 8const assert = require('assert'); 9const { webcrypto } = require('crypto'); 10const { subtle } = webcrypto; 11 12// This is only a partial test. The WebCrypto Web Platform Tests 13// will provide much greater coverage. 14 15// Test Encrypt/Decrypt RSA-OAEP 16{ 17 const buf = webcrypto.getRandomValues(new Uint8Array(50)); 18 19 async function test() { 20 const ec = new TextEncoder(); 21 const { publicKey, privateKey } = await subtle.generateKey({ 22 name: 'RSA-OAEP', 23 modulusLength: 2048, 24 publicExponent: new Uint8Array([1, 0, 1]), 25 hash: 'SHA-384', 26 }, true, ['encrypt', 'decrypt']); 27 28 const ciphertext = await subtle.encrypt({ 29 name: 'RSA-OAEP', 30 label: ec.encode('a label') 31 }, publicKey, buf); 32 33 const plaintext = await subtle.decrypt({ 34 name: 'RSA-OAEP', 35 label: ec.encode('a label') 36 }, privateKey, ciphertext); 37 38 assert.strictEqual( 39 Buffer.from(plaintext).toString('hex'), 40 Buffer.from(buf).toString('hex')); 41 } 42 43 test().then(common.mustCall()); 44} 45 46// Test Encrypt/Decrypt AES-CTR 47{ 48 const buf = webcrypto.getRandomValues(new Uint8Array(50)); 49 const counter = webcrypto.getRandomValues(new Uint8Array(16)); 50 51 async function test() { 52 const key = await subtle.generateKey({ 53 name: 'AES-CTR', 54 length: 256 55 }, true, ['encrypt', 'decrypt']); 56 57 const ciphertext = await subtle.encrypt( 58 { name: 'AES-CTR', counter, length: 64 }, key, buf, 59 ); 60 61 const plaintext = await subtle.decrypt( 62 { name: 'AES-CTR', counter, length: 64 }, key, ciphertext, 63 ); 64 65 assert.strictEqual( 66 Buffer.from(plaintext).toString('hex'), 67 Buffer.from(buf).toString('hex')); 68 } 69 70 test().then(common.mustCall()); 71} 72 73// Test Encrypt/Decrypt AES-CBC 74{ 75 const buf = webcrypto.getRandomValues(new Uint8Array(50)); 76 const iv = webcrypto.getRandomValues(new Uint8Array(16)); 77 78 async function test() { 79 const key = await subtle.generateKey({ 80 name: 'AES-CBC', 81 length: 256 82 }, true, ['encrypt', 'decrypt']); 83 84 const ciphertext = await subtle.encrypt( 85 { name: 'AES-CBC', iv }, key, buf, 86 ); 87 88 const plaintext = await subtle.decrypt( 89 { name: 'AES-CBC', iv }, key, ciphertext, 90 ); 91 92 assert.strictEqual( 93 Buffer.from(plaintext).toString('hex'), 94 Buffer.from(buf).toString('hex')); 95 } 96 97 test().then(common.mustCall()); 98} 99 100// Test Encrypt/Decrypt AES-GCM 101{ 102 const buf = webcrypto.getRandomValues(new Uint8Array(50)); 103 const iv = webcrypto.getRandomValues(new Uint8Array(12)); 104 105 async function test() { 106 const key = await subtle.generateKey({ 107 name: 'AES-GCM', 108 length: 256 109 }, true, ['encrypt', 'decrypt']); 110 111 const ciphertext = await subtle.encrypt( 112 { name: 'AES-GCM', iv }, key, buf, 113 ); 114 115 const plaintext = await subtle.decrypt( 116 { name: 'AES-GCM', iv }, key, ciphertext, 117 ); 118 119 assert.strictEqual( 120 Buffer.from(plaintext).toString('hex'), 121 Buffer.from(buf).toString('hex')); 122 } 123 124 test().then(common.mustCall()); 125} 126