1'use strict'; 2 3const common = require('../common'); 4 5if (!common.hasCrypto) 6 common.skip('missing crypto'); 7 8const assert = require('assert'); 9const { subtle } = require('crypto').webcrypto; 10 11// This is only a partial test. The WebCrypto Web Platform Tests 12// will provide much greater coverage. 13 14// Test Sign/Verify RSASSA-PKCS1-v1_5 15{ 16 async function test(data) { 17 const ec = new TextEncoder(); 18 const { publicKey, privateKey } = await subtle.generateKey({ 19 name: 'RSASSA-PKCS1-v1_5', 20 modulusLength: 1024, 21 publicExponent: new Uint8Array([1, 0, 1]), 22 hash: 'SHA-256' 23 }, true, ['sign', 'verify']); 24 25 const signature = await subtle.sign({ 26 name: 'RSASSA-PKCS1-v1_5' 27 }, privateKey, ec.encode(data)); 28 29 assert(await subtle.verify({ 30 name: 'RSASSA-PKCS1-v1_5' 31 }, publicKey, signature, ec.encode(data))); 32 } 33 34 test('hello world').then(common.mustCall()); 35} 36 37// Test Sign/Verify RSA-PSS 38{ 39 async function test(data) { 40 const ec = new TextEncoder(); 41 const { publicKey, privateKey } = await subtle.generateKey({ 42 name: 'RSA-PSS', 43 modulusLength: 4096, 44 publicExponent: new Uint8Array([1, 0, 1]), 45 hash: 'SHA-256' 46 }, true, ['sign', 'verify']); 47 48 const signature = await subtle.sign({ 49 name: 'RSA-PSS', 50 saltLength: 256, 51 }, privateKey, ec.encode(data)); 52 53 assert(await subtle.verify({ 54 name: 'RSA-PSS', 55 saltLength: 256, 56 }, publicKey, signature, ec.encode(data))); 57 } 58 59 test('hello world').then(common.mustCall()); 60} 61 62// Test Sign/Verify ECDSA 63{ 64 async function test(data) { 65 const ec = new TextEncoder(); 66 const { publicKey, privateKey } = await subtle.generateKey({ 67 name: 'ECDSA', 68 namedCurve: 'P-384', 69 }, true, ['sign', 'verify']); 70 71 const signature = await subtle.sign({ 72 name: 'ECDSA', 73 hash: 'SHA-384', 74 }, privateKey, ec.encode(data)); 75 76 assert(await subtle.verify({ 77 name: 'ECDSA', 78 hash: 'SHA-384', 79 }, publicKey, signature, ec.encode(data))); 80 } 81 82 test('hello world').then(common.mustCall()); 83} 84 85// Test Sign/Verify HMAC 86{ 87 async function test(data) { 88 const ec = new TextEncoder(); 89 90 const key = await subtle.generateKey({ 91 name: 'HMAC', 92 length: 256, 93 hash: 'SHA-256' 94 }, true, ['sign', 'verify']); 95 96 const signature = await subtle.sign({ 97 name: 'HMAC', 98 }, key, ec.encode(data)); 99 100 assert(await subtle.verify({ 101 name: 'HMAC', 102 }, key, signature, ec.encode(data))); 103 } 104 105 test('hello world').then(common.mustCall()); 106} 107 108// Test Sign/Verify Ed25519 109{ 110 async function test(data) { 111 const ec = new TextEncoder(); 112 const { publicKey, privateKey } = await subtle.generateKey({ 113 name: 'Ed25519', 114 }, true, ['sign', 'verify']); 115 116 const signature = await subtle.sign({ 117 name: 'Ed25519', 118 }, privateKey, ec.encode(data)); 119 120 assert(await subtle.verify({ 121 name: 'Ed25519', 122 }, publicKey, signature, ec.encode(data))); 123 } 124 125 test('hello world').then(common.mustCall()); 126} 127 128// Test Sign/Verify Ed448 129{ 130 async function test(data) { 131 const ec = new TextEncoder(); 132 const { publicKey, privateKey } = await subtle.generateKey({ 133 name: 'Ed448', 134 }, true, ['sign', 'verify']); 135 136 const signature = await subtle.sign({ 137 name: 'Ed448', 138 }, privateKey, ec.encode(data)); 139 140 assert(await subtle.verify({ 141 name: 'Ed448', 142 }, publicKey, signature, ec.encode(data))); 143 } 144 145 test('hello world').then(common.mustCall()); 146} 147