Build CryptoJS components into webcrypto.js
We only depend on cryptojs for this webcrypto polyfill, so let Grunt concatenate them into one file. The reference in the getString helper isn't needed since we use the built in string converters on CryptoJS's word arrays.
This commit is contained in:
parent
4518d03f43
commit
e190582d9e
9 changed files with 2660 additions and 2566 deletions
31
Gruntfile.js
31
Gruntfile.js
|
@ -15,19 +15,6 @@ module.exports = function(grunt) {
|
|||
src: components,
|
||||
dest: 'js/components.js',
|
||||
},
|
||||
cryptojs: {
|
||||
src: [
|
||||
"components/cryptojs/src/core.js",
|
||||
"components/cryptojs/src/sha256.js",
|
||||
"components/cryptojs/src/hmac.js",
|
||||
"components/cryptojs/src/enc-base64.js",
|
||||
"components/cryptojs/src/md5.js",
|
||||
"components/cryptojs/src/evpkdf.js",
|
||||
"components/cryptojs/src/cipher-core.js",
|
||||
"components/cryptojs/src/aes.js"
|
||||
],
|
||||
dest: 'js-deps/CryptoJS.js'
|
||||
},
|
||||
test: {
|
||||
src: [
|
||||
'components/mocha/mocha.js',
|
||||
|
@ -46,6 +33,24 @@ module.exports = function(grunt) {
|
|||
banner: ';(function(){\n',
|
||||
footer: '\n})();'
|
||||
}
|
||||
},
|
||||
webcrypto: {
|
||||
src: [
|
||||
'components/cryptojs/src/core.js',
|
||||
'components/cryptojs/src/sha256.js',
|
||||
'components/cryptojs/src/hmac.js',
|
||||
'components/cryptojs/src/enc-base64.js',
|
||||
'components/cryptojs/src/md5.js',
|
||||
'components/cryptojs/src/evpkdf.js',
|
||||
'components/cryptojs/src/cipher-core.js',
|
||||
'components/cryptojs/src/aes.js',
|
||||
'build/webcrypto.js'
|
||||
],
|
||||
dest: 'js/webcrypto.js',
|
||||
options: {
|
||||
banner: ';(function(){\n',
|
||||
footer: '\n})();'
|
||||
}
|
||||
}
|
||||
},
|
||||
sass: {
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
<html>
|
||||
<head>
|
||||
<script type="text/javascript" src="js-deps/nacl-common.js"></script>
|
||||
<script type="text/javascript" src="js-deps/CryptoJS.js"></script>
|
||||
<script type="text/javascript" src="js/components.js"></script>
|
||||
|
||||
<script type="text/javascript" src="js/protobufs.js"></script>
|
||||
|
|
92
build/webcrypto.js
Normal file
92
build/webcrypto.js
Normal file
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
;(function() {
|
||||
'use strict';
|
||||
// Test for webcrypto support, polyfill if needed.
|
||||
if (window.crypto.subtle === undefined || window.crypto.subtle === null) {
|
||||
window.crypto.subtle = (function () {
|
||||
var StaticArrayBufferProto = new ArrayBuffer().__proto__;
|
||||
function assertIsArrayBuffer(thing) {
|
||||
if (thing !== Object(thing) || thing.__proto__ != StaticArrayBufferProto)
|
||||
throw new Error("Needed a ArrayBuffer");
|
||||
}
|
||||
|
||||
// Synchronous implementation functions for polyfilling webcrypto
|
||||
// All inputs/outputs are arraybuffers!
|
||||
function HmacSHA256(key, input) {
|
||||
assertIsArrayBuffer(key);
|
||||
assertIsArrayBuffer(input);
|
||||
return CryptoJS.HmacSHA256(
|
||||
CryptoJS.enc.Latin1.parse(getString(input)),
|
||||
CryptoJS.enc.Latin1.parse(getString(key))
|
||||
).toString(CryptoJS.enc.Latin1);
|
||||
};
|
||||
|
||||
function encryptAESCBC(plaintext, key, iv) {
|
||||
assertIsArrayBuffer(plaintext);
|
||||
assertIsArrayBuffer(key);
|
||||
assertIsArrayBuffer(iv);
|
||||
return CryptoJS.AES.encrypt(
|
||||
CryptoJS.enc.Latin1.parse(getString(plaintext)),
|
||||
CryptoJS.enc.Latin1.parse(getString(key)),
|
||||
{ iv: CryptoJS.enc.Latin1.parse(getString(iv)) }
|
||||
).ciphertext.toString(CryptoJS.enc.Latin1);
|
||||
};
|
||||
|
||||
function decryptAESCBC(ciphertext, key, iv) {
|
||||
assertIsArrayBuffer(ciphertext);
|
||||
assertIsArrayBuffer(key);
|
||||
assertIsArrayBuffer(iv);
|
||||
return CryptoJS.AES.decrypt(
|
||||
btoa(getString(ciphertext)),
|
||||
CryptoJS.enc.Latin1.parse(getString(key)),
|
||||
{ iv: CryptoJS.enc.Latin1.parse(getString(iv)) }
|
||||
).toString(CryptoJS.enc.Latin1);
|
||||
};
|
||||
|
||||
// utility function for connecting front and back ends via promises
|
||||
// Takes an implementation function and 0 or more arguments
|
||||
function promise(implementation) {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
args.shift();
|
||||
return new Promise(function(resolve) {
|
||||
resolve(toArrayBuffer(implementation.apply(this, args)));
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
encrypt: function(algorithm, key, data) {
|
||||
if (algorithm.name === "AES-CBC")
|
||||
return promise(encryptAESCBC, data, key, algorithm.iv.buffer || algorithm.iv);
|
||||
},
|
||||
|
||||
decrypt: function(algorithm, key, data) {
|
||||
if (algorithm.name === "AES-CBC")
|
||||
return promise(decryptAESCBC, data, key, algorithm.iv.buffer || algorithm.iv);
|
||||
},
|
||||
|
||||
sign: function(algorithm, key, data) {
|
||||
if (algorithm.name === "HMAC" && algorithm.hash === "SHA-256")
|
||||
return promise(HmacSHA256, key, data);
|
||||
},
|
||||
|
||||
importKey: function(format, key, algorithm, extractable, usages) {
|
||||
return new Promise(function(resolve,reject){ resolve(key); });
|
||||
}
|
||||
};
|
||||
})();
|
||||
} // if !window.crypto.subtle
|
||||
})();
|
|
@ -125,7 +125,6 @@
|
|||
</form>
|
||||
</script>
|
||||
<script type="text/javascript" src="js-deps/nacl-common.js"></script>
|
||||
<script type="text/javascript" src="js-deps/CryptoJS.js"></script>
|
||||
<script type="text/javascript" src="js/components.js"></script>
|
||||
<script type="text/javascript" src="components/bootstrap-tagsinput/dist/bootstrap-tagsinput.js"></script>
|
||||
|
||||
|
|
2544
js-deps/CryptoJS.js
2544
js-deps/CryptoJS.js
File diff suppressed because it is too large
Load diff
|
@ -100,7 +100,6 @@ window.textsecure = window.textsecure || {};
|
|||
var StaticByteBufferProto = new dcodeIO.ByteBuffer().__proto__;
|
||||
var StaticArrayBufferProto = new ArrayBuffer().__proto__;
|
||||
var StaticUint8ArrayProto = new Uint8Array().__proto__;
|
||||
var StaticWordArrayProto = CryptoJS.lib.WordArray.create('').__proto__;
|
||||
function getString(thing) {
|
||||
if (thing === Object(thing)) {
|
||||
if (thing.__proto__ == StaticUint8ArrayProto)
|
||||
|
@ -109,8 +108,6 @@ function getString(thing) {
|
|||
return getString(new Uint8Array(thing));
|
||||
if (thing.__proto__ == StaticByteBufferProto)
|
||||
return thing.toString("binary");
|
||||
if (thing.__proto__ == StaticWordArrayProto)
|
||||
return thing.toString(CryptoJS.enc.Latin1);
|
||||
}
|
||||
return thing;
|
||||
}
|
||||
|
|
2552
js/webcrypto.js
2552
js/webcrypto.js
File diff suppressed because it is too large
Load diff
|
@ -91,7 +91,6 @@
|
|||
</div>
|
||||
</div>
|
||||
<script type="text/javascript" src="js-deps/nacl-common.js"></script>
|
||||
<script type="text/javascript" src="js-deps/CryptoJS.js"></script>
|
||||
<script type="text/javascript" src="js/components.js"></script>
|
||||
|
||||
<script type="text/javascript" src="js/protobufs.js"></script>
|
||||
|
|
|
@ -123,7 +123,6 @@
|
|||
<script type="text/javascript" src="blanket_mocha.js"></script>
|
||||
|
||||
<script type="text/javascript" src="../js-deps/nacl-common.js"></script>
|
||||
<script type="text/javascript" src="../js-deps/CryptoJS.js"></script>
|
||||
<script type="text/javascript" src="../js/components.js"></script>
|
||||
|
||||
<script type="text/javascript" src="../js/nativeclient.js"></script>
|
||||
|
|
Loading…
Reference in a new issue