summaryrefslogtreecommitdiff
path: root/ext/crypto/00_crypto.js
diff options
context:
space:
mode:
authorKenta Moriuchi <moriken@kimamass.com>2024-01-11 07:37:25 +0900
committerGitHub <noreply@github.com>2024-01-10 15:37:25 -0700
commit515a34b4de222e35c7ade1b92614d746e73d4c2e (patch)
tree8284201fc826a33f12597959a8a8be14e0f524bd /ext/crypto/00_crypto.js
parentd4893eb51a01c5a692d8ca74a3b8ff95c5fd1d9f (diff)
refactor: use `core.ensureFastOps()` (#21888)
Diffstat (limited to 'ext/crypto/00_crypto.js')
-rw-r--r--ext/crypto/00_crypto.js191
1 files changed, 104 insertions, 87 deletions
diff --git a/ext/crypto/00_crypto.js b/ext/crypto/00_crypto.js
index d2949eb62..18e51fbd4 100644
--- a/ext/crypto/00_crypto.js
+++ b/ext/crypto/00_crypto.js
@@ -7,20 +7,42 @@
/// <reference path="../web/lib.deno_web.d.ts" />
import { core, primordials } from "ext:core/mod.js";
-const ops = core.ops;
const {
+ isArrayBuffer,
+ isTypedArray,
+ isDataView,
+} = core;
+const {
+ op_crypto_base64url_decode,
+ op_crypto_base64url_encode,
op_crypto_decrypt,
op_crypto_derive_bits,
+ op_crypto_derive_bits_x25519,
op_crypto_encrypt,
+ op_crypto_export_key,
+ op_crypto_export_pkcs8_ed25519,
+ op_crypto_export_pkcs8_x25519,
+ op_crypto_export_spki_ed25519,
+ op_crypto_export_spki_x25519,
+ op_crypto_generate_ed25519_keypair,
op_crypto_generate_key,
+ op_crypto_generate_x25519_keypair,
+ op_crypto_get_random_values,
+ op_crypto_import_key,
+ op_crypto_import_pkcs8_ed25519,
+ op_crypto_import_pkcs8_x25519,
+ op_crypto_import_spki_ed25519,
+ op_crypto_import_spki_x25519,
+ op_crypto_jwk_x_ed25519,
+ op_crypto_random_uuid,
+ op_crypto_sign_ed25519,
op_crypto_sign_key,
op_crypto_subtle_digest,
+ op_crypto_unwrap_key,
+ op_crypto_verify_ed25519,
op_crypto_verify_key,
+ op_crypto_wrap_key,
} = core.ensureFastOps();
-
-import * as webidl from "ext:deno_webidl/00_webidl.js";
-import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
-import { DOMException } from "ext:deno_web/01_dom_exception.js";
const {
ArrayBufferIsView,
ArrayBufferPrototypeGetByteLength,
@@ -57,11 +79,10 @@ const {
WeakMapPrototypeGet,
WeakMapPrototypeSet,
} = primordials;
-const {
- isArrayBuffer,
- isTypedArray,
- isDataView,
-} = core;
+
+import * as webidl from "ext:deno_webidl/00_webidl.js";
+import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
+import { DOMException } from "ext:deno_web/01_dom_exception.js";
const supportedNamedCurves = ["P-256", "P-384", "P-521"];
const recognisedUsages = [
@@ -897,7 +918,7 @@ class SubtleCrypto {
// https://briansmith.org/rustdoc/src/ring/ec/curve25519/ed25519/signing.rs.html#260
const SIGNATURE_LEN = 32 * 2; // ELEM_LEN + SCALAR_LEN
const signature = new Uint8Array(SIGNATURE_LEN);
- if (!ops.op_crypto_sign_ed25519(keyData, data, signature)) {
+ if (!op_crypto_sign_ed25519(keyData, data, signature)) {
throw new DOMException(
"Failed to sign",
"OperationError",
@@ -1371,7 +1392,7 @@ class SubtleCrypto {
);
}
- return ops.op_crypto_verify_ed25519(keyData, data, signature);
+ return op_crypto_verify_ed25519(keyData, data, signature);
}
}
@@ -1461,7 +1482,7 @@ class SubtleCrypto {
switch (normalizedAlgorithm.name) {
case "AES-KW": {
- const cipherText = await ops.op_crypto_wrap_key({
+ const cipherText = await op_crypto_wrap_key({
key: keyData,
algorithm: normalizedAlgorithm.name,
}, bytes);
@@ -1593,7 +1614,7 @@ class SubtleCrypto {
switch (normalizedAlgorithm.name) {
case "AES-KW": {
- const plainText = await ops.op_crypto_unwrap_key({
+ const plainText = await op_crypto_unwrap_key({
key: keyData,
algorithm: normalizedAlgorithm.name,
}, wrappedKey);
@@ -2007,7 +2028,7 @@ async function generateKey(normalizedAlgorithm, extractable, usages) {
}
const privateKeyData = new Uint8Array(32);
const publicKeyData = new Uint8Array(32);
- ops.op_crypto_generate_x25519_keypair(privateKeyData, publicKeyData);
+ op_crypto_generate_x25519_keypair(privateKeyData, publicKeyData);
const handle = {};
WeakMapPrototypeSet(KEY_STORE, handle, privateKeyData);
@@ -2052,7 +2073,7 @@ async function generateKey(normalizedAlgorithm, extractable, usages) {
const privateKeyData = new Uint8Array(ED25519_SEED_LEN);
const publicKeyData = new Uint8Array(ED25519_PUBLIC_KEY_LEN);
if (
- !ops.op_crypto_generate_ed25519_keypair(privateKeyData, publicKeyData)
+ !op_crypto_generate_ed25519_keypair(privateKeyData, publicKeyData)
) {
throw new DOMException("Failed to generate key", "OperationError");
}
@@ -2189,7 +2210,7 @@ function importKeyEd25519(
}
const publicKeyData = new Uint8Array(32);
- if (!ops.op_crypto_import_spki_ed25519(keyData, publicKeyData)) {
+ if (!op_crypto_import_spki_ed25519(keyData, publicKeyData)) {
throw new DOMException("Invalid key data", "DataError");
}
@@ -2220,7 +2241,7 @@ function importKeyEd25519(
}
const privateKeyData = new Uint8Array(32);
- if (!ops.op_crypto_import_pkcs8_ed25519(keyData, privateKeyData)) {
+ if (!op_crypto_import_pkcs8_ed25519(keyData, privateKeyData)) {
throw new DOMException("Invalid key data", "DataError");
}
@@ -2326,7 +2347,7 @@ function importKeyEd25519(
// https://www.rfc-editor.org/rfc/rfc8037#section-2
let privateKeyData;
try {
- privateKeyData = ops.op_crypto_base64url_decode(jwk.d);
+ privateKeyData = op_crypto_base64url_decode(jwk.d);
} catch (_) {
throw new DOMException("invalid private key data", "DataError");
}
@@ -2349,7 +2370,7 @@ function importKeyEd25519(
// https://www.rfc-editor.org/rfc/rfc8037#section-2
let publicKeyData;
try {
- publicKeyData = ops.op_crypto_base64url_decode(jwk.x);
+ publicKeyData = op_crypto_base64url_decode(jwk.x);
} catch (_) {
throw new DOMException("invalid public key data", "DataError");
}
@@ -2412,7 +2433,7 @@ function importKeyX25519(
}
const publicKeyData = new Uint8Array(32);
- if (!ops.op_crypto_import_spki_x25519(keyData, publicKeyData)) {
+ if (!op_crypto_import_spki_x25519(keyData, publicKeyData)) {
throw new DOMException("Invalid key data", "DataError");
}
@@ -2443,7 +2464,7 @@ function importKeyX25519(
}
const privateKeyData = new Uint8Array(32);
- if (!ops.op_crypto_import_pkcs8_x25519(keyData, privateKeyData)) {
+ if (!op_crypto_import_pkcs8_x25519(keyData, privateKeyData)) {
throw new DOMException("Invalid key data", "DataError");
}
@@ -2539,7 +2560,7 @@ function importKeyX25519(
// 9.
if (jwk.d !== undefined) {
// https://www.rfc-editor.org/rfc/rfc8037#section-2
- const privateKeyData = ops.op_crypto_base64url_decode(jwk.d);
+ const privateKeyData = op_crypto_base64url_decode(jwk.d);
const handle = {};
WeakMapPrototypeSet(KEY_STORE, handle, privateKeyData);
@@ -2557,7 +2578,7 @@ function importKeyX25519(
);
} else {
// https://www.rfc-editor.org/rfc/rfc8037#section-2
- const publicKeyData = ops.op_crypto_base64url_decode(jwk.x);
+ const publicKeyData = op_crypto_base64url_decode(jwk.x);
const handle = {};
WeakMapPrototypeSet(KEY_STORE, handle, publicKeyData);
@@ -2600,7 +2621,7 @@ function exportKeyAES(
};
// 3.
- const data = ops.op_crypto_export_key({
+ const data = op_crypto_export_key({
format: "jwksecret",
algorithm: "AES",
}, innerKey);
@@ -2697,7 +2718,7 @@ function importKeyAES(
}
// 4.
- const { rawData } = ops.op_crypto_import_key(
+ const { rawData } = op_crypto_import_key(
{ algorithm: "AES" },
{ jwkSecret: jwk },
);
@@ -2857,7 +2878,7 @@ function importKeyHMAC(
}
// 4.
- const { rawData } = ops.op_crypto_import_key(
+ const { rawData } = op_crypto_import_key(
{ algorithm: "HMAC" },
{ jwkSecret: jwk },
);
@@ -3042,7 +3063,7 @@ function importKeyEC(
}
// 3.
- const { rawData } = ops.op_crypto_import_key({
+ const { rawData } = op_crypto_import_key({
algorithm: normalizedAlgorithm.name,
namedCurve: normalizedAlgorithm.namedCurve,
}, { raw: keyData });
@@ -3083,7 +3104,7 @@ function importKeyEC(
}
// 2-9.
- const { rawData } = ops.op_crypto_import_key({
+ const { rawData } = op_crypto_import_key({
algorithm: normalizedAlgorithm.name,
namedCurve: normalizedAlgorithm.namedCurve,
}, { pkcs8: keyData });
@@ -3126,7 +3147,7 @@ function importKeyEC(
}
// 2-12
- const { rawData } = ops.op_crypto_import_key({
+ const { rawData } = op_crypto_import_key({
algorithm: normalizedAlgorithm.name,
namedCurve: normalizedAlgorithm.namedCurve,
}, { spki: keyData });
@@ -3270,7 +3291,7 @@ function importKeyEC(
if (jwk.d !== undefined) {
// it's also a Private key
- const { rawData } = ops.op_crypto_import_key({
+ const { rawData } = op_crypto_import_key({
algorithm: normalizedAlgorithm.name,
namedCurve: normalizedAlgorithm.namedCurve,
}, { jwkPrivateEc: jwk });
@@ -3293,7 +3314,7 @@ function importKeyEC(
return key;
} else {
- const { rawData } = ops.op_crypto_import_key({
+ const { rawData } = op_crypto_import_key({
algorithm: normalizedAlgorithm.name,
namedCurve: normalizedAlgorithm.namedCurve,
}, { jwkPublicEc: jwk });
@@ -3374,15 +3395,14 @@ function importKeyRSA(
}
// 2-9.
- const { modulusLength, publicExponent, rawData } = ops
- .op_crypto_import_key(
- {
- algorithm: normalizedAlgorithm.name,
- // Needed to perform step 7 without normalization.
- hash: normalizedAlgorithm.hash.name,
- },
- { pkcs8: keyData },
- );
+ const { modulusLength, publicExponent, rawData } = op_crypto_import_key(
+ {
+ algorithm: normalizedAlgorithm.name,
+ // Needed to perform step 7 without normalization.
+ hash: normalizedAlgorithm.hash.name,
+ },
+ { pkcs8: keyData },
+ );
const handle = {};
WeakMapPrototypeSet(KEY_STORE, handle, rawData);
@@ -3420,15 +3440,14 @@ function importKeyRSA(
}
// 2-9.
- const { modulusLength, publicExponent, rawData } = ops
- .op_crypto_import_key(
- {
- algorithm: normalizedAlgorithm.name,
- // Needed to perform step 7 without normalization.
- hash: normalizedAlgorithm.hash.name,
- },
- { spki: keyData },
- );
+ const { modulusLength, publicExponent, rawData } = op_crypto_import_key(
+ {
+ algorithm: normalizedAlgorithm.name,
+ // Needed to perform step 7 without normalization.
+ hash: normalizedAlgorithm.hash.name,
+ },
+ { spki: keyData },
+ );
const handle = {};
WeakMapPrototypeSet(KEY_STORE, handle, rawData);
@@ -3670,14 +3689,13 @@ function importKeyRSA(
);
}
- const { modulusLength, publicExponent, rawData } = ops
- .op_crypto_import_key(
- {
- algorithm: normalizedAlgorithm.name,
- hash: normalizedAlgorithm.hash.name,
- },
- { jwkPrivateRsa: jwk },
- );
+ const { modulusLength, publicExponent, rawData } = op_crypto_import_key(
+ {
+ algorithm: normalizedAlgorithm.name,
+ hash: normalizedAlgorithm.hash.name,
+ },
+ { jwkPrivateRsa: jwk },
+ );
const handle = {};
WeakMapPrototypeSet(KEY_STORE, handle, rawData);
@@ -3713,14 +3731,13 @@ function importKeyRSA(
);
}
- const { modulusLength, publicExponent, rawData } = ops
- .op_crypto_import_key(
- {
- algorithm: normalizedAlgorithm.name,
- hash: normalizedAlgorithm.hash.name,
- },
- { jwkPublicRsa: jwk },
- );
+ const { modulusLength, publicExponent, rawData } = op_crypto_import_key(
+ {
+ algorithm: normalizedAlgorithm.name,
+ hash: normalizedAlgorithm.hash.name,
+ },
+ { jwkPublicRsa: jwk },
+ );
const handle = {};
WeakMapPrototypeSet(KEY_STORE, handle, rawData);
@@ -3875,7 +3892,7 @@ function exportKeyHMAC(format, key, innerKey) {
};
// 3.
- const data = ops.op_crypto_export_key({
+ const data = op_crypto_export_key({
format: "jwksecret",
algorithm: key[_algorithm].name,
}, innerKey);
@@ -3929,7 +3946,7 @@ function exportKeyRSA(format, key, innerKey) {
}
// 2.
- const data = ops.op_crypto_export_key({
+ const data = op_crypto_export_key({
algorithm: key[_algorithm].name,
format: "pkcs8",
}, innerKey);
@@ -3947,7 +3964,7 @@ function exportKeyRSA(format, key, innerKey) {
}
// 2.
- const data = ops.op_crypto_export_key({
+ const data = op_crypto_export_key({
algorithm: key[_algorithm].name,
format: "spki",
}, innerKey);
@@ -4028,7 +4045,7 @@ function exportKeyRSA(format, key, innerKey) {
}
// 5-6.
- const data = ops.op_crypto_export_key({
+ const data = op_crypto_export_key({
format: key[_type] === "private" ? "jwkprivate" : "jwkpublic",
algorithm: key[_algorithm].name,
}, innerKey);
@@ -4070,7 +4087,7 @@ function exportKeyEd25519(format, key, innerKey) {
);
}
- const spkiDer = ops.op_crypto_export_spki_ed25519(innerKey);
+ const spkiDer = op_crypto_export_spki_ed25519(innerKey);
return TypedArrayPrototypeGetBuffer(spkiDer);
}
case "pkcs8": {
@@ -4082,7 +4099,7 @@ function exportKeyEd25519(format, key, innerKey) {
);
}
- const pkcs8Der = ops.op_crypto_export_pkcs8_ed25519(
+ const pkcs8Der = op_crypto_export_pkcs8_ed25519(
new Uint8Array([0x04, 0x22, ...new SafeArrayIterator(innerKey)]),
);
pkcs8Der[15] = 0x20;
@@ -4090,8 +4107,8 @@ function exportKeyEd25519(format, key, innerKey) {
}
case "jwk": {
const x = key[_type] === "private"
- ? ops.op_crypto_jwk_x_ed25519(innerKey)
- : ops.op_crypto_base64url_encode(innerKey);
+ ? op_crypto_jwk_x_ed25519(innerKey)
+ : op_crypto_base64url_encode(innerKey);
const jwk = {
kty: "OKP",
crv: "Ed25519",
@@ -4100,7 +4117,7 @@ function exportKeyEd25519(format, key, innerKey) {
ext: key[_extractable],
};
if (key[_type] === "private") {
- jwk.d = ops.op_crypto_base64url_encode(innerKey);
+ jwk.d = op_crypto_base64url_encode(innerKey);
}
return jwk;
}
@@ -4132,7 +4149,7 @@ function exportKeyX25519(format, key, innerKey) {
);
}
- const spkiDer = ops.op_crypto_export_spki_x25519(innerKey);
+ const spkiDer = op_crypto_export_spki_x25519(innerKey);
return TypedArrayPrototypeGetBuffer(spkiDer);
}
case "pkcs8": {
@@ -4144,7 +4161,7 @@ function exportKeyX25519(format, key, innerKey) {
);
}
- const pkcs8Der = ops.op_crypto_export_pkcs8_x25519(
+ const pkcs8Der = op_crypto_export_pkcs8_x25519(
new Uint8Array([0x04, 0x22, ...new SafeArrayIterator(innerKey)]),
);
pkcs8Der[15] = 0x20;
@@ -4154,7 +4171,7 @@ function exportKeyX25519(format, key, innerKey) {
if (key[_type] === "private") {
throw new DOMException("Not implemented", "NotSupportedError");
}
- const x = ops.op_crypto_base64url_encode(innerKey);
+ const x = op_crypto_base64url_encode(innerKey);
const jwk = {
kty: "OKP",
crv: "X25519",
@@ -4181,7 +4198,7 @@ function exportKeyEC(format, key, innerKey) {
}
// 2.
- const data = ops.op_crypto_export_key({
+ const data = op_crypto_export_key({
algorithm: key[_algorithm].name,
namedCurve: key[_algorithm].namedCurve,
format: "raw",
@@ -4199,7 +4216,7 @@ function exportKeyEC(format, key, innerKey) {
}
// 2.
- const data = ops.op_crypto_export_key({
+ const data = op_crypto_export_key({
algorithm: key[_algorithm].name,
namedCurve: key[_algorithm].namedCurve,
format: "pkcs8",
@@ -4217,7 +4234,7 @@ function exportKeyEC(format, key, innerKey) {
}
// 2.
- const data = ops.op_crypto_export_key({
+ const data = op_crypto_export_key({
algorithm: key[_algorithm].name,
namedCurve: key[_algorithm].namedCurve,
format: "spki",
@@ -4261,7 +4278,7 @@ function exportKeyEC(format, key, innerKey) {
jwk.alg = algNamedCurve;
// 3.2 - 3.4.
- const data = ops.op_crypto_export_key({
+ const data = op_crypto_export_key({
format: key[_type] === "private" ? "jwkprivate" : "jwkpublic",
algorithm: key[_algorithm].name,
namedCurve: key[_algorithm].namedCurve,
@@ -4288,7 +4305,7 @@ function exportKeyEC(format, key, innerKey) {
jwk.crv = key[_algorithm].namedCurve;
// 3.2 - 3.4
- const data = ops.op_crypto_export_key({
+ const data = op_crypto_export_key({
format: key[_type] === "private" ? "jwkprivate" : "jwkpublic",
algorithm: key[_algorithm].name,
namedCurve: key[_algorithm].namedCurve,
@@ -4490,7 +4507,7 @@ async function deriveBits(normalizedAlgorithm, baseKey, length) {
const u = WeakMapPrototypeGet(KEY_STORE, uHandle);
const secret = new Uint8Array(32);
- const isIdentity = ops.op_crypto_derive_bits_x25519(k, u, secret);
+ const isIdentity = op_crypto_derive_bits_x25519(k, u, secret);
// 6.
if (isIdentity) {
@@ -4696,7 +4713,7 @@ class Crypto {
// Fast path for Uint8Array
const tag = TypedArrayPrototypeGetSymbolToStringTag(typedArray);
if (tag === "Uint8Array") {
- ops.op_crypto_get_random_values(typedArray);
+ op_crypto_get_random_values(typedArray);
return typedArray;
}
typedArray = webidl.converters.ArrayBufferView(
@@ -4725,13 +4742,13 @@ class Crypto {
TypedArrayPrototypeGetByteOffset(typedArray),
TypedArrayPrototypeGetByteLength(typedArray),
);
- ops.op_crypto_get_random_values(ui8);
+ op_crypto_get_random_values(ui8);
return typedArray;
}
randomUUID() {
webidl.assertBranded(this, CryptoPrototype);
- return ops.op_crypto_random_uuid();
+ return op_crypto_random_uuid();
}
get subtle() {