11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ci// Flags: --expose-internals
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci// This is the same as test/simple/test-crypto, but from before the shift
261cb0ef41Sopenharmony_ci// to use buffers by default.
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciconst common = require('../common');
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciif (!common.hasCrypto)
321cb0ef41Sopenharmony_ci  common.skip('missing crypto');
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciconst assert = require('assert');
351cb0ef41Sopenharmony_ciconst crypto = require('crypto');
361cb0ef41Sopenharmony_ciconst fs = require('fs');
371cb0ef41Sopenharmony_ciconst tls = require('tls');
381cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cirequire('internal/crypto/util').setDefaultEncoding('latin1');
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci// Test Certificates
431cb0ef41Sopenharmony_ciconst certPem = fixtures.readKey('rsa_cert.crt');
441cb0ef41Sopenharmony_ciconst certPfx = fixtures.readKey('rsa_cert.pfx');
451cb0ef41Sopenharmony_ciconst keyPem = fixtures.readKey('rsa_private.pem');
461cb0ef41Sopenharmony_ciconst rsaPubPem = fixtures.readKey('rsa_public.pem', 'ascii');
471cb0ef41Sopenharmony_ciconst rsaKeyPem = fixtures.readKey('rsa_private.pem', 'ascii');
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci// PFX tests
501cb0ef41Sopenharmony_citls.createSecureContext({ pfx: certPfx, passphrase: 'sample' });
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciassert.throws(function() {
531cb0ef41Sopenharmony_ci  tls.createSecureContext({ pfx: certPfx });
541cb0ef41Sopenharmony_ci}, /^Error: mac verify failure$/);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ciassert.throws(function() {
571cb0ef41Sopenharmony_ci  tls.createSecureContext({ pfx: certPfx, passphrase: 'test' });
581cb0ef41Sopenharmony_ci}, /^Error: mac verify failure$/);
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ciassert.throws(function() {
611cb0ef41Sopenharmony_ci  tls.createSecureContext({ pfx: 'sample', passphrase: 'test' });
621cb0ef41Sopenharmony_ci}, /^Error: not enough data$/);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci// Test HMAC
651cb0ef41Sopenharmony_ci{
661cb0ef41Sopenharmony_ci  const hmacHash = crypto.createHmac('sha1', 'Node')
671cb0ef41Sopenharmony_ci                         .update('some data')
681cb0ef41Sopenharmony_ci                         .update('to hmac')
691cb0ef41Sopenharmony_ci                         .digest('hex');
701cb0ef41Sopenharmony_ci  assert.strictEqual(hmacHash, '19fd6e1ba73d9ed2224dd5094a71babe85d9a892');
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci// Test HMAC-SHA-* (rfc 4231 Test Cases)
741cb0ef41Sopenharmony_ci{
751cb0ef41Sopenharmony_ci  const rfc4231 = [
761cb0ef41Sopenharmony_ci    {
771cb0ef41Sopenharmony_ci      key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'),
781cb0ef41Sopenharmony_ci      data: Buffer.from('4869205468657265', 'hex'), // 'Hi There'
791cb0ef41Sopenharmony_ci      hmac: {
801cb0ef41Sopenharmony_ci        sha224: '896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22',
811cb0ef41Sopenharmony_ci        sha256:
821cb0ef41Sopenharmony_ci            'b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c' +
831cb0ef41Sopenharmony_ci            '2e32cff7',
841cb0ef41Sopenharmony_ci        sha384:
851cb0ef41Sopenharmony_ci            'afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c' +
861cb0ef41Sopenharmony_ci            '7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6',
871cb0ef41Sopenharmony_ci        sha512:
881cb0ef41Sopenharmony_ci            '87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b305' +
891cb0ef41Sopenharmony_ci            '45e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f170' +
901cb0ef41Sopenharmony_ci            '2e696c203a126854'
911cb0ef41Sopenharmony_ci      }
921cb0ef41Sopenharmony_ci    },
931cb0ef41Sopenharmony_ci    {
941cb0ef41Sopenharmony_ci      key: Buffer.from('4a656665', 'hex'), // 'Jefe'
951cb0ef41Sopenharmony_ci      data: Buffer.from('7768617420646f2079612077616e7420666f72206e6f74686' +
961cb0ef41Sopenharmony_ci                        '96e673f', 'hex'), // 'what do ya want for nothing?'
971cb0ef41Sopenharmony_ci      hmac: {
981cb0ef41Sopenharmony_ci        sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44',
991cb0ef41Sopenharmony_ci        sha256:
1001cb0ef41Sopenharmony_ci            '5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b9' +
1011cb0ef41Sopenharmony_ci            '64ec3843',
1021cb0ef41Sopenharmony_ci        sha384:
1031cb0ef41Sopenharmony_ci            'af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec373' +
1041cb0ef41Sopenharmony_ci            '6322445e8e2240ca5e69e2c78b3239ecfab21649',
1051cb0ef41Sopenharmony_ci        sha512:
1061cb0ef41Sopenharmony_ci            '164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7' +
1071cb0ef41Sopenharmony_ci            'ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b' +
1081cb0ef41Sopenharmony_ci            '636e070a38bce737'
1091cb0ef41Sopenharmony_ci      }
1101cb0ef41Sopenharmony_ci    },
1111cb0ef41Sopenharmony_ci    {
1121cb0ef41Sopenharmony_ci      key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'hex'),
1131cb0ef41Sopenharmony_ci      data: Buffer.from('ddddddddddddddddddddddddddddddddddddddddddddddddd' +
1141cb0ef41Sopenharmony_ci                        'ddddddddddddddddddddddddddddddddddddddddddddddddddd',
1151cb0ef41Sopenharmony_ci                        'hex'),
1161cb0ef41Sopenharmony_ci      hmac: {
1171cb0ef41Sopenharmony_ci        sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea',
1181cb0ef41Sopenharmony_ci        sha256:
1191cb0ef41Sopenharmony_ci            '773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514' +
1201cb0ef41Sopenharmony_ci            'ced565fe',
1211cb0ef41Sopenharmony_ci        sha384:
1221cb0ef41Sopenharmony_ci            '88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e5' +
1231cb0ef41Sopenharmony_ci            '5966144b2a5ab39dc13814b94e3ab6e101a34f27',
1241cb0ef41Sopenharmony_ci        sha512:
1251cb0ef41Sopenharmony_ci            'fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33' +
1261cb0ef41Sopenharmony_ci            'b2279d39bf3e848279a722c806b485a47e67c807b946a337bee89426' +
1271cb0ef41Sopenharmony_ci            '74278859e13292fb'
1281cb0ef41Sopenharmony_ci      }
1291cb0ef41Sopenharmony_ci    },
1301cb0ef41Sopenharmony_ci    {
1311cb0ef41Sopenharmony_ci      key: Buffer.from('0102030405060708090a0b0c0d0e0f10111213141516171819',
1321cb0ef41Sopenharmony_ci                       'hex'),
1331cb0ef41Sopenharmony_ci      data: Buffer.from('cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdc' +
1341cb0ef41Sopenharmony_ci                        'dcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd',
1351cb0ef41Sopenharmony_ci                        'hex'),
1361cb0ef41Sopenharmony_ci      hmac: {
1371cb0ef41Sopenharmony_ci        sha224: '6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a',
1381cb0ef41Sopenharmony_ci        sha256:
1391cb0ef41Sopenharmony_ci            '82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff4' +
1401cb0ef41Sopenharmony_ci            '6729665b',
1411cb0ef41Sopenharmony_ci        sha384:
1421cb0ef41Sopenharmony_ci            '3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e' +
1431cb0ef41Sopenharmony_ci            '1f573b4e6801dd23c4a7d679ccf8a386c674cffb',
1441cb0ef41Sopenharmony_ci        sha512:
1451cb0ef41Sopenharmony_ci            'b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050' +
1461cb0ef41Sopenharmony_ci            '361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2d' +
1471cb0ef41Sopenharmony_ci            'e2adebeb10a298dd'
1481cb0ef41Sopenharmony_ci      }
1491cb0ef41Sopenharmony_ci    },
1501cb0ef41Sopenharmony_ci    {
1511cb0ef41Sopenharmony_ci      key: Buffer.from('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c', 'hex'),
1521cb0ef41Sopenharmony_ci      // 'Test With Truncation'
1531cb0ef41Sopenharmony_ci      data: Buffer.from('546573742057697468205472756e636174696f6e', 'hex'),
1541cb0ef41Sopenharmony_ci      hmac: {
1551cb0ef41Sopenharmony_ci        sha224: '0e2aea68a90c8d37c988bcdb9fca6fa8',
1561cb0ef41Sopenharmony_ci        sha256: 'a3b6167473100ee06e0c796c2955552b',
1571cb0ef41Sopenharmony_ci        sha384: '3abf34c3503b2a23a46efc619baef897',
1581cb0ef41Sopenharmony_ci        sha512: '415fad6271580a531d4179bc891d87a6'
1591cb0ef41Sopenharmony_ci      },
1601cb0ef41Sopenharmony_ci      truncate: true
1611cb0ef41Sopenharmony_ci    },
1621cb0ef41Sopenharmony_ci    {
1631cb0ef41Sopenharmony_ci      key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
1641cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
1651cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
1661cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
1671cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
1681cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaa', 'hex'),
1691cb0ef41Sopenharmony_ci      // 'Test Using Larger Than Block-Size Key - Hash Key First'
1701cb0ef41Sopenharmony_ci      data: Buffer.from('54657374205573696e67204c6172676572205468616e20426' +
1711cb0ef41Sopenharmony_ci                        'c6f636b2d53697a65204b6579202d2048617368204b657920' +
1721cb0ef41Sopenharmony_ci                        '4669727374', 'hex'),
1731cb0ef41Sopenharmony_ci      hmac: {
1741cb0ef41Sopenharmony_ci        sha224: '95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e',
1751cb0ef41Sopenharmony_ci        sha256:
1761cb0ef41Sopenharmony_ci            '60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f' +
1771cb0ef41Sopenharmony_ci            '0ee37f54',
1781cb0ef41Sopenharmony_ci        sha384:
1791cb0ef41Sopenharmony_ci            '4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05' +
1801cb0ef41Sopenharmony_ci            '033ac4c60c2ef6ab4030fe8296248df163f44952',
1811cb0ef41Sopenharmony_ci        sha512:
1821cb0ef41Sopenharmony_ci            '80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b0137' +
1831cb0ef41Sopenharmony_ci            '83f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec' +
1841cb0ef41Sopenharmony_ci            '8b915a985d786598'
1851cb0ef41Sopenharmony_ci      }
1861cb0ef41Sopenharmony_ci    },
1871cb0ef41Sopenharmony_ci    {
1881cb0ef41Sopenharmony_ci      key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
1891cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
1901cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
1911cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
1921cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
1931cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaa', 'hex'),
1941cb0ef41Sopenharmony_ci      // 'This is a test using a larger than block-size key and a larger ' +
1951cb0ef41Sopenharmony_ci      // 'than block-size data. The key needs to be hashed before being ' +
1961cb0ef41Sopenharmony_ci      // 'used by the HMAC algorithm.'
1971cb0ef41Sopenharmony_ci      data: Buffer.from('5468697320697320612074657374207573696e672061206c6' +
1981cb0ef41Sopenharmony_ci                        '172676572207468616e20626c6f636b2d73697a65206b6579' +
1991cb0ef41Sopenharmony_ci                        '20616e642061206c6172676572207468616e20626c6f636b2' +
2001cb0ef41Sopenharmony_ci                        'd73697a6520646174612e20546865206b6579206e65656473' +
2011cb0ef41Sopenharmony_ci                        '20746f20626520686173686564206265666f7265206265696' +
2021cb0ef41Sopenharmony_ci                        'e6720757365642062792074686520484d414320616c676f72' +
2031cb0ef41Sopenharmony_ci                        '6974686d2e', 'hex'),
2041cb0ef41Sopenharmony_ci      hmac: {
2051cb0ef41Sopenharmony_ci        sha224: '3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1',
2061cb0ef41Sopenharmony_ci        sha256:
2071cb0ef41Sopenharmony_ci            '9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f5153' +
2081cb0ef41Sopenharmony_ci            '5c3a35e2',
2091cb0ef41Sopenharmony_ci        sha384:
2101cb0ef41Sopenharmony_ci            '6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82' +
2111cb0ef41Sopenharmony_ci            '461e99c5a678cc31e799176d3860e6110c46523e',
2121cb0ef41Sopenharmony_ci        sha512:
2131cb0ef41Sopenharmony_ci            'e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d' +
2141cb0ef41Sopenharmony_ci            '20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de04460' +
2151cb0ef41Sopenharmony_ci            '65c97440fa8c6a58'
2161cb0ef41Sopenharmony_ci      }
2171cb0ef41Sopenharmony_ci    },
2181cb0ef41Sopenharmony_ci  ];
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci  for (const testCase of rfc4231) {
2211cb0ef41Sopenharmony_ci    for (const hash in testCase.hmac) {
2221cb0ef41Sopenharmony_ci      let result = crypto.createHmac(hash, testCase.key)
2231cb0ef41Sopenharmony_ci                       .update(testCase.data)
2241cb0ef41Sopenharmony_ci                       .digest('hex');
2251cb0ef41Sopenharmony_ci      if (testCase.truncate) {
2261cb0ef41Sopenharmony_ci        result = result.substr(0, 32); // first 128 bits == 32 hex chars
2271cb0ef41Sopenharmony_ci      }
2281cb0ef41Sopenharmony_ci      assert.strictEqual(
2291cb0ef41Sopenharmony_ci        testCase.hmac[hash],
2301cb0ef41Sopenharmony_ci        result
2311cb0ef41Sopenharmony_ci      );
2321cb0ef41Sopenharmony_ci    }
2331cb0ef41Sopenharmony_ci  }
2341cb0ef41Sopenharmony_ci}
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ci// Test HMAC-MD5/SHA1 (rfc 2202 Test Cases)
2371cb0ef41Sopenharmony_ci{
2381cb0ef41Sopenharmony_ci  const rfc2202_md5 = [
2391cb0ef41Sopenharmony_ci    {
2401cb0ef41Sopenharmony_ci      key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'),
2411cb0ef41Sopenharmony_ci      data: 'Hi There',
2421cb0ef41Sopenharmony_ci      hmac: '9294727a3638bb1c13f48ef8158bfc9d'
2431cb0ef41Sopenharmony_ci    },
2441cb0ef41Sopenharmony_ci    {
2451cb0ef41Sopenharmony_ci      key: 'Jefe',
2461cb0ef41Sopenharmony_ci      data: 'what do ya want for nothing?',
2471cb0ef41Sopenharmony_ci      hmac: '750c783e6ab0b503eaa86e310a5db738'
2481cb0ef41Sopenharmony_ci    },
2491cb0ef41Sopenharmony_ci    {
2501cb0ef41Sopenharmony_ci      key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'hex'),
2511cb0ef41Sopenharmony_ci      data: Buffer.from('ddddddddddddddddddddddddddddddddddddddddddddddddd' +
2521cb0ef41Sopenharmony_ci                        'ddddddddddddddddddddddddddddddddddddddddddddddddddd',
2531cb0ef41Sopenharmony_ci                        'hex'),
2541cb0ef41Sopenharmony_ci      hmac: '56be34521d144c88dbb8c733f0e8b3f6'
2551cb0ef41Sopenharmony_ci    },
2561cb0ef41Sopenharmony_ci    {
2571cb0ef41Sopenharmony_ci      key: Buffer.from('0102030405060708090a0b0c0d0e0f10111213141516171819',
2581cb0ef41Sopenharmony_ci                       'hex'),
2591cb0ef41Sopenharmony_ci      data: Buffer.from('cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdc' +
2601cb0ef41Sopenharmony_ci                        'dcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd' +
2611cb0ef41Sopenharmony_ci                        'cdcdcdcdcd',
2621cb0ef41Sopenharmony_ci                        'hex'),
2631cb0ef41Sopenharmony_ci      hmac: '697eaf0aca3a3aea3a75164746ffaa79'
2641cb0ef41Sopenharmony_ci    },
2651cb0ef41Sopenharmony_ci    {
2661cb0ef41Sopenharmony_ci      key: Buffer.from('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c', 'hex'),
2671cb0ef41Sopenharmony_ci      data: 'Test With Truncation',
2681cb0ef41Sopenharmony_ci      hmac: '56461ef2342edc00f9bab995690efd4c'
2691cb0ef41Sopenharmony_ci    },
2701cb0ef41Sopenharmony_ci    {
2711cb0ef41Sopenharmony_ci      key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
2721cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
2731cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
2741cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaa',
2751cb0ef41Sopenharmony_ci                       'hex'),
2761cb0ef41Sopenharmony_ci      data: 'Test Using Larger Than Block-Size Key - Hash Key First',
2771cb0ef41Sopenharmony_ci      hmac: '6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd'
2781cb0ef41Sopenharmony_ci    },
2791cb0ef41Sopenharmony_ci    {
2801cb0ef41Sopenharmony_ci      key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
2811cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
2821cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
2831cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaa',
2841cb0ef41Sopenharmony_ci                       'hex'),
2851cb0ef41Sopenharmony_ci      data:
2861cb0ef41Sopenharmony_ci          'Test Using Larger Than Block-Size Key and Larger Than One ' +
2871cb0ef41Sopenharmony_ci          'Block-Size Data',
2881cb0ef41Sopenharmony_ci      hmac: '6f630fad67cda0ee1fb1f562db3aa53e'
2891cb0ef41Sopenharmony_ci    },
2901cb0ef41Sopenharmony_ci  ];
2911cb0ef41Sopenharmony_ci  const rfc2202_sha1 = [
2921cb0ef41Sopenharmony_ci    {
2931cb0ef41Sopenharmony_ci      key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'),
2941cb0ef41Sopenharmony_ci      data: 'Hi There',
2951cb0ef41Sopenharmony_ci      hmac: 'b617318655057264e28bc0b6fb378c8ef146be00'
2961cb0ef41Sopenharmony_ci    },
2971cb0ef41Sopenharmony_ci    {
2981cb0ef41Sopenharmony_ci      key: 'Jefe',
2991cb0ef41Sopenharmony_ci      data: 'what do ya want for nothing?',
3001cb0ef41Sopenharmony_ci      hmac: 'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79'
3011cb0ef41Sopenharmony_ci    },
3021cb0ef41Sopenharmony_ci    {
3031cb0ef41Sopenharmony_ci      key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'hex'),
3041cb0ef41Sopenharmony_ci      data: Buffer.from('ddddddddddddddddddddddddddddddddddddddddddddd' +
3051cb0ef41Sopenharmony_ci                        'ddddddddddddddddddddddddddddddddddddddddddddd' +
3061cb0ef41Sopenharmony_ci                        'dddddddddd',
3071cb0ef41Sopenharmony_ci                        'hex'),
3081cb0ef41Sopenharmony_ci      hmac: '125d7342b9ac11cd91a39af48aa17b4f63f175d3'
3091cb0ef41Sopenharmony_ci    },
3101cb0ef41Sopenharmony_ci    {
3111cb0ef41Sopenharmony_ci      key: Buffer.from('0102030405060708090a0b0c0d0e0f10111213141516171819',
3121cb0ef41Sopenharmony_ci                       'hex'),
3131cb0ef41Sopenharmony_ci      data: Buffer.from('cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdc' +
3141cb0ef41Sopenharmony_ci                        'dcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd' +
3151cb0ef41Sopenharmony_ci                        'cdcdcdcdcd',
3161cb0ef41Sopenharmony_ci                        'hex'),
3171cb0ef41Sopenharmony_ci      hmac: '4c9007f4026250c6bc8414f9bf50c86c2d7235da'
3181cb0ef41Sopenharmony_ci    },
3191cb0ef41Sopenharmony_ci    {
3201cb0ef41Sopenharmony_ci      key: Buffer.from('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c', 'hex'),
3211cb0ef41Sopenharmony_ci      data: 'Test With Truncation',
3221cb0ef41Sopenharmony_ci      hmac: '4c1a03424b55e07fe7f27be1d58bb9324a9a5a04'
3231cb0ef41Sopenharmony_ci    },
3241cb0ef41Sopenharmony_ci    {
3251cb0ef41Sopenharmony_ci      key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
3261cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
3271cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
3281cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaa',
3291cb0ef41Sopenharmony_ci                       'hex'),
3301cb0ef41Sopenharmony_ci      data: 'Test Using Larger Than Block-Size Key - Hash Key First',
3311cb0ef41Sopenharmony_ci      hmac: 'aa4ae5e15272d00e95705637ce8a3b55ed402112'
3321cb0ef41Sopenharmony_ci    },
3331cb0ef41Sopenharmony_ci    {
3341cb0ef41Sopenharmony_ci      key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
3351cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
3361cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
3371cb0ef41Sopenharmony_ci                       'aaaaaaaaaaaaaaaaaaaaaa',
3381cb0ef41Sopenharmony_ci                       'hex'),
3391cb0ef41Sopenharmony_ci      data:
3401cb0ef41Sopenharmony_ci          'Test Using Larger Than Block-Size Key and Larger Than One ' +
3411cb0ef41Sopenharmony_ci          'Block-Size Data',
3421cb0ef41Sopenharmony_ci      hmac: 'e8e99d0f45237d786d6bbaa7965c7808bbff1a91'
3431cb0ef41Sopenharmony_ci    },
3441cb0ef41Sopenharmony_ci  ];
3451cb0ef41Sopenharmony_ci
3461cb0ef41Sopenharmony_ci  if (!common.hasFipsCrypto) {
3471cb0ef41Sopenharmony_ci    for (const testCase of rfc2202_md5) {
3481cb0ef41Sopenharmony_ci      assert.strictEqual(
3491cb0ef41Sopenharmony_ci        testCase.hmac,
3501cb0ef41Sopenharmony_ci        crypto.createHmac('md5', testCase.key)
3511cb0ef41Sopenharmony_ci          .update(testCase.data)
3521cb0ef41Sopenharmony_ci          .digest('hex')
3531cb0ef41Sopenharmony_ci      );
3541cb0ef41Sopenharmony_ci    }
3551cb0ef41Sopenharmony_ci  }
3561cb0ef41Sopenharmony_ci  for (const testCase of rfc2202_sha1) {
3571cb0ef41Sopenharmony_ci    assert.strictEqual(
3581cb0ef41Sopenharmony_ci      testCase.hmac,
3591cb0ef41Sopenharmony_ci      crypto.createHmac('sha1', testCase.key)
3601cb0ef41Sopenharmony_ci        .update(testCase.data)
3611cb0ef41Sopenharmony_ci        .digest('hex')
3621cb0ef41Sopenharmony_ci    );
3631cb0ef41Sopenharmony_ci  }
3641cb0ef41Sopenharmony_ci}
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ci// Test hashing
3671cb0ef41Sopenharmony_ci{
3681cb0ef41Sopenharmony_ci  const a1 = crypto.createHash('sha1').update('Test123').digest('hex');
3691cb0ef41Sopenharmony_ci  const a2 = crypto.createHash('sha256').update('Test123').digest('base64');
3701cb0ef41Sopenharmony_ci  const a3 = crypto.createHash('sha512').update('Test123').digest(); // binary
3711cb0ef41Sopenharmony_ci  const a4 = crypto.createHash('sha1').update('Test123').digest('buffer');
3721cb0ef41Sopenharmony_ci
3731cb0ef41Sopenharmony_ci  if (!common.hasFipsCrypto) {
3741cb0ef41Sopenharmony_ci    const a0 = crypto.createHash('md5').update('Test123').digest('latin1');
3751cb0ef41Sopenharmony_ci    assert.strictEqual(
3761cb0ef41Sopenharmony_ci      a0,
3771cb0ef41Sopenharmony_ci      'h\u00ea\u00cb\u0097\u00d8o\fF!\u00fa+\u000e\u0017\u00ca\u00bd\u008c'
3781cb0ef41Sopenharmony_ci    );
3791cb0ef41Sopenharmony_ci  }
3801cb0ef41Sopenharmony_ci
3811cb0ef41Sopenharmony_ci  assert.strictEqual(a1, '8308651804facb7b9af8ffc53a33a22d6a1c8ac2');
3821cb0ef41Sopenharmony_ci
3831cb0ef41Sopenharmony_ci  assert.strictEqual(a2, '2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4=');
3841cb0ef41Sopenharmony_ci
3851cb0ef41Sopenharmony_ci  // Test SHA512 as assumed latin1
3861cb0ef41Sopenharmony_ci  assert.strictEqual(
3871cb0ef41Sopenharmony_ci    a3,
3881cb0ef41Sopenharmony_ci    '\u00c1(4\u00f1\u0003\u001fd\u0097!O\'\u00d4C/&Qz\u00d4' +
3891cb0ef41Sopenharmony_ci    '\u0094\u0015l\u00b8\u008dQ+\u00db\u001d\u00c4\u00b5}\u00b2' +
3901cb0ef41Sopenharmony_ci    '\u00d6\u0092\u00a3\u00df\u00a2i\u00a1\u009b\n\n*\u000f' +
3911cb0ef41Sopenharmony_ci    '\u00d7\u00d6\u00a2\u00a8\u0085\u00e3<\u0083\u009c\u0093' +
3921cb0ef41Sopenharmony_ci    '\u00c2\u0006\u00da0\u00a1\u00879(G\u00ed\''
3931cb0ef41Sopenharmony_ci  );
3941cb0ef41Sopenharmony_ci
3951cb0ef41Sopenharmony_ci  assert.deepStrictEqual(
3961cb0ef41Sopenharmony_ci    a4,
3971cb0ef41Sopenharmony_ci    Buffer.from('8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'hex')
3981cb0ef41Sopenharmony_ci  );
3991cb0ef41Sopenharmony_ci}
4001cb0ef41Sopenharmony_ci
4011cb0ef41Sopenharmony_ci// Test multiple updates to same hash
4021cb0ef41Sopenharmony_ci{
4031cb0ef41Sopenharmony_ci  const h1 = crypto.createHash('sha1').update('Test123').digest('hex');
4041cb0ef41Sopenharmony_ci  const h2 = crypto.createHash('sha1').update('Test').update('123')
4051cb0ef41Sopenharmony_ci    .digest('hex');
4061cb0ef41Sopenharmony_ci  assert.strictEqual(h1, h2);
4071cb0ef41Sopenharmony_ci}
4081cb0ef41Sopenharmony_ci
4091cb0ef41Sopenharmony_ci// Test hashing for binary files
4101cb0ef41Sopenharmony_ci{
4111cb0ef41Sopenharmony_ci  const fn = fixtures.path('sample.png');
4121cb0ef41Sopenharmony_ci  const sha1Hash = crypto.createHash('sha1');
4131cb0ef41Sopenharmony_ci  const fileStream = fs.createReadStream(fn);
4141cb0ef41Sopenharmony_ci  fileStream.on('data', function(data) {
4151cb0ef41Sopenharmony_ci    sha1Hash.update(data);
4161cb0ef41Sopenharmony_ci  });
4171cb0ef41Sopenharmony_ci  fileStream.on('close', common.mustCall(function() {
4181cb0ef41Sopenharmony_ci    assert.strictEqual(
4191cb0ef41Sopenharmony_ci      sha1Hash.digest('hex'),
4201cb0ef41Sopenharmony_ci      '22723e553129a336ad96e10f6aecdf0f45e4149e'
4211cb0ef41Sopenharmony_ci    );
4221cb0ef41Sopenharmony_ci  }));
4231cb0ef41Sopenharmony_ci}
4241cb0ef41Sopenharmony_ci
4251cb0ef41Sopenharmony_ci// Unknown digest method should throw an error:
4261cb0ef41Sopenharmony_ci// https://github.com/nodejs/node-v0.x-archive/issues/2227
4271cb0ef41Sopenharmony_ciassert.throws(function() {
4281cb0ef41Sopenharmony_ci  crypto.createHash('xyzzy');
4291cb0ef41Sopenharmony_ci}, /^Error: Digest method not supported$/);
4301cb0ef41Sopenharmony_ci
4311cb0ef41Sopenharmony_ci// Test signing and verifying
4321cb0ef41Sopenharmony_ci{
4331cb0ef41Sopenharmony_ci  const s1 = crypto.createSign('SHA1')
4341cb0ef41Sopenharmony_ci                 .update('Test123')
4351cb0ef41Sopenharmony_ci                 .sign(keyPem, 'base64');
4361cb0ef41Sopenharmony_ci  const s1Verified = crypto.createVerify('SHA1')
4371cb0ef41Sopenharmony_ci                         .update('Test')
4381cb0ef41Sopenharmony_ci                         .update('123')
4391cb0ef41Sopenharmony_ci                         .verify(certPem, s1, 'base64');
4401cb0ef41Sopenharmony_ci  assert.strictEqual(s1Verified, true);
4411cb0ef41Sopenharmony_ci
4421cb0ef41Sopenharmony_ci  const s2 = crypto.createSign('SHA256')
4431cb0ef41Sopenharmony_ci                 .update('Test123')
4441cb0ef41Sopenharmony_ci                 .sign(keyPem); // binary
4451cb0ef41Sopenharmony_ci  const s2Verified = crypto.createVerify('SHA256')
4461cb0ef41Sopenharmony_ci                         .update('Test')
4471cb0ef41Sopenharmony_ci                         .update('123')
4481cb0ef41Sopenharmony_ci                         .verify(certPem, s2); // binary
4491cb0ef41Sopenharmony_ci  assert.strictEqual(s2Verified, true);
4501cb0ef41Sopenharmony_ci
4511cb0ef41Sopenharmony_ci  const s3 = crypto.createSign('SHA1')
4521cb0ef41Sopenharmony_ci                 .update('Test123')
4531cb0ef41Sopenharmony_ci                 .sign(keyPem, 'buffer');
4541cb0ef41Sopenharmony_ci  const s3Verified = crypto.createVerify('SHA1')
4551cb0ef41Sopenharmony_ci                         .update('Test')
4561cb0ef41Sopenharmony_ci                         .update('123')
4571cb0ef41Sopenharmony_ci                         .verify(certPem, s3);
4581cb0ef41Sopenharmony_ci  assert.strictEqual(s3Verified, true);
4591cb0ef41Sopenharmony_ci}
4601cb0ef41Sopenharmony_ci
4611cb0ef41Sopenharmony_ci
4621cb0ef41Sopenharmony_cifunction testCipher1(key) {
4631cb0ef41Sopenharmony_ci  // Test encryption and decryption
4641cb0ef41Sopenharmony_ci  const plaintext = 'Keep this a secret? No! Tell everyone about node.js!';
4651cb0ef41Sopenharmony_ci  const cipher = crypto.createCipher('aes192', key);
4661cb0ef41Sopenharmony_ci
4671cb0ef41Sopenharmony_ci  // Encrypt plaintext which is in utf8 format
4681cb0ef41Sopenharmony_ci  // to a ciphertext which will be in hex
4691cb0ef41Sopenharmony_ci  let ciph = cipher.update(plaintext, 'utf8', 'hex');
4701cb0ef41Sopenharmony_ci  // Only use binary or hex, not base64.
4711cb0ef41Sopenharmony_ci  ciph += cipher.final('hex');
4721cb0ef41Sopenharmony_ci
4731cb0ef41Sopenharmony_ci  const decipher = crypto.createDecipher('aes192', key);
4741cb0ef41Sopenharmony_ci  let txt = decipher.update(ciph, 'hex', 'utf8');
4751cb0ef41Sopenharmony_ci  txt += decipher.final('utf8');
4761cb0ef41Sopenharmony_ci
4771cb0ef41Sopenharmony_ci  assert.strictEqual(txt, plaintext);
4781cb0ef41Sopenharmony_ci}
4791cb0ef41Sopenharmony_ci
4801cb0ef41Sopenharmony_ci
4811cb0ef41Sopenharmony_cifunction testCipher2(key) {
4821cb0ef41Sopenharmony_ci  // Encryption and decryption with Base64.
4831cb0ef41Sopenharmony_ci  // Reported in https://github.com/joyent/node/issues/738
4841cb0ef41Sopenharmony_ci  const plaintext =
4851cb0ef41Sopenharmony_ci      '32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' +
4861cb0ef41Sopenharmony_ci      'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' +
4871cb0ef41Sopenharmony_ci      'jAfaFg**';
4881cb0ef41Sopenharmony_ci  const cipher = crypto.createCipher('aes256', key);
4891cb0ef41Sopenharmony_ci
4901cb0ef41Sopenharmony_ci  // Encrypt plaintext which is in utf8 format
4911cb0ef41Sopenharmony_ci  // to a ciphertext which will be in Base64
4921cb0ef41Sopenharmony_ci  let ciph = cipher.update(plaintext, 'utf8', 'base64');
4931cb0ef41Sopenharmony_ci  ciph += cipher.final('base64');
4941cb0ef41Sopenharmony_ci
4951cb0ef41Sopenharmony_ci  const decipher = crypto.createDecipher('aes256', key);
4961cb0ef41Sopenharmony_ci  let txt = decipher.update(ciph, 'base64', 'utf8');
4971cb0ef41Sopenharmony_ci  txt += decipher.final('utf8');
4981cb0ef41Sopenharmony_ci
4991cb0ef41Sopenharmony_ci  assert.strictEqual(txt, plaintext);
5001cb0ef41Sopenharmony_ci}
5011cb0ef41Sopenharmony_ci
5021cb0ef41Sopenharmony_ci
5031cb0ef41Sopenharmony_cifunction testCipher3(key, iv) {
5041cb0ef41Sopenharmony_ci  // Test encryption and decryption with explicit key and iv
5051cb0ef41Sopenharmony_ci  const plaintext =
5061cb0ef41Sopenharmony_ci      '32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' +
5071cb0ef41Sopenharmony_ci      'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' +
5081cb0ef41Sopenharmony_ci      'jAfaFg**';
5091cb0ef41Sopenharmony_ci  const cipher = crypto.createCipheriv('des-ede3-cbc', key, iv);
5101cb0ef41Sopenharmony_ci  let ciph = cipher.update(plaintext, 'utf8', 'hex');
5111cb0ef41Sopenharmony_ci  ciph += cipher.final('hex');
5121cb0ef41Sopenharmony_ci
5131cb0ef41Sopenharmony_ci  const decipher = crypto.createDecipheriv('des-ede3-cbc', key, iv);
5141cb0ef41Sopenharmony_ci  let txt = decipher.update(ciph, 'hex', 'utf8');
5151cb0ef41Sopenharmony_ci  txt += decipher.final('utf8');
5161cb0ef41Sopenharmony_ci
5171cb0ef41Sopenharmony_ci  assert.strictEqual(txt, plaintext);
5181cb0ef41Sopenharmony_ci}
5191cb0ef41Sopenharmony_ci
5201cb0ef41Sopenharmony_ci
5211cb0ef41Sopenharmony_cifunction testCipher4(key, iv) {
5221cb0ef41Sopenharmony_ci  // Test encryption and decryption with explicit key and iv
5231cb0ef41Sopenharmony_ci  const plaintext =
5241cb0ef41Sopenharmony_ci      '32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' +
5251cb0ef41Sopenharmony_ci      'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' +
5261cb0ef41Sopenharmony_ci      'jAfaFg**';
5271cb0ef41Sopenharmony_ci  const cipher = crypto.createCipheriv('des-ede3-cbc', key, iv);
5281cb0ef41Sopenharmony_ci  let ciph = cipher.update(plaintext, 'utf8', 'buffer');
5291cb0ef41Sopenharmony_ci  ciph = Buffer.concat([ciph, cipher.final('buffer')]);
5301cb0ef41Sopenharmony_ci
5311cb0ef41Sopenharmony_ci  const decipher = crypto.createDecipheriv('des-ede3-cbc', key, iv);
5321cb0ef41Sopenharmony_ci  let txt = decipher.update(ciph, 'buffer', 'utf8');
5331cb0ef41Sopenharmony_ci  txt += decipher.final('utf8');
5341cb0ef41Sopenharmony_ci
5351cb0ef41Sopenharmony_ci  assert.strictEqual(txt, plaintext);
5361cb0ef41Sopenharmony_ci}
5371cb0ef41Sopenharmony_ci
5381cb0ef41Sopenharmony_ci
5391cb0ef41Sopenharmony_cifunction testCipher5(key, iv) {
5401cb0ef41Sopenharmony_ci  // Test encryption and decryption with explicit key with aes128-wrap
5411cb0ef41Sopenharmony_ci  const plaintext =
5421cb0ef41Sopenharmony_ci      '32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' +
5431cb0ef41Sopenharmony_ci      'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' +
5441cb0ef41Sopenharmony_ci      'jAfaFg**';
5451cb0ef41Sopenharmony_ci  const cipher = crypto.createCipher('id-aes128-wrap', key);
5461cb0ef41Sopenharmony_ci  let ciph = cipher.update(plaintext, 'utf8', 'buffer');
5471cb0ef41Sopenharmony_ci  ciph = Buffer.concat([ciph, cipher.final('buffer')]);
5481cb0ef41Sopenharmony_ci
5491cb0ef41Sopenharmony_ci  const decipher = crypto.createDecipher('id-aes128-wrap', key);
5501cb0ef41Sopenharmony_ci  let txt = decipher.update(ciph, 'buffer', 'utf8');
5511cb0ef41Sopenharmony_ci  txt += decipher.final('utf8');
5521cb0ef41Sopenharmony_ci
5531cb0ef41Sopenharmony_ci  assert.strictEqual(txt, plaintext);
5541cb0ef41Sopenharmony_ci}
5551cb0ef41Sopenharmony_ci
5561cb0ef41Sopenharmony_ciif (!common.hasFipsCrypto) {
5571cb0ef41Sopenharmony_ci  testCipher1('MySecretKey123');
5581cb0ef41Sopenharmony_ci  testCipher1(Buffer.from('MySecretKey123'));
5591cb0ef41Sopenharmony_ci
5601cb0ef41Sopenharmony_ci  testCipher2('0123456789abcdef');
5611cb0ef41Sopenharmony_ci  testCipher2(Buffer.from('0123456789abcdef'));
5621cb0ef41Sopenharmony_ci
5631cb0ef41Sopenharmony_ci  testCipher5(Buffer.from('0123456789abcd0123456789'));
5641cb0ef41Sopenharmony_ci}
5651cb0ef41Sopenharmony_ci
5661cb0ef41Sopenharmony_citestCipher3('0123456789abcd0123456789', '12345678');
5671cb0ef41Sopenharmony_citestCipher3('0123456789abcd0123456789', Buffer.from('12345678'));
5681cb0ef41Sopenharmony_citestCipher3(Buffer.from('0123456789abcd0123456789'), '12345678');
5691cb0ef41Sopenharmony_citestCipher3(Buffer.from('0123456789abcd0123456789'), Buffer.from('12345678'));
5701cb0ef41Sopenharmony_ci
5711cb0ef41Sopenharmony_citestCipher4(Buffer.from('0123456789abcd0123456789'), Buffer.from('12345678'));
5721cb0ef41Sopenharmony_ci
5731cb0ef41Sopenharmony_ci
5741cb0ef41Sopenharmony_ci// update() should only take buffers / strings
5751cb0ef41Sopenharmony_ciassert.throws(
5761cb0ef41Sopenharmony_ci  () => crypto.createHash('sha1').update({ foo: 'bar' }),
5771cb0ef41Sopenharmony_ci  {
5781cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
5791cb0ef41Sopenharmony_ci    name: 'TypeError'
5801cb0ef41Sopenharmony_ci  });
5811cb0ef41Sopenharmony_ci
5821cb0ef41Sopenharmony_ci
5831cb0ef41Sopenharmony_ci// Test Diffie-Hellman with two parties sharing a secret,
5841cb0ef41Sopenharmony_ci// using various encodings as we go along
5851cb0ef41Sopenharmony_ci{
5861cb0ef41Sopenharmony_ci  const size = common.hasFipsCrypto || common.hasOpenSSL3 ? 1024 : 256;
5871cb0ef41Sopenharmony_ci  const dh1 = crypto.createDiffieHellman(size);
5881cb0ef41Sopenharmony_ci  const p1 = dh1.getPrime('buffer');
5891cb0ef41Sopenharmony_ci  const dh2 = crypto.createDiffieHellman(p1, 'base64');
5901cb0ef41Sopenharmony_ci  const key1 = dh1.generateKeys();
5911cb0ef41Sopenharmony_ci  const key2 = dh2.generateKeys('hex');
5921cb0ef41Sopenharmony_ci  const secret1 = dh1.computeSecret(key2, 'hex', 'base64');
5931cb0ef41Sopenharmony_ci  const secret2 = dh2.computeSecret(key1, 'latin1', 'buffer');
5941cb0ef41Sopenharmony_ci
5951cb0ef41Sopenharmony_ci  assert.strictEqual(secret1, secret2.toString('base64'));
5961cb0ef41Sopenharmony_ci
5971cb0ef41Sopenharmony_ci  // Create "another dh1" using generated keys from dh1,
5981cb0ef41Sopenharmony_ci  // and compute secret again
5991cb0ef41Sopenharmony_ci  const dh3 = crypto.createDiffieHellman(p1, 'buffer');
6001cb0ef41Sopenharmony_ci  const privkey1 = dh1.getPrivateKey();
6011cb0ef41Sopenharmony_ci  dh3.setPublicKey(key1);
6021cb0ef41Sopenharmony_ci  dh3.setPrivateKey(privkey1);
6031cb0ef41Sopenharmony_ci
6041cb0ef41Sopenharmony_ci  assert.strictEqual(dh1.getPrime(), dh3.getPrime());
6051cb0ef41Sopenharmony_ci  assert.strictEqual(dh1.getGenerator(), dh3.getGenerator());
6061cb0ef41Sopenharmony_ci  assert.strictEqual(dh1.getPublicKey(), dh3.getPublicKey());
6071cb0ef41Sopenharmony_ci  assert.strictEqual(dh1.getPrivateKey(), dh3.getPrivateKey());
6081cb0ef41Sopenharmony_ci
6091cb0ef41Sopenharmony_ci  const secret3 = dh3.computeSecret(key2, 'hex', 'base64');
6101cb0ef41Sopenharmony_ci
6111cb0ef41Sopenharmony_ci  assert.strictEqual(secret1, secret3);
6121cb0ef41Sopenharmony_ci
6131cb0ef41Sopenharmony_ci  // https://github.com/joyent/node/issues/2338
6141cb0ef41Sopenharmony_ci  const p = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' +
6151cb0ef41Sopenharmony_ci            '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' +
6161cb0ef41Sopenharmony_ci            '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' +
6171cb0ef41Sopenharmony_ci            'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF';
6181cb0ef41Sopenharmony_ci  crypto.createDiffieHellman(p, 'hex');
6191cb0ef41Sopenharmony_ci
6201cb0ef41Sopenharmony_ci  // Test RSA key signing/verification
6211cb0ef41Sopenharmony_ci  const rsaSign = crypto.createSign('SHA1');
6221cb0ef41Sopenharmony_ci  const rsaVerify = crypto.createVerify('SHA1');
6231cb0ef41Sopenharmony_ci  assert.ok(rsaSign instanceof crypto.Sign);
6241cb0ef41Sopenharmony_ci  assert.ok(rsaVerify instanceof crypto.Verify);
6251cb0ef41Sopenharmony_ci
6261cb0ef41Sopenharmony_ci  rsaSign.update(rsaPubPem);
6271cb0ef41Sopenharmony_ci  const rsaSignature = rsaSign.sign(rsaKeyPem, 'hex');
6281cb0ef41Sopenharmony_ci  const expectedSignature = fixtures.readKey(
6291cb0ef41Sopenharmony_ci    'rsa_public_sha1_signature_signedby_rsa_private.sha1',
6301cb0ef41Sopenharmony_ci    'hex'
6311cb0ef41Sopenharmony_ci  );
6321cb0ef41Sopenharmony_ci  assert.strictEqual(rsaSignature, expectedSignature);
6331cb0ef41Sopenharmony_ci
6341cb0ef41Sopenharmony_ci  rsaVerify.update(rsaPubPem);
6351cb0ef41Sopenharmony_ci  assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
6361cb0ef41Sopenharmony_ci}
6371cb0ef41Sopenharmony_ci
6381cb0ef41Sopenharmony_ci//
6391cb0ef41Sopenharmony_ci// Test RSA signing and verification
6401cb0ef41Sopenharmony_ci//
6411cb0ef41Sopenharmony_ci{
6421cb0ef41Sopenharmony_ci  const privateKey = fixtures.readKey('rsa_private_b.pem');
6431cb0ef41Sopenharmony_ci  const publicKey = fixtures.readKey('rsa_public_b.pem');
6441cb0ef41Sopenharmony_ci
6451cb0ef41Sopenharmony_ci  const input = 'I AM THE WALRUS';
6461cb0ef41Sopenharmony_ci
6471cb0ef41Sopenharmony_ci  const signature = fixtures.readKey(
6481cb0ef41Sopenharmony_ci    'I_AM_THE_WALRUS_sha256_signature_signedby_rsa_private_b.sha256',
6491cb0ef41Sopenharmony_ci    'hex'
6501cb0ef41Sopenharmony_ci  );
6511cb0ef41Sopenharmony_ci
6521cb0ef41Sopenharmony_ci  const sign = crypto.createSign('SHA256');
6531cb0ef41Sopenharmony_ci  sign.update(input);
6541cb0ef41Sopenharmony_ci
6551cb0ef41Sopenharmony_ci  const output = sign.sign(privateKey, 'hex');
6561cb0ef41Sopenharmony_ci  assert.strictEqual(output, signature);
6571cb0ef41Sopenharmony_ci
6581cb0ef41Sopenharmony_ci  const verify = crypto.createVerify('SHA256');
6591cb0ef41Sopenharmony_ci  verify.update(input);
6601cb0ef41Sopenharmony_ci
6611cb0ef41Sopenharmony_ci  assert.strictEqual(verify.verify(publicKey, signature, 'hex'), true);
6621cb0ef41Sopenharmony_ci}
6631cb0ef41Sopenharmony_ci
6641cb0ef41Sopenharmony_ci
6651cb0ef41Sopenharmony_ci//
6661cb0ef41Sopenharmony_ci// Test DSA signing and verification
6671cb0ef41Sopenharmony_ci//
6681cb0ef41Sopenharmony_ci{
6691cb0ef41Sopenharmony_ci  const privateKey = fixtures.readKey('dsa_private.pem');
6701cb0ef41Sopenharmony_ci  const publicKey = fixtures.readKey('dsa_public.pem');
6711cb0ef41Sopenharmony_ci
6721cb0ef41Sopenharmony_ci  const input = 'I AM THE WALRUS';
6731cb0ef41Sopenharmony_ci
6741cb0ef41Sopenharmony_ci  // DSA signatures vary across runs so there is no static string to verify
6751cb0ef41Sopenharmony_ci  // against
6761cb0ef41Sopenharmony_ci  const sign = crypto.createSign('SHA1');
6771cb0ef41Sopenharmony_ci  sign.update(input);
6781cb0ef41Sopenharmony_ci  const signature = sign.sign(privateKey, 'hex');
6791cb0ef41Sopenharmony_ci
6801cb0ef41Sopenharmony_ci  const verify = crypto.createVerify('SHA1');
6811cb0ef41Sopenharmony_ci  verify.update(input);
6821cb0ef41Sopenharmony_ci
6831cb0ef41Sopenharmony_ci  assert.strictEqual(verify.verify(publicKey, signature, 'hex'), true);
6841cb0ef41Sopenharmony_ci}
6851cb0ef41Sopenharmony_ci
6861cb0ef41Sopenharmony_ci
6871cb0ef41Sopenharmony_ci//
6881cb0ef41Sopenharmony_ci// Test PBKDF2 with RFC 6070 test vectors (except #4)
6891cb0ef41Sopenharmony_ci//
6901cb0ef41Sopenharmony_cifunction testPBKDF2(password, salt, iterations, keylen, expected) {
6911cb0ef41Sopenharmony_ci  const actual = crypto.pbkdf2Sync(password, salt, iterations, keylen,
6921cb0ef41Sopenharmony_ci                                   'sha256');
6931cb0ef41Sopenharmony_ci  assert.strictEqual(actual, expected);
6941cb0ef41Sopenharmony_ci
6951cb0ef41Sopenharmony_ci  const cb = common.mustCall((err, actual) => {
6961cb0ef41Sopenharmony_ci    assert.strictEqual(actual, expected);
6971cb0ef41Sopenharmony_ci  });
6981cb0ef41Sopenharmony_ci  crypto.pbkdf2(password, salt, iterations, keylen, 'sha256', cb);
6991cb0ef41Sopenharmony_ci}
7001cb0ef41Sopenharmony_ci
7011cb0ef41Sopenharmony_ci
7021cb0ef41Sopenharmony_citestPBKDF2('password', 'salt', 1, 20,
7031cb0ef41Sopenharmony_ci           '\x12\x0f\xb6\xcf\xfc\xf8\xb3\x2c\x43\xe7\x22\x52' +
7041cb0ef41Sopenharmony_ci           '\x56\xc4\xf8\x37\xa8\x65\x48\xc9');
7051cb0ef41Sopenharmony_ci
7061cb0ef41Sopenharmony_citestPBKDF2('password', 'salt', 2, 20,
7071cb0ef41Sopenharmony_ci           '\xae\x4d\x0c\x95\xaf\x6b\x46\xd3\x2d\x0a\xdf\xf9' +
7081cb0ef41Sopenharmony_ci           '\x28\xf0\x6d\xd0\x2a\x30\x3f\x8e');
7091cb0ef41Sopenharmony_ci
7101cb0ef41Sopenharmony_citestPBKDF2('password', 'salt', 4096, 20,
7111cb0ef41Sopenharmony_ci           '\xc5\xe4\x78\xd5\x92\x88\xc8\x41\xaa\x53\x0d\xb6' +
7121cb0ef41Sopenharmony_ci           '\x84\x5c\x4c\x8d\x96\x28\x93\xa0');
7131cb0ef41Sopenharmony_ci
7141cb0ef41Sopenharmony_citestPBKDF2('passwordPASSWORDpassword',
7151cb0ef41Sopenharmony_ci           'saltSALTsaltSALTsaltSALTsaltSALTsalt',
7161cb0ef41Sopenharmony_ci           4096,
7171cb0ef41Sopenharmony_ci           25,
7181cb0ef41Sopenharmony_ci           '\x34\x8c\x89\xdb\xcb\xd3\x2b\x2f\x32\xd8\x14\xb8' +
7191cb0ef41Sopenharmony_ci           '\x11\x6e\x84\xcf\x2b\x17\x34\x7e\xbc\x18\x00\x18\x1c');
7201cb0ef41Sopenharmony_ci
7211cb0ef41Sopenharmony_citestPBKDF2('pass\0word', 'sa\0lt', 4096, 16,
7221cb0ef41Sopenharmony_ci           '\x89\xb6\x9d\x05\x16\xf8\x29\x89\x3c\x69\x62\x26' +
7231cb0ef41Sopenharmony_ci           '\x65\x0a\x86\x87');
724