Files
node/test/parallel/test-webcrypto-encrypt-decrypt-aes.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

314 lines
8.2 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const { isBoringSSL } = require('../common/crypto');
const assert = require('assert');
const { getFips } = require('crypto');
const { subtle } = globalThis.crypto;
async function testEncrypt({ keyBuffer, algorithm, plaintext, result }) {
// Using a copy of plaintext to prevent tampering of the original
plaintext = Buffer.from(plaintext);
const keyFormat = algorithm.name === 'AES-OCB' ? 'raw-secret' : 'raw';
const key = await subtle.importKey(
keyFormat,
keyBuffer,
{ name: algorithm.name },
false,
['encrypt', 'decrypt']);
const output = await subtle.encrypt(algorithm, key, plaintext);
plaintext[0] = 255 - plaintext[0];
assert.strictEqual(
Buffer.from(output).toString('hex'),
Buffer.from(result).toString('hex'));
// Converting the returned ArrayBuffer into a Buffer right away,
// so that the next line works
const check = Buffer.from(await subtle.decrypt(algorithm, key, output));
check[0] = 255 - check[0];
assert.strictEqual(
Buffer.from(check).toString('hex'),
Buffer.from(plaintext).toString('hex'));
}
async function testEncryptNoEncrypt({ keyBuffer, algorithm, plaintext }) {
const keyFormat = algorithm.name === 'AES-OCB' ? 'raw-secret' : 'raw';
const key = await subtle.importKey(
keyFormat,
keyBuffer,
{ name: algorithm.name },
false,
['decrypt']);
return assert.rejects(subtle.encrypt(algorithm, key, plaintext), {
message: /Unable to use this key to encrypt/
});
}
async function testEncryptNoDecrypt({ keyBuffer, algorithm, plaintext }) {
const keyFormat = algorithm.name === 'AES-OCB' ? 'raw-secret' : 'raw';
const key = await subtle.importKey(
keyFormat,
keyBuffer,
{ name: algorithm.name },
false,
['encrypt']);
const output = await subtle.encrypt(algorithm, key, plaintext);
return assert.rejects(subtle.decrypt(algorithm, key, output), {
message: /Unable to use this key to decrypt/
});
}
async function testEncryptWrongAlg({ keyBuffer, algorithm, plaintext }, alg) {
assert.notStrictEqual(algorithm.name, alg);
const keyFormat = alg === 'AES-OCB' ? 'raw-secret' : 'raw';
const key = await subtle.importKey(
keyFormat,
keyBuffer,
{ name: alg },
false,
['encrypt']);
return assert.rejects(subtle.encrypt(algorithm, key, plaintext), {
message: /Key algorithm mismatch/
});
}
async function testDecryptWrongAlg({ keyBuffer, algorithm, result }, alg) {
if (result === undefined) return;
assert.notStrictEqual(algorithm.name, alg);
const keyFormat = alg === 'AES-OCB' ? 'raw-secret' : 'raw';
const key = await subtle.importKey(
keyFormat,
keyBuffer,
{ name: alg },
false,
['decrypt']);
return assert.rejects(subtle.decrypt(algorithm, key, result), {
message: /Key algorithm mismatch/
});
}
async function testDecrypt({ keyBuffer, algorithm, result }) {
const keyFormat = algorithm.name === 'AES-OCB' ? 'raw-secret' : 'raw';
const key = await subtle.importKey(
keyFormat,
keyBuffer,
{ name: algorithm.name },
false,
['encrypt', 'decrypt']);
await subtle.decrypt(algorithm, key, result);
}
// Test aes-cbc vectors
{
const {
passing,
failing,
decryptionFailing
} = require('../fixtures/crypto/aes_cbc')();
(async function() {
const variations = [];
passing.forEach((vector) => {
variations.push(testEncrypt(vector));
variations.push(testEncryptNoEncrypt(vector));
variations.push(testEncryptNoDecrypt(vector));
variations.push(testEncryptWrongAlg(vector, 'AES-CTR'));
variations.push(testDecryptWrongAlg(vector, 'AES-CTR'));
});
failing.forEach((vector) => {
variations.push(assert.rejects(testEncrypt(vector), {
message: /algorithm\.iv must contain exactly 16 bytes/
}));
variations.push(assert.rejects(testDecrypt(vector), {
message: /algorithm\.iv must contain exactly 16 bytes/
}));
});
decryptionFailing.forEach((vector) => {
variations.push(assert.rejects(testDecrypt(vector), {
name: 'OperationError'
}));
});
await Promise.all(variations);
})().then(common.mustCall());
}
// Test aes-ctr vectors
{
const {
passing,
failing,
decryptionFailing
} = require('../fixtures/crypto/aes_ctr')();
(async function() {
const variations = [];
passing.forEach((vector) => {
variations.push(testEncrypt(vector));
variations.push(testEncryptNoEncrypt(vector));
variations.push(testEncryptNoDecrypt(vector));
variations.push(testEncryptWrongAlg(vector, 'AES-CBC'));
variations.push(testDecryptWrongAlg(vector, 'AES-CBC'));
});
// TODO(@jasnell): These fail for different reasons. Need to
// make them consistent
failing.forEach((vector) => {
variations.push(assert.rejects(testEncrypt(vector), {
message: /.*/
}));
variations.push(assert.rejects(testDecrypt(vector), {
message: /.*/
}));
});
decryptionFailing.forEach((vector) => {
variations.push(assert.rejects(testDecrypt(vector), {
name: 'OperationError'
}));
});
await Promise.all(variations);
})().then(common.mustCall());
}
// Test aes-gcm vectors
{
const {
passing,
failing,
decryptionFailing
} = require('../fixtures/crypto/aes_gcm')();
(async function() {
const variations = [];
passing.forEach((vector) => {
variations.push(testEncrypt(vector));
variations.push(testEncryptNoEncrypt(vector));
variations.push(testEncryptNoDecrypt(vector));
variations.push(testEncryptWrongAlg(vector, 'AES-CBC'));
variations.push(testDecryptWrongAlg(vector, 'AES-CBC'));
});
failing.forEach((vector) => {
variations.push(assert.rejects(testEncrypt(vector), {
message: /is not a valid AES-GCM tag length/
}));
variations.push(assert.rejects(testDecrypt(vector), {
message: /is not a valid AES-GCM tag length/
}));
});
decryptionFailing.forEach((vector) => {
variations.push(assert.rejects(testDecrypt(vector), {
name: 'OperationError'
}));
});
await Promise.all(variations);
})().then(common.mustCall());
}
// Test aes-ocb vectors
if (!isBoringSSL) {
const {
passing,
failing,
decryptionFailing
} = require('../fixtures/crypto/aes_ocb')();
(async function() {
if (getFips() === 1) {
await assert.rejects(
testEncrypt(passing[0]),
{ name: 'NotSupportedError', message: 'Unrecognized algorithm name' });
return;
}
const variations = [];
passing.forEach((vector) => {
variations.push(testEncrypt(vector));
variations.push(testEncryptNoEncrypt(vector));
variations.push(testEncryptNoDecrypt(vector));
variations.push(testEncryptWrongAlg(vector, 'AES-GCM'));
variations.push(testDecryptWrongAlg(vector, 'AES-GCM'));
});
failing.forEach((vector) => {
variations.push(assert.rejects(testEncrypt(vector), {
message: /is not a valid AES-OCB tag length/
}));
variations.push(assert.rejects(testDecrypt(vector), {
message: /is not a valid AES-OCB tag length/
}));
});
decryptionFailing.forEach((vector) => {
variations.push(assert.rejects(testDecrypt(vector), {
name: 'OperationError'
}));
});
await Promise.all(variations);
})().then(common.mustCall());
}
{
(async function() {
const secretKey = await subtle.generateKey(
{
name: 'AES-GCM',
length: 256,
},
false,
['encrypt', 'decrypt'],
);
const iv = globalThis.crypto.getRandomValues(new Uint8Array(12));
const aad = globalThis.crypto.getRandomValues(new Uint8Array(32));
const encrypted = await subtle.encrypt(
{
name: 'AES-GCM',
iv,
additionalData: aad,
tagLength: 128
},
secretKey,
globalThis.crypto.getRandomValues(new Uint8Array(32))
);
await subtle.decrypt(
{
name: 'AES-GCM',
iv,
additionalData: aad,
tagLength: 128,
},
secretKey,
new Uint8Array(encrypted),
);
})().then(common.mustCall());
}