rncryptor.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. var RNCryptor = {};
  2. /*
  3. Takes password string and salt WordArray
  4. Returns key bitArray
  5. */
  6. RNCryptor.KeyForPassword = function(password, salt) {
  7. var hmacSHA1 = function (key) {
  8. var hasher = new sjcl.misc.hmac(key, sjcl.hash.sha1);
  9. this.encrypt = function () {
  10. return hasher.encrypt.apply(hasher, arguments);
  11. };
  12. };
  13. return sjcl.misc.pbkdf2(password, salt, 1000, 32 * 8, hmacSHA1);
  14. }
  15. /*
  16. Takes password string and plaintext bitArray
  17. options:
  18. iv
  19. encryption_salt
  20. html_salt
  21. Returns ciphertext bitArray
  22. */
  23. RNCryptor.Encrypt = function(password, plaintext, options) {
  24. options = options || {}
  25. var encryption_salt = options["encryption_salt"] || sjcl.random.randomWords(8 / 4); // FIXME: Need to seed PRNG
  26. var encryption_key = RNCryptor.KeyForPassword(password, encryption_salt);
  27. var hmac_salt = options["hmac_salt"] || sjcl.random.randomWords(8 / 4);
  28. var hmac_key = RNCryptor.KeyForPassword(password, hmac_salt);
  29. var iv = options["iv"] || sjcl.random.randomWords(16 / 4);
  30. var version = sjcl.codec.hex.toBits("03");
  31. var options = sjcl.codec.hex.toBits("01");
  32. var message = sjcl.bitArray.concat(version, options);
  33. message = sjcl.bitArray.concat(message, encryption_salt);
  34. message = sjcl.bitArray.concat(message, hmac_salt);
  35. message = sjcl.bitArray.concat(message, iv);
  36. var p = sjcl.json.defaults;
  37. p.iv = iv;
  38. p.mode = "cbc";
  39. var aes = new sjcl.cipher.aes(encryption_key);
  40. sjcl.beware["CBC mode is dangerous because it doesn't protect message integrity."]();
  41. var encrypted = sjcl.mode[p.mode].encrypt(aes, plaintext, p.iv);
  42. message = sjcl.bitArray.concat(message, encrypted);
  43. var hmac = new sjcl.misc.hmac(hmac_key).encrypt(message);
  44. message = sjcl.bitArray.concat(message, hmac);
  45. return message;
  46. }
  47. RNCryptor.Decrypt = function(password, message, options) {
  48. options = options || {}
  49. var version = sjcl.bitArray.extract(message, 0 * 8, 8);
  50. var options = sjcl.bitArray.extract(message, 1 * 8, 8);
  51. var encryption_salt = sjcl.bitArray.bitSlice(message, 2 * 8, 10 * 8);
  52. var encryption_key = RNCryptor.KeyForPassword(password, encryption_salt);
  53. var hmac_salt = sjcl.bitArray.bitSlice(message, 10 * 8, 18 * 8);
  54. var hmac_key = RNCryptor.KeyForPassword(password, hmac_salt);
  55. var iv = sjcl.bitArray.bitSlice(message, 18 * 8, 34 * 8);
  56. var ciphertext_end = sjcl.bitArray.bitLength(message) - (32 * 8);
  57. var ciphertext = sjcl.bitArray.bitSlice(message, 34 * 8, ciphertext_end);
  58. var hmac = sjcl.bitArray.bitSlice(message, ciphertext_end);
  59. var expected_hmac = new sjcl.misc.hmac(hmac_key).encrypt(sjcl.bitArray.bitSlice(message, 0, ciphertext_end));
  60. // .equal is of consistent time
  61. if (! sjcl.bitArray.equal(hmac, expected_hmac)) {
  62. throw new sjcl.exception.corrupt("HMAC mismatch or bad password.");
  63. }
  64. var p = sjcl.json.defaults;
  65. p.iv = iv;
  66. p.mode = "cbc";
  67. var aes = new sjcl.cipher.aes(encryption_key);
  68. sjcl.beware["CBC mode is dangerous because it doesn't protect message integrity."]();
  69. var decrypted = sjcl.mode[p.mode].decrypt(aes, ciphertext, p.iv);
  70. return decrypted;
  71. }