Fix/test pre key generation

This commit is contained in:
Matt Corallo 2014-05-09 01:35:16 -04:00
parent 3fffbad11a
commit 2b21111d7b
2 changed files with 41 additions and 30 deletions

View file

@ -488,10 +488,10 @@ var crypto_tests = {};
var crypto_storage = {};
crypto_storage.getNewPubKeySTORINGPrivKey = function(keyName, isIdentity, callback) {
createNewKeyPair(isIdentity).then(function(keyPair) {
crypto_storage.getNewPubKeySTORINGPrivKey = function(keyName, isIdentity) {
return createNewKeyPair(isIdentity).then(function(keyPair) {
storage.putEncrypted("25519Key" + keyName, keyPair);
callback(keyPair.pubKey);
return keyPair.pubKey;
});
}
@ -890,7 +890,7 @@ var crypto_tests = {};
}
var GENERATE_KEYS_KEYS_GENERATED = 100;
crypto.generateKeys = function(callback) {
crypto.generateKeys = function() {
var identityKey = crypto_storage.getStoredPubKey("identityKey");
var identityKeyCalculated = function(pubKey) {
identityKey = pubKey;
@ -899,33 +899,35 @@ var crypto_tests = {};
storage.putEncrypted("maxPreKeyId", firstKeyId + GENERATE_KEYS_KEYS_GENERATED);
if (firstKeyId > 16777000)
throw new Error("You crazy motherfucker");
return new Promise(function() { throw new Error("You crazy motherfucker") });
var keys = {};
keys.keys = [];
var keysLeft = GENERATE_KEYS_KEYS_GENERATED;
for (var i = firstKeyId; i < firstKeyId + GENERATE_KEYS_KEYS_GENERATED; i++) {
crypto_storage.getNewPubKeySTORINGPrivKey("preKey" + i, false, function(pubKey) {
keys.keys[i] = {keyId: i, publicKey: pubKey, identityKey: identityKey};
keysLeft--;
if (keysLeft == 0) {
// 0xFFFFFF == 16777215
keys.lastResortKey = {keyId: 16777215, publicKey: crypto_storage.getStoredPubKey("preKey16777215"), identityKey: identityKey};//TODO: Rotate lastResortKey
if (keys.lastResortKey.publicKey === undefined) {
crypto_storage.getNewPubKeySTORINGPrivKey("preKey16777215", false, function(pubKey) {
keys.lastResortKey.publicKey = pubKey;
callback(keys);
});
} else
callback(keys);
}
});
}
return new Promise(function(resolve) {
for (var i = firstKeyId; i < firstKeyId + GENERATE_KEYS_KEYS_GENERATED; i++) {
crypto_storage.getNewPubKeySTORINGPrivKey("preKey" + i, false).then(function(pubKey) {
keys.keys[i] = {keyId: i, publicKey: pubKey, identityKey: identityKey};
keysLeft--;
if (keysLeft == 0) {
// 0xFFFFFF == 16777215
keys.lastResortKey = {keyId: 16777215, publicKey: crypto_storage.getStoredPubKey("preKey16777215"), identityKey: identityKey};//TODO: Rotate lastResortKey
if (keys.lastResortKey.publicKey === undefined) {
return crypto_storage.getNewPubKeySTORINGPrivKey("preKey16777215", false).then(function(pubKey) {
keys.lastResortKey.publicKey = pubKey;
resolve(keys);
});
} else
resolve(keys);
}
});
}
});
}
if (identityKey === undefined)
crypto_storage.getNewPubKeySTORINGPrivKey("identityKey", true, function(pubKey) { identityKeyCalculated(pubKey); });
return crypto_storage.getNewPubKeySTORINGPrivKey("identityKey", true).then(function(pubKey) { return identityKeyCalculated(pubKey); });
else
identityKeyCalculated(identityKey);
return identityKeyCalculated(identityKey);
}
}( window.crypto = window.crypto || {}, jQuery ));

View file

@ -127,10 +127,19 @@ registerOnLoadFunction(function() {
}, 'Unencrypted PushMessageProto "decrypt"', true);
TEST(function(callback) {
crypto.generateKeys(function() {
crypto.generateKeys().then(function() {
if (storage.getEncrypted("25519KeyidentityKey") === undefined)
return callback(false);
if (storage.getEncrypted("25519KeypreKey16777215") === undefined)
return callback(false);
for (var i = 0; i < 100; i++)
if (storage.getEncrypted("25519KeypreKey" + i) === undefined)
return callback(false);
callback(true);
});
}, "Test simple create key", true);
}, "Test Identity/Pre Key Creation", true);
TEST(function(callback) {
// These are just some random curve25519 test vectors I found online (with a version byte prepended to pubkeys)
@ -437,7 +446,7 @@ registerOnLoadFunction(function() {
HmacSHA256(key, input).then(function(result) {
callback(getString(result).substring(0, mac.length) === mac)
});
}, "HMAC SHA-256", true);
}, "HMAC SHA-256", false);
TEST(function(callback) {
var key = hexToArrayBuffer('2b7e151628aed2a6abf7158809cf4f3c');
@ -447,7 +456,7 @@ registerOnLoadFunction(function() {
encryptAESCTR(plaintext, key, counter).then(function(result) {
callback(getString(result) === getString(ciphertext));
});
}, "Encrypt AES-CTR", true);
}, "Encrypt AES-CTR", false);
TEST(function(callback) {
var key = hexToArrayBuffer('2b7e151628aed2a6abf7158809cf4f3c');
@ -457,7 +466,7 @@ registerOnLoadFunction(function() {
decryptAESCTR(ciphertext, key, counter).then(function(result) {
callback(getString(result) === getString(plaintext));
});
}, "Decrypt AES-CTR", true);
}, "Decrypt AES-CTR", false);
TEST(function(callback) {
var key = hexToArrayBuffer('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4');
@ -467,7 +476,7 @@ registerOnLoadFunction(function() {
decryptAESCBC(ciphertext, key, iv).then(function(result) {
callback(getString(result) === getString(plaintext));
});
}, "Decrypt AES-CBC", true);
}, "Decrypt AES-CBC", false);
// Setup test timeouts (note that this will only work if things are actually
// being run async, ie in the case of NaCL)