Browse Source

KDF (short) vectors pass

Rob Napier 10 years ago
parent
commit
a0a0608660
6 changed files with 109 additions and 7 deletions
  1. 1 1
      Spec
  2. 70 0
      tests/GenVectorTests
  3. 31 0
      tests/Generated/rncryptor-vectors.js
  4. 2 0
      tests/Makefile
  5. 1 1
      tests/rncryptor-test.html
  6. 4 5
      tests/rncryptor-test.js

+ 1 - 1
Spec

@@ -1 +1 @@
-Subproject commit 2726a52e8125ab80de81c417cb3f1d5d01503f5d
+Subproject commit b4b73c65065ed8fe4f94c9ceec2d0b55a5845dd7

+ 70 - 0
tests/GenVectorTests

@@ -0,0 +1,70 @@
+#!/usr/bin/env ruby
+
+require 'optparse'
+require 'json'
+
+require File.join(File.dirname(__FILE__), '../Spec/vectors', 'vectorparser')
+
+@test_files = ["kdf_short"]
+
+@options = {}
+
+# Returns the text for a JS hash from a hash
+def NSDictionaryForHash(hash)
+  "@{\n" + hash.collect { |key, value| %Q(    @"#{key}": @"#{value}") }.join(",\n") + "}"
+end
+
+# Output the file header to output stream
+def outputHeader(output)
+  output << <<-HEADER
+// Automatically Generated by GenVectorTests
+HEADER
+end
+
+# Output the tests for a given filename to the output stream
+def outputTestsForFile(output, name)
+
+  VectorParser.new(@options[:v3_directory] + "/" + name).vectors.each do |vector|
+    output << <<-TEST_CASE
+
+test("#{vector["title"]}", function() {
+  verify_#{name}(#{JSON.dump vector});
+});
+
+TEST_CASE
+
+  end
+end
+
+# Output the footer to the output stream
+def outputFooter(output)
+    output<< <<-FOOTER
+FOOTER
+end
+
+###################
+### MAIN
+###################
+opt_parser = OptionParser.new do |opt|
+  opt.banner = "Usage: GenVectorTest -o VectorTests.m -3 v3_directory"
+  opt.separator  ""
+  opt.on("-o","--output PATH","path to output code") do |output_path|
+    @options[:output_path] = output_path
+  end
+  opt.on("-3", "-3 PATH", "path to v3 directory") do |v3_directory|
+    @options[:v3_directory] = v3_directory
+  end
+end
+
+opt_parser.parse!
+
+raise OptionParser::MissingArgument if @options[:output_path].nil?
+raise OptionParser::MissingArgument if @options[:v3_directory].nil?
+
+File.open(@options[:output_path], "w") do |output|
+  outputHeader(output)
+  @test_files.each do |file|
+    outputTestsForFile(output, file)
+  end
+  outputFooter(output)
+end

+ 31 - 0
tests/Generated/rncryptor-vectors.js

@@ -0,0 +1,31 @@
+// Automatically Generated by GenVectorTests
+
+test("One byte", function() {
+  verify_kdf_short({"title":"One byte","version":"3","password":"a","iterations":"1000","salt_hex":"0102030405060708","key_hex":"d48f10b7 ae39bd25 2bb68e1f af12acea 3474d7d7 702a15b2 ede3246e 82dbb2fd"});
+});
+
+
+test("Short password", function() {
+  verify_kdf_short({"title":"Short password","version":"3","password":"thepassword","iterations":"1000","salt_hex":"0203040506070801","key_hex":"3d3f64cd 31d8057b 5624567a 2caeace4 84adcfda 3fbf6401 a1f03a09 5cee6dd1"});
+});
+
+
+test("Passphrase", function() {
+  verify_kdf_short({"title":"Passphrase","version":"3","password":"this is a bit longer password","iterations":"1000","salt_hex":"0304050607080102","key_hex":"18d42ea5 900f9a94 932a6a5f 10e8f89d 5222ad47 8328ecde f7ba7fe9 1b08240c"});
+});
+
+
+test("Long passphrase", function() {
+  verify_kdf_short({"title":"Long passphrase","version":"3","password":"$$$it was the epoch of belief, it was the epoch of incredulity; it was the season of Light, it was the season of Darkness; it was the spring of hope, it was the winter of despair; we had everything before us, we had nothing before us; we were all going directly to Heaven, we were all going the other way.","iterations":"1000","salt_hex":"0405060708010203","key_hex":"7a7d41ca 9eb92999 23b47d63 6ff18068 1ca5449d f77b7568 2a09fa25 001618db"});
+});
+
+
+test("Multibyte", function() {
+  verify_kdf_short({"title":"Multibyte","version":"3","password":"中文密码","iterations":"1000","salt_hex":"0506070801020304","key_hex":"10ac6088 7fce25a3 5cb96b41 86e85fde 088f2cb5 5bdac89c 5deffad6 95ba8a20"});
+});
+
+
+test("Mixed language", function() {
+  verify_kdf_short({"title":"Mixed language","version":"3","password":"中文密码 with a little English, too.","iterations":"1000","salt_hex":"0607080102030405","key_hex":"1aecc100 8bf00812 1b6bbb30 201b4d62 22cf472c 3f438bcd fc52c0a0 fa03f659"});
+});
+

+ 2 - 0
tests/Makefile

@@ -0,0 +1,2 @@
+Generated/rncryptor-vectors.js: Makefile GenVectorTests ../Spec/vectors/v3/kdf_short
+	./GenVectorTests -o $@ -3 ../Spec/vectors/v3

+ 1 - 1
tests/rncryptor-test.html

@@ -9,9 +9,9 @@
   <div id="qunit"></div>
   <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/sha1.js"></script> -->
   <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/pbkdf2.js"></script>
   <script src="../rncryptor.js"></script>
   <script src="rncryptor-test.js"></script>
+  <script src="Generated/rncryptor-vectors.js"></script>
 </body>
 </html>

+ 4 - 5
tests/rncryptor-test.js

@@ -1,5 +1,4 @@
-test("KDF: One byte", function() {
-  var key = RNCryptor.KeyForPassword("a", CryptoJS.enc.Hex.parse("0102030405060708"));
-  equal(key.toString(), "d48f10b7 ae39bd25 2bb68e1f af12acea 3474d7d7 702a15b2 ede3246e 82dbb2fd".replace(/\s/g,''));
-});
-
+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,''));
+}