Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions lib/internal/crypto/aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {

const {
AESCipherJob,
kCryptoJobAsync,
kCryptoJobWebCrypto,
kKeyVariantAES_CTR_128,
kKeyVariantAES_CBC_128,
kKeyVariantAES_GCM_128,
Expand Down Expand Up @@ -107,7 +107,7 @@ function getVariant(name, length) {

function asyncAesCtrCipher(mode, key, data, algorithm) {
return jobPromise(() => new AESCipherJob(
kCryptoJobAsync,
kCryptoJobWebCrypto,
mode,
getCryptoKeyHandle(key),
data,
Expand All @@ -118,7 +118,7 @@ function asyncAesCtrCipher(mode, key, data, algorithm) {

function asyncAesCbcCipher(mode, key, data, algorithm) {
return jobPromise(() => new AESCipherJob(
kCryptoJobAsync,
kCryptoJobWebCrypto,
mode,
getCryptoKeyHandle(key),
data,
Expand All @@ -128,7 +128,7 @@ function asyncAesCbcCipher(mode, key, data, algorithm) {

function asyncAesKwCipher(mode, key, data) {
return jobPromise(() => new AESCipherJob(
kCryptoJobAsync,
kCryptoJobWebCrypto,
mode,
getCryptoKeyHandle(key),
data,
Expand All @@ -140,7 +140,7 @@ function asyncAesGcmCipher(mode, key, data, algorithm) {
const tagByteLength = tagLength / 8;

return jobPromise(() => new AESCipherJob(
kCryptoJobAsync,
kCryptoJobWebCrypto,
mode,
getCryptoKeyHandle(key),
data,
Expand All @@ -155,7 +155,7 @@ function asyncAesOcbCipher(mode, key, data, algorithm) {
const tagByteLength = tagLength / 8;

return jobPromise(() => new AESCipherJob(
kCryptoJobAsync,
kCryptoJobWebCrypto,
mode,
getCryptoKeyHandle(key),
data,
Expand All @@ -175,7 +175,7 @@ function aesCipher(mode, key, data, algorithm) {
}
}

async function aesGenerateKey(algorithm, extractable, keyUsages) {
function aesGenerateKey(algorithm, extractable, keyUsages) {
const { name, length } = algorithm;

const checkUsages = ['wrapKey', 'unwrapKey'];
Expand All @@ -188,14 +188,18 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) {
'Unsupported key usage for an AES key',
'SyntaxError');
}
if (usagesSet.size === 0) {
throw lazyDOMException(
'Usages cannot be empty when creating a key.',
'SyntaxError');
}

const handle = await jobPromise(() => new SecretKeyGenJob(kCryptoJobAsync, length));

return new InternalCryptoKey(
handle,
return jobPromise(() => new SecretKeyGenJob(
kCryptoJobWebCrypto,
length,
{ name, length },
getUsagesMask(usagesSet),
extractable);
extractable));
}

function aesImportKey(
Expand Down
69 changes: 27 additions & 42 deletions lib/internal/crypto/argon2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
const {
FunctionPrototypeCall,
MathPow,
StringPrototypeToLowerCase,
TypedArrayPrototypeGetBuffer,
Uint8Array,
} = primordials;

Expand All @@ -14,14 +12,14 @@ const {
Argon2Job,
kCryptoJobAsync,
kCryptoJobSync,
kCryptoJobWebCrypto,
kTypeArgon2d,
kTypeArgon2i,
kTypeArgon2id,
} = internalBinding('crypto');

const {
lazyDOMException,
promisify,
} = require('internal/util');

const {
Expand All @@ -30,6 +28,7 @@ const {

const {
getArrayBufferOrView,
jobPromise,
} = require('internal/crypto/util');

const {
Expand Down Expand Up @@ -143,20 +142,12 @@ function check(algorithm, parameters) {
validateString(algorithm, 'algorithm');
validateOneOf(algorithm, 'algorithm', ['argon2d', 'argon2i', 'argon2id']);

let type;
switch (algorithm) {
case 'argon2d':
type = kTypeArgon2d;
break;
case 'argon2i':
type = kTypeArgon2i;
break;
case 'argon2id':
type = kTypeArgon2id;
break;
default: // unreachable
throw new ERR_CRYPTO_ARGON2_NOT_SUPPORTED();
}
const type = {
'__proto__': null,
'argon2d': kTypeArgon2d,
'argon2i': kTypeArgon2i,
'argon2id': kTypeArgon2id,
}[algorithm];

validateObject(parameters, 'parameters');

Expand Down Expand Up @@ -193,7 +184,6 @@ function check(algorithm, parameters) {
return { message, nonce, secret, associatedData, tagLength, passes, parallelism, memory, type };
}

const argon2Promise = promisify(argon2);
function validateArgon2DeriveBitsLength(length) {
if (length === null)
throw lazyDOMException('length cannot be null', 'OperationError');
Expand All @@ -211,32 +201,27 @@ function validateArgon2DeriveBitsLength(length) {
}
}

async function argon2DeriveBits(algorithm, baseKey, length) {
function argon2DeriveBits(algorithm, baseKey, length) {
validateArgon2DeriveBitsLength(length);

let result;
try {
result = await argon2Promise(
StringPrototypeToLowerCase(algorithm.name),
{
// TODO(panva): call the job directly without needing to re-export the handle
message: getCryptoKeyHandle(baseKey).export(),
nonce: algorithm.nonce,
parallelism: algorithm.parallelism,
tagLength: length / 8,
memory: algorithm.memory,
passes: algorithm.passes,
secret: algorithm.secretValue,
associatedData: algorithm.associatedData,
},
);
} catch (err) {
throw lazyDOMException(
'The operation failed for an operation-specific reason',
{ name: 'OperationError', cause: err });
}

return TypedArrayPrototypeGetBuffer(result);
const type = {
'__proto__': null,
'Argon2d': kTypeArgon2d,
'Argon2i': kTypeArgon2i,
'Argon2id': kTypeArgon2id,
}[algorithm.name];

return jobPromise(() => new Argon2Job(
kCryptoJobWebCrypto,
getCryptoKeyHandle(baseKey),
algorithm.nonce,
algorithm.parallelism,
length / 8,
algorithm.memory,
algorithm.passes,
algorithm.secretValue === undefined ? new Uint8Array(0) : algorithm.secretValue,
algorithm.associatedData === undefined ? new Uint8Array(0) : algorithm.associatedData,
type));
}

module.exports = {
Expand Down
60 changes: 24 additions & 36 deletions lib/internal/crypto/cfrg.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {

const {
SignJob,
kCryptoJobAsync,
kCryptoJobWebCrypto,
kKeyFormatDER,
kKeyFormatRawPublic,
kSignJobModeSign,
Expand Down Expand Up @@ -73,7 +73,7 @@ function verifyAcceptableCfrgKeyUse(name, isPublic, usages) {
}
}

async function cfrgGenerateKey(algorithm, extractable, keyUsages) {
function cfrgGenerateKey(algorithm, extractable, keyUsages) {
const { name } = algorithm;

const usageSet = new SafeSet(keyUsages);
Expand All @@ -97,23 +97,13 @@ async function cfrgGenerateKey(algorithm, extractable, keyUsages) {
}
break;
}
let nid;
switch (name) {
case 'Ed25519':
nid = EVP_PKEY_ED25519;
break;
case 'Ed448':
nid = EVP_PKEY_ED448;
break;
case 'X25519':
nid = EVP_PKEY_X25519;
break;
case 'X448':
nid = EVP_PKEY_X448;
break;
}

const handles = await jobPromise(() => new NidKeyPairGenJob(kCryptoJobAsync, nid));
const nid = {
'__proto__': null,
'Ed25519': EVP_PKEY_ED25519,
'Ed448': EVP_PKEY_ED448,
'X25519': EVP_PKEY_X25519,
'X448': EVP_PKEY_X448,
}[name];

let publicUsages;
let privateUsages;
Expand All @@ -134,21 +124,19 @@ async function cfrgGenerateKey(algorithm, extractable, keyUsages) {

const keyAlgorithm = { name };

const publicKey =
new InternalCryptoKey(
handles[0],
keyAlgorithm,
getUsagesMask(publicUsages),
true);

const privateKey =
new InternalCryptoKey(
handles[1],
keyAlgorithm,
getUsagesMask(privateUsages),
extractable);
if (privateUsages.size === 0) {
throw lazyDOMException(
'Usages cannot be empty when creating a key.',
'SyntaxError');
}

return { __proto__: null, privateKey, publicKey };
return jobPromise(() => new NidKeyPairGenJob(
kCryptoJobWebCrypto,
nid,
keyAlgorithm,
getUsagesMask(publicUsages),
getUsagesMask(privateUsages),
extractable));
}

function cfrgExportKey(key, format) {
Expand Down Expand Up @@ -243,15 +231,15 @@ function cfrgImportKey(
extractable);
}

async function eddsaSignVerify(key, data, algorithm, signature) {
function eddsaSignVerify(key, data, algorithm, signature) {
const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
const type = mode === kSignJobModeSign ? 'private' : 'public';

if (getCryptoKeyType(key) !== type)
throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError');

return await jobPromise(() => new SignJob(
kCryptoJobAsync,
return jobPromise(() => new SignJob(
kCryptoJobWebCrypto,
mode,
getCryptoKeyHandle(key),
undefined,
Expand Down
20 changes: 12 additions & 8 deletions lib/internal/crypto/chacha20_poly1305.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
const {
ChaCha20Poly1305CipherJob,
SecretKeyGenJob,
kCryptoJobAsync,
kCryptoJobWebCrypto,
} = internalBinding('crypto');

const {
Expand Down Expand Up @@ -39,15 +39,15 @@ function validateKeyLength(length) {

function c20pCipher(mode, key, data, algorithm) {
return jobPromise(() => new ChaCha20Poly1305CipherJob(
kCryptoJobAsync,
kCryptoJobWebCrypto,
mode,
getCryptoKeyHandle(key),
data,
algorithm.iv,
algorithm.additionalData));
}

async function c20pGenerateKey(algorithm, extractable, keyUsages) {
function c20pGenerateKey(algorithm, extractable, keyUsages) {
const { name } = algorithm;

const checkUsages = ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'];
Expand All @@ -58,14 +58,18 @@ async function c20pGenerateKey(algorithm, extractable, keyUsages) {
`Unsupported key usage for a ${algorithm.name} key`,
'SyntaxError');
}
if (usagesSet.size === 0) {
throw lazyDOMException(
'Usages cannot be empty when creating a key.',
'SyntaxError');
}

const handle = await jobPromise(() => new SecretKeyGenJob(kCryptoJobAsync, 256));

return new InternalCryptoKey(
handle,
return jobPromise(() => new SecretKeyGenJob(
kCryptoJobWebCrypto,
256,
{ name },
getUsagesMask(usagesSet),
extractable);
extractable));
}

function c20pImportKey(
Expand Down
Loading
Loading