Skip to content

Commit

Permalink
lib: refactor SubtleCrypto experimental warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Aug 28, 2024
1 parent 4a0ec20 commit 0faf147
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
5 changes: 0 additions & 5 deletions lib/internal/crypto/cfrg.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const {
} = require('internal/crypto/util');

const {
emitExperimentalWarning,
lazyDOMException,
promisify,
} = require('internal/util');
Expand Down Expand Up @@ -105,7 +104,6 @@ function createCFRGRawKey(name, keyData, isPublic) {

async function cfrgGenerateKey(algorithm, extractable, keyUsages) {
const { name } = algorithm;
emitExperimentalWarning(`The ${name} Web Crypto API algorithm`);

const usageSet = new SafeSet(keyUsages);
switch (name) {
Expand Down Expand Up @@ -187,7 +185,6 @@ async function cfrgGenerateKey(algorithm, extractable, keyUsages) {
}

function cfrgExportKey(key, format) {
emitExperimentalWarning(`The ${key.algorithm.name} Web Crypto API algorithm`);
return jobPromise(() => new ECKeyExportJob(
kCryptoJobAsync,
format,
Expand All @@ -202,7 +199,6 @@ async function cfrgImportKey(
keyUsages) {

const { name } = algorithm;
emitExperimentalWarning(`The ${name} Web Crypto API algorithm`);
let keyObject;
const usagesSet = new SafeSet(keyUsages);
switch (format) {
Expand Down Expand Up @@ -319,7 +315,6 @@ async function cfrgImportKey(
}

function eddsaSignVerify(key, data, { name, context }, signature) {
emitExperimentalWarning(`The ${name} Web Crypto API algorithm`);
const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
const type = mode === kSignJobModeSign ? 'private' : 'public';

Expand Down
65 changes: 41 additions & 24 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const {
DataViewPrototypeGetByteOffset,
FunctionPrototypeBind,
Number,
ObjectDefineProperty,
ObjectEntries,
ObjectKeys,
ObjectPrototypeHasOwnProperty,
Promise,
Expand Down Expand Up @@ -63,6 +65,7 @@ const { Buffer } = require('buffer');

const {
cachedResult,
emitExperimentalWarning,
filterDuplicateStrings,
lazyDOMException,
} = require('internal/util');
Expand Down Expand Up @@ -168,15 +171,6 @@ const kNamedCurveAliases = {

const kAesKeyLengths = [128, 192, 256];

// These are the only hash algorithms we currently support via
// the Web Crypto API.
const kHashTypes = [
'SHA-1',
'SHA-256',
'SHA-384',
'SHA-512',
];

const kSupportedAlgorithms = {
'digest': {
'SHA-1': null,
Expand All @@ -195,26 +189,18 @@ const kSupportedAlgorithms = {
'AES-GCM': 'AesKeyGenParams',
'AES-KW': 'AesKeyGenParams',
'HMAC': 'HmacKeyGenParams',
'X25519': null,
'Ed25519': null,
'X448': null,
'Ed448': null,
},
'sign': {
'RSASSA-PKCS1-v1_5': null,
'RSA-PSS': 'RsaPssParams',
'ECDSA': 'EcdsaParams',
'HMAC': null,
'Ed25519': null,
'Ed448': 'Ed448Params',
},
'verify': {
'RSASSA-PKCS1-v1_5': null,
'RSA-PSS': 'RsaPssParams',
'ECDSA': 'EcdsaParams',
'HMAC': null,
'Ed25519': null,
'Ed448': 'Ed448Params',
},
'importKey': {
'RSASSA-PKCS1-v1_5': 'RsaHashedImportParams',
Expand All @@ -229,17 +215,11 @@ const kSupportedAlgorithms = {
'AES-CBC': null,
'AES-GCM': null,
'AES-KW': null,
'Ed25519': null,
'X25519': null,
'Ed448': null,
'X448': null,
},
'deriveBits': {
'HKDF': 'HkdfParams',
'PBKDF2': 'Pbkdf2Params',
'ECDH': 'EcdhKeyDeriveParams',
'X25519': 'EcdhKeyDeriveParams',
'X448': 'EcdhKeyDeriveParams',
},
'encrypt': {
'RSA-OAEP': 'RsaOaepParams',
Expand Down Expand Up @@ -270,6 +250,44 @@ const kSupportedAlgorithms = {
},
};

const experimentalAlgorithms = {
'X25519': {
generateKey: null,
importKey: null,
deriveBits: 'EcdhKeyDeriveParams',
},
'Ed25519': {
generateKey: null,
sign: null,
verify: null,
importKey: null,
},
'X448': {
generateKey: null,
importKey: null,
deriveBits: 'EcdhKeyDeriveParams',
},
'Ed448': {
generateKey: null,
sign: 'Ed448Params',
verify: 'Ed448Params',
importKey: null,
},
};

for (const { 0: name, 1: ops } of ObjectEntries(experimentalAlgorithms)) {
for (const { 0: op, 1: dict } of ObjectEntries(ops)) {
ObjectDefineProperty(kSupportedAlgorithms[op], name, {
get() {
emitExperimentalWarning(`The ${name} Web Crypto API algorithm`);
return dict;
},
__proto__: null,
enumerable: true,
});
}
}

const simpleAlgorithmDictionaries = {
AesGcmParams: { iv: 'BufferSource', additionalData: 'BufferSource' },
RsaHashedKeyGenParams: { hash: 'HashAlgorithmIdentifier' },
Expand Down Expand Up @@ -594,7 +612,6 @@ module.exports = {
setEngine,
toBuf,

kHashTypes,
kNamedCurveAliases,
kAesKeyLengths,
normalizeAlgorithm,
Expand Down

0 comments on commit 0faf147

Please sign in to comment.