From 4675cdf3f23b5bcb04d62e2d00b7c75506908b06 Mon Sep 17 00:00:00 2001 From: lilia Date: Thu, 23 Oct 2014 21:30:36 -0700 Subject: [PATCH] Webcrypto won't go down without a fight Turns out that assigning a new object to window.crypto.subtle is not so easy. That's probably a good thing. --- js/crypto.js | 14 +++++++------- js/test.js | 8 ++++---- js/webcrypto.js | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/js/crypto.js b/js/crypto.js index 445bcaa2..0e3b3967 100644 --- a/js/crypto.js +++ b/js/crypto.js @@ -60,7 +60,7 @@ window.textsecure.crypto = function() { } function HmacSHA256(key, input) { - return window.crypto.subtle.sign({name: "HMAC", hash: "SHA-256"}, key, input); + return window.textsecure.subtle.sign({name: "HMAC", hash: "SHA-256"}, key, input); } testing_only.privToPub = function(privKey, isIdentity) { @@ -687,7 +687,7 @@ window.textsecure.crypto = function() { return verifyMAC(macInput.buffer, keys[1], mac).then(function() { var counter = intToArrayBuffer(message.counter); - return window.crypto.subtle.decrypt({name: "AES-CTR", counter: counter}, keys[0], toArrayBuffer(message.ciphertext)) + return window.textsecure.subtle.decrypt({name: "AES-CTR", counter: counter}, keys[0], toArrayBuffer(message.ciphertext)) .then(function(paddedPlaintext) { paddedPlaintext = new Uint8Array(paddedPlaintext); @@ -740,7 +740,7 @@ window.textsecure.crypto = function() { var mac = decodedMessage.slice(decodedMessage.byteLength - 10, decodedMessage.byteLength); return verifyMAC(ivAndCiphertext, mac_key, mac).then(function() { - return window.crypto.subtle.decrypt({name: "AES-CBC", iv: iv}, aes_key, ciphertext); + return window.textsecure.subtle.decrypt({name: "AES-CBC", iv: iv}, aes_key, ciphertext); }); }; @@ -754,7 +754,7 @@ window.textsecure.crypto = function() { var mac = encryptedBin.slice(encryptedBin.byteLength - 32, encryptedBin.byteLength); return verifyMAC(ivAndCiphertext, mac_key, mac).then(function() { - return window.crypto.subtle.decrypt({name: "AES-CBC", iv: iv}, aes_key, ciphertext); + return window.textsecure.subtle.decrypt({name: "AES-CBC", iv: iv}, aes_key, ciphertext); }); }; @@ -762,7 +762,7 @@ window.textsecure.crypto = function() { var aes_key = keys.slice(0, 32); var mac_key = keys.slice(32, 64); - return window.crypto.subtle.encrypt({name: "AES-CBC", iv: iv}, aes_key, plaintext).then(function(ciphertext) { + return window.textsecure.subtle.encrypt({name: "AES-CBC", iv: iv}, aes_key, plaintext).then(function(ciphertext) { var ivAndCiphertext = new Uint8Array(16 + ciphertext.byteLength); ivAndCiphertext.set(iv); ivAndCiphertext.set(ciphertext, 16); @@ -817,7 +817,7 @@ window.textsecure.crypto = function() { msg.previousCounter = session.currentRatchet.previousCounter; var counter = intToArrayBuffer(chain.chainKey.counter); - return window.crypto.subtle.encrypt({name: "AES-CTR", counter: counter}, keys[0], paddedPlaintext.buffer).then(function(ciphertext) { + return window.textsecure.subtle.encrypt({name: "AES-CTR", counter: counter}, keys[0], paddedPlaintext.buffer).then(function(ciphertext) { msg.ciphertext = ciphertext; var encodedMsg = toArrayBuffer(msg.encode()); @@ -956,7 +956,7 @@ window.textsecure.crypto = function() { var ciphertext = message.slice(16 + 1, message.length - 32); return verifyMAC(ivAndCiphertext, ecRes[1], mac).then(function() { - window.crypto.subtle.decrypt({name: "AES-CBC", iv: iv}, ecRes[0], ciphertext).then(function(plaintext) { + window.textsecure.subtle.decrypt({name: "AES-CBC", iv: iv}, ecRes[0], ciphertext).then(function(plaintext) { var identityKeyMsg = textsecure.protobuf.IdentityKey.decode(plaintext); privToPub(toArrayBuffer(identityKeyMsg.identityKey)).then(function(identityKeyPair) { diff --git a/js/test.js b/js/test.js index a3820637..889ccf00 100644 --- a/js/test.js +++ b/js/test.js @@ -35,7 +35,7 @@ describe("Cryptographic primitives", function() { var counter = hexToArrayBuffer('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'); var plaintext = hexToArrayBuffer('6bc1bee22e409f96e93d7e117393172a'); var ciphertext = hexToArrayBuffer('874d6191b620e3261bef6864990db6ce'); - return window.crypto.subtle.encrypt({name: "AES-CTR", counter: counter}, key, plaintext).then(function(result) { + return window.textsecure.subtle.encrypt({name: "AES-CTR", counter: counter}, key, plaintext).then(function(result) { assert.strictEqual(getString(result) +"", getString(ciphertext)); }).then(done).catch(done); }); @@ -47,7 +47,7 @@ describe("Cryptographic primitives", function() { var counter = hexToArrayBuffer('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'); var plaintext = hexToArrayBuffer('6bc1bee22e409f96e93d7e117393172a'); var ciphertext = hexToArrayBuffer('874d6191b620e3261bef6864990db6ce'); - return window.crypto.subtle.decrypt({name: "AES-CTR", counter: counter}, key, ciphertext).then(function(result) { + return window.textsecure.subtle.decrypt({name: "AES-CTR", counter: counter}, key, ciphertext).then(function(result) { assert.strictEqual(getString(result), getString(plaintext)); }).then(done).catch(done); }); @@ -59,7 +59,7 @@ describe("Cryptographic primitives", function() { var iv = hexToArrayBuffer('000102030405060708090a0b0c0d0e0f'); var plaintext = hexToArrayBuffer('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710'); var ciphertext = hexToArrayBuffer('f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d39f23369a9d9bacfa530e26304231461b2eb05e2c39be9fcda6c19078c6a9d1b3f461796d6b0d6b2e0c2a72b4d80e644'); - return window.crypto.subtle.decrypt({name: "AES-CBC", iv: iv}, key, ciphertext).then(function(result) { + return window.textsecure.subtle.decrypt({name: "AES-CBC", iv: iv}, key, ciphertext).then(function(result) { assert.strictEqual(getString(result), getString(plaintext)); }).then(done).catch(done); }); @@ -70,7 +70,7 @@ describe("Cryptographic primitives", function() { var key = hexToArrayBuffer('6f35628d65813435534b5d67fbdb54cb33403d04e843103e6399f806cb5df95febbdd61236f33245'); var input = hexToArrayBuffer('752cff52e4b90768558e5369e75d97c69643509a5e5904e0a386cbe4d0970ef73f918f675945a9aefe26daea27587e8dc909dd56fd0468805f834039b345f855cfe19c44b55af241fff3ffcd8045cd5c288e6c4e284c3720570b58e4d47b8feeedc52fd1401f698a209fccfa3b4c0d9a797b046a2759f82a54c41ccd7b5f592b'); var mac = getString(hexToArrayBuffer('05d1243e6465ed9620c9aec1c351a186')); - return window.crypto.subtle.sign({name: "HMAC", hash: "SHA-256"}, key, input).then(function(result) { + return window.textsecure.subtle.sign({name: "HMAC", hash: "SHA-256"}, key, input).then(function(result) { assert.strictEqual(getString(result).substring(0, mac.length), mac); }).then(done).catch(done); }); diff --git a/js/webcrypto.js b/js/webcrypto.js index cf10a0c7..50fc40a8 100644 --- a/js/webcrypto.js +++ b/js/webcrypto.js @@ -16,7 +16,7 @@ /* Web Crypto polyfill. TODO: replace with web crypto */ // All inputs/outputs are arraybuffers! -window.crypto.subtle = (function() { +window.textsecure.subtle = (function() { /* if (window.crypto.subtle !== undefined && window.crypto.subtle !== null) { return window.crypto.subtle; } else*/ {