11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciif (!common.hasCrypto)
61cb0ef41Sopenharmony_ci  common.skip('missing crypto');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst { webcrypto } = require('crypto');
101cb0ef41Sopenharmony_ciconst { subtle } = webcrypto;
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// This is only a partial test. The WebCrypto Web Platform Tests
131cb0ef41Sopenharmony_ci// will provide much greater coverage.
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// Test Encrypt/Decrypt RSA-OAEP
161cb0ef41Sopenharmony_ci{
171cb0ef41Sopenharmony_ci  const buf = webcrypto.getRandomValues(new Uint8Array(50));
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  async function test() {
201cb0ef41Sopenharmony_ci    const ec = new TextEncoder();
211cb0ef41Sopenharmony_ci    const { publicKey, privateKey } = await subtle.generateKey({
221cb0ef41Sopenharmony_ci      name: 'RSA-OAEP',
231cb0ef41Sopenharmony_ci      modulusLength: 2048,
241cb0ef41Sopenharmony_ci      publicExponent: new Uint8Array([1, 0, 1]),
251cb0ef41Sopenharmony_ci      hash: 'SHA-384',
261cb0ef41Sopenharmony_ci    }, true, ['encrypt', 'decrypt']);
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci    const ciphertext = await subtle.encrypt({
291cb0ef41Sopenharmony_ci      name: 'RSA-OAEP',
301cb0ef41Sopenharmony_ci      label: ec.encode('a label')
311cb0ef41Sopenharmony_ci    }, publicKey, buf);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    const plaintext = await subtle.decrypt({
341cb0ef41Sopenharmony_ci      name: 'RSA-OAEP',
351cb0ef41Sopenharmony_ci      label: ec.encode('a label')
361cb0ef41Sopenharmony_ci    }, privateKey, ciphertext);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci    assert.strictEqual(
391cb0ef41Sopenharmony_ci      Buffer.from(plaintext).toString('hex'),
401cb0ef41Sopenharmony_ci      Buffer.from(buf).toString('hex'));
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  test().then(common.mustCall());
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci// Test Encrypt/Decrypt AES-CTR
471cb0ef41Sopenharmony_ci{
481cb0ef41Sopenharmony_ci  const buf = webcrypto.getRandomValues(new Uint8Array(50));
491cb0ef41Sopenharmony_ci  const counter = webcrypto.getRandomValues(new Uint8Array(16));
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  async function test() {
521cb0ef41Sopenharmony_ci    const key = await subtle.generateKey({
531cb0ef41Sopenharmony_ci      name: 'AES-CTR',
541cb0ef41Sopenharmony_ci      length: 256
551cb0ef41Sopenharmony_ci    }, true, ['encrypt', 'decrypt']);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci    const ciphertext = await subtle.encrypt(
581cb0ef41Sopenharmony_ci      { name: 'AES-CTR', counter, length: 64 }, key, buf,
591cb0ef41Sopenharmony_ci    );
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci    const plaintext = await subtle.decrypt(
621cb0ef41Sopenharmony_ci      { name: 'AES-CTR', counter, length: 64 }, key, ciphertext,
631cb0ef41Sopenharmony_ci    );
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci    assert.strictEqual(
661cb0ef41Sopenharmony_ci      Buffer.from(plaintext).toString('hex'),
671cb0ef41Sopenharmony_ci      Buffer.from(buf).toString('hex'));
681cb0ef41Sopenharmony_ci  }
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  test().then(common.mustCall());
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci// Test Encrypt/Decrypt AES-CBC
741cb0ef41Sopenharmony_ci{
751cb0ef41Sopenharmony_ci  const buf = webcrypto.getRandomValues(new Uint8Array(50));
761cb0ef41Sopenharmony_ci  const iv = webcrypto.getRandomValues(new Uint8Array(16));
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  async function test() {
791cb0ef41Sopenharmony_ci    const key = await subtle.generateKey({
801cb0ef41Sopenharmony_ci      name: 'AES-CBC',
811cb0ef41Sopenharmony_ci      length: 256
821cb0ef41Sopenharmony_ci    }, true, ['encrypt', 'decrypt']);
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci    const ciphertext = await subtle.encrypt(
851cb0ef41Sopenharmony_ci      { name: 'AES-CBC', iv }, key, buf,
861cb0ef41Sopenharmony_ci    );
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci    const plaintext = await subtle.decrypt(
891cb0ef41Sopenharmony_ci      { name: 'AES-CBC', iv }, key, ciphertext,
901cb0ef41Sopenharmony_ci    );
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci    assert.strictEqual(
931cb0ef41Sopenharmony_ci      Buffer.from(plaintext).toString('hex'),
941cb0ef41Sopenharmony_ci      Buffer.from(buf).toString('hex'));
951cb0ef41Sopenharmony_ci  }
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  test().then(common.mustCall());
981cb0ef41Sopenharmony_ci}
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci// Test Encrypt/Decrypt AES-GCM
1011cb0ef41Sopenharmony_ci{
1021cb0ef41Sopenharmony_ci  const buf = webcrypto.getRandomValues(new Uint8Array(50));
1031cb0ef41Sopenharmony_ci  const iv = webcrypto.getRandomValues(new Uint8Array(12));
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci  async function test() {
1061cb0ef41Sopenharmony_ci    const key = await subtle.generateKey({
1071cb0ef41Sopenharmony_ci      name: 'AES-GCM',
1081cb0ef41Sopenharmony_ci      length: 256
1091cb0ef41Sopenharmony_ci    }, true, ['encrypt', 'decrypt']);
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci    const ciphertext = await subtle.encrypt(
1121cb0ef41Sopenharmony_ci      { name: 'AES-GCM', iv }, key, buf,
1131cb0ef41Sopenharmony_ci    );
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci    const plaintext = await subtle.decrypt(
1161cb0ef41Sopenharmony_ci      { name: 'AES-GCM', iv }, key, ciphertext,
1171cb0ef41Sopenharmony_ci    );
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci    assert.strictEqual(
1201cb0ef41Sopenharmony_ci      Buffer.from(plaintext).toString('hex'),
1211cb0ef41Sopenharmony_ci      Buffer.from(buf).toString('hex'));
1221cb0ef41Sopenharmony_ci  }
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci  test().then(common.mustCall());
1251cb0ef41Sopenharmony_ci}
126