2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://services-crypto/WeaveCrypto.js", this);
|
2011-04-21 01:27:19 +04:00
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var cryptoSvc = new WeaveCrypto();
|
2010-06-22 11:20:31 +04:00
|
|
|
|
2017-09-16 05:21:31 +03:00
|
|
|
add_task(async function test_crypto_random() {
|
2011-01-26 08:31:41 +03:00
|
|
|
if (this.gczeal) {
|
|
|
|
_("Running crypto random tests with gczeal(2).");
|
|
|
|
gczeal(2);
|
|
|
|
}
|
|
|
|
|
2008-06-24 03:23:57 +04:00
|
|
|
// Test salt generation.
|
|
|
|
var salt;
|
|
|
|
|
|
|
|
salt = cryptoSvc.generateRandomBytes(0);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(salt.length, 0);
|
2008-06-24 03:23:57 +04:00
|
|
|
salt = cryptoSvc.generateRandomBytes(1);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(salt.length, 4);
|
2008-06-24 03:23:57 +04:00
|
|
|
salt = cryptoSvc.generateRandomBytes(2);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(salt.length, 4);
|
2008-06-24 03:23:57 +04:00
|
|
|
salt = cryptoSvc.generateRandomBytes(3);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(salt.length, 4);
|
2008-06-24 03:23:57 +04:00
|
|
|
salt = cryptoSvc.generateRandomBytes(4);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(salt.length, 8);
|
2008-06-24 03:23:57 +04:00
|
|
|
salt = cryptoSvc.generateRandomBytes(8);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(salt.length, 12);
|
2008-06-24 03:23:57 +04:00
|
|
|
|
|
|
|
// sanity check to make sure salts seem random
|
|
|
|
var salt2 = cryptoSvc.generateRandomBytes(8);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(salt2.length, 12);
|
|
|
|
Assert.notEqual(salt, salt2);
|
2008-06-24 03:23:57 +04:00
|
|
|
|
|
|
|
salt = cryptoSvc.generateRandomBytes(1024);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(salt.length, 1368);
|
2011-03-03 23:20:43 +03:00
|
|
|
salt = cryptoSvc.generateRandomBytes(16);
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(salt.length, 24);
|
2008-06-24 03:23:57 +04:00
|
|
|
|
|
|
|
|
|
|
|
// Test random key generation
|
|
|
|
var keydata, keydata2, iv;
|
|
|
|
|
2017-09-16 05:21:31 +03:00
|
|
|
keydata = await cryptoSvc.generateRandomKey();
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(keydata.length, 44);
|
2017-09-16 05:21:31 +03:00
|
|
|
keydata2 = await cryptoSvc.generateRandomKey();
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.notEqual(keydata, keydata2); // sanity check for randomness
|
2008-06-24 03:23:57 +04:00
|
|
|
iv = cryptoSvc.generateRandomIV();
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(iv.length, 24);
|
2008-06-24 03:23:57 +04:00
|
|
|
|
2011-01-26 08:31:41 +03:00
|
|
|
if (this.gczeal)
|
|
|
|
gczeal(0);
|
2017-10-15 21:50:30 +03:00
|
|
|
});
|