Fix/test pre key generation
This commit is contained in:
parent
3fffbad11a
commit
2b21111d7b
2 changed files with 41 additions and 30 deletions
|
@ -488,10 +488,10 @@ var crypto_tests = {};
|
||||||
|
|
||||||
var crypto_storage = {};
|
var crypto_storage = {};
|
||||||
|
|
||||||
crypto_storage.getNewPubKeySTORINGPrivKey = function(keyName, isIdentity, callback) {
|
crypto_storage.getNewPubKeySTORINGPrivKey = function(keyName, isIdentity) {
|
||||||
createNewKeyPair(isIdentity).then(function(keyPair) {
|
return createNewKeyPair(isIdentity).then(function(keyPair) {
|
||||||
storage.putEncrypted("25519Key" + keyName, 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;
|
var GENERATE_KEYS_KEYS_GENERATED = 100;
|
||||||
crypto.generateKeys = function(callback) {
|
crypto.generateKeys = function() {
|
||||||
var identityKey = crypto_storage.getStoredPubKey("identityKey");
|
var identityKey = crypto_storage.getStoredPubKey("identityKey");
|
||||||
var identityKeyCalculated = function(pubKey) {
|
var identityKeyCalculated = function(pubKey) {
|
||||||
identityKey = pubKey;
|
identityKey = pubKey;
|
||||||
|
@ -899,33 +899,35 @@ var crypto_tests = {};
|
||||||
storage.putEncrypted("maxPreKeyId", firstKeyId + GENERATE_KEYS_KEYS_GENERATED);
|
storage.putEncrypted("maxPreKeyId", firstKeyId + GENERATE_KEYS_KEYS_GENERATED);
|
||||||
|
|
||||||
if (firstKeyId > 16777000)
|
if (firstKeyId > 16777000)
|
||||||
throw new Error("You crazy motherfucker");
|
return new Promise(function() { throw new Error("You crazy motherfucker") });
|
||||||
|
|
||||||
var keys = {};
|
var keys = {};
|
||||||
keys.keys = [];
|
keys.keys = [];
|
||||||
var keysLeft = GENERATE_KEYS_KEYS_GENERATED;
|
var keysLeft = GENERATE_KEYS_KEYS_GENERATED;
|
||||||
for (var i = firstKeyId; i < firstKeyId + GENERATE_KEYS_KEYS_GENERATED; i++) {
|
return new Promise(function(resolve) {
|
||||||
crypto_storage.getNewPubKeySTORINGPrivKey("preKey" + i, false, function(pubKey) {
|
for (var i = firstKeyId; i < firstKeyId + GENERATE_KEYS_KEYS_GENERATED; i++) {
|
||||||
keys.keys[i] = {keyId: i, publicKey: pubKey, identityKey: identityKey};
|
crypto_storage.getNewPubKeySTORINGPrivKey("preKey" + i, false).then(function(pubKey) {
|
||||||
keysLeft--;
|
keys.keys[i] = {keyId: i, publicKey: pubKey, identityKey: identityKey};
|
||||||
if (keysLeft == 0) {
|
keysLeft--;
|
||||||
// 0xFFFFFF == 16777215
|
if (keysLeft == 0) {
|
||||||
keys.lastResortKey = {keyId: 16777215, publicKey: crypto_storage.getStoredPubKey("preKey16777215"), identityKey: identityKey};//TODO: Rotate lastResortKey
|
// 0xFFFFFF == 16777215
|
||||||
if (keys.lastResortKey.publicKey === undefined) {
|
keys.lastResortKey = {keyId: 16777215, publicKey: crypto_storage.getStoredPubKey("preKey16777215"), identityKey: identityKey};//TODO: Rotate lastResortKey
|
||||||
crypto_storage.getNewPubKeySTORINGPrivKey("preKey16777215", false, function(pubKey) {
|
if (keys.lastResortKey.publicKey === undefined) {
|
||||||
keys.lastResortKey.publicKey = pubKey;
|
return crypto_storage.getNewPubKeySTORINGPrivKey("preKey16777215", false).then(function(pubKey) {
|
||||||
callback(keys);
|
keys.lastResortKey.publicKey = pubKey;
|
||||||
});
|
resolve(keys);
|
||||||
} else
|
});
|
||||||
callback(keys);
|
} else
|
||||||
}
|
resolve(keys);
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (identityKey === undefined)
|
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
|
else
|
||||||
identityKeyCalculated(identityKey);
|
return identityKeyCalculated(identityKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
}( window.crypto = window.crypto || {}, jQuery ));
|
}( window.crypto = window.crypto || {}, jQuery ));
|
||||||
|
|
21
js/test.js
21
js/test.js
|
@ -127,10 +127,19 @@ registerOnLoadFunction(function() {
|
||||||
}, 'Unencrypted PushMessageProto "decrypt"', true);
|
}, 'Unencrypted PushMessageProto "decrypt"', true);
|
||||||
|
|
||||||
TEST(function(callback) {
|
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);
|
callback(true);
|
||||||
});
|
});
|
||||||
}, "Test simple create key", true);
|
}, "Test Identity/Pre Key Creation", true);
|
||||||
|
|
||||||
TEST(function(callback) {
|
TEST(function(callback) {
|
||||||
// These are just some random curve25519 test vectors I found online (with a version byte prepended to pubkeys)
|
// 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) {
|
HmacSHA256(key, input).then(function(result) {
|
||||||
callback(getString(result).substring(0, mac.length) === mac)
|
callback(getString(result).substring(0, mac.length) === mac)
|
||||||
});
|
});
|
||||||
}, "HMAC SHA-256", true);
|
}, "HMAC SHA-256", false);
|
||||||
|
|
||||||
TEST(function(callback) {
|
TEST(function(callback) {
|
||||||
var key = hexToArrayBuffer('2b7e151628aed2a6abf7158809cf4f3c');
|
var key = hexToArrayBuffer('2b7e151628aed2a6abf7158809cf4f3c');
|
||||||
|
@ -447,7 +456,7 @@ registerOnLoadFunction(function() {
|
||||||
encryptAESCTR(plaintext, key, counter).then(function(result) {
|
encryptAESCTR(plaintext, key, counter).then(function(result) {
|
||||||
callback(getString(result) === getString(ciphertext));
|
callback(getString(result) === getString(ciphertext));
|
||||||
});
|
});
|
||||||
}, "Encrypt AES-CTR", true);
|
}, "Encrypt AES-CTR", false);
|
||||||
|
|
||||||
TEST(function(callback) {
|
TEST(function(callback) {
|
||||||
var key = hexToArrayBuffer('2b7e151628aed2a6abf7158809cf4f3c');
|
var key = hexToArrayBuffer('2b7e151628aed2a6abf7158809cf4f3c');
|
||||||
|
@ -457,7 +466,7 @@ registerOnLoadFunction(function() {
|
||||||
decryptAESCTR(ciphertext, key, counter).then(function(result) {
|
decryptAESCTR(ciphertext, key, counter).then(function(result) {
|
||||||
callback(getString(result) === getString(plaintext));
|
callback(getString(result) === getString(plaintext));
|
||||||
});
|
});
|
||||||
}, "Decrypt AES-CTR", true);
|
}, "Decrypt AES-CTR", false);
|
||||||
|
|
||||||
TEST(function(callback) {
|
TEST(function(callback) {
|
||||||
var key = hexToArrayBuffer('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4');
|
var key = hexToArrayBuffer('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4');
|
||||||
|
@ -467,7 +476,7 @@ registerOnLoadFunction(function() {
|
||||||
decryptAESCBC(ciphertext, key, iv).then(function(result) {
|
decryptAESCBC(ciphertext, key, iv).then(function(result) {
|
||||||
callback(getString(result) === getString(plaintext));
|
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
|
// Setup test timeouts (note that this will only work if things are actually
|
||||||
// being run async, ie in the case of NaCL)
|
// being run async, ie in the case of NaCL)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue