From 244e051fc3513305a249b3038c951800294be832 Mon Sep 17 00:00:00 2001 From: lilia Date: Fri, 24 Oct 2014 13:41:35 -0700 Subject: [PATCH] Add importKey to webcrypto.js --- js/test.js | 24 +++++++++++++++--------- js/webcrypto.js | 45 +++++++++++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/js/test.js b/js/test.js index 031c84ed..d8d60ea0 100644 --- a/js/test.js +++ b/js/test.js @@ -35,9 +35,11 @@ describe("Cryptographic primitives", function() { var iv = hexToArrayBuffer('000102030405060708090a0b0c0d0e0f'); var plaintext = hexToArrayBuffer('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710'); var ciphertext = hexToArrayBuffer('f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d39f23369a9d9bacfa530e26304231461b2eb05e2c39be9fcda6c19078c6a9d1b3f461796d6b0d6b2e0c2a72b4d80e644'); - return window.textsecure.subtle.encrypt({name: "AES-CBC", iv: iv}, key, plaintext).then(function(result) { - assert.strictEqual(getString(result), getString(ciphertext)); - }).then(done).catch(done); + window.textsecure.subtle.importKey('raw', key, {name: 'AES-CBC'}, true, ['encrypt']).then(function(key) { + return window.textsecure.subtle.encrypt({name: 'AES-CBC', iv: new Uint8Array(iv)}, key, plaintext).then(function(result) { + assert.strictEqual(getString(result), getString(ciphertext)); + }).then(done).catch(done); + }).catch(done); }); }); @@ -47,9 +49,11 @@ describe("Cryptographic primitives", function() { var iv = hexToArrayBuffer('000102030405060708090a0b0c0d0e0f'); var plaintext = hexToArrayBuffer('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710'); var ciphertext = hexToArrayBuffer('f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d39f23369a9d9bacfa530e26304231461b2eb05e2c39be9fcda6c19078c6a9d1b3f461796d6b0d6b2e0c2a72b4d80e644'); - 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); + window.textsecure.subtle.importKey('raw', key, {name: 'AES-CBC'}, true, ['decrypt']).then(function(key) { + return window.textsecure.subtle.decrypt({name: 'AES-CBC', iv: new Uint8Array(iv)}, key, ciphertext).then(function(result) { + assert.strictEqual(getString(result), getString(plaintext)); + }).then(done).catch(done); + }).catch(done); }); }); @@ -58,9 +62,11 @@ describe("Cryptographic primitives", function() { var key = hexToArrayBuffer('6f35628d65813435534b5d67fbdb54cb33403d04e843103e6399f806cb5df95febbdd61236f33245'); var input = hexToArrayBuffer('752cff52e4b90768558e5369e75d97c69643509a5e5904e0a386cbe4d0970ef73f918f675945a9aefe26daea27587e8dc909dd56fd0468805f834039b345f855cfe19c44b55af241fff3ffcd8045cd5c288e6c4e284c3720570b58e4d47b8feeedc52fd1401f698a209fccfa3b4c0d9a797b046a2759f82a54c41ccd7b5f592b'); var mac = getString(hexToArrayBuffer('05d1243e6465ed9620c9aec1c351a186')); - 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); + window.textsecure.subtle.importKey('raw', key, {name: 'HMAC', hash: {name: 'SHA-256'}}, true, ['sign']).then(function(key) { + 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); + }).catch(done); }); }); diff --git a/js/webcrypto.js b/js/webcrypto.js index 77b86d5c..8dbdd7f4 100644 --- a/js/webcrypto.js +++ b/js/webcrypto.js @@ -59,23 +59,32 @@ window.textsecure.subtle = (function() { return Promise.resolve(toArrayBuffer(implementation.apply(this, args))); } - // public interface functions - function encrypt(algorithm, key, data) { - if (algorithm.name === "AES-CBC") - return promise(encryptAESCBC, data, key, algorithm.iv); - }; - function decrypt(algorithm, key, data) { - if (algorithm.name === "AES-CBC") - return promise(decryptAESCBC, data, key, algorithm.iv); - }; - function sign(algorithm, key, data) { - if (algorithm.name === "HMAC" && algorithm.hash === "SHA-256") - return promise(HmacSHA256, key, data); - }; + // public interface functions + function encrypt(algorithm, key, data) { + if (algorithm.name === "AES-CTR") + return promise(encryptAESCTR, data, key, algorithm.counter); + if (algorithm.name === "AES-CBC") + return promise(encryptAESCBC, data, key, algorithm.iv.buffer || algorithm.iv); + }; + function decrypt(algorithm, key, data) { + if (algorithm.name === "AES-CTR") + return promise(decryptAESCTR, data, key, algorithm.counter); + if (algorithm.name === "AES-CBC") + return promise(decryptAESCBC, data, key, algorithm.iv.buffer || algorithm.iv); + }; + function sign(algorithm, key, data) { + if (algorithm.name === "HMAC" && algorithm.hash === "SHA-256") + return promise(HmacSHA256, key, data); + }; - return { - encrypt : encrypt, - decrypt : decrypt, - sign : sign, - } + function importKey(format, key, algorithm, extractable, usages) { + return new Promise(function(resolve,reject){ resolve(key); }); + }; + + return { + encrypt : encrypt, + decrypt : decrypt, + sign : sign, + importKey : importKey, + } })();