2016-10-25 04:16:47 +02:00
|
|
|
function CryptoService(constantsService) {
|
|
|
|
this.constantsService = constantsService;
|
2016-09-04 06:34:24 +02:00
|
|
|
initCryptoService();
|
2016-09-03 06:03:13 +02:00
|
|
|
};
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2016-09-04 06:34:24 +02:00
|
|
|
function initCryptoService() {
|
2016-09-03 06:03:13 +02:00
|
|
|
var _key,
|
|
|
|
_b64Key,
|
2016-10-25 04:16:47 +02:00
|
|
|
_keyHash,
|
|
|
|
_b64KeyHash,
|
2016-09-03 06:03:13 +02:00
|
|
|
_aes;
|
|
|
|
|
2016-09-05 07:49:44 +02:00
|
|
|
sjcl.beware["CBC mode is dangerous because it doesn't protect message integrity."]();
|
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
CryptoService.prototype.setKey = function (key, callback) {
|
2016-09-02 06:50:57 +02:00
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
2016-10-25 04:16:47 +02:00
|
|
|
var self = this;
|
2016-09-02 06:50:57 +02:00
|
|
|
_key = key;
|
2016-10-25 04:16:47 +02:00
|
|
|
|
|
|
|
chrome.storage.local.get(self.constantsService.lockOptionKey, function (obj) {
|
|
|
|
if (obj && (obj[self.constantsService.lockOptionKey] || obj[self.constantsService.lockOptionKey] === 0)) {
|
|
|
|
// if we have a lock option set, we do not store the key
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
chrome.storage.local.set({
|
|
|
|
'key': sjcl.codec.base64.fromBits(key)
|
|
|
|
}, function () {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
CryptoService.prototype.setKeyHash = function (keyHash, callback) {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
|
|
|
_keyHash = sjcl.codec.base64.toBits(keyHash);
|
|
|
|
|
2016-09-02 06:50:57 +02:00
|
|
|
chrome.storage.local.set({
|
2016-10-25 04:16:47 +02:00
|
|
|
'keyHash': keyHash
|
2016-09-02 06:50:57 +02:00
|
|
|
}, function () {
|
|
|
|
callback();
|
|
|
|
});
|
2016-09-03 06:03:13 +02:00
|
|
|
}
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
CryptoService.prototype.getKey = function (b64, callback) {
|
2016-09-02 06:50:57 +02:00
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b64 && b64 === true && _b64Key) {
|
2016-10-25 04:16:47 +02:00
|
|
|
callback(_b64Key);
|
|
|
|
return;
|
2016-09-02 06:50:57 +02:00
|
|
|
}
|
|
|
|
else if (!b64 && _key) {
|
2016-10-25 04:16:47 +02:00
|
|
|
callback(_key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (b64 && b64 === true && _key && !_b64Key) {
|
|
|
|
_b64Key = sjcl.codec.base64.fromBits(_key);
|
|
|
|
callback(_b64Key);
|
|
|
|
return;
|
2016-09-02 06:50:57 +02:00
|
|
|
}
|
|
|
|
|
2016-10-25 04:16:47 +02:00
|
|
|
var self = this;
|
|
|
|
chrome.storage.local.get(self.constantsService.lockOptionKey, function (obj) {
|
|
|
|
if (obj && (obj[self.constantsService.lockOptionKey] || obj[self.constantsService.lockOptionKey] === 0)) {
|
|
|
|
// if we have a lock option set, we do not try to fetch the storage key since it should not even be there
|
|
|
|
callback(null);
|
|
|
|
return;
|
2016-09-02 06:50:57 +02:00
|
|
|
}
|
|
|
|
|
2016-10-25 04:16:47 +02:00
|
|
|
chrome.storage.local.get('key', function (obj) {
|
|
|
|
if (obj && obj.key) {
|
|
|
|
_key = sjcl.codec.base64.toBits(obj.key);
|
|
|
|
|
|
|
|
if (b64 && b64 === true) {
|
|
|
|
_b64Key = obj.key;
|
|
|
|
callback(_b64Key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(_key);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
CryptoService.prototype.getKeyHash = function (b64, callback) {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b64 && b64 === true && _b64KeyHash) {
|
|
|
|
callback(_b64KeyHash);
|
|
|
|
}
|
|
|
|
else if (!b64 && _keyHash) {
|
|
|
|
callback(_keyHash);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (b64 && b64 === true && _keyHash && !_b64KeyHash) {
|
|
|
|
_b64KeyHash = sjcl.codec.base64.fromBits(_keyHash);
|
|
|
|
callback(_b64KeyHash);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
chrome.storage.local.get('keyHash', function (obj) {
|
|
|
|
if (obj && obj.keyHash) {
|
|
|
|
_keyHash = sjcl.codec.base64.toBits(obj.keyHash);
|
|
|
|
|
|
|
|
if (b64 && b64 === true) {
|
|
|
|
_b64KeyHash = obj.keyHash;
|
|
|
|
callback(_b64KeyHash);
|
|
|
|
return;
|
|
|
|
}
|
2016-09-02 06:50:57 +02:00
|
|
|
}
|
|
|
|
|
2016-10-25 04:16:47 +02:00
|
|
|
callback(_keyHash);
|
2016-09-02 06:50:57 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
CryptoService.prototype.clearKey = function (callback) {
|
2016-09-02 06:50:57 +02:00
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
|
|
|
_key = _b64Key = _aes = null;
|
|
|
|
chrome.storage.local.remove('key', function () {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-25 04:16:47 +02:00
|
|
|
CryptoService.prototype.clearKeyHash = function (callback) {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
|
|
|
_keyHash = _b64KeyHash = null;
|
|
|
|
chrome.storage.local.remove('keyHash', function () {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
CryptoService.prototype.toggleKey = function (callback) {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
self.getKey(false, function (key) {
|
|
|
|
chrome.storage.local.get(self.constantsService.lockOptionKey, function (obj) {
|
|
|
|
if (obj && (obj[self.constantsService.lockOptionKey] || obj[self.constantsService.lockOptionKey] === 0)) {
|
|
|
|
// if we have a lock option set, clear the key
|
|
|
|
self.clearKey(function () {
|
|
|
|
_key = key;
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// no lock option, so store the current key
|
|
|
|
self.setKey(key, function () {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
CryptoService.prototype.makeKey = function (password, salt, b64) {
|
2016-09-02 06:50:57 +02:00
|
|
|
var key = sjcl.misc.pbkdf2(password, salt, 5000, 256, null);
|
|
|
|
|
|
|
|
if (b64 && b64 === true) {
|
|
|
|
return sjcl.codec.base64.fromBits(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
};
|
|
|
|
|
2016-09-05 06:03:49 +02:00
|
|
|
CryptoService.prototype.hashPassword = function (password, key, callback) {
|
|
|
|
this.getKey(false, function (storedKey) {
|
|
|
|
if (!key) {
|
|
|
|
key = storedKey;
|
|
|
|
}
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2016-09-05 06:03:49 +02:00
|
|
|
if (!password || !key) {
|
|
|
|
throw 'Invalid parameters.';
|
|
|
|
}
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2016-09-05 06:03:49 +02:00
|
|
|
var hashBits = sjcl.misc.pbkdf2(key, password, 1, 256, null);
|
|
|
|
callback(sjcl.codec.base64.fromBits(hashBits));
|
|
|
|
});
|
2016-09-02 06:50:57 +02:00
|
|
|
};
|
|
|
|
|
2016-09-04 06:34:24 +02:00
|
|
|
CryptoService.prototype.getAes = function (callback) {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
2016-09-02 06:50:57 +02:00
|
|
|
}
|
|
|
|
|
2016-09-04 06:34:24 +02:00
|
|
|
this.getKey(false, function (key) {
|
|
|
|
if (!_aes && key) {
|
|
|
|
_aes = new sjcl.cipher.aes(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(_aes);
|
|
|
|
});
|
2016-09-02 06:50:57 +02:00
|
|
|
};
|
|
|
|
|
2016-09-17 05:47:50 +02:00
|
|
|
CryptoService.prototype.encrypt = function (plaintextValue) {
|
|
|
|
var deferred = Q.defer();
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2016-09-05 07:49:44 +02:00
|
|
|
if (plaintextValue === null || plaintextValue === undefined) {
|
2016-09-17 05:47:50 +02:00
|
|
|
deferred.resolve(null);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.getKey(false, function (key) {
|
|
|
|
if (!key) {
|
|
|
|
throw 'Encryption key unavailable.';
|
|
|
|
}
|
|
|
|
|
|
|
|
var response = {};
|
|
|
|
var params = {
|
|
|
|
mode: "cbc",
|
2016-10-11 03:28:57 +02:00
|
|
|
iv: sjcl.random.randomWords(4, 10)
|
2016-09-17 05:47:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var ctJson = sjcl.encrypt(key, plaintextValue, params, response);
|
|
|
|
|
|
|
|
var ct = ctJson.match(/"ct":"([^"]*)"/)[1];
|
|
|
|
var iv = sjcl.codec.base64.fromBits(response.iv);
|
|
|
|
|
|
|
|
var cs = new CipherString(iv + "|" + ct);
|
|
|
|
deferred.resolve(cs);
|
|
|
|
});
|
2016-09-05 07:49:44 +02:00
|
|
|
}
|
|
|
|
|
2016-09-17 05:47:50 +02:00
|
|
|
return deferred.promise;
|
2016-09-02 06:50:57 +02:00
|
|
|
};
|
|
|
|
|
2016-09-17 05:47:50 +02:00
|
|
|
CryptoService.prototype.decrypt = function (cipherString) {
|
|
|
|
var deferred = Q.defer();
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2016-09-05 17:05:27 +02:00
|
|
|
if (cipherString === null || cipherString === undefined || !cipherString.encryptedString) {
|
2016-09-05 07:49:44 +02:00
|
|
|
throw 'cannot decrypt nothing';
|
|
|
|
}
|
|
|
|
|
2016-09-04 06:34:24 +02:00
|
|
|
this.getAes(function (aes) {
|
|
|
|
if (!aes) {
|
|
|
|
throw 'AES encryption unavailable.';
|
|
|
|
}
|
|
|
|
|
|
|
|
var ivBits = sjcl.codec.base64.toBits(cipherString.initializationVector);
|
|
|
|
var ctBits = sjcl.codec.base64.toBits(cipherString.cipherText);
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2016-09-04 06:34:24 +02:00
|
|
|
var decBits = sjcl.mode.cbc.decrypt(aes, ctBits, ivBits, null);
|
2016-09-06 04:27:32 +02:00
|
|
|
var decValue = sjcl.codec.utf8String.fromBits(decBits);
|
2016-09-17 05:47:50 +02:00
|
|
|
deferred.resolve(decValue);
|
2016-09-04 06:34:24 +02:00
|
|
|
});
|
2016-09-17 05:47:50 +02:00
|
|
|
|
|
|
|
return deferred.promise;
|
2016-09-02 06:50:57 +02:00
|
|
|
};
|
2016-09-04 06:34:24 +02:00
|
|
|
};
|