Files
node/test/parallel/test-crypto-hkdf.js
Filip Skokan acbc3ff133 test,benchmark: drop OpenSSL 1.x-only coverage
Delete test-crypto-ecb.js, which can no longer run anywhere: Blowfish
is available only from OpenSSL's legacy provider and is absent from
BoringSSL.

The addon and cctest version guards become OPENSSL_IS_BORINGSSL checks.
Remove legacy digest-alias coverage and unreachable OpenSSL 1.x
branches. JavaScript test and benchmark gates, skip reasons, and TLS
error expectations that distinguished OpenSSL 1.x from provider-backed
OpenSSL now distinguish BoringSSL, including PKCS12 parsing, named DH
groups, private-key serialization, RSA-PSS parameters, consolidated
provider caches, FIPS availability, and empty KMAC output. Keep the CCM
alternate finalization error only for BoringSSL.

Provider-only test skips use version-neutral terminology or state the
actual minimum OpenSSL version.

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
Assisted-by: Codex
PR-URL: https://github.com/nodejs/node/pull/64777
Fixes: https://github.com/nodejs/node/issues/56733
Refs: https://github.com/nodejs/node/pull/64211
Refs: https://github.com/nodejs/node/pull/63966
Refs: https://github.com/nodejs/node/pull/53329
Refs: https://github.com/nodejs/node/pull/48950
Refs: https://github.com/nodejs/node/issues/45465
Refs: https://github.com/nodejs/node/issues/42827
Refs: https://github.com/nodejs/node/issues/47076
Refs: https://github.com/nodejs/node/pull/62862
Refs: https://github.com/nodejs/node/pull/38512
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Richard Lau <richard.lau@ibm.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
2026-10-25 22:36:32 +01:00

