Skip to content

Commit 5710d4e

Browse files
committed
crypto: improve accuracy of SubtleCrypto.supports
Signed-off-by: Filip Skokan <panva.ip@gmail.com>
1 parent 25f80fb commit 5710d4e

3 files changed

Lines changed: 57 additions & 17 deletions

File tree

lib/internal/crypto/webcrypto.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,11 @@ class SubtleCrypto {
16581658
}
16591659
}
16601660

1661-
return check(operation, algorithm, length);
1661+
try {
1662+
return check(operation, algorithm, length);
1663+
} catch {
1664+
return false;
1665+
}
16621666
}
16631667
}
16641668

@@ -1701,25 +1705,35 @@ function check(op, alg, length) {
17011705
return true;
17021706
case 'deriveBits': {
17031707
if (normalizedAlgorithm.name === 'HKDF') {
1704-
try {
1705-
require('internal/crypto/hkdf').validateHkdfDeriveBitsLength(length);
1706-
} catch {
1707-
return false;
1708-
}
1708+
require('internal/crypto/hkdf').validateHkdfDeriveBitsLength(length);
17091709
}
17101710

17111711
if (normalizedAlgorithm.name === 'PBKDF2') {
1712-
try {
1713-
require('internal/crypto/pbkdf2').validatePbkdf2DeriveBitsLength(length);
1714-
} catch {
1715-
return false;
1716-
}
1712+
require('internal/crypto/pbkdf2').validatePbkdf2DeriveBitsLength(length);
17171713
}
17181714

17191715
if (StringPrototypeStartsWith(normalizedAlgorithm.name, 'Argon2')) {
1720-
try {
1721-
require('internal/crypto/argon2').validateArgon2DeriveBitsLength(length);
1722-
} catch {
1716+
require('internal/crypto/argon2').validateArgon2DeriveBitsLength(length);
1717+
}
1718+
1719+
if (normalizedAlgorithm.name === 'X25519' && length > 256) {
1720+
return false;
1721+
}
1722+
1723+
if (normalizedAlgorithm.name === 'X448' && length > 448) {
1724+
return false;
1725+
}
1726+
1727+
if (normalizedAlgorithm.name === 'ECDH') {
1728+
const namedCurve = getCryptoKeyAlgorithm(normalizedAlgorithm.public).namedCurve;
1729+
const maxLength = {
1730+
'__proto__': null,
1731+
'P-256': 256,
1732+
'P-384': 384,
1733+
'P-521': 528,
1734+
}[namedCurve];
1735+
1736+
if (length > maxLength) {
17231737
return false;
17241738
}
17251739
}

test/fixtures/webcrypto/supports-level-2.mjs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,30 @@ export const vectors = {
103103
[true,
104104
{ name: 'X25519', public: X25519.publicKey },
105105
{ name: 'AES-CBC', length: 128 }],
106-
[true,
106+
[false,
107107
{ name: 'X25519', public: X25519.publicKey },
108108
{ name: 'HMAC', hash: 'SHA-256' }],
109+
[true,
110+
{ name: 'X25519', public: X25519.publicKey },
111+
{ name: 'HMAC', hash: 'SHA-256', length: 256 }],
112+
[false,
113+
{ name: 'X25519', public: X25519.publicKey },
114+
{ name: 'HMAC', hash: 'SHA-256', length: 257 }],
109115
[true,
110116
{ name: 'X25519', public: X25519.publicKey },
111117
'HKDF'],
112118
[true,
113119
{ name: 'ECDH', public: ECDH.publicKey },
114120
{ name: 'AES-CBC', length: 128 }],
115-
[true,
121+
[false,
116122
{ name: 'ECDH', public: ECDH.publicKey },
117123
{ name: 'HMAC', hash: 'SHA-256' }],
124+
[true,
125+
{ name: 'ECDH', public: ECDH.publicKey },
126+
{ name: 'HMAC', hash: 'SHA-256', length: 256 }],
127+
[false,
128+
{ name: 'ECDH', public: ECDH.publicKey },
129+
{ name: 'HMAC', hash: 'SHA-256', length: 257 }],
118130
[true,
119131
{ name: 'ECDH', public: ECDH.publicKey },
120132
'HKDF'],
@@ -143,10 +155,16 @@ export const vectors = {
143155

144156
[true,
145157
{ name: 'ECDH', public: ECDH.publicKey }],
158+
[true,
159+
{ name: 'ECDH', public: ECDH.publicKey }, 256],
160+
[false,
161+
{ name: 'ECDH', public: ECDH.publicKey }, 257],
146162
[false, { name: 'ECDH', public: ECDH.privateKey }],
147163
[false, 'ECDH'],
148164

149165
[true, { name: 'X25519', public: X25519.publicKey }],
166+
[true, { name: 'X25519', public: X25519.publicKey }, 256],
167+
[false, { name: 'X25519', public: X25519.publicKey }, 257],
150168
[false, { name: 'X25519', public: X25519.privateKey }],
151169
[false, 'X25519'],
152170
],

test/fixtures/webcrypto/supports-secure-curves.mjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,23 @@ export const vectors = {
2828
[!boringSSL,
2929
{ name: 'X448', public: X448?.publicKey },
3030
{ name: 'AES-CBC', length: 128 }],
31-
[!boringSSL,
31+
[false,
3232
{ name: 'X448', public: X448?.publicKey },
3333
{ name: 'HMAC', hash: 'SHA-256' }],
34+
[!boringSSL,
35+
{ name: 'X448', public: X448?.publicKey },
36+
{ name: 'HMAC', hash: 'SHA-256', length: 448 }],
37+
[false,
38+
{ name: 'X448', public: X448?.publicKey },
39+
{ name: 'HMAC', hash: 'SHA-256', length: 449 }],
3440
[!boringSSL,
3541
{ name: 'X448', public: X448?.publicKey },
3642
'HKDF'],
3743
],
3844
'deriveBits': [
3945
[!boringSSL, { name: 'X448', public: X448?.publicKey }],
46+
[!boringSSL, { name: 'X448', public: X448?.publicKey }, 448],
47+
[false, { name: 'X448', public: X448?.publicKey }, 449],
4048
[false, { name: 'X448', public: X25519.publicKey }],
4149
[false, { name: 'X448', public: X448?.privateKey }],
4250
[false, 'X448'],

0 commit comments

Comments
 (0)