2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-01-16 04:11:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
;(function(){
|
|
|
|
'use strict';
|
|
|
|
|
2016-05-08 00:49:45 +00:00
|
|
|
var encrypt = libsignal.crypto.encrypt;
|
|
|
|
var decrypt = libsignal.crypto.decrypt;
|
|
|
|
var calculateMAC = libsignal.crypto.calculateMAC;
|
|
|
|
var verifyMAC = libsignal.crypto.verifyMAC;
|
2015-01-16 04:11:08 +00:00
|
|
|
|
|
|
|
window.textsecure = window.textsecure || {};
|
|
|
|
window.textsecure.crypto = {
|
|
|
|
// Decrypts message into a raw string
|
2015-08-07 21:52:33 +00:00
|
|
|
decryptWebsocketMessage: function(message, signaling_key) {
|
2015-01-16 04:11:08 +00:00
|
|
|
var decodedMessage = message.toArrayBuffer();
|
2015-10-27 19:15:08 +00:00
|
|
|
|
|
|
|
if (signaling_key.byteLength != 52) {
|
|
|
|
throw new Error("Got invalid length signaling_key");
|
|
|
|
}
|
|
|
|
if (decodedMessage.byteLength < 1 + 16 + 10) {
|
|
|
|
throw new Error("Got invalid length message");
|
|
|
|
}
|
|
|
|
if (new Uint8Array(decodedMessage)[0] != 1) {
|
2015-01-16 04:11:08 +00:00
|
|
|
throw new Error("Got bad version number: " + decodedMessage[0]);
|
2015-10-27 19:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var aes_key = signaling_key.slice(0, 32);
|
|
|
|
var mac_key = signaling_key.slice(32, 32 + 20);
|
2015-01-16 04:11:08 +00:00
|
|
|
|
|
|
|
var iv = decodedMessage.slice(1, 1 + 16);
|
|
|
|
var ciphertext = decodedMessage.slice(1 + 16, decodedMessage.byteLength - 10);
|
|
|
|
var ivAndCiphertext = decodedMessage.slice(0, decodedMessage.byteLength - 10);
|
|
|
|
var mac = decodedMessage.slice(decodedMessage.byteLength - 10, decodedMessage.byteLength);
|
|
|
|
|
2015-11-25 00:14:12 +00:00
|
|
|
return verifyMAC(ivAndCiphertext, mac_key, mac, 10).then(function() {
|
2015-01-16 08:15:00 +00:00
|
|
|
return decrypt(aes_key, ciphertext, iv);
|
2015-01-16 04:11:08 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
decryptAttachment: function(encryptedBin, keys) {
|
2015-10-27 19:15:08 +00:00
|
|
|
if (keys.byteLength != 64) {
|
|
|
|
throw new Error("Got invalid length attachment keys");
|
|
|
|
}
|
|
|
|
if (encryptedBin.byteLength < 16 + 32) {
|
|
|
|
throw new Error("Got invalid length attachment");
|
|
|
|
}
|
|
|
|
|
2015-01-16 04:11:08 +00:00
|
|
|
var aes_key = keys.slice(0, 32);
|
|
|
|
var mac_key = keys.slice(32, 64);
|
|
|
|
|
|
|
|
var iv = encryptedBin.slice(0, 16);
|
|
|
|
var ciphertext = encryptedBin.slice(16, encryptedBin.byteLength - 32);
|
|
|
|
var ivAndCiphertext = encryptedBin.slice(0, encryptedBin.byteLength - 32);
|
|
|
|
var mac = encryptedBin.slice(encryptedBin.byteLength - 32, encryptedBin.byteLength);
|
|
|
|
|
2015-11-25 00:14:12 +00:00
|
|
|
return verifyMAC(ivAndCiphertext, mac_key, mac, 32).then(function() {
|
2015-01-16 08:15:00 +00:00
|
|
|
return decrypt(aes_key, ciphertext, iv);
|
2015-01-16 04:11:08 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
encryptAttachment: function(plaintext, keys, iv) {
|
2015-10-27 19:15:08 +00:00
|
|
|
if (keys.byteLength != 64) {
|
|
|
|
throw new Error("Got invalid length attachment keys");
|
|
|
|
}
|
|
|
|
if (iv.byteLength != 16) {
|
|
|
|
throw new Error("Got invalid length attachment iv");
|
|
|
|
}
|
2015-01-16 04:11:08 +00:00
|
|
|
var aes_key = keys.slice(0, 32);
|
|
|
|
var mac_key = keys.slice(32, 64);
|
|
|
|
|
2015-01-16 08:15:00 +00:00
|
|
|
return encrypt(aes_key, plaintext, iv).then(function(ciphertext) {
|
2015-01-16 04:11:08 +00:00
|
|
|
var ivAndCiphertext = new Uint8Array(16 + ciphertext.byteLength);
|
|
|
|
ivAndCiphertext.set(new Uint8Array(iv));
|
|
|
|
ivAndCiphertext.set(new Uint8Array(ciphertext), 16);
|
|
|
|
|
2015-01-16 08:15:00 +00:00
|
|
|
return calculateMAC(mac_key, ivAndCiphertext.buffer).then(function(mac) {
|
2015-01-16 04:11:08 +00:00
|
|
|
var encryptedBin = new Uint8Array(16 + ciphertext.byteLength + 32);
|
|
|
|
encryptedBin.set(ivAndCiphertext);
|
|
|
|
encryptedBin.set(new Uint8Array(mac), 16 + ciphertext.byteLength);
|
|
|
|
return encryptedBin.buffer;
|
|
|
|
});
|
|
|
|
});
|
2015-01-16 07:57:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getRandomBytes: function(size) {
|
2016-05-08 00:49:45 +00:00
|
|
|
return libsignal.crypto.getRandomBytes(size);
|
2015-01-16 04:11:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|