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 { subtle } = require('crypto').webcrypto;
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci// This is only a partial test. The WebCrypto Web Platform Tests
121cb0ef41Sopenharmony_ci// will provide much greater coverage.
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci// Test Sign/Verify RSASSA-PKCS1-v1_5
151cb0ef41Sopenharmony_ci{
161cb0ef41Sopenharmony_ci  async function test(data) {
171cb0ef41Sopenharmony_ci    const ec = new TextEncoder();
181cb0ef41Sopenharmony_ci    const { publicKey, privateKey } = await subtle.generateKey({
191cb0ef41Sopenharmony_ci      name: 'RSASSA-PKCS1-v1_5',
201cb0ef41Sopenharmony_ci      modulusLength: 1024,
211cb0ef41Sopenharmony_ci      publicExponent: new Uint8Array([1, 0, 1]),
221cb0ef41Sopenharmony_ci      hash: 'SHA-256'
231cb0ef41Sopenharmony_ci    }, true, ['sign', 'verify']);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci    const signature = await subtle.sign({
261cb0ef41Sopenharmony_ci      name: 'RSASSA-PKCS1-v1_5'
271cb0ef41Sopenharmony_ci    }, privateKey, ec.encode(data));
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    assert(await subtle.verify({
301cb0ef41Sopenharmony_ci      name: 'RSASSA-PKCS1-v1_5'
311cb0ef41Sopenharmony_ci    }, publicKey, signature, ec.encode(data)));
321cb0ef41Sopenharmony_ci  }
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  test('hello world').then(common.mustCall());
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci// Test Sign/Verify RSA-PSS
381cb0ef41Sopenharmony_ci{
391cb0ef41Sopenharmony_ci  async function test(data) {
401cb0ef41Sopenharmony_ci    const ec = new TextEncoder();
411cb0ef41Sopenharmony_ci    const { publicKey, privateKey } = await subtle.generateKey({
421cb0ef41Sopenharmony_ci      name: 'RSA-PSS',
431cb0ef41Sopenharmony_ci      modulusLength: 4096,
441cb0ef41Sopenharmony_ci      publicExponent: new Uint8Array([1, 0, 1]),
451cb0ef41Sopenharmony_ci      hash: 'SHA-256'
461cb0ef41Sopenharmony_ci    }, true, ['sign', 'verify']);
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci    const signature = await subtle.sign({
491cb0ef41Sopenharmony_ci      name: 'RSA-PSS',
501cb0ef41Sopenharmony_ci      saltLength: 256,
511cb0ef41Sopenharmony_ci    }, privateKey, ec.encode(data));
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci    assert(await subtle.verify({
541cb0ef41Sopenharmony_ci      name: 'RSA-PSS',
551cb0ef41Sopenharmony_ci      saltLength: 256,
561cb0ef41Sopenharmony_ci    }, publicKey, signature, ec.encode(data)));
571cb0ef41Sopenharmony_ci  }
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  test('hello world').then(common.mustCall());
601cb0ef41Sopenharmony_ci}
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci// Test Sign/Verify ECDSA
631cb0ef41Sopenharmony_ci{
641cb0ef41Sopenharmony_ci  async function test(data) {
651cb0ef41Sopenharmony_ci    const ec = new TextEncoder();
661cb0ef41Sopenharmony_ci    const { publicKey, privateKey } = await subtle.generateKey({
671cb0ef41Sopenharmony_ci      name: 'ECDSA',
681cb0ef41Sopenharmony_ci      namedCurve: 'P-384',
691cb0ef41Sopenharmony_ci    }, true, ['sign', 'verify']);
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci    const signature = await subtle.sign({
721cb0ef41Sopenharmony_ci      name: 'ECDSA',
731cb0ef41Sopenharmony_ci      hash: 'SHA-384',
741cb0ef41Sopenharmony_ci    }, privateKey, ec.encode(data));
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci    assert(await subtle.verify({
771cb0ef41Sopenharmony_ci      name: 'ECDSA',
781cb0ef41Sopenharmony_ci      hash: 'SHA-384',
791cb0ef41Sopenharmony_ci    }, publicKey, signature, ec.encode(data)));
801cb0ef41Sopenharmony_ci  }
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  test('hello world').then(common.mustCall());
831cb0ef41Sopenharmony_ci}
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci// Test Sign/Verify HMAC
861cb0ef41Sopenharmony_ci{
871cb0ef41Sopenharmony_ci  async function test(data) {
881cb0ef41Sopenharmony_ci    const ec = new TextEncoder();
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci    const key = await subtle.generateKey({
911cb0ef41Sopenharmony_ci      name: 'HMAC',
921cb0ef41Sopenharmony_ci      length: 256,
931cb0ef41Sopenharmony_ci      hash: 'SHA-256'
941cb0ef41Sopenharmony_ci    }, true, ['sign', 'verify']);
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    const signature = await subtle.sign({
971cb0ef41Sopenharmony_ci      name: 'HMAC',
981cb0ef41Sopenharmony_ci    }, key, ec.encode(data));
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci    assert(await subtle.verify({
1011cb0ef41Sopenharmony_ci      name: 'HMAC',
1021cb0ef41Sopenharmony_ci    }, key, signature, ec.encode(data)));
1031cb0ef41Sopenharmony_ci  }
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci  test('hello world').then(common.mustCall());
1061cb0ef41Sopenharmony_ci}
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci// Test Sign/Verify Ed25519
1091cb0ef41Sopenharmony_ci{
1101cb0ef41Sopenharmony_ci  async function test(data) {
1111cb0ef41Sopenharmony_ci    const ec = new TextEncoder();
1121cb0ef41Sopenharmony_ci    const { publicKey, privateKey } = await subtle.generateKey({
1131cb0ef41Sopenharmony_ci      name: 'Ed25519',
1141cb0ef41Sopenharmony_ci    }, true, ['sign', 'verify']);
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci    const signature = await subtle.sign({
1171cb0ef41Sopenharmony_ci      name: 'Ed25519',
1181cb0ef41Sopenharmony_ci    }, privateKey, ec.encode(data));
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci    assert(await subtle.verify({
1211cb0ef41Sopenharmony_ci      name: 'Ed25519',
1221cb0ef41Sopenharmony_ci    }, publicKey, signature, ec.encode(data)));
1231cb0ef41Sopenharmony_ci  }
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  test('hello world').then(common.mustCall());
1261cb0ef41Sopenharmony_ci}
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci// Test Sign/Verify Ed448
1291cb0ef41Sopenharmony_ci{
1301cb0ef41Sopenharmony_ci  async function test(data) {
1311cb0ef41Sopenharmony_ci    const ec = new TextEncoder();
1321cb0ef41Sopenharmony_ci    const { publicKey, privateKey } = await subtle.generateKey({
1331cb0ef41Sopenharmony_ci      name: 'Ed448',
1341cb0ef41Sopenharmony_ci    }, true, ['sign', 'verify']);
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci    const signature = await subtle.sign({
1371cb0ef41Sopenharmony_ci      name: 'Ed448',
1381cb0ef41Sopenharmony_ci    }, privateKey, ec.encode(data));
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci    assert(await subtle.verify({
1411cb0ef41Sopenharmony_ci      name: 'Ed448',
1421cb0ef41Sopenharmony_ci    }, publicKey, signature, ec.encode(data)));
1431cb0ef41Sopenharmony_ci  }
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  test('hello world').then(common.mustCall());
1461cb0ef41Sopenharmony_ci}
147