240 lines
7.0 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const { kMaxLength } = require('buffer');
const assert = require('assert');
const {
createSecretKey,
hkdf,
hkdfSync,
getHashes
} = require('crypto');
const { isBoringSSL } = require('../common/crypto');
{
assert.throws(() => hkdf(), {
code: 'ERR_INVALID_ARG_TYPE',
message: /The "digest" argument must be of type string/
});
[1, {}, [], false, Infinity].forEach((i) => {
assert.throws(() => hkdf(i, 'a'), {
code: 'ERR_INVALID_ARG_TYPE',
message: /^The "digest" argument must be of type string/
});
assert.throws(() => hkdfSync(i, 'a'), {
code: 'ERR_INVALID_ARG_TYPE',
message: /^The "digest" argument must be of type string/
});
});
[1, {}, [], false, Infinity].forEach((i) => {
assert.throws(() => hkdf('sha256', i), {
code: 'ERR_INVALID_ARG_TYPE',
message: /^The "ikm" argument must be /
});
assert.throws(() => hkdfSync('sha256', i), {
code: 'ERR_INVALID_ARG_TYPE',
message: /^The "ikm" argument must be /
});
});
[1, {}, [], false, Infinity].forEach((i) => {
assert.throws(() => hkdf('sha256', 'secret', i), {
code: 'ERR_INVALID_ARG_TYPE',
message: /^The "salt" argument must be /
});
assert.throws(() => hkdfSync('sha256', 'secret', i), {
code: 'ERR_INVALID_ARG_TYPE',
message: /^The "salt" argument must be /
});
});
[1, {}, [], false, Infinity].forEach((i) => {
assert.throws(() => hkdf('sha256', 'secret', 'salt', i), {
code: 'ERR_INVALID_ARG_TYPE',
message: /^The "info" argument must be /
});
assert.throws(() => hkdfSync('sha256', 'secret', 'salt', i), {
code: 'ERR_INVALID_ARG_TYPE',
message: /^The "info" argument must be /
});
});
['test', {}, [], false].forEach((i) => {
assert.throws(() => hkdf('sha256', 'secret', 'salt', 'info', i), {
code: 'ERR_INVALID_ARG_TYPE',
message: /^The "length" argument must be of type number/
});
assert.throws(() => hkdfSync('sha256', 'secret', 'salt', 'info', i), {
code: 'ERR_INVALID_ARG_TYPE',
message: /^The "length" argument must be of type number/
});
});
assert.throws(() => hkdf('sha256', 'secret', 'salt', 'info', -1), {
code: 'ERR_OUT_OF_RANGE'
});
assert.throws(() => hkdfSync('sha256', 'secret', 'salt', 'info', -1), {
code: 'ERR_OUT_OF_RANGE'
});
assert.throws(() => hkdf('sha256', 'secret', 'salt', 'info',
kMaxLength + 1), {
code: 'ERR_OUT_OF_RANGE'
});
assert.throws(() => hkdfSync('sha256', 'secret', 'salt', 'info',
kMaxLength + 1), {
code: 'ERR_OUT_OF_RANGE'
});
assert.throws(() => hkdfSync('unknown', 'a', '', '', 10), {
code: 'ERR_CRYPTO_INVALID_DIGEST'
});
assert.throws(() => hkdf('unknown', 'a', '', '', 10, common.mustNotCall()), {
code: 'ERR_CRYPTO_INVALID_DIGEST'
});
assert.throws(() => hkdf('unknown', 'a', '', Buffer.alloc(1025), 10,
common.mustNotCall()), {
code: 'ERR_OUT_OF_RANGE'
});
assert.throws(() => hkdfSync('unknown', 'a', '', Buffer.alloc(1025), 10), {
code: 'ERR_OUT_OF_RANGE'
});
{
const info = new Uint8Array(2048);
Object.defineProperty(info, 'byteLength', {
__proto__: null,
get() {
return 1;
},
});
const calls = [
() => hkdf('sha256', 'a', '', info, 10, common.mustNotCall()),
() => hkdfSync('sha256', 'a', '', info, 10),
];
for (const call of calls)
assert.throws(call, { code: 'ERR_OUT_OF_RANGE' });
}
assert.throws(
() => hkdf('sha512', 'a', '', '', 64 * 255 + 1, common.mustNotCall()), {
code: 'ERR_CRYPTO_INVALID_KEYLEN'
});
assert.throws(
() => hkdfSync('sha512', 'a', '', '', 64 * 255 + 1), {
code: 'ERR_CRYPTO_INVALID_KEYLEN'
});
}
const algorithms = [
['sha256', '0123456789abcdef', '0123456789abcdef', 'info', 10],
['sha256', '', '', '', 10],
['sha256', '', 'salt', '', 10],
['sha512', 'secret', 'salt', '', 15],
];
algorithms.forEach(([ hash, secret, salt, info, length ]) => {
{
const syncResult = hkdfSync(hash, secret, salt, info, length);
assert(syncResult instanceof ArrayBuffer);
let is_async = false;
hkdf(hash, secret, salt, info, length,
common.mustSucceed((asyncResult) => {
assert(is_async);
assert(asyncResult instanceof ArrayBuffer);
assert.deepStrictEqual(syncResult, asyncResult);
}));
// Keep this after the hkdf call above. This verifies
// that the callback is invoked asynchronously.
is_async = true;
}
{
const buf_secret = Buffer.from(secret);
const buf_salt = Buffer.from(salt);
const buf_info = Buffer.from(info);
const syncResult = hkdfSync(hash, buf_secret, buf_salt, buf_info, length);
hkdf(hash, buf_secret, buf_salt, buf_info, length,
common.mustSucceed((asyncResult) => {
assert.deepStrictEqual(syncResult, asyncResult);
}));
}
{
const key_secret = createSecretKey(Buffer.from(secret));
const buf_salt = Buffer.from(salt);
const buf_info = Buffer.from(info);
const syncResult = hkdfSync(hash, key_secret, buf_salt, buf_info, length);
hkdf(hash, key_secret, buf_salt, buf_info, length,
common.mustSucceed((asyncResult) => {
assert.deepStrictEqual(syncResult, asyncResult);
}));
}
{
const ta_secret = new Uint8Array(Buffer.from(secret));
const ta_salt = new Uint16Array(Buffer.from(salt));
const ta_info = new Uint32Array(Buffer.from(info));
const syncResult = hkdfSync(hash, ta_secret, ta_salt, ta_info, length);
hkdf(hash, ta_secret, ta_salt, ta_info, length,
common.mustSucceed((asyncResult) => {
assert.deepStrictEqual(syncResult, asyncResult);
}));
}
{
const ta_secret = new Uint8Array(Buffer.from(secret));
const ta_salt = new Uint16Array(Buffer.from(salt));
const ta_info = new Uint32Array(Buffer.from(info));
const syncResult = hkdfSync(
hash,
ta_secret.buffer,
ta_salt.buffer,
ta_info.buffer,
length);
hkdf(hash, ta_secret, ta_salt, ta_info, length,
common.mustSucceed((asyncResult) => {
assert.deepStrictEqual(syncResult, asyncResult);
}));
}
{
const ta_secret = new Uint8Array(Buffer.from(secret));
const sa_salt = new SharedArrayBuffer(0);
const sa_info = new SharedArrayBuffer(1);
const syncResult = hkdfSync(
hash,
ta_secret.buffer,
sa_salt,
sa_info,
length);
hkdf(hash, ta_secret, sa_salt, sa_info, length,
common.mustSucceed((asyncResult) => {
assert.deepStrictEqual(syncResult, asyncResult);
}));
}
});
if (isBoringSSL) {
const kKnownUnsupported = ['shake128', 'shake256'];
for (const hash of getHashes()) {
if (kKnownUnsupported.includes(hash)) continue;
assert(hkdfSync(hash, 'key', 'salt', 'info', 5));
}
}