1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

refactor(ext/crypto): de-duplicate copyBuffer code (#13030)

This commit de-duplicates the buffer copying code in ext/crypto.

Co-authored-by: yacinehmito <yacinehmito@users.noreply.github.com>
This commit is contained in:
Luca Casonato 2021-12-09 16:50:04 +01:00 committed by GitHub
parent fce7e4bd31
commit e70dc53460
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -268,6 +268,18 @@
return normalizedAlgorithm; return normalizedAlgorithm;
} }
/**
* @param {ArrayBufferView | ArrayBuffer} input
* @returns
*/
function copyBuffer(input) {
return TypedArrayPrototypeSlice(
ArrayBufferIsView(input)
? new Uint8Array(input.buffer, input.byteOffset, input.byteLength)
: new Uint8Array(input),
);
}
const _handle = Symbol("[[handle]]"); const _handle = Symbol("[[handle]]");
const _algorithm = Symbol("[[algorithm]]"); const _algorithm = Symbol("[[algorithm]]");
const _extractable = Symbol("[[extractable]]"); const _extractable = Symbol("[[extractable]]");
@ -446,13 +458,7 @@
context: "Argument 2", context: "Argument 2",
}); });
if (ArrayBufferIsView(data)) { data = copyBuffer(data);
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
} else {
data = new Uint8Array(data);
}
data = TypedArrayPrototypeSlice(data);
algorithm = normalizeAlgorithm(algorithm, "digest"); algorithm = normalizeAlgorithm(algorithm, "digest");
@ -489,12 +495,7 @@
}); });
// 2. // 2.
if (ArrayBufferIsView(data)) { data = copyBuffer(data);
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
} else {
data = new Uint8Array(data);
}
data = TypedArrayPrototypeSlice(data);
// 3. // 3.
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "encrypt"); const normalizedAlgorithm = normalizeAlgorithm(algorithm, "encrypt");
@ -542,12 +543,7 @@
}); });
// 2. // 2.
if (ArrayBufferIsView(data)) { data = copyBuffer(data);
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
} else {
data = new Uint8Array(data);
}
data = TypedArrayPrototypeSlice(data);
// 3. // 3.
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "decrypt"); const normalizedAlgorithm = normalizeAlgorithm(algorithm, "decrypt");
@ -583,20 +579,7 @@
// 2. // 2.
if (normalizedAlgorithm.label) { if (normalizedAlgorithm.label) {
if (ArrayBufferIsView(normalizedAlgorithm.label)) { normalizedAlgorithm.label = copyBuffer(normalizedAlgorithm.label);
normalizedAlgorithm.label = new Uint8Array(
normalizedAlgorithm.label.buffer,
normalizedAlgorithm.label.byteOffset,
normalizedAlgorithm.label.byteLength,
);
} else {
normalizedAlgorithm.label = new Uint8Array(
normalizedAlgorithm.label,
);
}
normalizedAlgorithm.label = TypedArrayPrototypeSlice(
normalizedAlgorithm.label,
);
} else { } else {
normalizedAlgorithm.label = new Uint8Array(); normalizedAlgorithm.label = new Uint8Array();
} }
@ -614,20 +597,7 @@
return plainText.buffer; return plainText.buffer;
} }
case "AES-CBC": { case "AES-CBC": {
if (ArrayBufferIsView(normalizedAlgorithm.iv)) { normalizedAlgorithm.iv = copyBuffer(normalizedAlgorithm.iv);
normalizedAlgorithm.iv = new Uint8Array(
normalizedAlgorithm.iv.buffer,
normalizedAlgorithm.iv.byteOffset,
normalizedAlgorithm.iv.byteLength,
);
} else {
normalizedAlgorithm.iv = new Uint8Array(
normalizedAlgorithm.iv,
);
}
normalizedAlgorithm.iv = TypedArrayPrototypeSlice(
normalizedAlgorithm.iv,
);
// 1. // 1.
if (normalizedAlgorithm.iv.byteLength !== 16) { if (normalizedAlgorithm.iv.byteLength !== 16) {
@ -676,12 +646,7 @@
}); });
// 1. // 1.
if (ArrayBufferIsView(data)) { data = copyBuffer(data);
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
} else {
data = new Uint8Array(data);
}
data = TypedArrayPrototypeSlice(data);
// 2. // 2.
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "sign"); const normalizedAlgorithm = normalizeAlgorithm(algorithm, "sign");
@ -822,16 +787,7 @@
// 2. // 2.
if (format !== "jwk") { if (format !== "jwk") {
if (ArrayBufferIsView(keyData) || keyData instanceof ArrayBuffer) { if (ArrayBufferIsView(keyData) || keyData instanceof ArrayBuffer) {
if (ArrayBufferIsView(keyData)) { keyData = copyBuffer(keyData);
keyData = new Uint8Array(
keyData.buffer,
keyData.byteOffset,
keyData.byteLength,
);
} else {
keyData = new Uint8Array(keyData);
}
keyData = TypedArrayPrototypeSlice(keyData);
} else { } else {
throw new TypeError("keyData is a JsonWebKey"); throw new TypeError("keyData is a JsonWebKey");
} }
@ -1130,24 +1086,10 @@
}); });
// 2. // 2.
if (ArrayBufferIsView(signature)) { signature = copyBuffer(signature);
signature = new Uint8Array(
signature.buffer,
signature.byteOffset,
signature.byteLength,
);
} else {
signature = new Uint8Array(signature);
}
signature = TypedArrayPrototypeSlice(signature);
// 3. // 3.
if (ArrayBufferIsView(data)) { data = copyBuffer(data);
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
} else {
data = new Uint8Array(data);
}
data = TypedArrayPrototypeSlice(data);
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "verify"); const normalizedAlgorithm = normalizeAlgorithm(algorithm, "verify");
@ -1388,16 +1330,7 @@
}); });
// 2. // 2.
if (ArrayBufferIsView(wrappedKey)) { wrappedKey = copyBuffer(wrappedKey);
wrappedKey = new Uint8Array(
wrappedKey.buffer,
wrappedKey.byteOffset,
wrappedKey.byteLength,
);
} else {
wrappedKey = new Uint8Array(wrappedKey);
}
wrappedKey = TypedArrayPrototypeSlice(wrappedKey);
let normalizedAlgorithm; let normalizedAlgorithm;
@ -2653,18 +2586,7 @@
const handle = baseKey[_handle]; const handle = baseKey[_handle];
const keyData = WeakMapPrototypeGet(KEY_STORE, handle); const keyData = WeakMapPrototypeGet(KEY_STORE, handle);
if (ArrayBufferIsView(normalizedAlgorithm.salt)) { normalizedAlgorithm.salt = copyBuffer(normalizedAlgorithm.salt);
normalizedAlgorithm.salt = new Uint8Array(
normalizedAlgorithm.salt.buffer,
normalizedAlgorithm.salt.byteOffset,
normalizedAlgorithm.salt.byteLength,
);
} else {
normalizedAlgorithm.salt = new Uint8Array(normalizedAlgorithm.salt);
}
normalizedAlgorithm.salt = TypedArrayPrototypeSlice(
normalizedAlgorithm.salt,
);
const buf = await core.opAsync("op_crypto_derive_bits", { const buf = await core.opAsync("op_crypto_derive_bits", {
key: keyData, key: keyData,
@ -2737,31 +2659,9 @@
const handle = baseKey[_handle]; const handle = baseKey[_handle];
const keyDerivationKey = WeakMapPrototypeGet(KEY_STORE, handle); const keyDerivationKey = WeakMapPrototypeGet(KEY_STORE, handle);
if (ArrayBufferIsView(normalizedAlgorithm.salt)) { normalizedAlgorithm.salt = copyBuffer(normalizedAlgorithm.salt);
normalizedAlgorithm.salt = new Uint8Array(
normalizedAlgorithm.salt.buffer,
normalizedAlgorithm.salt.byteOffset,
normalizedAlgorithm.salt.byteLength,
);
} else {
normalizedAlgorithm.salt = new Uint8Array(normalizedAlgorithm.salt);
}
normalizedAlgorithm.salt = TypedArrayPrototypeSlice(
normalizedAlgorithm.salt,
);
if (ArrayBufferIsView(normalizedAlgorithm.info)) { normalizedAlgorithm.info = copyBuffer(normalizedAlgorithm.info);
normalizedAlgorithm.info = new Uint8Array(
normalizedAlgorithm.info.buffer,
normalizedAlgorithm.info.byteOffset,
normalizedAlgorithm.info.byteLength,
);
} else {
normalizedAlgorithm.info = new Uint8Array(normalizedAlgorithm.info);
}
normalizedAlgorithm.info = TypedArrayPrototypeSlice(
normalizedAlgorithm.info,
);
const buf = await core.opAsync("op_crypto_derive_bits", { const buf = await core.opAsync("op_crypto_derive_bits", {
key: keyDerivationKey, key: keyDerivationKey,
@ -2794,20 +2694,7 @@
// 2. // 2.
if (normalizedAlgorithm.label) { if (normalizedAlgorithm.label) {
if (ArrayBufferIsView(normalizedAlgorithm.label)) { normalizedAlgorithm.label = copyBuffer(normalizedAlgorithm.label);
normalizedAlgorithm.label = new Uint8Array(
normalizedAlgorithm.label.buffer,
normalizedAlgorithm.label.byteOffset,
normalizedAlgorithm.label.byteLength,
);
} else {
normalizedAlgorithm.label = new Uint8Array(
normalizedAlgorithm.label,
);
}
normalizedAlgorithm.label = TypedArrayPrototypeSlice(
normalizedAlgorithm.label,
);
} else { } else {
normalizedAlgorithm.label = new Uint8Array(); normalizedAlgorithm.label = new Uint8Array();
} }
@ -2824,20 +2711,7 @@
return cipherText.buffer; return cipherText.buffer;
} }
case "AES-CBC": { case "AES-CBC": {
if (ArrayBufferIsView(normalizedAlgorithm.iv)) { normalizedAlgorithm.iv = copyBuffer(normalizedAlgorithm.iv);
normalizedAlgorithm.iv = new Uint8Array(
normalizedAlgorithm.iv.buffer,
normalizedAlgorithm.iv.byteOffset,
normalizedAlgorithm.iv.byteLength,
);
} else {
normalizedAlgorithm.iv = new Uint8Array(
normalizedAlgorithm.iv,
);
}
normalizedAlgorithm.iv = TypedArrayPrototypeSlice(
normalizedAlgorithm.iv,
);
// 1. // 1.
if (normalizedAlgorithm.iv.byteLength !== 16) { if (normalizedAlgorithm.iv.byteLength !== 16) {