Browse Source

Pass all short-password tests

Rob Napier 10 years ago
parent
commit
3f09884dd7
7 changed files with 84 additions and 2 deletions
  1. 1 1
      .travis.yml
  2. 38 0
      rncryptor.js
  3. 1 1
      tests/GenVectorTests
  4. 27 0
      tests/Generated/rncryptor-vectors.js
  5. 3 0
      tests/Makefile
  6. 3 0
      tests/rncryptor-test.html
  7. 11 0
      tests/rncryptor-test.js

+ 1 - 1
.travis.yml

@@ -1,2 +1,2 @@
 language: node_js
-script: phantomjs tests/testrunner.js tests/rncryptor-test.html
+script: make -C tests

+ 38 - 0
rncryptor.js

@@ -2,7 +2,45 @@ var RNCryptor = {};
 
 /*
     Takes password string and salt WordArray
+    Returns key WordArray
 */
 RNCryptor.KeyForPassword = function(password, salt) {
     return CryptoJS.PBKDF2(password, salt, { keySize: 256/32, iterations: 1000 });
+}
+
+/*
+  Takes password string and plaintext WordArray
+  options:
+    iv
+    encryption_salt
+    html_salt
+  Returns ciphertext WordArray
+*/
+RNCryptor.Encrypt = function(password, plaintext, options) {
+  options = options || {}
+  var encryption_salt = options["encryption_salt"] || CryptoJS.lib.WordArray.random(64/8);
+  var encryption_key = RNCryptor.KeyForPassword(password, encryption_salt);
+
+  var hmac_salt = options["hmac_salt"] || CryptoJS.lib.WordArray.random(64/8)
+  var hmac_key = RNCryptor.KeyForPassword(password, hmac_salt);
+
+  var iv = options["iv"] || CryptoJS.lib.WordArray.random(64/8)
+
+  var version = CryptoJS.enc.Hex.parse("03");
+  var options = CryptoJS.enc.Hex.parse("01");
+  
+  var message = version.clone();
+  message.concat(options);
+  message.concat(encryption_salt);
+  message.concat(hmac_salt);
+  message.concat(iv);
+
+  var encrypted = CryptoJS.AES.encrypt(plaintext, encryption_key, {iv: iv});
+  message.concat(encrypted.ciphertext);
+
+  var hmac = CryptoJS.HmacSHA256(message, hmac_key);
+
+  message.concat(hmac);
+
+  return message;
 }

+ 1 - 1
tests/GenVectorTests

@@ -5,7 +5,7 @@ require 'json'
 
 require File.join(File.dirname(__FILE__), '../Spec/vectors', 'vectorparser')
 
-@test_files = ["kdf_short"]
+@test_files = ["kdf_short", "password_short"]
 
 @options = {}
 

File diff suppressed because it is too large
+ 27 - 0
tests/Generated/rncryptor-vectors.js


+ 3 - 0
tests/Makefile

@@ -1,2 +1,5 @@
+test: Generated/rncryptor-vectors.js
+	phantomjs runner.js rncryptor-test.html
+
 Generated/rncryptor-vectors.js: Makefile GenVectorTests ../Spec/vectors/v3/kdf_short
 	./GenVectorTests -o $@ -3 ../Spec/vectors/v3

+ 3 - 0
tests/rncryptor-test.html

@@ -10,6 +10,9 @@
   <div id="qunit-fixture"></div>
   <script src="http://code.jquery.com/qunit/qunit-1.13.0.js"></script>
   <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/pbkdf2.js"></script>
+  <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
+  <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>
+  <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha256.js"></script>
   <script src="../rncryptor.js"></script>
   <script src="rncryptor-test.js"></script>
   <script src="Generated/rncryptor-vectors.js"></script>

+ 11 - 0
tests/rncryptor-test.js

@@ -2,3 +2,14 @@ var verify_kdf_short = function(vector) {
   var key = RNCryptor.KeyForPassword(vector["password"], CryptoJS.enc.Hex.parse(vector["salt_hex"]));
   equal(key.toString(), vector["key_hex"].replace(/\s/g,''));
 }
+
+var verify_password_short = function(vector) {
+  var ciphertext = RNCryptor.Encrypt(vector["password"], 
+                                     CryptoJS.enc.Hex.parse(vector["plaintext_hex"].replace(/\s/g,'')), 
+                                     { "encryption_salt": CryptoJS.enc.Hex.parse(vector["enc_salt_hex"].replace(/\s/g,'')),
+                                       "hmac_salt": CryptoJS.enc.Hex.parse(vector["hmac_salt_hex"].replace(/\s/g,'')),
+                                       "iv": CryptoJS.enc.Hex.parse(vector["iv_hex"].replace(/\s/g,''))
+                                     });
+
+  equal(ciphertext.toString(), vector["ciphertext_hex"].replace(/\s/g,''));
+}

Some files were not shown because too many files changed in this diff