From 85f419b923bd59ba23bc1f8b79a1d91887f62eb8 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sat, 8 Mar 2014 20:23:24 -0500 Subject: [PATCH] Passing test-cases (fscking type conversions...) --- js/helpers.js | 26 ++++++++++++--------- js/test.js | 65 +++++++++++++++++++++++++++++---------------------- 2 files changed, 52 insertions(+), 39 deletions(-) diff --git a/js/helpers.js b/js/helpers.js index a715a67e..d81ddc63 100644 --- a/js/helpers.js +++ b/js/helpers.js @@ -525,9 +525,10 @@ var crypto_tests = {}; } var encryptAESCTR = function(plaintext, key, counter) { - return CryptoJS.AES.encrypt(btoa(getString(plaintext)), + return CryptoJS.AES.encrypt(CryptoJS.enc.Latin1.parse(getString(plaintext)), CryptoJS.enc.Latin1.parse(getString(key)), {mode: CryptoJS.mode.CTR, iv: CryptoJS.enc.Latin1.parse(""), padding: CryptoJS.pad.NoPadding}) + .ciphertext .toString(CryptoJS.enc.Latin1); } @@ -615,8 +616,10 @@ var crypto_tests = {}; } var maybeStepRatchet = function(session, remoteKey, previousCounter, callback) { - if (session[getString(remoteKey)] !== undefined) //TODO: null??? + if (session[getString(remoteKey)] !== undefined) { //TODO: null??? + callback();//TODO: This is happening in tests as alice (when bob is checking), probably shouldn't? return; + } var ratchet = session.currentRatchet; @@ -712,7 +715,7 @@ var crypto_tests = {}; break; case 3: //TYPE_MESSAGE_PREKEY_BUNDLE if (proto.message.readUint8() != (2 << 4 | 2)) - throw "Bad version byte"; + throw "Bad version byte"; //TODO: I don't believe this actually happens on the wire var preKeyProto = decodePreKeyWhisperMessageProtobuf(getString(proto.message)); initSessionFromPreKeyWhisperMessage(proto.source, preKeyProto, function() { decryptWhisperMessage(proto.source, getString(preKeyProto.message), function(result) { callback(result); }); @@ -727,10 +730,10 @@ var crypto_tests = {}; var doEncryptPushMessageContent = function(callback) { var msg = new WhisperMessageProtobuf(); - var plaintext = pushMessageContent.encode(); + var plaintext = toArrayBuffer(pushMessageContent.encode()); - msg.ephemeralKey = getString(session.currentRatchet.ephemeralKeyPair.pubKey); - var chain = session[msg.ephemeralKey]; + msg.ephemeralKey = toArrayBuffer(session.currentRatchet.ephemeralKeyPair.pubKey); + var chain = session[getString(msg.ephemeralKey)]; fillMessageKeys(chain, chain.counter + 1); var keys = HKDF(chain.messageKeys[chain.counter], '', "WhisperMessageKeys"); @@ -740,7 +743,7 @@ var crypto_tests = {}; //TODO msg.previousCounter = 1; - msg.ciphertext = encryptAESCTR(plaintext, keys[0], chain.counter); + msg.ciphertext = toArrayBuffer(encryptAESCTR(plaintext, keys[0], chain.counter)); var encodedMsg = getString(msg.encode()); var mac = calculateMACWithVersionByte(encodedMsg, keys[1], (2 << 4) | 2); @@ -752,17 +755,18 @@ var crypto_tests = {}; if (session === undefined) { var preKeyMsg = new PreKeyWhisperMessageProtobuf(); - preKeyMsg.identityKey = getString(crypto_storage.getStoredPubKey("identityKey")); + preKeyMsg.identityKey = toArrayBuffer(crypto_storage.getStoredPubKey("identityKey")); createNewKeyPair(function(baseKey) { - preKeyMsg.baseKey = getString(baseKey.pubKey); + preKeyMsg.baseKey = toArrayBuffer(baseKey.pubKey); preKeyMsg.preKeyId = deviceObject.preKeyId; initSession(true, baseKey, deviceObject.encodedNumber, deviceObject.identityKey, deviceObject.publicKey, function() { //TODO: Delete preKey info now? session = crypto_storage.getSession(deviceObject.encodedNumber); //TODO: We need to step ratchet here, I think doEncryptPushMessageContent(function(message) { - preKeyMsg.message = getString(message); - callback({type: 3, body: getString(preKeyMsg.encode())}); + preKeyMsg.message = toArrayBuffer(message); + var result = String.fromCharCode((2 << 4) | 2) + getString(preKeyMsg.encode()); + callback({type: 3, body: result}); }); }); }); diff --git a/js/test.js b/js/test.js index 88cb541d..24c73a40 100644 --- a/js/test.js +++ b/js/test.js @@ -201,9 +201,34 @@ sessionKey: hexToArrayBuffer("3d71b56ab9763865905597a90c6746640a946bf3a11632b31a encryptedMessage: hexToArrayBuffer("415a326e6f457937756a6c5355785876342f6b5856346970342b6d45636f636c35424d396c4978364f525948696438634f4a68374c4e2f48534b776a4755556f304e73582f634255742b6a58464b6357697368364b363441315963316f5a47304168676466734e572b53484f313131306e664b6e6c47595445723661624e57556b394c515145706b6f52385746626c5952312b636a4b576d554d5131646f477a376b345955415055544e4d474b78413349694135797575706d6544453173545359552b736133575876366f5a7a624a614275486b5044345a4f3773416b34667558434135466e724e2f462f34445a61586952696f4a76744849413d3d"), }; - // Axolotl test vectors + function axolotlTestVectorsAsBob(v, callback) { + localStorage.clear(); + storage.putEncrypted("25519KeyidentityKey", { pubKey: v.bobIdentityPub, privKey: v.bobIdentityPriv }); + postNaclMessage({command: "privToPub", priv: v.bobPre0}, function(message) { + storage.putEncrypted("25519KeypreKey0", { pubKey: message.res, privKey: v.bobPre0 }); + + if (v.sessionKey !== undefined) { + storage.putEncrypted("signaling_key", v.sessionKey); + var aliceToBob = crypto.decryptWebsocketMessage(v.encryptedMessage); + if (getString(aliceToBob) != getString(v.aliceToBob)) { + callback(false); + return; + } + } + + var b64 = base64EncArr(new Uint8Array(toArrayBuffer(v.aliceToBob))); + var thing = IncomingPushMessageProtobuf.decode(b64); + crypto.handleIncomingPushMessageProto(thing, function(decrypted_message) { + callback(decrypted_message.body == "Hi Bob!" && decrypted_message.attachments.length == 0); + }); + }); + }; + TEST(function(callback) { - var v = axolotlTestVectors; + var v = {}; + for (key in axolotlTestVectors) + v[key] = axolotlTestVectors[key]; + storage.putEncrypted("25519KeyidentityKey", { pubKey: v.aliceIdentityPub, privKey: v.aliceIdentityPriv }); postNaclMessage({command: "privToPub", priv: v.alicePre0}, function(message) { storage.putEncrypted("25519KeypreKey0", { pubKey: message.res, privKey: v.alicePre0 }); @@ -214,40 +239,24 @@ encryptedMessage: hexToArrayBuffer("415a326e6f457937756a6c5355785876342f6b585634 var message = new PushMessageContentProtobuf(); message.body = "Hi Bob!"; crypto.encryptMessageFor(bobsDevice, message, function(encryptedMsg) { - callback(true); + var message = new IncomingPushMessageProtobuf(); + message.message = toArrayBuffer(encryptedMsg.body); + message.type = encryptedMsg.type; + if (message.type != 3) { callback(false); return; } + message.source = "ALICE"; + + delete v['sessionKey']; + v.aliceToBob = getString(message.encode()); + axolotlTestVectorsAsBob(v, callback); }); - /*storage.putEncrypted("signaling_key", v.sessionKey); - var aliceToBob = crypto.decryptWebsocketMessage(v.encryptedMessage); - if (getString(aliceToBob) != getString(v.aliceToBob)) - callback(false); - storage.putEncrypted("25519KeypreKey16777215", { pubKey: message.res, privKey: v.bobLastResort }); - var b64 = base64EncArr(new Uint8Array(v.aliceToBob)); - crypto.handleIncomingPushMessageProto(IncomingPushMessageProtobuf.decode(b64), function(decrypted_message) { - callback(decrypted_message.body == "Hi Bob!" && decrypted_message.attachments.length == 0); - });*/ }); }); - }, "Axolotl test vectors as alice", true); - // Axolotl test vectors TEST(function(callback) { - var v = axolotlTestVectors; - storage.putEncrypted("25519KeyidentityKey", { pubKey: v.bobIdentityPub, privKey: v.bobIdentityPriv }); - postNaclMessage({command: "privToPub", priv: v.bobPre0}, function(message) { - storage.putEncrypted("25519KeypreKey0", { pubKey: message.res, privKey: v.bobPre0 }); - storage.putEncrypted("signaling_key", v.sessionKey); - var aliceToBob = crypto.decryptWebsocketMessage(v.encryptedMessage); - if (getString(aliceToBob) != getString(v.aliceToBob)) - callback(false); - var b64 = base64EncArr(new Uint8Array(toArrayBuffer(aliceToBob))); - crypto.handleIncomingPushMessageProto(IncomingPushMessageProtobuf.decode(b64), function(decrypted_message) { - callback(decrypted_message.body == "Hi Bob!" && decrypted_message.attachments.length == 0); - }); - }); + axolotlTestVectorsAsBob(axolotlTestVectors, callback); }, "Axolotl test vectors as bob", true); - window.setInterval(function() { for (var i = 0; i < maxTestId; i++) { if (testsOutstanding[i] !== undefined) {