(key, algorithm, extractable, usages, kind)
| 89 | // Is key a CryptoKey object with correct algorithm, extractable, and usages? |
| 90 | // Is it a secret, private, or public kind of key? |
| 91 | function assert_goodCryptoKey(key, algorithm, extractable, usages, kind) { |
| 92 | if (typeof algorithm === "string") { |
| 93 | algorithm = { name: algorithm }; |
| 94 | } |
| 95 | |
| 96 | var correctUsages = []; |
| 97 | |
| 98 | var registeredAlgorithmName; |
| 99 | registeredAlgorithmNames.forEach(function(name) { |
| 100 | if (name.toUpperCase() === algorithm.name.toUpperCase()) { |
| 101 | registeredAlgorithmName = name; |
| 102 | } |
| 103 | }); |
| 104 | |
| 105 | assert_equals(key.constructor, CryptoKey, "Is a CryptoKey"); |
| 106 | assert_equals(key.type, kind, "Is a " + kind + " key"); |
| 107 | assert_equals(key.extractable, extractable, "Extractability is correct"); |
| 108 | |
| 109 | assert_equals(key.algorithm.name, registeredAlgorithmName, "Correct algorithm name"); |
| 110 | if (key.algorithm.name.toUpperCase() === "HMAC" && algorithm.length === undefined) { |
| 111 | switch (key.algorithm.hash.name.toUpperCase()) { |
| 112 | case 'SHA-1': |
| 113 | case 'SHA-256': |
| 114 | assert_equals(key.algorithm.length, 512, "Correct length"); |
| 115 | break; |
| 116 | case 'SHA-384': |
| 117 | case 'SHA-512': |
| 118 | assert_equals(key.algorithm.length, 1024, "Correct length"); |
| 119 | break; |
| 120 | default: |
| 121 | assert_unreached("Unrecognized hash"); |
| 122 | } |
| 123 | } else if (key.algorithm.name.toUpperCase().startsWith("KMAC") && algorithm.length === undefined) { |
| 124 | switch (key.algorithm.name.toUpperCase()) { |
| 125 | case 'KMAC128': |
| 126 | assert_equals(key.algorithm.length, 128, "Correct length"); |
| 127 | break; |
| 128 | case 'KMAC256': |
| 129 | assert_equals(key.algorithm.length, 256, "Correct length"); |
| 130 | break; |
| 131 | } |
| 132 | } else { |
| 133 | assert_equals(key.algorithm.length, algorithm.length, "Correct length"); |
| 134 | } |
| 135 | if (["HMAC", "RSASSA-PKCS1-v1_5", "RSA-PSS"].includes(registeredAlgorithmName)) { |
| 136 | assert_equals(key.algorithm.hash.name.toUpperCase(), algorithm.hash.toUpperCase(), "Correct hash function"); |
| 137 | } |
| 138 | |
| 139 | if (/^(?:Ed|X)(?:25519|448)$/.test(key.algorithm.name)) { |
| 140 | assert_false('namedCurve' in key.algorithm, "Does not have a namedCurve property"); |
| 141 | } |
| 142 | |
| 143 | // usages is expected to be provided for a key pair, but we are checking |
| 144 | // only a single key. The publicKey and privateKey portions of a key pair |
| 145 | // recognize only some of the usages appropriate for a key pair. |
| 146 | if (key.type === "public") { |
| 147 | ["encrypt", "verify", "wrapKey", "encapsulateBits", "encapsulateKey"].forEach(function(usage) { |
| 148 | if (usages.includes(usage)) { |
no test coverage detected
searching dependent graphs…