PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/services/sync/tests/unit/test_utils_deriveKey.js

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