2021-07-06 08:16:04 -04:00
|
|
|
import { assert, assertEquals, unitTest } from "./test_util.ts";
|
|
|
|
|
2021-08-13 05:27:56 -04:00
|
|
|
// https://github.com/denoland/deno/issues/11664
|
|
|
|
unitTest(async function testImportArrayBufferKey() {
|
|
|
|
const subtle = window.crypto.subtle;
|
|
|
|
assert(subtle);
|
|
|
|
|
|
|
|
// deno-fmt-ignore
|
|
|
|
const key = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
|
|
|
|
|
|
|
|
const cryptoKey = await subtle.importKey(
|
|
|
|
"raw",
|
|
|
|
key.buffer,
|
|
|
|
{ name: "HMAC", hash: "SHA-1" },
|
|
|
|
true,
|
|
|
|
["sign"],
|
|
|
|
);
|
|
|
|
assert(cryptoKey);
|
|
|
|
|
|
|
|
// Test key usage
|
|
|
|
await subtle.sign({ name: "HMAC" }, cryptoKey, new Uint8Array(8));
|
|
|
|
});
|
|
|
|
|
2021-07-12 08:45:36 -04:00
|
|
|
// TODO(@littledivy): Remove this when we enable WPT for sign_verify
|
|
|
|
unitTest(async function testSignVerify() {
|
|
|
|
const subtle = window.crypto.subtle;
|
|
|
|
assert(subtle);
|
|
|
|
for (const algorithm of ["RSA-PSS", "RSASSA-PKCS1-v1_5"]) {
|
|
|
|
for (
|
|
|
|
const hash of [
|
|
|
|
"SHA-1",
|
|
|
|
"SHA-256",
|
|
|
|
"SHA-384",
|
|
|
|
"SHA-512",
|
|
|
|
]
|
|
|
|
) {
|
|
|
|
const keyPair = await subtle.generateKey(
|
|
|
|
{
|
|
|
|
name: algorithm,
|
|
|
|
modulusLength: 2048,
|
|
|
|
publicExponent: new Uint8Array([1, 0, 1]),
|
|
|
|
hash,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
["sign", "verify"],
|
|
|
|
);
|
|
|
|
|
|
|
|
const data = new Uint8Array([1, 2, 3]);
|
|
|
|
const signAlgorithm = { name: algorithm, saltLength: 32 };
|
|
|
|
|
|
|
|
const signature = await subtle.sign(
|
|
|
|
signAlgorithm,
|
|
|
|
keyPair.privateKey,
|
|
|
|
data,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(signature);
|
|
|
|
assert(signature.byteLength > 0);
|
|
|
|
assert(signature.byteLength % 8 == 0);
|
|
|
|
assert(signature instanceof ArrayBuffer);
|
|
|
|
|
|
|
|
const verified = await subtle.verify(
|
|
|
|
signAlgorithm,
|
|
|
|
keyPair.publicKey,
|
|
|
|
signature,
|
|
|
|
data,
|
|
|
|
);
|
|
|
|
assert(verified);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-07-06 08:16:04 -04:00
|
|
|
unitTest(async function testGenerateRSAKey() {
|
|
|
|
const subtle = window.crypto.subtle;
|
|
|
|
assert(subtle);
|
|
|
|
|
|
|
|
const keyPair = await subtle.generateKey(
|
|
|
|
{
|
|
|
|
name: "RSA-PSS",
|
|
|
|
modulusLength: 2048,
|
|
|
|
publicExponent: new Uint8Array([1, 0, 1]),
|
|
|
|
hash: "SHA-256",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
["sign", "verify"],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(keyPair.privateKey);
|
|
|
|
assert(keyPair.publicKey);
|
|
|
|
assertEquals(keyPair.privateKey.extractable, true);
|
|
|
|
assert(keyPair.privateKey.usages.includes("sign"));
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(async function testGenerateHMACKey() {
|
|
|
|
const key = await window.crypto.subtle.generateKey(
|
|
|
|
{
|
|
|
|
name: "HMAC",
|
|
|
|
hash: "SHA-512",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
["sign", "verify"],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(key);
|
|
|
|
assertEquals(key.extractable, true);
|
|
|
|
assert(key.usages.includes("sign"));
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(async function testSignECDSA() {
|
|
|
|
const key = await window.crypto.subtle.generateKey(
|
|
|
|
{
|
|
|
|
name: "ECDSA",
|
|
|
|
namedCurve: "P-384",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
["sign", "verify"],
|
|
|
|
);
|
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const encoded = encoder.encode("Hello, World!");
|
|
|
|
const signature = await window.crypto.subtle.sign(
|
|
|
|
{ name: "ECDSA", hash: "SHA-384" },
|
|
|
|
key.privateKey,
|
|
|
|
encoded,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(signature);
|
|
|
|
});
|
2021-07-07 10:33:58 -04:00
|
|
|
|
|
|
|
// https://github.com/denoland/deno/issues/11313
|
|
|
|
unitTest(async function testSignRSASSAKey() {
|
|
|
|
const subtle = window.crypto.subtle;
|
|
|
|
assert(subtle);
|
|
|
|
|
|
|
|
const keyPair = await subtle.generateKey(
|
|
|
|
{
|
|
|
|
name: "RSASSA-PKCS1-v1_5",
|
|
|
|
modulusLength: 2048,
|
|
|
|
publicExponent: new Uint8Array([1, 0, 1]),
|
|
|
|
hash: "SHA-256",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
["sign", "verify"],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(keyPair.privateKey);
|
|
|
|
assert(keyPair.publicKey);
|
|
|
|
assertEquals(keyPair.privateKey.extractable, true);
|
|
|
|
assert(keyPair.privateKey.usages.includes("sign"));
|
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const encoded = encoder.encode("Hello, World!");
|
|
|
|
|
|
|
|
const signature = await window.crypto.subtle.sign(
|
|
|
|
{ name: "RSASSA-PKCS1-v1_5" },
|
|
|
|
keyPair.privateKey,
|
|
|
|
encoded,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(signature);
|
|
|
|
});
|
2021-08-14 10:23:11 -04:00
|
|
|
|
2021-08-17 05:29:32 -04:00
|
|
|
unitTest(async function subtleCryptoHmacImportExport() {
|
2021-08-14 10:23:11 -04:00
|
|
|
// deno-fmt-ignore
|
|
|
|
const rawKey = new Uint8Array([
|
|
|
|
1, 2, 3, 4, 5, 6, 7, 8,
|
|
|
|
9, 10, 11, 12, 13, 14, 15, 16
|
|
|
|
]);
|
|
|
|
const key = await crypto.subtle.importKey(
|
|
|
|
"raw",
|
|
|
|
rawKey,
|
|
|
|
{ name: "HMAC", hash: "SHA-256" },
|
|
|
|
true,
|
|
|
|
["sign"],
|
|
|
|
);
|
|
|
|
const actual = await crypto.subtle.sign(
|
|
|
|
{ name: "HMAC" },
|
|
|
|
key,
|
|
|
|
new Uint8Array([1, 2, 3, 4]),
|
|
|
|
);
|
|
|
|
// deno-fmt-ignore
|
|
|
|
const expected = new Uint8Array([
|
|
|
|
59, 170, 255, 216, 51, 141, 51, 194,
|
|
|
|
213, 48, 41, 191, 184, 40, 216, 47,
|
|
|
|
130, 165, 203, 26, 163, 43, 38, 71,
|
|
|
|
23, 122, 222, 1, 146, 46, 182, 87,
|
|
|
|
]);
|
|
|
|
assertEquals(
|
|
|
|
new Uint8Array(actual),
|
|
|
|
expected,
|
|
|
|
);
|
2021-08-17 05:29:32 -04:00
|
|
|
|
|
|
|
const exportedKey = await crypto.subtle.exportKey("raw", key);
|
|
|
|
assertEquals(new Uint8Array(exportedKey), rawKey);
|
2021-08-14 10:23:11 -04:00
|
|
|
});
|