LibWeb: Add Argon2 WPT tests

A modest 4 new passes :)
This commit is contained in:
Tete17
2025-11-27 14:40:56 +01:00
committed by Shannon Booth
parent 94410298f3
commit 0fe7f3e74a
Notes: github-actions[bot] 2026-01-04 14:57:58 +00:00
5 changed files with 189 additions and 0 deletions

View File

@@ -0,0 +1,89 @@
function define_tests() {
var subtle = self.crypto.subtle;
var testData = getTestData();
var testVectors = testData.testVectors;
return setUpBaseKeys().then(function (allKeys) {
var baseKeys = allKeys.baseKeys;
testVectors.forEach(function (vector) {
var algorithmName = vector.algorithm;
var params = vector.params;
var expected = vector.expected;
var testName = algorithmName + ' deriveBits';
// Test deriveBits
subsetTest(
promise_test,
function (test) {
var algorithm = Object.assign({ name: algorithmName }, params);
return subtle
.deriveBits(algorithm, baseKeys[algorithmName], 256)
.then(
function (derivation) {
assert_true(
equalBuffers(derivation, expected),
'Derived correct key'
);
},
function (err) {
assert_unreached(
'deriveBits failed with error ' +
err.name +
': ' +
err.message
);
}
);
},
testName
);
});
});
function setUpBaseKeys() {
var promises = [];
var baseKeys = {};
testVectors.forEach(function (vector) {
var algorithmName = vector.algorithm;
var password = vector.password;
// Key for normal operations
promises.push(
subtle
.importKey('raw-secret', password, algorithmName, false, [
'deriveBits',
])
.then(function (key) {
baseKeys[algorithmName] = key;
})
);
});
return Promise.all(promises).then(function () {
return {
baseKeys: baseKeys,
};
});
}
}
function equalBuffers(a, b) {
if (a.byteLength !== b.byteLength) {
return false;
}
var aView = new Uint8Array(a);
var bView = new Uint8Array(b);
for (var i = 0; i < aView.length; i++) {
if (aView[i] !== bView[i]) {
return false;
}
}
return true;
}

View File

@@ -0,0 +1,18 @@
<!doctype html>
<meta charset=utf-8>
<title>WebCryptoAPI: deriveBits() Using Argon2</title>
<meta name="timeout" content="long">
<script>
self.GLOBAL = {
isWindow: function() { return true; },
isWorker: function() { return false; },
isShadowRealm: function() { return false; },
};
</script>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../../common/subset-tests.js"></script>
<script src="argon2_vectors.js"></script>
<script src="argon2.js"></script>
<div id=log></div>
<script src="../../WebCryptoAPI/derive_bits_keys/argon2.tentative.https.any.js"></script>

View File

@@ -0,0 +1,7 @@
// META: title=WebCryptoAPI: deriveBits() Using Argon2
// META: timeout=long
// META: script=/common/subset-tests.js
// META: script=argon2_vectors.js
// META: script=argon2.js
promise_test(define_tests, 'setup - define tests');

View File

@@ -0,0 +1,66 @@
function getTestData() {
// Test vectors from RFC 9106
// https://www.rfc-editor.org/rfc/rfc9106
// Test vectors from RFC 9106
var testVectors = [
// Argon2d test vector
{
algorithm: 'Argon2d',
password: new Uint8Array(32).fill(0x01),
params: {
memory: 32,
passes: 3,
parallelism: 4,
nonce: new Uint8Array(16).fill(0x02),
secretValue: new Uint8Array(8).fill(0x03),
associatedData: new Uint8Array(12).fill(0x04),
},
expected: new Uint8Array([
0x51, 0x2b, 0x39, 0x1b, 0x6f, 0x11, 0x62, 0x97, 0x53, 0x71, 0xd3, 0x09,
0x19, 0x73, 0x42, 0x94, 0xf8, 0x68, 0xe3, 0xbe, 0x39, 0x84, 0xf3, 0xc1,
0xa1, 0x3a, 0x4d, 0xb9, 0xfa, 0xbe, 0x4a, 0xcb,
]),
},
// Argon2i test vector
{
algorithm: 'Argon2i',
password: new Uint8Array(32).fill(0x01),
params: {
memory: 32,
passes: 3,
parallelism: 4,
nonce: new Uint8Array(16).fill(0x02),
secretValue: new Uint8Array(8).fill(0x03),
associatedData: new Uint8Array(12).fill(0x04),
},
expected: new Uint8Array([
0xc8, 0x14, 0xd9, 0xd1, 0xdc, 0x7f, 0x37, 0xaa, 0x13, 0xf0, 0xd7, 0x7f,
0x24, 0x94, 0xbd, 0xa1, 0xc8, 0xde, 0x6b, 0x01, 0x6d, 0xd3, 0x88, 0xd2,
0x99, 0x52, 0xa4, 0xc4, 0x67, 0x2b, 0x6c, 0xe8,
]),
},
// Argon2id test vector
{
algorithm: 'Argon2id',
password: new Uint8Array(32).fill(0x01),
params: {
memory: 32,
passes: 3,
parallelism: 4,
nonce: new Uint8Array(16).fill(0x02),
secretValue: new Uint8Array(8).fill(0x03),
associatedData: new Uint8Array(12).fill(0x04),
},
expected: new Uint8Array([
0x0d, 0x64, 0x0d, 0xf5, 0x8d, 0x78, 0x76, 0x6c, 0x08, 0xc0, 0x37, 0xa3,
0x4a, 0x8b, 0x53, 0xc9, 0xd0, 0x1e, 0xf0, 0x45, 0x2d, 0x75, 0xb6, 0x5e,
0xb5, 0x25, 0x20, 0xe9, 0x6b, 0x01, 0xe6, 0x59,
]),
},
];
return {
testVectors: testVectors,
};
}