2014-04-04 08:33:02 +02:00
|
|
|
/* Web Crypto polyfill. TODO: replace with web crypto */
|
|
|
|
// All inputs/outputs are arraybuffers!
|
|
|
|
window.crypto.subtle = (function() {
|
|
|
|
if (window.crypto.subtle !== undefined && window.crypto.subtle !== null) {
|
|
|
|
return window.crypto.subtle;
|
|
|
|
} else {
|
2014-05-09 09:43:23 +02:00
|
|
|
var StaticArrayBufferProto = new ArrayBuffer().__proto__;
|
|
|
|
function assertIsArrayBuffer(thing) {
|
|
|
|
if (thing !== Object(thing) || thing.__proto__ != StaticArrayBufferProto)
|
|
|
|
throw new Error("WARNING: Needed a ArrayBuffer");
|
|
|
|
}
|
|
|
|
|
2014-04-04 08:33:02 +02:00
|
|
|
// private implementation functions
|
|
|
|
function HmacSHA256(key, input) {
|
2014-05-09 09:43:23 +02:00
|
|
|
assertIsArrayBuffer(key);
|
|
|
|
assertIsArrayBuffer(input);
|
2014-04-04 08:33:02 +02:00
|
|
|
return CryptoJS.HmacSHA256(
|
|
|
|
CryptoJS.lib.WordArray.create(toArrayBuffer(input)),
|
|
|
|
CryptoJS.enc.Latin1.parse(getString(key))
|
|
|
|
).toString(CryptoJS.enc.Latin1);
|
|
|
|
};
|
|
|
|
|
|
|
|
function encryptAESCTR(plaintext, key, counter) {
|
2014-05-09 09:43:23 +02:00
|
|
|
assertIsArrayBuffer(plaintext);
|
|
|
|
assertIsArrayBuffer(key);
|
|
|
|
assertIsArrayBuffer(counter);
|
2014-04-04 08:33:02 +02:00
|
|
|
return CryptoJS.AES.encrypt(CryptoJS.enc.Latin1.parse(getString(plaintext)),
|
|
|
|
CryptoJS.enc.Latin1.parse(getString(key)),
|
|
|
|
{mode: CryptoJS.mode.CTR, iv: CryptoJS.enc.Latin1.parse(getString(counter)),
|
|
|
|
padding: CryptoJS.pad.NoPadding})
|
|
|
|
.ciphertext.toString(CryptoJS.enc.Latin1);
|
|
|
|
};
|
|
|
|
|
|
|
|
function decryptAESCTR(ciphertext, key, counter) {
|
2014-05-09 09:43:23 +02:00
|
|
|
assertIsArrayBuffer(ciphertext);
|
|
|
|
assertIsArrayBuffer(key);
|
|
|
|
assertIsArrayBuffer(counter);
|
2014-04-04 08:33:02 +02:00
|
|
|
return CryptoJS.AES.decrypt(btoa(getString(ciphertext)),
|
|
|
|
CryptoJS.enc.Latin1.parse(getString(key)),
|
|
|
|
{mode: CryptoJS.mode.CTR, iv: CryptoJS.enc.Latin1.parse(getString(counter)),
|
|
|
|
padding: CryptoJS.pad.NoPadding})
|
|
|
|
.toString(CryptoJS.enc.Latin1);
|
|
|
|
};
|
|
|
|
|
|
|
|
function decryptAESCBC(ciphertext, key, iv) {
|
2014-05-09 09:43:23 +02:00
|
|
|
assertIsArrayBuffer(ciphertext);
|
|
|
|
assertIsArrayBuffer(key);
|
|
|
|
assertIsArrayBuffer(iv);
|
2014-04-04 08:33:02 +02:00
|
|
|
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();
|
2014-05-09 09:43:23 +02:00
|
|
|
return new Promise.resolve(toArrayBuffer(implementation.apply(this, args)));
|
2014-04-04 08:33:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// public interface functions
|
|
|
|
function encrypt(algorithm, key, data) {
|
2014-05-06 05:42:48 +02:00
|
|
|
if (algorithm.name === "AES-CTR")
|
|
|
|
return promise(encryptAESCTR, data, key, algorithm.counter);
|
2014-04-04 08:33:02 +02:00
|
|
|
};
|
|
|
|
function decrypt(algorithm, key, data) {
|
2014-05-06 05:42:48 +02:00
|
|
|
if (algorithm.name === "AES-CTR")
|
|
|
|
return promise(decryptAESCTR, data, key, algorithm.counter);
|
|
|
|
if (algorithm.name === "AES-CBC")
|
|
|
|
return promise(decryptAESCBC, data, key, algorithm.iv);
|
2014-04-04 08:33:02 +02:00
|
|
|
};
|
|
|
|
function sign(algorithm, key, data) {
|
2014-05-06 05:42:48 +02:00
|
|
|
if (algorithm.name === "HMAC" && algorithm.hash === "SHA-256")
|
2014-04-04 08:33:02 +02:00
|
|
|
return promise(HmacSHA256, key, data);
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
encrypt : encrypt,
|
|
|
|
decrypt : decrypt,
|
|
|
|
sign : sign,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|