services/sync/tests/unit/test_utils_deriveKey.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 Cu.import("resource://services-crypto/WeaveCrypto.js");
michael@0 2 Cu.import("resource://services-sync/util.js");
michael@0 3
michael@0 4 let cryptoSvc = new WeaveCrypto();
michael@0 5
michael@0 6 function run_test() {
michael@0 7 if (this.gczeal) {
michael@0 8 _("Running deriveKey tests with gczeal(2).");
michael@0 9 gczeal(2);
michael@0 10 } else {
michael@0 11 _("Running deriveKey tests with default gczeal.");
michael@0 12 }
michael@0 13
michael@0 14 var iv = cryptoSvc.generateRandomIV();
michael@0 15 var der_passphrase = "secret phrase";
michael@0 16 var der_salt = "RE5YUHpQcGl3bg=="; // btoa("DNXPzPpiwn")
michael@0 17
michael@0 18 _("Testing deriveKeyFromPassphrase. Input is \"" + der_passphrase + "\", \"" + der_salt + "\" (base64-encoded).");
michael@0 19
michael@0 20 // Test friendly-ing.
michael@0 21 do_check_eq("abcdefghijk8mn9pqrstuvwxyz234567",
michael@0 22 Utils.base32ToFriendly("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"));
michael@0 23 do_check_eq("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
michael@0 24 Utils.base32FromFriendly(
michael@0 25 Utils.base32ToFriendly("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567")));
michael@0 26
michael@0 27 // Test translation.
michael@0 28 do_check_false(Utils.isPassphrase("o-5wmnu-o5tqc-7lz2h-amkbw-izqzi")); // Wrong charset.
michael@0 29 do_check_false(Utils.isPassphrase("O-5WMNU-O5TQC-7LZ2H-AMKBW-IZQZI")); // Wrong charset.
michael@0 30 do_check_true(Utils.isPassphrase("9-5wmnu-95tqc-78z2h-amkbw-izqzi"));
michael@0 31 do_check_true(Utils.isPassphrase("9-5WMNU-95TQC-78Z2H-AMKBW-IZQZI")); // isPassphrase normalizes.
michael@0 32 do_check_true(Utils.isPassphrase(
michael@0 33 Utils.normalizePassphrase("9-5WMNU-95TQC-78Z2H-AMKBW-IZQZI")));
michael@0 34
michael@0 35 // Base64. We don't actually use this in anger, particularly not with a 32-byte key.
michael@0 36 var der_key = Utils.deriveEncodedKeyFromPassphrase(der_passphrase, der_salt);
michael@0 37 _("Derived key in base64: " + der_key);
michael@0 38 do_check_eq(cryptoSvc.decrypt(cryptoSvc.encrypt("bacon", der_key, iv), der_key, iv), "bacon");
michael@0 39
michael@0 40 // Base64, 16-byte output.
michael@0 41 var der_key = Utils.deriveEncodedKeyFromPassphrase(der_passphrase, der_salt, 16);
michael@0 42 _("Derived key in base64: " + der_key);
michael@0 43 do_check_eq("d2zG0d2cBfXnRwMUGyMwyg==", der_key);
michael@0 44 do_check_eq(cryptoSvc.decrypt(cryptoSvc.encrypt("bacon", der_key, iv), der_key, iv), "bacon");
michael@0 45
michael@0 46 // Base32. Again, specify '16' to avoid it generating a 256-bit key string.
michael@0 47 var b32key = Utils.derivePresentableKeyFromPassphrase(der_passphrase, der_salt, 16);
michael@0 48 var hyphenated = Utils.hyphenatePassphrase(b32key);
michael@0 49 do_check_true(Utils.isPassphrase(b32key));
michael@0 50
michael@0 51 _("Derived key in base32: " + b32key);
michael@0 52 do_check_eq(b32key.length, 26);
michael@0 53 do_check_eq(hyphenated.length, 31); // 1 char, plus 5 groups of 5, hyphenated = 5 + (5*5) + 1 = 31.
michael@0 54 do_check_eq(hyphenated, "9-5wmnu-95tqc-78z2h-amkbw-izqzi");
michael@0 55
michael@0 56 if (this.gczeal)
michael@0 57 gczeal(0);
michael@0 58
michael@0 59 // Test the equivalence of our NSS and JS versions.
michael@0 60 // Will only work on FF4, of course.
michael@0 61 // Note that we don't add gczeal here: the pure-JS implementation is
michael@0 62 // astonishingly slow, and this check takes five minutes to run.
michael@0 63 do_check_eq(
michael@0 64 Utils.deriveEncodedKeyFromPassphrase(der_passphrase, der_salt, 16, false),
michael@0 65 Utils.deriveEncodedKeyFromPassphrase(der_passphrase, der_salt, 16, true));
michael@0 66 }

mercurial