| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a553822 commit f4fbcca
9 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -101,6 +101,10 @@ Key Formats: | |||
| 101 | 101 | * `'raw-secret'` | |
| 102 | 102 | * `'raw-seed'` | |
| 103 | 103 | ||
| 104 | + Methods: | ||
| 105 | + | ||
| 106 | + * [`SubtleCrypto.supports()`][] | ||
| 107 | + | ||
| 104 | 108 | ## Secure Curves in the Web Cryptography API | |
| 105 | 109 | ||
| 106 | 110 | > Stability: 1.1 - Active development | |
@@ -387,6 +391,76 @@ async function digest(data, algorithm = 'SHA-512') { | |||
| 387 | 391 | } | |
| 388 | 392 | ``` | |
| 389 | 393 | ||
| 394 | + ### Checking for runtime algorithm support | ||
| 395 | + | ||
| 396 | + [`SubtleCrypto.supports()`][] allows feature detection in Web Crypto API, | ||
| 397 | + which can be used to detect whether a given algorithm identifier | ||
| 398 | + (including its parameters) is supported for the given operation. | ||
| 399 | + | ||
| 400 | + This example derives a key from a password using Argon2, if available, | ||
| 401 | + or PBKDF2, otherwise; and then encrypts and decrypts some text with it | ||
| 402 | + using AES-OCB, if available, and AES-GCM, otherwise. | ||
| 403 | + | ||
| 404 | + ```mjs | ||
| 405 | + const { SubtleCrypto, crypto } = globalThis; | ||
| 406 | + | ||
| 407 | + const password = 'correct horse battery staple'; | ||
| 408 | + const derivationAlg = | ||
| 409 | + SubtleCrypto.supports?.('importKey', 'Argon2id') ? | ||
| 410 | + 'Argon2id' : | ||
| 411 | + 'PBKDF2'; | ||
| 412 | + const encryptionAlg = | ||
| 413 | + SubtleCrypto.supports?.('importKey', 'AES-OCB') ? | ||
| 414 | + 'AES-OCB' : | ||
| 415 | + 'AES-GCM'; | ||
| 416 | + const passwordKey = await crypto.subtle.importKey( | ||
| 417 | + derivationAlg === 'Argon2id' ? 'raw-secret' : 'raw', | ||
| 418 | + new TextEncoder().encode(password), | ||
| 419 | + derivationAlg, | ||
| 420 | + false, | ||
| 421 | + ['deriveKey'], | ||
| 422 | + ); | ||
| 423 | + const nonce = crypto.getRandomValues(new Uint8Array(16)); | ||
| 424 | + const derivationParams = | ||
| 425 | + derivationAlg === 'Argon2id' ? | ||
| 426 | + { | ||
| 427 | + nonce, | ||
| 428 | + parallelism: 4, | ||
| 429 | + memory: 2 ** 21, | ||
| 430 | + passes: 1, | ||
| 431 | + } : | ||
| 432 | + { | ||
| 433 | + salt: nonce, | ||
| 434 | + iterations: 100_000, | ||
| 435 | + hash: 'SHA-256', | ||
| 436 | + }; | ||
| 437 | + const key = await crypto.subtle.deriveKey( | ||
| 438 | + { | ||
| 439 | + name: derivationAlg, | ||
| 440 | + ...derivationParams, | ||
| 441 | + }, | ||
| 442 | + passwordKey, | ||
| 443 | + { | ||
| 444 | + name: encryptionAlg, | ||
| 445 | + length: 256, | ||
| 446 | + }, | ||
| 447 | + false, | ||
| 448 | + ['encrypt', 'decrypt'], | ||
| 449 | + ); | ||
| 450 | + const plaintext = 'Hello, world!'; | ||
| 451 | + const iv = crypto.getRandomValues(new Uint8Array(16)); | ||
| 452 | + const encrypted = await crypto.subtle.encrypt( | ||
| 453 | + { name: encryptionAlg, iv }, | ||
| 454 | + key, | ||
| 455 | + new TextEncoder().encode(plaintext), | ||
| 456 | + ); | ||
| 457 | + const decrypted = new TextDecoder().decode(await crypto.subtle.decrypt( | ||
| 458 | + { name: encryptionAlg, iv }, | ||
| 459 | + key, | ||
| 460 | + encrypted, | ||
| 461 | + )); | ||
| 462 | + ``` | ||
| 463 | + | ||
| 390 | 464 | ## Algorithm matrix | |
| 391 | 465 | ||
| 392 | 466 | The table details the algorithms supported by the Node.js Web Crypto API | |
@@ -591,6 +665,27 @@ added: v15.0.0 | |||
| 591 | 665 | added: v15.0.0 | |
| 592 | 666 | --> | |
| 593 | 667 | ||
| 668 | + ### Static method: `SubtleCrypto.supports(operation, algorithm[, lengthOrAdditionalAlgorithm])` | ||
| 669 | + | ||
| 670 | + <!-- YAML | ||
| 671 | + added: REPLACEME | ||
| 672 | + --> | ||
| 673 | + | ||
| 674 | + > Stability: 1.1 - Active development | ||
| 675 | + | ||
| 676 | + <!--lint disable maximum-line-length remark-lint--> | ||
| 677 | + | ||
| 678 | + * `operation` {string} "encrypt", "decrypt", "sign", "verify", "digest", "generateKey", "deriveKey", "deriveBits", "importKey", "exportKey", "wrapKey", or "unwrapKey" | ||
| 679 | + * `algorithm` {string|Algorithm} | ||
| 680 | + * `lengthOrAdditionalAlgorithm` {null|number|string|Algorithm|undefined} Depending on the operation this is either ignored, the value of the length argument when operation is "deriveBits", the algorithm of key to be derived when operation is "deriveKey", the algorithm of key to be exported before wrapping when operation is "wrapKey", or the algorithm of key to be imported after unwrapping when operation is "unwrapKey". **Default:** `null` when operation is "deriveBits", `undefined` otherwise. | ||
| 681 | + * Returns: {boolean} Indicating whether the implementation supports the given operation | ||
| 682 | + | ||
| 683 | + <!--lint enable maximum-line-length remark-lint--> | ||
| 684 | + | ||
| 685 | + Allows feature detection in Web Crypto API, | ||
| 686 | + which can be used to detect whether a given algorithm identifier | ||
| 687 | + (including its parameters) is supported for the given operation. | ||
| 688 | + | ||
| 594 | 689 | ### `subtle.decrypt(algorithm, key, data)` | |
| 595 | 690 | ||
| 596 | 691 | <!-- YAML | |
@@ -1922,3 +2017,4 @@ The length (in bytes) of the random salt to use. | |||
| 1922 | 2017 | [RFC 4122]: https://www.rfc-editor.org/rfc/rfc4122.txt | |
| 1923 | 2018 | [Secure Curves in the Web Cryptography API]: #secure-curves-in-the-web-cryptography-api | |
| 1924 | 2019 | [Web Crypto API]: https://www.w3.org/TR/WebCryptoAPI/ | |
| 2020 | + [`SubtleCrypto.supports()`]: #static-method-subtlecryptosupportsoperation-algorithm-lengthoradditionalalgorithm | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -170,4 +170,5 @@ module.exports = { | |||
| 170 | 170 | hkdf, | |
| 171 | 171 | hkdfSync, | |
| 172 | 172 | hkdfDeriveBits, | |
| 173 | + validateHkdfDeriveBitsLength, | ||
| 173 | 174 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -128,4 +128,5 @@ module.exports = { | |||
| 128 | 128 | pbkdf2, | |
| 129 | 129 | pbkdf2Sync, | |
| 130 | 130 | pbkdf2DeriveBits, | |
| 131 | + validatePbkdf2DeriveBitsLength, | ||
| 131 | 132 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -47,6 +47,7 @@ const { | |||
| 47 | 47 | } = require('internal/crypto/util'); | |
| 48 | 48 | ||
| 49 | 49 | const { | |
| 50 | + emitExperimentalWarning, | ||
| 50 | 51 | kEnumerableProperty, | |
| 51 | 52 | lazyDOMException, | |
| 52 | 53 | } = require('internal/util'); | |
@@ -1026,7 +1027,147 @@ class SubtleCrypto { | |||
| 1026 | 1027 | constructor() { | |
| 1027 | 1028 | throw new ERR_ILLEGAL_CONSTRUCTOR(); | |
| 1028 | 1029 | } | |
| 1030 | + | ||
| 1031 | + // Implements https://wicg.github.io/webcrypto-modern-algos/#SubtleCrypto-method-supports | ||
| 1032 | + static supports(operation, algorithm, lengthOrAdditionalAlgorithm = null) { | ||
| 1033 | + emitExperimentalWarning('The supports Web Crypto API method'); | ||
| 1034 | + if (this !== SubtleCrypto) throw new ERR_INVALID_THIS('SubtleCrypto constructor'); | ||
| 1035 | + webidl ??= require('internal/crypto/webidl'); | ||
| 1036 | + const prefix = "Failed to execute 'supports' on 'SubtleCrypto'"; | ||
| 1037 | + webidl.requiredArguments(arguments.length, 2, { prefix }); | ||
| 1038 | + | ||
| 1039 | + operation = webidl.converters.DOMString(operation, { | ||
| 1040 | + prefix, | ||
| 1041 | + context: '1st argument', | ||
| 1042 | + }); | ||
| 1043 | + algorithm = webidl.converters.AlgorithmIdentifier(algorithm, { | ||
| 1044 | + prefix, | ||
| 1045 | + context: '2nd argument', | ||
| 1046 | + }); | ||
| 1047 | + | ||
| 1048 | + switch (operation) { | ||
| 1049 | + case 'encrypt': | ||
| 1050 | + case 'decrypt': | ||
| 1051 | + case 'sign': | ||
| 1052 | + case 'verify': | ||
| 1053 | + case 'digest': | ||
| 1054 | + case 'generateKey': | ||
| 1055 | + case 'deriveKey': | ||
| 1056 | + case 'deriveBits': | ||
| 1057 | + case 'importKey': | ||
| 1058 | + case 'exportKey': | ||
| 1059 | + case 'wrapKey': | ||
| 1060 | + case 'unwrapKey': | ||
| 1061 | + break; | ||
| 1062 | + default: | ||
| 1063 | + return false; | ||
| 1064 | + } | ||
| 1065 | + | ||
| 1066 | + let length; | ||
| 1067 | + let additionalAlgorithm; | ||
| 1068 | + if (operation === 'deriveKey') { | ||
| 1069 | + additionalAlgorithm = webidl.converters.AlgorithmIdentifier(lengthOrAdditionalAlgorithm, { | ||
| 1070 | + prefix, | ||
| 1071 | + context: '3rd argument', | ||
| 1072 | + }); | ||
| 1073 | + | ||
| 1074 | + if (!check('importKey', additionalAlgorithm)) { | ||
| 1075 | + return false; | ||
| 1076 | + } | ||
| 1077 | + | ||
| 1078 | + try { | ||
| 1079 | + length = getKeyLength(normalizeAlgorithm(additionalAlgorithm, 'get key length')); | ||
| 1080 | + } catch { | ||
| 1081 | + return false; | ||
| 1082 | + } | ||
| 1083 | + | ||
| 1084 | + operation = 'deriveBits'; | ||
| 1085 | + } else if (operation === 'wrapKey') { | ||
| 1086 | + additionalAlgorithm = webidl.converters.AlgorithmIdentifier(lengthOrAdditionalAlgorithm, { | ||
| 1087 | + prefix, | ||
| 1088 | + context: '3rd argument', | ||
| 1089 | + }); | ||
| 1090 | + | ||
| 1091 | + if (!check('exportKey', additionalAlgorithm)) { | ||
| 1092 | + return false; | ||
| 1093 | + } | ||
| 1094 | + } else if (operation === 'unwrapKey') { | ||
| 1095 | + additionalAlgorithm = webidl.converters.AlgorithmIdentifier(lengthOrAdditionalAlgorithm, { | ||
| 1096 | + prefix, | ||
| 1097 | + context: '3rd argument', | ||
| 1098 | + }); | ||
| 1099 | + | ||
| 1100 | + if (!check('importKey', additionalAlgorithm)) { | ||
| 1101 | + return false; | ||
| 1102 | + } | ||
| 1103 | + } else if (operation === 'deriveBits') { | ||
| 1104 | + length = lengthOrAdditionalAlgorithm; | ||
| 1105 | + if (length !== null) { | ||
| 1106 | + length = webidl.converters['unsigned long'](length, { | ||
| 1107 | + prefix, | ||
| 1108 | + context: '3rd argument', | ||
| 1109 | + }); | ||
| 1110 | + } | ||
| 1111 | + } | ||
| 1112 | + | ||
| 1113 | + return check(operation, algorithm, length); | ||
| 1114 | + } | ||
| 1029 | 1115 | } | |
| 1116 | + | ||
| 1117 | + function check(op, alg, length) { | ||
| 1118 | + let normalizedAlgorithm; | ||
| 1119 | + try { | ||
| 1120 | + normalizedAlgorithm = normalizeAlgorithm(alg, op); | ||
| 1121 | + } catch { | ||
| 1122 | + if (op === 'wrapKey') { | ||
| 1123 | + return check('encrypt', alg); | ||
| 1124 | + } | ||
| 1125 | + | ||
| 1126 | + if (op === 'unwrapKey') { | ||
| 1127 | + return check('decrypt', alg); | ||
| 1128 | + } | ||
| 1129 | + | ||
| 1130 | + return false; | ||
| 1131 | + } | ||
| 1132 | + | ||
| 1133 | + switch (op) { | ||
| 1134 | + case 'encrypt': | ||
| 1135 | + case 'decrypt': | ||
| 1136 | + case 'sign': | ||
| 1137 | + case 'verify': | ||
| 1138 | + case 'digest': | ||
| 1139 | + case 'generateKey': | ||
| 1140 | + case 'importKey': | ||
| 1141 | + case 'exportKey': | ||
| 1142 | + case 'wrapKey': | ||
| 1143 | + case 'unwrapKey': | ||
| 1144 | + return true; | ||
| 1145 | + case 'deriveBits': { | ||
| 1146 | + if (normalizedAlgorithm.name === 'HKDF') { | ||
| 1147 | + try { | ||
| 1148 | + require('internal/crypto/hkdf').validateHkdfDeriveBitsLength(length); | ||
| 1149 | + } catch { | ||
| 1150 | + return false; | ||
| 1151 | + } | ||
| 1152 | + } | ||
| 1153 | + | ||
| 1154 | + if (normalizedAlgorithm.name === 'PBKDF2') { | ||
| 1155 | + try { | ||
| 1156 | + require('internal/crypto/pbkdf2').validatePbkdf2DeriveBitsLength(length); | ||
| 1157 | + } catch { | ||
| 1158 | + return false; | ||
| 1159 | + } | ||
| 1160 | + } | ||
| 1161 | + | ||
| 1162 | + return true; | ||
| 1163 | + } | ||
| 1164 | + default: { | ||
| 1165 | + const assert = require('internal/assert'); | ||
| 1166 | + assert.fail('Unreachable code'); | ||
| 1167 | + } | ||
| 1168 | + } | ||
| 1169 | + } | ||
| 1170 | + | ||
| 1030 | 1171 | const subtle = ReflectConstruct(function() {}, [], SubtleCrypto); | |
| 1031 | 1172 | ||
| 1032 | 1173 | class Crypto { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments