OLD | NEW |
(Empty) | |
| 1 if (self.importScripts) { |
| 2 importScripts('../../resources/js-test.js'); |
| 3 importScripts('common.js'); |
| 4 } |
| 5 |
| 6 function shouldEvaluateAsSilent(expressionToEval, expectedResult) |
| 7 { |
| 8 var result = eval(expressionToEval); |
| 9 if (result !== expectedResult) { |
| 10 testFailed(expressionToEval + " evaluated to " + result + " instead of "
+ expectedResult); |
| 11 } |
| 12 } |
| 13 |
| 14 function doPostMessage(data) |
| 15 { |
| 16 if (isWorker()) |
| 17 self.postMessage(data); |
| 18 else |
| 19 self.postMessage(data, '*'); |
| 20 } |
| 21 |
| 22 function notifySuccess() |
| 23 { |
| 24 doPostMessage("TEST_FINISHED"); |
| 25 } |
| 26 |
| 27 function notifyFailure(details) |
| 28 { |
| 29 doPostMessage("FAIL:" + details); |
| 30 } |
| 31 |
| 32 function testGenerateRsaKey() |
| 33 { |
| 34 var extractable = false; |
| 35 var usages = ['encrypt', 'decrypt']; |
| 36 // Note that the modulus length is unsually small, in order to speed up the
test. |
| 37 var algorithm = {name: "RSAES-PKCS1-v1_5", modulusLength: 256, publicExponen
t: hexStringToUint8Array("010001")}; |
| 38 |
| 39 return crypto.subtle.generateKey(algorithm, extractable, usages).then(functi
on(result) { |
| 40 publicKey = result.publicKey; |
| 41 privateKey = result.privateKey; |
| 42 |
| 43 shouldEvaluateAsSilent("publicKey.type", "public"); |
| 44 shouldEvaluateAsSilent("publicKey.extractable", true); |
| 45 shouldEvaluateAsSilent("publicKey.algorithm.name", algorithm.name); |
| 46 shouldEvaluateAsSilent("publicKey.algorithm.modulusLength", algorithm.mo
dulusLength); |
| 47 shouldEvaluateAsSilent("bytesToHexString(publicKey.algorithm.publicExpon
ent)", "010001"); |
| 48 |
| 49 shouldEvaluateAsSilent("privateKey.type", "private"); |
| 50 shouldEvaluateAsSilent("privateKey.extractable", false); |
| 51 shouldEvaluateAsSilent("privateKey.algorithm.name", algorithm.name); |
| 52 shouldEvaluateAsSilent("privateKey.algorithm.modulusLength", algorithm.m
odulusLength); |
| 53 shouldEvaluateAsSilent("bytesToHexString(privateKey.algorithm.publicExpo
nent)", "010001"); |
| 54 }); |
| 55 } |
| 56 |
| 57 // Very similar to "hmac-sign-verify.html". |
| 58 function testHmac() |
| 59 { |
| 60 var importAlgorithm = {name: 'HMAC', hash: {name: "SHA-256"}}; |
| 61 var algorithm = {name: 'HMAC'}; |
| 62 |
| 63 var key = null; |
| 64 |
| 65 var testCase = { |
| 66 hash: "SHA-256", |
| 67 key: "9779d9120642797f1747025d5b22b7ac607cab08e1758f2f3a46c8be1e25c53b8c6a
8f58ffefa176", |
| 68 message: "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc28
8baf4a92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92d1b0
ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f22a003b8ab8de5
4f6ded0e3ab9245fa79568451dfa258e", |
| 69 mac: "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b" |
| 70 }; |
| 71 |
| 72 var keyData = hexStringToUint8Array(testCase.key); |
| 73 var usages = ['sign', 'verify']; |
| 74 var extractable = true; |
| 75 |
| 76 // (1) Import the key |
| 77 return crypto.subtle.importKey('raw', keyData, importAlgorithm, extractable,
usages).then(function(result) { |
| 78 key = result; |
| 79 |
| 80 // shouldBe() can only resolve variables in global context. |
| 81 tmpKey = key; |
| 82 shouldEvaluateAsSilent("tmpKey.type", "secret"); |
| 83 shouldEvaluateAsSilent("tmpKey.extractable", true); |
| 84 shouldEvaluateAsSilent("tmpKey.algorithm.name", "HMAC"); |
| 85 shouldEvaluateAsSilent("tmpKey.algorithm.hash.name", testCase.hash); |
| 86 shouldEvaluateAsSilent("tmpKey.algorithm.length", keyData.length * 8); |
| 87 shouldEvaluateAsSilent("tmpKey.usages.join(',')", "sign,verify"); |
| 88 |
| 89 // (2) Sign. |
| 90 return crypto.subtle.sign(algorithm, key, hexStringToUint8Array(testCase
.message)); |
| 91 }).then(function(result) { |
| 92 mac = result; |
| 93 shouldEvaluateAsSilent("bytesToHexString(mac)", testCase.mac); |
| 94 |
| 95 // (3) Verify |
| 96 return crypto.subtle.verify(algorithm, key, hexStringToUint8Array(testCa
se.mac), hexStringToUint8Array(testCase.message)); |
| 97 }).then(function(result) { |
| 98 verifyResult = result; |
| 99 shouldEvaluateAsSilent("verifyResult", true); |
| 100 |
| 101 // (4) Verify truncated mac (by stripping 1 byte off of it). |
| 102 var expectedMac = hexStringToUint8Array(testCase.mac); |
| 103 return crypto.subtle.verify(algorithm, key, expectedMac.subarray(0, expe
ctedMac.byteLength - 1), hexStringToUint8Array(testCase.message)); |
| 104 }).then(function(result) { |
| 105 verifyResult = result; |
| 106 shouldEvaluateAsSilent("verifyResult", false); |
| 107 |
| 108 return crypto.subtle.exportKey('raw', key); |
| 109 }).then(function(result) { |
| 110 exportedKeyData = result; |
| 111 shouldEvaluateAsSilent("bytesToHexString(exportedKeyData)", testCase.key
); |
| 112 }); |
| 113 } |
| 114 |
| 115 // Very similar to aes-gcm-encrypt-decrypt.hml |
| 116 function testAesGcm() |
| 117 { |
| 118 var testCase = { |
| 119 "key": "ae7972c025d7f2ca3dd37dcc3d41c506671765087c6b61b8", |
| 120 "iv": "984c1379e6ba961c828d792d", |
| 121 "plainText": "d30b02c343487105219d6fa080acc743", |
| 122 "cipherText": "c4489fa64a6edf80e7e6a3b8855bc37c", |
| 123 "additionalData": "edd8f630f9bbc31b0acf122998f15589d6e6e3e1a3ec89e0c6a6ece
751610ebbf57fdfb9d82028ff1d9faebe37a268c1", |
| 124 "authenticationTag": "772ee7de0f91a981c36c93a35c88" |
| 125 }; |
| 126 |
| 127 var key = null; |
| 128 var keyData = hexStringToUint8Array(testCase.key); |
| 129 var iv = hexStringToUint8Array(testCase.iv); |
| 130 var additionalData = hexStringToUint8Array(testCase.additionalData); |
| 131 var tag = hexStringToUint8Array(testCase.authenticationTag); |
| 132 var usages = ['encrypt', 'decrypt']; |
| 133 var extractable = false; |
| 134 |
| 135 var tagLengthBits = tag.byteLength * 8; |
| 136 |
| 137 var algorithm = {name: 'aes-gcm', iv: iv, additionalData: additionalData, ta
gLength: tagLengthBits}; |
| 138 |
| 139 // (1) Import the key |
| 140 return crypto.subtle.importKey('raw', keyData, algorithm, extractable, usage
s).then(function(result) { |
| 141 key = result; |
| 142 |
| 143 // shouldBe() can only resolve variables in global context. |
| 144 tmpKey = key; |
| 145 shouldEvaluateAsSilent("tmpKey.type", "secret"); |
| 146 shouldEvaluateAsSilent("tmpKey.extractable", false); |
| 147 shouldEvaluateAsSilent("tmpKey.algorithm.name", "AES-GCM"); |
| 148 shouldEvaluateAsSilent("tmpKey.usages.join(',')", "encrypt,decrypt"); |
| 149 |
| 150 // (2) Encrypt. |
| 151 return crypto.subtle.encrypt(algorithm, key, hexStringToUint8Array(testC
ase.plainText)); |
| 152 }).then(function(result) { |
| 153 cipherText = result; |
| 154 shouldEvaluateAsSilent("bytesToHexString(cipherText)", testCase.cipherTe
xt + testCase.authenticationTag); |
| 155 |
| 156 // (3) Decrypt |
| 157 return crypto.subtle.decrypt(algorithm, key, hexStringToUint8Array(testC
ase.cipherText + testCase.authenticationTag)); |
| 158 }).then(function(result) { |
| 159 plainText = result; |
| 160 shouldEvaluateAsSilent("bytesToHexString(plainText)", testCase.plainText
); |
| 161 }); |
| 162 } |
| 163 |
| 164 Promise.all([ |
| 165 testHmac(), |
| 166 testGenerateRsaKey(), |
| 167 testAesGcm(), |
| 168 testHmac(), |
| 169 testAesGcm(), |
| 170 ]).then(notifySuccess, notifyFailure); |
OLD | NEW |